mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 09:33:15 +00:00
Merge main into craft-stream-codecs/jbig2-decode
main has #953 as this branch's base had it; this branch's side of crates/stream-codecs is kept.
This commit is contained in:
Generated
+177
-3
@@ -369,6 +369,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit_field"
|
||||
version = "0.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@@ -926,7 +932,7 @@ version = "0.1.0"
|
||||
name = "craft-cms"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"md-5",
|
||||
"md-5 0.10.6",
|
||||
"moxcms",
|
||||
"rayon",
|
||||
]
|
||||
@@ -1025,16 +1031,27 @@ name = "craft-epub"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"flate2",
|
||||
"craft-zip",
|
||||
"thiserror 2.0.21",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ews"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"proptest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-exr"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"exr",
|
||||
"half",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1180,16 +1197,32 @@ dependencies = [
|
||||
"writeable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ical"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"proptest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-idml"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"flate2",
|
||||
"craft-zip",
|
||||
"thiserror 2.0.21",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jmap"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
@@ -1275,6 +1308,18 @@ dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-tiff",
|
||||
"proptest",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metafile"
|
||||
version = "0.1.0"
|
||||
@@ -1371,6 +1416,7 @@ dependencies = [
|
||||
name = "craft-pdf"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"bytemuck",
|
||||
"hayro",
|
||||
"hayro-cmap",
|
||||
@@ -1378,9 +1424,23 @@ dependencies = [
|
||||
"hayro-syntax",
|
||||
"krilla",
|
||||
"kurbo 0.13.1",
|
||||
"md-5 0.11.0",
|
||||
"proptest",
|
||||
"sha2 0.11.1",
|
||||
"stringprep",
|
||||
"thiserror 2.0.21",
|
||||
"ttf-parser",
|
||||
"wasm-bindgen-test",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-pdn"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"proptest",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1538,7 +1598,9 @@ dependencies = [
|
||||
name = "craft-rtf"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-mime",
|
||||
"encoding_rs",
|
||||
"proptest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1552,6 +1614,10 @@ dependencies = [
|
||||
"toml_edit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-sheet"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "craft-silhouette-studio"
|
||||
version = "0.1.0"
|
||||
@@ -1753,6 +1819,14 @@ dependencies = [
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-webp"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-window"
|
||||
version = "0.1.0"
|
||||
@@ -1771,11 +1845,43 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-wordmodel"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"log",
|
||||
"proptest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xcf"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xls"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-cfb",
|
||||
"craft-sheet",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xlsx"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-opc",
|
||||
"craft-sheet",
|
||||
"craft-xml",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
@@ -2397,6 +2503,21 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "exr"
|
||||
version = "1.74.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6"
|
||||
dependencies = [
|
||||
"bit_field",
|
||||
"half",
|
||||
"lebe",
|
||||
"miniz_oxide 0.8.9",
|
||||
"multiversion",
|
||||
"smallvec",
|
||||
"zune-inflate",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "extended"
|
||||
version = "0.1.0"
|
||||
@@ -3980,6 +4101,12 @@ version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
|
||||
|
||||
[[package]]
|
||||
name = "lebe"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.190"
|
||||
@@ -4095,6 +4222,16 @@ dependencies = [
|
||||
"digest 0.10.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "md-5"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69b6441f590336821bb897fb28fc622898ccceb1d6cea3fde5ea86b090c4de98"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"digest 0.11.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
@@ -4194,6 +4331,28 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multiversion"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076"
|
||||
dependencies = [
|
||||
"multiversion-macros",
|
||||
"target-features",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multiversion-macros"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
"target-features",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multiversion_no_op"
|
||||
version = "1.0.0"
|
||||
@@ -6643,6 +6802,12 @@ dependencies = [
|
||||
"xattr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "target-features"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5"
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.27.0"
|
||||
@@ -8477,6 +8642,15 @@ version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b"
|
||||
|
||||
[[package]]
|
||||
name = "zune-inflate"
|
||||
version = "0.2.54"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02"
|
||||
dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-jpeg"
|
||||
version = "0.5.15"
|
||||
|
||||
+10
@@ -17,6 +17,7 @@ craft-aep = { path = "crates/aep" }
|
||||
craft-affinity = { path = "crates/affinity" }
|
||||
craft-ai-core = { path = "crates/ai-core" }
|
||||
craft-pdf = { path = "crates/pdf", version = "=0.1.0" }
|
||||
craft-pdn = { path = "crates/pdn" }
|
||||
craft-plate-test = { path = "crates/plate-test" }
|
||||
krilla = { version = "=0.8.2", default-features = false, features = ["raster-images"] }
|
||||
hayro = { version = "=0.7.1", default-features = false }
|
||||
@@ -55,6 +56,8 @@ craft-fig = { path = "crates/fig" }
|
||||
craft-fonts-build = { path = "crates/fonts-build" }
|
||||
craft-i18n = { path = "crates/i18n" }
|
||||
craft-idml = { path = "crates/idml" }
|
||||
craft-jmap = { path = "crates/jmap" }
|
||||
craft-ical = { path = "crates/ical" }
|
||||
craft-jpeg = { path = "crates/jpeg" }
|
||||
craft-kiwi = { path = "crates/kiwi" }
|
||||
craft-keymap = { path = "crates/keymap" }
|
||||
@@ -86,6 +89,7 @@ craft-pst = { path = "crates/pst" }
|
||||
craft-quit = { path = "crates/quit" }
|
||||
craft-rtf = { path = "crates/rtf" }
|
||||
craft-raw = { path = "crates/raw" }
|
||||
craft-sheet = { path = "crates/sheet" }
|
||||
craft-sketch = { path = "crates/sketch" }
|
||||
craft-settings = { path = "crates/settings" }
|
||||
craft-silhouette-studio = { path = "crates/silhouette-studio" }
|
||||
@@ -96,20 +100,26 @@ craft-text = { path = "crates/text" }
|
||||
craft-tga = { path = "crates/tga" }
|
||||
craft-timeline = { path = "crates/timeline" }
|
||||
craft-tiff = { path = "crates/tiff" }
|
||||
craft-metadata = { path = "crates/metadata" }
|
||||
craft-ui = { path = "crates/ui" }
|
||||
craft-update = { path = "crates/update" }
|
||||
craft-vcard = { path = "crates/vcard" }
|
||||
craft-vectorcraft-format = { path = "crates/vectorcraft-format" }
|
||||
craft-vectordrawable = { path = "crates/vectordrawable" }
|
||||
craft-xls = { path = "crates/xls" }
|
||||
craft-wordmodel = { path = "crates/wordmodel" }
|
||||
craft-xlsx = { path = "crates/xlsx" }
|
||||
craft-xcf = { path = "crates/xcf" }
|
||||
craft-xml = { path = "crates/xml" }
|
||||
craft-zip = { path = "crates/zip" }
|
||||
craft-y4m = { path = "crates/y4m" }
|
||||
craft-epub = { path = "crates/epub" }
|
||||
craft-ews = { path = "crates/ews" }
|
||||
craft-video = { path = "crates/video" }
|
||||
craft-web-api = { path = "crates/web-api" }
|
||||
craft-window = { path = "crates/window" }
|
||||
craft-web-host = { path = "crates/web-host" }
|
||||
craft-webp = { path = "crates/webp" }
|
||||
egui = { version = "0.37", default-features = false }
|
||||
md5 = { package = "md-5", version = "0.10", default-features = false }
|
||||
rayon = "1"
|
||||
|
||||
@@ -93,7 +93,7 @@ crates/raw (craft-raw) is moved from LightCraft, with behaviour ported from two
|
||||
- LightCraft, crates/raw (src/lib.rs, dng.rs, dngwrite.rs, tiffraw.rs, unpack.rs, ljpeg.rs, jxl.rs,
|
||||
opcodes.rs, profile.rs, gaintable.rs, semantic.rs, preview.rs, vendor/crw.rs, vendor/hasselblad.rs,
|
||||
vendor.rs (shared level helpers), vendor/mrw.rs, vendor/erf.rs, vendor/mef.rs, vendor/kodak.rs, vendor/pef.rs,
|
||||
vendor/srw.rs, vendor/cr2.rs, vendor/cr2/sraw.rs, kelvin_presets.rs (the preset ordering), tests/it/vendor_robust.rs and their tests) and crates/meta/src/mrw.rs (the MRW block container),
|
||||
vendor/srw.rs, vendor/cr2.rs, vendor/cr2/sraw.rs, vendor/nef.rs, vendor/nefc.rs, vendor/nef_lens.rs, docs/nikon-lens-corrections.md, kelvin_presets.rs (the preset ordering), tests/it/vendor_robust.rs and their tests) and crates/meta/src/mrw.rs (the MRW block container),
|
||||
revision 5ff567a4a2e067bef1b6bce152c11e7f727ccbaf.
|
||||
https://github.com/storytold/lightcraft
|
||||
Copyright (c) 2026 ArtCraft Team and the LightCraft contributors.
|
||||
|
||||
@@ -34,6 +34,7 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
- [`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-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.
|
||||
- [`craft-assuan`](crates/assuan/README.md): the Assuan protocol and gpg-agent's operations (key lookup, signing a digest, decrypting a session key) over any `Read + Write` stream, with a bounded canonical S-expression reader. No sockets; everything the agent sends is capped.
|
||||
- [`craft-affinity`](crates/affinity/README.md): bounded reader for native Affinity documents (`.af`, `.afdesign`, `.afphoto`, `.afpub`): archive, object stream and a neutral document model with layers, artboards, vectors, text, tables, images and masks. Read only; mapping onto an app's document stays in the app.
|
||||
@@ -78,7 +79,7 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
- [`craft-dxf`](crates/dxf/src/lib.rs): DXF tag reader and writer, from CADCraft: ASCII and
|
||||
binary DXF as (group code, value) tags, split into sections and entities, with pre-2007 code
|
||||
pages and version detection, and written back; and `model`, the drawing above the tags (header
|
||||
variables, tables, blocks, layouts), merged from CADCraft's and VectorCraft's readers. Bounded
|
||||
variables, tables, blocks, layouts, entities), merged from CADCraft's and VectorCraft's readers. Bounded
|
||||
on hostile input. Mapping the model onto a document stays in each app.
|
||||
- [`craft-ecproj-format`](crates/ecproj-format/README.md): the envelope of EffectCraft `.ecproj`
|
||||
project files: `savedBy`, the schema version and its check, and the file object with the
|
||||
@@ -88,9 +89,14 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
and message, images, and the vector network and geometry blobs. Bounded on hostile input.
|
||||
Carved from FigCraft.
|
||||
- [`craft-epub`](crates/epub/README.md): EPUB packages: the OCF ZIP container, the package document (metadata, manifest, spine) and the navigation document, written as EPUB 3 (reflowable and fixed layout) and read (EPUB 2 and 3) with bounded entry sizes. Content documents are the caller's.
|
||||
- [`craft-ews`](crates/ews/README.md): the SOAP layer of Exchange Web Services, from MailCraft, without
|
||||
sockets: the request envelope and an escaping element builder, and the answer envelope, faults
|
||||
and response messages with their codes, read with `craft-xml`. The operations and everything
|
||||
over HTTP stay in each app.
|
||||
- [`craft-exr`](crates/exr/src/lib.rs): OpenEXR pieces shared by PhotoCraft and EffectCraft: layer
|
||||
and channel names, the channels that make a colour picture, and Cryptomatte IDs, metadata keys,
|
||||
manifests and rank channels. Takes names and attribute text; decoding is not part of it yet.
|
||||
manifests and rank channels. The optional `decode` feature adds bounded part listing, flat
|
||||
decoding (scan lines, tiles, every compression of the `exr` crate) and writing.
|
||||
- [`craft-font-catalog`](crates/font-catalog/README.md): the installed fonts on every platform
|
||||
(font folders, fontconfig and Flatpak host folders, CoreText, DirectWrite, the font store),
|
||||
lookup by family, style, localized, legacy, full and PostScript names, family loads shared
|
||||
@@ -134,8 +140,7 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
what PhotoCraft's and FilmCraft's readers added: container parsing and decompression to a neutral
|
||||
raw image (samples, CFA, black and white levels, active area, crop, orientation, colour tags,
|
||||
opcode lists, embedded previews), one Cargo feature per format. DNG reading and writing,
|
||||
Hasselblad 3FR/FFF, Minolta MRW, Epson ERF, Mamiya MEF, Kodak DCS, Pentax PEF, Samsung SRW, Canon CR2 and Canon CRW (through
|
||||
Hasselblad 3FR/FFF, Minolta MRW, Epson ERF, Mamiya MEF, Kodak DCS, Pentax PEF, Samsung SRW and Canon CRW (through
|
||||
Hasselblad 3FR/FFF, Minolta MRW, Epson ERF, Mamiya MEF, Kodak DCS, Pentax PEF, Samsung SRW, Canon CR2, Nikon NEF/NRW and Canon CRW (through
|
||||
`craft-crw`) so far; demosaicing and everything after stay with the develop
|
||||
pipeline.
|
||||
- [`craft-tga`](crates/tga/src/lib.rs): Truevision Targa, from FilmCraft (reader) and PhotoCraft
|
||||
@@ -143,12 +148,20 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
origin, to RGBA8, and a run-length writer whose packets end with their scan line. Bounded on
|
||||
hostile input.
|
||||
- [`craft-idml`](crates/idml/README.md): IDML (InDesign Markup Language) packages: the ZIP container read with bounded part sizes and lenient part names, `designmap.xml` with its includes in place, the `<Properties>` form on craft-xml elements, value codecs and the package writer; and InDesign Tagged Text, read and written.
|
||||
- [`craft-ical`](crates/ical/README.md): iCalendar (RFC 5545) building blocks, from MailCraft: content
|
||||
lines, DATE/DATE-TIME values, durations, TEXT escaping, the common subset of recurrence rules,
|
||||
and `VTIMEZONE` definitions read and written. No time-zone database; what events and to-dos
|
||||
mean stays in each app. Bounded and panic-free on hostile input.
|
||||
- [`craft-stream-codecs`](crates/stream-codecs/README.md): byte-stream and bi-level image codecs that
|
||||
PDF, PostScript and TIFF share: LZW, ASCII85, ASCIIHex, RunLength, TIFF and PNG predictors,
|
||||
CCITT Group 3 and 4 decoding and Group 4 encoding, JBIG2 decoding and lossless encoding. Every
|
||||
decoder is bounded and returns its partial output on damage. From PdfCraft; a PDF filter chain
|
||||
is built on it in `craft-pdf`.
|
||||
- [`craft-timeline`](crates/timeline/README.md): editorial timeline interchange: exact media time and SMPTE timecode, import/export reports, media path and `file://` URL handling, Avid Log Exchange (ALE) and CMX 3600 EDL documents, format detection. From FilmCraft.
|
||||
- [`craft-metadata`](crates/metadata/README.md): photo metadata, from LightCraft: EXIF (read and write,
|
||||
maker-note lens and focus point, every tag by name), IPTC-IIM, XMP packets (read, write, and a merge
|
||||
that keeps what another program wrote), GPX track logs, and the blocks that carry them in JPEG, PNG,
|
||||
WebP, TIFF-based, RAF and CR3 files. One `Metadata` model for all of them. Built on `craft-tiff`.
|
||||
- [`craft-tiff`](crates/tiff/README.md): TIFF and BigTIFF IFD reader and writer, from LightCraft:
|
||||
every field type, the IFD chain, SubIFDs and Exif/GPS/Interop pointer IFDs, maker-note IFDs
|
||||
(Canon, Nikon, Sony, Fujifilm, Olympus, Panasonic, Pentax), strip and tile layout. Bounded and
|
||||
@@ -164,6 +177,10 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
primitives (`.abr`, `.asl`, `.aco`, `.act`, `.ase`, `.grd`, `.pat`, `.atn`, `.csh`, `.shc`) and
|
||||
the Photoshop blocks of a layered TIFF. Bounded and panic-free on hostile input. Compositing
|
||||
and the mapping onto a document stay in each app.
|
||||
- [`craft-jmap`](crates/jmap/README.md): the JMAP protocol core (RFC 8620), from MailCraft, without
|
||||
sockets: session, requests of method calls with back-references, responses, ids, address
|
||||
templates, credentials and the judgement of HTTP answers, over any HTTP implementation. Bounded
|
||||
on hostile answers; what a program asks and makes of the answers stays in it.
|
||||
- [`craft-jpeg`](crates/jpeg/README.md): clean-room decoder for 12-bit and arithmetic-coded
|
||||
JPEG (T.81 SOF0/SOF1 at 12 bits, SOF9, SOF10), from PhotoCraft, and a frame probe for routing.
|
||||
Metadata stays in each app.
|
||||
@@ -246,6 +263,7 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
(`\uN`, `\'hh`, single-byte and East Asian multibyte pages), escaping and dates, and a reader and writer
|
||||
for a small neutral model (paragraphs, character formatting, outline levels, tables).
|
||||
- [`craft-settings`](crates/settings/README.md): `config.toml` settings storage for every app: platform folders, portable mode and overrides, atomic saves while running, backup, damaged-file recovery and schema migrations. Policy in [docs/settings-storage.md](docs/settings-storage.md).
|
||||
- [`craft-sheet`](crates/sheet/README.md): spreadsheet vocabulary shared by `craft-xls`, `craft-xlsx` and `craft-ods`: cell positions and A1 text, cell values and error values, sheet-name quoting, Excel's built-in number formats and indexed colour palette. Not a file format; no dependencies.
|
||||
- [`craft-silhouette-studio`](crates/silhouette-studio/src/lib.rs): bounded, fail-closed reader for Silhouette Studio `.studio` and `.studio3` documents: outlines, line colours and page size in millimetres. Protected designs are refused.
|
||||
- [`craft-sketch`](crates/sketch/README.md): clean-room reader for the `.sketch` container (Sketch 48 and newer): the ZIP archive, the version cut, `document.json`, the pages in document order and images, as the JSON the file holds. Bounded on hostile input. Carved from FigCraft.
|
||||
- [`craft-svg`](crates/svg/src/lib.rs): SVG. `sniff` recognises SVG and SVGZ data; `svgz` unpacks
|
||||
@@ -280,12 +298,21 @@ craft-<name> = { git = "https://github.com/storytold/craft-libs", rev = "<commit
|
||||
`window.<app>` shape (`request`, `execute`, `commands`, `docInspect`, `uiInspect`, `screenshot`) over the app's
|
||||
JSON control channel.
|
||||
- [`craft-web-host`](crates/web-host/README.md): the host messages between a Craft app's web build and the page that hosts it (`?host=parent` or `data-host="page"`): one contract and one bridge for every app.
|
||||
- [`craft-webp`](crates/webp/src/lib.rs): the WebP container, from EffectCraft (reader) and
|
||||
PhotoCraft (writer): canvas size, alpha and every animation frame's timing from the chunk
|
||||
headers without decoding, and the RIFF container around an encoded VP8 frame with alpha, ICC,
|
||||
EXIF and XMP chunks. The VP8 and VP8L codecs are not part of it.
|
||||
- [`craft-xls`](crates/xls/README.md): clean-room Excel 97-2003 workbook (`.xls`, BIFF8) reader: worksheets, cell values, formulas as A1 text (shared and array formulas included), cell formats with resolved palette colours, merged cells, column and row records, defined names, print areas and calculation settings, with limits on records, cells and text.
|
||||
- [`craft-wordmodel`](crates/wordmodel/README.md): a word-processing document model (stories, paragraphs,
|
||||
runs, tables, sections, styles, numbering, fields, notes, comments, revisions, drawings, math): what the
|
||||
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-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.
|
||||
- [`craft-kra`](crates/kra/README.md): Krita documents (`.kra`, `.krz`), read: the ZIP archive, the `maindoc.xml` node tree as written, and the tiled pixel files with their LZF compression, a tile at a time. From PhotoCraft.
|
||||
- [`craft-xcf`](crates/xcf/README.md): GIMP XCF images (`.xcf`, `.xcf.gz`), read: header, properties, parasites, layers with their item paths, channels, and tiled pixels (uncompressed, RLE, zlib) at every precision, under a caller's byte budget. From PhotoCraft.
|
||||
- [`craft-xml`](crates/xml/README.md): bounded XML for hostile input: an owned element tree with limits on size, depth, nodes and attributes, streaming of repeated elements, namespace handling (as written, by local name, or canonical prefixes by URI), escaping and a writer. No DTDs and no entity expansion.
|
||||
- [`craft-zip`](crates/zip/README.md): ZIP archives from untrusted files: an entry-count check before the archive is indexed (ZIP64 included), entry reads within per-entry and total budgets that count real bytes, duplicate-name detection, a strict structure preflight, and a deterministic stored/deflate writer. The shared ZIP layer for the package formats.
|
||||
- [Shared UI component audit](docs/ui-components-audit.md): source inventory, extraction priorities
|
||||
|
||||
@@ -24,6 +24,13 @@ The XML form of a project (`.aepx`) is read into the same tree by `read_aepx` (a
|
||||
XML tokenizer of its own); `parse_any` picks the reader by content, so the typed view takes
|
||||
either form. The mapping is in [`docs/aep-format.md`](docs/aep-format.md), section 1.
|
||||
|
||||
A reader that only looks does not need the owned tree. `TreeIndex` is the same tree as one flat,
|
||||
read-only array in depth-first order, borrowed from the input: `TreeIndex::parse` for an `.aep`,
|
||||
`index_aepx` and `TreeIndex::from_drafts` for an `.aepx` (decoded into one buffer first). It
|
||||
counts the same chunks against the same `Limits` and returns the same errors as `parse`, and the
|
||||
typed view reads through either (`Project::new` for the owned tree, `Project::from_index` for the
|
||||
index). An importer should use the index; it is about twice as fast and allocates far less.
|
||||
|
||||
Bodies that look like lists but are not chunk sequences (for example `LIST btdk`, raw text) stay
|
||||
opaque rather than failing the parse. Input is untrusted: sizes are checked against the remaining
|
||||
input, nesting and chunk count are bounded (`Limits`), nothing is allocated from a declared size,
|
||||
|
||||
@@ -121,6 +121,11 @@ counterpart (a name that is not 1 to 4 characters) are skipped with their conten
|
||||
with the byte offset. It runs without recursion; the nesting depth (`Limits::max_depth`) and the
|
||||
element count (`Limits::max_chunks`) are bounded, and everything it allocates is bounded by the
|
||||
size of the input.
|
||||
- There are two readers. `read_aepx` builds the owned tree element by element. The importer, which
|
||||
only reads, uses a second reader that decodes every body (hex, text, the alias JSON, the
|
||||
numbers) into one buffer and records the chunks as a flat depth-first list whose bodies are
|
||||
ranges of that buffer; it indexes that buffer (`TreeIndex`) as it indexes an `.aep`. The tests
|
||||
hold both readers to the same tree and the same errors.
|
||||
- Tests: `tests/aepx.rs` compares the five fixture pairs chunk by chunk and byte by byte, compares
|
||||
the typed view of both forms, and runs truncated, bit-flipped, deeply nested and oversized
|
||||
inputs; `crates/aep/tests/it/import/aepx.rs` imports both forms of each pair and expects the same
|
||||
@@ -139,8 +144,8 @@ write on their own.
|
||||
|---|---|
|
||||
| `svap` (4 bytes, the first top-level chunk) is the version of the After Effects that **wrote** the file: `0f 11 0e 02` in all 36 (a down-saved copy keeps the 2026 word) and in all 1 192 other `.aep` files of the local corpus folders. After Effects opens a file without `svap` (`t-nosvap`). | All 36; corpus scan. |
|
||||
| `head` bytes 4 to 7 are the version the file is **written for**: `0f 11 0e 02` (= `svap`) in the 2026 saves, `0f 0b 06 61` in the 25.x copies, `0f 03 06 41` in the 24.x copies, the same word in every family. After Effects refuses a file whose word is zero ("created with an older version [0.0x0]", `u-zhead`). | All 36; `u-zhead`. |
|
||||
| The word is packed (bit 31 first): bits 25-24 and 21-19 the major, 18-15 the minor, 14-11 the bug-fix, 10-8 a stage (6 in all), 7-0 the build; bits 31-26 (`000011`) and 23-22 (`00`) are the same in all samples and not decoded. `0f110e02` = **26.2.1x2**, which is what the saving After Effects reports for itself (`app.version` recorded by the corpus scripts) - this one sample fixes minor, bug-fix and build. The copies read as **25.6.0x97** and **24.6.0x65** (major **V** from three consecutive majors; minor, bug-fix and build of the copies **E**: one other word decides their positions). `effectcraft_aep::AeVersion`, `AepFile::file_version`, `AepFile::saved_by_version`. | Tests `decodes_the_sampled_words`, `the_container_of_a_copy_states_its_version_in_head_and_the_writer_in_svap`. |
|
||||
| `head` bytes 0 to 1 (a `u16`) step with the major: 97 (`0x61`) in the 2026 saves, 96 in 25.x, 95 in 24.x; bytes 2 to 3 are 5, 9 and 6 (**?**). Bytes 8 to 15 differ between families, not between versions; bytes 16 to 19 are the save counter (one higher in each copy). Nothing is derived from bytes 0 to 3 (`AepFile::head_revision` exposes the `u16`). | All 36. |
|
||||
| The word is packed (bit 31 first): bits 25-24 and 21-19 the major, 18-15 the minor, 14-11 the bug-fix, 10-8 a stage (6 in all), 7-0 the build; bits 31-26 (`000011`) and 23-22 (`00`) are the same in all samples and not decoded. `0f110e02` = **26.2.1x2**, which is what the saving After Effects reports for itself (`app.version` recorded by the corpus scripts) - this one sample fixes minor, bug-fix and build. The copies read as **25.6.0x97** and **24.6.0x65** (major **V** from three consecutive majors; minor, bug-fix and build of the copies **E**: one other word decides their positions). `craft_aep::AeVersion`, `AepFile::file_version`, `AepFile::saved_by_version`. | Tests `decodes_the_sampled_words`, `the_container_of_a_copy_states_its_version_in_head_and_the_writer_in_svap`. |
|
||||
| `head` bytes 0 to 1 (a `u16`) step with the major: 97 (`0x61`) in the 2026 saves, 96 in 25.x, 95 in 24.x; bytes 2 to 3 are 5, 9 and 6 (**?**). Bytes 8 to 15 differ between families, not between versions; bytes 16 to 19 are the save counter (one higher in each copy). Nothing is derived from bytes 0 to 3, and the reader does not expose them. | All 36. |
|
||||
| The XMP packet after the container is no guide: its `CreatorTool` is "Adobe After Effects 2026" in the copies too. | All 36. |
|
||||
| **26 against 25.x** (per family, chunk tree aligned by item and layer id): besides `head`, `nhed` / `nnhd` differ in 6 bytes (`nhed` 20-23 and 26-27: `827fa930` / `0015` in the 2026 save, `00000000` / `011b` in both copies; session state, see section 4). Every **solid's** `Pin /CLRS` changes: the 2026 save has `dcui` (1 byte, 1), `apid` all `0xff` and an empty `Utf8`; the copies drop `dcui`, set `apid` to a GUID (`601e9973...`, the same in all) and embed a 935-byte JSON with an ICC profile (`baseColorProfile/colorProfileData`). A **footage** item that the 2026 save stores with an assigned profile (`mcsp` = 1 and a profile JSON) loses `mcsp` and that JSON in the copies, and its `epid` becomes all `0xff`. The rest is save noise: the footage alias `ascendcount_base` (the copies were saved in another folder), `sspc` byte 112 of a footage item (a modification time), the `oEXR` `opti` of an EXR item (the copies hold the display device name `\.\DISPLAY1` as UTF-16 at byte 126 and, in the 24.x copy, a few non-zero bytes from byte 2 102 that look like uninitialised memory; the 2026 save has zeros), the plug-ins' sequence data `sdat` of Lumetri Color (16 instances and the project's `EfdG`), the mask counter `mkif` byte 8. No chunk is added or reordered, and no `pard`, `cdat`, `ldta`, `cdta`, `tdb4`, keyframe, text or shape chunk differs. | All 12 families. |
|
||||
| **25.x against 24.x**: only `head` (bytes 1, 3, 5 and 7), and the session noise in `opti` and `sdat` above. | All 12 families. |
|
||||
@@ -218,12 +223,12 @@ the value from the corpus manifest (test file in parentheses at the end of each
|
||||
|
||||
API: `LayerHeader` (`is_video_enabled`/`is_enabled`, `is_audio_enabled`, `is_effects_enabled`,
|
||||
`is_auto_oriented`, `light_type`, `blend_mode`, `stretch` / `stretch_den`, `stretch_ratio`,
|
||||
`stretch_percent`, `in_point`, `out_point`); the same switches on `Layer`; `Layer::track_matte`
|
||||
`in_point`, `out_point`); the same switches on `Layer`; `Layer::track_matte`
|
||||
(`TrackMatte`, `MatteMode`; test `tests/matte.rs`). `stretch` is now the
|
||||
numerator of the ratio (it was the whole multiplier before 50 % and 150 % were sampled). Tests:
|
||||
`tests/header.rs`, `tests/layer_switches.rs`. The layer name itself is the `Utf8` child of the
|
||||
`Layr` (empty while the name is the source's); the layer then shows the name of its source item
|
||||
(`Project::layer_display_name`).
|
||||
(`Project::layer_source(...)` then `Item::display_name`, the rule the importer uses).
|
||||
|
||||
### 3.2 Composition header (`Item/cdta`, 204 bytes; the decoder needs the first 176) and renderer
|
||||
|
||||
@@ -248,9 +253,9 @@ numerator of the ratio (it was the whole multiplier before 50 % and 150 % were s
|
||||
|
||||
The inert `ADBE Time Remapping` record of every AV layer has `tduM` = the source's duration in seconds: 5 for 5 s compositions (124, 257, 258), 3 and 2 for a 3 s and a 2 s composition (600, 601), 1 for the 1 s WAV (351), 0.1 for the three-frame sequence (353), 0 on solids and stills (000, 126). Every property's keyframe time scale (`tdb4` bytes 12 to 15) follows the composition (23 976 in every
|
||||
property of `L` after 229 and 230). The corpus manifest calls the classic renderer
|
||||
`ADBE Advanced 3d`; the file stores `ADBE Escher` / `Classic 3D`. API: `CompHeader::frame_rate_exact`
|
||||
(exact fraction), `duration_frames`, `shutter_phase`, `display_start`, `display_start_seconds`,
|
||||
`display_start_frame`. Tests: `tests/header.rs`, `tests/layer_switches.rs`.
|
||||
`ADBE Advanced 3d`; the file stores `ADBE Escher` / `Classic 3D`. API: `CompHeader::{time_base,
|
||||
frame_rate_fixed, duration_units, shutter_phase, display_start}` (`display_start` is the exact
|
||||
fraction). Tests: `tests/header.rs`, `tests/layer_switches.rs`.
|
||||
|
||||
### 3.3 Items, footage and precomps
|
||||
|
||||
@@ -295,7 +300,8 @@ name. A footage layer shows the item's name while it has no name of its own (351
|
||||
|
||||
Test: `tests/header.rs` (precomp), `tests/text_markers_footage.rs` and `tests/batch3_objects.rs`
|
||||
(footage), `tests/layer_switches.rs` (solid colour). API: `Footage::{duration, native_frame_rate,
|
||||
bits_per_pixel, file_size, audio_sample_rate, is_sequence, sequence_frame_count}`.
|
||||
bits_per_pixel, file_size, audio_sample_rate, is_sequence}`; `Item::sequence_info` (with
|
||||
`SequenceInfo::frame_count`).
|
||||
|
||||
### 3.4 Properties: sparse storage, static values and units
|
||||
|
||||
@@ -404,8 +410,8 @@ out speed, at 40 out influence**). Scale key 1 of 305 (`n = 3`, in 0 / 33 %, out
|
||||
15 / 66) is `0,0, .33,.33, 15,15, .66,.66`. Component `i` uses slot `i` of each block (**V** for the
|
||||
block layout and the units, Scale speed in fractions per second and Mask Feather speed in pixels
|
||||
per second; **E** that different values per dimension sit in the slot of their dimension, because
|
||||
the tool that made the corpus applies one ease to all dimensions; **V** since the exporter wrote three different eases for the three Scale dimensions and After Effects read the same three back, aep-export.md R13). `Keyframe::plain_ease_slots`
|
||||
returns the doubles as stored, `Keyframe::ease_component(i)` one component's `KeyEase`. Layout of
|
||||
the tool that made the corpus applies one ease to all dimensions; **V** since the exporter wrote three different eases for the three Scale dimensions and After Effects read the same three back, aep-export.md R13). `Keyframe::ease_component(i)`
|
||||
returns one component's `KeyEase`, `Keyframe::ease_components` all of them. Layout of
|
||||
the spatial template: `u32` at 8 (1 normally, 0 after the spatial auto-bezier and continuous
|
||||
switches were turned off together, 054), `f64` arc length of the segment to the next key at 16
|
||||
(100 in 204 key 1, 0 on the last key and right after creation; safe to ignore), **ease at 24 in
|
||||
@@ -430,7 +436,7 @@ in tangent (`n` x `f64`, relative offset from the value) at `56 + 8 n`, out tang
|
||||
| **ease, colour switched to bezier** | spatial n=4 @24..55 | keyframe.set_interpolation bezier on two colour keys stores speed 0 and influence 0 on both sides, and After Effects interpolates them linearly (598 Start Color red to blue: 0.75 / 0.5 / 0.25 red at 0.5 / 1 / 1.5 s); a custom colour ease cannot be set by script | 598 | V |
|
||||
| **ease, mask path** | 64-byte record @24..55 | in speed, in influence, out speed, out influence as in the spatial template: 599 out / in influence 0.75, 700 0.2 / 0.5, speeds 0 (the path at 0.5 s is 5.42 % / 28.0 % of the way). The `f64` at 16 is 1 (093) or 2 (599, 700) on the first key and 0 on the last (not decoded) | 093, 599, 700 | V (influence), ? (speed unit) |
|
||||
|
||||
API: `Keyframe::{interpolation_in, interpolation_out, kind, value, ease, ease_component, ease_components, plain_ease_slots, color_distance, spatial}`;
|
||||
API: `Keyframe::{interpolation_in, interpolation_out, kind, value, ease, ease_component, ease_components, spatial}` (the `f64` at byte 16 of a colour key is `SpatialKey::segment_length`);
|
||||
`Property::keys()` hands the property's template to its records. Marker record (16 bytes): time,
|
||||
`03 03` hold bytes, 8 stale bytes. Mask path record (64 bytes): the common fields and the ease at byte 24 (599, 700). Test:
|
||||
`tests/values.rs`, `tests/keyframes.rs`.
|
||||
@@ -767,7 +773,7 @@ pins are placed by hand or by a preset.
|
||||
328), so mode and inverted are reported per mask. `mkif` byte 8 is the running mask id counter of the
|
||||
**layer** in the session (1, 2 for the two masks of 328; 329 then gets 3; undo does not reset it; a new
|
||||
layer starts at 1 again): exposed as `Mask::counter`, a session counter and not a stable id. API:
|
||||
`Mask::{mode, inverted, counter}`, `Project::mask_vertices_px` (vertices in layer pixels), `Layer::mask_atoms` (`MaskAtom`: the mask with its atom group, its path property and its shapes by position, for the keys and expressions of the mask properties). Tests:
|
||||
`Mask::{mode, inverted, counter}`, `ShapePath::vertices(size)` (vertices in layer pixels: `size` is the size of the layer's source, `Project::layer_source_size`; a text or shape layer has no source size and its mask boxes are already pixels, so the importer passes 1 x 1), `Layer::mask_atoms` (`MaskAtom`: the mask with its atom group, its path property and its shapes by position, for the keys and expressions of the mask properties). Tests:
|
||||
`tests/masks.rs` (with a synthetic check of the handle order), `tests/batch3_objects.rs`.
|
||||
|
||||
### 3.9 Shape layers (`tdgp[ADBE Root Vectors Group]`)
|
||||
@@ -895,7 +901,7 @@ groups as on other layers, with these conventions:
|
||||
|
||||
| What | Path | Stored | Evidence | Conf. |
|
||||
|---|---|---|---|---|
|
||||
| camera Position, default (auto state) | `ADBE Transform Group/ADBE Position`, `tdb4` 76 and 77 = 1 although no expression exists | `[0, 0, -2666.67]` (x and y are 0 whatever the composition size; z = -zoom). A **one-node** camera reports `[0, 0, -2666.67]` (251). **Correction (S16): a reopened two-node camera reports `[0, 0, -2666.67]` too (256 read back), and After Effects renders it there**: the default two-node camera of 537 above the 3D solid of 486 renders from `[0, 0, -2666.67]` towards the point of interest, not like the composition's default view (537 differs from 486 in 24 % of the grid points, and EffectCraft's import with the stored position matches 537 but for 4 edge points). The `[960, 540, ...]` of the batch-3 notes was not reproduced (535 and 537, created by script, read back the stored value too); `CameraPosition::reported` keeps the old rule and is marked as contradicted | 256, 251, 537 | V |
|
||||
| camera Position, default (auto state) | `ADBE Transform Group/ADBE Position`, `tdb4` 76 and 77 = 1 although no expression exists | `[0, 0, -2666.67]` (x and y are 0 whatever the composition size; z = -zoom). A **one-node** camera reports `[0, 0, -2666.67]` (251). **Correction (S16): a reopened two-node camera reports `[0, 0, -2666.67]` too (256 read back), and After Effects renders it there**: the default two-node camera of 537 above the 3D solid of 486 renders from `[0, 0, -2666.67]` towards the point of interest, not like the composition's default view (537 differs from 486 in 24 % of the grid points, and EffectCraft's import with the stored position matches 537 but for 4 edge points). The `[960, 540, ...]` of the batch-3 notes was not reproduced (535 and 537, created by script, read back the stored value too); the reader keeps only the stored value (`CameraPosition::stored`) | 256, 251, 537 | V |
|
||||
| camera Position, set by the user | same, `tdb4` 76 and 77 = 0 | the absolute value `[100, 200, -1000]` | 253 | V |
|
||||
| camera point of interest | `ADBE Anchor Point` | `[960, 540, 0]` in **pixels** (not a fraction); the record's `tdsb` is 1 on a two-node and 3 on the one-node camera (not asserted) | 251, 253, 256 | V (value), E (`tdsb`) |
|
||||
| light Position, default | same as the camera | `[0, 0, -1333.33]`, as After Effects reports it (255 read back); the point of interest `[960, 540, 0]` | 254, 255 | V |
|
||||
@@ -911,10 +917,8 @@ groups as on other layers, with these conventions:
|
||||
|
||||
A point light made by converting a spot light has a different property set (`Orientation`, `Rotate
|
||||
X/Y/Z`, `Cone Angle`, `Cone Feather 2` in the default state) that a spot light lacks (252); whether that
|
||||
is an artefact of the conversion is untested. API: `Layer::camera_position`,
|
||||
`CameraPosition::reported`, `Layer::camera_point_of_interest`, `Layer::light_intensity`,
|
||||
`Layer::{camera_zoom, camera_depth_of_field, camera_focus_distance, camera_aperture, light_color,
|
||||
light_cone_angle, light_cone_feather}`, `DEFAULT_CAMERA_ZOOM` (2 666.67 is verified for the 1920 px
|
||||
is an artefact of the conversion is untested. API: `Layer::camera_position`; the camera and light
|
||||
options are read by the importer's table (`import/camera.rs`); `DEFAULT_CAMERA_ZOOM` (2 666.67 is verified for the 1920 px
|
||||
composition only), `Layer::orientation` (`Orientation`; test `orientation_is_otst_otda` in
|
||||
`tests/batch5.rs`). Render checks against After Effects (20 px grid, 5 184 points): 486 1 point
|
||||
off, 535 2, 536 0, 537 4 (anti-aliased edges); **488 (Orientation `[10, 20, 30]`): 272 points
|
||||
|
||||
+681
-101
@@ -12,13 +12,23 @@
|
||||
//! instructions. A document type declaration is refused (no entity expansion). It runs without
|
||||
//! recursion; the nesting depth and the element count are bounded by [`Limits`], and everything
|
||||
//! it allocates is bounded by the size of the input.
|
||||
//!
|
||||
//! There are two readers. [`read_aepx`] builds the owned tree element by element
|
||||
//! (`aepx/tree.rs`). The importer, which only reads, uses `index_aepx`: it decodes every body
|
||||
//! (hex, text, the alias JSON, the numbers) into one buffer and records the chunks as entries of
|
||||
//! the flat index ([`Draft`]s, in depth-first order, bodies as ranges of the buffer), which
|
||||
//! [`crate::TreeIndex::from_drafts`] turns into an index that borrows the buffer.
|
||||
//! The tests hold both to the same tree and the same errors.
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS};
|
||||
use crate::chunk::{AepFile, LIST, NESTED_IDS};
|
||||
use crate::error::AepError;
|
||||
use crate::index::{Draft, Kind as EntryKind};
|
||||
use crate::read::{Limits, parse};
|
||||
|
||||
mod tree;
|
||||
|
||||
/// The root element of an `.aepx`.
|
||||
const ROOT: &[u8] = b"AfterEffectsProject";
|
||||
/// The element that holds the XMP packet (the trailer of an `.aep`).
|
||||
@@ -68,17 +78,71 @@ pub fn read_aepx(data: &[u8]) -> Result<AepFile<'static>, AepError> {
|
||||
/// # Errors
|
||||
/// See [`read_aepx`].
|
||||
pub fn read_aepx_with_limits(data: &[u8], limits: Limits) -> Result<AepFile<'static>, AepError> {
|
||||
tree::read(data, limits)
|
||||
}
|
||||
|
||||
/// An `.aepx` read for the flat index: every body decoded into one buffer, and the chunks as
|
||||
/// [`Draft`]s in depth-first order with their bodies as ranges of that buffer.
|
||||
/// [`crate::TreeIndex::from_drafts`] makes the index, which borrows the buffer.
|
||||
pub struct AepxDrafts {
|
||||
/// The decoded bodies (hex, text, alias JSON, numbers) and the XMP packet.
|
||||
pub buffer: Vec<u8>,
|
||||
pub nodes: Vec<Draft>,
|
||||
/// The XMP packet: a range of `buffer` (empty without one).
|
||||
pub trailer: (usize, usize),
|
||||
}
|
||||
|
||||
/// Reads an `.aepx` into [`AepxDrafts`]: the tree [`read_aepx_with_limits`] gives, with the same
|
||||
/// errors, without one allocation per chunk.
|
||||
///
|
||||
/// # Errors
|
||||
/// See [`read_aepx_with_limits`]. When memory runs out, two errors come up in more cases than
|
||||
/// there: [`AepError::TooLarge`] when the chunk list cannot grow (the owned-tree reader would
|
||||
/// abort), and [`AepError::Xml`] "bdata is too large to hold in memory" also when the shared body
|
||||
/// buffer cannot grow (the owned-tree reader reserves each value on its own).
|
||||
pub fn index_aepx(data: &[u8], limits: Limits) -> Result<AepxDrafts, AepError> {
|
||||
let text = match std::str::from_utf8(data) {
|
||||
Ok(t) => t.as_bytes(),
|
||||
Err(e) => return Err(xml(e.valid_up_to(), "the file is not valid UTF-8")),
|
||||
};
|
||||
Reader { data: text, pos: 0, limits, count: 0, stack: Vec::new(), top: None, trailer: None }.run()
|
||||
// Every counted element takes at least 3 bytes (`<a>` in a document cut short), so there are
|
||||
// never more chunks than this.
|
||||
let bound = (text.len() / 3).min(limits.max_chunks);
|
||||
let mut nodes = Vec::new();
|
||||
let _ = nodes.try_reserve_exact(bound.min(INITIAL_SLOTS));
|
||||
// Hex is most of an `.aepx` and decodes to half its length: one buffer of that size usually
|
||||
// holds everything. A failed reservation is not an error here; the buffer grows as it goes.
|
||||
let mut buffer = Vec::new();
|
||||
let _ = buffer.try_reserve_exact(text.len() / 2);
|
||||
let reader = Reader {
|
||||
data: text,
|
||||
pos: 0,
|
||||
limits,
|
||||
count: 0,
|
||||
stack: Vec::new(),
|
||||
root_closed: false,
|
||||
trailer: None,
|
||||
buffer,
|
||||
nodes,
|
||||
bound,
|
||||
attrs: Vec::new(),
|
||||
};
|
||||
reader.run()
|
||||
}
|
||||
|
||||
/// The slots the chunk list starts with; it doubles from there (as the array of
|
||||
/// [`crate::TreeIndex`] does).
|
||||
const INITIAL_SLOTS: usize = 1024;
|
||||
|
||||
fn xml(offset: usize, what: &'static str) -> AepError {
|
||||
AepError::Xml { offset, what }
|
||||
}
|
||||
|
||||
/// The pad byte of a body of `len` bytes, as [`crate::Chunk::data`] sets it.
|
||||
fn pad_of(len: usize) -> Option<u8> {
|
||||
if len % 2 == 1 { Some(0) } else { None }
|
||||
}
|
||||
|
||||
/// What an open element becomes when it closes.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind {
|
||||
@@ -102,13 +166,14 @@ struct Frame {
|
||||
at: usize,
|
||||
/// The name as written, to match the end tag.
|
||||
name: (usize, usize),
|
||||
/// The decoded `bdata` attribute.
|
||||
bdata: Option<Vec<u8>>,
|
||||
/// The chunk's entry in the list (for `Chunk`, `Utf8` and `Alias`).
|
||||
entry: Option<usize>,
|
||||
/// The buffer length when the element opened: its body starts here.
|
||||
mark: usize,
|
||||
/// The end of the decoded `bdata` attribute in the buffer.
|
||||
bdata_end: Option<usize>,
|
||||
/// The attributes of a `fileReference`, in document order.
|
||||
attrs: Vec<(String, String)>,
|
||||
/// Collected text (only for `string` and the XMP packet).
|
||||
text: Vec<u8>,
|
||||
children: Vec<Chunk<'static>>,
|
||||
}
|
||||
|
||||
struct Reader<'d> {
|
||||
@@ -117,13 +182,19 @@ struct Reader<'d> {
|
||||
limits: Limits,
|
||||
count: usize,
|
||||
stack: Vec<Frame>,
|
||||
/// The top-level chunks once the root element has closed.
|
||||
top: Option<Vec<Chunk<'static>>>,
|
||||
trailer: Option<Vec<u8>>,
|
||||
/// True once the root element has closed.
|
||||
root_closed: bool,
|
||||
trailer: Option<(usize, usize)>,
|
||||
buffer: Vec<u8>,
|
||||
nodes: Vec<Draft>,
|
||||
/// The most chunks the input can hold under the limits: `nodes` never reserves past it.
|
||||
bound: usize,
|
||||
/// The attributes of the current start tag; kept to reuse its allocation.
|
||||
attrs: Vec<((usize, usize), Cow<'d, [u8]>)>,
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
fn run(mut self) -> Result<AepFile<'static>, AepError> {
|
||||
impl<'d> Reader<'d> {
|
||||
fn run(mut self) -> Result<AepxDrafts, AepError> {
|
||||
if self.data.starts_with(&[0xef, 0xbb, 0xbf]) {
|
||||
self.pos = 3;
|
||||
}
|
||||
@@ -137,11 +208,13 @@ impl Reader<'_> {
|
||||
if let Some(open) = self.stack.last() {
|
||||
return Err(xml(open.at, "an element is not closed before the end of the file"));
|
||||
}
|
||||
let chunks = self.top.ok_or_else(|| xml(self.data.len(), "no AfterEffectsProject root element"))?;
|
||||
Ok(AepFile { chunks, trailer: Cow::Owned(self.trailer.unwrap_or_default()) })
|
||||
if !self.root_closed {
|
||||
return Err(xml(self.data.len(), "no AfterEffectsProject root element"));
|
||||
}
|
||||
Ok(AepxDrafts { buffer: self.buffer, nodes: self.nodes, trailer: self.trailer.unwrap_or((0, 0)) })
|
||||
}
|
||||
|
||||
fn rest(&self) -> &[u8] {
|
||||
fn rest(&self) -> &'d [u8] {
|
||||
self.data.get(self.pos..).unwrap_or(&[])
|
||||
}
|
||||
|
||||
@@ -181,14 +254,13 @@ impl Reader<'_> {
|
||||
self.pos = start;
|
||||
let end = self.skip_past(b"]]>", "a CDATA section is not closed")?;
|
||||
let content = self.data.get(start..end).unwrap_or(&[]);
|
||||
let at = start;
|
||||
match self.stack.last_mut() {
|
||||
match self.stack.last() {
|
||||
Some(frame) => {
|
||||
if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) {
|
||||
frame.text.extend_from_slice(content);
|
||||
self.buffer.extend_from_slice(content);
|
||||
}
|
||||
}
|
||||
None => return Err(xml(at, "CDATA outside the root element")),
|
||||
None => return Err(xml(start, "CDATA outside the root element")),
|
||||
}
|
||||
} else if rest.starts_with(b"<!") {
|
||||
return Err(xml(self.pos, "document type declarations are not supported"));
|
||||
@@ -219,25 +291,22 @@ impl Reader<'_> {
|
||||
self.pos = self.pos.saturating_add(n);
|
||||
}
|
||||
|
||||
fn slice(&self, (a, b): (usize, usize)) -> &[u8] {
|
||||
fn slice(&self, (a, b): (usize, usize)) -> &'d [u8] {
|
||||
self.data.get(a..b).unwrap_or(&[])
|
||||
}
|
||||
|
||||
fn start_tag(&mut self) -> Result<(), AepError> {
|
||||
let at = self.pos;
|
||||
self.pos = self.pos.saturating_add(1);
|
||||
let name = self.name()?;
|
||||
let mut attrs: Vec<((usize, usize), Vec<u8>)> = Vec::new();
|
||||
let self_closing = loop {
|
||||
/// Reads the attributes of a start tag into `attrs`; true when the tag closes itself.
|
||||
fn attributes(&mut self, at: usize, attrs: &mut Vec<((usize, usize), Cow<'d, [u8]>)>) -> Result<bool, AepError> {
|
||||
loop {
|
||||
self.skip_ws();
|
||||
let rest = self.rest();
|
||||
if rest.starts_with(b"/>") {
|
||||
self.pos = self.pos.saturating_add(2);
|
||||
break true;
|
||||
return Ok(true);
|
||||
}
|
||||
if rest.starts_with(b">") {
|
||||
self.pos = self.pos.saturating_add(1);
|
||||
break false;
|
||||
return Ok(false);
|
||||
}
|
||||
if rest.is_empty() {
|
||||
return Err(xml(at, "a start tag is not closed"));
|
||||
@@ -259,11 +328,27 @@ impl Reader<'_> {
|
||||
if raw.contains(&b'<') {
|
||||
return Err(xml(vstart, "`<` inside an attribute value"));
|
||||
}
|
||||
let mut value = Vec::new();
|
||||
decode_entities(raw, vstart, &mut value)?;
|
||||
// A value without `&` is its own decoding (most of them, every `bdata` After Effects
|
||||
// writes): borrow it instead of copying it.
|
||||
let value = if raw.contains(&b'&') {
|
||||
let mut value = Vec::new();
|
||||
decode_entities(raw, vstart, &mut value)?;
|
||||
Cow::Owned(value)
|
||||
} else {
|
||||
Cow::Borrowed(raw)
|
||||
};
|
||||
self.pos = vend.saturating_add(1);
|
||||
attrs.push((key, value));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn start_tag(&mut self) -> Result<(), AepError> {
|
||||
let at = self.pos;
|
||||
self.pos = self.pos.saturating_add(1);
|
||||
let name = self.name()?;
|
||||
let mut attrs = std::mem::take(&mut self.attrs);
|
||||
attrs.clear();
|
||||
let self_closing = self.attributes(at, &mut attrs)?;
|
||||
let kind = self.kind_of(name, at)?;
|
||||
if kind != Kind::Root {
|
||||
self.count = self.count.saturating_add(1);
|
||||
@@ -275,15 +360,27 @@ impl Reader<'_> {
|
||||
return Err(AepError::TooDeep);
|
||||
}
|
||||
}
|
||||
let mut frame = Frame { kind, at, name, bdata: None, attrs: Vec::new(), text: Vec::new(), children: Vec::new() };
|
||||
if matches!(kind, Kind::Chunk(_)) {
|
||||
if let Some((key, value)) = attrs.iter().find(|(k, _)| self.slice(*k) == b"bdata") {
|
||||
frame.bdata = Some(decode_hex(value, key.1)?);
|
||||
let mark = self.buffer.len();
|
||||
let mut frame = Frame { kind, at, name, entry: None, mark, bdata_end: None, attrs: Vec::new() };
|
||||
match kind {
|
||||
Kind::Chunk(id) => {
|
||||
if let Some((key, value)) = attrs.iter().find(|(k, _)| self.slice(*k) == b"bdata") {
|
||||
decode_hex_into(value, key.1, MAX_BDATA, &mut self.buffer)?;
|
||||
frame.bdata_end = Some(self.buffer.len());
|
||||
}
|
||||
frame.entry = Some(self.open_entry(id)?);
|
||||
}
|
||||
} else if kind == Kind::Alias {
|
||||
frame.attrs =
|
||||
attrs.iter().map(|(k, v)| (String::from_utf8_lossy(self.slice(*k)).into_owned(), String::from_utf8_lossy(v).into_owned())).collect();
|
||||
Kind::Utf8 => frame.entry = Some(self.open_entry(*b"Utf8")?),
|
||||
Kind::Alias => {
|
||||
frame.attrs = attrs
|
||||
.iter()
|
||||
.map(|(k, v)| (String::from_utf8_lossy(self.slice(*k)).into_owned(), String::from_utf8_lossy(v).into_owned()))
|
||||
.collect();
|
||||
frame.entry = Some(self.open_entry(*b"alas")?);
|
||||
}
|
||||
Kind::Root | Kind::Xmp | Kind::Skip => {}
|
||||
}
|
||||
self.attrs = attrs;
|
||||
self.stack.push(frame);
|
||||
if self_closing {
|
||||
self.close()?;
|
||||
@@ -291,12 +388,38 @@ impl Reader<'_> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Appends the entry of a chunk whose element just opened; its body is filled in when it
|
||||
/// closes. The list grows by its length (at least 16) but never past [`Self::bound`]; an
|
||||
/// allocation the target cannot make is an error, not an abort.
|
||||
fn open_entry(&mut self, id: [u8; 4]) -> Result<usize, AepError> {
|
||||
let at = self.nodes.len();
|
||||
// This entry and the one after it must have `u32` indices.
|
||||
u32::try_from(at.checked_add(1).ok_or(AepError::TooManyChunks)?).map_err(|_| AepError::TooManyChunks)?;
|
||||
if at == self.nodes.capacity() {
|
||||
// Past the bound (which only a wrong guess could reach) it keeps doubling.
|
||||
let room = self.bound.saturating_sub(at);
|
||||
let more = if room == 0 { at.max(16) } else { at.max(16).min(room) };
|
||||
self.nodes.try_reserve_exact(more).map_err(|_| AepError::TooLarge)?;
|
||||
}
|
||||
self.nodes.push(Draft::placeholder(id));
|
||||
Ok(at)
|
||||
}
|
||||
|
||||
/// Fills in the entry at `at` once its element has closed; its subtree is what follows it.
|
||||
fn finish(&mut self, at: usize, id: [u8; 4], kind: EntryKind, list_type: [u8; 4], body: (usize, usize), pad: Option<u8>) -> Result<(), AepError> {
|
||||
let end = u32::try_from(self.nodes.len()).map_err(|_| AepError::TooManyChunks)?;
|
||||
if let Some(d) = self.nodes.get_mut(at) {
|
||||
*d = Draft { start: body.0, len: body.1.saturating_sub(body.0), id, list_type, end, kind, pad };
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Classifies an element by its name and where it is.
|
||||
fn kind_of(&self, name: (usize, usize), at: usize) -> Result<Kind, AepError> {
|
||||
let n = self.slice(name);
|
||||
let parent = match self.stack.last() {
|
||||
None => {
|
||||
if self.top.is_some() {
|
||||
if self.root_closed {
|
||||
return Err(xml(at, "a second root element"));
|
||||
}
|
||||
if n != ROOT {
|
||||
@@ -340,39 +463,104 @@ impl Reader<'_> {
|
||||
self.close()
|
||||
}
|
||||
|
||||
/// Closes the innermost element and hands its chunk to the parent.
|
||||
/// Closes the innermost element and fills in its chunk's entry.
|
||||
fn close(&mut self) -> Result<(), AepError> {
|
||||
let frame = self.stack.pop().ok_or(xml(self.pos, "an end tag without a start tag"))?;
|
||||
let chunk = match frame.kind {
|
||||
Kind::Root => {
|
||||
self.top = Some(frame.children);
|
||||
return Ok(());
|
||||
}
|
||||
Kind::Skip => return Ok(()),
|
||||
match frame.kind {
|
||||
Kind::Root => self.root_closed = true,
|
||||
Kind::Skip => {}
|
||||
Kind::Xmp => {
|
||||
// Only the first packet is kept; a later one is dropped from the buffer (its
|
||||
// children are skipped, so nothing else was written after it opened).
|
||||
if self.trailer.is_none() {
|
||||
self.trailer = Some(frame.text);
|
||||
self.trailer = Some((frame.mark, self.buffer.len()));
|
||||
} else {
|
||||
self.buffer.truncate(frame.mark);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Kind::Utf8 => Chunk::data(*b"Utf8", frame.text),
|
||||
Kind::Alias => Chunk::data(*b"alas", alias_json(&frame.attrs).into_bytes()),
|
||||
Kind::Chunk(id) => chunk_of(id, frame)?,
|
||||
};
|
||||
if let Some(parent) = self.stack.last_mut() {
|
||||
parent.children.push(chunk);
|
||||
Kind::Utf8 => {
|
||||
let at = frame.entry.ok_or(xml(frame.at, "an element lost its chunk"))?;
|
||||
let end = self.buffer.len();
|
||||
self.finish(at, *b"Utf8", EntryKind::Data, [0; 4], (frame.mark, end), pad_of(end.saturating_sub(frame.mark)))?;
|
||||
}
|
||||
Kind::Alias => {
|
||||
let at = frame.entry.ok_or(xml(frame.at, "an element lost its chunk"))?;
|
||||
self.buffer.extend_from_slice(alias_json(&frame.attrs).as_bytes());
|
||||
let end = self.buffer.len();
|
||||
self.finish(at, *b"alas", EntryKind::Data, [0; 4], (frame.mark, end), pad_of(end.saturating_sub(frame.mark)))?;
|
||||
}
|
||||
Kind::Chunk(id) => self.close_chunk(id, &frame)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The chunk of a closed element named by a chunk id (what the owned reader's `chunk_of`
|
||||
/// built): `bdata` wins over children, `numS` / `ppSn` hold their number, a nested id holds its
|
||||
/// children (or nothing), any other name is a `LIST` of that type.
|
||||
fn close_chunk(&mut self, id: [u8; 4], frame: &Frame) -> Result<(), AepError> {
|
||||
let at = frame.entry.ok_or(xml(frame.at, "an element lost its chunk"))?;
|
||||
let after = at.checked_add(1).ok_or(AepError::TooManyChunks)?;
|
||||
if let Some(bdata_end) = frame.bdata_end {
|
||||
// The children were read (and counted, and checked) but are not part of the chunk.
|
||||
self.nodes.truncate(after);
|
||||
self.buffer.truncate(bdata_end);
|
||||
let body = (frame.mark, bdata_end);
|
||||
let pad = pad_of(bdata_end.saturating_sub(frame.mark));
|
||||
if OPAQUE_LIST_TYPES.contains(&id) {
|
||||
return self.finish(at, LIST, EntryKind::OpaqueList, id, body, pad);
|
||||
}
|
||||
return self.finish(at, id, EntryKind::Data, [0; 4], body, pad);
|
||||
}
|
||||
// Numbers written as text: `<numS><string>0</string></numS>` and `<ppSn><string>150.000000000</string></ppSn>`.
|
||||
if &id == b"numS" || &id == b"ppSn" {
|
||||
let missing = || xml(frame.at, "a numS or ppSn element without its number");
|
||||
// Exactly one child chunk, a `Utf8`: the entry after this one, whose subtree runs to the end.
|
||||
let only = self.nodes.get(after).copied().filter(|c| usize::try_from(c.end).ok() == Some(self.nodes.len())).ok_or_else(missing)?;
|
||||
if only.id != *b"Utf8" {
|
||||
return Err(missing());
|
||||
}
|
||||
let text = match only.kind {
|
||||
EntryKind::Data | EntryKind::OpaqueList => {
|
||||
only.start.checked_add(only.len).and_then(|end| self.buffer.get(only.start..end)).unwrap_or(&[])
|
||||
}
|
||||
EntryKind::List | EntryKind::Nested => &[],
|
||||
};
|
||||
let text = std::str::from_utf8(text).map_err(|_| xml(frame.at, "a number is not text"))?.trim();
|
||||
let body: [u8; 8];
|
||||
let bytes: &[u8] = if &id == b"numS" {
|
||||
// Only `0` is sampled, so whether the field is signed is open: accept both readings of 4 bytes.
|
||||
let n = text.parse::<i64>().ok().filter(|n| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(n));
|
||||
let n = n.ok_or(xml(frame.at, "numS is not a 32-bit integer"))?;
|
||||
body = u64::from(n as u32).to_be_bytes();
|
||||
body.get(4..).unwrap_or(&[])
|
||||
} else {
|
||||
body = text.parse::<f64>().map_err(|_| xml(frame.at, "ppSn is not a number"))?.to_be_bytes();
|
||||
&body
|
||||
};
|
||||
self.nodes.truncate(after);
|
||||
self.buffer.truncate(frame.mark);
|
||||
self.buffer.extend_from_slice(bytes);
|
||||
let end = self.buffer.len();
|
||||
return self.finish(at, id, EntryKind::Data, [0; 4], (frame.mark, end), pad_of(bytes.len()));
|
||||
}
|
||||
let empty = (frame.mark, frame.mark);
|
||||
if NESTED_IDS.contains(&id) {
|
||||
if self.nodes.len() == after {
|
||||
return self.finish(at, id, EntryKind::Data, [0; 4], empty, None);
|
||||
}
|
||||
return self.finish(at, id, EntryKind::Nested, [0; 4], empty, None);
|
||||
}
|
||||
self.finish(at, LIST, EntryKind::List, id, empty, None)
|
||||
}
|
||||
|
||||
fn text(&mut self) -> Result<(), AepError> {
|
||||
let start = self.pos;
|
||||
let len = self.rest().iter().position(|b| *b == b'<').unwrap_or(self.rest().len());
|
||||
let end = start.saturating_add(len);
|
||||
self.pos = end;
|
||||
let raw = self.data.get(start..end).unwrap_or(&[]);
|
||||
match self.stack.last_mut() {
|
||||
Some(frame) if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) => decode_entities(raw, start, &mut frame.text),
|
||||
match self.stack.last() {
|
||||
Some(frame) if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) => decode_entities(raw, start, &mut self.buffer),
|
||||
Some(_) => Ok(()),
|
||||
None if raw.iter().all(u8::is_ascii_whitespace) => Ok(()),
|
||||
None => Err(xml(start, "text outside the root element")),
|
||||
@@ -393,41 +581,6 @@ fn chunk_id(name: &[u8]) -> Option<[u8; 4]> {
|
||||
Some(id)
|
||||
}
|
||||
|
||||
/// The chunk of a closed element named by a chunk id.
|
||||
fn chunk_of(id: [u8; 4], frame: Frame) -> Result<Chunk<'static>, AepError> {
|
||||
if let Some(bytes) = frame.bdata {
|
||||
if OPAQUE_LIST_TYPES.contains(&id) {
|
||||
let pad = if bytes.len() % 2 == 1 { Some(0) } else { None };
|
||||
return Ok(Chunk { id: LIST, body: Body::OpaqueList { list_type: id, data: Cow::Owned(bytes) }, pad });
|
||||
}
|
||||
return Ok(Chunk::data(id, bytes));
|
||||
}
|
||||
// Numbers written as text: `<numS><string>0</string></numS>` and `<ppSn><string>150.000000000</string></ppSn>`.
|
||||
if &id == b"numS" || &id == b"ppSn" {
|
||||
let text = match frame.children.as_slice() {
|
||||
[only] if only.is(b"Utf8") => only.bytes().unwrap_or(&[]),
|
||||
_ => return Err(xml(frame.at, "a numS or ppSn element without its number")),
|
||||
};
|
||||
let text = std::str::from_utf8(text).map_err(|_| xml(frame.at, "a number is not text"))?.trim();
|
||||
let body = if &id == b"numS" {
|
||||
// Only `0` is sampled, so whether the field is signed is open: accept both readings of 4 bytes.
|
||||
let n = text.parse::<i64>().ok().filter(|n| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(n));
|
||||
let n = n.ok_or(xml(frame.at, "numS is not a 32-bit integer"))?;
|
||||
(n as u32).to_be_bytes().to_vec()
|
||||
} else {
|
||||
text.parse::<f64>().map_err(|_| xml(frame.at, "ppSn is not a number"))?.to_be_bytes().to_vec()
|
||||
};
|
||||
return Ok(Chunk::data(id, body));
|
||||
}
|
||||
if NESTED_IDS.contains(&id) {
|
||||
if frame.children.is_empty() {
|
||||
return Ok(Chunk::data(id, Vec::new()));
|
||||
}
|
||||
return Ok(Chunk { id, body: Body::Chunks(frame.children), pad: None });
|
||||
}
|
||||
Ok(Chunk::list(id, frame.children))
|
||||
}
|
||||
|
||||
/// Appends `raw` with the predefined entities and character references decoded.
|
||||
fn decode_entities(raw: &[u8], offset: usize, out: &mut Vec<u8>) -> Result<(), AepError> {
|
||||
let mut i = 0;
|
||||
@@ -465,23 +618,74 @@ fn decode_entities(raw: &[u8], offset: usize, out: &mut Vec<u8>) -> Result<(), A
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Decodes a `bdata` value (hex digits, two per byte).
|
||||
/// Decodes a `bdata` value (hex digits, two per byte) into a new `Vec` of exactly its size.
|
||||
fn decode_hex(text: &[u8], offset: usize) -> Result<Vec<u8>, AepError> {
|
||||
if text.len() % 2 == 1 {
|
||||
return Err(xml(offset, "bdata has an odd number of hex digits"));
|
||||
}
|
||||
decode_hex_at_most(text, offset, MAX_BDATA)
|
||||
}
|
||||
|
||||
/// The most bytes one `bdata` may decode to: what the 32-bit size field of a chunk can hold (a
|
||||
/// larger body could not be written back as an `.aep` either).
|
||||
const MAX_BDATA: usize = u32::MAX as usize;
|
||||
|
||||
/// [`decode_hex`] with the decoded-size limit as a parameter (so tests can reach it).
|
||||
fn decode_hex_at_most(text: &[u8], offset: usize, max: usize) -> Result<Vec<u8>, AepError> {
|
||||
// Checked before anything is reserved, then sized once (collecting into `Option<Vec>` has no
|
||||
// size hint and regrows step by step), and without aborting when the memory is not there.
|
||||
let pairs = check_hex(text, offset, max)?;
|
||||
let mut out = Vec::new();
|
||||
out.try_reserve_exact(pairs).map_err(|_| xml(offset, "bdata is too large to hold in memory"))?;
|
||||
decode_checked(text, offset, &mut out)?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Appends the bytes of a `bdata` value (hex digits, two per byte, at most `max` bytes) to `out`.
|
||||
/// On an error `out` is unchanged.
|
||||
fn decode_hex_into(text: &[u8], offset: usize, max: usize, out: &mut Vec<u8>) -> Result<(), AepError> {
|
||||
let pairs = check_hex(text, offset, max)?;
|
||||
// Grown at most once per value (amortised over a shared buffer), and without aborting when the
|
||||
// memory is not there.
|
||||
out.try_reserve(pairs).map_err(|_| xml(offset, "bdata is too large to hold in memory"))?;
|
||||
decode_checked(text, offset, out)
|
||||
}
|
||||
|
||||
/// Appends the bytes of a `bdata` value that [`check_hex`] accepted. On an error (a digit the
|
||||
/// check let through, which it does not) `out` is unchanged.
|
||||
fn decode_checked(text: &[u8], offset: usize, out: &mut Vec<u8>) -> Result<(), AepError> {
|
||||
let digit = |b: u8| match b {
|
||||
b'0'..=b'9' => Some(b - b'0'),
|
||||
b'a'..=b'f' => Some(b - b'a' + 10),
|
||||
b'A'..=b'F' => Some(b - b'A' + 10),
|
||||
_ => None,
|
||||
};
|
||||
let (pairs, _) = text.as_chunks::<2>();
|
||||
pairs
|
||||
.iter()
|
||||
.map(|[hi, lo]| digit(*hi).zip(digit(*lo)).map(|(h, l)| (h << 4) | l))
|
||||
.collect::<Option<Vec<u8>>>()
|
||||
.ok_or(xml(offset, "bdata holds a character that is not a hex digit"))
|
||||
let mark = out.len();
|
||||
for [hi, lo] in text.as_chunks::<2>().0 {
|
||||
match (digit(*hi), digit(*lo)) {
|
||||
(Some(h), Some(l)) => out.push((h << 4) | l),
|
||||
_ => {
|
||||
out.truncate(mark);
|
||||
return Err(xml(offset, "bdata holds a character that is not a hex digit"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Checks a `bdata` value before anything is reserved for it: an even number of hex digits, at
|
||||
/// most `max` bytes. Returns the number of bytes it decodes to.
|
||||
fn check_hex(text: &[u8], offset: usize, max: usize) -> Result<usize, AepError> {
|
||||
if text.len() % 2 == 1 {
|
||||
return Err(xml(offset, "bdata has an odd number of hex digits"));
|
||||
}
|
||||
let pairs = text.len() / 2;
|
||||
if pairs > max {
|
||||
return Err(AepError::TooLarge);
|
||||
}
|
||||
// One cheap pass checks every digit first. Blocks without an early exit inside, so the check
|
||||
// vectorises; it stops at the first bad block.
|
||||
if !text.chunks(4096).all(|block| block.iter().fold(true, |ok, b| ok & b.is_ascii_hexdigit())) {
|
||||
return Err(xml(offset, "bdata holds a character that is not a hex digit"));
|
||||
}
|
||||
Ok(pairs)
|
||||
}
|
||||
|
||||
/// The JSON body of an `alas` chunk from the attributes of a `fileReference`, keys in document
|
||||
@@ -529,6 +733,7 @@ fn json_str(out: &mut String, s: &str) {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::index::TreeIndex;
|
||||
|
||||
fn doc(body: &str) -> String {
|
||||
format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<AfterEffectsProject xmlns=\"x\">{body}</AfterEffectsProject>\n")
|
||||
@@ -629,6 +834,92 @@ mod tests {
|
||||
assert!(matches!(read_aepx(b"<AfterEffectsProject>\xff</AfterEffectsProject>"), Err(AepError::Xml { .. })));
|
||||
}
|
||||
|
||||
/// Attribute values decode entities only where they have them.
|
||||
#[test]
|
||||
fn attribute_values_decode_entities_only_where_they_have_them() {
|
||||
// An entity inside `bdata` still decodes before the hex does.
|
||||
let f = read_aepx(doc("<a bdata=\"01Ab\"/>").as_bytes()).unwrap();
|
||||
assert_eq!(f.chunks[0].bytes(), Some(&[0x01, 0xab][..]));
|
||||
// A bad entity in any attribute, even of an element that is skipped, is still an error.
|
||||
for bad in ["<a other=\" \"/>", "<Unknown x=\"&bogus;\"/>", "<a bdata=\"00\" note=\"&\"/>"] {
|
||||
assert!(matches!(read_aepx(doc(bad).as_bytes()), Err(AepError::Xml { .. })), "{bad}");
|
||||
}
|
||||
// Without `&` the value is taken as written.
|
||||
let f = read_aepx(doc("<a bdata=\"0aFf\" x=\"y\"/>").as_bytes()).unwrap();
|
||||
assert_eq!(f.chunks[0].bytes(), Some(&[0x0a, 0xff][..]));
|
||||
}
|
||||
|
||||
/// A large bdata with a bad digit is refused before it is sized.
|
||||
#[test]
|
||||
fn a_large_bdata_with_a_bad_digit_is_refused_before_it_is_sized() {
|
||||
// 10 MB of hex that is bad from the first pair (and once more at the very end).
|
||||
let mut text = b"zz".to_vec();
|
||||
text.extend(std::iter::repeat_n(*b"00", 5_000_000).flatten());
|
||||
assert_eq!(decode_hex(&text, 3), Err(xml(3, "bdata holds a character that is not a hex digit")));
|
||||
let mut late = vec![b'0'; 10_000_000];
|
||||
late.extend_from_slice(b"0g");
|
||||
assert_eq!(decode_hex(&late, 3), Err(xml(3, "bdata holds a character that is not a hex digit")));
|
||||
let x = doc(&format!("<a bdata=\"{}\"/>", std::str::from_utf8(&text).unwrap()));
|
||||
assert!(matches!(read_aepx(x.as_bytes()), Err(AepError::Xml { .. })));
|
||||
}
|
||||
|
||||
/// Bdata decodes up to the size limit and not past it.
|
||||
#[test]
|
||||
fn bdata_decodes_up_to_the_size_limit_and_not_past_it() {
|
||||
assert_eq!(decode_hex_at_most(b"00ff10", 0, 3), Ok(vec![0x00, 0xff, 0x10]));
|
||||
assert_eq!(decode_hex_at_most(b"00ff10", 0, 2), Err(AepError::TooLarge));
|
||||
assert_eq!(decode_hex_at_most(b"", 0, 0), Ok(Vec::new()));
|
||||
assert_eq!(decode_hex_at_most(b"00", 0, 0), Err(AepError::TooLarge));
|
||||
// The odd-length error still comes first.
|
||||
assert_eq!(decode_hex_at_most(b"001", 0, 0), Err(xml(0, "bdata has an odd number of hex digits")));
|
||||
// The real limit is what a chunk's 32-bit size field holds.
|
||||
assert_eq!(MAX_BDATA as u64, u64::from(u32::MAX));
|
||||
}
|
||||
|
||||
/// Decode hex matches the collecting version.
|
||||
#[test]
|
||||
fn decode_hex_matches_the_collecting_version() {
|
||||
/// The collecting decoder this one replaced, as the reference.
|
||||
fn old(text: &[u8], offset: usize) -> Result<Vec<u8>, AepError> {
|
||||
if text.len() % 2 == 1 {
|
||||
return Err(xml(offset, "bdata has an odd number of hex digits"));
|
||||
}
|
||||
let digit = |b: u8| match b {
|
||||
b'0'..=b'9' => Some(b - b'0'),
|
||||
b'a'..=b'f' => Some(b - b'a' + 10),
|
||||
b'A'..=b'F' => Some(b - b'A' + 10),
|
||||
_ => None,
|
||||
};
|
||||
let (pairs, _) = text.as_chunks::<2>();
|
||||
pairs
|
||||
.iter()
|
||||
.map(|[hi, lo]| digit(*hi).zip(digit(*lo)).map(|(h, l)| (h << 4) | l))
|
||||
.collect::<Option<Vec<u8>>>()
|
||||
.ok_or(xml(offset, "bdata holds a character that is not a hex digit"))
|
||||
}
|
||||
let alphabet = b"0123456789abcdefABCDEFgG \0";
|
||||
let mut seed = 0x2545_f491_4f6c_dd1du64;
|
||||
for len in 0..200usize {
|
||||
for _ in 0..20 {
|
||||
let text: Vec<u8> = (0..len)
|
||||
.map(|_| {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 7;
|
||||
seed ^= seed << 17;
|
||||
// Mostly hex digits, now and then a byte that is not one.
|
||||
let i = (seed % 1000) as usize;
|
||||
alphabet[if i < 990 { i % 22 } else { 22 + i % 4 }]
|
||||
})
|
||||
.collect();
|
||||
let new = decode_hex(&text, 7);
|
||||
assert_eq!(new, old(&text, 7));
|
||||
if let Ok(v) = new {
|
||||
assert_eq!(v.capacity(), v.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn limits_hold() {
|
||||
let deep = format!("<AfterEffectsProject>{}{}</AfterEffectsProject>", "<a>".repeat(100_000), "</a>".repeat(100_000));
|
||||
@@ -638,4 +929,293 @@ mod tests {
|
||||
let many = format!("<AfterEffectsProject>{}</AfterEffectsProject>", "<a/>".repeat(10));
|
||||
assert_eq!(read_aepx_with_limits(many.as_bytes(), Limits { max_depth: 64, max_chunks: 9 }), Err(AepError::TooManyChunks));
|
||||
}
|
||||
|
||||
/// A tiny deterministic generator (xorshift64*), so the tests need no extra crate.
|
||||
struct Rng(u64);
|
||||
|
||||
impl Rng {
|
||||
/// The next pseudo-random number.
|
||||
fn next(&mut self) -> u64 {
|
||||
let mut x = self.0;
|
||||
x ^= x >> 12;
|
||||
x ^= x << 25;
|
||||
x ^= x >> 27;
|
||||
self.0 = x;
|
||||
x.wrapping_mul(0x2545_f491_4f6c_dd1d)
|
||||
}
|
||||
|
||||
/// A number below `n` (0 for `n == 0`).
|
||||
fn below(&mut self, n: usize) -> usize {
|
||||
if n == 0 { 0 } else { usize::try_from(self.next() % n as u64).unwrap() }
|
||||
}
|
||||
|
||||
/// One of `items`.
|
||||
fn pick<'s>(&mut self, items: &[&'s str]) -> &'s str {
|
||||
items[self.below(items.len())]
|
||||
}
|
||||
}
|
||||
|
||||
/// `n` random bytes as hex, upper or lower case.
|
||||
fn hex(rng: &mut Rng, n: usize) -> String {
|
||||
let upper = rng.below(4) == 0;
|
||||
(0..n).map(|_| if upper { format!("{:02X}", rng.below(256)) } else { format!("{:02x}", rng.below(256)) }).collect()
|
||||
}
|
||||
|
||||
/// Random `.aepx` content: every element form the reader knows (data, lists, nested ids,
|
||||
/// strings, numbers, `btdk`, file references, skipped elements, XMP packets), `bdata` next to
|
||||
/// children, bodies that read as chunk sequences, entities, CDATA, comments and stray text.
|
||||
fn content(rng: &mut Rng, depth: usize, budget: &mut usize, out: &mut String) {
|
||||
for _ in 0..rng.below(6) {
|
||||
if *budget == 0 {
|
||||
return;
|
||||
}
|
||||
*budget -= 1;
|
||||
match rng.below(20) {
|
||||
0..=3 => {
|
||||
let id = rng.pick(&["cdat", "tdmn", "head", "Utf8", "idta", "LIST", "a"]);
|
||||
let n = rng.below(9);
|
||||
out.push_str(&format!("<{id} bdata=\"{}\"/>", hex(rng, n)));
|
||||
}
|
||||
4..=6 if depth < 10 => {
|
||||
let id = rng.pick(&["Fold", "tdgp", "Layr", "Pin", "LIST", "btdk", "sspc"]);
|
||||
let attr = if rng.below(5) == 0 { format!(" bdata=\"{}\"", hex(rng, 3)) } else { String::new() };
|
||||
out.push_str(&format!("<{id}{attr}>"));
|
||||
content(rng, depth + 1, budget, out);
|
||||
out.push_str(&format!("</{id}>"));
|
||||
}
|
||||
7 => {
|
||||
let id = rng.pick(&["tdsn", "pdnm", "fnam"]);
|
||||
match rng.below(4) {
|
||||
0 => out.push_str(&format!("<{id}/>")),
|
||||
1 => out.push_str(&format!("<{id}><string>name & A</string></{id}>")),
|
||||
// A body that is itself a chunk sequence (`Utf8`, 1 byte, pad): still data.
|
||||
2 => out.push_str(&format!("<{id} bdata=\"557466380000000161{}\"/>", if rng.below(2) == 0 { "00" } else { "" })),
|
||||
_ => out.push_str(&format!("<{id}><string>a</string><string>b</string></{id}>")),
|
||||
}
|
||||
}
|
||||
8 => {
|
||||
let text =
|
||||
rng.pick(&["plain", "A < B", "<![CDATA[x<y]]>", "a<!-- c -->b", "", "été", "x<Skip>gone</Skip>y", "\r\n"]);
|
||||
out.push_str(&format!("<string>{text}</string>"));
|
||||
}
|
||||
9 => {
|
||||
let id = rng.pick(&["numS", "ppSn"]);
|
||||
let inner = rng.pick(&[
|
||||
"<string>0</string>",
|
||||
"<string> 7 </string>",
|
||||
"<string>-2147483648</string>",
|
||||
"<string>150.000000000</string>",
|
||||
"<string>x</string>",
|
||||
"",
|
||||
"<string>1</string><string>2</string>",
|
||||
"<Utf8 bdata=\"31\"/>",
|
||||
"<cdat bdata=\"31\"/>",
|
||||
"<Comment>3</Comment><string>4</string>",
|
||||
]);
|
||||
let attr = if rng.below(6) == 0 { " bdata=\"00000002\"" } else { "" };
|
||||
out.push_str(&format!("<{id}{attr}>{inner}</{id}>"));
|
||||
}
|
||||
10 => {
|
||||
let n = rng.pick(&["", "41", "414243", "4c495354000000000000"]);
|
||||
out.push_str(&format!("<btdk bdata=\"{n}\"/>"));
|
||||
}
|
||||
11 => {
|
||||
let platform = rng.pick(&["Win", "1", "Mac"]);
|
||||
out.push_str(&format!(
|
||||
"<Als2><fileReference ascendcount_base=\"1\" fullpath=\"C:\\a\\"b	.png\" platform=\"{platform}\" target_is_folder=\"{}\"/></Als2>",
|
||||
rng.below(3)
|
||||
));
|
||||
}
|
||||
12 if depth < 10 => {
|
||||
out.push_str("<Comment note=\"&\">text<a bdata=\"00\"/>");
|
||||
content(rng, depth + 1, budget, out);
|
||||
out.push_str("</Comment>");
|
||||
}
|
||||
13 => {
|
||||
let packet = rng.pick(&["<![CDATA[<?xpacket begin?>x<?xpacket end?>]]>", "plain & text", ""]);
|
||||
out.push_str(&format!("<ProjectXMPMetadata>{packet}</ProjectXMPMetadata>"));
|
||||
}
|
||||
14 => out.push_str(rng.pick(&["<!-- comment -->", "<?pi x?>", "\n ", "stray"])),
|
||||
_ => {
|
||||
let n = rng.below(5);
|
||||
out.push_str(&format!("<tdb4 bdata=\"{}\" x='y'></tdb4>", hex(rng, n)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The default limits and six tight ones (depth 0, 1 and 3; 0, 7 and 40 chunks).
|
||||
fn limit_sets() -> [Limits; 7] {
|
||||
[
|
||||
Limits { max_depth: 0, max_chunks: 10_000_000 },
|
||||
Limits { max_depth: 64, max_chunks: 0 },
|
||||
Limits::default(),
|
||||
Limits { max_depth: 1, max_chunks: 10_000_000 },
|
||||
Limits { max_depth: 3, max_chunks: 10_000_000 },
|
||||
Limits { max_depth: 64, max_chunks: 7 },
|
||||
Limits { max_depth: 64, max_chunks: 40 },
|
||||
]
|
||||
}
|
||||
|
||||
/// The index reader and the owned-tree reader give the same `Result` (the tree, or the error),
|
||||
/// and the index walks like the index of the owned tree.
|
||||
fn same(bytes: &[u8], limits: Limits) {
|
||||
let old = read_aepx_with_limits(bytes, limits);
|
||||
let new = index_aepx(bytes, limits).map(|d| TreeIndex::from_drafts(&d.buffer, d.nodes, d.trailer).to_file().into_owned());
|
||||
assert_eq!(new, old, "{}", String::from_utf8_lossy(bytes));
|
||||
if let Ok(file) = &old {
|
||||
let AepxDrafts { buffer, nodes, trailer } = index_aepx(bytes, limits).unwrap();
|
||||
let index = TreeIndex::from_drafts(&buffer, nodes, trailer);
|
||||
let expected = TreeIndex::from_file(file).unwrap();
|
||||
assert_eq!(index.len(), expected.len());
|
||||
assert_eq!(index.trailer(), expected.trailer());
|
||||
let a: Vec<String> = index.walk().map(|(d, n)| format!("{d} {n:?}")).collect();
|
||||
let b: Vec<String> = expected.walk().map(|(d, n)| format!("{d} {n:?}")).collect();
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
/// The two readers give the same tree and errors on generated and damaged documents.
|
||||
#[test]
|
||||
fn the_two_readers_agree_on_generated_and_damaged_documents() {
|
||||
let mut rng = Rng(0x9e37_79b9_7f4a_7c15);
|
||||
let noise = b"<>/=\"'&;#x0aZ \n![]-?";
|
||||
for _ in 0..600 {
|
||||
let mut budget = 80;
|
||||
let mut body = String::new();
|
||||
content(&mut rng, 0, &mut budget, &mut body);
|
||||
let x = doc(&body);
|
||||
for l in limit_sets() {
|
||||
same(x.as_bytes(), l);
|
||||
}
|
||||
for _ in 0..4 {
|
||||
let mut bad = x.clone().into_bytes();
|
||||
for _ in 0..1 + rng.below(3) {
|
||||
let at = rng.below(bad.len());
|
||||
bad[at] = noise[rng.below(noise.len())];
|
||||
}
|
||||
for l in limit_sets() {
|
||||
same(&bad, l);
|
||||
}
|
||||
}
|
||||
let cut = rng.below(x.len());
|
||||
same(&x.as_bytes()[..cut], Limits::default());
|
||||
// Bytes that are not UTF-8, and a multi-byte character cut in half.
|
||||
let mut bad = x.clone().into_bytes();
|
||||
let at = rng.below(bad.len());
|
||||
bad.insert(at, 0xff);
|
||||
same(&bad, Limits::default());
|
||||
let open = format!("{}<string>é", x.trim_end().trim_end_matches("</AfterEffectsProject>")).into_bytes();
|
||||
same(&open[..open.len() - 1], Limits::default());
|
||||
}
|
||||
}
|
||||
|
||||
/// The flat chunk list never reserves more slots than the input or the chunk limit allows.
|
||||
#[test]
|
||||
fn the_chunk_list_is_bounded_by_the_input_and_the_limit() {
|
||||
let many = format!("<AfterEffectsProject>{}</AfterEffectsProject>", "<a/>".repeat(10_000));
|
||||
let d = index_aepx(many.as_bytes(), Limits::default()).unwrap();
|
||||
assert_eq!(d.nodes.len(), 10_000);
|
||||
assert!(d.nodes.capacity() <= 2 * 10_000, "{} slots for 10 000 chunks", d.nodes.capacity());
|
||||
// The buffer is reserved once, at half the input, and holds nothing here.
|
||||
assert!(d.buffer.is_empty() && d.buffer.capacity() <= many.len() / 2);
|
||||
// At the limit the list never holds more slots than the limit.
|
||||
let d = index_aepx(many.as_bytes(), Limits { max_depth: 64, max_chunks: 10_000 }).unwrap();
|
||||
assert!(d.nodes.capacity() <= 10_000);
|
||||
let few = format!("<AfterEffectsProject>{}</AfterEffectsProject>", "<a/>".repeat(100));
|
||||
let d = index_aepx(few.as_bytes(), Limits { max_depth: 64, max_chunks: 100 }).unwrap();
|
||||
assert!(d.nodes.capacity() <= 100, "{} slots for at most 100 chunks", d.nodes.capacity());
|
||||
assert_eq!(index_aepx(few.as_bytes(), Limits { max_depth: 64, max_chunks: 99 }).err(), Some(AepError::TooManyChunks));
|
||||
// A tiny input cannot hold more than a third of its bytes in elements.
|
||||
let tiny = doc("<a/>");
|
||||
assert!(index_aepx(tiny.as_bytes(), Limits::default()).unwrap().nodes.capacity() <= tiny.len() / 3);
|
||||
}
|
||||
|
||||
/// What a closed element drops is gone from the buffer and the chunk list, but was counted.
|
||||
#[test]
|
||||
fn dropped_content_leaves_nothing_behind_but_still_counts() {
|
||||
// `bdata` wins over the children: they are read, counted and dropped.
|
||||
let x = doc("<a bdata=\"01\"><b bdata=\"0203\"/><string>xyz</string><tdsn><string>q</string></tdsn></a>");
|
||||
let d = index_aepx(x.as_bytes(), Limits::default()).unwrap();
|
||||
assert_eq!((d.buffer.as_slice(), d.nodes.len()), (&[1u8][..], 1));
|
||||
assert_eq!(index_aepx(x.as_bytes(), Limits { max_depth: 64, max_chunks: 4 }).err(), Some(AepError::TooManyChunks));
|
||||
assert!(index_aepx(x.as_bytes(), Limits { max_depth: 64, max_chunks: 5 }).is_ok());
|
||||
// A child's error is raised although the child would be dropped.
|
||||
assert!(matches!(index_aepx(doc("<a bdata=\"01\"><numS></numS></a>").as_bytes(), Limits::default()), Err(AepError::Xml { .. })));
|
||||
// Only the first XMP packet stays in the buffer.
|
||||
let packets = doc(&"<ProjectXMPMetadata>packet</ProjectXMPMetadata>".repeat(1000));
|
||||
let d = index_aepx(packets.as_bytes(), Limits::default()).unwrap();
|
||||
assert_eq!((d.buffer.as_slice(), d.trailer), (&b"packet"[..], (0, 6)));
|
||||
// A number keeps its 4 or 8 bytes, not its text.
|
||||
let d = index_aepx(doc("<numS><string> 7 </string></numS><ppSn><string>1.5</string></ppSn>").as_bytes(), Limits::default()).unwrap();
|
||||
assert_eq!(d.buffer, [&[0, 0, 0, 7][..], &1.5f64.to_be_bytes()[..]].concat());
|
||||
assert_eq!(d.nodes.len(), 2);
|
||||
}
|
||||
|
||||
/// Bodies keep what the element says they are, even where reading the same bytes as an `.aep`
|
||||
/// would decide otherwise: a nested id whose `bdata` is a chunk sequence stays data, an empty
|
||||
/// `btdk` stays an opaque list, an empty list at the deepest level stays a list.
|
||||
#[test]
|
||||
fn bodies_keep_the_kind_the_element_gives_them() {
|
||||
let x = doc("<tdsn bdata=\"5574663800000001610000\"/><btdk bdata=\"\"/><btdk bdata=\"4c4953540000000474657374\"/>");
|
||||
let AepxDrafts { buffer, nodes, trailer } = index_aepx(x.as_bytes(), Limits::default()).unwrap();
|
||||
let index = TreeIndex::from_drafts(&buffer, nodes, trailer);
|
||||
let top: Vec<_> = index.chunks().collect();
|
||||
assert_eq!(top.len(), 3);
|
||||
assert_eq!(top[0].bytes().map(<[u8]>::len), Some(11));
|
||||
assert!(top[0].children().next().is_none());
|
||||
assert!(top[1].is_opaque_list() && top[1].bytes() == Some(&[][..]));
|
||||
assert!(top[2].is_opaque_list() && top[2].bytes().map(<[u8]>::len) == Some(12));
|
||||
// 64 levels: the innermost (empty) list is a list, not opaque.
|
||||
let deep = format!("<AfterEffectsProject>{}{}</AfterEffectsProject>", "<Fold>".repeat(64), "</Fold>".repeat(64));
|
||||
let AepxDrafts { buffer, nodes, trailer } = index_aepx(deep.as_bytes(), Limits::default()).unwrap();
|
||||
let index = TreeIndex::from_drafts(&buffer, nodes, trailer);
|
||||
assert_eq!(index.walk().map(|(d, _)| d).max(), Some(64));
|
||||
assert!(index.walk().all(|(_, n)| n.is_list(b"Fold") && !n.is_opaque_list()));
|
||||
same(x.as_bytes(), Limits::default());
|
||||
same(deep.as_bytes(), Limits::default());
|
||||
}
|
||||
|
||||
/// Hostile input is refused by both readers with the same errors.
|
||||
#[test]
|
||||
fn hostile_input_is_refused_alike_by_both_readers() {
|
||||
let hostile = [
|
||||
format!("<AfterEffectsProject>{}{}</AfterEffectsProject>", "<a>".repeat(100_000), "</a>".repeat(100_000)),
|
||||
format!("<AfterEffectsProject>{}", "<string>".repeat(1000)),
|
||||
doc(&format!("<a bdata=\"{}0\"/>", "00".repeat(100_000))),
|
||||
doc(&format!("<a bdata=\"{}zz\"/>", "00".repeat(100_000))),
|
||||
doc("<numS><string>4294967296</string></numS>"),
|
||||
doc("<ppSn><string>1e400x</string></ppSn>"),
|
||||
doc("<string>�</string>"),
|
||||
doc("<string>�</string>"),
|
||||
doc(&format!("<Fold>{}", "<b/>".repeat(1000))),
|
||||
format!("<AfterEffectsProject>{}</AfterEffectsProject><x/>", "<a/>".repeat(10)),
|
||||
"<AfterEffectsProject><a bdata=\"00".to_string(),
|
||||
"<AfterEffectsProject><![CDATA[x".to_string(),
|
||||
];
|
||||
for x in &hostile {
|
||||
assert!(read_aepx(x.as_bytes()).is_err(), "{}", &x[..x.len().min(80)]);
|
||||
for l in limit_sets() {
|
||||
same(x.as_bytes(), l);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every `.aepx` of the fixture directory gives the same tree through both readers (skipped
|
||||
/// without `CRAFT_AEP_FIXTURES`).
|
||||
#[test]
|
||||
fn every_fixture_aepx_gives_the_same_tree_through_both_readers() {
|
||||
let Some(dir) = std::env::var_os("CRAFT_AEP_FIXTURES").filter(|d| !d.is_empty()) else { return };
|
||||
let Ok(entries) = std::fs::read_dir(dir) else { return };
|
||||
let mut paths: Vec<std::path::PathBuf> =
|
||||
entries.filter_map(Result::ok).map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "aepx")).collect();
|
||||
paths.sort();
|
||||
for path in paths {
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
for l in limit_sets() {
|
||||
let index = index_aepx(&bytes, l).map(|d| TreeIndex::from_drafts(&d.buffer, d.nodes, d.trailer).to_file().into_owned());
|
||||
assert_eq!(index, read_aepx_with_limits(&bytes, l), "{}", path.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
//! The `.aepx` reader of the owned chunk tree ([`super::read_aepx`]): it builds the tree element by
|
||||
//! element. The importer uses the index reader in `aepx.rs` instead; the tests compare the two on
|
||||
//! generated, damaged and corpus input (the same tree and the same errors).
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::{FILE_REFERENCE, OPAQUE_LIST_TYPES, ROOT, STRING, XMP, alias_json, chunk_id, decode_entities, decode_hex, xml};
|
||||
use crate::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS};
|
||||
use crate::error::AepError;
|
||||
use crate::read::Limits;
|
||||
|
||||
/// [`super::read_aepx_with_limits`].
|
||||
pub(super) fn read(data: &[u8], limits: Limits) -> Result<AepFile<'static>, AepError> {
|
||||
let text = match std::str::from_utf8(data) {
|
||||
Ok(t) => t.as_bytes(),
|
||||
Err(e) => return Err(xml(e.valid_up_to(), "the file is not valid UTF-8")),
|
||||
};
|
||||
Reader { data: text, pos: 0, limits, count: 0, stack: Vec::new(), top: None, trailer: None }.run()
|
||||
}
|
||||
|
||||
/// What an open element becomes when it closes.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind {
|
||||
/// The root element: its children are the top-level chunks.
|
||||
Root,
|
||||
/// A chunk with this id (or a `LIST` with this list type).
|
||||
Chunk([u8; 4]),
|
||||
/// A `Utf8` chunk.
|
||||
Utf8,
|
||||
/// An `alas` chunk built from the attributes.
|
||||
Alias,
|
||||
/// The XMP packet (only directly below the root).
|
||||
Xmp,
|
||||
/// An element without a chunk counterpart (a name that is no chunk id); skipped with its content.
|
||||
Skip,
|
||||
}
|
||||
|
||||
struct Frame {
|
||||
kind: Kind,
|
||||
/// Offset of the start tag (for errors).
|
||||
at: usize,
|
||||
/// The name as written, to match the end tag.
|
||||
name: (usize, usize),
|
||||
/// The decoded `bdata` attribute.
|
||||
bdata: Option<Vec<u8>>,
|
||||
/// The attributes of a `fileReference`, in document order.
|
||||
attrs: Vec<(String, String)>,
|
||||
/// Collected text (only for `string` and the XMP packet).
|
||||
text: Vec<u8>,
|
||||
children: Vec<Chunk<'static>>,
|
||||
}
|
||||
|
||||
struct Reader<'d> {
|
||||
data: &'d [u8],
|
||||
pos: usize,
|
||||
limits: Limits,
|
||||
count: usize,
|
||||
stack: Vec<Frame>,
|
||||
/// The top-level chunks once the root element has closed.
|
||||
top: Option<Vec<Chunk<'static>>>,
|
||||
trailer: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
fn run(mut self) -> Result<AepFile<'static>, AepError> {
|
||||
if self.data.starts_with(&[0xef, 0xbb, 0xbf]) {
|
||||
self.pos = 3;
|
||||
}
|
||||
while self.pos < self.data.len() {
|
||||
if self.rest().starts_with(b"<") {
|
||||
self.markup()?;
|
||||
} else {
|
||||
self.text()?;
|
||||
}
|
||||
}
|
||||
if let Some(open) = self.stack.last() {
|
||||
return Err(xml(open.at, "an element is not closed before the end of the file"));
|
||||
}
|
||||
let chunks = self.top.ok_or_else(|| xml(self.data.len(), "no AfterEffectsProject root element"))?;
|
||||
Ok(AepFile { chunks, trailer: Cow::Owned(self.trailer.unwrap_or_default()) })
|
||||
}
|
||||
|
||||
fn rest(&self) -> &[u8] {
|
||||
self.data.get(self.pos..).unwrap_or(&[])
|
||||
}
|
||||
|
||||
/// The offset of `needle` at or after `from`.
|
||||
fn find(&self, from: usize, needle: &[u8]) -> Option<usize> {
|
||||
let hay = self.data.get(from..)?;
|
||||
if needle.is_empty() || hay.len() < needle.len() {
|
||||
return None;
|
||||
}
|
||||
let first = *needle.first()?;
|
||||
let mut i = 0;
|
||||
while let Some(p) = hay.get(i..)?.iter().position(|b| *b == first) {
|
||||
let at = i.checked_add(p)?;
|
||||
if hay.get(at..)?.starts_with(needle) {
|
||||
return from.checked_add(at);
|
||||
}
|
||||
i = at.checked_add(1)?;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Skips to just after `close`, which must follow.
|
||||
fn skip_past(&mut self, close: &[u8], what: &'static str) -> Result<usize, AepError> {
|
||||
let at = self.find(self.pos, close).ok_or(xml(self.pos, what))?;
|
||||
self.pos = at.saturating_add(close.len());
|
||||
Ok(at)
|
||||
}
|
||||
|
||||
fn markup(&mut self) -> Result<(), AepError> {
|
||||
let rest = self.rest();
|
||||
if rest.starts_with(b"<?") {
|
||||
self.skip_past(b"?>", "a processing instruction is not closed")?;
|
||||
} else if rest.starts_with(b"<!--") {
|
||||
self.skip_past(b"-->", "a comment is not closed")?;
|
||||
} else if rest.starts_with(b"<![CDATA[") {
|
||||
let start = self.pos.saturating_add(9);
|
||||
self.pos = start;
|
||||
let end = self.skip_past(b"]]>", "a CDATA section is not closed")?;
|
||||
let content = self.data.get(start..end).unwrap_or(&[]);
|
||||
let at = start;
|
||||
match self.stack.last_mut() {
|
||||
Some(frame) => {
|
||||
if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) {
|
||||
frame.text.extend_from_slice(content);
|
||||
}
|
||||
}
|
||||
None => return Err(xml(at, "CDATA outside the root element")),
|
||||
}
|
||||
} else if rest.starts_with(b"<!") {
|
||||
return Err(xml(self.pos, "document type declarations are not supported"));
|
||||
} else if rest.starts_with(b"</") {
|
||||
self.end_tag()?;
|
||||
} else {
|
||||
self.start_tag()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_name_byte(b: u8) -> bool {
|
||||
!(b.is_ascii_whitespace() || matches!(b, b'<' | b'>' | b'/' | b'=' | b'"' | b'\'' | b'&'))
|
||||
}
|
||||
|
||||
fn name(&mut self) -> Result<(usize, usize), AepError> {
|
||||
let start = self.pos;
|
||||
let len = self.rest().iter().position(|b| !Self::is_name_byte(*b)).unwrap_or(self.rest().len());
|
||||
if len == 0 {
|
||||
return Err(xml(start, "a tag or attribute has no name"));
|
||||
}
|
||||
self.pos = start.saturating_add(len);
|
||||
Ok((start, self.pos))
|
||||
}
|
||||
|
||||
fn skip_ws(&mut self) {
|
||||
let n = self.rest().iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(self.rest().len());
|
||||
self.pos = self.pos.saturating_add(n);
|
||||
}
|
||||
|
||||
fn slice(&self, (a, b): (usize, usize)) -> &[u8] {
|
||||
self.data.get(a..b).unwrap_or(&[])
|
||||
}
|
||||
|
||||
fn start_tag(&mut self) -> Result<(), AepError> {
|
||||
let at = self.pos;
|
||||
self.pos = self.pos.saturating_add(1);
|
||||
let name = self.name()?;
|
||||
let mut attrs = Vec::new();
|
||||
let self_closing = loop {
|
||||
self.skip_ws();
|
||||
let rest = self.rest();
|
||||
if rest.starts_with(b"/>") {
|
||||
self.pos = self.pos.saturating_add(2);
|
||||
break true;
|
||||
}
|
||||
if rest.starts_with(b">") {
|
||||
self.pos = self.pos.saturating_add(1);
|
||||
break false;
|
||||
}
|
||||
if rest.is_empty() {
|
||||
return Err(xml(at, "a start tag is not closed"));
|
||||
}
|
||||
let key = self.name()?;
|
||||
self.skip_ws();
|
||||
if !self.rest().starts_with(b"=") {
|
||||
return Err(xml(self.pos, "an attribute has no value"));
|
||||
}
|
||||
self.pos = self.pos.saturating_add(1);
|
||||
self.skip_ws();
|
||||
let quote = *self.rest().first().ok_or(xml(self.pos, "a start tag is not closed"))?;
|
||||
if quote != b'"' && quote != b'\'' {
|
||||
return Err(xml(self.pos, "an attribute value is not quoted"));
|
||||
}
|
||||
let vstart = self.pos.saturating_add(1);
|
||||
let vend = self.find(vstart, &[quote]).ok_or(xml(vstart, "an attribute value is not closed"))?;
|
||||
let raw = self.data.get(vstart..vend).unwrap_or(&[]);
|
||||
if raw.contains(&b'<') {
|
||||
return Err(xml(vstart, "`<` inside an attribute value"));
|
||||
}
|
||||
// A value without `&` is its own decoding (most of them, every `bdata` After Effects
|
||||
// writes): borrow it instead of copying it.
|
||||
let value = if raw.contains(&b'&') {
|
||||
let mut value = Vec::new();
|
||||
decode_entities(raw, vstart, &mut value)?;
|
||||
Cow::Owned(value)
|
||||
} else {
|
||||
Cow::Borrowed(raw)
|
||||
};
|
||||
self.pos = vend.saturating_add(1);
|
||||
attrs.push((key, value));
|
||||
};
|
||||
let kind = self.kind_of(name, at)?;
|
||||
if kind != Kind::Root {
|
||||
self.count = self.count.saturating_add(1);
|
||||
if self.count > self.limits.max_chunks {
|
||||
return Err(AepError::TooManyChunks);
|
||||
}
|
||||
// The root's children are depth 1, like the top-level chunks of an `.aep`.
|
||||
if self.stack.len() > self.limits.max_depth {
|
||||
return Err(AepError::TooDeep);
|
||||
}
|
||||
}
|
||||
let mut frame = Frame { kind, at, name, bdata: None, attrs: Vec::new(), text: Vec::new(), children: Vec::new() };
|
||||
if matches!(kind, Kind::Chunk(_)) {
|
||||
if let Some((key, value)) = attrs.iter().find(|(k, _)| self.slice(*k) == b"bdata") {
|
||||
frame.bdata = Some(decode_hex(value, key.1)?);
|
||||
}
|
||||
} else if kind == Kind::Alias {
|
||||
frame.attrs =
|
||||
attrs.iter().map(|(k, v)| (String::from_utf8_lossy(self.slice(*k)).into_owned(), String::from_utf8_lossy(v).into_owned())).collect();
|
||||
}
|
||||
self.stack.push(frame);
|
||||
if self_closing {
|
||||
self.close()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Classifies an element by its name and where it is.
|
||||
fn kind_of(&self, name: (usize, usize), at: usize) -> Result<Kind, AepError> {
|
||||
let n = self.slice(name);
|
||||
let parent = match self.stack.last() {
|
||||
None => {
|
||||
if self.top.is_some() {
|
||||
return Err(xml(at, "a second root element"));
|
||||
}
|
||||
if n != ROOT {
|
||||
return Err(xml(at, "the root element is not AfterEffectsProject"));
|
||||
}
|
||||
return Ok(Kind::Root);
|
||||
}
|
||||
Some(p) => p.kind,
|
||||
};
|
||||
Ok(match parent {
|
||||
Kind::Skip | Kind::Utf8 | Kind::Xmp | Kind::Alias => Kind::Skip,
|
||||
Kind::Root | Kind::Chunk(_) => {
|
||||
if n == STRING {
|
||||
Kind::Utf8
|
||||
} else if n == FILE_REFERENCE {
|
||||
Kind::Alias
|
||||
} else if n == XMP && parent == Kind::Root {
|
||||
Kind::Xmp
|
||||
} else if let Some(id) = chunk_id(n) {
|
||||
Kind::Chunk(id)
|
||||
} else {
|
||||
Kind::Skip
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn end_tag(&mut self) -> Result<(), AepError> {
|
||||
let at = self.pos;
|
||||
self.pos = self.pos.saturating_add(2);
|
||||
let name = self.name()?;
|
||||
self.skip_ws();
|
||||
if !self.rest().starts_with(b">") {
|
||||
return Err(xml(at, "an end tag is not closed"));
|
||||
}
|
||||
self.pos = self.pos.saturating_add(1);
|
||||
let open = self.stack.last().ok_or(xml(at, "an end tag without a start tag"))?;
|
||||
if self.slice(open.name) != self.slice(name) {
|
||||
return Err(xml(at, "an end tag does not match its start tag"));
|
||||
}
|
||||
self.close()
|
||||
}
|
||||
|
||||
/// Closes the innermost element and hands its chunk to the parent.
|
||||
fn close(&mut self) -> Result<(), AepError> {
|
||||
let frame = self.stack.pop().ok_or(xml(self.pos, "an end tag without a start tag"))?;
|
||||
let chunk = match frame.kind {
|
||||
Kind::Root => {
|
||||
self.top = Some(frame.children);
|
||||
return Ok(());
|
||||
}
|
||||
Kind::Skip => return Ok(()),
|
||||
Kind::Xmp => {
|
||||
if self.trailer.is_none() {
|
||||
self.trailer = Some(frame.text);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Kind::Utf8 => Chunk::data(*b"Utf8", frame.text),
|
||||
Kind::Alias => Chunk::data(*b"alas", alias_json(&frame.attrs).into_bytes()),
|
||||
Kind::Chunk(id) => chunk_of(id, frame)?,
|
||||
};
|
||||
if let Some(parent) = self.stack.last_mut() {
|
||||
parent.children.push(chunk);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn text(&mut self) -> Result<(), AepError> {
|
||||
let start = self.pos;
|
||||
let len = self.rest().iter().position(|b| *b == b'<').unwrap_or(self.rest().len());
|
||||
let end = start.saturating_add(len);
|
||||
self.pos = end;
|
||||
let raw = self.data.get(start..end).unwrap_or(&[]);
|
||||
match self.stack.last_mut() {
|
||||
Some(frame) if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) => decode_entities(raw, start, &mut frame.text),
|
||||
Some(_) => Ok(()),
|
||||
None if raw.iter().all(u8::is_ascii_whitespace) => Ok(()),
|
||||
None => Err(xml(start, "text outside the root element")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The chunk of a closed element named by a chunk id.
|
||||
fn chunk_of(id: [u8; 4], frame: Frame) -> Result<Chunk<'static>, AepError> {
|
||||
if let Some(bytes) = frame.bdata {
|
||||
if OPAQUE_LIST_TYPES.contains(&id) {
|
||||
let pad = if bytes.len() % 2 == 1 { Some(0) } else { None };
|
||||
return Ok(Chunk { id: LIST, body: Body::OpaqueList { list_type: id, data: Cow::Owned(bytes) }, pad });
|
||||
}
|
||||
return Ok(Chunk::data(id, bytes));
|
||||
}
|
||||
// Numbers written as text: `<numS><string>0</string></numS>` and `<ppSn><string>150.000000000</string></ppSn>`.
|
||||
if &id == b"numS" || &id == b"ppSn" {
|
||||
let text = match frame.children.as_slice() {
|
||||
[only] if only.is(b"Utf8") => only.bytes().unwrap_or(&[]),
|
||||
_ => return Err(xml(frame.at, "a numS or ppSn element without its number")),
|
||||
};
|
||||
let text = std::str::from_utf8(text).map_err(|_| xml(frame.at, "a number is not text"))?.trim();
|
||||
let body = if &id == b"numS" {
|
||||
// Only `0` is sampled, so whether the field is signed is open: accept both readings of 4 bytes.
|
||||
let n = text.parse::<i64>().ok().filter(|n| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(n));
|
||||
let n = n.ok_or(xml(frame.at, "numS is not a 32-bit integer"))?;
|
||||
(n as u32).to_be_bytes().to_vec()
|
||||
} else {
|
||||
text.parse::<f64>().map_err(|_| xml(frame.at, "ppSn is not a number"))?.to_be_bytes().to_vec()
|
||||
};
|
||||
return Ok(Chunk::data(id, body));
|
||||
}
|
||||
if NESTED_IDS.contains(&id) {
|
||||
if frame.children.is_empty() {
|
||||
return Ok(Chunk::data(id, Vec::new()));
|
||||
}
|
||||
return Ok(Chunk { id, body: Body::Chunks(frame.children), pad: None });
|
||||
}
|
||||
Ok(Chunk::list(id, frame.children))
|
||||
}
|
||||
@@ -7,9 +7,9 @@
|
||||
//! Hue/Saturation (444-446) or a Levels value (512-516) changes only bytes of this `aRbp` (and stale
|
||||
//! UI bytes of `pard`); the visible parameters of the same names keep no record.
|
||||
|
||||
use crate::Chunk;
|
||||
use crate::bytes::{be_f32, be_i32, be_u16, be_u32};
|
||||
use crate::effects::Effect;
|
||||
use crate::index::Node;
|
||||
|
||||
impl<'f, 'a> Effect<'f, 'a> {
|
||||
/// The arbitrary data stored for the parameter with this match name (`<effect>-NNNN`): the
|
||||
@@ -17,13 +17,13 @@ impl<'f, 'a> Effect<'f, 'a> {
|
||||
/// none.
|
||||
pub fn arbitrary_data(&self, match_name: &str) -> Option<&'f [u8]> {
|
||||
let values = self.chunk().child_list(b"tdgp")?;
|
||||
let kids = values.children();
|
||||
let at = kids.iter().position(|c| c.is(b"tdmn") && c.bytes().is_some_and(|b| name_is(b, match_name)))?;
|
||||
// The value record, then the data list (no match name of its own).
|
||||
let rest = kids.get(at.checked_add(1)?..)?;
|
||||
let mut it = rest.iter().take(3);
|
||||
let mut kids = values.children();
|
||||
// Up to the parameter's match name, then the value record, then the data list (no match
|
||||
// name of its own).
|
||||
kids.find(|c| c.is(b"tdmn") && c.bytes().is_some_and(|b| name_is(b, match_name)))?;
|
||||
let mut it = kids.take(3);
|
||||
let _record = it.next().filter(|c| c.is_list(b"tdbs"))?;
|
||||
it.find(|c| c.is_list(b"aRbs")).and_then(|l| l.child(b"aRbp")).and_then(Chunk::bytes)
|
||||
it.find(|c| c.is_list(b"aRbs")).and_then(|l| l.child(b"aRbp")).and_then(Node::bytes)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
//! only when it is not the default 100.
|
||||
|
||||
use crate::typed::{Layer, PropNode};
|
||||
use crate::values::{Tracked, ValueSource, read, read_opt};
|
||||
|
||||
/// The Position of a camera or light layer as stored.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
@@ -28,16 +27,6 @@ pub struct CameraPosition {
|
||||
pub auto_state: bool,
|
||||
}
|
||||
|
||||
impl CameraPosition {
|
||||
/// The position as the batch-3 notes had After Effects report it: in the auto state of a
|
||||
/// two-node camera (`two_node`) x and y are the composition centre; otherwise the stored
|
||||
/// value. **Contradicted by 256 and 537 (session S16): After Effects reports and renders the
|
||||
/// stored value**; use `stored` for what After Effects shows.
|
||||
pub fn reported(&self, comp_width: u32, comp_height: u32, two_node: bool) -> [f64; 3] {
|
||||
if self.auto_state && two_node { [f64::from(comp_width) / 2.0, f64::from(comp_height) / 2.0, self.stored[2]] } else { self.stored }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'f, 'a> Layer<'f, 'a> {
|
||||
/// The Position record of a camera or light layer.
|
||||
pub fn camera_position(&self) -> Option<CameraPosition> {
|
||||
@@ -49,87 +38,9 @@ impl<'f, 'a> Layer<'f, 'a> {
|
||||
let derived = h.get(76) == Some(&1) && h.get(77) == Some(&1) && position.expression().is_none();
|
||||
Some(CameraPosition { stored, auto_state: derived })
|
||||
}
|
||||
|
||||
/// The point of interest of a camera in pixels (its Anchor Point record); `None` when absent.
|
||||
pub fn camera_point_of_interest(&self) -> Option<[f64; 3]> {
|
||||
let Some(PropNode::Group(transform)) = self.properties()?.get("ADBE Transform Group") else { return None };
|
||||
let tracked = read(&transform, "ADBE Anchor Point", [0.0; 3], 1.0);
|
||||
matches!(tracked.source, ValueSource::Stored | ValueSource::Keyframed).then_some(tracked.value)
|
||||
}
|
||||
|
||||
/// A light's intensity in percent; the default 100 when the property is absent.
|
||||
pub fn light_intensity(&self) -> Option<Tracked<f64>> {
|
||||
let root = self.properties()?;
|
||||
let Some(PropNode::Group(options)) = root.get("ADBE Light Options Group") else { return None };
|
||||
let t = read(&options, "ADBE Light Intensity", [100.0], 1.0);
|
||||
Some(Tracked { value: t.value[0], source: t.source })
|
||||
}
|
||||
}
|
||||
|
||||
/// The camera zoom After Effects uses when `ADBE Camera Zoom` is absent: 2666.67 px for the 50 mm
|
||||
/// default camera in the 1920 px wide corpus composition. Whether it follows other composition
|
||||
/// widths was not sampled.
|
||||
pub const DEFAULT_CAMERA_ZOOM: f64 = 2_666.666_666_666_666_5;
|
||||
|
||||
impl<'f, 'a> Layer<'f, 'a> {
|
||||
fn camera_options(&self) -> Option<crate::typed::PropGroup<'f, 'a>> {
|
||||
match self.properties()?.get("ADBE Camera Options Group") {
|
||||
Some(PropNode::Group(g)) => Some(g),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn light_options(&self) -> Option<crate::typed::PropGroup<'f, 'a>> {
|
||||
match self.properties()?.get("ADBE Light Options Group") {
|
||||
Some(PropNode::Group(g)) => Some(g),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A camera's zoom in pixels (`ADBE Camera Options Group/ADBE Camera Zoom`); absent = the
|
||||
/// default, [`DEFAULT_CAMERA_ZOOM`] (341: 2000 is stored). The auto Position keeps `-2666.67` and
|
||||
/// is not recomputed from the zoom.
|
||||
pub fn camera_zoom(&self) -> Option<Tracked<f64>> {
|
||||
let t = read(&self.camera_options()?, "ADBE Camera Zoom", [DEFAULT_CAMERA_ZOOM], 1.0);
|
||||
Some(Tracked { value: t.value[0], source: t.source })
|
||||
}
|
||||
|
||||
/// The depth of field switch (`ADBE Camera Depth of Field`, a checkbox; off by default, 342).
|
||||
pub fn camera_depth_of_field(&self) -> Option<Tracked<bool>> {
|
||||
let t = read(&self.camera_options()?, "ADBE Camera Depth of Field", [0.0], 1.0);
|
||||
Some(Tracked { value: t.value[0] != 0.0, source: t.source })
|
||||
}
|
||||
|
||||
/// A camera's focus distance in pixels (`ADBE Camera Focus Distance`); `None` while absent (its
|
||||
/// default is not known).
|
||||
pub fn camera_focus_distance(&self) -> Option<Tracked<f64>> {
|
||||
let t = read_opt::<1>(&self.camera_options()?, "ADBE Camera Focus Distance", 1.0)?;
|
||||
Some(Tracked { value: t.value[0], source: t.source })
|
||||
}
|
||||
|
||||
/// A camera's aperture (`ADBE Camera Aperture`); `None` while absent (its default is not known).
|
||||
pub fn camera_aperture(&self) -> Option<Tracked<f64>> {
|
||||
let t = read_opt::<1>(&self.camera_options()?, "ADBE Camera Aperture", 1.0)?;
|
||||
Some(Tracked { value: t.value[0], source: t.source })
|
||||
}
|
||||
|
||||
/// A light's colour as `[A, R, G, B]` fractions (`ADBE Light Options Group/ADBE Light Color`,
|
||||
/// stored as doubles 0..255); white when absent (343 stores red).
|
||||
pub fn light_color(&self) -> Option<Tracked<[f64; 4]>> {
|
||||
Some(read(&self.light_options()?, "ADBE Light Color", [1.0; 4], 1.0 / 255.0))
|
||||
}
|
||||
|
||||
/// A spot light's cone angle in degrees (`ADBE Light Cone Angle`); 90 when absent (344 stores 60).
|
||||
/// A point light stores 90 with `tdsb` 3.
|
||||
pub fn light_cone_angle(&self) -> Option<Tracked<f64>> {
|
||||
let t = read(&self.light_options()?, "ADBE Light Cone Angle", [90.0], 1.0);
|
||||
Some(Tracked { value: t.value[0], source: t.source })
|
||||
}
|
||||
|
||||
/// A spot light's cone feather in percent (`ADBE Light Cone Feather 2`); 50 when absent (345
|
||||
/// stores 30).
|
||||
pub fn light_cone_feather(&self) -> Option<Tracked<f64>> {
|
||||
let t = read(&self.light_options()?, "ADBE Light Cone Feather 2", [50.0], 1.0);
|
||||
Some(Tracked { value: t.value[0], source: t.source })
|
||||
}
|
||||
}
|
||||
|
||||
+144
-59
@@ -20,8 +20,8 @@
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::Chunk;
|
||||
use crate::bytes::{array, be_f32, be_f64, be_i32, be_u16, be_u32, cstr, text, until_nul};
|
||||
use crate::index::Node;
|
||||
use crate::typed::{KeyList, Layer, Project, PropGroup, PropNode, Property};
|
||||
|
||||
/// The parameter type, the `u32` at byte 12 of `pard`. The numbering matches the plug-in
|
||||
@@ -132,7 +132,7 @@ pub enum LayerRef {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Effect<'f, 'a> {
|
||||
match_name: String,
|
||||
chunk: &'f Chunk<'a>,
|
||||
chunk: Node<'f, 'a>,
|
||||
}
|
||||
|
||||
/// Where the parameter definitions of an effect instance come from.
|
||||
@@ -178,13 +178,13 @@ impl<'f, 'a> Effect<'f, 'a> {
|
||||
}
|
||||
|
||||
/// The underlying `LIST sspc` chunk.
|
||||
pub fn chunk(&self) -> &'f Chunk<'a> {
|
||||
pub fn chunk(&self) -> Node<'f, 'a> {
|
||||
self.chunk
|
||||
}
|
||||
|
||||
/// The instance name shown in the effect controls (`fnam`).
|
||||
pub fn name(&self) -> Option<String> {
|
||||
self.chunk.child(b"fnam")?.children().iter().find(|c| c.is(b"Utf8"))?.bytes().map(text)
|
||||
self.chunk.child(b"fnam")?.child(b"Utf8")?.bytes().map(text)
|
||||
}
|
||||
|
||||
/// The name the user gave the instance: the `tdsn` of the instance's value group when it is
|
||||
@@ -192,7 +192,7 @@ impl<'f, 'a> Effect<'f, 'a> {
|
||||
/// `None` while the instance shows the effect's name.
|
||||
pub fn user_name(&self) -> Option<String> {
|
||||
let values = self.chunk.child_list(b"tdgp")?;
|
||||
let name = values.child(b"tdsn")?.children().iter().find(|c| c.is(b"Utf8"))?.bytes().map(text)?;
|
||||
let name = values.child(b"tdsn")?.child(b"Utf8")?.bytes().map(text)?;
|
||||
(name != "-_0_/-" && !name.is_empty()).then_some(name)
|
||||
}
|
||||
|
||||
@@ -250,7 +250,7 @@ impl<'f, 'a> Effect<'f, 'a> {
|
||||
}
|
||||
|
||||
/// The parameters described by `part` (a `LIST parT`) with the values of this instance.
|
||||
fn params_from(&self, part: &'f Chunk<'a>, borrowed: bool) -> Vec<EffectParam<'f, 'a>> {
|
||||
fn params_from(&self, part: Node<'f, 'a>, borrowed: bool) -> Vec<EffectParam<'f, 'a>> {
|
||||
let mut out: Vec<EffectParam<'f, 'a>> = Vec::new();
|
||||
// The stored values by match name: the first entry of a name, as `PropGroup::get` finds it
|
||||
// (a group of that name means no stored value). Indexed once: a lookup per parameter
|
||||
@@ -271,8 +271,8 @@ impl<'f, 'a> Effect<'f, 'a> {
|
||||
out.push(EffectParam {
|
||||
index: out.len(),
|
||||
match_name,
|
||||
kind: ParamKind::from_u32(be_u32(pard, 12).unwrap_or(u32::MAX)),
|
||||
name: cstr(pard, 16, 32).unwrap_or_default(),
|
||||
kind: Pard(pard).kind(),
|
||||
name: Pard(pard).name().unwrap_or_default(),
|
||||
items: None,
|
||||
borrowed_definition: borrowed,
|
||||
pard,
|
||||
@@ -280,7 +280,7 @@ impl<'f, 'a> Effect<'f, 'a> {
|
||||
});
|
||||
} else if c.is(b"pdnm")
|
||||
&& let Some(last) = out.last_mut()
|
||||
&& let Some(u) = c.children().iter().find(|u| u.is(b"Utf8"))
|
||||
&& let Some(u) = c.child(b"Utf8")
|
||||
&& let Some(b) = u.bytes()
|
||||
// An empty `pdnm` names nothing (some writers store one: the effect
|
||||
// supplies its items).
|
||||
@@ -293,8 +293,9 @@ impl<'f, 'a> Effect<'f, 'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn has_pard(part: &Chunk<'_>) -> bool {
|
||||
part.children().iter().any(|c| c.is(b"pard"))
|
||||
/// True when a `LIST parT` holds parameter definitions (a `pard`).
|
||||
fn has_pard(part: Node<'_, '_>) -> bool {
|
||||
part.children().any(|c| c.is(b"pard"))
|
||||
}
|
||||
|
||||
impl<'f, 'a> Project<'f, 'a> {
|
||||
@@ -302,18 +303,18 @@ impl<'f, 'a> Project<'f, 'a> {
|
||||
/// name and the `LIST parT` of the copied instance, in file order (the stack order of the
|
||||
/// types, 365/366). The `EfDC` count before them is little-endian and is not needed to read
|
||||
/// them. A type whose copy has no definitions is left out.
|
||||
pub fn effect_definitions(&self) -> Vec<(String, &'f Chunk<'a>)> {
|
||||
let mut out: Vec<(String, &'f Chunk<'a>)> = Vec::new();
|
||||
pub fn effect_definitions(&self) -> Vec<(String, Node<'f, 'a>)> {
|
||||
let mut out: Vec<(String, Node<'f, 'a>)> = Vec::new();
|
||||
let Some(defs) = self.effect_defs else { return out };
|
||||
let mut seen: HashSet<String> = HashSet::new();
|
||||
for df in defs.children().iter().filter(|c| c.is_list(b"EfDf")) {
|
||||
for df in defs.children().filter(|c| c.is_list(b"EfDf")) {
|
||||
let mut name: Option<String> = None;
|
||||
for c in df.children() {
|
||||
if c.is(b"tdmn") {
|
||||
name = c.bytes().map(|b| text(until_nul(b)));
|
||||
} else if c.is_list(b"sspc")
|
||||
&& let Some(n) = name.take()
|
||||
&& let Some(part) = c.child_list(b"parT").filter(|p| has_pard(p))
|
||||
&& let Some(part) = c.child_list(b"parT").filter(|p| has_pard(*p))
|
||||
&& seen.insert(n.clone())
|
||||
{
|
||||
out.push((n, part));
|
||||
@@ -328,8 +329,8 @@ impl<'f, 'a> Project<'f, 'a> {
|
||||
/// For a type without a project-level copy it also keeps the own `parT` of the first instance
|
||||
/// in the file (items, layers and effects in file order) that has one.
|
||||
pub fn effect_definition_index(&self) -> EffectDefinitions<'f, 'a> {
|
||||
let by_name: HashMap<String, &'f Chunk<'a>> = self.effect_definitions().into_iter().collect();
|
||||
let mut instances: HashMap<String, &'f Chunk<'a>> = HashMap::new();
|
||||
let by_name: HashMap<String, Node<'f, 'a>> = self.effect_definitions().into_iter().collect();
|
||||
let mut instances: HashMap<String, Node<'f, 'a>> = HashMap::new();
|
||||
for item in self.all_items() {
|
||||
let Some(comp) = item.comp() else { continue };
|
||||
for layer in comp.layers() {
|
||||
@@ -337,7 +338,7 @@ impl<'f, 'a> Project<'f, 'a> {
|
||||
if by_name.contains_key(&fx.match_name) || instances.contains_key(&fx.match_name) {
|
||||
continue;
|
||||
}
|
||||
if let Some(part) = fx.chunk.child_list(b"parT").filter(|p| has_pard(p)) {
|
||||
if let Some(part) = fx.chunk.child_list(b"parT").filter(|p| has_pard(*p)) {
|
||||
instances.insert(fx.match_name, part);
|
||||
}
|
||||
}
|
||||
@@ -363,9 +364,9 @@ impl<'f, 'a> Project<'f, 'a> {
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct EffectDefinitions<'f, 'a> {
|
||||
/// The project-level copies (`EfdG`).
|
||||
by_name: HashMap<String, &'f Chunk<'a>>,
|
||||
by_name: HashMap<String, Node<'f, 'a>>,
|
||||
/// For a type without a project-level copy: the own `parT` of the first instance that has one.
|
||||
instances: HashMap<String, &'f Chunk<'a>>,
|
||||
instances: HashMap<String, Node<'f, 'a>>,
|
||||
}
|
||||
|
||||
impl<'f, 'a> EffectDefinitions<'f, 'a> {
|
||||
@@ -392,7 +393,7 @@ impl<'f, 'a> EffectDefinitions<'f, 'a> {
|
||||
return effect.params();
|
||||
}
|
||||
match self.by_name.get(effect.match_name()).or_else(|| self.instances.get(effect.match_name())) {
|
||||
Some(part) => effect.params_from(part, true),
|
||||
Some(part) => effect.params_from(*part, true),
|
||||
None => Vec::new(),
|
||||
}
|
||||
}
|
||||
@@ -484,26 +485,7 @@ impl<'f, 'a> EffectParam<'f, 'a> {
|
||||
pub fn pard_default(&self) -> Option<ParamValue> {
|
||||
match self.kind {
|
||||
ParamKind::Popup if self.borrowed_definition => None,
|
||||
ParamKind::FloatSlider => be_f64(self.pard, 56).map(ParamValue::Number),
|
||||
ParamKind::FixedSlider | ParamKind::Angle => be_i32(self.pard, 56).map(|v| ParamValue::Number(f64::from(v) / 65536.0)),
|
||||
ParamKind::IntSlider => be_i32(self.pard, 56).map(|v| ParamValue::Number(f64::from(v))),
|
||||
ParamKind::Checkbox => be_u32(self.pard, 56).map(|v| ParamValue::Bool(v != 0)),
|
||||
ParamKind::Popup => be_u32(self.pard, 56).map(ParamValue::Index),
|
||||
ParamKind::Point => {
|
||||
let p = [f64::from(be_i32(self.pard, 56)?) / 65536.0, f64::from(be_i32(self.pard, 60)?) / 65536.0];
|
||||
// A fresh instance stores fractions of the layer (0.5, 0.5); After Effects' save of
|
||||
// a project it opened stores percent (50, 50: the re-saves of read-back tests e1 and
|
||||
// e2, where Gradient Ramp's Start and End and every other point default came back
|
||||
// as 50 / 0 and 50 / 100). Point
|
||||
// defaults lie inside the layer, so a component beyond 1 tells percent.
|
||||
let percent = p.iter().any(|c| c.abs() > 1.0 + 1e-9);
|
||||
Some(ParamValue::Point(if percent { p.map(|c| c / 100.0) } else { p }))
|
||||
}
|
||||
ParamKind::Color => {
|
||||
let [a, r, g, b] = array::<4>(self.pard, 56)?.map(|c| f64::from(c) / 255.0);
|
||||
Some(ParamValue::Argb([a, r, g, b]))
|
||||
}
|
||||
_ => None,
|
||||
kind => Pard(self.pard).default(kind),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -557,22 +539,14 @@ impl<'f, 'a> EffectParam<'f, 'a> {
|
||||
/// slider differs per parameter (Amount 37.5 % is stored 37.5, Blend 25 % is stored 0.25), so
|
||||
/// this is what tells a reader the scale. `None` for other types.
|
||||
pub fn valid_max(&self) -> Option<f64> {
|
||||
match self.kind {
|
||||
ParamKind::FixedSlider => be_i32(self.pard, 128).map(|v| f64::from(v) / 65536.0),
|
||||
ParamKind::FloatSlider => be_f32(self.pard, 108).map(f64::from),
|
||||
ParamKind::IntSlider => be_i32(self.pard, 128).map(f64::from),
|
||||
_ => None,
|
||||
}
|
||||
Pard(self.pard).valid_max(self.kind)
|
||||
}
|
||||
|
||||
/// The smallest valid value of a float slider, the `f32` at byte 104 of `pard` (Exposure
|
||||
/// -100, Offset -2, Gamma Correction 0.1: 389). `None` for other types (a fixed slider's
|
||||
/// minimum is not decoded).
|
||||
pub fn valid_min(&self) -> Option<f64> {
|
||||
match self.kind {
|
||||
ParamKind::FloatSlider => be_f32(self.pard, 104).map(f64::from),
|
||||
_ => None,
|
||||
}
|
||||
Pard(self.pard).valid_min(self.kind)
|
||||
}
|
||||
|
||||
/// The range of a float slider's slider (what the control's drag covers, inside the valid
|
||||
@@ -581,7 +555,7 @@ impl<'f, 'a> EffectParam<'f, 'a> {
|
||||
/// for other types.
|
||||
pub fn slider_range(&self) -> Option<(f64, f64)> {
|
||||
match self.kind {
|
||||
ParamKind::FloatSlider => Some((f64::from(be_f32(self.pard, 112)?), f64::from(be_f32(self.pard, 116)?))),
|
||||
ParamKind::FloatSlider => Pard(self.pard).slider_range(self.kind),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -591,7 +565,7 @@ impl<'f, 'a> EffectParam<'f, 'a> {
|
||||
if self.kind != ParamKind::Popup {
|
||||
return None;
|
||||
}
|
||||
be_u16(self.pard, 60)
|
||||
Pard(self.pard).popup_item_count()
|
||||
}
|
||||
|
||||
/// For a popup: the selected item index (1-based): the stored selection, else the default.
|
||||
@@ -619,6 +593,93 @@ impl<'f, 'a> EffectParam<'f, 'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A parameter definition (`pard`, 148 bytes; aep-format.md 3.7): the one decoder of its fields.
|
||||
/// The import reads them through [`EffectParam`]; the export writes them from its effect table and
|
||||
/// its tests read them back through this.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Pard<'f>(pub &'f [u8]);
|
||||
|
||||
impl Pard<'_> {
|
||||
/// The type code, the `u32` at byte 12.
|
||||
pub fn code(self) -> Option<u32> {
|
||||
be_u32(self.0, 12)
|
||||
}
|
||||
|
||||
/// The parameter type (an unreadable code is [`ParamKind::Other`]).
|
||||
pub fn kind(self) -> ParamKind {
|
||||
ParamKind::from_u32(self.code().unwrap_or(u32::MAX))
|
||||
}
|
||||
|
||||
/// The parameter name at byte 16 (32 bytes).
|
||||
pub fn name(self) -> Option<String> {
|
||||
cstr(self.0, 16, 32)
|
||||
}
|
||||
|
||||
/// The default at byte 56 as a parameter of type `kind` reads it (see
|
||||
/// [`EffectParam::pard_default`]).
|
||||
pub fn default(self, kind: ParamKind) -> Option<ParamValue> {
|
||||
let p = self.0;
|
||||
match kind {
|
||||
ParamKind::FloatSlider => be_f64(p, 56).map(ParamValue::Number),
|
||||
ParamKind::FixedSlider | ParamKind::Angle => be_i32(p, 56).map(|v| ParamValue::Number(f64::from(v) / 65536.0)),
|
||||
ParamKind::IntSlider => be_i32(p, 56).map(|v| ParamValue::Number(f64::from(v))),
|
||||
ParamKind::Checkbox => be_u32(p, 56).map(|v| ParamValue::Bool(v != 0)),
|
||||
ParamKind::Popup => be_u32(p, 56).map(ParamValue::Index),
|
||||
ParamKind::Point => {
|
||||
let pt = [f64::from(be_i32(p, 56)?) / 65536.0, f64::from(be_i32(p, 60)?) / 65536.0];
|
||||
// A fresh instance stores fractions of the layer (0.5, 0.5); After Effects' save of
|
||||
// a project it opened stores percent (50, 50: the re-saves of read-back tests e1 and
|
||||
// e2, where Gradient Ramp's Start and End and every other point default came back
|
||||
// as 50 / 0 and 50 / 100). Point
|
||||
// defaults lie inside the layer, so a component beyond 1 tells percent.
|
||||
let percent = pt.iter().any(|c| c.abs() > 1.0 + 1e-9);
|
||||
Some(ParamValue::Point(if percent { pt.map(|c| c / 100.0) } else { pt }))
|
||||
}
|
||||
ParamKind::Color => {
|
||||
let [a, r, g, b] = array::<4>(p, 56)?.map(|c| f64::from(c) / 255.0);
|
||||
Some(ParamValue::Argb([a, r, g, b]))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The valid maximum of a slider (see [`EffectParam::valid_max`]).
|
||||
pub fn valid_max(self, kind: ParamKind) -> Option<f64> {
|
||||
match kind {
|
||||
ParamKind::FixedSlider => be_i32(self.0, 128).map(|v| f64::from(v) / 65536.0),
|
||||
ParamKind::FloatSlider => be_f32(self.0, 108).map(f64::from),
|
||||
ParamKind::IntSlider => be_i32(self.0, 128).map(f64::from),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The valid minimum of a float slider (see [`EffectParam::valid_min`]).
|
||||
pub fn valid_min(self, kind: ParamKind) -> Option<f64> {
|
||||
match kind {
|
||||
ParamKind::FloatSlider => be_f32(self.0, 104).map(f64::from),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A slider's range, the one its value records repeat as `tdum` / `tduM`: a float slider's
|
||||
/// `f32` pair at 112 / 116, a fixed slider's 16.16 pair and an integer slider's `i32` pair at
|
||||
/// 132 / 136.
|
||||
pub fn slider_range(self, kind: ParamKind) -> Option<(f64, f64)> {
|
||||
let p = self.0;
|
||||
match kind {
|
||||
ParamKind::FloatSlider => Some((f64::from(be_f32(p, 112)?), f64::from(be_f32(p, 116)?))),
|
||||
ParamKind::FixedSlider => Some((f64::from(be_i32(p, 132)?) / 65536.0, f64::from(be_i32(p, 136)?) / 65536.0)),
|
||||
ParamKind::IntSlider => Some((f64::from(be_i32(p, 132)?), f64::from(be_i32(p, 136)?))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A popup's item count, the `u16` at byte 60.
|
||||
pub fn popup_item_count(self) -> Option<u16> {
|
||||
be_u16(self.0, 60)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'f, 'a> Layer<'f, 'a> {
|
||||
/// The effects on the layer, in stack order (`ADBE Effect Parade`).
|
||||
pub fn effects(&self) -> Vec<Effect<'f, 'a>> {
|
||||
@@ -638,6 +699,30 @@ impl<'f, 'a> Layer<'f, 'a> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Chunk;
|
||||
|
||||
#[test]
|
||||
fn a_parameter_definition_reads_its_fields_and_nothing_from_a_cut_record() {
|
||||
let mut b = vec![0u8; 148];
|
||||
b[12..16].copy_from_slice(&7u32.to_be_bytes());
|
||||
b[16..22].copy_from_slice(b"Amount");
|
||||
b[56..64].copy_from_slice(&2.5f64.to_be_bytes());
|
||||
let p = Pard(&b);
|
||||
assert_eq!((p.code(), p.name().as_deref()), (Some(7), Some("Amount")));
|
||||
assert_eq!(p.default(ParamKind::FloatSlider), Some(ParamValue::Number(2.5)));
|
||||
// The same bytes as a checkbox and as a popup: the `u32` at 56, and the `u16` at 60.
|
||||
b[56..60].copy_from_slice(&1u32.to_be_bytes());
|
||||
b[60..62].copy_from_slice(&3u16.to_be_bytes());
|
||||
let p = Pard(&b);
|
||||
assert_eq!(p.default(ParamKind::Checkbox), Some(ParamValue::Bool(true)));
|
||||
assert_eq!((p.default(ParamKind::Popup), p.popup_item_count()), (Some(ParamValue::Index(1)), Some(3)));
|
||||
assert_eq!(p.default(ParamKind::IntSlider), Some(ParamValue::Number(1.0)));
|
||||
// A record cut before a field has no value for it, and an unreadable code is `Other`.
|
||||
let cut = Pard(&b[..12]);
|
||||
assert_eq!((cut.code(), cut.kind(), cut.name(), cut.popup_item_count()), (None, ParamKind::Other(u32::MAX), None, None));
|
||||
assert_eq!(cut.default(ParamKind::FloatSlider), None);
|
||||
assert_eq!(Pard(&b[..58]).default(ParamKind::Checkbox), None);
|
||||
}
|
||||
|
||||
fn tdmn(name: &str) -> Chunk<'static> {
|
||||
let mut b = name.as_bytes().to_vec();
|
||||
@@ -670,11 +755,11 @@ mod tests {
|
||||
("A".to_string(), Chunk::list(*b"tdbs", vec![Chunk::data(*b"cdat", vec![0u8; 8])])),
|
||||
];
|
||||
let chunk = instance(&names, &stored);
|
||||
let fx = Effect { match_name: "X".into(), chunk: &chunk };
|
||||
let fx = Effect { match_name: "X".into(), chunk: Node::from(&chunk) };
|
||||
let params = fx.params();
|
||||
assert_eq!(params.iter().map(|p| p.match_name.as_str()).collect::<Vec<_>>(), ["A", "B", "C"]);
|
||||
let first = params.first().and_then(|p| p.stored).expect("A stored");
|
||||
assert!(first.chunk.children().is_empty(), "the first A (the second has a cdat)");
|
||||
assert!(first.chunk.children().next().is_none(), "the first A (the second has a cdat)");
|
||||
assert!(params.get(1).is_some_and(|p| p.stored.is_none()), "a group of that name is no stored value");
|
||||
assert!(params.get(2).is_some_and(|p| p.stored.is_none()));
|
||||
}
|
||||
@@ -687,7 +772,7 @@ mod tests {
|
||||
let names: Vec<String> = (0..n).map(|i| format!("P{i}")).collect();
|
||||
let stored: Vec<(String, Chunk<'static>)> = names.iter().map(|m| (m.clone(), tdbs())).collect();
|
||||
let chunk = instance(&names, &stored);
|
||||
let fx = Effect { match_name: "X".into(), chunk: &chunk };
|
||||
let fx = Effect { match_name: "X".into(), chunk: Node::from(&chunk) };
|
||||
let params = fx.params();
|
||||
assert_eq!(params.len(), n);
|
||||
assert!(params.iter().all(|p| p.stored.is_some()));
|
||||
@@ -707,19 +792,19 @@ mod tests {
|
||||
}
|
||||
let efdg = Chunk::list(*b"EfdG", defs);
|
||||
let fold = Chunk::list(*b"Fold", Vec::new());
|
||||
let project = Project { fold: &fold, effect_defs: Some(&efdg) };
|
||||
let project = Project { fold: Node::from(&fold), effect_defs: Some(Node::from(&efdg)) };
|
||||
let all = project.effect_definitions();
|
||||
assert_eq!(all.len(), n);
|
||||
let index = project.effect_definition_index();
|
||||
let bare = Chunk::list(*b"sspc", vec![Chunk::list(*b"parT", Vec::new())]);
|
||||
for i in (0..n).step_by(50) {
|
||||
let fx = Effect { match_name: format!("FX{i}"), chunk: &bare };
|
||||
let fx = Effect { match_name: format!("FX{i}"), chunk: Node::from(&bare) };
|
||||
assert_eq!(index.source(&fx), DefinitionSource::Project);
|
||||
let params = index.params(&fx);
|
||||
assert_eq!(params.iter().map(|p| p.match_name.as_str()).collect::<Vec<_>>(), ["R0"], "the first copy's definitions");
|
||||
assert!(params.iter().all(|p| p.borrowed_definition));
|
||||
}
|
||||
let unknown = Effect { match_name: "nope".into(), chunk: &bare };
|
||||
let unknown = Effect { match_name: "nope".into(), chunk: Node::from(&bare) };
|
||||
assert_eq!(index.source(&unknown), DefinitionSource::Missing);
|
||||
assert!(index.params(&unknown).is_empty());
|
||||
assert_eq!(project.definition_source(&unknown), DefinitionSource::Missing);
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
//! of a layer's source.
|
||||
|
||||
use crate::bytes::{be_f32, be_f64, be_i32, be_u32, cstr, le_u32, text, until_nul};
|
||||
use crate::chunk::Chunk;
|
||||
use crate::header::Rational;
|
||||
use crate::index::Node;
|
||||
use crate::typed::{Item, ItemKind, Layer, Project};
|
||||
|
||||
/// The footage data of an item (`LIST Pin ` with `sspc`, `opti` and, for files, `Als2/alas`).
|
||||
@@ -101,21 +101,6 @@ impl PsdLayer {
|
||||
}
|
||||
|
||||
impl Footage {
|
||||
/// The number of frames of an image sequence (`duration * native_frame_rate`: 3/30 s at 30 fps is
|
||||
/// 3); `None` for anything else.
|
||||
pub fn sequence_frame_count(&self) -> Option<u32> {
|
||||
if !self.is_sequence {
|
||||
return None;
|
||||
}
|
||||
let d = self.duration?;
|
||||
let frames = i128::from(d.num).checked_mul(i128::from(self.native_frame_rate))?;
|
||||
let den = i128::from(d.den);
|
||||
if den <= 0 || frames % den != 0 {
|
||||
return None;
|
||||
}
|
||||
u32::try_from(frames / den).ok()
|
||||
}
|
||||
|
||||
/// The solid's colour as fractions R, G, B.
|
||||
pub fn solid_rgb(&self) -> Option<[f32; 3]> {
|
||||
let [_, r, g, b] = self.solid_argb?;
|
||||
@@ -269,14 +254,14 @@ impl SequenceInfo {
|
||||
/// is unreadable (empty). A name that is not a plain file name (holding `/`, `\`, `:` or a control
|
||||
/// character such as NUL, or being `.` or `..`) makes the whole list unreadable (empty): the names
|
||||
/// are joined to the folder.
|
||||
fn sequence_file_list(list: &Chunk<'_>) -> Vec<String> {
|
||||
fn sequence_file_list(list: Node<'_, '_>) -> Vec<String> {
|
||||
// After Effects writes the count first (the one sample); a list without a readable count is
|
||||
// not read rather than guessed.
|
||||
let Some(count) = list.child(b"StVS").and_then(|c| c.bytes()).and_then(|b| le_u32(b, 0)).and_then(|n| usize::try_from(n).ok()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let names: Vec<String> =
|
||||
list.children().iter().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).take(count).map(text).filter(|n| !n.is_empty()).collect();
|
||||
list.children().filter(|c| c.is(b"Utf8")).filter_map(Node::bytes).take(count).map(text).filter(|n| !n.is_empty()).collect();
|
||||
if names.iter().any(|n| n.contains(|c: char| matches!(c, '/' | '\\' | ':') || c.is_control()) || n == "." || n == "..") {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -308,9 +293,8 @@ impl<'f, 'a> Item<'f, 'a> {
|
||||
}
|
||||
let pin = self.chunk.child_list(b"Pin ")?;
|
||||
let sspc = pin.child(b"sspc")?.bytes()?;
|
||||
let kids = pin.children();
|
||||
let after = kids.iter().position(|c| c.is_list(b"Als2"))?;
|
||||
let naming = || kids.iter().skip(after.saturating_add(1)).take_while(|c| !c.is(b"opti"));
|
||||
let after = pin.children().position(|c| c.is_list(b"Als2"))?;
|
||||
let naming = || pin.children().skip(after.saturating_add(1)).take_while(|c| !c.is(b"opti"));
|
||||
let (start, end, digits) = (be_u32(sspc, 172)?, be_u32(sspc, 176)?, be_u32(sspc, 180)?);
|
||||
// Force alphabetical order: the file list replaces the numbering.
|
||||
if let Some(list) = naming().find(|c| c.is_list(b"StVc")) {
|
||||
@@ -320,7 +304,7 @@ impl<'f, 'a> Item<'f, 'a> {
|
||||
}
|
||||
return Some(SequenceInfo { prefix: String::new(), suffix: String::new(), start, end, digits, files });
|
||||
}
|
||||
let names: Vec<String> = naming().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).map(text).take(2).collect();
|
||||
let names: Vec<String> = naming().filter(|c| c.is(b"Utf8")).filter_map(Node::bytes).map(text).take(2).collect();
|
||||
let (prefix, suffix) = match names.as_slice() {
|
||||
[prefix, suffix] => (prefix.clone(), suffix.clone()),
|
||||
// An empty prefix is not stored (`0001.png` has only `.png`).
|
||||
@@ -350,12 +334,6 @@ impl<'f, 'a> Item<'f, 'a> {
|
||||
self.chunk.child(b"idta")?.bytes()?.get(58).copied()
|
||||
}
|
||||
|
||||
/// The ids of the compositions this item uses as precomp layers: the little-endian `u32` of
|
||||
/// each `idpi` chunk.
|
||||
pub fn used_item_ids(&self) -> Vec<u32> {
|
||||
self.chunk.children().iter().filter(|c| c.is(b"idpi")).filter_map(|c| le_u32(c.bytes()?, 0)).collect()
|
||||
}
|
||||
|
||||
/// The footage data of a footage item.
|
||||
pub fn footage(&self) -> Option<Footage> {
|
||||
if self.kind()? != ItemKind::Footage {
|
||||
@@ -405,11 +383,6 @@ impl<'f, 'a> Project<'f, 'a> {
|
||||
self.item_by_id(layer.header()?.source_id?)
|
||||
}
|
||||
|
||||
/// The name a layer shows: its own name, or its source's name when it has none.
|
||||
pub fn layer_display_name(&self, layer: &Layer<'f, 'a>) -> Option<String> {
|
||||
layer.name().or_else(|| self.layer_source(layer)?.display_name())
|
||||
}
|
||||
|
||||
/// The pixel size of a layer's source: a footage item's width and height, or a precomp's
|
||||
/// composition size.
|
||||
pub fn layer_source_size(&self, layer: &Layer<'f, 'a>) -> Option<(u32, u32)> {
|
||||
|
||||
@@ -237,12 +237,6 @@ impl LayerHeader {
|
||||
self.flag_bytes.get(byte).is_some_and(|b| b & mask != 0)
|
||||
}
|
||||
|
||||
/// Byte 37 bit `0x01`: the layer has a name set by the user (clear on a default-named camera or
|
||||
/// light and on a precomp layer, whose name is the source's).
|
||||
pub fn name_is_user_set(&self) -> bool {
|
||||
self.flag(0, 0x01)
|
||||
}
|
||||
|
||||
/// Byte 37 bit `0x02`: guide layer.
|
||||
pub fn is_guide(&self) -> bool {
|
||||
self.flag(0, 0x02)
|
||||
@@ -333,17 +327,6 @@ impl LayerHeader {
|
||||
Rational::new(i64::from(self.stretch), i64::from(self.stretch_den))
|
||||
}
|
||||
|
||||
/// The time stretch in percent (`stretch / stretch_den * 100`); a zero denominator reads as 1.
|
||||
pub fn stretch_percent(&self) -> f64 {
|
||||
let den = if self.stretch_den == 0 { 1.0 } else { f64::from(self.stretch_den) };
|
||||
f64::from(self.stretch) / den * 100.0
|
||||
}
|
||||
|
||||
/// The start time in seconds, `None` for a zero denominator.
|
||||
pub fn start_seconds(&self) -> Option<f64> {
|
||||
self.start.to_f64()
|
||||
}
|
||||
|
||||
/// The absolute in point, `start + in * stretch`, exact and reduced.
|
||||
pub fn in_point(&self) -> Option<Rational> {
|
||||
self.start.plus_scaled(&self.in_relative, &self.stretch_ratio()?)
|
||||
@@ -355,16 +338,6 @@ impl LayerHeader {
|
||||
pub fn out_point(&self) -> Option<Rational> {
|
||||
self.start.plus_scaled(&self.out_relative, &self.stretch_ratio()?)
|
||||
}
|
||||
|
||||
/// The absolute in point in seconds.
|
||||
pub fn in_point_seconds(&self) -> Option<f64> {
|
||||
self.in_point()?.to_f64()
|
||||
}
|
||||
|
||||
/// The absolute out point in seconds.
|
||||
pub fn out_point_seconds(&self) -> Option<f64> {
|
||||
self.out_point()?.to_f64()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'f, 'a> Layer<'f, 'a> {
|
||||
@@ -464,64 +437,10 @@ impl CompHeader {
|
||||
self.time_base.frame_rate()
|
||||
}
|
||||
|
||||
/// The frame rate from the 16.16 fixed-point field (25 is `0x0019_0000`).
|
||||
pub fn frame_rate_from_fixed(&self) -> f64 {
|
||||
f64::from(self.frame_rate_fixed) / 65536.0
|
||||
}
|
||||
|
||||
/// The duration in seconds.
|
||||
pub fn duration_seconds(&self) -> Option<f64> {
|
||||
self.time_base.seconds(i32::try_from(self.duration_units).ok()?)
|
||||
}
|
||||
|
||||
/// The work area start in seconds.
|
||||
pub fn work_area_start_seconds(&self) -> Option<f64> {
|
||||
self.time_base.seconds(i32::try_from(self.work_area_start_units).ok()?)
|
||||
}
|
||||
|
||||
/// The work area end in seconds; the composition duration when the work area is the whole
|
||||
/// composition.
|
||||
pub fn work_area_end_seconds(&self) -> Option<f64> {
|
||||
match self.work_area_end_units {
|
||||
Some(end) => self.time_base.seconds(i32::try_from(end).ok()?),
|
||||
None => self.duration_seconds(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The background colour as fractions 0..1 (`byte / 255`).
|
||||
pub fn background_rgb(&self) -> [f64; 3] {
|
||||
self.background.map(|c| f64::from(c) / 255.0)
|
||||
}
|
||||
|
||||
/// The pixel aspect ratio as a number.
|
||||
pub fn pixel_aspect_ratio(&self) -> Option<f64> {
|
||||
self.pixel_aspect.to_f64()
|
||||
}
|
||||
|
||||
/// The display start time in seconds (the start timecode), `None` when absent or the
|
||||
/// denominator is zero.
|
||||
pub fn display_start_seconds(&self) -> Option<f64> {
|
||||
self.display_start?.to_f64()
|
||||
}
|
||||
|
||||
/// The display start as a frame number (`display start * frame rate`, rounded); 100 at 25 fps
|
||||
/// for a display start of 4 s.
|
||||
pub fn display_start_frame(&self) -> Option<i64> {
|
||||
let rate = self.frame_rate()?;
|
||||
Some((self.display_start_seconds()? * rate).round() as i64)
|
||||
}
|
||||
|
||||
/// The frame rate as an exact fraction `timescale / frame_duration`, as typed: 29.97 fps is
|
||||
/// `23976 / 800` (not `30000 / 1001`), 23.976 fps is `23976 / 1000`.
|
||||
pub fn frame_rate_exact(&self) -> Option<Rational> {
|
||||
Rational::new(i64::from(self.time_base.timescale), i64::from(self.time_base.frame_duration))
|
||||
}
|
||||
|
||||
/// The duration in whole frames (`duration / frame_duration`). After Effects snaps a duration
|
||||
/// to a whole number of frames: 5 s at 29.97 fps becomes 150 frames (5.005 s).
|
||||
pub fn duration_frames(&self) -> Option<u32> {
|
||||
self.duration_units.checked_div(self.time_base.frame_duration)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'f, 'a> Comp<'f, 'a> {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+1
-40
@@ -193,19 +193,10 @@ impl<'f> Keyframe<'f> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The value of a 3-component spatial keyframe (Position), x, y and z in pixels.
|
||||
pub fn spatial_value(&self) -> Option<[f64; 3]> {
|
||||
let v = self.value()?;
|
||||
if !matches!(self.kind(), KeyframeKind::Spatial { components: 3 }) {
|
||||
return None;
|
||||
}
|
||||
Some([*v.first()?, *v.get(1)?, *v.get(2)?])
|
||||
}
|
||||
|
||||
/// The ease of the key: the four slots at byte 16 of a one-component plain record or at byte 24
|
||||
/// of a spatial record or a mask path record. For linear and hold keys all four are 0 (a bezier key stores what
|
||||
/// `setTemporalEase` was given). Plain records with several components keep four slots per
|
||||
/// component, see [`Keyframe::ease_component`] and [`Keyframe::plain_ease_slots`].
|
||||
/// component, see [`Keyframe::ease_component`].
|
||||
pub fn ease(&self) -> Option<KeyEase> {
|
||||
match self.kind() {
|
||||
KeyframeKind::Plain { components: 1 } => ease_at(self.record, 16),
|
||||
@@ -214,16 +205,6 @@ impl<'f> Keyframe<'f> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The `4 n` ease `f64` of a plain record as stored (from byte `8 + 8 n`). With one component
|
||||
/// they are [`Keyframe::ease`]; with several they are type-major, see
|
||||
/// [`Keyframe::ease_component`].
|
||||
pub fn plain_ease_slots(&self) -> Option<Vec<f64>> {
|
||||
match self.kind() {
|
||||
KeyframeKind::Plain { components } => doubles(self.record, components.checked_mul(8)?.checked_add(8)?, components.checked_mul(4)?),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The ease of one value component. A plain record with `n` components stores the `4 n` ease
|
||||
/// doubles **type-major**: `[in speed x n][in influence x n][out speed x n][out influence x n]`
|
||||
/// starting at byte `8 + 8 n`, and component `i` uses slot `i` of each block (verified for the
|
||||
@@ -260,16 +241,6 @@ impl<'f> Keyframe<'f> {
|
||||
(0..n).map(|i| self.ease_component(i)).collect()
|
||||
}
|
||||
|
||||
/// The Euclidean ARGB distance to the next key that After Effects writes at byte 16 of a colour
|
||||
/// (4-component spatial) record whose interpolation is not linear: 255 sqrt(2) = 360.6245 for
|
||||
/// red to blue; 0 on the last key and on linear keys. `None` for other records.
|
||||
pub fn color_distance(&self) -> Option<f64> {
|
||||
match self.kind() {
|
||||
KeyframeKind::Spatial { components: 4 } => be_f64(self.record, 16),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The decoded spatial record (Position: 128 bytes, colour: 152 bytes).
|
||||
pub fn spatial(&self) -> Option<SpatialKey> {
|
||||
let KeyframeKind::Spatial { components: n } = self.kind() else { return None };
|
||||
@@ -300,11 +271,6 @@ pub struct ExpressionFlags {
|
||||
}
|
||||
|
||||
impl ExpressionFlags {
|
||||
/// True when bytes 76, 77 and 120 are all 1.
|
||||
pub fn all_set(&self) -> bool {
|
||||
self.byte_76 == 1 && self.byte_77 == 1 && self.byte_120 == 1
|
||||
}
|
||||
|
||||
/// True when byte 119 is 1.
|
||||
pub fn disabled(&self) -> bool {
|
||||
self.byte_119 == 1
|
||||
@@ -324,11 +290,6 @@ impl<'f, 'a> Property<'f, 'a> {
|
||||
be_u32(self.chunk.child(b"tdsb")?.bytes()?, 0)
|
||||
}
|
||||
|
||||
/// True when `tdb4` byte 5 has bit 0 set: the property is static, it has no keyframes.
|
||||
pub fn is_static_flag_set(&self) -> Option<bool> {
|
||||
Some(self.header()?.get(5)? & 1 != 0)
|
||||
}
|
||||
|
||||
/// The expression source: the raw UTF-8 text of the `Utf8` child that follows the value in the
|
||||
/// property (no length prefix, no engine tag; the engine is project-wide).
|
||||
pub fn expression(&self) -> Option<String> {
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
//! list) on damaged data instead of failing.
|
||||
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
// Ranges and borrowed bytes are passed around as plain tuples, as in EffectCraft, which allows this lint.
|
||||
#![allow(clippy::type_complexity)]
|
||||
|
||||
mod aepx;
|
||||
mod arbitrary;
|
||||
@@ -24,6 +26,7 @@ mod effects;
|
||||
mod error;
|
||||
mod footage;
|
||||
mod header;
|
||||
pub mod index;
|
||||
mod keys;
|
||||
mod masks;
|
||||
mod matte;
|
||||
@@ -36,16 +39,17 @@ mod values;
|
||||
mod version;
|
||||
mod write;
|
||||
|
||||
pub use aepx::{is_aepx, parse_any, read_aepx, read_aepx_with_limits};
|
||||
pub use aepx::{AepxDrafts, index_aepx, is_aepx, parse_any, read_aepx, read_aepx_with_limits};
|
||||
pub use arbitrary::{
|
||||
CURVE_IDENTITY, CryptomatteData, CurvesData, ExtractorChannels, HueSaturationData, LevelsData, MeshData, curve_lut, identifier_channel_type,
|
||||
};
|
||||
pub use camera::{CameraPosition, DEFAULT_CAMERA_ZOOM};
|
||||
pub use chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS, Walk};
|
||||
pub use effects::{DefinitionSource, Effect, EffectDefinitions, EffectParam, LayerRef, ParamKind, ParamValue};
|
||||
pub use effects::{DefinitionSource, Effect, EffectDefinitions, EffectParam, LayerRef, ParamKind, ParamValue, Pard};
|
||||
pub use error::AepError;
|
||||
pub use footage::{Footage, PsdLayer, SequenceInfo};
|
||||
pub use header::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Quality, Rational, Renderer};
|
||||
pub use index::{Draft, Node, NodeWalk, Nodes, Root, TreeIndex};
|
||||
pub use keys::{ExpressionFlags, Interpolation, KeyEase, KeyframeKind, SpatialKey};
|
||||
pub use masks::{Mask, MaskAtom, MaskMode, PathVertex, ShapePath};
|
||||
pub use matte::{MatteMode, TrackMatte};
|
||||
|
||||
+8
-16
@@ -6,9 +6,9 @@
|
||||
//! property (`tdbs`, with a key list when animated) and `omks` with one `LIST shap` per keyframe (a
|
||||
//! single one when the path is static).
|
||||
|
||||
use crate::Chunk;
|
||||
use crate::bytes::{be_f32, be_u32, until_nul};
|
||||
use crate::typed::{KeyList, Layer, Project, PropGroup, PropNode, Property};
|
||||
use crate::index::Node;
|
||||
use crate::typed::{KeyList, Layer, PropGroup, PropNode, Property};
|
||||
use crate::values::{Tracked, read};
|
||||
|
||||
/// The mask mode (`mkif` `u32` at byte 4).
|
||||
@@ -93,8 +93,10 @@ pub struct ShapePath {
|
||||
}
|
||||
|
||||
impl ShapePath {
|
||||
/// Decodes a `LIST shap`; `None` when `shph` or the point list is malformed.
|
||||
pub fn from_chunk(shap: &Chunk<'_>) -> Option<Self> {
|
||||
/// Decodes a `LIST shap` (a [`Chunk`](crate::Chunk) or a [`Node`]); `None` when
|
||||
/// `shph` or the point list is malformed.
|
||||
pub fn from_chunk<'f, 'a: 'f>(shap: impl Into<Node<'f, 'a>>) -> Option<Self> {
|
||||
let shap = shap.into();
|
||||
let shph = shap.child(b"shph")?.bytes()?;
|
||||
let bbox = [be_f32(shph, 4)?, be_f32(shph, 8)?, be_f32(shph, 12)?, be_f32(shph, 16)?];
|
||||
let closed_flag = *shph.get(20)?;
|
||||
@@ -187,22 +189,12 @@ fn mask_from_atom(atom: &PropGroup<'_, '_>, mkif: Option<&[u8]>) -> Mask {
|
||||
}
|
||||
}
|
||||
if let Some(omks) = oms.child_list(b"omks") {
|
||||
mask.shapes = omks.children().iter().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect();
|
||||
mask.shapes = omks.children().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect();
|
||||
}
|
||||
}
|
||||
mask
|
||||
}
|
||||
|
||||
impl<'f, 'a> Project<'f, 'a> {
|
||||
/// The vertices of every path shape of a mask in pixels of the layer: mask boxes are normalised
|
||||
/// to the **layer** size (the size of its source), not the composition's. `None` when the layer
|
||||
/// has no source with a size.
|
||||
pub fn mask_vertices_px(&self, layer: &Layer<'f, 'a>, mask: &Mask) -> Option<Vec<Vec<PathVertex>>> {
|
||||
let (width, height) = self.layer_source_size(layer)?;
|
||||
Some(mask.shapes.iter().map(|s| s.vertices((f64::from(width), f64::from(height)))).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// One mask with the views of its records, for readers that need more than [`Mask`]: the
|
||||
/// keyframes and expressions of the mask properties (in [`MaskAtom::atom`]: `ADBE Mask Feather`,
|
||||
/// `ADBE Mask Opacity`, `ADBE Mask Offset`) and the path keyframes paired with their shapes.
|
||||
@@ -246,7 +238,7 @@ impl<'f, 'a> Layer<'f, 'a> {
|
||||
if let Some(PropNode::Other(oms)) = atom.get("ADBE Mask Shape") {
|
||||
path = oms.child_list(b"tdbs").and_then(Property::from_chunk);
|
||||
if let Some(omks) = oms.child_list(b"omks") {
|
||||
path_shapes = omks.children().iter().filter(|c| c.is_list(b"shap")).map(ShapePath::from_chunk).collect();
|
||||
path_shapes = omks.children().filter(|c| c.is_list(b"shap")).map(ShapePath::from_chunk).collect();
|
||||
}
|
||||
}
|
||||
masks.push(MaskAtom { mask, atom, path, path_shapes });
|
||||
|
||||
+150
-14
@@ -2,6 +2,7 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::bytes::{array, be_u32};
|
||||
use crate::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS};
|
||||
use crate::error::AepError;
|
||||
|
||||
@@ -36,10 +37,20 @@ pub fn parse(data: &[u8]) -> Result<AepFile<'_>, AepError> {
|
||||
/// # Errors
|
||||
/// See [`parse`]; also fails with [`AepError::TooManyChunks`].
|
||||
pub fn parse_with_limits(data: &[u8], limits: Limits) -> Result<AepFile<'_>, AepError> {
|
||||
let end = container_end(data)?;
|
||||
let mut parser = Parser { data, limits, count: 0 };
|
||||
let chunks = parser.sequence(12, end, 1)?;
|
||||
let trailer = data.get(end..).unwrap_or(&[]);
|
||||
Ok(AepFile { chunks, trailer: Cow::Borrowed(trailer) })
|
||||
}
|
||||
|
||||
/// Checks the `RIFX`/`Egg!` header and returns where the container ends (its chunks fill
|
||||
/// `12..end`; the trailer follows). Shared with [`crate::index::TreeIndex::parse`].
|
||||
pub(super) fn container_end(data: &[u8]) -> Result<usize, AepError> {
|
||||
if id4(data, 0) != Some(*b"RIFX") {
|
||||
return Err(AepError::NotRifx);
|
||||
}
|
||||
let declared = u64::from(be32(data, 4).ok_or(AepError::NotRifx)?);
|
||||
let declared = u64::from(be_u32(data, 4).ok_or(AepError::NotRifx)?);
|
||||
let form = id4(data, 8).ok_or(AepError::NotRifx)?;
|
||||
if &form != b"Egg!" {
|
||||
return Err(AepError::BadFormType(form));
|
||||
@@ -49,20 +60,12 @@ pub fn parse_with_limits(data: &[u8], limits: Limits) -> Result<AepFile<'_>, Aep
|
||||
return Err(AepError::Truncated { declared, available });
|
||||
}
|
||||
// `declared <= available < data.len()`, so the end is inside the input.
|
||||
let end = usize::try_from(declared).map_err(|_| AepError::TooLarge)?.checked_add(8).ok_or(AepError::TooLarge)?;
|
||||
let mut parser = Parser { data, limits, count: 0 };
|
||||
let chunks = parser.sequence(12, end, 1)?;
|
||||
let trailer = data.get(end..).unwrap_or(&[]);
|
||||
Ok(AepFile { chunks, trailer: Cow::Borrowed(trailer) })
|
||||
usize::try_from(declared).map_err(|_| AepError::TooLarge)?.checked_add(8).ok_or(AepError::TooLarge)
|
||||
}
|
||||
|
||||
fn be32(data: &[u8], at: usize) -> Option<u32> {
|
||||
id4(data, at).map(u32::from_be_bytes)
|
||||
}
|
||||
|
||||
fn id4(data: &[u8], at: usize) -> Option<[u8; 4]> {
|
||||
let b = data.get(at..at.checked_add(4)?)?;
|
||||
Some([*b.first()?, *b.get(1)?, *b.get(2)?, *b.get(3)?])
|
||||
/// The 4-byte id at `at`, `None` when the input is too short.
|
||||
pub(super) fn id4(data: &[u8], at: usize) -> Option<[u8; 4]> {
|
||||
array::<4>(data, at)
|
||||
}
|
||||
|
||||
struct Parser<'a> {
|
||||
@@ -74,13 +77,16 @@ struct Parser<'a> {
|
||||
impl<'a> Parser<'a> {
|
||||
/// Parses the sibling chunks that exactly fill `start..end`.
|
||||
fn sequence(&mut self, start: usize, end: usize, depth: usize) -> Result<Vec<Chunk<'a>>, AepError> {
|
||||
// Sized once: a well-formed sequence fills exactly this many slots. `try_reserve` so a
|
||||
// limit raised past what the target can allocate fails nothing here (the Vec then grows).
|
||||
let mut out = Vec::new();
|
||||
let _ = out.try_reserve_exact(self.capacity_for(start, end));
|
||||
let mut pos = start;
|
||||
while pos < end {
|
||||
let short = AepError::Malformed { offset: pos, what: "bytes left over that are too short for a chunk header" };
|
||||
let body_start = pos.checked_add(8).filter(|b| *b <= end).ok_or(short)?;
|
||||
let id = id4(self.data, pos).ok_or(AepError::Malformed { offset: pos, what: "short read" })?;
|
||||
let size = be32(self.data, pos + 4).ok_or(AepError::Malformed { offset: pos, what: "short read" })? as usize;
|
||||
let size = be_u32(self.data, pos + 4).ok_or(AepError::Malformed { offset: pos, what: "short read" })? as usize;
|
||||
let avail = end - body_start;
|
||||
if size > avail {
|
||||
return Err(AepError::ChunkTooLarge { offset: pos, size: size as u64, available: avail as u64 });
|
||||
@@ -103,6 +109,26 @@ impl<'a> Parser<'a> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The slots to reserve for the sequence `start..end`: its header count, bounded by the input
|
||||
/// (one per 8 bytes at most) and by the chunks the limit still allows.
|
||||
fn capacity_for(&self, start: usize, end: usize) -> usize {
|
||||
self.count_siblings(start, end).min(self.limits.max_chunks.saturating_sub(self.count))
|
||||
}
|
||||
|
||||
/// The number of chunk headers that tile `start..end` the way [`Self::sequence`] walks them,
|
||||
/// stopping at the first that does not fit. Never more than `(end - start) / 8`.
|
||||
fn count_siblings(&self, start: usize, end: usize) -> usize {
|
||||
let mut n = 0usize;
|
||||
let mut pos = start;
|
||||
while let Some(body_start) = pos.checked_add(8).filter(|b| *b <= end) {
|
||||
let Some(size) = be_u32(self.data, pos.saturating_add(4)) else { break };
|
||||
let Some(next) = usize::try_from(size).ok().and_then(|s| body_start.checked_add(s)).filter(|e| *e <= end) else { break };
|
||||
n = n.saturating_add(1);
|
||||
pos = if size % 2 == 1 && next < end { next.saturating_add(1) } else { next };
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
/// Like [`Self::sequence`] but a structural problem yields `None` (the caller keeps bytes).
|
||||
fn tentative(&mut self, start: usize, end: usize, depth: usize) -> Result<Option<Vec<Chunk<'a>>>, AepError> {
|
||||
match self.sequence(start, end, depth) {
|
||||
@@ -140,3 +166,113 @@ impl<'a> Parser<'a> {
|
||||
Ok(Body::Data(Cow::Borrowed(self.slice(start, end))))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Every children `Vec` in the tree, depth first.
|
||||
fn child_vecs<'f, 'a>(chunks: &'f [Chunk<'a>], out: &mut Vec<&'f Vec<Chunk<'a>>>) {
|
||||
for c in chunks {
|
||||
match &c.body {
|
||||
Body::List { children, .. } | Body::Chunks(children) => {
|
||||
out.push(children);
|
||||
child_vecs(children, out);
|
||||
}
|
||||
Body::Data(_) | Body::OpaqueList { .. } => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every sibling list is allocated once at its exact length.
|
||||
#[test]
|
||||
fn every_sibling_list_is_allocated_once_at_its_exact_length() {
|
||||
let inner: Vec<Chunk<'static>> = (0..37u8).map(|i| Chunk::data(*b"cdat", vec![i; usize::from(i % 5)])).collect();
|
||||
let middle: Vec<Chunk<'static>> = (0..9).map(|_| Chunk::list(*b"tdbs", inner.clone())).collect();
|
||||
let file = AepFile { chunks: vec![Chunk::data(*b"head", vec![0; 3]), Chunk::list(*b"Fold", middle)], trailer: Cow::Borrowed(&[]) };
|
||||
let bytes = crate::write(&file).expect("write");
|
||||
let parsed = parse(&bytes).expect("parse");
|
||||
assert_eq!(crate::write(&parsed).expect("rewrite"), bytes);
|
||||
let mut vecs = vec![&parsed.chunks];
|
||||
child_vecs(&parsed.chunks, &mut vecs);
|
||||
assert_eq!(vecs.len(), 11);
|
||||
// `try_reserve_exact` gives exactly the requested capacity with std's allocator today; a
|
||||
// Vec grown push by push would have rounded up to a power of two (37 -> 64, 9 -> 16).
|
||||
for v in vecs {
|
||||
assert_eq!(v.capacity(), v.len(), "a sibling Vec grew past its length");
|
||||
}
|
||||
}
|
||||
|
||||
/// The preallocation is bounded by the input and the chunk limit.
|
||||
#[test]
|
||||
fn the_preallocation_is_bounded_by_the_input_and_the_chunk_limit() {
|
||||
// 1000 empty headers: the most chunks the bytes can hold.
|
||||
let mut data = b"RIFX\0\0\0\0Egg!".to_vec();
|
||||
data.extend(std::iter::repeat_n(*b"abcd\0\0\0\0", 1000).flatten());
|
||||
let declared = u32::try_from(data.len() - 8).expect("small");
|
||||
data[4..8].copy_from_slice(&declared.to_be_bytes());
|
||||
let parser = Parser { data: &data, limits: Limits::default(), count: 0 };
|
||||
assert_eq!(parser.count_siblings(12, data.len()), 1000);
|
||||
assert!(parser.count_siblings(12, data.len()) <= (data.len() - 12) / 8);
|
||||
// A header whose size runs past the end stops the count; a stray tail is not counted.
|
||||
assert_eq!(parser.count_siblings(12, data.len() - 3), 999);
|
||||
let mut big = data.clone();
|
||||
big[16..20].copy_from_slice(&u32::MAX.to_be_bytes());
|
||||
let parser = Parser { data: &big, limits: Limits::default(), count: 0 };
|
||||
assert_eq!(parser.count_siblings(12, big.len()), 0);
|
||||
let f = parse(&data).expect("parse");
|
||||
assert_eq!((f.chunks.len(), f.chunks.capacity()), (1000, 1000));
|
||||
// Under a tighter limit the parse fails as before, without sizing for more than allowed.
|
||||
let tight = Limits { max_depth: 64, max_chunks: 10 };
|
||||
assert_eq!(parse_with_limits(&data, tight), Err(AepError::TooManyChunks));
|
||||
// The reservation never exceeds what the chunk limit still allows.
|
||||
let mut parser = Parser { data: &data, limits: tight, count: 0 };
|
||||
assert_eq!(parser.capacity_for(12, data.len()), 10);
|
||||
parser.count = 7;
|
||||
assert_eq!(parser.capacity_for(12, data.len()), 3);
|
||||
parser.count = 11;
|
||||
assert_eq!(parser.capacity_for(12, data.len()), 0);
|
||||
let parser = Parser { data: &data, limits: Limits::default(), count: 0 };
|
||||
assert_eq!(parser.capacity_for(12, data.len()), 1000);
|
||||
}
|
||||
|
||||
/// `LIST Fold` around `inner`, followed by 6 bytes too short for a header, so the list's
|
||||
/// sequence fails and the list is kept opaque.
|
||||
fn broken_level(inner: &[u8]) -> Vec<u8> {
|
||||
let mut body = b"Fold".to_vec();
|
||||
body.extend_from_slice(inner);
|
||||
body.extend_from_slice(&[0xee; 6]);
|
||||
let mut out = b"LIST".to_vec();
|
||||
out.extend_from_slice(&u32::try_from(body.len()).expect("small").to_be_bytes());
|
||||
out.extend_from_slice(&body);
|
||||
out
|
||||
}
|
||||
|
||||
/// Nested lists with broken tails parse each header once.
|
||||
#[test]
|
||||
fn nested_lists_with_broken_tails_parse_each_header_once() {
|
||||
// A wide, well-formed list at the bottom, then 50 levels that each fail and fall back to
|
||||
// opaque: every level re-parses its child before giving up.
|
||||
let mut wide = b"Fold".to_vec();
|
||||
wide.extend(std::iter::repeat_n(*b"abcd\0\0\0\0", 1000).flatten());
|
||||
let mut level = b"LIST".to_vec();
|
||||
level.extend_from_slice(&u32::try_from(wide.len()).expect("small").to_be_bytes());
|
||||
level.extend_from_slice(&wide);
|
||||
for _ in 0..50 {
|
||||
level = broken_level(&level);
|
||||
}
|
||||
let mut data = b"RIFX\0\0\0\0Egg!".to_vec();
|
||||
data.extend_from_slice(&level);
|
||||
let declared = u32::try_from(data.len() - 8).expect("small");
|
||||
data[4..8].copy_from_slice(&declared.to_be_bytes());
|
||||
// 1 + 50 + 1000 headers in the file: the parse counts each exactly once (the count spans
|
||||
// every attempt), so the work stays linear in the input.
|
||||
let headers = 1 + 50 + 1000;
|
||||
let f = parse_with_limits(&data, Limits { max_depth: 64, max_chunks: headers }).expect("parse");
|
||||
assert_eq!(parse_with_limits(&data, Limits { max_depth: 64, max_chunks: headers - 1 }), Err(AepError::TooManyChunks));
|
||||
// The outermost list keeps its bytes, and the file writes back unchanged.
|
||||
let [top] = f.chunks.as_slice() else { panic!("one top-level chunk") };
|
||||
assert!(matches!(&top.body, Body::OpaqueList { list_type, data } if list_type == b"Fold" && data.len() + 12 == level.len()));
|
||||
assert_eq!(crate::write(&f).expect("write"), data);
|
||||
}
|
||||
}
|
||||
|
||||
+24
-11
@@ -2,8 +2,10 @@
|
||||
//! linear-light switches, the time display and the frame count start. Cases 620-634: one change
|
||||
//! each against the base project 620; every field below is the only byte (or chunk) that changes.
|
||||
|
||||
use crate::bytes::be_u32;
|
||||
use crate::chunk::AepFile;
|
||||
use crate::footage::json_string;
|
||||
use crate::index::{Node, Root, TreeIndex};
|
||||
|
||||
/// The time display style (`nhed` byte 8, feet + frames byte 11, film type byte 13).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -54,7 +56,17 @@ pub struct ProjectSettings {
|
||||
impl ProjectSettings {
|
||||
/// Reads the settings from the top-level chunks.
|
||||
pub fn read(file: &AepFile<'_>) -> ProjectSettings {
|
||||
let nhed = file.chunk(b"nhed").and_then(|c| c.bytes());
|
||||
Self::of(Root::from(file))
|
||||
}
|
||||
|
||||
/// Reads the settings from the top-level chunks of an indexed file.
|
||||
pub fn read_index(index: &TreeIndex<'_>) -> ProjectSettings {
|
||||
Self::of(Root::from(index))
|
||||
}
|
||||
|
||||
/// Reads the settings from the top-level chunks of either tree.
|
||||
pub fn of(file: Root<'_, '_>) -> ProjectSettings {
|
||||
let nhed = file.chunk(b"nhed").and_then(Node::bytes);
|
||||
let byte = |i: usize| nhed.and_then(|b| b.get(i).copied());
|
||||
let bits_per_channel = byte(15).and_then(|v| match v {
|
||||
0 => Some(8),
|
||||
@@ -73,9 +85,9 @@ impl ProjectSettings {
|
||||
1 => Some(1),
|
||||
_ => None,
|
||||
});
|
||||
let flag = |id: &[u8; 4]| file.chunk(id).and_then(|c| c.bytes()).and_then(|b| b.first().copied()) == Some(1);
|
||||
let working_gamma_24 = file.chunk(b"dwga").and_then(|c| c.bytes()).and_then(|b| b.get(..4)).map(|b| b != [0, 0, 0, 0]);
|
||||
let scene_compensation = file.chunk(b"acer").and_then(|c| c.bytes()).and_then(|b| b.first().copied()).map(|v| v != 0);
|
||||
let flag = |id: &[u8; 4]| file.chunk(id).and_then(Node::bytes).and_then(|b| b.first().copied()) == Some(1);
|
||||
let working_gamma_24 = file.chunk(b"dwga").and_then(Node::bytes).and_then(|b| b.get(..4)).map(|b| b != [0, 0, 0, 0]);
|
||||
let scene_compensation = file.chunk(b"acer").and_then(Node::bytes).and_then(|b| b.first().copied()).map(|v| v != 0);
|
||||
ProjectSettings {
|
||||
bits_per_channel,
|
||||
working_space: working_space(file),
|
||||
@@ -90,9 +102,10 @@ impl ProjectSettings {
|
||||
}
|
||||
|
||||
/// The working space: the `Utf8` right after `PwCs`.
|
||||
fn working_space(file: &AepFile<'_>) -> Option<WorkingSpace> {
|
||||
let at = file.chunks.iter().position(|c| c.is(b"PwCs"))?;
|
||||
let json = file.chunks.get(at.checked_add(1)?).filter(|c| c.is(b"Utf8"))?.bytes()?;
|
||||
fn working_space(file: Root<'_, '_>) -> Option<WorkingSpace> {
|
||||
let mut chunks = file.chunks();
|
||||
chunks.find(|c| c.is(b"PwCs"))?;
|
||||
let json = chunks.next().filter(|c| c.is(b"Utf8"))?.bytes()?;
|
||||
let trimmed: Vec<u8> = json.iter().copied().filter(|b| !b.is_ascii_whitespace() && *b != 0).collect();
|
||||
if trimmed == b"{}" {
|
||||
return Some(WorkingSpace::None);
|
||||
@@ -130,7 +143,7 @@ fn base64_decode(text: &str) -> Option<Vec<u8>> {
|
||||
/// The profile description (ICC.1 tag `desc`): a v2 `desc` (ASCII) or a v4 `mluc` (its first
|
||||
/// record, UTF-16BE). `None` when the profile or the tag is cut short.
|
||||
fn icc_description(icc: &[u8]) -> Option<String> {
|
||||
let be32 = |at: usize| icc.get(at..at.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_be_bytes);
|
||||
let be32 = |at: usize| be_u32(icc, at);
|
||||
let count = be32(128)?;
|
||||
let tag = (0..count.min(1024)).find_map(|i| {
|
||||
let at = 132usize.checked_add(usize::try_from(i).ok()?.checked_mul(12)?)?;
|
||||
@@ -141,13 +154,13 @@ fn icc_description(icc: &[u8]) -> Option<String> {
|
||||
let body = icc.get(off..off.checked_add(len)?)?;
|
||||
let text = match body.get(..4)? {
|
||||
b"desc" => {
|
||||
let n = usize::try_from(u32::from_be_bytes(body.get(8..12)?.try_into().ok()?)).ok()?;
|
||||
let n = usize::try_from(be_u32(body, 8)?).ok()?;
|
||||
let ascii = body.get(12..12usize.checked_add(n)?)?;
|
||||
String::from_utf8_lossy(ascii.split(|b| *b == 0).next().unwrap_or_default()).into_owned()
|
||||
}
|
||||
b"mluc" => {
|
||||
let n = usize::try_from(u32::from_be_bytes(body.get(20..24)?.try_into().ok()?)).ok()?;
|
||||
let at = usize::try_from(u32::from_be_bytes(body.get(24..28)?.try_into().ok()?)).ok()?;
|
||||
let n = usize::try_from(be_u32(body, 20)?).ok()?;
|
||||
let at = usize::try_from(be_u32(body, 24)?).ok()?;
|
||||
let units: Vec<u16> = body.get(at..at.checked_add(n)?)?.as_chunks::<2>().0.iter().map(|p| u16::from_be_bytes(*p)).collect();
|
||||
String::from_utf16_lossy(&units)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//! points are in pixels relative to the layer origin.
|
||||
|
||||
use crate::masks::ShapePath;
|
||||
use crate::typed::{Layer, PropGroup, PropNode, Property};
|
||||
use crate::typed::{Layer, PropGroup, PropNode};
|
||||
use crate::values::{Tracked, read, read_opt};
|
||||
|
||||
/// The stroke line cap (`ADBE Vector Stroke Line Cap`, stored as a double; butt is the default and
|
||||
@@ -145,7 +145,7 @@ fn number(group: &PropGroup<'_, '_>, name: &str) -> Option<Tracked<f64>> {
|
||||
fn paths_of(group: &PropGroup<'_, '_>) -> Vec<ShapePath> {
|
||||
let Some(PropNode::Other(oms)) = group.get("ADBE Vector Shape") else { return Vec::new() };
|
||||
let Some(omks) = oms.child_list(b"omks") else { return Vec::new() };
|
||||
omks.children().iter().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect()
|
||||
omks.children().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect()
|
||||
}
|
||||
|
||||
fn item(match_name: String, group: &PropGroup<'_, '_>) -> ShapeItem {
|
||||
@@ -269,12 +269,3 @@ impl<'f, 'a> Layer<'f, 'a> {
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'f, 'a> Property<'f, 'a> {
|
||||
/// The ARGB fractions of a static colour property (4 components stored as doubles 0..255).
|
||||
pub fn color_argb(&self) -> Option<[f64; 4]> {
|
||||
let v = self.static_value()?;
|
||||
let [a, r, g, b] = *v.as_slice().first_chunk::<4>()?;
|
||||
Some([a / 255.0, r / 255.0, g / 255.0, b / 255.0])
|
||||
}
|
||||
}
|
||||
|
||||
+945
-145
File diff suppressed because it is too large
Load Diff
+92
-34
@@ -4,42 +4,57 @@
|
||||
//!
|
||||
//! Only facts that were established from our own one-change-per-file corpus and are checked by
|
||||
//! tests against it are decoded (see `docs/aep-format.md` for the evidence). Everything else
|
||||
//! stays reachable through the raw [`Chunk`] tree. Views borrow the file and never fail loudly:
|
||||
//! a chunk that does not have the expected shape yields `None` or is skipped.
|
||||
//! stays reachable through the raw chunk tree ([`Node`]). Views borrow the file and never fail
|
||||
//! loudly: a chunk that does not have the expected shape yields `None` or is skipped.
|
||||
//!
|
||||
//! A view reads either tree: the owned [`Chunk`](crate::Chunk) tree
|
||||
//! ([`Project::new`]) or the flat [`TreeIndex`] the importer builds ([`Project::from_index`]);
|
||||
//! both give the same answers.
|
||||
|
||||
use crate::bytes::{be_f64, be_u16, be_u32, text, until_nul};
|
||||
use crate::chunk::{AepFile, Chunk};
|
||||
use crate::chunk::AepFile;
|
||||
use crate::index::{Node, Root, TreeIndex};
|
||||
|
||||
/// The name a user gave a property or group: the `Utf8` inside its `tdsn` chunk. `None` for the
|
||||
/// sentinel `-_0_/-` ("no custom name") and for empty names.
|
||||
pub(crate) fn user_name(chunk: &Chunk<'_>) -> Option<String> {
|
||||
let name = chunk.child(b"tdsn")?.children().iter().find(|c| c.is(b"Utf8"))?.bytes().map(text)?;
|
||||
pub(crate) fn user_name(chunk: Node<'_, '_>) -> Option<String> {
|
||||
let name = chunk.child(b"tdsn")?.child(b"Utf8")?.bytes().map(text)?;
|
||||
(!name.is_empty() && name != "-_0_/-").then_some(name)
|
||||
}
|
||||
|
||||
/// The content of a `Utf8` child as text (not trimmed).
|
||||
pub(crate) fn utf8_child(chunk: &Chunk<'_>) -> Option<String> {
|
||||
chunk.child(b"Utf8").and_then(Chunk::bytes).map(text)
|
||||
pub(crate) fn utf8_child(chunk: Node<'_, '_>) -> Option<String> {
|
||||
chunk.child(b"Utf8").and_then(Node::bytes).map(text)
|
||||
}
|
||||
|
||||
/// The project: the top-level `LIST Fold`, which holds the project items.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Project<'f, 'a> {
|
||||
pub(crate) fold: &'f Chunk<'a>,
|
||||
pub(crate) fold: Node<'f, 'a>,
|
||||
/// The top-level `LIST EfdG` (project-level effect definitions), when there is one.
|
||||
pub(crate) effect_defs: Option<&'f Chunk<'a>>,
|
||||
pub(crate) effect_defs: Option<Node<'f, 'a>>,
|
||||
}
|
||||
|
||||
impl<'f, 'a> Project<'f, 'a> {
|
||||
/// The project view of a file, or `None` when it has no `LIST Fold`.
|
||||
pub fn new(file: &'f AepFile<'a>) -> Option<Self> {
|
||||
file.list(b"Fold").map(|fold| Self { fold, effect_defs: file.list(b"EfdG") })
|
||||
Self::of(Root::from(file))
|
||||
}
|
||||
|
||||
/// The project view of an indexed file ([`TreeIndex`]), or `None` when it has no `LIST Fold`.
|
||||
pub fn from_index(index: &'f TreeIndex<'a>) -> Option<Self> {
|
||||
Self::of(Root::from(index))
|
||||
}
|
||||
|
||||
/// The project view of either tree: its `LIST Fold` and `LIST EfdG`.
|
||||
pub fn of(root: Root<'f, 'a>) -> Option<Self> {
|
||||
root.list(b"Fold").map(|fold| Self { fold, effect_defs: root.list(b"EfdG") })
|
||||
}
|
||||
|
||||
/// The top-level items (the project panel's root; folders hold their children, see
|
||||
/// [`Item::children`]).
|
||||
pub fn items(&self) -> impl Iterator<Item = Item<'f, 'a>> + 'f {
|
||||
self.fold.children().iter().filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk })
|
||||
self.fold.children().filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk })
|
||||
}
|
||||
|
||||
/// True when the `LIST Fold` could not be read as chunks (damaged): its items cannot be seen.
|
||||
@@ -78,12 +93,12 @@ pub enum ItemKind {
|
||||
/// A project item (`LIST Item`).
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Item<'f, 'a> {
|
||||
pub(crate) chunk: &'f Chunk<'a>,
|
||||
pub(crate) chunk: Node<'f, 'a>,
|
||||
}
|
||||
|
||||
impl<'f, 'a> Item<'f, 'a> {
|
||||
/// The underlying chunk.
|
||||
pub fn chunk(&self) -> &'f Chunk<'a> {
|
||||
pub fn chunk(&self) -> Node<'f, 'a> {
|
||||
self.chunk
|
||||
}
|
||||
|
||||
@@ -106,7 +121,7 @@ impl<'f, 'a> Item<'f, 'a> {
|
||||
|
||||
/// The items inside a folder (`LIST Sfdr`).
|
||||
pub fn children(&self) -> impl Iterator<Item = Item<'f, 'a>> + 'f {
|
||||
self.chunk.child_list(b"Sfdr").into_iter().flat_map(|sfdr| sfdr.children().iter()).filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk })
|
||||
self.chunk.child_list(b"Sfdr").into_iter().flat_map(Node::children).filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk })
|
||||
}
|
||||
|
||||
/// Whether the item has the `LIST Sfdr` of a folder's contents (After Effects writes an empty
|
||||
@@ -117,7 +132,7 @@ impl<'f, 'a> Item<'f, 'a> {
|
||||
|
||||
/// True when the folder's `LIST Sfdr` is there but could not be read as chunks (damaged).
|
||||
pub fn folder_contents_unreadable(&self) -> bool {
|
||||
self.chunk.child_list(b"Sfdr").is_some_and(Chunk::is_opaque_list)
|
||||
self.chunk.child_list(b"Sfdr").is_some_and(Node::is_opaque_list)
|
||||
}
|
||||
|
||||
/// The composition view when this item has composition data (`cdta`).
|
||||
@@ -135,6 +150,10 @@ pub struct TimeBase {
|
||||
}
|
||||
|
||||
impl TimeBase {
|
||||
/// The NTSC rates as `(numerator, denominator)` and the value After Effects stores for each (a
|
||||
/// float32 of exactly these decimals): the one table an importer and an exporter use.
|
||||
pub const NTSC: [((u32, u32), f32); 4] = [((24000, 1001), 23.976), ((30000, 1001), 29.97), ((60000, 1001), 59.94), ((120000, 1001), 119.88)];
|
||||
|
||||
/// Frames per second (`timescale / frame_duration`), `None` for a zero duration.
|
||||
pub fn frame_rate(&self) -> Option<f64> {
|
||||
(self.frame_duration != 0).then(|| f64::from(self.timescale) / f64::from(self.frame_duration))
|
||||
@@ -149,7 +168,7 @@ impl TimeBase {
|
||||
/// A composition item (an [`Item`] with `cdta`).
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Comp<'f, 'a> {
|
||||
pub(crate) item: &'f Chunk<'a>,
|
||||
pub(crate) item: Node<'f, 'a>,
|
||||
}
|
||||
|
||||
impl<'f, 'a> Comp<'f, 'a> {
|
||||
@@ -163,19 +182,19 @@ impl<'f, 'a> Comp<'f, 'a> {
|
||||
/// The layers, top layer first (`LIST Layr`; the `DLay`/`SLay`/`CLay`/`SecL` lists next to
|
||||
/// them are not layers).
|
||||
pub fn layers(&self) -> impl Iterator<Item = Layer<'f, 'a>> + 'f {
|
||||
self.item.children().iter().filter(|c| c.is_list(b"Layr")).map(|chunk| Layer { chunk })
|
||||
self.item.children().filter(|c| c.is_list(b"Layr")).map(|chunk| Layer { chunk })
|
||||
}
|
||||
}
|
||||
|
||||
/// A layer (`LIST Layr`).
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Layer<'f, 'a> {
|
||||
pub(crate) chunk: &'f Chunk<'a>,
|
||||
pub(crate) chunk: Node<'f, 'a>,
|
||||
}
|
||||
|
||||
impl<'f, 'a> Layer<'f, 'a> {
|
||||
/// The underlying chunk.
|
||||
pub fn chunk(&self) -> &'f Chunk<'a> {
|
||||
pub fn chunk(&self) -> Node<'f, 'a> {
|
||||
self.chunk
|
||||
}
|
||||
|
||||
@@ -205,12 +224,12 @@ pub enum PropNode<'f, 'a> {
|
||||
/// A property (`LIST tdbs`).
|
||||
Property(Property<'f, 'a>),
|
||||
/// Any other list that follows a match name (observed: `otst`, the effect instance `sspc`).
|
||||
Other(&'f Chunk<'a>),
|
||||
Other(Node<'f, 'a>),
|
||||
}
|
||||
|
||||
impl<'f, 'a> PropNode<'f, 'a> {
|
||||
/// The node a `LIST` after a match name is.
|
||||
fn of(c: &'f Chunk<'a>) -> Self {
|
||||
fn of(c: Node<'f, 'a>) -> Self {
|
||||
if c.is_list(b"tdgp") {
|
||||
PropNode::Group(PropGroup { chunk: c })
|
||||
} else if c.is_list(b"tdbs") {
|
||||
@@ -233,17 +252,18 @@ pub struct PropEntry<'f, 'a> {
|
||||
/// `ADBE Group End` closes the group and names nothing.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct PropGroup<'f, 'a> {
|
||||
pub(crate) chunk: &'f Chunk<'a>,
|
||||
pub(crate) chunk: Node<'f, 'a>,
|
||||
}
|
||||
|
||||
impl<'f, 'a> PropGroup<'f, 'a> {
|
||||
/// A group view of a `LIST tdgp` chunk.
|
||||
pub fn from_chunk(chunk: &'f Chunk<'a>) -> Option<Self> {
|
||||
/// A group view of a `LIST tdgp` chunk (a [`Chunk`](crate::Chunk) or a [`Node`]).
|
||||
pub fn from_chunk(chunk: impl Into<Node<'f, 'a>>) -> Option<Self> {
|
||||
let chunk = chunk.into();
|
||||
chunk.is_list(b"tdgp").then_some(Self { chunk })
|
||||
}
|
||||
|
||||
/// The underlying chunk.
|
||||
pub fn chunk(&self) -> &'f Chunk<'a> {
|
||||
pub fn chunk(&self) -> Node<'f, 'a> {
|
||||
self.chunk
|
||||
}
|
||||
|
||||
@@ -264,7 +284,7 @@ impl<'f, 'a> PropGroup<'f, 'a> {
|
||||
for c in self.chunk.children() {
|
||||
if c.is(b"tdmn") {
|
||||
pending = c.bytes().map(match_name);
|
||||
} else if c.id == crate::chunk::LIST
|
||||
} else if c.is(&crate::chunk::LIST)
|
||||
&& let Some(name) = pending.take()
|
||||
{
|
||||
out.push(PropEntry { match_name: name, node: PropNode::of(c) });
|
||||
@@ -285,9 +305,9 @@ impl<'f, 'a> PropGroup<'f, 'a> {
|
||||
for c in self.chunk.children() {
|
||||
if c.is(b"tdmn") {
|
||||
pending = c.bytes();
|
||||
} else if c.id == crate::chunk::LIST
|
||||
} else if c.is(&crate::chunk::LIST)
|
||||
&& let Some(body) = pending.take()
|
||||
&& match_name(body) == name
|
||||
&& match_name_is(body, name)
|
||||
{
|
||||
return Some(PropNode::of(c));
|
||||
}
|
||||
@@ -314,21 +334,30 @@ fn match_name(body: &[u8]) -> String {
|
||||
text(until_nul(body))
|
||||
}
|
||||
|
||||
/// `match_name(body) == name` without building the `String`: equal bytes match; otherwise only
|
||||
/// a body that is not valid UTF-8 can still match (through its U+FFFD replacements), and only a
|
||||
/// name that holds U+FFFD.
|
||||
fn match_name_is(body: &[u8], name: &str) -> bool {
|
||||
let bytes = until_nul(body);
|
||||
bytes == name.as_bytes() || (name.contains('\u{FFFD}') && String::from_utf8_lossy(bytes) == name)
|
||||
}
|
||||
|
||||
/// A property (`LIST tdbs`): `tdsb`, `tdsn`, the 124-byte header `tdb4`, then either the static
|
||||
/// value `cdat` or a keyframe `LIST list`.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Property<'f, 'a> {
|
||||
pub(crate) chunk: &'f Chunk<'a>,
|
||||
pub(crate) chunk: Node<'f, 'a>,
|
||||
}
|
||||
|
||||
impl<'f, 'a> Property<'f, 'a> {
|
||||
/// A property view of a `LIST tdbs` chunk.
|
||||
pub fn from_chunk(chunk: &'f Chunk<'a>) -> Option<Self> {
|
||||
/// A property view of a `LIST tdbs` chunk (a [`Chunk`](crate::Chunk) or a [`Node`]).
|
||||
pub fn from_chunk(chunk: impl Into<Node<'f, 'a>>) -> Option<Self> {
|
||||
let chunk = chunk.into();
|
||||
chunk.is_list(b"tdbs").then_some(Self { chunk })
|
||||
}
|
||||
|
||||
/// The underlying chunk.
|
||||
pub fn chunk(&self) -> &'f Chunk<'a> {
|
||||
pub fn chunk(&self) -> Node<'f, 'a> {
|
||||
self.chunk
|
||||
}
|
||||
|
||||
@@ -436,12 +465,13 @@ pub struct KeyLayout {
|
||||
impl<'f> KeyList<'f> {
|
||||
/// A view of a `LIST list` chunk. `None` when `lhd3` is not 52 bytes or `ldat` is not exactly
|
||||
/// `count * record_size` bytes (a list without `ldat` must have no records).
|
||||
pub fn from_chunk<'a>(chunk: &'f Chunk<'a>) -> Option<Self> {
|
||||
pub fn from_chunk<'a: 'f>(chunk: impl Into<Node<'f, 'a>>) -> Option<Self> {
|
||||
let chunk = chunk.into();
|
||||
if !chunk.is_list(b"list") {
|
||||
return None;
|
||||
}
|
||||
let header = ListHeader::parse(chunk.child(b"lhd3")?.bytes()?)?;
|
||||
let ldat: &[u8] = chunk.child(b"ldat").and_then(Chunk::bytes).unwrap_or(&[]);
|
||||
let ldat: &[u8] = chunk.child(b"ldat").and_then(Node::bytes).unwrap_or(&[]);
|
||||
let expected = usize::try_from(header.count).ok()?.checked_mul(usize::try_from(header.record_size).ok()?)?;
|
||||
(ldat.len() == expected).then_some(Self { header, ldat, layout: None })
|
||||
}
|
||||
@@ -500,6 +530,7 @@ impl<'f> Keyframe<'f> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Chunk;
|
||||
|
||||
fn key_list(count: u32, record_size: u32, ldat: &[u8]) -> Chunk<'static> {
|
||||
let mut lhd3 = vec![0u8; 52];
|
||||
@@ -543,4 +574,31 @@ mod tests {
|
||||
assert!(g.get("C").is_none());
|
||||
assert_eq!(g.names(), ["A", "B", "A"]);
|
||||
}
|
||||
|
||||
/// Match name is agrees with comparing the decoded name.
|
||||
#[test]
|
||||
fn match_name_is_agrees_with_comparing_the_decoded_name() {
|
||||
let bodies: [&[u8]; 9] = [
|
||||
b"ADBE Position\0\0\0",
|
||||
b"ADBE Position",
|
||||
b"ADBE Pos",
|
||||
b"",
|
||||
b"\0ADBE",
|
||||
b"ADBE \xff Bad\0",
|
||||
b"\xff",
|
||||
b"\xef\xbf\xbd",
|
||||
"Gr\u{fc}n\0".as_bytes(),
|
||||
];
|
||||
let names = ["ADBE Position", "ADBE Pos", "", "ADBE \u{FFFD} Bad", "\u{FFFD}", "Gr\u{fc}n", "ADBE Position\0", "ADBE \u{FFFD}"];
|
||||
for body in bodies {
|
||||
for name in names {
|
||||
assert_eq!(match_name_is(body, name), match_name(body) == name, "{body:?} vs {name:?}");
|
||||
}
|
||||
}
|
||||
// Names read back from a group find their entries, a damaged one included.
|
||||
let group = Chunk::list(*b"tdgp", vec![Chunk::data(*b"tdmn", b"X\xffY\0".to_vec()), Chunk::list(*b"tdbs", Vec::new())]);
|
||||
let g = PropGroup::from_chunk(&group).expect("group");
|
||||
let names = g.names();
|
||||
assert!(g.get(&names[0]).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
//! 100 %. These defaults are verified for AV layers (solids); cameras and lights use other
|
||||
//! conventions (see the `camera` module).
|
||||
|
||||
use crate::bytes::be_f64;
|
||||
use crate::typed::{Layer, Project, PropGroup, PropNode};
|
||||
|
||||
/// Where a [`Tracked`] value came from.
|
||||
@@ -192,8 +193,7 @@ impl<'f, 'a> Layer<'f, 'a> {
|
||||
let otda = otst.child_list(b"otky")?.child(b"otda")?.bytes()?;
|
||||
let mut degrees = [0.0; 3];
|
||||
for (i, slot) in degrees.iter_mut().enumerate() {
|
||||
let b = otda.get(i * 8..i * 8 + 8)?;
|
||||
*slot = f64::from_be_bytes(b.try_into().ok()?);
|
||||
*slot = be_f64(otda, i * 8)?;
|
||||
}
|
||||
Some(Orientation {
|
||||
degrees,
|
||||
@@ -223,12 +223,10 @@ impl<'f, 'a> Layer<'f, 'a> {
|
||||
.child_list(b"otky")
|
||||
.map(|k| {
|
||||
k.children()
|
||||
.iter()
|
||||
.filter(|c| c.is(b"otda"))
|
||||
.map(|c| {
|
||||
let b = c.bytes()?;
|
||||
let at = |i: usize| b.get(i * 8..i * 8 + 8).and_then(|s| s.try_into().ok()).map(f64::from_be_bytes);
|
||||
Some([at(0)?, at(1)?, at(2)?])
|
||||
Some([be_f64(b, 0)?, be_f64(b, 8)?, be_f64(b, 16)?])
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::bytes::{be_u16, be_u32};
|
||||
use crate::bytes::be_u32;
|
||||
use crate::chunk::AepFile;
|
||||
use crate::index::{Root, TreeIndex};
|
||||
|
||||
/// An After Effects version as packed in `head` / `svap`: `26.2.1x2` is major 26, minor 2, bug-fix
|
||||
/// 1, build 2 (the form of the scripting API's `app.version`).
|
||||
@@ -71,21 +72,38 @@ impl AepFile<'_> {
|
||||
/// version there, and the older version for File ▸ Save a Copy As 25.x / 24.x. `None` when the
|
||||
/// chunk is missing, short or holds no version.
|
||||
pub fn file_version(&self) -> Option<AeVersion> {
|
||||
AeVersion::from_word(be_u32(self.chunk(b"head")?.bytes()?, 4)?)
|
||||
Root::from(self).file_version()
|
||||
}
|
||||
|
||||
/// The version of the After Effects that wrote the file: `svap` bytes 0 to 3 (the same word in a
|
||||
/// down-saved copy as in a normal save). `None` when the chunk is missing (After Effects opens such
|
||||
/// a file), short or holds no version.
|
||||
pub fn saved_by_version(&self) -> Option<AeVersion> {
|
||||
AeVersion::from_word(be_u32(self.chunk(b"svap")?.bytes()?, 0)?)
|
||||
Root::from(self).saved_by_version()
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeIndex<'_> {
|
||||
/// [`AepFile::file_version`] of the indexed file.
|
||||
pub fn file_version(&self) -> Option<AeVersion> {
|
||||
Root::from(self).file_version()
|
||||
}
|
||||
|
||||
/// The `u16` at `head` byte 0: 97 (`0x61`) in a 2026 file, 96 in a copy saved as 25.x, 95 as
|
||||
/// 24.x. It steps with the major version of [`AepFile::file_version`]; what it counts is not
|
||||
/// known, so nothing is derived from it.
|
||||
pub fn head_revision(&self) -> Option<u16> {
|
||||
be_u16(self.chunk(b"head")?.bytes()?, 0)
|
||||
/// [`AepFile::saved_by_version`] of the indexed file.
|
||||
pub fn saved_by_version(&self) -> Option<AeVersion> {
|
||||
Root::from(self).saved_by_version()
|
||||
}
|
||||
}
|
||||
|
||||
impl Root<'_, '_> {
|
||||
/// The version the file is written for (`head` bytes 4 to 7).
|
||||
pub fn file_version(self) -> Option<AeVersion> {
|
||||
AeVersion::from_word(be_u32(self.chunk(b"head")?.bytes()?, 4)?)
|
||||
}
|
||||
|
||||
/// The version of the application that wrote the file (`svap`).
|
||||
pub fn saved_by_version(self) -> Option<AeVersion> {
|
||||
AeVersion::from_word(be_u32(self.chunk(b"svap")?.bytes()?, 0)?)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,18 @@ const MAX_WRITE_DEPTH: usize = 256;
|
||||
/// [`AepError::TooDeep`] for an absurdly deep hand-built tree.
|
||||
pub fn write(file: &AepFile<'_>) -> Result<Vec<u8>, AepError> {
|
||||
let mut out = Vec::new();
|
||||
// Sized once to the exact output. A tree past the 32-bit container size fails at once (the
|
||||
// same error the write below would give after serialising it). `try_reserve` so a hand-built
|
||||
// tree that repeats one large borrowed body many times cannot abort on the allocation; the
|
||||
// writer then grows as it goes, as before.
|
||||
if let Some(len) = sequence_len(&file.chunks, 1) {
|
||||
if u32::try_from(len.saturating_add(4)).is_err() {
|
||||
return Err(AepError::TooLarge);
|
||||
}
|
||||
if let Some(total) = len.checked_add(12).and_then(|n| n.checked_add(file.trailer.len())) {
|
||||
let _ = out.try_reserve_exact(total);
|
||||
}
|
||||
}
|
||||
out.extend_from_slice(b"RIFX");
|
||||
out.extend_from_slice(&[0; 4]);
|
||||
out.extend_from_slice(b"Egg!");
|
||||
@@ -56,6 +68,28 @@ fn write_sequence(out: &mut Vec<u8>, chunks: &[Chunk<'_>], depth: usize) -> Resu
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The number of bytes [`write_sequence`] produces for `chunks`; `None` past the depth limit or
|
||||
/// on overflow (both make the write fail).
|
||||
fn sequence_len(chunks: &[Chunk<'_>], depth: usize) -> Option<usize> {
|
||||
if depth > MAX_WRITE_DEPTH {
|
||||
return None;
|
||||
}
|
||||
let count = chunks.len();
|
||||
let mut total = 0usize;
|
||||
for (i, chunk) in chunks.iter().enumerate() {
|
||||
let body = match &chunk.body {
|
||||
Body::Data(d) => d.len(),
|
||||
Body::Chunks(c) => sequence_len(c, depth + 1)?,
|
||||
Body::List { children, .. } => sequence_len(children, depth + 1)?.checked_add(4)?,
|
||||
Body::OpaqueList { data, .. } => data.len().checked_add(4)?,
|
||||
};
|
||||
// The same pad rule as `write_chunk`.
|
||||
let pad = usize::from(body % 2 == 1 && (chunk.pad.is_some() || i + 1 != count));
|
||||
total = total.checked_add(8)?.checked_add(body)?.checked_add(pad)?;
|
||||
}
|
||||
Some(total)
|
||||
}
|
||||
|
||||
fn write_chunk(out: &mut Vec<u8>, chunk: &Chunk<'_>, is_last: bool, depth: usize) -> Result<(), AepError> {
|
||||
out.extend_from_slice(&chunk.id);
|
||||
let size_at = out.len();
|
||||
@@ -84,3 +118,62 @@ fn write_chunk(out: &mut Vec<u8>, chunk: &Chunk<'_>, is_last: bool, depth: usize
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// The output is sized once to its exact length.
|
||||
#[test]
|
||||
fn the_output_is_sized_once_to_its_exact_length() {
|
||||
let odd = |n: u8| Chunk::data(*b"cdat", vec![n; usize::from(n)]);
|
||||
let mut padded = odd(3);
|
||||
padded.pad = Some(0x7f);
|
||||
let trees = [
|
||||
AepFile { chunks: Vec::new(), trailer: Cow::Borrowed(&[]) },
|
||||
AepFile {
|
||||
chunks: vec![
|
||||
odd(1),
|
||||
Chunk::list(*b"Fold", vec![odd(2), odd(3), Chunk::list(*b"tdgp", vec![odd(5)])]),
|
||||
Chunk { id: *b"LIST", body: Body::OpaqueList { list_type: *b"btdk", data: Cow::Owned(vec![1, 2, 3]) }, pad: None },
|
||||
Chunk { id: *b"tdmn", body: Body::Chunks(vec![odd(7), padded]), pad: None },
|
||||
odd(9),
|
||||
],
|
||||
trailer: Cow::Borrowed(b"<x:xmpmeta/>"),
|
||||
},
|
||||
];
|
||||
for file in &trees {
|
||||
let out = write(file).expect("write");
|
||||
assert_eq!(sequence_len(&file.chunks, 1).map(|n| n + 12 + file.trailer.len()), Some(out.len()));
|
||||
// Exact with std's allocator today; grown push by push it would be rounded up.
|
||||
assert_eq!(out.capacity(), out.len(), "the output grew past its size");
|
||||
}
|
||||
}
|
||||
|
||||
/// A tree past the container size fails before serialising.
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[test]
|
||||
fn a_tree_past_the_container_size_fails_before_serialising() {
|
||||
// 4097 chunks borrowing one 1 MiB body: over 4 GiB of output, none of it written.
|
||||
let body = vec![0u8; 1 << 20];
|
||||
let chunks: Vec<Chunk<'_>> = (0..4097).map(|_| Chunk::data(*b"cdat", &body[..])).collect();
|
||||
let file = AepFile { chunks, trailer: Cow::Borrowed(&[]) };
|
||||
assert_eq!(write(&file), Err(AepError::TooLarge));
|
||||
// Just under the limit still sizes, and fails nothing up front.
|
||||
assert!(sequence_len(&file.chunks[..4000], 1).is_some_and(|n| u32::try_from(n + 4).is_ok()));
|
||||
}
|
||||
|
||||
/// A tree past the depth limit has no size.
|
||||
#[test]
|
||||
fn a_tree_past_the_depth_limit_has_no_size() {
|
||||
let mut deep = Chunk::data(*b"cdat", vec![0]);
|
||||
for _ in 0..=MAX_WRITE_DEPTH {
|
||||
deep = Chunk::list(*b"tdgp", vec![deep]);
|
||||
}
|
||||
let file = AepFile { chunks: vec![deep], trailer: Cow::Borrowed(&[]) };
|
||||
assert_eq!(sequence_len(&file.chunks, 1), None);
|
||||
assert_eq!(write(&file), Err(AepError::TooDeep));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +128,13 @@ pub fn resolve(name: &str) -> Option<PathBuf> {
|
||||
locate(name).map(|s| s.path().clone())
|
||||
}
|
||||
|
||||
/// The ids of the compositions an item uses as precomposition layers, as the file stores them:
|
||||
/// the little-endian `u32` of each `idpi` chunk (the importer does not read them; the exporter
|
||||
/// writes them).
|
||||
pub fn used_item_ids(item: &craft_aep::Item<'_, '_>) -> Vec<u32> {
|
||||
item.chunk().children().filter(|c| c.is(b"idpi")).filter_map(|c| Some(u32::from_le_bytes(c.bytes()?.get(..4)?.try_into().ok()?))).collect()
|
||||
}
|
||||
|
||||
/// The bytes of a case (see the module comment), or `None` when it is missing.
|
||||
pub fn try_fixture(name: &str) -> Option<Vec<u8>> {
|
||||
match locate(name)? {
|
||||
|
||||
@@ -25,5 +25,6 @@ mod shapes;
|
||||
mod text_fields;
|
||||
mod text_markers_footage;
|
||||
mod text_style_slots;
|
||||
mod tree_index;
|
||||
mod typed;
|
||||
mod values;
|
||||
|
||||
@@ -117,8 +117,8 @@ fn a_transform_set_to_its_default_adds_no_record() {
|
||||
assert_eq!(t.opacity_percent.source, ValueSource::Default);
|
||||
assert!(layer.properties().unwrap().find(&["ADBE Transform Group", "ADBE Position"]).is_none());
|
||||
// The two cases are layers of one file: they differ in the layer id only.
|
||||
let a = without_layer_id(layer.chunk());
|
||||
skip!(with_layer("299-save-noise.aep", L, |_, other| assert_eq!(a, without_layer_id(other.chunk()))));
|
||||
let a = without_layer_id(&layer.chunk().to_chunk());
|
||||
skip!(with_layer("299-save-noise.aep", L, |_, other| assert_eq!(a, without_layer_id(&other.chunk().to_chunk()))));
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -140,16 +140,13 @@ fn scale_keys_keep_their_ease_type_major() {
|
||||
}
|
||||
assert_near(&keys[0].value().unwrap(), &[1.0, 1.0, 1.0], 1e-9);
|
||||
assert_near(&keys[1].value().unwrap(), &[2.0, 1.5, 1.0], 1e-9);
|
||||
// The raw block: [in speed x3][in influence x3][out speed x3][out influence x3].
|
||||
assert_near(&keys[0].plain_ease_slots().unwrap(), &[0.0, 0.0, 0.0, 0.33, 0.33, 0.33, 0.2, 0.2, 0.2, 0.66, 0.66, 0.66], 1e-9);
|
||||
assert_near(&keys[1].plain_ease_slots().unwrap(), &[0.1, 0.1, 0.1, 0.66, 0.66, 0.66, 0.0, 0.0, 0.0, 0.33, 0.33, 0.33], 1e-9);
|
||||
// The raw block is [in speed x3][in influence x3][out speed x3][out influence x3].
|
||||
for i in 0..3 {
|
||||
assert!(same_ease(&keys[0].ease_component(i).unwrap(), &ease(0.0, 0.33, 0.2, 0.66)), "key 1 component {i}");
|
||||
assert!(same_ease(&keys[1].ease_component(i).unwrap(), &ease(0.1, 0.66, 0.0, 0.33)), "key 2 component {i}");
|
||||
}
|
||||
assert!(keys[0].ease_component(3).is_none());
|
||||
assert_eq!(keys[0].ease_components().unwrap().len(), 3);
|
||||
assert!(keys[0].color_distance().is_none());
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -172,7 +169,6 @@ fn mask_feather_keys_keep_their_ease_type_major() {
|
||||
}
|
||||
assert_near(&keys[0].value().unwrap(), &[0.0, 0.0], 1e-9);
|
||||
assert_near(&keys[1].value().unwrap(), &[50.0, 30.0], 1e-9);
|
||||
assert_near(&keys[0].plain_ease_slots().unwrap(), &[0.0, 0.0, 0.33, 0.33, 15.0, 15.0, 0.66, 0.66], 1e-9);
|
||||
for i in 0..2 {
|
||||
assert!(same_ease(&keys[0].ease_component(i).unwrap(), &ease(0.0, 0.33, 15.0, 0.66)), "key 1 component {i}");
|
||||
assert!(same_ease(&keys[1].ease_component(i).unwrap(), &ease(8.0, 0.66, 0.0, 0.33)), "key 2 component {i}");
|
||||
@@ -213,14 +209,15 @@ fn bezier_colour_keys_carry_the_distance_to_the_next_key() {
|
||||
assert_eq!((k.interpolation_in(), k.interpolation_out()), (Some(Interpolation::Bezier), Some(Interpolation::Bezier)));
|
||||
assert!(same_ease(&k.ease_component(0).unwrap(), &ease(0.0, 0.0, 0.0, 0.0)));
|
||||
}
|
||||
assert!(near(keys[0].color_distance().unwrap(), 255.0 * 2f64.sqrt(), 1e-3), "{:?}", keys[0].color_distance());
|
||||
assert!(near(keys[0].color_distance().unwrap(), 360.6245, 1e-3));
|
||||
assert_eq!(keys[1].color_distance(), Some(0.0));
|
||||
let distance = |k: &craft_aep::Keyframe<'_>| k.spatial().unwrap().segment_length;
|
||||
assert!(near(distance(&keys[0]), 255.0 * 2f64.sqrt(), 1e-3), "{}", distance(&keys[0]));
|
||||
assert!(near(distance(&keys[0]), 360.6245, 1e-3));
|
||||
assert_eq!(distance(&keys[1]), 0.0);
|
||||
}));
|
||||
skip!(with_layer("210-key-fill-color-2key-linear.aep", L, |_, layer| {
|
||||
let p = layer.effects()[0].params().into_iter().find(|p| p.name == "Color").unwrap();
|
||||
let k = p.stored().unwrap().keys().unwrap().keyframes().next().unwrap();
|
||||
assert_eq!(k.color_distance(), Some(0.0));
|
||||
assert_eq!(k.spatial().unwrap().segment_length, 0.0);
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -243,21 +240,19 @@ fn time_stretch_is_an_exact_ratio() {
|
||||
let (h, in_p, out_p, scale) = skip!(stretch_facts("316-layer-stretch50.aep"));
|
||||
assert_eq!((h.stretch, h.stretch_den), (1, 2));
|
||||
assert_eq!(h.stretch_ratio(), Some(rat(1, 2)));
|
||||
assert_eq!(h.stretch_percent(), 50.0);
|
||||
assert_eq!(h.out_relative, rat(128000, 25600));
|
||||
assert_eq!((in_p, out_p, scale), (Some(rat(0, 1)), Some(rat(5, 2)), 25600));
|
||||
// 317: 150 %: 3/2, ends at 7.5 s, the property time scale is 25600 x 3/2.
|
||||
let (h, in_p, out_p, scale) = skip!(stretch_facts("317-layer-stretch150.aep"));
|
||||
assert_eq!((h.stretch, h.stretch_den), (3, 2));
|
||||
assert_eq!(h.stretch_ratio(), Some(rat(3, 2)));
|
||||
assert_eq!(h.stretch_percent(), 150.0);
|
||||
assert_eq!((in_p, out_p, scale), (Some(rat(0, 1)), Some(rat(15, 2)), 38400));
|
||||
// 018: 200 %: 2/1, ends at 10 s, time scale 51200. The base is 1/1.
|
||||
let (h, _, out_p, scale) = skip!(stretch_facts("018-layer-stretch200.aep"));
|
||||
assert_eq!((h.stretch, h.stretch_den, h.stretch_ratio()), (2, 1, Some(rat(2, 1))));
|
||||
assert_eq!((out_p, scale), (Some(rat(10, 1)), 51200));
|
||||
let (h, _, out_p, scale) = skip!(stretch_facts("000-base.aep"));
|
||||
assert_eq!((h.stretch, h.stretch_den, h.stretch_percent()), (1, 1, 100.0));
|
||||
assert_eq!((h.stretch, h.stretch_den, h.stretch_ratio()), (1, 1, Some(rat(1, 1))));
|
||||
assert_eq!((out_p, scale), (Some(rat(5, 1)), 25600));
|
||||
}
|
||||
|
||||
@@ -269,9 +264,9 @@ fn fractional_times_are_exact_multiples_of_one_over_25600() {
|
||||
assert_eq!(h.in_relative, rat(8532, 25600));
|
||||
assert_eq!(h.out_relative, rat(136532, 25600));
|
||||
assert_eq!(h.start, rat(0, 25600));
|
||||
// 0.33328125 s and 5.33328125 s.
|
||||
assert_eq!(h.in_point(), Some(rat(2133, 6400)));
|
||||
assert_eq!(h.in_point_seconds(), Some(0.33328125));
|
||||
assert_eq!(h.out_point_seconds(), Some(5.33328125));
|
||||
assert_eq!(h.out_point(), Some(rat(34133, 6400)));
|
||||
assert_eq!(h.stretch_ratio(), Some(rat(1, 1)));
|
||||
}
|
||||
|
||||
@@ -364,18 +359,17 @@ fn pixel_aspect_shutter_phase_and_display_start() {
|
||||
// 325: pixelAspect 0.9091 reads back 0.90909090909091 = the exact ratio 10/11.
|
||||
let h = skip!(comp_header("325-comp-pixelaspect-0.9091.aep"));
|
||||
assert_eq!(h.pixel_aspect.reduced(), Some(rat(10, 11)));
|
||||
assert!(near(h.pixel_aspect_ratio().unwrap(), 0.90909090909091, 1e-12));
|
||||
// 326: shutterPhase -90, the shutter angle stays 180.
|
||||
let h = skip!(comp_header("326-comp-shutterphase-m90.aep"));
|
||||
assert_eq!((h.shutter_phase, h.shutter_angle), (Some(-90), 180));
|
||||
// 327: displayStartFrame 100 at 25 fps = 4 s = 102400/25600; the shutter phase is back to 0.
|
||||
let h = skip!(comp_header("327-comp-displaystartframe-100.aep"));
|
||||
assert_eq!(h.display_start, Some(rat(102400, 25600)));
|
||||
assert_eq!((h.display_start_seconds(), h.display_start_frame(), h.shutter_phase), (Some(4.0), Some(100), Some(0)));
|
||||
assert_eq!(h.shutter_phase, Some(0));
|
||||
// The base: no phase, start 0.
|
||||
let h = skip!(comp_header("000-base.aep"));
|
||||
assert_eq!((h.shutter_phase, h.display_start_seconds(), h.display_start_frame()), (Some(0), Some(0.0), Some(0)));
|
||||
assert_eq!(h.pixel_aspect_ratio(), Some(1.0));
|
||||
assert_eq!((h.shutter_phase, h.display_start.and_then(|r| r.reduced())), (Some(0), Some(rat(0, 1))));
|
||||
assert_eq!(h.pixel_aspect.reduced(), Some(rat(1, 1)));
|
||||
// A short chunk still decodes the old fields.
|
||||
let bytes = [0u8; 176];
|
||||
let h = craft_aep::CompHeader::parse(&bytes).unwrap();
|
||||
|
||||
@@ -148,19 +148,21 @@ fn a_mask_box_is_normalised_to_the_layer_size() {
|
||||
for (got, want) in m.shapes[0].bbox.iter().zip(want_box) {
|
||||
assert!(near(f64::from(*got), want, 1e-6), "{:?}", m.shapes[0].bbox);
|
||||
}
|
||||
let shapes = project.mask_vertices_px(&layer, m).unwrap();
|
||||
assert_eq!(shapes.len(), 1);
|
||||
let got: Vec<[f64; 2]> = shapes[0].iter().map(|v| v.position).collect();
|
||||
// The box scales by the size of the layer's source (the importer takes that size from the
|
||||
// source item it matched, the same item `layer_source_size` reads).
|
||||
let (w, h) = project.layer_source_size(&layer).unwrap();
|
||||
assert_eq!(m.shapes.len(), 1);
|
||||
let got: Vec<[f64; 2]> = m.shapes[0].vertices((f64::from(w), f64::from(h))).iter().map(|v| v.position).collect();
|
||||
for (g, w) in got.iter().zip([[20.0, 30.0], [80.0, 30.0], [80.0, 70.0], [20.0, 70.0]]) {
|
||||
assert_near(g, &w, 1e-3);
|
||||
}
|
||||
assert_eq!(got.len(), 4);
|
||||
// A layer without a sized source has no pixel vertices.
|
||||
// A layer without a sized source has no size to scale by.
|
||||
let bytes = fixture!("333-shape-group-two-rects.aep");
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
let shape = layer_named(&first_comp(&project), "Shape");
|
||||
assert!(project.mask_vertices_px(&shape, m).is_none());
|
||||
assert!(project.layer_source_size(&shape).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -178,12 +180,14 @@ fn one_mkif_per_mask_and_the_mode_none() {
|
||||
assert_eq!((m.mode, m.inverted, m.counter), (Some(MaskMode::Add), Some(false), Some(counter)));
|
||||
assert_eq!(m.shapes.len(), 1);
|
||||
}
|
||||
let v1 = project.mask_vertices_px(&layer, &masks[0]).unwrap();
|
||||
let (w, h) = project.layer_source_size(&layer).unwrap();
|
||||
let px = |m: &craft_aep::Mask| m.shapes.iter().map(|s| s.vertices((f64::from(w), f64::from(h)))).collect::<Vec<_>>();
|
||||
let v1 = px(&masks[0]);
|
||||
for (v, w) in v1[0].iter().zip(ellipse_vertices()) {
|
||||
assert_near(&v.position, &w, 1e-2);
|
||||
}
|
||||
// Mask 2 has its own box (500..900 x 350..650) normalised to the layer.
|
||||
let v2 = project.mask_vertices_px(&layer, &masks[1]).unwrap();
|
||||
let v2 = px(&masks[1]);
|
||||
for (v, w) in v2[0].iter().zip([[500.0, 350.0], [900.0, 350.0], [900.0, 650.0], [500.0, 650.0]]) {
|
||||
assert_near(&v.position, &w, 1e-2);
|
||||
}
|
||||
@@ -384,81 +388,16 @@ fn the_last_style_run_absorbs_the_final_carriage_return() {
|
||||
|
||||
// ------------------------------------------------------------------ camera and light (341-345)
|
||||
|
||||
/// Camera zoom keeps the auto position.
|
||||
#[test]
|
||||
fn camera_zoom_and_depth_of_field() {
|
||||
let camera = |name: &str| {
|
||||
Some({
|
||||
let bytes = crate::common::try_fixture(name)?;
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
let layer = layer_of_type(&first_comp(&project), LayerType::Camera);
|
||||
(
|
||||
layer.camera_zoom(),
|
||||
layer.camera_depth_of_field(),
|
||||
layer.camera_focus_distance(),
|
||||
layer.camera_aperture(),
|
||||
layer.camera_position().map(|p| (p.stored, p.auto_state)),
|
||||
)
|
||||
})
|
||||
};
|
||||
// 341: Zoom 2000 is stored; the auto Position keeps [0, 0, -2666.67].
|
||||
let (zoom, dof, focus, aperture, pos) = skip!(camera("341-camera-zoom-2000.aep"));
|
||||
assert_eq!(zoom.map(|z| (z.value, z.source)), Some((2000.0, ValueSource::Stored)));
|
||||
assert_eq!((dof.map(|d| (d.value, d.source)), focus, aperture), (Some((false, ValueSource::Default)), None, None));
|
||||
let (stored, auto) = pos.unwrap();
|
||||
assert!(auto && near(stored[2], -2666.67, 0.01) && stored[0] == 0.0, "{stored:?}");
|
||||
// 342: DOF on, Focus Distance 800, Aperture 30; Zoom stays the default.
|
||||
let (zoom, dof, focus, aperture, _) = skip!(camera("342-camera-dof-800-30.aep"));
|
||||
assert_eq!(zoom.map(|z| (z.source, near(z.value, 2666.67, 0.01))), Some((ValueSource::Default, true)));
|
||||
assert_eq!(dof.map(|d| (d.value, d.source)), Some((true, ValueSource::Stored)));
|
||||
assert_eq!(focus.map(|f| (f.value, f.source)), Some((800.0, ValueSource::Stored)));
|
||||
assert_eq!(aperture.map(|f| (f.value, f.source)), Some((30.0, ValueSource::Stored)));
|
||||
// The default camera (256) has nothing but the default zoom.
|
||||
let (zoom, dof, ..) = skip!(camera("256-camera-two-node-default.aep"));
|
||||
assert_eq!((zoom.unwrap().source, dof.unwrap().value), (ValueSource::Default, false));
|
||||
assert_eq!(zoom.unwrap().value, craft_aep::DEFAULT_CAMERA_ZOOM);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn light_colour_cone_angle_and_feather() {
|
||||
let light = |name: &str| {
|
||||
Some({
|
||||
let bytes = crate::common::try_fixture(name)?;
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
let layer = layer_of_type(&first_comp(&project), LayerType::Light);
|
||||
assert_eq!(layer.header().unwrap().light_type(), Some(craft_aep::LightType::Spot), "{name}");
|
||||
let t = |x: Option<craft_aep::Tracked<f64>>| x.map(|x| (x.value, x.source));
|
||||
(layer.light_color().map(|c| (c.value, c.source)), t(layer.light_cone_angle()), t(layer.light_cone_feather()))
|
||||
})
|
||||
};
|
||||
// The default spot light: white, 90, 50, all absent.
|
||||
let (colour, angle, feather) = skip!(light("254-light-spot-intensity-50.aep"));
|
||||
assert_eq!(
|
||||
(colour, angle, feather),
|
||||
(Some(([1.0; 4], ValueSource::Default)), Some((90.0, ValueSource::Default)), Some((50.0, ValueSource::Default)))
|
||||
);
|
||||
// 343: colour [1,0,0,1] = ARGB doubles [255,255,0,0].
|
||||
let (colour, angle, feather) = skip!(light("343-light-spot-color-red.aep"));
|
||||
assert_eq!(colour, Some(([1.0, 1.0, 0.0, 0.0], ValueSource::Stored)));
|
||||
assert_eq!((angle, feather), (Some((90.0, ValueSource::Default)), Some((50.0, ValueSource::Default))));
|
||||
// 344: Cone Angle 60. 345: Cone Feather 30.
|
||||
let (colour, angle, feather) = skip!(light("344-light-spot-coneangle-60.aep"));
|
||||
assert_eq!((colour.unwrap().1, angle, feather), (ValueSource::Default, Some((60.0, ValueSource::Stored)), Some((50.0, ValueSource::Default))));
|
||||
let (_, angle, feather) = skip!(light("345-light-spot-conefeather-30.aep"));
|
||||
assert_eq!((angle, feather), (Some((90.0, ValueSource::Default)), Some((30.0, ValueSource::Stored))));
|
||||
// A point light (252) stores Cone Angle 90 / Feather 50 in their default state.
|
||||
let bytes = fixture!("252-light-point.aep");
|
||||
fn camera_zoom_keeps_the_auto_position() {
|
||||
// 341: Zoom 2000 is stored (checked on import); the auto Position keeps [0, 0, -2666.67].
|
||||
let bytes = fixture!("341-camera-zoom-2000.aep");
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
let layer = layer_of_type(&first_comp(&project), LayerType::Light);
|
||||
assert_eq!(layer.light_cone_angle().map(|a| (a.value, a.source)), Some((90.0, ValueSource::Stored)));
|
||||
// Layers without light options have no light accessors.
|
||||
let bytes = fixture!("000-base.aep");
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
let solid = base_layer(&first_comp(&project));
|
||||
assert!(solid.light_color().is_none() && solid.camera_zoom().is_none() && solid.camera_depth_of_field().is_none());
|
||||
let layer = layer_of_type(&first_comp(&project), LayerType::Camera);
|
||||
let pos = layer.camera_position().unwrap();
|
||||
assert!(pos.auto_state && near(pos.stored[2], -2666.67, 0.01) && pos.stored[0] == 0.0, "{:?}", pos.stored);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ marker data (347-349, 249)
|
||||
@@ -508,7 +447,7 @@ fn audio_footage_item() {
|
||||
assert_eq!((f.width, f.height, f.is_solid), (0, 0, false));
|
||||
assert_eq!(f.duration, Some(rat(48000, 48000)));
|
||||
assert_eq!((f.audio_sample_rate, f.file_size, f.bits_per_pixel, f.native_frame_rate), (Some(48000.0), 96044, 0, 0));
|
||||
assert!(!f.is_sequence && f.sequence_frame_count().is_none());
|
||||
assert!(!f.is_sequence && item.sequence_info().is_none());
|
||||
// The combined file stores no display name for the item (the single-case file had
|
||||
// "test-tone.wav"); the path names the file.
|
||||
assert_eq!(item.label(), Some(7));
|
||||
@@ -532,7 +471,7 @@ fn image_sequence_is_one_item() {
|
||||
assert_eq!(f.duration, Some(rat(3, 30)), "{name}");
|
||||
assert_eq!((f.native_frame_rate, f.bits_per_pixel, f.file_size, f.audio_sample_rate), (30, 24, 504, None), "{name}");
|
||||
assert!(f.is_sequence, "{name}");
|
||||
assert_eq!(f.sequence_frame_count(), Some(3), "{name}");
|
||||
assert_eq!(item.sequence_info().map(|s| s.frame_count()), Some(3), "{name}");
|
||||
// The first file name sits in opti; the alias points at the sequence folder.
|
||||
assert_eq!(f.name.as_deref(), Some("seq_001.png"), "{name}");
|
||||
assert!(f.path.as_deref().is_some_and(|p| p.ends_with("test-seq")), "{name}");
|
||||
|
||||
@@ -38,7 +38,7 @@ fn a_composition_counts_the_compositions_it_shows() {
|
||||
let id = |n: &str| items.iter().find(|i| i.display_name().as_deref() == Some(n)).and_then(|i| i.id()).expect(n);
|
||||
let c = items.iter().find(|i| i.display_name().as_deref() == Some("C")).expect("C");
|
||||
// The list holds each used composition once (in no particular order).
|
||||
let mut used = c.used_item_ids();
|
||||
let mut used = crate::common::used_item_ids(c);
|
||||
used.sort();
|
||||
let mut want = vec![id("P30"), id("Q")];
|
||||
want.sort();
|
||||
@@ -60,8 +60,8 @@ fn an_image_sequence_knows_its_numbering() {
|
||||
let item = project.all_items().into_iter().find(|i| i.footage().is_some_and(|f| !f.is_solid)).expect("sequence");
|
||||
let f = item.footage().expect("footage");
|
||||
assert_eq!((f.file_type.as_str(), f.width, f.height, f.native_frame_rate, f.is_sequence), ("oEXR", 64, 48, 30, true));
|
||||
assert_eq!(f.sequence_frame_count(), Some(3));
|
||||
let s = item.sequence_info().expect("numbering");
|
||||
assert_eq!(s.frame_count(), 3);
|
||||
assert_eq!((s.prefix.as_str(), s.suffix.as_str(), s.start, s.end, s.digits), ("shot_", ".exr", 101, 103, 4));
|
||||
assert_eq!(s.file_name(101), "shot_0101.exr");
|
||||
// A still has no numbering (000: the solid; the base has no other footage).
|
||||
|
||||
@@ -55,7 +55,6 @@ fn assert_plain(h: &LayerHeader) {
|
||||
assert_eq!(h.label, 1);
|
||||
// No name was set, so the copy of the name at byte 64 is empty (the layer shows its source name).
|
||||
assert_eq!(h.name_copy, "");
|
||||
assert!(!h.name_is_user_set());
|
||||
assert!(!h.is_null_object());
|
||||
assert_eq!(h.parent_id, None);
|
||||
assert_eq!(h.source_id, Some(0x1a));
|
||||
@@ -67,16 +66,13 @@ fn base_layer_header() {
|
||||
assert_plain(&h);
|
||||
assert_eq!(h.quality, Quality::Best);
|
||||
assert_eq!(h.blend_mode, BlendMode::Normal);
|
||||
assert_eq!(h.stretch, 1);
|
||||
assert_eq!(h.stretch_percent(), 100.0);
|
||||
assert_eq!((h.stretch, h.stretch_den), (1, 1));
|
||||
assert_eq!(switches(&h), NONE);
|
||||
// start 0 s, in 0 s, out 5 s; denominator = the composition timescale.
|
||||
assert_eq!(h.start, Rational { num: 0, den: 25600 });
|
||||
assert_eq!(h.in_relative, Rational { num: 0, den: 25600 });
|
||||
assert_eq!(h.out_relative, Rational { num: 128_000, den: 25600 });
|
||||
assert_eq!(h.start_seconds(), Some(0.0));
|
||||
assert_eq!(h.in_point_seconds(), Some(0.0));
|
||||
assert_eq!(h.out_point_seconds(), Some(5.0));
|
||||
assert_eq!(h.in_point(), Rational::new(0, 1));
|
||||
assert_eq!(h.out_point(), Rational::new(5, 1));
|
||||
}
|
||||
|
||||
@@ -98,7 +94,7 @@ fn one_switch_per_case() {
|
||||
assert_eq!(switches(&h), expected, "{name}");
|
||||
assert_plain(&h);
|
||||
assert_eq!((h.quality, h.blend_mode, h.stretch), (Quality::Best, BlendMode::Normal, 1), "{name}");
|
||||
assert_eq!(h.out_point_seconds(), Some(5.0), "{name}");
|
||||
assert_eq!(h.out_point(), Rational::new(5, 1), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +102,6 @@ fn one_switch_per_case() {
|
||||
fn name_blend_mode_and_quality() {
|
||||
let h = skip!(l_header("010-layer-name.aep"));
|
||||
assert_eq!(h.name_copy, "Renamed");
|
||||
assert!(h.name_is_user_set());
|
||||
let bytes = fixture!("010-layer-name.aep");
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
@@ -114,7 +109,6 @@ fn name_blend_mode_and_quality() {
|
||||
// The layer name is the user's; the source footage keeps its own name.
|
||||
assert_eq!(layer.name().as_deref(), Some("Renamed"));
|
||||
assert_eq!(project.layer_source(&layer).and_then(|i| i.display_name()).as_deref(), Some("L"));
|
||||
assert_eq!(project.layer_display_name(&layer).as_deref(), Some("Renamed"));
|
||||
|
||||
assert_eq!(skip!(l_header("016-layer-blend-multiply.aep")).blend_mode, BlendMode::Multiply);
|
||||
assert_eq!(skip!(l_header("023-layer-quality-draft.aep")).quality, Quality::Draft);
|
||||
@@ -127,7 +121,7 @@ fn display_name_falls_back_to_the_source_name() {
|
||||
let project = Project::new(&file).unwrap();
|
||||
let layer = base_layer(&first_comp(&project));
|
||||
assert_eq!(layer.name(), None);
|
||||
assert_eq!(project.layer_display_name(&layer).as_deref(), Some("L"));
|
||||
assert_eq!(project.layer_source(&layer).and_then(|i| i.display_name()).as_deref(), Some("L"));
|
||||
assert_eq!(project.layer_source_size(&layer), Some((1920, 1080)));
|
||||
let source = project.layer_source(&layer).unwrap();
|
||||
assert_eq!(source.id(), Some(0x1a));
|
||||
@@ -139,26 +133,23 @@ fn time_stretch_and_the_in_out_start_convention() {
|
||||
// 018: stretch 100 -> 200: the stored out point stays 5 s, the layer ends at 10 s.
|
||||
let h = skip!(l_header("018-layer-stretch200.aep"));
|
||||
assert_eq!(h.stretch, 2);
|
||||
assert_eq!(h.stretch_percent(), 200.0);
|
||||
assert_eq!(h.stretch_ratio(), Rational::new(2, 1));
|
||||
assert_eq!(h.out_relative, Rational { num: 128_000, den: 25600 });
|
||||
assert_eq!(h.out_point_seconds(), Some(10.0));
|
||||
assert_eq!(h.in_point_seconds(), Some(0.0));
|
||||
assert_eq!(h.out_point(), Rational::new(10, 1));
|
||||
assert_eq!(h.in_point(), Rational::new(0, 1));
|
||||
|
||||
// 019: inPoint 0 -> 1; AE moved the out point to 6 s and left the start at 0.
|
||||
let h = skip!(l_header("019-layer-inpoint1.aep"));
|
||||
assert_eq!(h.start_seconds(), Some(0.0));
|
||||
assert_eq!(h.start.num, 0);
|
||||
assert_eq!(h.in_relative, Rational { num: 25600, den: 25600 });
|
||||
assert_eq!(h.in_point(), Rational::new(1, 1));
|
||||
assert_eq!(h.out_point_seconds(), Some(6.0));
|
||||
assert_eq!(h.out_point(), Rational::new(6, 1));
|
||||
|
||||
// 021: startTime 0 -> 0.5: in 0.5 s and out 5.5 s although the stored in point stays 0.
|
||||
let h = skip!(l_header("021-layer-starttime0.5.aep"));
|
||||
assert_eq!(h.start, Rational { num: 12800, den: 25600 });
|
||||
assert_eq!(h.start_seconds(), Some(0.5));
|
||||
assert_eq!(h.in_relative.num, 0);
|
||||
assert_eq!(h.in_point(), Rational::new(1, 2));
|
||||
assert_eq!(h.in_point_seconds(), Some(0.5));
|
||||
assert_eq!(h.out_point_seconds(), Some(5.5));
|
||||
assert_eq!(h.out_point(), Rational::new(11, 2));
|
||||
}
|
||||
|
||||
@@ -175,7 +166,6 @@ fn parent_link_and_null_object() {
|
||||
let nh = null.header().unwrap();
|
||||
assert_eq!(null.name().as_deref(), Some("Null"));
|
||||
assert!(nh.is_null_object());
|
||||
assert!(nh.name_is_user_set());
|
||||
assert_eq!(nh.layer_type, LayerType::Av);
|
||||
assert_eq!(nh.parent_id, None);
|
||||
assert_eq!(project.layer_source(null).and_then(|i| i.display_name()).as_deref(), Some("Null 1"));
|
||||
@@ -225,17 +215,14 @@ fn base_comp_header() {
|
||||
assert_eq!((h.width, h.height), (1920, 1080));
|
||||
assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1024, 25600));
|
||||
assert_eq!(h.frame_rate(), Some(25.0));
|
||||
assert_eq!(h.frame_rate_from_fixed(), 25.0);
|
||||
assert_eq!(h.frame_rate_fixed, 25 << 16);
|
||||
assert_eq!(h.duration_units, 128_000);
|
||||
assert_eq!(h.duration_seconds(), Some(5.0));
|
||||
assert_eq!(h.background, [0, 0, 0]);
|
||||
assert_eq!(h.background_rgb(), [0.0, 0.0, 0.0]);
|
||||
assert_eq!(h.shutter_angle, 180);
|
||||
assert_eq!(h.work_area_start_seconds(), Some(0.0));
|
||||
assert_eq!(h.work_area_start_units, 0);
|
||||
assert_eq!(h.work_area_end_units, None);
|
||||
assert_eq!(h.work_area_end_seconds(), Some(5.0));
|
||||
assert_eq!(h.pixel_aspect, Rational { num: 1, den: 1 });
|
||||
assert_eq!(h.pixel_aspect_ratio(), Some(1.0));
|
||||
assert!(!h.motion_blur);
|
||||
// The manifest calls the classic renderer `ADBE Advanced 3d`; the file stores `ADBE Escher`
|
||||
// and the display name `Classic 3D`.
|
||||
@@ -255,7 +242,7 @@ fn one_comp_change_per_case() {
|
||||
let (h, _, _) = skip!(comp_header("031-comp-fps30.aep"));
|
||||
assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1024, 30720));
|
||||
assert_eq!(h.frame_rate(), Some(30.0));
|
||||
assert_eq!(h.frame_rate_from_fixed(), 30.0);
|
||||
assert_eq!(h.frame_rate_fixed, 30 << 16);
|
||||
assert_eq!(h.duration_units, 153_600);
|
||||
assert_eq!(h.duration_seconds(), Some(5.0));
|
||||
|
||||
@@ -263,12 +250,11 @@ fn one_comp_change_per_case() {
|
||||
let (h, _, _) = skip!(comp_header("032-comp-duration10.aep"));
|
||||
assert_eq!(h.duration_units, 256_000);
|
||||
assert_eq!(h.duration_seconds(), Some(10.0));
|
||||
assert_eq!(h.work_area_end_seconds(), Some(10.0));
|
||||
assert_eq!(h.work_area_end_units, None);
|
||||
|
||||
// 033: bgColor [0,0,0] -> [0,1,0].
|
||||
let (h, _, _) = skip!(comp_header("033-comp-bg-green.aep"));
|
||||
assert_eq!(h.background, [0, 255, 0]);
|
||||
assert_eq!(h.background_rgb(), [0.0, 1.0, 0.0]);
|
||||
|
||||
// 034: shutterAngle 180 -> 90.
|
||||
let (h, _, _) = skip!(comp_header("034-comp-shutter90.aep"));
|
||||
@@ -279,8 +265,6 @@ fn one_comp_change_per_case() {
|
||||
let (h, _, _) = skip!(comp_header("035-comp-workarea-1-2.aep"));
|
||||
assert_eq!(h.work_area_start_units, 25600);
|
||||
assert_eq!(h.work_area_end_units, Some(76_800));
|
||||
assert_eq!(h.work_area_start_seconds(), Some(1.0));
|
||||
assert_eq!(h.work_area_end_seconds(), Some(3.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -290,7 +274,6 @@ fn one_comp_change_per_case_aspect_blur_renderer() {
|
||||
// 037: pixelAspect 1 -> 2.
|
||||
let (h, _, _) = skip!(comp_header("037-comp-pixelaspect2.aep"));
|
||||
assert_eq!(h.pixel_aspect, Rational { num: 2, den: 1 });
|
||||
assert_eq!(h.pixel_aspect_ratio(), Some(2.0));
|
||||
|
||||
// 038: comp motion blur on.
|
||||
let (h, _, _) = skip!(comp_header("038-comp-motionblur.aep"));
|
||||
@@ -328,17 +311,16 @@ fn precomp_item_ids_and_labels() {
|
||||
assert_eq!(h.source_id, pre_item.id());
|
||||
assert_eq!(project.layer_source(layer).unwrap().id(), pre_item.id());
|
||||
assert_eq!(h.label, 15);
|
||||
assert!(!h.name_is_user_set());
|
||||
assert_eq!(layer.name(), None);
|
||||
assert_eq!(project.layer_display_name(layer).as_deref(), Some("Pre"));
|
||||
assert_eq!(project.layer_source(layer).and_then(|i| i.display_name()).as_deref(), Some("Pre"));
|
||||
assert_eq!(project.layer_source_size(layer), Some((1920, 1080)));
|
||||
// The precomp layer's start time has the denominator 1; the Pre comp is 1920x1080, 25 fps, 5 s.
|
||||
assert_eq!(h.start.den, 1);
|
||||
let ph = pre.header().unwrap();
|
||||
assert_eq!((ph.width, ph.height, ph.frame_rate(), ph.duration_seconds()), (1920, 1080, Some(25.0), Some(5.0)));
|
||||
// C records that it uses the comp Pre (chunk idpi, a little-endian u32).
|
||||
assert_eq!(c_item.used_item_ids(), [pre_item.id().unwrap()]);
|
||||
assert!(pre_item.used_item_ids().is_empty());
|
||||
assert_eq!(crate::common::used_item_ids(c_item), [pre_item.id().unwrap()]);
|
||||
assert!(crate::common::used_item_ids(pre_item).is_empty());
|
||||
assert_eq!(pre_item.label(), Some(15));
|
||||
// The layer inside Pre is a new layer made from the solid L (moving the layers into a precomp
|
||||
// creates new layers).
|
||||
@@ -347,7 +329,7 @@ fn precomp_item_ids_and_labels() {
|
||||
let ih = inner[0].header().unwrap();
|
||||
assert_ne!(ih.id, L);
|
||||
assert_eq!(ih.source_id, Some(0x1a));
|
||||
assert_eq!(project.layer_display_name(&inner[0]).as_deref(), Some("L"));
|
||||
assert_eq!(project.layer_source(&inner[0]).and_then(|i| i.display_name()).as_deref(), Some("L"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -356,9 +338,8 @@ fn a_negative_stretch_is_a_reversed_layer() {
|
||||
// point 0 and out point 5 s relative, so the layer runs from 5 s back to 0 s; 983: -50 % = -1/2.
|
||||
let h = skip!(l_header("981-layer-stretch-minus100-start5.aep"));
|
||||
assert_eq!((h.stretch, h.stretch_den), (-1, 1));
|
||||
assert_eq!(h.stretch_percent(), -100.0);
|
||||
assert_eq!((h.in_point_seconds(), h.out_point_seconds()), (Some(5.0), Some(0.0)));
|
||||
assert_eq!((h.in_point(), h.out_point()), (Rational::new(5, 1), Rational::new(0, 1)));
|
||||
let h = skip!(l_header("983-layer-stretch-minus50-opacity-keys.aep"));
|
||||
assert_eq!((h.stretch, h.stretch_den), (-1, 2));
|
||||
assert_eq!((h.in_point_seconds(), h.out_point_seconds()), (Some(5.0), Some(2.5)));
|
||||
assert_eq!((h.in_point(), h.out_point()), (Rational::new(5, 1), Rational::new(5, 2)));
|
||||
}
|
||||
|
||||
@@ -379,7 +379,8 @@ pub fn layer_named<'f, 'a>(comp: &Comp<'f, 'a>, name: &str) -> Layer<'f, 'a> {
|
||||
|
||||
/// The layer that shows this name: its own name, or its source's while it has none.
|
||||
pub fn layer_shown_as<'f, 'a>(project: &Project<'f, 'a>, comp: &Comp<'f, 'a>, name: &str) -> Layer<'f, 'a> {
|
||||
comp.layers().find(|l| project.layer_display_name(l).as_deref() == Some(name)).unwrap_or_else(|| panic!("no layer shown as {name}"))
|
||||
let shown = |l: &Layer<'f, 'a>| l.name().or_else(|| project.layer_source(l)?.display_name());
|
||||
comp.layers().find(|l| shown(l).as_deref() == Some(name)).unwrap_or_else(|| panic!("no layer shown as {name}"))
|
||||
}
|
||||
|
||||
/// The first layer of this type in a composition.
|
||||
|
||||
@@ -19,6 +19,9 @@ fn prop_of<R>(name: &str, path: &[&str], f: impl FnOnce(Property<'_, '_>) -> R)
|
||||
const POSITION: &[&str] = &["ADBE Transform Group", "ADBE Position"];
|
||||
const OPACITY: &[&str] = &["ADBE Transform Group", "ADBE Opacity"];
|
||||
|
||||
/// The ease of a linear key: all four slots 0.
|
||||
const NO_EASE: KeyEase = KeyEase { in_speed: 0.0, in_influence: 0.0, out_speed: 0.0, out_influence: 0.0 };
|
||||
|
||||
fn ease(in_speed: f64, in_influence: f64, out_speed: f64, out_influence: f64) -> KeyEase {
|
||||
KeyEase { in_speed, in_influence, out_speed, out_influence }
|
||||
}
|
||||
@@ -33,7 +36,7 @@ fn in_and_out_interpolation_are_separate_bytes() {
|
||||
assert_eq!((keys[0].interpolation_in(), keys[0].interpolation_out()), (Some(Interpolation::Linear), Some(Interpolation::Hold)));
|
||||
assert_eq!((keys[1].interpolation_in(), keys[1].interpolation_out()), (Some(Interpolation::Linear), Some(Interpolation::Linear)));
|
||||
assert_eq!(keys.iter().map(|k| k.seconds(25600).unwrap()).collect::<Vec<_>>(), [0.0, 1.0]);
|
||||
assert_eq!(keys[1].spatial_value(), Some([100.0, 0.0, 0.0]));
|
||||
assert_eq!(keys[1].value(), Some(vec![100.0, 0.0, 0.0]));
|
||||
}));
|
||||
// 203: key 1 inType HOLD, outType LINEAR (bytes 03 01): byte 4 is the incoming side.
|
||||
skip!(prop_of("203-key-in-hold-out-linear.aep", POSITION, |p| {
|
||||
@@ -76,7 +79,7 @@ fn opacity_ease_speed_is_in_stored_units_per_second() {
|
||||
assert_near(&[e0.in_speed, e0.in_influence, e0.out_speed, e0.out_influence], &[0.05, 0.2, 0.2, 0.8], 1e-12);
|
||||
let e1 = keys[1].ease().unwrap();
|
||||
assert_near(&[e1.in_speed, e1.in_influence, e1.out_speed, e1.out_influence], &[0.3, 0.6, 0.09, 0.4], 1e-12);
|
||||
assert_eq!(keys[0].plain_ease_slots().unwrap().len(), 4);
|
||||
assert_eq!(keys[0].ease_components(), Some(vec![e0]));
|
||||
for k in &keys {
|
||||
assert_eq!((k.interpolation_in(), k.interpolation_out()), (Some(Interpolation::Bezier), Some(Interpolation::Bezier)));
|
||||
}
|
||||
@@ -94,7 +97,7 @@ fn scale_keys_use_the_plain_template_with_three_components() {
|
||||
// A 128-byte record is not always spatial: the header's template flag decides.
|
||||
assert_eq!(k.kind(), KeyframeKind::Plain { components: 3 });
|
||||
assert_eq!(k.spatial(), None);
|
||||
assert_eq!(k.plain_ease_slots(), Some(vec![0.0; 12]));
|
||||
assert_eq!(k.ease_components(), Some(vec![NO_EASE; 3]));
|
||||
assert_eq!(k.ease(), None);
|
||||
assert_eq!((k.interpolation_in(), k.interpolation_out()), (Some(Interpolation::Linear), Some(Interpolation::Linear)));
|
||||
}
|
||||
@@ -125,7 +128,7 @@ fn two_dimensional_mask_feather_keys() {
|
||||
assert!(keys.iter().all(|k| k.raw().len() == 88 && k.kind() == KeyframeKind::Plain { components: 2 }));
|
||||
assert_eq!(keys.iter().map(|k| k.value().unwrap()).collect::<Vec<_>>(), [vec![0.0, 0.0], vec![40.0, 20.0]]);
|
||||
assert_eq!(keys.iter().map(|k| k.seconds(25600).unwrap()).collect::<Vec<_>>(), [0.0, 1.0]);
|
||||
assert_eq!(keys[0].plain_ease_slots(), Some(vec![0.0; 8]));
|
||||
assert_eq!(keys[0].ease_components(), Some(vec![NO_EASE; 2]));
|
||||
let mask = &layer.masks()[0];
|
||||
assert!(mask.feather.source == ValueSource::Keyframed && mask.feather.value == [0.0, 0.0]);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! colours, checked against the values the corpus was made with (its MANIFEST, batch 2).
|
||||
|
||||
use crate::container::helpers::{assert_near, base_layer, first_comp, layer_of_type, layer_shown_as, open};
|
||||
use craft_aep::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Project, Rational, ValueSource};
|
||||
use craft_aep::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Project};
|
||||
|
||||
fn l_header(name: &str) -> Option<LayerHeader> {
|
||||
Some({
|
||||
@@ -80,22 +80,24 @@ fn ntsc_frame_rates_are_exact_fractions() {
|
||||
// 229: frameRate 29.97 -> read back 29.9700012207031; the duration snaps from 5 s to 150 frames (5.005005 s).
|
||||
let h = skip!(comp_header("229-comp-fps-29.97.aep"));
|
||||
assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (800, 23976));
|
||||
assert_eq!(h.frame_rate_exact(), Some(Rational { num: 2997, den: 100 }));
|
||||
assert!((h.frame_rate().unwrap() - 29.97).abs() < 1e-12);
|
||||
assert!((h.frame_rate_from_fixed() - 29.970_001_220_703_1).abs() < 1e-7);
|
||||
assert_eq!((h.duration_units, h.duration_frames()), (120_000, Some(150)));
|
||||
// The 16.16 rate holds the float32 29.97 (29.9700012207031).
|
||||
assert_eq!(h.frame_rate_fixed, 1_964_114);
|
||||
// 150 frames of 800 units.
|
||||
assert_eq!(h.duration_units, 120_000);
|
||||
assert!((h.duration_seconds().unwrap() - 5.005_005_005_005_01).abs() < 1e-9);
|
||||
// 230: frameRate 23.976 -> read back 23.9759979248047; 120 frames.
|
||||
let h = skip!(comp_header("230-comp-fps-23.976.aep"));
|
||||
assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1000, 23976));
|
||||
assert_eq!(h.frame_rate_exact(), Some(Rational { num: 2997, den: 125 }));
|
||||
assert!((h.frame_rate().unwrap() - 23.976).abs() < 1e-12);
|
||||
assert!((h.frame_rate_from_fixed() - 23.975_997_924_804_7).abs() < 1e-7);
|
||||
assert_eq!((h.duration_units, h.duration_frames()), (120_000, Some(120)));
|
||||
// The 16.16 rate holds the float32 23.976 (23.9759979248047).
|
||||
assert_eq!(h.frame_rate_fixed, 1_571_291);
|
||||
// 120 frames of 1000 units.
|
||||
assert_eq!(h.duration_units, 120_000);
|
||||
assert!((h.duration_seconds().unwrap() - 5.005_005_005_005_01).abs() < 1e-9);
|
||||
// 25 fps stays 25.
|
||||
let h = skip!(comp_header("000-base.aep"));
|
||||
assert_eq!((h.frame_rate_exact(), h.duration_frames()), (Some(Rational { num: 25, den: 1 }), Some(125)));
|
||||
assert_eq!((h.time_base.timescale, h.time_base.frame_duration, h.duration_units), (25600, 1024, 128_000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -128,7 +130,6 @@ fn background_is_8_bits_per_channel() {
|
||||
// 231: bgColor [0.5, 0.5, 0.5] is stored as 128 and reads back as 128/255 = 0.50196.
|
||||
let h = skip!(comp_header("231-comp-bg-grey-0.5.aep"));
|
||||
assert_eq!(h.background, [128, 128, 128]);
|
||||
assert_near(&h.background_rgb(), &[0.501_960_784_313_73; 3], 1e-12);
|
||||
// The frame rate stays 25 even though an earlier edit of the AE session left other numbers in the cache-like fields.
|
||||
assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1024, 25600));
|
||||
}
|
||||
@@ -143,12 +144,10 @@ fn cameras_auto_orient_and_position() {
|
||||
let camera = layer_of_type(&comp, LayerType::Camera);
|
||||
let h = camera.header().unwrap();
|
||||
assert_eq!((h.flag_bytes[1], h.is_auto_oriented(), h.is_three_d(), h.source_id, h.light_type()), (0x44, true, true, None, None));
|
||||
// The default Position is an "auto state" record that stores [0, 0, -2666.67]; the two-node camera reports the comp centre.
|
||||
// The default Position is an "auto state" record that stores [0, 0, -2666.67].
|
||||
let pos = camera.camera_position().unwrap();
|
||||
assert!(pos.auto_state);
|
||||
assert_near(&pos.stored, &[0.0, 0.0, -2666.67], 0.01);
|
||||
assert_near(&pos.reported(1920, 1080, true), &[960.0, 540.0, -2666.67], 0.01);
|
||||
assert_eq!(camera.camera_point_of_interest(), Some([960.0, 540.0, 0.0]));
|
||||
|
||||
let bytes = fixture!("251-camera-one-node.aep");
|
||||
let file = open(&bytes);
|
||||
@@ -159,10 +158,9 @@ fn cameras_auto_orient_and_position() {
|
||||
// AE reports [0, 0, -2666.67] for the one-node camera: the stored value.
|
||||
let pos = camera.camera_position().unwrap();
|
||||
assert!(pos.auto_state);
|
||||
assert_near(&pos.reported(1920, 1080, h.is_auto_oriented()), &[0.0, 0.0, -2666.67], 0.01);
|
||||
assert_eq!(camera.camera_point_of_interest(), Some([960.0, 540.0, 0.0]));
|
||||
assert_near(&pos.stored, &[0.0, 0.0, -2666.67], 0.01);
|
||||
|
||||
// 253: Position set to [100, 200, -1000]: stored as given, no auto state; the point of interest stays [960, 540, 0].
|
||||
// 253: Position set to [100, 200, -1000]: stored as given, no auto state.
|
||||
let bytes = fixture!("253-camera-position-100-200-m1000.aep");
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
@@ -170,35 +168,28 @@ fn cameras_auto_orient_and_position() {
|
||||
let pos = camera.camera_position().unwrap();
|
||||
assert!(!pos.auto_state);
|
||||
assert_eq!(pos.stored, [100.0, 200.0, -1000.0]);
|
||||
assert_eq!(pos.reported(1920, 1080, true), [100.0, 200.0, -1000.0]);
|
||||
assert_eq!(camera.camera_point_of_interest(), Some([960.0, 540.0, 0.0]));
|
||||
// A camera has no light intensity.
|
||||
assert_eq!(camera.light_intensity(), None);
|
||||
}
|
||||
|
||||
/// Lights type and position.
|
||||
#[test]
|
||||
fn lights_type_and_intensity() {
|
||||
// 254: a spot light, Intensity 100 -> 50 (stored as 50.0 percent, absent at the default 100).
|
||||
fn lights_type_and_position() {
|
||||
// 254: a spot light (its Intensity 50 is checked on import).
|
||||
let bytes = fixture!("254-light-spot-intensity-50.aep");
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
let light = layer_of_type(&first_comp(&project), LayerType::Light);
|
||||
let h = light.header().unwrap();
|
||||
assert_eq!((h.light_type(), h.light_type_code, h.source_id, h.is_auto_oriented(), h.is_three_d()), (Some(LightType::Spot), 1, None, true, true));
|
||||
let intensity = light.light_intensity().unwrap();
|
||||
assert_eq!((intensity.value, intensity.source), (50.0, ValueSource::Stored));
|
||||
assert!(light.camera_position().unwrap().auto_state);
|
||||
assert_near(&light.camera_position().unwrap().stored, &[0.0, 0.0, -1333.33], 0.01);
|
||||
|
||||
// 252: a point light (light type 2) at the default intensity, which is not stored.
|
||||
// 252: a point light (light type 2).
|
||||
let bytes = fixture!("252-light-point.aep");
|
||||
let file = open(&bytes);
|
||||
let project = Project::new(&file).unwrap();
|
||||
let light = layer_of_type(&first_comp(&project), LayerType::Light);
|
||||
let h = light.header().unwrap();
|
||||
assert_eq!((h.light_type(), h.light_type_code), (Some(LightType::Point), 2));
|
||||
let intensity = light.light_intensity().unwrap();
|
||||
assert_eq!((intensity.value, intensity.source), (100.0, ValueSource::Default));
|
||||
// The solid under it is no light.
|
||||
let solid = base_layer(&first_comp(&project)).header().unwrap();
|
||||
assert_eq!((solid.light_type(), solid.light_type_code), (None, 0));
|
||||
@@ -220,6 +211,6 @@ fn solid_colour_is_four_float32_argb() {
|
||||
// L is still red.
|
||||
let red = project.layer_source(&base_layer(&comp)).unwrap().footage().unwrap();
|
||||
assert_eq!(red.solid_rgb(), Some([1.0, 0.0, 0.0]));
|
||||
// The new layer has no user-set name flag (it shows its source's name).
|
||||
assert!(!blue.header().unwrap().name_is_user_set());
|
||||
// The new layer has no name of its own (it shows its source's name).
|
||||
assert_eq!(blue.name(), None);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ fn layers(name: &str) -> Option<Vec<(String, bool, Option<TrackMatte>, u32)>> {
|
||||
comp.layers()
|
||||
.map(|l| {
|
||||
let h = l.header().expect("header");
|
||||
(project.layer_display_name(&l).unwrap_or_default(), h.is_video_enabled(), l.track_matte(), h.id)
|
||||
(l.name().or_else(|| project.layer_source(&l)?.display_name()).unwrap_or_default(), h.is_video_enabled(), l.track_matte(), h.id)
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
|
||||
@@ -154,16 +154,15 @@ use craft_aep::{AepFile, Body, Chunk, Comp, Layer, Project, PropGroup, PropNode,
|
||||
|
||||
/// Touches the property accessors; none may panic on a damaged property.
|
||||
fn exercise_property(p: &Property<'_, '_>) {
|
||||
let _ = (p.header(), p.dims(), p.is_animated(), p.static_value(), p.time_scale(), p.flags(), p.is_static_flag_set());
|
||||
let _ = (p.expression(), p.expression_flags().map(|f| (f.all_set(), f.disabled())), p.expression_enabled(), p.user_name(), p.first_key_value());
|
||||
let _ = p.color_argb();
|
||||
let _ = (p.header(), p.dims(), p.is_animated(), p.static_value(), p.time_scale(), p.flags());
|
||||
let _ = (p.expression(), p.expression_flags().map(|f| f.disabled()), p.expression_enabled(), p.user_name(), p.first_key_value());
|
||||
if let Some(keys) = p.keys() {
|
||||
let scale = p.time_scale().unwrap_or(0);
|
||||
for k in keys.keyframes() {
|
||||
let _ = (k.time_units(), k.spatial_value(), k.raw().len(), k.kind(), k.seconds(scale), k.seconds(0));
|
||||
let _ = (k.time_units(), k.raw().len(), k.kind(), k.seconds(scale), k.seconds(0));
|
||||
let _ = (k.interpolation_in(), k.interpolation_out(), k.flags(), k.label(), k.is_roving());
|
||||
let _ = (k.scalar_value(), k.ease(), k.spatial(), k.components(), k.value(), k.plain_ease_slots());
|
||||
let _ = (k.ease_components(), k.color_distance());
|
||||
let _ = (k.scalar_value(), k.ease(), k.spatial(), k.components(), k.value());
|
||||
let _ = k.ease_components();
|
||||
for component in [0, 1, 2, 3, 7, 255, usize::MAX] {
|
||||
let _ = k.ease_component(component);
|
||||
}
|
||||
@@ -193,10 +192,9 @@ fn exercise_group(g: &PropGroup<'_, '_>, depth: usize) {
|
||||
fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Layer<'_, '_>) {
|
||||
let _ = (layer.name(), layer.is_three_d(), layer.id(), layer.track_matte());
|
||||
if let Some(h) = layer.header() {
|
||||
let _ = (h.in_point(), h.out_point(), h.in_point_seconds(), h.out_point_seconds(), h.start_seconds(), h.stretch_percent());
|
||||
let _ = (h.stretch_ratio(), h.stretch_den);
|
||||
let _ = (h.in_point(), h.out_point(), h.stretch_ratio(), h.stretch_den);
|
||||
let _ = (h.is_shy(), h.is_locked(), h.is_solo(), h.is_three_d(), h.is_motion_blur(), h.is_adjustment(), h.is_guide());
|
||||
let _ = (h.is_null_object(), h.collapses_transformations(), h.name_is_user_set(), h.quality, h.blend_mode, h.layer_type);
|
||||
let _ = (h.is_null_object(), h.collapses_transformations(), h.quality, h.blend_mode, h.layer_type);
|
||||
let _ = (
|
||||
h.is_enabled(),
|
||||
h.is_video_enabled(),
|
||||
@@ -208,10 +206,8 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye
|
||||
);
|
||||
}
|
||||
let _ = (layer.is_enabled(), layer.is_audio_enabled(), layer.is_effects_enabled());
|
||||
let _ = (layer.camera_position().map(|p| p.reported(comp_size.0, comp_size.1, true)), layer.camera_point_of_interest(), layer.light_intensity());
|
||||
let _ = layer.camera_position().map(|p| (p.stored, p.auto_state));
|
||||
let _ = project.layer_anchor_px(layer);
|
||||
let _ = (layer.camera_zoom(), layer.camera_depth_of_field(), layer.camera_focus_distance(), layer.camera_aperture());
|
||||
let _ = (layer.light_color(), layer.light_cone_angle(), layer.light_cone_feather());
|
||||
let _ = layer
|
||||
.time_remap()
|
||||
.map(|t| (t.enabled, t.keys.iter().fold(0.0, |acc, k| acc + k.value_seconds + k.time_seconds.unwrap_or(0.0) + f64::from(k.time_units))));
|
||||
@@ -238,7 +234,8 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = (project.layer_source(layer).map(|i| i.id()), project.layer_display_name(layer), project.layer_source_size(layer));
|
||||
let _ =
|
||||
(project.layer_source(layer).map(|i| i.id()), project.layer_source(layer).and_then(|i| i.display_name()), project.layer_source_size(layer));
|
||||
if let Some(t) = layer.transform(comp_size.0, comp_size.1) {
|
||||
let _ = (t.anchor_px(f64::from(comp_size.0), f64::from(comp_size.1)), t.position.value, t.opacity_percent.value);
|
||||
}
|
||||
@@ -271,7 +268,6 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye
|
||||
}
|
||||
for m in layer.masks() {
|
||||
let _ = (m.name.clone(), m.mode, m.inverted, m.counter, m.feather, m.opacity_percent, m.expansion, m.path_animated, m.path_key_seconds.len());
|
||||
let _ = project.mask_vertices_px(layer, &m).map(|shapes| shapes.iter().map(Vec::len).sum::<usize>());
|
||||
for s in &m.shapes {
|
||||
let _ = (s.vertex_count(), s.vertices((1920.0, 1080.0)).len(), s.vertices((f64::NAN, f64::INFINITY)).len(), s.open);
|
||||
}
|
||||
@@ -302,10 +298,9 @@ fn exercise_comp(project: &Project<'_, '_>, comp: &Comp<'_, '_>) {
|
||||
let mut size = (1920, 1080);
|
||||
if let Some(h) = comp.header() {
|
||||
size = (u32::from(h.width), u32::from(h.height));
|
||||
let _ = (h.frame_rate(), h.frame_rate_from_fixed(), h.duration_seconds(), h.work_area_start_seconds(), h.work_area_end_seconds());
|
||||
let _ = (h.frame_rate_exact(), h.duration_frames());
|
||||
let _ = (h.background_rgb(), h.pixel_aspect_ratio(), h.pixel_aspect.reduced(), h.shutter_angle, h.motion_blur);
|
||||
let _ = (h.shutter_phase, h.display_start, h.display_start_seconds(), h.display_start_frame());
|
||||
let _ = (h.frame_rate(), h.frame_rate_fixed, h.duration_seconds(), h.work_area_start_units, h.work_area_end_units);
|
||||
let _ = (h.background, h.pixel_aspect.to_f64(), h.pixel_aspect.reduced(), h.shutter_angle, h.motion_blur);
|
||||
let _ = (h.shutter_phase, h.display_start.map(|r| r.to_f64()));
|
||||
}
|
||||
for layer in comp.layers() {
|
||||
exercise_layer(project, size, &layer);
|
||||
@@ -316,10 +311,8 @@ fn exercise_comp(project: &Project<'_, '_>, comp: &Comp<'_, '_>) {
|
||||
fn exercise(file: &AepFile<'_>) {
|
||||
let Some(project) = Project::new(file) else { return };
|
||||
for item in project.all_items() {
|
||||
let _ = (item.kind(), item.name(), item.id(), item.label(), item.used_item_ids(), item.footage(), item.display_name());
|
||||
let _ = item
|
||||
.footage()
|
||||
.map(|f| (f.duration, f.native_frame_rate, f.bits_per_pixel, f.file_size, f.audio_sample_rate, f.is_sequence, f.sequence_frame_count()));
|
||||
let _ = (item.kind(), item.name(), item.id(), item.label(), item.footage(), item.display_name());
|
||||
let _ = item.footage().map(|f| (f.duration, f.native_frame_rate, f.bits_per_pixel, f.file_size, f.audio_sample_rate, f.is_sequence));
|
||||
let _ = item.id().and_then(|id| project.item_by_id(id));
|
||||
if let Some(comp) = item.comp() {
|
||||
exercise_comp(&project, &comp);
|
||||
@@ -572,12 +565,12 @@ fn extreme_values_in_the_decoded_headers() {
|
||||
use craft_aep::{CompHeader, LayerHeader, Rational, ShapePath};
|
||||
// Every field at its maximum: no overflow, no division by zero, no panic.
|
||||
let h = LayerHeader::parse(&[0xff; 164]).unwrap();
|
||||
let _ = (h.in_point(), h.out_point(), h.in_point_seconds(), h.out_point_seconds(), h.start_seconds(), h.stretch_percent());
|
||||
let _ = (h.in_point(), h.out_point(), h.start.to_f64(), h.stretch_ratio());
|
||||
let mut zero_den = [0u8; 164];
|
||||
zero_den[8..12].copy_from_slice(&u32::MAX.to_be_bytes());
|
||||
zero_den[12..16].copy_from_slice(&i32::MIN.to_be_bytes());
|
||||
let h = LayerHeader::parse(&zero_den).unwrap();
|
||||
assert_eq!((h.in_point(), h.out_point(), h.start_seconds()), (None, None, None));
|
||||
assert_eq!((h.in_point(), h.out_point(), h.start.to_f64()), (None, None, None));
|
||||
let mut wide = [0u8; 164];
|
||||
wide[8..12].copy_from_slice(&u32::MAX.to_be_bytes());
|
||||
wide[12..16].copy_from_slice(&i32::MIN.to_be_bytes());
|
||||
@@ -585,14 +578,12 @@ fn extreme_values_in_the_decoded_headers() {
|
||||
wide[20..24].copy_from_slice(&i32::MAX.to_be_bytes());
|
||||
wide[24..28].copy_from_slice(&u32::MAX.to_be_bytes());
|
||||
let h = LayerHeader::parse(&wide).unwrap();
|
||||
let _ = (h.in_point(), h.in_point_seconds());
|
||||
let _ = h.in_point();
|
||||
let c = CompHeader::parse(&[0xff; 204]).unwrap();
|
||||
assert_eq!((c.duration_seconds(), c.work_area_start_seconds(), c.work_area_end_units), (None, None, None));
|
||||
let _ = (c.frame_rate(), c.frame_rate_from_fixed(), c.background_rgb(), c.pixel_aspect_ratio(), c.frame_rate_exact());
|
||||
assert_eq!(c.duration_frames(), Some(1));
|
||||
assert_eq!((c.duration_seconds(), c.work_area_end_units), (None, None));
|
||||
let _ = (c.frame_rate(), c.pixel_aspect.to_f64());
|
||||
let zero = CompHeader::parse(&[0; 204]).unwrap();
|
||||
assert_eq!((zero.frame_rate(), zero.pixel_aspect_ratio(), zero.duration_seconds()), (None, None, None));
|
||||
assert_eq!((zero.frame_rate_exact(), zero.duration_frames()), (None, None));
|
||||
assert_eq!((zero.frame_rate(), zero.pixel_aspect.to_f64(), zero.duration_seconds()), (None, None, None));
|
||||
let all = LayerHeader::parse(&[0xff; 164]).unwrap();
|
||||
let _ = (all.is_enabled(), all.is_auto_oriented(), all.light_type(), all.blend_mode.code());
|
||||
assert_eq!(LayerHeader::parse(&[0u8; 164]).unwrap().light_type(), None);
|
||||
@@ -691,7 +682,7 @@ fn hostile_text_dictionaries_and_marker_headers_never_panic() {
|
||||
|
||||
#[test]
|
||||
fn extreme_values_in_the_batch_3_fields() {
|
||||
use craft_aep::{CompHeader, Footage, LayerHeader, Rational};
|
||||
use craft_aep::{CompHeader, LayerHeader};
|
||||
// Time stretch: zero and maximal numerators and denominators (bytes 8 and 108), with extreme times.
|
||||
for (num, den) in [(0u32, 0u32), (1, 0), (0, 1), (u32::MAX, 1), (1, u32::MAX), (u32::MAX, u32::MAX), (3, 2)] {
|
||||
let mut ldta = [0u8; 164];
|
||||
@@ -704,7 +695,7 @@ fn extreme_values_in_the_batch_3_fields() {
|
||||
ldta[28..32].copy_from_slice(&i32::MIN.to_be_bytes());
|
||||
ldta[32..36].copy_from_slice(&1u32.to_be_bytes());
|
||||
let h = LayerHeader::parse(&ldta).unwrap();
|
||||
let _ = (h.stretch_ratio(), h.stretch_percent(), h.in_point(), h.out_point(), h.in_point_seconds(), h.out_point_seconds());
|
||||
let _ = (h.stretch_ratio(), h.in_point(), h.out_point());
|
||||
if den == 0 {
|
||||
assert_eq!((h.stretch_ratio(), h.in_point(), h.out_point()), (None, None, None));
|
||||
}
|
||||
@@ -712,34 +703,8 @@ fn extreme_values_in_the_batch_3_fields() {
|
||||
// Display start and shutter phase from extreme bytes; a missing tail leaves them unknown.
|
||||
for fill in [0x00u8, 0x7f, 0x80, 0xff] {
|
||||
let c = CompHeader::parse(&[fill; 204]).unwrap();
|
||||
let _ = (c.shutter_phase, c.display_start, c.display_start_seconds(), c.display_start_frame());
|
||||
let _ = (c.shutter_phase, c.display_start.map(|r| r.to_f64()));
|
||||
}
|
||||
let zero_rate = CompHeader::parse(&[0; 204]).unwrap();
|
||||
assert_eq!((zero_rate.display_start_seconds(), zero_rate.display_start_frame()), (None, None));
|
||||
// Footage durations: zero denominator, negative and maximal numerators, a huge frame rate.
|
||||
let base = Footage {
|
||||
file_type: String::new(),
|
||||
width: 0,
|
||||
height: 0,
|
||||
name: None,
|
||||
is_solid: false,
|
||||
solid_argb: None,
|
||||
path: None,
|
||||
duration: None,
|
||||
native_frame_rate: 0,
|
||||
bits_per_pixel: 0,
|
||||
file_size: 0,
|
||||
audio_sample_rate: None,
|
||||
is_sequence: true,
|
||||
psd_layer: None,
|
||||
};
|
||||
for (num, den, rate) in
|
||||
[(3i64, 30i64, 30u32), (3, 0, 30), (-3, 30, 30), (i64::MAX, 1, u32::MAX), (i64::MIN, -1, u32::MAX), (1, i64::MAX, 1), (0, 1, 0)]
|
||||
{
|
||||
let f = Footage { duration: Some(Rational { num, den }), native_frame_rate: rate, ..base.clone() };
|
||||
let _ = f.sequence_frame_count();
|
||||
}
|
||||
assert_eq!(Footage { duration: Some(Rational { num: 3, den: 30 }), native_frame_rate: 30, ..base.clone() }.sequence_frame_count(), Some(3));
|
||||
assert_eq!(Footage { duration: Some(Rational { num: 3, den: 0 }), native_frame_rate: 30, ..base.clone() }.sequence_frame_count(), None);
|
||||
assert_eq!(Footage { duration: None, ..base }.sequence_frame_count(), None);
|
||||
// A zero denominator has no value.
|
||||
assert_eq!(CompHeader::parse(&[0; 204]).unwrap().display_start.and_then(|r| r.to_f64()), None);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ fn text_layer_document(name: &str) -> Option<TextDocument> {
|
||||
let layer = comp.layers().find(|l| l.name().as_deref() == Some("T")).expect("the text layer T");
|
||||
let h = layer.header().unwrap();
|
||||
assert_eq!(h.layer_type, LayerType::Text);
|
||||
assert!(h.name_is_user_set());
|
||||
layer.text_document().expect("text document")
|
||||
})
|
||||
}
|
||||
@@ -192,7 +191,6 @@ fn png_footage_layer() {
|
||||
// The path comes from the alias JSON (the fixture holds a neutral replacement of the original).
|
||||
assert!(f.path.as_deref().is_some_and(|p| p.ends_with("test8x8.png")), "{:?}", f.path);
|
||||
assert_eq!(project.layer_source_size(&layer), Some((8, 8)));
|
||||
assert_eq!(project.layer_display_name(&layer).as_deref(), Some("png"));
|
||||
// L is still there below it.
|
||||
assert_eq!(comp.layers().count(), 2);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
//! The flat tree index the importer reads ([`TreeIndex`]) against the owned chunk tree, on every
|
||||
//! file of the fixture directory: the same tree, and the same answers from the typed view.
|
||||
|
||||
use std::fmt::Write as _;
|
||||
|
||||
use craft_aep::index::TreeIndex;
|
||||
use craft_aep::{
|
||||
AepFile, Chunk, CryptomatteData, CurvesData, Effect, HueSaturationData, LevelsData, Limits, MeshData, Project, PropGroup, PropNode, is_aepx,
|
||||
parse, parse_with_limits, read_aepx, write,
|
||||
};
|
||||
|
||||
/// Every `.aep` and `.aepx` in the fixture directory (the real projects included), sorted.
|
||||
fn every_file() -> Vec<std::path::PathBuf> {
|
||||
let Some(dir) = crate::common::dir() else {
|
||||
crate::common::skipped("*.aep");
|
||||
return Vec::new();
|
||||
};
|
||||
let Ok(entries) = std::fs::read_dir(&dir) else {
|
||||
crate::common::skipped("*.aep");
|
||||
return Vec::new();
|
||||
};
|
||||
let mut out: Vec<std::path::PathBuf> =
|
||||
entries.filter_map(Result::ok).map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "aep" || x == "aepx")).collect();
|
||||
out.sort();
|
||||
out
|
||||
}
|
||||
|
||||
/// What the typed view reads from a project, as text: items, compositions, layers with their
|
||||
/// headers, transforms, masks, effects with every parameter value, text, markers, the whole
|
||||
/// property tree with static values and key records, settings and versions.
|
||||
fn dump(view: &Project<'_, '_>) -> String {
|
||||
let mut s = String::new();
|
||||
let defs = view.effect_definition_index();
|
||||
for item in view.all_items() {
|
||||
let _ = writeln!(s, "item {:?} {:?} {:?} {:?} {:?}", item.kind(), item.name(), item.display_name(), item.id(), item.label());
|
||||
let _ = writeln!(s, " footage {:?} {:?} {:?}", item.footage(), item.sequence_info(), item.conform_frame_rate());
|
||||
let _ = writeln!(s, " folder {} {}", item.has_folder_contents(), item.folder_contents_unreadable());
|
||||
let Some(comp) = item.comp() else { continue };
|
||||
let _ = writeln!(s, " comp {:?} {:?} {:?}", comp.time_base(), comp.header(), comp.markers());
|
||||
for layer in comp.layers() {
|
||||
let _ = writeln!(s, " layer {:?} {:?} {:?} {:?}", layer.name(), layer.id(), layer.header(), layer.track_matte());
|
||||
let _ = writeln!(s, " transform {:?}", layer.transform(1920, 1080));
|
||||
let _ = writeln!(s, " time remap {:?} orientation {:?} {:?}", layer.time_remap(), layer.orientation(), layer.orientation_keys());
|
||||
let _ = writeln!(s, " masks {:?}", layer.mask_atoms().iter().map(|m| (&m.mask, &m.path_shapes)).collect::<Vec<_>>());
|
||||
let _ = writeln!(s, " shapes {:?}", layer.shape_groups());
|
||||
let _ = writeln!(
|
||||
s,
|
||||
" text {:?} {} {:?}",
|
||||
layer.text_document(),
|
||||
layer.text_document_count(),
|
||||
layer.source_text_property().map(|p| p.is_animated())
|
||||
);
|
||||
let _ = writeln!(s, " markers {:?} source {:?}", layer.markers(), view.layer_source(&layer).map(|i| i.id()));
|
||||
for fx in layer.effects() {
|
||||
let _ = writeln!(
|
||||
s,
|
||||
" fx {} {:?} {:?} {:?} {:?}",
|
||||
fx.match_name(),
|
||||
fx.display_name(),
|
||||
fx.enabled(),
|
||||
defs.source(&fx),
|
||||
fx.value_record_names()
|
||||
);
|
||||
let _ =
|
||||
writeln!(s, " names {:?} {:?} {:?} {:?}", fx.name(), fx.user_name(), fx.declared_param_count(), fx.has_own_definitions());
|
||||
for p in defs.params(&fx) {
|
||||
let _ = writeln!(s, " {} {:?} {:?} {:?} {:?}", p.match_name, p.kind, p.name, p.items, p.value());
|
||||
arbitrary(&fx, &p.match_name, &mut s);
|
||||
}
|
||||
for name in fx.value_record_names() {
|
||||
arbitrary(&fx, &name, &mut s);
|
||||
}
|
||||
}
|
||||
if let Some(root) = layer.properties() {
|
||||
tree(&root, 0, &mut s);
|
||||
}
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// The arbitrary data stored for a parameter and what each decoder reads from it.
|
||||
fn arbitrary(fx: &Effect<'_, '_>, name: &str, s: &mut String) {
|
||||
let Some(data) = fx.arbitrary_data(name) else { return };
|
||||
let _ = writeln!(s, " arbitrary {name} {data:?}");
|
||||
let _ = writeln!(s, " {:?} {:?}", HueSaturationData::parse(data), LevelsData::parse(data));
|
||||
let _ = writeln!(s, " {:?} {:?} {:?}", CurvesData::parse(data), MeshData::parse(data), CryptomatteData::parse(data));
|
||||
}
|
||||
|
||||
/// Appends a group and everything below it to the dump, depth-limited.
|
||||
fn tree(group: &PropGroup<'_, '_>, depth: usize, s: &mut String) {
|
||||
if depth > 32 {
|
||||
return;
|
||||
}
|
||||
let _ = writeln!(s, "{:depth$}group {:?} {:?}", "", group.user_name(), group.flags());
|
||||
for e in group.entries() {
|
||||
match e.node {
|
||||
PropNode::Group(g) => tree(&g, depth + 1, s),
|
||||
PropNode::Property(p) => {
|
||||
let keys: Option<Vec<Vec<u8>>> = p.keys().map(|k| k.records().map(<[u8]>::to_vec).collect());
|
||||
let _ = writeln!(s, "{:depth$} {} {:?} {:?} {:?} {:?}", "", e.match_name, p.user_name(), p.header(), p.static_value(), keys);
|
||||
}
|
||||
PropNode::Other(c) => {
|
||||
let _ = writeln!(s, "{:depth$} {} other {:?} {}", "", e.match_name, c.list_type(), c.descendants().count());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The index is the parsed tree and the view reads the same on every file.
|
||||
#[test]
|
||||
fn the_index_is_the_parsed_tree_and_the_view_reads_the_same_on_every_file() {
|
||||
for path in every_file() {
|
||||
let name = path.display().to_string();
|
||||
let bytes = std::fs::read(&path).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
// `.aepx` is read into the chunk tree and indexed from it; `.aep` is indexed in place.
|
||||
// A file that does not read (a damaged sample) must fail the index the same way.
|
||||
let file: AepFile<'_> = match if is_aepx(&bytes) { read_aepx(&bytes) } else { parse(&bytes) } {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
if !is_aepx(&bytes) {
|
||||
assert_eq!(TreeIndex::parse(&bytes).err(), Some(e), "{name}");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let index = (if is_aepx(&bytes) { TreeIndex::from_file(&file) } else { TreeIndex::parse(&bytes) }).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
assert!(index.to_file() == file, "{name}: the index is not the parsed tree");
|
||||
assert_eq!(index.len(), file.walk().count(), "{name}");
|
||||
assert_eq!(index.file_version(), file.file_version(), "{name}");
|
||||
assert_eq!(index.saved_by_version(), file.saved_by_version(), "{name}");
|
||||
assert_eq!(craft_aep::ProjectSettings::read_index(&index), craft_aep::ProjectSettings::read(&file), "{name}");
|
||||
let (Some(a), Some(b)) = (Project::new(&file), Project::from_index(&index)) else {
|
||||
assert!(Project::new(&file).is_none() && Project::from_index(&index).is_none(), "{name}");
|
||||
continue;
|
||||
};
|
||||
let (da, db) = (dump(&a), dump(&b));
|
||||
assert!(da == db, "{name}: the view reads differently through the index");
|
||||
}
|
||||
}
|
||||
|
||||
/// The index and the parser fail alike under tight limits.
|
||||
#[test]
|
||||
fn the_index_and_the_parser_fail_alike_under_tight_limits() {
|
||||
for path in every_file().into_iter().filter(|p| p.extension().is_some_and(|x| x == "aep")) {
|
||||
let name = path.display().to_string();
|
||||
let bytes = std::fs::read(&path).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
let total = parse(&bytes).map(|f| f.walk().count()).unwrap_or(0);
|
||||
for limits in [
|
||||
Limits { max_depth: 2, max_chunks: 10_000_000 },
|
||||
Limits { max_depth: 6, max_chunks: 10_000_000 },
|
||||
Limits { max_depth: 64, max_chunks: total },
|
||||
Limits { max_depth: 64, max_chunks: total.saturating_sub(1) },
|
||||
Limits { max_depth: 64, max_chunks: total / 2 },
|
||||
] {
|
||||
let old = parse_with_limits(&bytes, limits);
|
||||
let new = TreeIndex::parse_with_limits(&bytes, limits);
|
||||
match (old, new) {
|
||||
(Ok(f), Ok(i)) => assert!(i.to_file() == f, "{name}: {limits:?}"),
|
||||
(Err(a), Err(b)) => assert_eq!(a, b, "{name}: {limits:?}"),
|
||||
(a, b) => panic!("{name}: {limits:?}: {:?} vs {:?}", a.err(), b.err()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A 40-byte NUL-padded match name.
|
||||
fn tdmn(name: &str) -> Chunk<'static> {
|
||||
let mut b = name.as_bytes().to_vec();
|
||||
b.resize(40, 0);
|
||||
Chunk::data(*b"tdmn", b)
|
||||
}
|
||||
|
||||
/// A nested chunk `id` holding one `Utf8` with `text`.
|
||||
fn utf8(id: [u8; 4], text: &str) -> Chunk<'static> {
|
||||
Chunk { id, body: craft_aep::Body::Chunks(vec![Chunk::data(*b"Utf8", text.as_bytes().to_vec())]), pad: None }
|
||||
}
|
||||
|
||||
/// A project with one composition, one layer and one effect whose five parameters carry arbitrary
|
||||
/// data in the sizes the decoders read (Hue/Saturation, Levels, Curves, a 2 x 2 mesh, Cryptomatte),
|
||||
/// so the arbitrary-data path runs without the fixture directory.
|
||||
fn synthetic() -> Vec<u8> {
|
||||
let mut mesh = vec![0u8; 180];
|
||||
mesh[..8].copy_from_slice(&[0, 0, 0, 2, 0, 0, 0, 2]);
|
||||
let mut cry = vec![0u8; 200];
|
||||
cry[..4].copy_from_slice(b"cry1");
|
||||
cry[32..37].copy_from_slice(b"Layer");
|
||||
let mut levels = vec![0u8; 108];
|
||||
levels[8..16].copy_from_slice(&1.0f64.to_be_bytes());
|
||||
let payloads: [Vec<u8>; 5] = [vec![7u8; 180], levels, vec![0u8; CurvesData::SIZE], mesh, cry];
|
||||
let (mut part, mut values) =
|
||||
(vec![Chunk::data(*b"parn", 6u32.to_be_bytes().to_vec())], vec![Chunk::data(*b"tdsb", 1u32.to_be_bytes().to_vec()), utf8(*b"tdsn", "Mine")]);
|
||||
for (i, data) in payloads.into_iter().enumerate() {
|
||||
let name = format!("FX-{:04}", i + 1);
|
||||
let mut pard = vec![0u8; 148];
|
||||
pard[12..16].copy_from_slice(&11u32.to_be_bytes());
|
||||
part.extend([tdmn(&name), Chunk::data(*b"pard", pard)]);
|
||||
let record = Chunk::list(*b"tdbs", vec![Chunk::data(*b"tdsb", 1u32.to_be_bytes().to_vec()), Chunk::data(*b"cdat", vec![0, 0, 0, 1])]);
|
||||
values.extend([tdmn(&name), record, Chunk::list(*b"aRbs", vec![Chunk::data(*b"aRbp", data)])]);
|
||||
}
|
||||
values.push(tdmn("ADBE Group End"));
|
||||
let sspc = Chunk::list(*b"sspc", vec![utf8(*b"fnam", "Effect"), Chunk::list(*b"parT", part), Chunk::list(*b"tdgp", values)]);
|
||||
let parade = Chunk::list(*b"tdgp", vec![tdmn("FX"), sspc, tdmn("ADBE Group End")]);
|
||||
let root = Chunk::list(*b"tdgp", vec![tdmn("ADBE Effect Parade"), parade, tdmn("ADBE Group End")]);
|
||||
let layer = Chunk::list(*b"Layr", vec![Chunk::data(*b"ldta", vec![0u8; 160]), Chunk::data(*b"Utf8", b"Layer".to_vec()), root]);
|
||||
let mut idta = vec![0u8; 64];
|
||||
idta[..2].copy_from_slice(&4u16.to_be_bytes());
|
||||
idta[16..20].copy_from_slice(&2u32.to_be_bytes());
|
||||
let mut cdta = vec![0u8; 200];
|
||||
cdta[4..12].copy_from_slice(&[0, 0, 0, 1, 0, 0, 0, 30]);
|
||||
let item = Chunk::list(*b"Item", vec![Chunk::data(*b"idta", idta), Chunk::data(*b"Utf8", b"Comp".to_vec()), Chunk::data(*b"cdta", cdta), layer]);
|
||||
let file =
|
||||
AepFile { chunks: vec![Chunk::data(*b"head", vec![0u8; 20]), Chunk::list(*b"Fold", vec![item])], trailer: std::borrow::Cow::Borrowed(&[]) };
|
||||
write(&file).expect("write")
|
||||
}
|
||||
|
||||
/// The view reads arbitrary effect data the same through the index.
|
||||
#[test]
|
||||
fn the_view_reads_arbitrary_effect_data_the_same_through_the_index() {
|
||||
let bytes = synthetic();
|
||||
let file = parse(&bytes).expect("parse");
|
||||
let index = TreeIndex::parse(&bytes).expect("index");
|
||||
let (a, b) = (Project::new(&file).expect("project"), Project::from_index(&index).expect("project"));
|
||||
let (da, db) = (dump(&a), dump(&b));
|
||||
assert_eq!(da, db);
|
||||
// The synthetic effect really reaches every decoder (the dump is not empty of them).
|
||||
assert_eq!(da.matches("arbitrary FX-").count(), 10, "{da}");
|
||||
assert!(da.contains("Some(HueSaturationData") && da.contains("Some(LevelsData") && da.contains("Some(CurvesData"), "{da}");
|
||||
assert!(da.contains("Some(MeshData") && da.contains("Some(CryptomatteData"), "{da}");
|
||||
assert!(da.contains("Some(\"Mine\")"), "the user name is read: {da}");
|
||||
}
|
||||
@@ -111,7 +111,7 @@ fn match_names_come_from_the_tdmn_chunks() {
|
||||
assert!(!names.iter().any(|n| n == "ADBE Group End"));
|
||||
assert!(root.find(&["ADBE Transform Group", "ADBE Nonexistent"]).is_none());
|
||||
// The raw tdmn body is 40 bytes, NUL padded.
|
||||
let raw = root.chunk().children().iter().find(|c| c.is(b"tdmn")).unwrap();
|
||||
let raw = root.chunk().children().find(|c| c.is(b"tdmn")).unwrap();
|
||||
assert_eq!(raw.bytes().unwrap().len(), 40);
|
||||
}
|
||||
|
||||
@@ -150,8 +150,8 @@ fn effect_parameter_value() {
|
||||
assert!(instance.is_list(b"sspc"));
|
||||
// The effect's name is in a nested fnam chunk.
|
||||
let fnam = instance.child(b"fnam").unwrap();
|
||||
assert!(matches!(fnam.body, Body::Chunks(_)));
|
||||
assert_eq!(fnam.children()[0].bytes(), Some(&b"Slider Control"[..]));
|
||||
assert!(matches!(fnam.to_chunk().body, Body::Chunks(_)));
|
||||
assert_eq!(fnam.children().next().unwrap().bytes(), Some(&b"Slider Control"[..]));
|
||||
// The parameters are a property group inside the instance.
|
||||
let params = PropGroup::from_chunk(instance.child_list(b"tdgp").unwrap()).unwrap();
|
||||
assert_eq!(params.names(), ["ADBE Slider Control-0000", "ADBE Slider Control-0001", "ADBE Effect Built In Params"]);
|
||||
@@ -186,7 +186,7 @@ fn position_keyframes() {
|
||||
assert_eq!(times, [0, 25_600, 64_000]);
|
||||
let seconds: Vec<f64> = times.iter().map(|t| tb.seconds(*t).unwrap()).collect();
|
||||
assert_eq!(seconds, [0.0, 1.0, 2.5]);
|
||||
let values: Vec<[f64; 3]> = keys.keyframes().map(|k| k.spatial_value().unwrap()).collect();
|
||||
let values: Vec<Vec<f64>> = keys.keyframes().map(|k| k.value().unwrap()).collect();
|
||||
assert_eq!(values, [[0.0, 0.0, 0.0], [100.0, 0.0, 0.0], [200.0, 100.0, 0.0]]);
|
||||
}
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ fn linear_position_keys() {
|
||||
let values: Vec<_> = keys.iter().map(|k| k.spatial().unwrap().value).collect();
|
||||
assert_eq!(values, [[0.0, 0.0, 0.0], [100.0, 0.0, 0.0], [200.0, 100.0, 0.0]]);
|
||||
assert_eq!(p.first_key_value(), Some(vec![0.0, 0.0, 0.0]));
|
||||
assert_eq!(p.is_static_flag_set(), Some(false));
|
||||
assert!(p.is_animated());
|
||||
}));
|
||||
// The transform of the layer reports the animated Position as keyframed (value at the first key).
|
||||
let (t, _) = skip!(transform("055-key-position-3key-inserted-t2.5.aep"));
|
||||
@@ -181,7 +181,7 @@ fn roving_and_label_flags() {
|
||||
assert_eq!(roving, [false, true, false]);
|
||||
assert_eq!(keys[1].time_units(), Some(26509));
|
||||
assert_eq!(keys[1].seconds(25600), Some(1.0355078125));
|
||||
assert_eq!(keys[1].spatial_value(), Some([100.0, 0.0, 0.0]));
|
||||
assert_eq!(keys[1].value(), Some(vec![100.0, 0.0, 0.0]));
|
||||
assert_eq!(keys[1].flags(), Some(0x0027));
|
||||
assert_eq!(keys[1].label(), Some(0));
|
||||
}));
|
||||
@@ -193,7 +193,7 @@ fn roving_and_label_flags() {
|
||||
assert_eq!(keys[1].flags(), Some(0x0247));
|
||||
assert_eq!(keys[0].flags(), Some(0x0007));
|
||||
assert_eq!(keys[1].is_roving(), Some(false));
|
||||
assert_eq!(keys[1].spatial_value(), Some([100.0, 0.0, 0.0]));
|
||||
assert_eq!(keys[1].value(), Some(vec![100.0, 0.0, 0.0]));
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -204,7 +204,6 @@ fn scalar_opacity_keys() {
|
||||
assert!(p.is_animated());
|
||||
assert_eq!(p.dims(), Some(1));
|
||||
assert_eq!(p.time_scale(), Some(25600));
|
||||
assert_eq!(p.is_static_flag_set(), Some(false));
|
||||
let list = p.keys().unwrap();
|
||||
assert_eq!(list.header.record_size, 48);
|
||||
let keys: Vec<_> = list.keyframes().collect();
|
||||
@@ -243,10 +242,8 @@ fn expression_text_and_flag_bytes() {
|
||||
assert_eq!(p.expression(), None);
|
||||
let flags = p.expression_flags().unwrap();
|
||||
assert_eq!((flags.byte_76, flags.byte_77, flags.byte_120), (0, 0, 0));
|
||||
assert!(!flags.all_set());
|
||||
// A written static property: tdsb 1, byte 5 of tdb4 has bit 0 set (15 for Position).
|
||||
assert_eq!(p.flags(), Some(1));
|
||||
assert_eq!(p.is_static_flag_set(), Some(true));
|
||||
assert_eq!(p.header().and_then(|h| h.get(5).copied()), Some(15));
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -113,6 +113,26 @@ pub fn wav_order(channel_config: u8) -> Option<&'static [usize]> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The channel configuration whose speakers a stream has: its own, or for configuration 0 the
|
||||
/// configuration 3–6 that its program config element spells out. A program config element lists
|
||||
/// front, side, back and LFE elements in that order, so one that opens with a centre and a front
|
||||
/// pair and goes on with exactly the elements of a configuration has that configuration's speakers
|
||||
/// (and [`wav_order`]). Any other list has no speaker order that can be told, and gives 0: ffmpeg's
|
||||
/// 5.1(side), for one, lists the pair first and already decodes in WAV order.
|
||||
#[cfg(feature = "aac-decode")]
|
||||
pub fn speaker_config(asc: &AudioSpecificConfig) -> u8 {
|
||||
if asc.channel_config != 0 {
|
||||
return asc.channel_config;
|
||||
}
|
||||
let centre_then_pair = asc.pce.as_ref().is_some_and(|p| p.front.iter().map(|e| e.0).eq([false, true]));
|
||||
if !centre_then_pair {
|
||||
return 0;
|
||||
}
|
||||
let elements = asc.layout();
|
||||
let same = |config: &u8| config_layout(*config).is_some_and(|l| l.iter().map(|e| e.0).eq(elements.iter().map(|e| e.0)));
|
||||
(3..=6).find(same).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// The order the [`Encoder`] takes its input in, from WAV order: for each encoder input channel
|
||||
/// (AAC element order, see [`EncoderConfig::channels`]), the WAV-order channel to feed it. The
|
||||
/// inverse of [`wav_order`] for 3 to 7 channels; `None` for mono and stereo (already in order) and
|
||||
@@ -279,6 +299,30 @@ mod tests {
|
||||
mod order_tests {
|
||||
use super::*;
|
||||
|
||||
/// A program config element in the shape of a channel configuration has that configuration's
|
||||
/// speakers, with the centre first. ffmpeg's 5.1(side) lists its pair first and already decodes
|
||||
/// in WAV order, so it stays as it is.
|
||||
#[cfg(feature = "aac-decode")]
|
||||
#[test]
|
||||
fn a_program_config_element_takes_the_configuration_it_matches() {
|
||||
let with = |pce| AudioSpecificConfig { pce: Some(pce), ..AudioSpecificConfig::lc(3, 0) };
|
||||
let centre_first = ProgramConfig { front: vec![(false, 0), (true, 0)], back: vec![(true, 1)], lfe: vec![0], ..Default::default() };
|
||||
assert_eq!(speaker_config(&with(centre_first)), 6);
|
||||
let three = ProgramConfig { front: vec![(false, 0), (true, 0)], ..Default::default() };
|
||||
assert_eq!(speaker_config(&with(three)), 3);
|
||||
let ffmpeg = ProgramConfig { front: vec![(true, 0), (false, 0)], side: vec![(false, 1)], back: vec![(true, 1)], ..Default::default() };
|
||||
assert_eq!(speaker_config(&with(ffmpeg)), 0);
|
||||
// the same element types with no front pair: the side pair is not left and right
|
||||
let no_front_pair =
|
||||
ProgramConfig { front: vec![(false, 0)], side: vec![(true, 0)], back: vec![(true, 1)], lfe: vec![0], ..Default::default() };
|
||||
assert_eq!(speaker_config(&with(no_front_pair)), 0);
|
||||
// 7.1 in a program config element is not one of the unambiguous configurations
|
||||
let wide =
|
||||
ProgramConfig { front: vec![(false, 0), (true, 0)], side: vec![(true, 1)], back: vec![(true, 2)], lfe: vec![0], ..Default::default() };
|
||||
assert_eq!(speaker_config(&with(wide)), 0);
|
||||
assert_eq!(speaker_config(&AudioSpecificConfig::lc(3, 6)), 6);
|
||||
}
|
||||
|
||||
/// `aac_order` undoes `wav_order` for each channel count the encoder maps to a configuration
|
||||
/// (3–6 channels: configurations 3–6; 7: configuration 11).
|
||||
#[test]
|
||||
|
||||
@@ -156,7 +156,8 @@ impl TrackDecoder {
|
||||
let codec = if up.is_some() { CodecId::HeAac } else { CodecId::Aac };
|
||||
// Only twice the core rate is upsampled to; any other signalled rate plays the core.
|
||||
let out = if up.is_some() { out } else { dec.sample_rate() };
|
||||
let cfg = dec.config().channel_config;
|
||||
// a program config element that spells out a configuration has its speakers
|
||||
let cfg = aac::speaker_config(dec.config());
|
||||
let layout = aac_layout(cfg, dec.channels());
|
||||
let order = layout.is_ordered().then(|| aac::wav_order(cfg)).flatten();
|
||||
(Inner::Aac { dec: Box::new(dec), up, order, adts_format }, codec, out, layout, SampleFormat::Compressed)
|
||||
@@ -1079,6 +1080,8 @@ mod tests {
|
||||
let mut dec = TrackDecoder::new(&aac_params(), &[&frame]).unwrap();
|
||||
assert_eq!(dec.channels(), 3);
|
||||
assert_eq!(dec.decode(&frame).unwrap().channels.len(), 3);
|
||||
// its element lists a centre and a front pair, as configuration 3 does: L R C, in WAV order
|
||||
assert_eq!(dec.layout(), ChannelLayout::SURROUND_3_0);
|
||||
// no program config element where one is needed
|
||||
let bare = aac::AdtsHeader::write(2, 3, 0, 1, 0x7FF).unwrap();
|
||||
let bare = [&bare[..], &[0xE0]].concat();
|
||||
|
||||
@@ -669,10 +669,14 @@ fn adts_with_a_program_config_element_decodes() {
|
||||
assert_eq!(bytes[3] >> 6 | (bytes[2] & 1) << 2, 0, "the fixture signals its layout in a PCE");
|
||||
let (info, buf) = decode(&bytes, None).unwrap();
|
||||
assert_eq!((info.container, info.codec, buf.channels.len()), (c.container, c.codec, c.channels));
|
||||
assert_eq!(info.layout, c.layout);
|
||||
// A program config element that lists a centre, a front pair and then the elements of 5.1 has
|
||||
// 5.1's speakers, in WAV order like ffmpeg's decode; any other list keeps the decoded order,
|
||||
// which ffmpeg versions write differently.
|
||||
let ordered = info.layout.is_ordered();
|
||||
assert_eq!(info.layout, if ordered { ChannelLayout::SURROUND_5_1_SIDE } else { c.layout });
|
||||
assert_eq!(buf.frames(), reference(&ff, &path).len() / c.channels);
|
||||
// A program config's channels come in its own order, and which order ffmpeg writes differs by
|
||||
// version: each of ours must match a different one of ffmpeg's.
|
||||
// Each of our channels must match a different one of ffmpeg's: the same one when the layout is
|
||||
// ordered.
|
||||
let refr = reference(&ff, &path);
|
||||
let mut matched = Vec::new();
|
||||
for plane in &buf.channels {
|
||||
@@ -686,7 +690,9 @@ fn adts_with_a_program_config_element_decodes() {
|
||||
assert!(s >= c.min_snr, "channel {} matches ffmpeg's {best} at only {s:.1} dB", matched.len());
|
||||
matched.push(best);
|
||||
}
|
||||
matched.sort_unstable();
|
||||
if !ordered {
|
||||
matched.sort_unstable();
|
||||
}
|
||||
assert_eq!(matched, (0..c.channels).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,8 @@ Buffers are checked once per call, before any pixel is written.
|
||||
|
||||
* `Transform::proof(src, proof, display, intent, bpc, simulate_paper)`: soft-proof chain.
|
||||
* `GamutCheck::new(src, proof, ΔE)`: out-of-gamut test (gamut warning).
|
||||
`check.mask_rgba(&rgba, &mut mask)?` fills a whole mask (255 where alpha > 0 and out of gamut) on
|
||||
the thread pool and returns the count; it needs an RGB source profile.
|
||||
* `Lut3d::from_transform(&t, 33)?.to_rgba16f_bytes()`: display transform as a 3D texture;
|
||||
`Lut3d::from_fn(size, f)` samples any RGB function.
|
||||
* `Transform::gpu_matrix_shaper()`: accurate RGB input curves + matrix + output curves for
|
||||
|
||||
+89
-3
@@ -2,13 +2,18 @@
|
||||
|
||||
use crate::clut::Clut;
|
||||
use crate::pipeline::{Pipeline, Stage};
|
||||
use crate::profile::Profile;
|
||||
use crate::profile::{ColorSpace, Profile};
|
||||
use crate::transform::link_stages;
|
||||
use crate::{CmsError, Intent, check_len};
|
||||
use crate::{CmsError, Intent, check_len, invalid_arg};
|
||||
|
||||
/// Default ΔE76 threshold above which a colour counts as out of gamut.
|
||||
pub const DEFAULT_THRESHOLD: f32 = 4.0;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const PAR_MIN_PIXELS: usize = 16 * 1024;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const PAR_CHUNK_PIXELS: usize = 16 * 1024;
|
||||
|
||||
/// Precomputed out-of-gamut test for colours of `src` against `proof`.
|
||||
///
|
||||
/// A colour is out of gamut when sending it to the proof device (relative colorimetric,
|
||||
@@ -17,6 +22,7 @@ pub const DEFAULT_THRESHOLD: f32 = 4.0;
|
||||
#[derive(Debug)]
|
||||
pub struct GamutCheck {
|
||||
inputs: usize,
|
||||
source_space: ColorSpace,
|
||||
grid: Option<Clut>,
|
||||
exact: (Pipeline, Pipeline),
|
||||
pub threshold: f32,
|
||||
@@ -44,7 +50,7 @@ impl GamutCheck {
|
||||
4 => Some(Clut::sample(vec![17; 4], 1, de)?),
|
||||
_ => None,
|
||||
};
|
||||
Ok(GamutCheck { inputs: n, grid, exact: (direct, via), threshold })
|
||||
Ok(GamutCheck { inputs: n, source_space: src.color_space, grid, exact: (direct, via), threshold })
|
||||
}
|
||||
|
||||
pub fn inputs(&self) -> usize {
|
||||
@@ -76,4 +82,84 @@ impl GamutCheck {
|
||||
pub fn out_of_gamut(&self, px: &[f32]) -> Result<bool, CmsError> {
|
||||
Ok(self.delta_e(px)? > self.threshold)
|
||||
}
|
||||
|
||||
/// Out-of-gamut mask for RGBA pixels: `255` where the pixel has alpha > 0 and its RGB
|
||||
/// channels are out of gamut, `0` elsewhere. Returns the number of flagged pixels. Only
|
||||
/// `min(px.len(), mask.len())` pixels are processed. Runs on the rayon pool (not on wasm);
|
||||
/// the result is identical to calling [`Self::out_of_gamut`] per pixel.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`CmsError::InvalidArgument`] when the check was not built from an RGB source profile: an
|
||||
/// RGBA pixel carries only RGB colour channels, so it cannot be tested against a CMYK, gray
|
||||
/// or Lab source. Nothing is written to `mask` in that case.
|
||||
pub fn mask_rgba(&self, px: &[[f32; 4]], mask: &mut [u8]) -> Result<usize, CmsError> {
|
||||
if self.source_space != ColorSpace::Rgb || self.inputs != 3 {
|
||||
return Err(invalid_arg(format!("gamut mask needs an RGB source profile, got {:?} with {} channels", self.source_space, self.inputs)));
|
||||
}
|
||||
let n = px.len().min(mask.len());
|
||||
let (Some(px), Some(mask)) = (px.get(..n), mask.get_mut(..n)) else { return Ok(0) };
|
||||
let run = |p: &[[f32; 4]], m: &mut [u8]| {
|
||||
let mut count = 0;
|
||||
for (p, m) in p.iter().zip(m.iter_mut()) {
|
||||
// The source has three channels (checked above), so the check cannot fail.
|
||||
let hit = p[3] > 0.0 && self.out_of_gamut(&[p[0], p[1], p[2]]).unwrap_or(false);
|
||||
*m = if hit { 255 } else { 0 };
|
||||
count += usize::from(hit);
|
||||
}
|
||||
count
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if n >= PAR_MIN_PIXELS {
|
||||
use rayon::prelude::*;
|
||||
return Ok(px.par_chunks(PAR_CHUNK_PIXELS).zip(mask.par_chunks_mut(PAR_CHUNK_PIXELS)).map(|(p, m)| run(p, m)).sum());
|
||||
}
|
||||
Ok(run(px, mask))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Builtin;
|
||||
|
||||
fn image(n: usize) -> Vec<[f32; 4]> {
|
||||
let mut s = 0x9E37_79B9u32;
|
||||
let mut rnd = move || {
|
||||
s ^= s << 13;
|
||||
s ^= s >> 17;
|
||||
s ^= s << 5;
|
||||
(s >> 8) as f32 / (1u32 << 24) as f32
|
||||
};
|
||||
// Includes out-of-range values and zero alpha.
|
||||
(0..n).map(|i| [rnd() * 1.2 - 0.1, rnd(), rnd(), if i % 7 == 0 { 0.0 } else { rnd() }]).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mask_rgba_matches_per_pixel_check() {
|
||||
let src = Builtin::Srgb.profile();
|
||||
// Sizes straddle the parallel threshold and the chunk size, odd ones included.
|
||||
for (proof, threshold) in [(Builtin::CoatedCmyk, 4.0), (Builtin::CoatedCmyk, 1.5), (Builtin::GrayGamma22, 4.0), (Builtin::DisplayP3, 0.5)] {
|
||||
let check = GamutCheck::new(src, proof.profile(), threshold).unwrap();
|
||||
for n in [0, 1, 1023, 16 * 1024 - 1, 16 * 1024, 16 * 1024 + 1, 50_001] {
|
||||
let px = image(n);
|
||||
let want: Vec<u8> = px.iter().map(|p| if p[3] > 0.0 && check.out_of_gamut(&p[..3]).unwrap() { 255 } else { 0 }).collect();
|
||||
let mut got = vec![7u8; n];
|
||||
let count = check.mask_rgba(&px, &mut got).unwrap();
|
||||
assert_eq!(got, want, "{proof:?} threshold {threshold} n {n}");
|
||||
assert_eq!(count, want.iter().filter(|&&m| m == 255).count());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mask_rgba_rejects_non_rgb_source() {
|
||||
// A CMYK (4-channel), gray (1-channel) and Lab (3-channel, not RGB) source cannot be tested from RGBA pixels.
|
||||
for src in [Builtin::CoatedCmyk, Builtin::GrayGamma22, Builtin::LabD50] {
|
||||
let check = GamutCheck::new(src.profile(), Builtin::Srgb.profile(), 4.0).unwrap();
|
||||
let px = image(64);
|
||||
let mut mask = vec![9u8; 64];
|
||||
assert!(matches!(check.mask_rgba(&px, &mut mask), Err(CmsError::InvalidArgument(_))), "{src:?}");
|
||||
assert!(mask.iter().all(|&m| m == 9), "mask must stay untouched");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+149
-13
@@ -135,7 +135,8 @@ pub fn parse_as(format: Format, bytes: &[u8]) -> Result<LutFile, LutError> {
|
||||
}
|
||||
}
|
||||
|
||||
/// UTF-16 with a byte-order mark (Photoshop loads such `.look` files, #9118), else UTF-8.
|
||||
/// UTF-16 with a byte-order mark (Photoshop loads such `.look` files, #9118), else UTF-8
|
||||
/// (a UTF-8 BOM is stripped).
|
||||
fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> {
|
||||
let utf16 = |rest: &[u8], from: fn([u8; 2]) -> u16| {
|
||||
let units: Vec<u16> = rest.as_chunks::<2>().0.iter().map(|&c| from(c)).collect();
|
||||
@@ -144,6 +145,7 @@ fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> {
|
||||
match bytes {
|
||||
[0xFF, 0xFE, rest @ ..] => utf16(rest, u16::from_le_bytes),
|
||||
[0xFE, 0xFF, rest @ ..] => utf16(rest, u16::from_be_bytes),
|
||||
[0xEF, 0xBB, 0xBF, rest @ ..] => String::from_utf8_lossy(rest),
|
||||
_ => String::from_utf8_lossy(bytes),
|
||||
}
|
||||
}
|
||||
@@ -155,14 +157,19 @@ pub fn parse_cube(text: &str) -> Result<LutFile, LutError> {
|
||||
let (mut dmin, mut dmax) = ([0.0f32; 3], [1.0f32; 3]);
|
||||
let mut rows: Vec<f32> = Vec::new();
|
||||
for line in text.lines() {
|
||||
// A '#' inside a quoted TITLE is part of the title, not a comment.
|
||||
let raw = line.trim_start();
|
||||
let skip = if raw.starts_with("TITLE") { raw.find('"').and_then(|q| raw[q + 1..].find('"').map(|e| q + e + 2)) } else { None }.unwrap_or(0);
|
||||
let line = match raw[skip..].find('#') {
|
||||
Some(i) => &raw[..skip + i],
|
||||
None => raw,
|
||||
// Only a quote at the beginning of a TITLE value protects '#'. Quotes inside an
|
||||
// unquoted title's comment must not turn that comment into part of the title.
|
||||
if let Some(quoted) = line
|
||||
.trim_start()
|
||||
.strip_prefix("TITLE")
|
||||
.filter(|rest| rest.starts_with(char::is_whitespace))
|
||||
.and_then(|rest| rest.trim_start().strip_prefix('"'))
|
||||
{
|
||||
let Some((value, _)) = quoted.split_once('"') else { return err("unterminated quoted TITLE") };
|
||||
title = value.to_string();
|
||||
continue;
|
||||
}
|
||||
.trim();
|
||||
let line = line.split('#').next().unwrap_or("").trim();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
@@ -187,7 +194,9 @@ pub fn parse_cube(text: &str) -> Result<LutFile, LutError> {
|
||||
}
|
||||
k if k.starts_with(|c: char| c.is_ascii_digit() || c == '-' || c == '+' || c == '.') => {
|
||||
let mut v = vec![k.parse::<f32>().map_err(|_| LutError(format!("bad number in `{line}`")))?];
|
||||
v.extend(it.filter_map(|s| s.parse::<f32>().ok()));
|
||||
for value in it {
|
||||
v.push(value.parse::<f32>().map_err(|_| LutError(format!("bad number in `{line}`")))?);
|
||||
}
|
||||
if v.len() != 3 {
|
||||
return err(format!("bad data line `{line}`"));
|
||||
}
|
||||
@@ -209,6 +218,15 @@ pub fn parse_cube(text: &str) -> Result<LutFile, LutError> {
|
||||
if rows.len() != expected_rows {
|
||||
return err("no LUT_3D_SIZE in .cube file");
|
||||
}
|
||||
// #8913: the resample below bakes the curve onto a fixed 33-point grid, so a steep
|
||||
// curve over a narrow domain cannot survive the resample — the baked table silently
|
||||
// shifts colours (measured 42 RGB8 levels for a two-node ramp over 0.24..0.26, where
|
||||
// Photoshop renders the ramp exactly). Rather than proxy the risk by domain width (a
|
||||
// 0.1..0.17 ramp spans two grid cells yet still loses ~28 levels), measure the
|
||||
// resample's actual error against the exact curve and refuse only tables whose baked
|
||||
// output shifts by more than a couple of RGB8 levels. Default-domain tables keep the
|
||||
// existing bake untouched; gentle curves over any domain bake within tolerance and
|
||||
// stay accepted.
|
||||
// The 1D table is resampled onto a 0..1 cube anyway, so its input domain
|
||||
// (`DOMAIN_MIN`/`MAX`, `LUT_1D_INPUT_RANGE`) is baked into the resample: an input `v`
|
||||
// reads the table at `(v - min) / (max - min)`, saturating outside.
|
||||
@@ -221,6 +239,36 @@ pub fn parse_cube(text: &str) -> Result<LutFile, LutError> {
|
||||
};
|
||||
let mut l = LutFile::from_fn(&title, 33, |c| [curve(0, c[0]), curve(1, c[1]), curve(2, c[2])])?;
|
||||
l.title = title;
|
||||
let non_default = dmin.iter().any(|v| *v != 0.0) || dmax.iter().any(|v| *v != 1.0);
|
||||
if non_default {
|
||||
// Worst baked-vs-exact error per channel, in RGB8 levels: the baked cube is read
|
||||
// by linear interpolation between grid points (red fastest, so channel `ch` sits
|
||||
// at stride 33^ch), while `curve` is what the file actually says.
|
||||
const MAX_BAKE_ERROR: f32 = 2.0 / 255.0;
|
||||
for ch in 0..3 {
|
||||
let stride = [1usize, 33, 33 * 33][ch];
|
||||
let mut worst = 0.0f32;
|
||||
for k in 0..=256 {
|
||||
let v = k as f32 / 256.0;
|
||||
let x = v * 32.0;
|
||||
let i = (x as usize).min(31);
|
||||
let f = x - i as f32;
|
||||
let baked = l.data[i * stride * 3 + ch] * (1.0 - f) + l.data[(i + 1).min(32) * stride * 3 + ch] * f;
|
||||
worst = worst.max((baked - curve(ch, v)).abs());
|
||||
}
|
||||
if worst > MAX_BAKE_ERROR {
|
||||
return err(format!(
|
||||
"1D LUT domain on {} is {min}..{max}: the curve changes faster than the 33-point grid can \
|
||||
hold (baking shifts colours by up to {levels:.0} RGB8 levels); export the look as a 3D LUT \
|
||||
or widen DOMAIN_MIN/DOMAIN_MAX",
|
||||
["red", "green", "blue"][ch],
|
||||
min = dmin[ch],
|
||||
max = dmax[ch],
|
||||
levels = worst * 255.0,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return l.check();
|
||||
}
|
||||
err("no LUT_3D_SIZE in .cube file")
|
||||
@@ -554,6 +602,48 @@ pub fn builtin(id: &str) -> Option<LutFile> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cube_title_hashes_and_comment_quotes_are_distinct() {
|
||||
let rows = "LUT_1D_SIZE 2\n0 0 0 # low\n1 1 1 # high\n";
|
||||
for (line, title) in [
|
||||
(r#"TITLE "Look #3" # comment "ignored""#, "Look #3"),
|
||||
(r#"TITLE Look # "draft""#, "Look"),
|
||||
(" TITLE\t\"Été #2\"", "Été #2"),
|
||||
(r#"TITLE "" # empty"#, ""),
|
||||
] {
|
||||
assert_eq!(parse_cube(&format!("{line}\n{rows}")).unwrap().title, title);
|
||||
}
|
||||
let lut = LutFile::from_fn("Look #3", 2, |c| c).unwrap();
|
||||
assert_eq!(parse_cube(&write_cube(&lut)).unwrap().title, "Look #3");
|
||||
assert!(parse_cube(&format!("TITLE \"unterminated # title\n{rows}")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_rejects_invalid_numeric_fields() {
|
||||
let text = "LUT_3D_SIZE 2\n0.25 nope 0.5 0.75\n0 0 1\n0 1 0\n0 1 1\n1 0 0\n1 0 1\n1 1 0\n1 1 1\n";
|
||||
assert!(parse_cube(text).is_err(), "invalid numeric field must not be ignored");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn utf8_bom_is_stripped() {
|
||||
let l = LutFile::identity(3).unwrap();
|
||||
let mut bytes = vec![0xEF, 0xBB, 0xBF];
|
||||
bytes.extend_from_slice(write_cube(&l).as_bytes());
|
||||
let p = parse("a.cube", &bytes).unwrap();
|
||||
assert_eq!(p.size, l.size);
|
||||
assert_eq!(p.title, l.title);
|
||||
let titled = LutFile::from_fn("Look", 3, |c| c).unwrap();
|
||||
let mut bytes = vec![0xEF, 0xBB, 0xBF];
|
||||
bytes.extend_from_slice(write_cube(&titled).as_bytes());
|
||||
assert_eq!(parse("a.cube", &bytes).unwrap().title, "Look");
|
||||
let mut bytes = vec![0xEF, 0xBB, 0xBF];
|
||||
bytes.extend_from_slice(b"LUT_3D_SIZE 2\n");
|
||||
for i in 0..8 {
|
||||
bytes.extend_from_slice(format!("{} {} {}\n", i & 1, (i >> 1) & 1, i >> 2).as_bytes());
|
||||
}
|
||||
assert_eq!(parse("a.cube", &bytes).unwrap().size, 2);
|
||||
}
|
||||
|
||||
/// An identity `.3dl` of edge `n` (12-bit nodes, blue fastest) under `shaper`.
|
||||
fn identity_3dl(shaper: &[u32], n: usize) -> String {
|
||||
let mut t = shaper.iter().map(u32::to_string).collect::<Vec<_>>().join(" ") + "\n";
|
||||
@@ -811,6 +901,49 @@ DOMAIN_MAX 0.5 1 1
|
||||
assert!(e.0.contains("below DOMAIN_MAX"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_1d_narrow_domain_is_refused() {
|
||||
// #8913: the issue's fixture — a two-node ramp over 0.24..0.26. Baking it onto the
|
||||
// 33-point grid rendered grey-65 as 148 where Photoshop renders 190 (42-level error).
|
||||
// It must now be an actionable parse error rather than a silently wrong table.
|
||||
let e = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0.24 0.24 0.24\nDOMAIN_MAX 0.26 0.26 0.26\n0 0 0\n1 1 1\n").unwrap_err();
|
||||
assert!(e.0.contains("changes faster than the 33-point grid"), "{e}");
|
||||
// The same domain via LUT_1D_INPUT_RANGE is refused identically.
|
||||
let e = parse_cube("LUT_1D_SIZE 2\nLUT_1D_INPUT_RANGE 0.24 0.26\n0 0 0\n1 1 1\n").unwrap_err();
|
||||
assert!(e.0.contains("changes faster"), "{e}");
|
||||
// A per-channel narrow domain names the offending channel.
|
||||
let e = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0 0 0.24\nDOMAIN_MAX 1 1 0.26\n0 0 0\n1 1 1\n").unwrap_err();
|
||||
assert!(e.0.contains("blue"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_1d_wide_domain_still_bakes() {
|
||||
// A steep ramp over 0.1..0.17 spans two grid cells but still loses ~28 RGB8 levels
|
||||
// to the resample, so the measured-error check refuses it even though the old width
|
||||
// check let it through.
|
||||
let e = parse_cube("LUT_1D_SIZE 2\nLUT_1D_INPUT_RANGE 0.1 0.17\n0 0 0\n1 1 1\n").unwrap_err();
|
||||
assert!(e.0.contains("changes faster"), "steep ramp over 0.1..0.17 refused: {e}");
|
||||
// A gentle (here linear) curve over a grid-aligned domain bakes exactly and stays
|
||||
// accepted — the check measures the bake's error, not the domain's width. Value
|
||||
// assertions, not just a successful parse: the baked table must reproduce the curve.
|
||||
// Table row k sits at t = k/32 of the domain 0.25..0.75; the curve y = 0.4 + 0.4t
|
||||
// is gentle (a half-level per grid step), so the bake must reproduce it exactly.
|
||||
let mut table = String::from("LUT_1D_SIZE 33\nLUT_1D_INPUT_RANGE 0.25 0.75\n");
|
||||
for k in 0..33 {
|
||||
let y = 0.4 + 0.4 * (k as f32 / 32.0);
|
||||
table.push_str(&format!("{y} {y} {y}\n"));
|
||||
}
|
||||
let l = parse_cube(&table).expect("gentle curve over a non-default domain still bakes");
|
||||
let at = |r: usize| l.data[r * 3];
|
||||
// Grid entry r is input r/32: 0.5 is the middle of the domain (t=0.5) -> 0.6.
|
||||
assert!((at(16) - 0.6).abs() < 1.0 / 255.0, "baked value at 0.5 tracks the curve: {}", at(16));
|
||||
// Domain top (input 0.75 = grid 24, t=1) -> 0.8.
|
||||
assert!((at(24) - 0.8).abs() < 1.0 / 255.0, "baked value at the domain top tracks the curve: {}", at(24));
|
||||
// Below the domain saturates to the first row.
|
||||
assert!((at(8) - 0.4).abs() < 1.0 / 255.0, "below the domain saturates: {}", at(8));
|
||||
parse_cube("LUT_1D_SIZE 2\n0 0 0\n1 1 1\n").expect("default domain");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cube_1d_domain_is_baked_into_the_resample() {
|
||||
// An identity ramp over the domain 0.25..0.75: 0.25 maps to 0, 0.5 to 0.5, 0.75 to 1.
|
||||
@@ -1042,11 +1175,14 @@ DOMAIN_MAX 0.5 1 1
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn look_hex_floats() {
|
||||
let id = LutFile::identity(2).unwrap();
|
||||
let hex: String = id.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect();
|
||||
fn look_data_is_little_endian_float32() {
|
||||
// Bytes written out by hand rather than by the encoder the parser mirrors, so a wrong byte
|
||||
// order fails: 0.25, 0.5, 1.0 (3E800000, 3F000000, 3F800000 stored low byte first), then zeros.
|
||||
let hex = format!("0000803E 0000003F 0000803F {}", "00000000 ".repeat(21));
|
||||
let xml = format!("<?xml version=\"1.0\"?><look><LUT><size>\"2\"</size><data>\"{hex}\"</data></LUT></look>");
|
||||
assert_eq!(parse("a.look", xml.as_bytes()).unwrap().data, id.data);
|
||||
let data = parse("a.look", xml.as_bytes()).unwrap().data;
|
||||
assert_eq!(data[..3], [0.25, 0.5, 1.0]);
|
||||
assert!(data[3..].iter().all(|&v| v == 0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
//! - [`warp`]: preset text warps (`a:prstTxWarp`).
|
||||
//! - The model, without readers and writers yet: [`style`] (colour references, fills, lines,
|
||||
//! effects), [`text`] (text bodies, paragraphs, runs), [`table`], [`chart`] with [`numfmt`]
|
||||
//! (number format codes) and [`diagram`] (SmartArt outlines). Media are named by [`MediaId`];
|
||||
//! (number format codes), [`theme`] (colour, font and format schemes) and [`diagram`] (SmartArt outlines). Media are named by [`MediaId`];
|
||||
//! the bytes stay with the caller.
|
||||
//!
|
||||
//! Nothing here opens a package: readers take a [`craft_xml::El`] and writers a
|
||||
@@ -27,6 +27,7 @@ pub mod preset;
|
||||
pub mod style;
|
||||
pub mod table;
|
||||
pub mod text;
|
||||
pub mod theme;
|
||||
mod trendline;
|
||||
pub mod warp;
|
||||
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
//! Themes: colour scheme, font scheme and format scheme (the fill/line/effect "style matrix").
|
||||
|
||||
use crate::color::{ColorScheme, ColorTransform, Rgba, SchemeSlot};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::style::{ColorRef, Effects, Fill, Gradient, GradientShape, GradientStop, Line, Shadow};
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct FontSet {
|
||||
pub latin: String,
|
||||
pub ea: String,
|
||||
pub cs: String,
|
||||
/// Per-script typefaces (`a:font script="Arab" typeface="…"`), in document order.
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub scripts: Vec<ScriptFont>,
|
||||
}
|
||||
|
||||
/// A theme font for one script (ISO 15924 tag, e.g. `Arab`).
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct ScriptFont {
|
||||
pub script: String,
|
||||
pub typeface: String,
|
||||
}
|
||||
|
||||
impl FontSet {
|
||||
/// The typeface the theme gives `script` (`"Arab"`), if any.
|
||||
pub fn script(&self, script: &str) -> Option<&str> {
|
||||
self.scripts.iter().find(|f| f.script.eq_ignore_ascii_case(script) && !f.typeface.is_empty()).map(|f| f.typeface.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct FontScheme {
|
||||
pub name: String,
|
||||
/// Headings.
|
||||
pub major: FontSet,
|
||||
/// Body.
|
||||
pub minor: FontSet,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct FormatScheme {
|
||||
pub name: String,
|
||||
/// Subtle, moderate, intense (style index 1–3).
|
||||
pub fills: Vec<Fill>,
|
||||
pub lines: Vec<Line>,
|
||||
pub effects: Vec<Effects>,
|
||||
/// Background fills (index 1001–1003).
|
||||
pub bg_fills: Vec<Fill>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct Theme {
|
||||
pub name: String,
|
||||
pub colors: ColorScheme,
|
||||
pub fonts: FontScheme,
|
||||
pub format: FormatScheme,
|
||||
/// Raw theme XML extras kept for round-trip (object defaults, extra colour lists).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub raw_extra: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Theme {
|
||||
/// A plain theme: black and white, a slate and a warm grey, six accents, no font names (the
|
||||
/// writer puts `Arial` where a Latin face is required) and [`default_format`].
|
||||
fn default() -> Self {
|
||||
let hex = |s: &str| Rgba::from_hex(s).unwrap_or(Rgba::BLACK);
|
||||
let colors = ["000000", "FFFFFF", "1E2B3C", "EDEBE7", "2E6FD8", "F26B1D", "2EA36F", "8A5CD8", "D83F6B", "E3B21C", "2E6FD8", "7A5BA6"];
|
||||
Theme {
|
||||
name: "Theme".into(),
|
||||
colors: ColorScheme { name: "Default".into(), colors: colors.map(hex) },
|
||||
fonts: FontScheme::default(),
|
||||
format: default_format(),
|
||||
raw_extra: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
/// Resolve a theme font reference (`+mj-lt`, `+mn-ea`…) to a family name.
|
||||
pub fn font(&self, name: &str) -> String {
|
||||
match name {
|
||||
"+mj-lt" => self.fonts.major.latin.clone(),
|
||||
"+mn-lt" => self.fonts.minor.latin.clone(),
|
||||
"+mj-ea" => self.fonts.major.ea.clone(),
|
||||
"+mn-ea" => self.fonts.minor.ea.clone(),
|
||||
"+mj-cs" => self.fonts.major.cs.clone(),
|
||||
"+mn-cs" => self.fonts.minor.cs.clone(),
|
||||
other => other.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ph() -> ColorRef {
|
||||
ColorRef::scheme(SchemeSlot::PhClr)
|
||||
}
|
||||
|
||||
/// The format scheme used where a theme has none, and to fill a short style list: flat solid fills, a soft gradient, and a gentle shadow for "intense".
|
||||
pub fn default_format() -> FormatScheme {
|
||||
let grad = |a: i32, b: i32| {
|
||||
Fill::Gradient(Gradient {
|
||||
tile: [0.0; 4],
|
||||
stops: vec![
|
||||
GradientStop { pos: 0.0, color: ph().with(ColorTransform::LumMod(a)).with(ColorTransform::SatMod(103000)) },
|
||||
GradientStop { pos: 1.0, color: ph().with(ColorTransform::LumMod(b)) },
|
||||
],
|
||||
shape: GradientShape::Linear { angle: 90.0, scaled: false },
|
||||
rotate_with_shape: true,
|
||||
flip: None,
|
||||
})
|
||||
};
|
||||
FormatScheme {
|
||||
name: "Default".into(),
|
||||
fills: vec![Fill::solid(ph()), grad(110000, 92000), grad(105000, 80000)],
|
||||
lines: vec![Line::solid(ph(), 0.75), Line::solid(ph(), 1.0), Line::solid(ph(), 1.5)],
|
||||
effects: vec![
|
||||
Effects::default(),
|
||||
Effects::default(),
|
||||
Effects {
|
||||
outer_shadow: Some(Shadow {
|
||||
color: ColorRef::rgb(Rgba::BLACK).with(ColorTransform::Alpha(40000)),
|
||||
blur: 6.0,
|
||||
dist: 3.0,
|
||||
dir: 90.0,
|
||||
inner: false,
|
||||
sx: 1.0,
|
||||
sy: 1.0,
|
||||
kx: 0.0,
|
||||
ky: 0.0,
|
||||
align: "t".into(),
|
||||
rotate_with_shape: false,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
bg_fills: vec![
|
||||
Fill::solid(ph()),
|
||||
Fill::solid(ph().with(ColorTransform::Tint(95000)).with(ColorTransform::SatMod(170000))),
|
||||
grad(102000, 85000),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_default_theme_has_a_full_style_matrix_and_no_font_names() {
|
||||
let t = Theme::default();
|
||||
assert_eq!((t.format.fills.len(), t.format.lines.len(), t.format.effects.len(), t.format.bg_fills.len()), (3, 3, 3, 3));
|
||||
assert_eq!(t.colors.get(SchemeSlot::Lt1), Rgba::WHITE);
|
||||
assert_eq!(t.fonts, FontScheme::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn theme_font_references_resolve_and_other_names_pass_through() {
|
||||
let mut t = Theme::default();
|
||||
t.fonts.major.latin = "Head".into();
|
||||
t.fonts.minor.cs = "Body CS".into();
|
||||
t.fonts.minor.scripts.push(ScriptFont { script: "Arab".into(), typeface: "Naskh".into() });
|
||||
assert_eq!(t.font("+mj-lt"), "Head");
|
||||
assert_eq!(t.font("+mn-cs"), "Body CS");
|
||||
assert_eq!(t.font("Georgia"), "Georgia");
|
||||
assert_eq!(t.fonts.minor.script("arab"), Some("Naskh"));
|
||||
assert_eq!(t.fonts.minor.script("Hebr"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_theme_survives_json_and_missing_fields_take_the_default() {
|
||||
let t = Theme::default();
|
||||
let json = serde_json::to_string(&t).unwrap();
|
||||
assert_eq!(serde_json::from_str::<Theme>(&json).unwrap(), t);
|
||||
let partial: Theme = serde_json::from_str(r#"{"name":"X"}"#).unwrap();
|
||||
assert_eq!(partial.colors, t.colors);
|
||||
assert_eq!(partial.name, "X");
|
||||
}
|
||||
}
|
||||
+10
-2
@@ -3,8 +3,8 @@
|
||||
//! [`Document::read`] indexes a tag list (from [`crate::parse`]) into its header variables,
|
||||
//! symbol tables, blocks, entities and objects. Nothing is copied: every record borrows its tags,
|
||||
//! so a caller can still read a group this module gives no name to (its own extended data, a
|
||||
//! newer group). The typed forms ([`Layer`], [`Linetype`], [`TextStyle`], [`Layout`]...) are made
|
||||
//! on request.
|
||||
//! newer group). The typed forms ([`Layer`], [`Linetype`], [`TextStyle`], [`Layout`], and
|
||||
//! [`Entity`] for an entity record) are made on request.
|
||||
//!
|
||||
//! The values are the file's: points in the coordinate system the format stores them in (the
|
||||
//! object coordinate system for planar entities, see [`ocs_to_wcs`]), angles in the unit the
|
||||
@@ -18,10 +18,18 @@ use crate::Tag;
|
||||
|
||||
pub mod aci;
|
||||
mod document;
|
||||
mod entity;
|
||||
mod hatch;
|
||||
mod tables;
|
||||
pub mod text;
|
||||
|
||||
pub use document::{Block, Dictionary, Document, EntityRecord, Header, Layout, Record, entity_records, records};
|
||||
pub use entity::{
|
||||
Attrib, Common, Dimension, Entity, EntityKind, Image, Insert, Leader, LwPolyline, LwVertex, Polyline, Spline, Text, Vertex, Viewport,
|
||||
};
|
||||
pub use hatch::{Gradient, Hatch, HatchEdge, HatchLoop, HatchPath, PatternLine};
|
||||
pub use tables::{AppId, BlockRecord, DashElement, DimStyle, Layer, Linetype, TextStyle, Ucs, View, Vport, dim_var_code, dim_var_name};
|
||||
pub use text::{MText, MTextColumns};
|
||||
|
||||
/// A point or vector: x, y, z.
|
||||
pub type Point = [f64; 3];
|
||||
|
||||
@@ -0,0 +1,669 @@
|
||||
//! Entities, typed: the properties every entity has ([`Common`]) and the geometry of each type.
|
||||
//!
|
||||
//! Values are as the file stores them. Planar entities (CIRCLE, ARC, LWPOLYLINE, 2D POLYLINE,
|
||||
//! TEXT, INSERT, SOLID, TRACE, HATCH) have their points in the object coordinate system of
|
||||
//! [`Common::extrusion`] ([`super::ocs_to_wcs`]); LINE, POINT, ELLIPSE, SPLINE, RAY, XLINE,
|
||||
//! 3DFACE, MTEXT, LEADER and DIMENSION definition points are world coordinates. Angles are in
|
||||
//! degrees, except an ELLIPSE's parameters, which the format stores in radians.
|
||||
|
||||
use super::document::{EntityRecord, Record};
|
||||
use super::hatch::Hatch;
|
||||
use super::text::{MText, decode_line, split_embedded};
|
||||
use super::{Color, Groups, Lineweight, Point, Transparency, outside_app, parse_handle};
|
||||
use crate::Tag;
|
||||
|
||||
/// The properties every entity has.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Common {
|
||||
pub handle: Option<u64>,
|
||||
/// Group 330: the block record (or other object) that owns the entity.
|
||||
pub owner: Option<u64>,
|
||||
/// Group 8 (`0` when absent).
|
||||
pub layer: String,
|
||||
/// Group 6; `None` is ByLayer.
|
||||
pub linetype: Option<String>,
|
||||
pub color: Color,
|
||||
pub lineweight: Lineweight,
|
||||
/// Group 48 (1 when absent or not positive).
|
||||
pub ltscale: f64,
|
||||
/// Group 60 is not 1.
|
||||
pub visible: bool,
|
||||
/// Group 67 is 1.
|
||||
pub paper_space: bool,
|
||||
pub transparency: Transparency,
|
||||
/// Group 39.
|
||||
pub thickness: f64,
|
||||
/// Groups 210/220/230 (the world Z axis when absent).
|
||||
pub extrusion: Point,
|
||||
/// Group 390: the plot style name object.
|
||||
pub plot_style: Option<u64>,
|
||||
}
|
||||
|
||||
/// The groups of the AcDbEntity subclass: the record up to the entity's own subclass marker (all
|
||||
/// of it in files without markers, and in records with no AcDbEntity marker, whose layer and
|
||||
/// colour may follow the entity's marker), or up to an embedded object (101). Later groups reuse
|
||||
/// the codes: an MTEXT's column width is 48. Markers inside an application-defined group do not
|
||||
/// count.
|
||||
fn entity_groups(tags: &[Tag]) -> &[Tag] {
|
||||
let is_entity = |t: &Tag| t.str_ref().is_some_and(|s| s == "AcDbEntity");
|
||||
let marked = outside_app(tags).any(|(_, t)| t.code == 100 && is_entity(t));
|
||||
let end = outside_app(tags).find(|(_, t)| (marked && t.code == 100 && !is_entity(t)) || t.code == 101).map_or(tags.len(), |(i, _)| i);
|
||||
tags.get(..end).unwrap_or(tags)
|
||||
}
|
||||
|
||||
impl Common {
|
||||
pub fn read(tags: &[Tag]) -> Common {
|
||||
let (all, e) = (Groups(tags), Groups(entity_groups(tags)));
|
||||
Common {
|
||||
handle: e.handle(5),
|
||||
owner: outside_app(tags).find(|(_, t)| t.code == 330).and_then(|(_, t)| parse_handle(&t.str())),
|
||||
layer: e.name(8).unwrap_or_else(|| "0".into()),
|
||||
linetype: e.name(6).filter(|l| !l.eq_ignore_ascii_case("ByLayer")),
|
||||
color: Color::of(&e),
|
||||
lineweight: e.i(370).map_or(Lineweight::ByLayer, Lineweight::from_dxf),
|
||||
ltscale: e.f(48).filter(|s| *s > 0.0).unwrap_or(1.0),
|
||||
visible: e.i(60) != Some(1),
|
||||
paper_space: e.i(67) == Some(1),
|
||||
transparency: e.i(440).map_or(Transparency::ByLayer, Transparency::from_dxf),
|
||||
thickness: all.f_or(39, 0.0),
|
||||
extrusion: [all.f_or(210, 0.0), all.f_or(220, 0.0), all.f_or(230, 1.0)],
|
||||
plot_style: e.handle(390).filter(|h| *h != 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A vertex of an LWPOLYLINE.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Default)]
|
||||
pub struct LwVertex {
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
/// Group 42: the tangent of a quarter of the arc to the next vertex (0: straight).
|
||||
pub bulge: f64,
|
||||
pub start_width: f64,
|
||||
pub end_width: f64,
|
||||
}
|
||||
|
||||
/// An LWPOLYLINE.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct LwPolyline {
|
||||
pub vertices: Vec<LwVertex>,
|
||||
/// Flags (70) bit 1.
|
||||
pub closed: bool,
|
||||
/// Flags bit 128: the linetype runs around the vertices.
|
||||
pub plinegen: bool,
|
||||
/// Group 43.
|
||||
pub const_width: f64,
|
||||
/// Group 38.
|
||||
pub elevation: f64,
|
||||
}
|
||||
|
||||
/// A VERTEX of a POLYLINE.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Vertex {
|
||||
pub location: Point,
|
||||
pub bulge: f64,
|
||||
pub start_width: f64,
|
||||
pub end_width: f64,
|
||||
/// Group 70: 1 added by curve fitting, 8 added by spline fitting, 16 a spline frame control
|
||||
/// point, 32 a 3D polyline vertex, 64 a mesh vertex, 128 a polyface vertex (128 without 64:
|
||||
/// a face record).
|
||||
pub flags: i64,
|
||||
/// Groups 71–74: the vertex numbers of a polyface face record (1-based; negative: the edge
|
||||
/// from it is invisible; 0: none).
|
||||
pub face: [i64; 4],
|
||||
}
|
||||
|
||||
impl Vertex {
|
||||
/// A face record of a polyface mesh, which holds vertex numbers and no point.
|
||||
pub fn is_face(&self) -> bool {
|
||||
self.flags & 128 != 0 && self.flags & 64 == 0
|
||||
}
|
||||
/// A control point of the frame a spline-fit polyline was made from: not on the curve.
|
||||
pub fn is_spline_frame(&self) -> bool {
|
||||
self.flags & 16 != 0
|
||||
}
|
||||
}
|
||||
|
||||
/// A POLYLINE with its VERTEX records: a 2D or 3D polyline, a polygon mesh or a polyface mesh.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Polyline {
|
||||
/// Group 70: 1 closed (a mesh: closed in M), 2 curve-fit, 4 spline-fit, 8 a 3D polyline, 16 a
|
||||
/// polygon mesh, 32 a mesh closed in N, 64 a polyface mesh, 128 the linetype runs around the
|
||||
/// vertices.
|
||||
pub flags: i64,
|
||||
/// The z of the POLYLINE's own point: a 2D polyline's elevation.
|
||||
pub elevation: f64,
|
||||
/// Groups 40 and 41: the default widths.
|
||||
pub start_width: f64,
|
||||
pub end_width: f64,
|
||||
/// Groups 71 and 72: a mesh's M and N vertex counts (a polyface mesh: vertices and faces).
|
||||
pub m: i64,
|
||||
pub n: i64,
|
||||
/// Group 75: the smooth surface type (5 quadratic, 6 cubic, 8 Bezier).
|
||||
pub smooth: i64,
|
||||
pub vertices: Vec<Vertex>,
|
||||
}
|
||||
|
||||
impl Polyline {
|
||||
pub fn closed(&self) -> bool {
|
||||
self.flags & 1 != 0
|
||||
}
|
||||
pub fn is_3d(&self) -> bool {
|
||||
self.flags & 8 != 0
|
||||
}
|
||||
pub fn is_mesh(&self) -> bool {
|
||||
self.flags & 16 != 0
|
||||
}
|
||||
pub fn is_polyface(&self) -> bool {
|
||||
self.flags & 64 != 0
|
||||
}
|
||||
}
|
||||
|
||||
/// A SPLINE (NURBS), in world coordinates.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Spline {
|
||||
/// Group 71 (3 when absent).
|
||||
pub degree: i64,
|
||||
/// Group 70: 1 closed, 2 periodic, 4 rational, 8 planar, 16 linear.
|
||||
pub flags: i64,
|
||||
pub knots: Vec<f64>,
|
||||
pub control: Vec<Point>,
|
||||
/// Group 41: one per control point when rational.
|
||||
pub weights: Vec<f64>,
|
||||
pub fit: Vec<Point>,
|
||||
/// Groups 12/22/32 and 13/23/33.
|
||||
pub start_tangent: Option<Point>,
|
||||
pub end_tangent: Option<Point>,
|
||||
/// Group 44.
|
||||
pub fit_tolerance: f64,
|
||||
}
|
||||
|
||||
/// TEXT, and the text of an ATTRIB or ATTDEF.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Text {
|
||||
/// Group 10: the first alignment point.
|
||||
pub insert: Point,
|
||||
/// Group 11: the second alignment point, which places text that is not left-baseline.
|
||||
pub align: Option<Point>,
|
||||
pub height: f64,
|
||||
/// The string, decoded ([`decode_line`]); `%%` codes are left as written.
|
||||
pub value: String,
|
||||
/// Group 50, in degrees.
|
||||
pub rotation: f64,
|
||||
/// Group 41 (1 when absent).
|
||||
pub width_factor: f64,
|
||||
/// Group 51, in degrees.
|
||||
pub oblique: f64,
|
||||
/// Group 7 (`Standard` when absent).
|
||||
pub style: String,
|
||||
/// Group 71: 2 backwards, 4 upside down.
|
||||
pub generation: i64,
|
||||
/// Group 72: 0 left, 1 centre, 2 right, 3 aligned, 4 middle, 5 fit.
|
||||
pub halign: i64,
|
||||
/// Group 73 (74 in an ATTRIB or ATTDEF): 0 baseline, 1 bottom, 2 middle, 3 top.
|
||||
pub valign: i64,
|
||||
}
|
||||
|
||||
impl Text {
|
||||
pub fn read(g: &Groups<'_>) -> Text {
|
||||
Text {
|
||||
insert: g.point(10),
|
||||
align: g.point_opt(11),
|
||||
height: g.f_or(40, 0.0),
|
||||
value: decode_line(&g.s(1).unwrap_or_default()),
|
||||
rotation: g.f_or(50, 0.0),
|
||||
width_factor: g.f_or(41, 1.0),
|
||||
oblique: g.f_or(51, 0.0),
|
||||
style: g.name(7).unwrap_or_else(|| "Standard".into()),
|
||||
generation: g.i_or(71, 0),
|
||||
halign: g.i_or(72, 0),
|
||||
valign: g.i(73).or_else(|| g.i(74)).unwrap_or(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An ATTRIB (an attribute of a block reference) or ATTDEF (its definition in the block).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Attrib {
|
||||
/// Group 2.
|
||||
pub tag: String,
|
||||
pub text: Text,
|
||||
/// Group 3 of an ATTDEF, decoded; empty in an ATTRIB.
|
||||
pub prompt: String,
|
||||
/// Group 70: 1 invisible, 2 constant, 4 verify, 8 preset.
|
||||
pub flags: i64,
|
||||
/// The 280 after the tag in the attribute's subclass (one before the tag is the subclass
|
||||
/// version).
|
||||
pub lock_position: bool,
|
||||
/// The embedded MTEXT of a multiline attribute.
|
||||
pub multiline: Option<MText>,
|
||||
}
|
||||
|
||||
impl Attrib {
|
||||
pub fn read(kind: &str, tags: &[Tag]) -> Attrib {
|
||||
// A multiline attribute has its text as embedded MTEXT after its own groups.
|
||||
let (own, embedded) = split_embedded(tags);
|
||||
let g = Groups(own);
|
||||
let sub = own.iter().position(|t| t.code == 100 && t.str_ref().is_some_and(|s| s.trim().starts_with("AcDbAttribute"))).unwrap_or(0);
|
||||
let rest = own.get(sub..).unwrap_or_default();
|
||||
let lock_position = rest.iter().position(|t| t.code == 2).is_some_and(|at| rest.iter().skip(at + 1).any(|t| t.code == 280 && t.i64() != 0));
|
||||
Attrib {
|
||||
tag: g.s(2).unwrap_or_default(),
|
||||
text: Text::read(&g),
|
||||
prompt: if kind == "ATTDEF" { decode_line(&g.s(3).unwrap_or_default()) } else { String::new() },
|
||||
flags: g.i_or(70, 0),
|
||||
lock_position,
|
||||
multiline: embedded.filter(|e| !e.is_empty()).map(MText::read),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An INSERT: a block reference, with its attributes.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Insert {
|
||||
/// Group 2.
|
||||
pub block: String,
|
||||
pub insert: Point,
|
||||
/// Groups 41, 42 and 43 (1 when absent).
|
||||
pub scale: Point,
|
||||
/// Group 50, in degrees.
|
||||
pub rotation: f64,
|
||||
/// Groups 70 and 71 (1 when absent or less).
|
||||
pub columns: i64,
|
||||
pub rows: i64,
|
||||
/// Groups 44 and 45.
|
||||
pub column_spacing: f64,
|
||||
pub row_spacing: f64,
|
||||
/// The ATTRIB records that follow, each with its own properties.
|
||||
pub attribs: Vec<(Common, Attrib)>,
|
||||
}
|
||||
|
||||
/// A DIMENSION. The geometry it is drawn with is in its block ([`Dimension::block`]).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Dimension {
|
||||
/// Group 70: the type in the low bits (0 rotated, 1 aligned, 2 angular, 3 diameter, 4 radius,
|
||||
/// 5 angular 3-point, 6 ordinate), 32 the block is this dimension's alone, 64 an ordinate's
|
||||
/// X type, 128 the text is at a user position.
|
||||
pub flags: i64,
|
||||
/// Group 2: the block that holds the dimension's picture.
|
||||
pub block: Option<String>,
|
||||
/// Group 3 (`Standard` when absent).
|
||||
pub style: String,
|
||||
/// Group 10: the definition point.
|
||||
pub defpt: Point,
|
||||
/// Group 11: the middle of the text, in the object coordinate system.
|
||||
pub text_mid: Point,
|
||||
/// Group 12: the insertion point for clones (baseline and continue).
|
||||
pub insertion: Point,
|
||||
/// Groups 13 to 16: the definition points of the type.
|
||||
pub p13: Point,
|
||||
pub p14: Point,
|
||||
pub p15: Point,
|
||||
pub p16: Point,
|
||||
/// Group 1, decoded: empty or `<>` for the measurement, a space for none.
|
||||
pub text: String,
|
||||
/// Group 42: the measurement, when the file states it.
|
||||
pub measurement: Option<f64>,
|
||||
/// Group 50: the angle of a rotated dimension, in degrees.
|
||||
pub rotation: f64,
|
||||
/// Group 52: the oblique angle of the extension lines, in degrees.
|
||||
pub oblique: f64,
|
||||
/// Group 53: the text's rotation, in degrees.
|
||||
pub text_rotation: f64,
|
||||
/// Group 51: the horizontal direction, in degrees.
|
||||
pub horizontal: f64,
|
||||
/// Group 40: the leader length of a radius or diameter dimension.
|
||||
pub leader_length: f64,
|
||||
}
|
||||
|
||||
impl Dimension {
|
||||
/// The type: the low four bits of the flags.
|
||||
pub fn kind(&self) -> i64 {
|
||||
self.flags & 0x0f
|
||||
}
|
||||
}
|
||||
|
||||
/// A LEADER.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Leader {
|
||||
pub vertices: Vec<Point>,
|
||||
/// Group 71 is not 0.
|
||||
pub arrow: bool,
|
||||
/// Group 72 is not 0: a spline path.
|
||||
pub spline: bool,
|
||||
/// Group 3 (`Standard` when absent).
|
||||
pub style: String,
|
||||
/// Group 73: what the leader was made with (0 text, 1 a tolerance, 2 a block, 3 nothing).
|
||||
pub annotation_type: i64,
|
||||
/// Group 340: the annotation.
|
||||
pub annotation: Option<u64>,
|
||||
}
|
||||
|
||||
/// A paper space VIEWPORT.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Viewport {
|
||||
/// Group 10: the centre on the sheet.
|
||||
pub center: Point,
|
||||
/// Groups 40 and 41: the size on the sheet.
|
||||
pub width: f64,
|
||||
pub height: f64,
|
||||
/// Group 69: the id (1 is the sheet's own view). Other programs write -1 for unknown.
|
||||
pub id: i64,
|
||||
/// Group 90: the status flags (16384 locked, 65536 clipped by the entity of group 340,
|
||||
/// 131072 off).
|
||||
pub status: i64,
|
||||
/// Groups 12/22: the view centre in display coordinates, whose origin is the target.
|
||||
pub view_center: [f64; 2],
|
||||
/// Groups 16/26/36 and 17/27/37.
|
||||
pub view_direction: Point,
|
||||
pub view_target: Point,
|
||||
/// Group 45.
|
||||
pub view_height: f64,
|
||||
/// Group 51: the twist, in degrees.
|
||||
pub twist: f64,
|
||||
/// Group 340: the entity that clips the viewport.
|
||||
pub clip: Option<u64>,
|
||||
/// Groups 331 and 341: the layers frozen in the viewport.
|
||||
pub frozen_layers: Vec<u64>,
|
||||
}
|
||||
|
||||
/// An IMAGE or WIPEOUT: a raster's place and clip. The file is named by the IMAGEDEF object.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Image {
|
||||
/// Group 10: the lower-left corner.
|
||||
pub insert: Point,
|
||||
/// Groups 11 and 12: the vectors of one pixel along the image's width and height.
|
||||
pub u: Point,
|
||||
pub v: Point,
|
||||
/// Groups 13/23: the size in pixels.
|
||||
pub size: [f64; 2],
|
||||
/// Group 340: the IMAGEDEF.
|
||||
pub image_def: Option<u64>,
|
||||
/// Group 70: 1 show, 2 show when not aligned with the screen, 4 use the clip, 8 transparency.
|
||||
pub display: i64,
|
||||
/// Group 280 is not 0.
|
||||
pub clipping: bool,
|
||||
/// Groups 281, 282 and 283 (50, 50 and 0 when absent).
|
||||
pub brightness: i64,
|
||||
pub contrast: i64,
|
||||
pub fade: i64,
|
||||
/// Group 71: 1 a rectangle (two corners), 2 a polygon.
|
||||
pub clip_type: i64,
|
||||
/// Groups 14/24: the clip boundary in pixels, with the origin at the image's top-left corner
|
||||
/// and y down: a point `q` is at `insert + u * (q.x + 0.5) + v * (0.5 - q.y)`.
|
||||
pub clip: Vec<[f64; 2]>,
|
||||
/// Group 290 is not 0: the clip keeps the outside.
|
||||
pub clip_inverted: bool,
|
||||
}
|
||||
|
||||
/// The geometry of an entity.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum EntityKind {
|
||||
Line {
|
||||
start: Point,
|
||||
end: Point,
|
||||
},
|
||||
/// `angle` is group 50: the X axis of the point's symbol, in degrees.
|
||||
Point {
|
||||
location: Point,
|
||||
angle: f64,
|
||||
},
|
||||
Circle {
|
||||
center: Point,
|
||||
radius: f64,
|
||||
},
|
||||
Arc {
|
||||
center: Point,
|
||||
radius: f64,
|
||||
start_angle: f64,
|
||||
end_angle: f64,
|
||||
},
|
||||
/// `major` is the end of the major axis from the centre; `start` and `end` are parameters in
|
||||
/// radians (0 and 2π for a full ellipse).
|
||||
Ellipse {
|
||||
center: Point,
|
||||
major: Point,
|
||||
ratio: f64,
|
||||
start: f64,
|
||||
end: f64,
|
||||
},
|
||||
LwPolyline(LwPolyline),
|
||||
Polyline(Polyline),
|
||||
Spline(Spline),
|
||||
Ray {
|
||||
base: Point,
|
||||
direction: Point,
|
||||
},
|
||||
XLine {
|
||||
base: Point,
|
||||
direction: Point,
|
||||
},
|
||||
Text(Text),
|
||||
MText(MText),
|
||||
AttDef(Attrib),
|
||||
Insert(Insert),
|
||||
Dimension(Dimension),
|
||||
Leader(Leader),
|
||||
/// The fourth corner repeats the third when the file has three.
|
||||
Solid {
|
||||
corners: [Point; 4],
|
||||
},
|
||||
Trace {
|
||||
corners: [Point; 4],
|
||||
},
|
||||
/// `hidden_edges` is group 70: bit n hides edge n.
|
||||
Face3d {
|
||||
corners: [Point; 4],
|
||||
hidden_edges: i64,
|
||||
},
|
||||
Hatch(Hatch),
|
||||
Viewport(Viewport),
|
||||
Image(Image),
|
||||
Wipeout(Image),
|
||||
/// `text` is the feature control frame's string, as written.
|
||||
Tolerance {
|
||||
insert: Point,
|
||||
direction: Point,
|
||||
style: String,
|
||||
text: String,
|
||||
},
|
||||
/// A type this module does not model (or a HATCH with no boundary data): read it from the
|
||||
/// record's tags.
|
||||
Other,
|
||||
}
|
||||
|
||||
/// An entity: its properties and geometry.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Entity {
|
||||
pub common: Common,
|
||||
pub kind: EntityKind,
|
||||
}
|
||||
|
||||
fn lwpolyline(tags: &[Tag], g: &Groups<'_>) -> LwPolyline {
|
||||
let mut vertices: Vec<LwVertex> = Vec::new();
|
||||
for t in tags {
|
||||
if t.code == 10 {
|
||||
vertices.push(LwVertex { x: t.f64(), ..LwVertex::default() });
|
||||
} else if let Some(v) = vertices.last_mut() {
|
||||
match t.code {
|
||||
20 => v.y = t.f64(),
|
||||
40 => v.start_width = t.f64(),
|
||||
41 => v.end_width = t.f64(),
|
||||
42 => v.bulge = t.f64(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
let flags = g.i_or(70, 0);
|
||||
LwPolyline { vertices, closed: flags & 1 != 0, plinegen: flags & 128 != 0, const_width: g.f_or(43, 0.0), elevation: g.f_or(38, 0.0) }
|
||||
}
|
||||
|
||||
fn polyline(g: &Groups<'_>, followers: &[Record<'_>]) -> Polyline {
|
||||
let vertices = followers
|
||||
.iter()
|
||||
.map(|r| {
|
||||
let v = r.groups();
|
||||
Vertex {
|
||||
location: v.point(10),
|
||||
bulge: v.f_or(42, 0.0),
|
||||
start_width: v.f_or(40, 0.0),
|
||||
end_width: v.f_or(41, 0.0),
|
||||
flags: v.i_or(70, 0),
|
||||
face: [71, 72, 73, 74].map(|c| v.i_or(c, 0)),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Polyline {
|
||||
flags: g.i_or(70, 0),
|
||||
elevation: g.f_or(30, 0.0),
|
||||
start_width: g.f_or(40, 0.0),
|
||||
end_width: g.f_or(41, 0.0),
|
||||
m: g.i_or(71, 0),
|
||||
n: g.i_or(72, 0),
|
||||
smooth: g.i_or(75, 0),
|
||||
vertices,
|
||||
}
|
||||
}
|
||||
|
||||
fn spline(g: &Groups<'_>) -> Spline {
|
||||
Spline {
|
||||
degree: g.i_or(71, 3),
|
||||
flags: g.i_or(70, 0),
|
||||
knots: g.all_f(40),
|
||||
control: g.points(10),
|
||||
weights: g.all_f(41),
|
||||
fit: g.points(11),
|
||||
start_tangent: g.point_opt(12),
|
||||
end_tangent: g.point_opt(13),
|
||||
fit_tolerance: g.f_or(44, 0.0),
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(g: &Groups<'_>, followers: &[Record<'_>]) -> Insert {
|
||||
Insert {
|
||||
block: g.s(2).unwrap_or_default(),
|
||||
insert: g.point(10),
|
||||
scale: [g.f_or(41, 1.0), g.f_or(42, 1.0), g.f_or(43, 1.0)],
|
||||
rotation: g.f_or(50, 0.0),
|
||||
columns: g.i_or(70, 1).max(1),
|
||||
rows: g.i_or(71, 1).max(1),
|
||||
column_spacing: g.f_or(44, 0.0),
|
||||
row_spacing: g.f_or(45, 0.0),
|
||||
attribs: followers.iter().map(|r| (Common::read(r.tags), Attrib::read("ATTRIB", r.tags))).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn dimension(g: &Groups<'_>) -> Dimension {
|
||||
Dimension {
|
||||
flags: g.i_or(70, 0),
|
||||
block: g.name(2),
|
||||
style: g.name(3).unwrap_or_else(|| "Standard".into()),
|
||||
defpt: g.point(10),
|
||||
text_mid: g.point(11),
|
||||
insertion: g.point(12),
|
||||
p13: g.point(13),
|
||||
p14: g.point(14),
|
||||
p15: g.point(15),
|
||||
p16: g.point(16),
|
||||
text: decode_line(&g.s(1).unwrap_or_default()),
|
||||
measurement: g.f(42),
|
||||
rotation: g.f_or(50, 0.0),
|
||||
oblique: g.f_or(52, 0.0),
|
||||
text_rotation: g.f_or(53, 0.0),
|
||||
horizontal: g.f_or(51, 0.0),
|
||||
leader_length: g.f_or(40, 0.0),
|
||||
}
|
||||
}
|
||||
|
||||
fn viewport(tags: &[Tag], g: &Groups<'_>) -> Viewport {
|
||||
Viewport {
|
||||
center: g.point(10),
|
||||
width: g.f_or(40, 0.0),
|
||||
height: g.f_or(41, 0.0),
|
||||
id: g.i_or(69, 0),
|
||||
status: g.i_or(90, 0),
|
||||
view_center: [g.f_or(12, 0.0), g.f_or(22, 0.0)],
|
||||
view_direction: g.point_opt(16).unwrap_or([0.0, 0.0, 1.0]),
|
||||
view_target: g.point(17),
|
||||
view_height: g.f_or(45, 0.0),
|
||||
twist: g.f_or(51, 0.0),
|
||||
clip: g.handle(340).filter(|h| *h != 0),
|
||||
frozen_layers: tags.iter().filter(|t| matches!(t.code, 331 | 341)).filter_map(|t| parse_handle(&t.str())).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn image(g: &Groups<'_>) -> Image {
|
||||
Image {
|
||||
insert: g.point(10),
|
||||
u: g.point(11),
|
||||
v: g.point(12),
|
||||
size: [g.f_or(13, 0.0), g.f_or(23, 0.0)],
|
||||
image_def: g.handle(340).filter(|h| *h != 0),
|
||||
display: g.i_or(70, 0),
|
||||
clipping: g.i_or(280, 0) != 0,
|
||||
brightness: g.i_or(281, 50),
|
||||
contrast: g.i_or(282, 50),
|
||||
fade: g.i_or(283, 0),
|
||||
clip_type: g.i_or(71, 1),
|
||||
clip: g.points(14).into_iter().map(|p| [p[0], p[1]]).collect(),
|
||||
clip_inverted: g.i_or(290, 0) != 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn corners(g: &Groups<'_>) -> [Point; 4] {
|
||||
[g.point(10), g.point(11), g.point(12), g.point_opt(13).unwrap_or_else(|| g.point(12))]
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
/// The typed form of an entity record.
|
||||
pub fn read(e: &EntityRecord<'_>) -> Entity {
|
||||
let g = e.groups();
|
||||
let line = |g: &Groups<'_>| (g.point(10), g.point(11));
|
||||
let kind = match e.kind {
|
||||
"LINE" => EntityKind::Line { start: line(&g).0, end: line(&g).1 },
|
||||
"POINT" => EntityKind::Point { location: g.point(10), angle: g.f_or(50, 0.0) },
|
||||
"CIRCLE" => EntityKind::Circle { center: g.point(10), radius: g.f_or(40, 0.0) },
|
||||
"ARC" => EntityKind::Arc { center: g.point(10), radius: g.f_or(40, 0.0), start_angle: g.f_or(50, 0.0), end_angle: g.f_or(51, 360.0) },
|
||||
"ELLIPSE" => EntityKind::Ellipse {
|
||||
center: g.point(10),
|
||||
major: g.point(11),
|
||||
ratio: g.f_or(40, 1.0),
|
||||
start: g.f_or(41, 0.0),
|
||||
end: g.f_or(42, std::f64::consts::TAU),
|
||||
},
|
||||
"LWPOLYLINE" => EntityKind::LwPolyline(lwpolyline(e.tags, &g)),
|
||||
"POLYLINE" => EntityKind::Polyline(polyline(&g, &e.followers)),
|
||||
"SPLINE" => EntityKind::Spline(spline(&g)),
|
||||
"RAY" => EntityKind::Ray { base: line(&g).0, direction: line(&g).1 },
|
||||
"XLINE" => EntityKind::XLine { base: line(&g).0, direction: line(&g).1 },
|
||||
"TEXT" => EntityKind::Text(Text::read(&g)),
|
||||
"MTEXT" => EntityKind::MText(MText::read(e.tags)),
|
||||
"ATTDEF" => EntityKind::AttDef(Attrib::read("ATTDEF", e.tags)),
|
||||
"INSERT" => EntityKind::Insert(insert(&g, &e.followers)),
|
||||
"DIMENSION" => EntityKind::Dimension(dimension(&g)),
|
||||
"LEADER" => EntityKind::Leader(Leader {
|
||||
vertices: g.points(10),
|
||||
arrow: g.i_or(71, 1) != 0,
|
||||
spline: g.i_or(72, 0) != 0,
|
||||
style: g.name(3).unwrap_or_else(|| "Standard".into()),
|
||||
annotation_type: g.i_or(73, 3),
|
||||
annotation: g.handle(340).filter(|h| *h != 0),
|
||||
}),
|
||||
"SOLID" => EntityKind::Solid { corners: corners(&g) },
|
||||
"TRACE" => EntityKind::Trace { corners: corners(&g) },
|
||||
"3DFACE" => EntityKind::Face3d { corners: corners(&g), hidden_edges: g.i_or(70, 0) },
|
||||
"HATCH" => Hatch::read(e.tags).map_or(EntityKind::Other, EntityKind::Hatch),
|
||||
"VIEWPORT" => EntityKind::Viewport(viewport(e.tags, &g)),
|
||||
"IMAGE" => EntityKind::Image(image(&g)),
|
||||
"WIPEOUT" => EntityKind::Wipeout(image(&g)),
|
||||
"TOLERANCE" => EntityKind::Tolerance {
|
||||
insert: g.point(10),
|
||||
direction: g.point_opt(11).unwrap_or([1.0, 0.0, 0.0]),
|
||||
style: g.name(3).unwrap_or_else(|| "Standard".into()),
|
||||
text: g.s(1).unwrap_or_default(),
|
||||
},
|
||||
_ => EntityKind::Other,
|
||||
};
|
||||
Entity { common: Common::read(e.tags), kind }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,355 @@
|
||||
//! HATCH: the boundary loops (polylines or edges), the pattern definition, the seed points and
|
||||
//! the gradient, as the file holds them. Boundary points are 2D, in the object coordinate system
|
||||
//! of the hatch at its elevation.
|
||||
|
||||
use super::{Color, Groups, parse_handle};
|
||||
use crate::Tag;
|
||||
|
||||
/// One edge of a boundary loop made of edges.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum HatchEdge {
|
||||
Line {
|
||||
start: [f64; 2],
|
||||
end: [f64; 2],
|
||||
},
|
||||
/// Angles in degrees; a clockwise arc (`ccw` false) has them measured clockwise.
|
||||
Arc {
|
||||
center: [f64; 2],
|
||||
radius: f64,
|
||||
start_angle: f64,
|
||||
end_angle: f64,
|
||||
ccw: bool,
|
||||
},
|
||||
/// `major` is the end of the major axis from the centre, `ratio` the minor axis's length
|
||||
/// over the major's; angles in degrees, as for an arc.
|
||||
Ellipse {
|
||||
center: [f64; 2],
|
||||
major: [f64; 2],
|
||||
ratio: f64,
|
||||
start_angle: f64,
|
||||
end_angle: f64,
|
||||
ccw: bool,
|
||||
},
|
||||
Spline {
|
||||
degree: i64,
|
||||
rational: bool,
|
||||
periodic: bool,
|
||||
knots: Vec<f64>,
|
||||
control: Vec<[f64; 2]>,
|
||||
/// One per control point when rational; else empty.
|
||||
weights: Vec<f64>,
|
||||
fit: Vec<[f64; 2]>,
|
||||
start_tangent: Option<[f64; 2]>,
|
||||
end_tangent: Option<[f64; 2]>,
|
||||
},
|
||||
}
|
||||
|
||||
/// The path of a boundary loop.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum HatchPath {
|
||||
/// Vertices as x, y and bulge.
|
||||
Polyline {
|
||||
closed: bool,
|
||||
vertices: Vec<[f64; 3]>,
|
||||
},
|
||||
Edges(Vec<HatchEdge>),
|
||||
}
|
||||
|
||||
/// A boundary loop.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct HatchLoop {
|
||||
/// Group 92: 1 external, 2 a polyline, 4 derived, 8 a text box, 16 outermost.
|
||||
pub flags: i64,
|
||||
pub path: HatchPath,
|
||||
/// Groups 330 after the loop's 97: the entities an associative hatch follows.
|
||||
pub sources: Vec<u64>,
|
||||
}
|
||||
|
||||
/// One line family of a pattern definition, as drawn: scaled and rotated with the hatch.
|
||||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
pub struct PatternLine {
|
||||
/// Group 53, in degrees.
|
||||
pub angle: f64,
|
||||
/// Groups 43/44: a point the first line passes through.
|
||||
pub base: [f64; 2],
|
||||
/// Groups 45/46: the step from one line to the next, as a vector.
|
||||
pub offset: [f64; 2],
|
||||
/// Groups 49: dash (positive), gap (negative) and dot (0) lengths; empty for a solid line.
|
||||
pub dashes: Vec<f64>,
|
||||
}
|
||||
|
||||
/// A gradient fill (groups 450–470).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Gradient {
|
||||
/// Group 470 (`LINEAR`, `CYLINDER`, `SPHERICAL`...).
|
||||
pub name: String,
|
||||
/// Group 460, in radians.
|
||||
pub angle: f64,
|
||||
/// Group 461: 0 centred, 1 shifted up and left.
|
||||
pub shift: f64,
|
||||
/// Group 452 is 1: one colour, shaded by `tint` (group 462).
|
||||
pub single: bool,
|
||||
pub tint: f64,
|
||||
/// The colour of each 463 record: its true colour (421), else its index (63).
|
||||
pub colors: Vec<Color>,
|
||||
}
|
||||
|
||||
/// A HATCH.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Hatch {
|
||||
/// Group 2, as written (`SOLID` when absent).
|
||||
pub pattern: String,
|
||||
/// Group 70 is 1.
|
||||
pub solid: bool,
|
||||
/// Group 71 is not 0.
|
||||
pub associative: bool,
|
||||
/// Group 75: 0 normal (odd parity), 1 outer, 2 ignore islands.
|
||||
pub style: i64,
|
||||
/// Group 76: 0 user-defined, 1 predefined, 2 custom.
|
||||
pub pattern_type: i64,
|
||||
/// Group 52, in degrees.
|
||||
pub angle: f64,
|
||||
/// Group 41 (1 when absent).
|
||||
pub scale: f64,
|
||||
/// Group 77 is not 0: a user-defined pattern is doubled (crossed).
|
||||
pub double: bool,
|
||||
/// The z of the elevation point (the first group 30).
|
||||
pub elevation: f64,
|
||||
pub loops: Vec<HatchLoop>,
|
||||
pub pattern_lines: Vec<PatternLine>,
|
||||
/// Groups 10/20 after the 98 count.
|
||||
pub seeds: Vec<[f64; 2]>,
|
||||
pub gradient: Option<Gradient>,
|
||||
}
|
||||
|
||||
/// A cursor over a record's tags, for the boundary data, which is read in order.
|
||||
struct Cur<'a> {
|
||||
tags: &'a [Tag],
|
||||
at: usize,
|
||||
}
|
||||
|
||||
impl<'a> Cur<'a> {
|
||||
fn peek(&self) -> Option<&'a Tag> {
|
||||
self.tags.get(self.at)
|
||||
}
|
||||
/// The next tag when its code is `code`, consumed.
|
||||
fn take(&mut self, code: i32) -> Option<&'a Tag> {
|
||||
let t = self.peek().filter(|t| t.code == code)?;
|
||||
self.at += 1;
|
||||
Some(t)
|
||||
}
|
||||
fn f(&mut self, code: i32) -> f64 {
|
||||
self.take(code).map_or(0.0, Tag::f64)
|
||||
}
|
||||
fn xy(&mut self, code: i32) -> [f64; 2] {
|
||||
[self.f(code), self.f(code + 10)]
|
||||
}
|
||||
/// Move to the next `code`; false when there is none.
|
||||
fn seek(&mut self, code: i32) -> bool {
|
||||
while self.peek().is_some_and(|t| t.code != code) {
|
||||
self.at += 1;
|
||||
}
|
||||
self.peek().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// A declared count: never trusted for an allocation, only as the most items to read.
|
||||
fn count(t: &Tag) -> usize {
|
||||
usize::try_from(t.i64()).unwrap_or(0)
|
||||
}
|
||||
|
||||
fn polyline_path(c: &mut Cur<'_>) -> Option<HatchPath> {
|
||||
// 72 has bulges, 73 closed, 93 the vertex count.
|
||||
let mut closed = false;
|
||||
while let Some(t) = c.peek().filter(|t| t.code != 93) {
|
||||
if t.code == 73 {
|
||||
closed = t.i64() != 0;
|
||||
} else if t.code != 72 {
|
||||
return None;
|
||||
}
|
||||
c.at += 1;
|
||||
}
|
||||
let n = count(c.take(93)?);
|
||||
let mut vertices = Vec::new();
|
||||
while vertices.len() < n && c.peek().is_some_and(|t| t.code == 10) {
|
||||
let [x, y] = c.xy(10);
|
||||
vertices.push([x, y, c.f(42)]);
|
||||
}
|
||||
Some(HatchPath::Polyline { closed, vertices })
|
||||
}
|
||||
|
||||
fn spline_edge(c: &mut Cur<'_>) -> HatchEdge {
|
||||
let (mut degree, mut rational, mut periodic, mut knot_count, mut control_count) = (3, false, false, 0, 0);
|
||||
while let Some(t) = c.peek().filter(|t| matches!(t.code, 94 | 73 | 74 | 95 | 96)) {
|
||||
match t.code {
|
||||
94 => degree = t.i64(),
|
||||
73 => rational = t.i64() != 0,
|
||||
74 => periodic = t.i64() != 0,
|
||||
95 => knot_count = count(t),
|
||||
_ => control_count = count(t),
|
||||
}
|
||||
c.at += 1;
|
||||
}
|
||||
let mut knots = Vec::new();
|
||||
while knots.len() < knot_count && c.peek().is_some_and(|t| t.code == 40) {
|
||||
knots.push(c.f(40));
|
||||
}
|
||||
let (mut control, mut weights) = (Vec::new(), Vec::new());
|
||||
while control.len() < control_count && c.peek().is_some_and(|t| t.code == 10) {
|
||||
control.push(c.xy(10));
|
||||
weights.extend(c.take(42).map(Tag::f64));
|
||||
}
|
||||
if weights.len() != control.len() {
|
||||
weights.clear();
|
||||
}
|
||||
// A 97 here is the edge's fit point count (R2010 and later) when fit points follow it, or
|
||||
// when it is 0 and the loop's own 97 comes next; otherwise it is the loop's source count.
|
||||
let mut fit = Vec::new();
|
||||
let next = c.tags.get(c.at + 1).map(|t| t.code);
|
||||
let fit_count = c.peek().filter(|t| t.code == 97 && (next == Some(11) || (t.i64() == 0 && next == Some(97))));
|
||||
if let Some(n) = fit_count.map(count) {
|
||||
c.at += 1;
|
||||
while fit.len() < n && c.peek().is_some_and(|t| t.code == 11) {
|
||||
fit.push(c.xy(11));
|
||||
}
|
||||
}
|
||||
let tangent = |c: &mut Cur<'_>, code: i32| c.peek().is_some_and(|t| t.code == code).then(|| c.xy(code));
|
||||
let start_tangent = tangent(c, 12);
|
||||
let end_tangent = tangent(c, 13);
|
||||
HatchEdge::Spline { degree, rational, periodic, knots, control, weights, fit, start_tangent, end_tangent }
|
||||
}
|
||||
|
||||
fn edge_path(c: &mut Cur<'_>) -> Option<HatchPath> {
|
||||
if !c.seek(93) {
|
||||
return None;
|
||||
}
|
||||
let n = count(c.take(93)?);
|
||||
let mut edges = Vec::new();
|
||||
while edges.len() < n {
|
||||
// Groups this reader does not know may sit between edges; the loop's own end may not.
|
||||
while c.peek().is_some_and(|t| !matches!(t.code, 72 | 97 | 92 | 75)) {
|
||||
c.at += 1;
|
||||
}
|
||||
if c.peek().is_none_or(|t| t.code != 72) {
|
||||
break;
|
||||
}
|
||||
let kind = c.take(72).map_or(0, Tag::i64);
|
||||
// 73 is counter-clockwise unless it says 0.
|
||||
let ccw = |c: &mut Cur<'_>| c.take(73).is_none_or(|t| t.i64() != 0);
|
||||
edges.push(match kind {
|
||||
1 => HatchEdge::Line { start: c.xy(10), end: c.xy(11) },
|
||||
2 => HatchEdge::Arc { center: c.xy(10), radius: c.f(40), start_angle: c.f(50), end_angle: c.f(51), ccw: ccw(c) },
|
||||
3 => HatchEdge::Ellipse { center: c.xy(10), major: c.xy(11), ratio: c.f(40), start_angle: c.f(50), end_angle: c.f(51), ccw: ccw(c) },
|
||||
4 => spline_edge(c),
|
||||
// An edge type the format does not have: its groups are passed over above.
|
||||
_ => continue,
|
||||
});
|
||||
}
|
||||
Some(HatchPath::Edges(edges))
|
||||
}
|
||||
|
||||
fn pattern_lines(tags: &[Tag]) -> Vec<PatternLine> {
|
||||
let Some(at) = tags.iter().position(|t| t.code == 78) else { return Vec::new() };
|
||||
let mut lines: Vec<PatternLine> = Vec::new();
|
||||
for t in tags.iter().skip(at + 1) {
|
||||
if t.code == 53 {
|
||||
lines.push(PatternLine { angle: t.f64(), ..PatternLine::default() });
|
||||
continue;
|
||||
}
|
||||
let Some(l) = lines.last_mut() else { break };
|
||||
match t.code {
|
||||
43 => l.base[0] = t.f64(),
|
||||
44 => l.base[1] = t.f64(),
|
||||
45 => l.offset[0] = t.f64(),
|
||||
46 => l.offset[1] = t.f64(),
|
||||
79 => {}
|
||||
49 => l.dashes.push(t.f64()),
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
/// Groups 450–470, when 450 says gradient. Each 463 starts a colour record whose 63 (index) and
|
||||
/// 421 (true colour) follow.
|
||||
fn gradient(tags: &[Tag]) -> Option<Gradient> {
|
||||
let start = tags.iter().position(|t| t.code == 450)?;
|
||||
if tags.get(start)?.i64() != 1 {
|
||||
return None;
|
||||
}
|
||||
let mut g = Gradient { name: String::new(), angle: 0.0, shift: 0.0, single: false, tint: 0.0, colors: Vec::new() };
|
||||
for t in tags.iter().skip(start + 1).take_while(|t| t.code != 1001) {
|
||||
match t.code {
|
||||
452 => g.single = t.i64() == 1,
|
||||
460 => g.angle = t.f64(),
|
||||
461 => g.shift = t.f64(),
|
||||
462 => g.tint = t.f64(),
|
||||
463 => g.colors.push(Color::ByLayer),
|
||||
63 => {
|
||||
// A true colour already read for this record wins over its index.
|
||||
if let Some(c) = g.colors.last_mut().filter(|c| !matches!(c, Color::Rgb(_))) {
|
||||
*c = Color::from_index(t.i64());
|
||||
}
|
||||
}
|
||||
421 => {
|
||||
if let Some(c) = g.colors.last_mut() {
|
||||
*c = Color::from_true(t.i64());
|
||||
}
|
||||
}
|
||||
470 => g.name = t.str(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Some(g)
|
||||
}
|
||||
|
||||
impl Hatch {
|
||||
/// The typed form of a HATCH record; `None` when it has no boundary data (no group 91) or
|
||||
/// the data ends inside a loop.
|
||||
pub fn read(tags: &[Tag]) -> Option<Hatch> {
|
||||
let g = Groups(tags);
|
||||
let n_loops = count(tags.iter().find(|t| t.code == 91)?);
|
||||
let mut c = Cur { tags, at: tags.iter().position(|t| t.code == 91)? + 1 };
|
||||
let mut loops = Vec::new();
|
||||
while loops.len() < n_loops {
|
||||
if !c.seek(92) {
|
||||
return None;
|
||||
}
|
||||
let flags = c.take(92)?.i64();
|
||||
let path = if flags & 2 != 0 { polyline_path(&mut c)? } else { edge_path(&mut c)? };
|
||||
// 97 counts the source entities (330) that follow.
|
||||
let mut sources = Vec::new();
|
||||
if let Some(n) = c.take(97).map(count) {
|
||||
while sources.len() < n
|
||||
&& let Some(t) = c.take(330)
|
||||
{
|
||||
sources.extend(parse_handle(&t.str()).filter(|h| *h != 0));
|
||||
}
|
||||
}
|
||||
loops.push(HatchLoop { flags, path, sources });
|
||||
}
|
||||
let mut seeds = Vec::new();
|
||||
if c.seek(98) {
|
||||
let n = c.take(98).map_or(0, count);
|
||||
while seeds.len() < n && c.peek().is_some_and(|t| t.code == 10) {
|
||||
seeds.push(c.xy(10));
|
||||
}
|
||||
}
|
||||
let pattern = g.name(2).unwrap_or_else(|| "SOLID".into());
|
||||
Some(Hatch {
|
||||
solid: g.i_or(70, 0) == 1,
|
||||
associative: g.i_or(71, 0) != 0,
|
||||
style: g.i_or(75, 0),
|
||||
pattern_type: g.i_or(76, 1),
|
||||
angle: g.f_or(52, 0.0),
|
||||
scale: g.f_or(41, 1.0),
|
||||
double: g.i_or(77, 0) != 0,
|
||||
elevation: g.f_or(30, 0.0),
|
||||
loops,
|
||||
pattern_lines: pattern_lines(tags),
|
||||
seeds,
|
||||
gradient: gradient(tags),
|
||||
pattern,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
//! Text in DXF: the caret encoding of control characters in strings, and MTEXT with its columns.
|
||||
//!
|
||||
//! Control characters are written as a caret and a letter (`^I` a tab, `^J` a line feed, `^ ` a
|
||||
//! caret). TEXT, ATTDEF, ATTRIB and DIMENSION strings use the same encoding as MTEXT contents,
|
||||
//! without line feeds: AutoCAD reads a bare `^2` in `10^2` as a control character.
|
||||
//!
|
||||
//! MTEXT columns are groups 75 (type), 76 (count), 78 (flow reversed), 79 (auto height), 48
|
||||
//! (width), 49 (gutter) and 50 (column heights, after the column type; before it, 50 is the
|
||||
//! rotation) before R2018, and an embedded object (group 101) from R2018 on.
|
||||
|
||||
use super::{Groups, Point, outside_app};
|
||||
use crate::Tag;
|
||||
|
||||
/// Decode MTEXT contents: `^I` (tab), `^J` (line feed) and `^ ` (caret). Other carets stay as
|
||||
/// they are (older files write stacked tolerances `\S+0.1^-0.1;` with a bare caret).
|
||||
pub fn decode_contents(s: &str) -> String {
|
||||
decode(s, true)
|
||||
}
|
||||
|
||||
/// A single-line string as read: [`decode_contents`] without line feeds (`^J` stays as it is).
|
||||
pub fn decode_line(s: &str) -> String {
|
||||
decode(s, false)
|
||||
}
|
||||
|
||||
fn decode(s: &str, line_feeds: bool) -> String {
|
||||
if !s.contains('^') {
|
||||
return s.to_string();
|
||||
}
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut it = s.chars().peekable();
|
||||
while let Some(c) = it.next() {
|
||||
if c != '^' {
|
||||
out.push(c);
|
||||
continue;
|
||||
}
|
||||
match it.peek().copied() {
|
||||
Some(' ') => out.push('^'),
|
||||
Some('I') => out.push('\t'),
|
||||
Some('J') if line_feeds => out.push('\n'),
|
||||
_ => {
|
||||
out.push('^');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
it.next();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A single-line string (TEXT, ATTDEF/ATTRIB, DIMENSION) for a DXF file: a caret as `^ ` and a
|
||||
/// tab as `^I`, read back by [`decode_line`]. It must hold no line break.
|
||||
pub fn encode_line(s: &str) -> String {
|
||||
s.replace('^', "^ ").replace('\t', "^I")
|
||||
}
|
||||
|
||||
/// Encode MTEXT contents: a tab as `^I`, a line feed as `^J`, a caret as `^ `; other control
|
||||
/// characters (which draw nothing) are dropped.
|
||||
pub fn encode_contents(s: &str) -> String {
|
||||
if !s.chars().any(|c| c == '^' || c.is_control()) {
|
||||
return s.to_string();
|
||||
}
|
||||
let mut out = String::with_capacity(s.len() + 8);
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'^' => out.push_str("^ "),
|
||||
'\t' => out.push_str("^I"),
|
||||
'\n' => out.push_str("^J"),
|
||||
c if c.is_control() => {}
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Apply `f` ([`encode_line`] or [`decode_line`]) to the groups of a `kind` record that hold an
|
||||
/// encoded string: the text (1), and an ATTDEF's prompt (3). An embedded MTEXT (from group 101)
|
||||
/// has its own and is left alone.
|
||||
pub fn map_text_groups(kind: &str, tags: &mut [Tag], f: fn(&str) -> String) {
|
||||
let kind = kind.to_ascii_uppercase();
|
||||
if !matches!(kind.as_str(), "TEXT" | "ATTRIB" | "ATTDEF" | "DIMENSION") {
|
||||
return;
|
||||
}
|
||||
for t in tags.iter_mut().take_while(|t| t.code != 101) {
|
||||
if t.code == 1 || (t.code == 3 && kind == "ATTDEF") {
|
||||
*t = Tag::s(t.code, f(&t.str()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A record's groups split at its embedded object (`101 Embedded Object`): its own groups and,
|
||||
/// when it has one, the object's.
|
||||
pub fn split_embedded(tags: &[Tag]) -> (&[Tag], Option<&[Tag]>) {
|
||||
match tags.iter().position(|t| t.code == 101) {
|
||||
Some(i) => (tags.get(..i).unwrap_or(tags), tags.get(i + 1..)),
|
||||
None => (tags, None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Upper bound on the column heights read from one MTEXT.
|
||||
pub const MAX_COLUMNS: usize = 1_000;
|
||||
|
||||
/// The columns of an MTEXT.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MTextColumns {
|
||||
/// Dynamic columns (type 2): the text flows into as many as it needs. Otherwise static
|
||||
/// (type 1).
|
||||
pub dynamic: bool,
|
||||
pub count: u16,
|
||||
/// One column's width (the MTEXT's own width is the total in some writers' files).
|
||||
pub width: f64,
|
||||
pub gutter: f64,
|
||||
/// The column height: the tallest for manual heights.
|
||||
pub height: f64,
|
||||
pub auto_height: bool,
|
||||
/// Each column's height, for dynamic columns with manual heights; else empty.
|
||||
pub heights: Vec<f64>,
|
||||
pub flow_reversed: bool,
|
||||
}
|
||||
|
||||
fn pos(v: f64) -> f64 {
|
||||
v.max(0.0)
|
||||
}
|
||||
|
||||
impl MTextColumns {
|
||||
/// The columns of an MTEXT record, from the legacy groups or the embedded object.
|
||||
pub fn read(tags: &[Tag]) -> Option<MTextColumns> {
|
||||
let (own, embedded) = split_embedded(tags);
|
||||
// An application-defined group in the record may hold any code, 50 included.
|
||||
let own_tags: Vec<&Tag> = outside_app(own).map(|(_, t)| t).collect();
|
||||
let Some(start) = own_tags.iter().position(|t| t.code == 75) else { return Self::read_embedded(own, embedded?) };
|
||||
let dynamic = match own_tags.get(start).map(|t| t.i64()) {
|
||||
Some(1) => false,
|
||||
Some(2) => true,
|
||||
_ => return None,
|
||||
};
|
||||
let cols = own_tags.get(start..).unwrap_or_default();
|
||||
let first = |code: i32| cols.iter().find(|t| t.code == code).copied();
|
||||
// The first 50 is the number of heights that follow, unless the file has the heights
|
||||
// of group 76 alone.
|
||||
let fifties = cols.iter().filter(|t| t.code == 50);
|
||||
let counted = first(76).is_some_and(|t| i64::try_from(fifties.clone().count()).is_ok_and(|n| n - 1 == t.i64()));
|
||||
let heights: Vec<f64> = fifties.skip(usize::from(counted)).take(MAX_COLUMNS).map(|t| pos(t.f64())).collect();
|
||||
let count = first(76).map_or(heights.len().max(1), |t| usize::try_from(t.i64()).unwrap_or(1)).clamp(1, MAX_COLUMNS);
|
||||
let auto_height = first(79).is_some_and(|t| t.i64() != 0);
|
||||
let defined = own_tags.iter().find(|t| t.code == 46).map_or(0.0, |t| pos(t.f64()));
|
||||
let manual = dynamic && !auto_height;
|
||||
let height =
|
||||
if manual { heights.iter().copied().fold(0.0, f64::max) } else { heights.first().copied().filter(|h| *h > 0.0).unwrap_or(defined) };
|
||||
Some(MTextColumns {
|
||||
dynamic,
|
||||
count: u16::try_from(count).unwrap_or(1),
|
||||
width: first(48).map_or(0.0, |t| pos(t.f64())),
|
||||
gutter: first(49).map_or(0.0, |t| pos(t.f64())),
|
||||
height,
|
||||
auto_height,
|
||||
heights: if manual { heights } else { Vec::new() },
|
||||
flow_reversed: first(78).is_some_and(|t| t.i64() != 0),
|
||||
})
|
||||
}
|
||||
|
||||
/// The columns of an R2018 MTEXT, from its embedded object, whose groups reuse MTEXT's
|
||||
/// codes: 71 type, 72 count, 44 column width, 45 gutter, 73 auto height, 74 flow reversed,
|
||||
/// 46 heights.
|
||||
#[expect(clippy::cast_possible_truncation, reason = "the rounded count is clamped to 1..=MAX_COLUMNS before use")]
|
||||
fn read_embedded(own: &[Tag], obj: &[Tag]) -> Option<MTextColumns> {
|
||||
let first = |code: i32| obj.iter().find(|t| t.code == code);
|
||||
let dynamic = match first(71).map(Tag::i64) {
|
||||
Some(1) => false,
|
||||
Some(2) => true,
|
||||
_ => return None,
|
||||
};
|
||||
let heights: Vec<f64> = obj.iter().filter(|t| t.code == 46).take(MAX_COLUMNS).map(|t| pos(t.f64())).collect();
|
||||
let auto_height = first(73).is_some_and(|t| t.i64() != 0);
|
||||
let manual = dynamic && !auto_height;
|
||||
// The column height: the entity's own 46 (defined height), else the object's 41 or first 46.
|
||||
let own_46 = own.iter().find(|t| t.code == 46).map(|t| pos(t.f64()));
|
||||
let defined = [own_46, first(41).map(Tag::f64), heights.first().copied()].into_iter().flatten().find(|h| *h > 0.0).unwrap_or(0.0);
|
||||
// A dynamic auto-height object stores 0 columns in 72: the count follows from the total
|
||||
// width (42 = n * 44 + (n - 1) * 45).
|
||||
let [width, gutter, total] = [44, 45, 42].map(|g| first(g).map_or(0.0, Tag::f64));
|
||||
let count = match first(72).map(Tag::i64) {
|
||||
Some(n) if n > 0 => n,
|
||||
Some(_) if width + gutter > 0.0 && total > 0.0 => ((total + gutter) / (width + gutter)).round().clamp(1.0, MAX_COLUMNS as f64) as i64,
|
||||
_ => 1,
|
||||
};
|
||||
Some(MTextColumns {
|
||||
dynamic,
|
||||
count: u16::try_from(count.clamp(1, MAX_COLUMNS as i64)).unwrap_or(1),
|
||||
width: pos(width),
|
||||
gutter: pos(gutter),
|
||||
height: if manual { heights.iter().copied().fold(0.0, f64::max) } else { defined },
|
||||
auto_height,
|
||||
heights: if manual { heights } else { Vec::new() },
|
||||
flow_reversed: first(74).is_some_and(|t| t.i64() != 0),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Multiline text: an MTEXT entity, or the one embedded in a multiline attribute.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MText {
|
||||
/// Group 10.
|
||||
pub insert: Point,
|
||||
/// Group 40: the text height.
|
||||
pub height: f64,
|
||||
/// Group 41: the reference rectangle's width (0: none). With columns, see
|
||||
/// [`MTextColumns::width`].
|
||||
pub width: f64,
|
||||
/// Group 71: 1–9, top left to bottom right.
|
||||
pub attach: u8,
|
||||
/// Group 72: 1 left to right, 3 top to bottom, 5 by style.
|
||||
pub drawing_direction: i64,
|
||||
/// Groups 11/21/31: the X axis direction, which wins over the rotation when given and not
|
||||
/// zero.
|
||||
pub direction: Option<Point>,
|
||||
/// Group 50, in degrees.
|
||||
pub rotation: f64,
|
||||
/// Group 7 (`Standard` when absent).
|
||||
pub style: String,
|
||||
/// The contents (the group 3 chunks, then group 1), decoded ([`decode_contents`]); the
|
||||
/// formatting codes (`\P`, `\f...;`) are left as written.
|
||||
pub contents: String,
|
||||
/// Group 44: the line spacing factor (1 when absent).
|
||||
pub line_spacing: f64,
|
||||
/// Group 73 is 2: the spacing is exact, not "at least".
|
||||
pub line_spacing_exact: bool,
|
||||
/// Group 90: the background fill flags (1 a fill colour, 2 the window's colour, 16 a
|
||||
/// frame), with the fill colour (63, 421) and the border offset factor (45).
|
||||
pub background: Option<(i64, super::Color, f64)>,
|
||||
pub columns: Option<MTextColumns>,
|
||||
}
|
||||
|
||||
impl MText {
|
||||
/// The typed form of MTEXT groups.
|
||||
pub fn read(tags: &[Tag]) -> MText {
|
||||
let columns = MTextColumns::read(tags);
|
||||
// The MTEXT's own groups: an embedded object reuses their codes (44 is its column
|
||||
// width, not the line spacing factor).
|
||||
let (own, _) = split_embedded(tags);
|
||||
let g = Groups(own);
|
||||
let mut contents = String::new();
|
||||
for t in own.iter().filter(|t| t.code == 3) {
|
||||
contents.push_str(&t.str());
|
||||
}
|
||||
contents.push_str(&g.s(1).unwrap_or_default());
|
||||
// The direction and rotation come from before the column groups, where 50 is a height.
|
||||
let head = Groups(own.get(..own.iter().position(|t| t.code == 75).unwrap_or(own.len())).unwrap_or(own));
|
||||
let background = g.i(90).filter(|f| *f != 0).map(|f| {
|
||||
let color = match (g.i(421), g.i(63)) {
|
||||
(Some(tc), _) => super::Color::from_true(tc),
|
||||
(None, Some(v)) => super::Color::from_index(v),
|
||||
(None, None) => super::Color::ByLayer,
|
||||
};
|
||||
(f, color, g.f_or(45, 1.5))
|
||||
});
|
||||
MText {
|
||||
insert: g.point(10),
|
||||
height: g.f_or(40, 0.0),
|
||||
width: pos(g.f_or(41, 0.0)),
|
||||
attach: u8::try_from(g.i_or(71, 1).clamp(1, 9)).unwrap_or(1),
|
||||
drawing_direction: g.i_or(72, 1),
|
||||
direction: head.point_opt(11).filter(|d| d[0] != 0.0 || d[1] != 0.0 || d[2] != 0.0),
|
||||
rotation: head.f_or(50, 0.0),
|
||||
style: g.name(7).unwrap_or_else(|| "Standard".into()),
|
||||
contents: decode_contents(&contents),
|
||||
line_spacing: g.f_or(44, 1.0),
|
||||
line_spacing_exact: g.i(73) == Some(2),
|
||||
background,
|
||||
columns,
|
||||
}
|
||||
}
|
||||
/// The angle of the text's X axis in radians: of the direction when the record has one,
|
||||
/// else the rotation.
|
||||
pub fn angle(&self) -> f64 {
|
||||
match self.direction {
|
||||
Some(d) if d[0] != 0.0 || d[1] != 0.0 => d[1].atan2(d[0]),
|
||||
_ => self.rotation.to_radians(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
//! The drawing model's entities: common properties, geometry, text, hatches, dimensions.
|
||||
|
||||
use craft_dxf::model::text::{MTextColumns, decode_contents, decode_line, encode_contents, encode_line, map_text_groups};
|
||||
use craft_dxf::model::{Color, Document, Entity, EntityKind, HatchEdge, HatchPath, Lineweight, Transparency, entity_records, records};
|
||||
use craft_dxf::{Tag, parse};
|
||||
|
||||
fn tags(text: &str) -> Vec<Tag> {
|
||||
let lines: Vec<&str> = text.split(['\n', '|']).map(str::trim).filter(|l| !l.is_empty()).map(|l| if l == "~" { "" } else { l }).collect();
|
||||
parse((lines.join("\n") + "\n").as_bytes()).expect("the test drawing parses")
|
||||
}
|
||||
|
||||
/// The entities of `text`, the records of an ENTITIES section.
|
||||
fn entities(text: &str) -> Vec<Entity> {
|
||||
let t = tags(text);
|
||||
let (recs, strays) = entity_records(&records(&t));
|
||||
assert_eq!(strays, 0);
|
||||
recs.iter().map(Entity::read).collect()
|
||||
}
|
||||
|
||||
fn one(text: &str) -> Entity {
|
||||
let mut e = entities(text);
|
||||
assert_eq!(e.len(), 1);
|
||||
e.remove(0)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn common_properties() {
|
||||
let c = one("0|LINE|5|2A|330|1F|100|AcDbEntity|8|Walls|6|DASHED|62|1|420|65280|370|50|48|2.5|60|1|67|1|440|33554687|390|B|100|AcDbLine|39|1.5|210|0.0|220|0.0|230|-1.0").common;
|
||||
assert_eq!((c.handle, c.owner, c.layer.as_str(), c.linetype.as_deref()), (Some(0x2A), Some(0x1F), "Walls", Some("DASHED")));
|
||||
assert_eq!((c.color, c.lineweight, c.ltscale), (Color::Rgb([0, 255, 0]), Lineweight::Hundredths(50), 2.5));
|
||||
assert!(!c.visible && c.paper_space);
|
||||
assert_eq!((c.transparency, c.thickness, c.extrusion, c.plot_style), (Transparency::Alpha(255), 1.5, [0.0, 0.0, -1.0], Some(0xB)));
|
||||
let d = one("0|LINE").common;
|
||||
assert_eq!((d.handle, d.owner, d.layer.as_str(), d.linetype.as_deref(), d.color), (None, None, "0", None, Color::ByLayer));
|
||||
assert_eq!((d.lineweight, d.ltscale, d.visible, d.paper_space, d.extrusion), (Lineweight::ByLayer, 1.0, true, false, [0.0, 0.0, 1.0]));
|
||||
assert_eq!(one("0|LINE|6|BYLAYER|48|-1.0|62|-2").common, d, "ByLayer by name, a negative scale and a negative colour are the defaults");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entity_properties_are_not_read_from_the_entitys_own_subclass() {
|
||||
// An MTEXT's 48 is its column width, not the linetype scale, and a marker in an
|
||||
// application's braces is the application's: it does not end the entity's groups. A record with no AcDbEntity marker has its
|
||||
// layer wherever it is.
|
||||
let c = one("0|MTEXT|102|{APP|100|AcDbMText|102|}|100|AcDbEntity|8|A|100|AcDbMText|48|7.0|62|3").common;
|
||||
assert_eq!((c.ltscale, c.color, c.layer.as_str()), (1.0, Color::ByLayer, "A"));
|
||||
let c = one("0|LINE|100|AcDbLine|8|B|62|3").common;
|
||||
assert_eq!((c.color, c.layer.as_str()), (Color::Index(3), "B"));
|
||||
// An embedded object ends the entity's groups.
|
||||
assert_eq!(one("0|ATTDEF|8|C|101|Embedded Object|62|5").common.color, Color::ByLayer);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn curves() {
|
||||
let k = |text: &str| one(text).kind;
|
||||
assert_eq!(k("0|LINE|10|1.0|20|2.0|30|3.0|11|4.0|21|5.0"), EntityKind::Line { start: [1.0, 2.0, 3.0], end: [4.0, 5.0, 0.0] });
|
||||
assert_eq!(k("0|POINT|10|1.0|50|30.0"), EntityKind::Point { location: [1.0, 0.0, 0.0], angle: 30.0 });
|
||||
assert_eq!(k("0|CIRCLE|10|1.0|20|2.0|40|3.0"), EntityKind::Circle { center: [1.0, 2.0, 0.0], radius: 3.0 });
|
||||
assert_eq!(k("0|ARC|10|1.0|40|3.0|50|10.0"), EntityKind::Arc { center: [1.0, 0.0, 0.0], radius: 3.0, start_angle: 10.0, end_angle: 360.0 });
|
||||
assert_eq!(
|
||||
k("0|ELLIPSE|10|1.0|11|2.0|21|0.0|40|0.5|41|0.0|42|3.0"),
|
||||
EntityKind::Ellipse { center: [1.0, 0.0, 0.0], major: [2.0, 0.0, 0.0], ratio: 0.5, start: 0.0, end: 3.0 }
|
||||
);
|
||||
assert_eq!(k("0|RAY|10|1.0|11|0.0|21|1.0"), EntityKind::Ray { base: [1.0, 0.0, 0.0], direction: [0.0, 1.0, 0.0] });
|
||||
assert_eq!(k("0|XLINE|10|1.0|11|0.0|21|1.0"), EntityKind::XLine { base: [1.0, 0.0, 0.0], direction: [0.0, 1.0, 0.0] });
|
||||
let EntityKind::Spline(s) = k(
|
||||
"0|SPLINE|70|8|71|2|40|0.0|40|0.0|40|1.0|10|0.0|20|0.0|10|1.0|20|1.0|30|5.0|10|2.0|41|1.0|41|2.0|41|1.0|11|9.0|21|9.0|12|1.0|22|0.0|44|0.01",
|
||||
) else {
|
||||
panic!("a spline")
|
||||
};
|
||||
assert_eq!((s.degree, s.flags, s.knots.len(), s.weights.as_slice(), s.fit_tolerance), (2, 8, 3, [1.0, 2.0, 1.0].as_slice(), 0.01));
|
||||
assert_eq!(s.control, [[0.0, 0.0, 0.0], [1.0, 1.0, 5.0], [2.0, 0.0, 0.0]], "the third point has no y or z of its own");
|
||||
assert_eq!((s.fit.as_slice(), s.start_tangent, s.end_tangent), ([[9.0, 9.0, 0.0]].as_slice(), Some([1.0, 0.0, 0.0]), None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polylines() {
|
||||
let EntityKind::LwPolyline(p) = one("0|LWPOLYLINE|90|3|70|129|43|0.5|38|2.0|10|0.0|20|0.0|42|1.0|10|4.0|20|0.0|40|0.1|41|0.2|10|4.0|20|3.0").kind
|
||||
else {
|
||||
panic!("a lightweight polyline")
|
||||
};
|
||||
assert!(p.closed && p.plinegen);
|
||||
assert_eq!((p.const_width, p.elevation, p.vertices.len()), (0.5, 2.0, 3));
|
||||
assert_eq!((p.vertices[0].bulge, p.vertices[1].start_width, p.vertices[1].end_width, p.vertices[2].y), (1.0, 0.1, 0.2, 3.0));
|
||||
// A polyface mesh: three vertices and a face record that names them.
|
||||
let e = one(
|
||||
"0|POLYLINE|66|1|70|64|71|3|72|1|10|0.0|20|0.0|30|7.0|0|VERTEX|10|0.0|20|0.0|30|0.0|70|192|0|VERTEX|10|1.0|70|192|0|VERTEX|10|1.0|20|1.0|70|192|0|VERTEX|70|128|71|1|72|2|73|-3|0|SEQEND",
|
||||
);
|
||||
let EntityKind::Polyline(m) = e.kind else { panic!("a polyline") };
|
||||
assert!(m.is_polyface() && !m.is_mesh() && !m.is_3d() && !m.closed());
|
||||
assert_eq!((m.m, m.n, m.elevation, m.vertices.len()), (3, 1, 7.0, 4));
|
||||
assert!(m.vertices[3].is_face() && !m.vertices[0].is_face());
|
||||
assert_eq!(m.vertices[3].face, [1, 2, -3, 0]);
|
||||
let EntityKind::Polyline(h) = one("0|POLYLINE|70|4|40|0.5|41|0.25|0|VERTEX|10|1.0|42|0.5|40|0.1|41|0.2|70|16|0|SEQEND").kind else {
|
||||
panic!("a polyline")
|
||||
};
|
||||
assert_eq!((h.start_width, h.end_width), (0.5, 0.25));
|
||||
let v = h.vertices[0];
|
||||
assert!(v.is_spline_frame());
|
||||
assert_eq!((v.bulge, v.start_width, v.end_width), (0.5, 0.1, 0.2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_and_attributes() {
|
||||
let EntityKind::Text(t) = one("0|TEXT|10|1.0|20|2.0|11|3.0|21|4.0|40|2.5|1|10^ 2^Im|50|90.0|41|0.8|51|15.0|7|Notes|71|2|72|1|73|2").kind else {
|
||||
panic!("text")
|
||||
};
|
||||
assert_eq!((t.insert, t.align, t.height, t.value.as_str()), ([1.0, 2.0, 0.0], Some([3.0, 4.0, 0.0]), 2.5, "10^2\tm"));
|
||||
assert_eq!((t.rotation, t.width_factor, t.oblique, t.style.as_str(), t.generation, t.halign, t.valign), (90.0, 0.8, 15.0, "Notes", 2, 1, 2));
|
||||
let EntityKind::Text(d) = one("0|TEXT|1|x").kind else { panic!("text") };
|
||||
assert_eq!((d.align, d.width_factor, d.style.as_str(), d.halign, d.valign), (None, 1.0, "Standard", 0, 0));
|
||||
|
||||
let EntityKind::AttDef(a) =
|
||||
one("0|ATTDEF|100|AcDbText|10|1.0|1|default|100|AcDbAttributeDefinition|280|0|3|Enter^Ia|2|TAG|70|12|74|3|280|1").kind
|
||||
else {
|
||||
panic!("an attribute definition")
|
||||
};
|
||||
assert_eq!((a.tag.as_str(), a.prompt.as_str(), a.flags, a.text.valign, a.lock_position), ("TAG", "Enter\ta", 12, 3, true));
|
||||
assert!(a.multiline.is_none());
|
||||
|
||||
// A block reference with two attributes; the second is multiline.
|
||||
let EntityKind::Insert(i) = one("0|INSERT|66|1|2|DOOR|10|1.0|20|2.0|41|2.0|43|-1.0|50|45.0|70|3|71|0|44|5.0|45|6.0
|
||||
0|ATTRIB|8|Tags|62|2|1|one|2|A|70|1|280|0
|
||||
0|ATTRIB|1|~|2|B|101|Embedded Object|10|7.0|40|2.0|71|5|3|line one^J|1|line two
|
||||
0|SEQEND")
|
||||
.kind
|
||||
else {
|
||||
panic!("an insert")
|
||||
};
|
||||
assert_eq!((i.block.as_str(), i.insert, i.scale, i.rotation), ("DOOR", [1.0, 2.0, 0.0], [2.0, 1.0, -1.0], 45.0));
|
||||
assert_eq!((i.columns, i.rows, i.column_spacing, i.row_spacing), (3, 1, 5.0, 6.0));
|
||||
assert_eq!(i.attribs.len(), 2);
|
||||
let (c, a) = &i.attribs[0];
|
||||
assert_eq!(
|
||||
(c.layer.as_str(), c.color, a.tag.as_str(), a.text.value.as_str(), a.flags, a.lock_position),
|
||||
("Tags", Color::Index(2), "A", "one", 1, false)
|
||||
);
|
||||
assert!(a.prompt.is_empty(), "an ATTRIB has no prompt");
|
||||
let m = i.attribs[1].1.multiline.as_ref().expect("embedded MTEXT");
|
||||
assert_eq!((m.contents.as_str(), m.insert, m.height, m.attach), ("line one\nline two", [7.0, 0.0, 0.0], 2.0, 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caret_encoding() {
|
||||
assert_eq!(decode_contents("a^Ib^Jc^ d^2^"), "a\tb\nc^d^2^");
|
||||
assert_eq!(decode_line("a^Ib^Jc^ d"), "a\tb^Jc^d", "a single line has no line feed");
|
||||
assert_eq!(encode_contents("a\tb\nc^d\u{7}"), "a^Ib^Jc^ d", "other control characters are dropped");
|
||||
assert_eq!(encode_line("10^2\t"), "10^ 2^I");
|
||||
for s in ["", "plain", "10^2", "tab\there", "^^ ^I"] {
|
||||
assert_eq!(decode_line(&encode_line(s)), s);
|
||||
assert_eq!(decode_contents(&encode_contents(s)), s);
|
||||
}
|
||||
let mut t = vec![Tag::s(1, "a^ b"), Tag::s(3, "p^ q"), Tag::i(101, 0), Tag::s(1, "e^ f")];
|
||||
map_text_groups("attdef", &mut t, decode_line);
|
||||
assert_eq!(t.iter().map(Tag::str).collect::<Vec<_>>(), ["a^b", "p^q", "0", "e^ f"], "the embedded object's text is left alone");
|
||||
let mut t = vec![Tag::s(1, "a^ b"), Tag::s(3, "p^ q")];
|
||||
map_text_groups("TEXT", &mut t, decode_line);
|
||||
assert_eq!(t[1].str(), "p^ q", "only an ATTDEF's 3 is a prompt");
|
||||
map_text_groups("LINE", &mut t, |_| String::new());
|
||||
assert_eq!(t[0].str(), "a^b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mtext() {
|
||||
let EntityKind::MText(m) = one("0|MTEXT|10|1.0|20|2.0|40|2.5|41|40.0|71|5|72|1|3|first ^Ipart, |3|second, |1|last\\Pline|7|Notes|11|0.0|21|1.0|50|30.0|44|1.2|73|2|90|1|63|4|45|1.25").kind else {
|
||||
panic!("multiline text")
|
||||
};
|
||||
assert_eq!((m.insert, m.height, m.width, m.attach, m.style.as_str()), ([1.0, 2.0, 0.0], 2.5, 40.0, 5, "Notes"));
|
||||
assert_eq!(m.contents, "first \tpart,second,last\\Pline");
|
||||
assert_eq!((m.direction, m.rotation, m.line_spacing, m.line_spacing_exact), (Some([0.0, 1.0, 0.0]), 30.0, 1.2, true));
|
||||
assert!((m.angle() - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "the direction wins over the rotation");
|
||||
assert_eq!(m.background, Some((1, Color::Index(4), 1.25)));
|
||||
assert!(m.columns.is_none());
|
||||
let EntityKind::MText(r) = one("0|MTEXT|1|x|50|90.0|11|0.0|21|0.0|71|99").kind else { panic!("multiline text") };
|
||||
assert!((r.angle() - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "a zero direction is none");
|
||||
assert_eq!((r.attach, r.background, r.line_spacing), (9, None, 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mtext_columns() {
|
||||
// Legacy groups: static, 2 columns, the first 50 after 75 counts the heights; the 50 before
|
||||
// 75 is the rotation.
|
||||
let t = tags("0|MTEXT|10|0.0|50|15.0|1|x|75|1|76|2|78|1|79|0|48|30.0|49|5.0|50|2|50|80.0|50|80.0");
|
||||
let c = MTextColumns::read(&t[1..]).expect("columns");
|
||||
assert_eq!((c.dynamic, c.count, c.width, c.gutter, c.height, c.auto_height, c.flow_reversed), (false, 2, 30.0, 5.0, 80.0, false, true));
|
||||
assert!(c.heights.is_empty(), "only manual dynamic columns keep each height");
|
||||
let EntityKind::MText(m) = one("0|MTEXT|10|0.0|50|15.0|1|x|75|1|76|2|48|30.0|49|5.0|50|2|50|80.0|50|80.0").kind else { panic!("multiline text") };
|
||||
assert_eq!(m.rotation, 15.0);
|
||||
// Dynamic with manual heights, and no count before them.
|
||||
let t = tags("0|MTEXT|1|x|75|2|76|3|79|0|48|30.0|49|5.0|50|10.0|50|40.0|50|20.0");
|
||||
let c = MTextColumns::read(&t[1..]).expect("columns");
|
||||
assert_eq!((c.dynamic, c.count, c.height, c.heights.as_slice()), (true, 3, 40.0, [10.0, 40.0, 20.0].as_slice()));
|
||||
// R2018: the embedded object; 72 is 0 for dynamic auto-height, the count follows the widths.
|
||||
let t = tags("0|MTEXT|1|x|46|50.0|101|Embedded Object|70|1|10|1.0|11|0.0|40|0.0|41|0.0|42|65.0|43|0.0|71|2|72|0|44|30.0|45|5.0|73|1|74|0");
|
||||
let c = MTextColumns::read(&t[1..]).expect("columns");
|
||||
assert_eq!((c.dynamic, c.count, c.width, c.gutter, c.height, c.auto_height), (true, 2, 30.0, 5.0, 50.0, true));
|
||||
// No columns: type 0, or no column groups at all.
|
||||
assert!(MTextColumns::read(&tags("0|MTEXT|1|x|75|0|76|0")[1..]).is_none());
|
||||
assert!(MTextColumns::read(&tags("0|MTEXT|1|x|101|Embedded Object|71|0")[1..]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_column_height_limit() {
|
||||
let mut text = String::from("0|MTEXT|1|x|75|2|76|5000|79|0");
|
||||
for _ in 0..5000 {
|
||||
text.push_str("|50|1.0");
|
||||
}
|
||||
let t = tags(&text);
|
||||
let c = MTextColumns::read(&t[1..]).expect("columns");
|
||||
assert_eq!((c.heights.len(), c.count), (1000, 1000));
|
||||
let mut text = String::from("0|MTEXT|1|x|101|Embedded Object|71|2|72|40000|73|0");
|
||||
for _ in 0..5000 {
|
||||
text.push_str("|46|1.0");
|
||||
}
|
||||
let t = tags(&text);
|
||||
let c = MTextColumns::read(&t[1..]).expect("columns");
|
||||
assert_eq!((c.heights.len(), c.count), (1000, 1000));
|
||||
// A total width that asks for a count beyond any number.
|
||||
let t = tags("0|MTEXT|1|x|101|Embedded Object|71|2|72|0|73|1|42|1e300|44|1e-300|45|0.0");
|
||||
assert_eq!(MTextColumns::read(&t[1..]).expect("columns").count, 1000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dimensions_and_leaders() {
|
||||
let EntityKind::Dimension(d) =
|
||||
one("0|DIMENSION|2|*D1|3|ISO-25|10|1.0|20|2.0|11|3.0|21|4.0|12|0.5|13|5.0|14|6.0|15|7.0|16|8.0|70|161|1|<> ^Im|42|12.5|50|30.0|52|60.0|53|45.0|40|2.0").kind
|
||||
else {
|
||||
panic!("a dimension")
|
||||
};
|
||||
assert_eq!((d.kind(), d.flags & 128, d.flags & 32), (1, 128, 32));
|
||||
assert_eq!((d.block.as_deref(), d.style.as_str(), d.text.as_str(), d.measurement), (Some("*D1"), "ISO-25", "<> \tm", Some(12.5)));
|
||||
assert_eq!((d.defpt, d.text_mid, d.insertion[0]), ([1.0, 2.0, 0.0], [3.0, 4.0, 0.0], 0.5));
|
||||
assert_eq!((d.p13[0], d.p14[0], d.p15[0], d.p16[0]), (5.0, 6.0, 7.0, 8.0));
|
||||
assert_eq!((d.rotation, d.oblique, d.text_rotation, d.leader_length), (30.0, 60.0, 45.0, 2.0));
|
||||
let EntityKind::Dimension(n) = one("0|DIMENSION|70|6").kind else { panic!("a dimension") };
|
||||
assert_eq!((n.block.as_deref(), n.style.as_str(), n.measurement, n.kind()), (None, "Standard", None, 6));
|
||||
|
||||
// The second vertex has no z: it does not take the third's.
|
||||
let EntityKind::Leader(l) = one("0|LEADER|3|Arrows|71|0|72|1|73|0|340|4F|10|1.0|20|2.0|30|3.0|10|4.0|20|5.0|10|6.0|20|7.0|30|8.0").kind else {
|
||||
panic!("a leader")
|
||||
};
|
||||
assert_eq!(l.vertices, [[1.0, 2.0, 3.0], [4.0, 5.0, 0.0], [6.0, 7.0, 8.0]]);
|
||||
assert!(!l.arrow && l.spline);
|
||||
assert_eq!((l.style.as_str(), l.annotation_type, l.annotation), ("Arrows", 0, Some(0x4F)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn faces_viewports_images_and_tolerances() {
|
||||
let k = |text: &str| one(text).kind;
|
||||
let three = "|10|0.0|11|1.0|12|1.0|22|1.0";
|
||||
assert_eq!(k(&format!("0|SOLID{three}")), EntityKind::Solid { corners: [[0.0; 3], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [1.0, 1.0, 0.0]] });
|
||||
assert!(matches!(k(&format!("0|TRACE{three}|13|9.0")), EntityKind::Trace { corners } if corners[3] == [9.0, 0.0, 0.0]));
|
||||
assert!(matches!(k(&format!("0|3DFACE{three}|70|5")), EntityKind::Face3d { hidden_edges: 5, .. }));
|
||||
let EntityKind::Viewport(v) =
|
||||
k("0|VIEWPORT|10|5.0|20|4.0|40|8.0|41|6.0|69|2|90|81920|12|1.0|22|2.0|17|9.0|45|30.0|51|90.0|340|A1|331|B1|331|B2|341|B3")
|
||||
else {
|
||||
panic!("a viewport")
|
||||
};
|
||||
assert_eq!((v.center, v.width, v.height, v.id, v.status & 16384, v.status & 131_072), ([5.0, 4.0, 0.0], 8.0, 6.0, 2, 16384, 0));
|
||||
assert_eq!((v.view_center, v.view_target, v.view_direction, v.view_height, v.twist), ([1.0, 2.0], [9.0, 0.0, 0.0], [0.0, 0.0, 1.0], 30.0, 90.0));
|
||||
assert_eq!((v.clip, v.frozen_layers.as_slice()), (Some(0xA1), [0xB1, 0xB2, 0xB3].as_slice()));
|
||||
let EntityKind::Image(i) =
|
||||
k("0|IMAGE|10|1.0|11|0.5|12|0.0|22|0.5|13|640.0|23|480.0|340|C1|70|7|280|1|281|60|71|2|14|0.0|24|0.0|14|10.0|24|0.0|14|10.0|24|10.0|290|1")
|
||||
else {
|
||||
panic!("an image")
|
||||
};
|
||||
assert_eq!((i.insert[0], i.u[0], i.v[1], i.size, i.image_def), (1.0, 0.5, 0.5, [640.0, 480.0], Some(0xC1)));
|
||||
assert_eq!((i.display, i.clipping, i.brightness, i.contrast, i.fade, i.clip_type, i.clip_inverted), (7, true, 60, 50, 0, 2, true));
|
||||
assert_eq!(i.clip, [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0]]);
|
||||
assert!(matches!(k("0|WIPEOUT|10|1.0|14|-0.5|24|-0.5|14|0.5|24|0.5"), EntityKind::Wipeout(w) if w.clip.len() == 2 && w.clip_type == 1));
|
||||
assert_eq!(
|
||||
k("0|TOLERANCE|3|Tol|10|1.0|1|{\\Fgdt;j}%%v0.1"),
|
||||
EntityKind::Tolerance { insert: [1.0, 0.0, 0.0], direction: [1.0, 0.0, 0.0], style: "Tol".into(), text: "{\\Fgdt;j}%%v0.1".into() }
|
||||
);
|
||||
assert_eq!(k("0|MULTILEADER|10|1.0"), EntityKind::Other, "a type the model does not have is read from its tags");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hatch_with_a_polyline_loop_and_a_pattern() {
|
||||
let EntityKind::Hatch(h) = one("0|HATCH|10|0.0|20|0.0|30|2.5|2|ANSI31|70|0|71|1|91|1
|
||||
92|7|72|1|73|1|93|3|10|0.0|20|0.0|42|0.5|10|4.0|20|0.0|42|0.0|10|4.0|20|3.0|42|0.0|97|2|330|A1|330|A2
|
||||
75|1|76|1|52|45.0|41|2.0|77|0|78|2
|
||||
53|45.0|43|0.0|44|0.0|45|-1.4|46|1.4|79|0
|
||||
53|135.0|43|1.0|44|2.0|45|1.0|46|1.0|79|2|49|3.0|49|-1.5
|
||||
98|2|10|1.0|20|1.0|10|2.0|20|2.0")
|
||||
.kind
|
||||
else {
|
||||
panic!("a hatch")
|
||||
};
|
||||
assert_eq!((h.pattern.as_str(), h.solid, h.associative, h.style, h.pattern_type), ("ANSI31", false, true, 1, 1));
|
||||
assert_eq!((h.angle, h.scale, h.double, h.elevation), (45.0, 2.0, false, 2.5));
|
||||
assert_eq!(h.loops.len(), 1);
|
||||
let l = &h.loops[0];
|
||||
assert_eq!((l.flags, l.sources.as_slice()), (7, [0xA1, 0xA2].as_slice()));
|
||||
assert_eq!(l.path, HatchPath::Polyline { closed: true, vertices: vec![[0.0, 0.0, 0.5], [4.0, 0.0, 0.0], [4.0, 3.0, 0.0]] });
|
||||
assert_eq!(h.pattern_lines.len(), 2);
|
||||
assert_eq!((h.pattern_lines[0].angle, h.pattern_lines[0].offset, h.pattern_lines[0].dashes.len()), (45.0, [-1.4, 1.4], 0));
|
||||
assert_eq!((h.pattern_lines[1].base, h.pattern_lines[1].dashes.as_slice()), ([1.0, 2.0], [3.0, -1.5].as_slice()));
|
||||
assert_eq!(h.seeds, [[1.0, 1.0], [2.0, 2.0]]);
|
||||
assert!(h.gradient.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hatch_with_edge_loops_and_a_gradient() {
|
||||
let EntityKind::Hatch(h) = one(
|
||||
"0|HATCH|2|SOLID|70|1|71|0|91|2
|
||||
92|1|93|4
|
||||
72|1|10|0.0|20|0.0|11|4.0|21|0.0
|
||||
72|2|10|4.0|20|1.0|40|1.0|50|270.0|51|360.0|73|1
|
||||
72|3|10|2.0|20|2.0|11|2.0|21|0.0|40|0.5|50|0.0|51|180.0|73|0
|
||||
72|4|94|3|73|1|74|0|95|8|96|4|40|0.0|40|0.0|40|0.0|40|0.0|40|1.0|40|1.0|40|1.0|40|1.0|10|0.0|20|2.0|42|1.0|10|0.0|20|1.5|42|2.0|10|0.0|20|0.5|42|2.0|10|0.0|20|0.0|42|1.0|97|2|11|0.0|21|2.0|11|0.0|21|0.0|12|0.0|22|-1.0|13|0.0|23|-1.0
|
||||
97|0
|
||||
92|16|93|1
|
||||
72|2|10|2.0|20|1.0|40|0.25|50|0.0|51|360.0|73|1
|
||||
97|1|330|B1
|
||||
75|0|76|1|98|0
|
||||
450|1|451|0|460|0.785|461|0.0|452|0|462|0.0|453|2|463|0.0|63|5|421|255|463|1.0|63|2|470|LINEAR
|
||||
1001|ACAD|1010|0.0",
|
||||
)
|
||||
.kind
|
||||
else {
|
||||
panic!("a hatch")
|
||||
};
|
||||
assert!(h.solid && !h.associative);
|
||||
assert_eq!(h.loops.len(), 2);
|
||||
let HatchPath::Edges(e) = &h.loops[0].path else { panic!("edges") };
|
||||
assert_eq!(e.len(), 4);
|
||||
assert_eq!(e[0], HatchEdge::Line { start: [0.0, 0.0], end: [4.0, 0.0] });
|
||||
assert_eq!(e[1], HatchEdge::Arc { center: [4.0, 1.0], radius: 1.0, start_angle: 270.0, end_angle: 360.0, ccw: true });
|
||||
assert_eq!(e[2], HatchEdge::Ellipse { center: [2.0, 2.0], major: [2.0, 0.0], ratio: 0.5, start_angle: 0.0, end_angle: 180.0, ccw: false });
|
||||
let HatchEdge::Spline { degree, rational, periodic, knots, control, weights, fit, start_tangent, end_tangent } = &e[3] else {
|
||||
panic!("a spline edge")
|
||||
};
|
||||
assert_eq!((*degree, *rational, *periodic, knots.len(), control.len()), (3, true, false, 8, 4));
|
||||
assert_eq!(
|
||||
(weights.as_slice(), fit.len(), *start_tangent, *end_tangent),
|
||||
([1.0, 2.0, 2.0, 1.0].as_slice(), 2, Some([0.0, -1.0]), Some([0.0, -1.0]))
|
||||
);
|
||||
// The spline's own 97 (fit points) is not the loop's source count.
|
||||
assert!(h.loops[0].sources.is_empty());
|
||||
assert_eq!((h.loops[1].flags, h.loops[1].sources.as_slice()), (16, [0xB1].as_slice()));
|
||||
let g = h.gradient.expect("a gradient");
|
||||
assert_eq!((g.name.as_str(), g.angle, g.shift, g.single), ("LINEAR", 0.785, 0.0, false));
|
||||
assert_eq!(g.colors, [Color::Rgb([0, 0, 255]), Color::Index(2)], "a true colour wins over its index");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn damaged_hatches() {
|
||||
let k = |text: &str| one(text).kind;
|
||||
assert_eq!(k("0|HATCH|2|SOLID"), EntityKind::Other, "no boundary data");
|
||||
assert_eq!(k("0|HATCH|91|2|92|2|93|1|10|0.0|20|0.0"), EntityKind::Other, "the second loop is missing");
|
||||
assert_eq!(k("0|HATCH|91|1|92|2|8|x|93|1"), EntityKind::Other, "a foreign group in the loop header");
|
||||
// Counts larger than the data: what is there is read, nothing is reserved for the rest.
|
||||
let EntityKind::Hatch(h) = k("0|HATCH|91|1|92|2|93|2000000000|10|1.0|20|2.0|97|2000000000|330|A|98|2000000000|10|5.0|20|6.0") else {
|
||||
panic!("a hatch")
|
||||
};
|
||||
assert_eq!(h.loops[0].path, HatchPath::Polyline { closed: false, vertices: vec![[1.0, 2.0, 0.0]] });
|
||||
assert_eq!((h.loops[0].sources.as_slice(), h.seeds.as_slice()), ([0xA].as_slice(), [[5.0, 6.0]].as_slice()));
|
||||
let EntityKind::Hatch(h) = k("0|HATCH|91|1|92|0|93|9|72|9|10|1.0|72|1|10|1.0|20|1.0|11|2.0|21|2.0|72|4|95|-5|96|2000000000|10|1.0|20|1.0") else {
|
||||
panic!("a hatch")
|
||||
};
|
||||
let HatchPath::Edges(e) = &h.loops[0].path else { panic!("edges") };
|
||||
assert_eq!(e.len(), 2, "an edge type the format does not have is passed over");
|
||||
assert!(matches!(&e[1], HatchEdge::Spline { knots, control, .. } if knots.is_empty() && control.len() == 1));
|
||||
assert_eq!(k("0|HATCH|91|-4").clone(), k("0|HATCH|91|0"), "a negative loop count is none");
|
||||
// A 450 that says no gradient.
|
||||
assert!(matches!(k("0|HATCH|91|0|450|0|463|0.0|63|1"), EntityKind::Hatch(h) if h.gradient.is_none()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entities_of_a_whole_drawing() {
|
||||
let t = tags(
|
||||
"0|SECTION|2|BLOCKS|0|BLOCK|2|B|0|CIRCLE|40|2.0|0|ENDBLK|0|ENDSEC|0|SECTION|2|ENTITIES|0|INSERT|2|B|0|POLYLINE|0|VERTEX|10|1.0|0|SEQEND|0|ENDSEC|0|EOF",
|
||||
);
|
||||
let d = Document::read(&t);
|
||||
let kinds: Vec<EntityKind> = d.entities.iter().chain(&d.blocks[0].entities).map(|e| Entity::read(e).kind).collect();
|
||||
assert!(matches!(kinds.as_slice(), [EntityKind::Insert(_), EntityKind::Polyline(p), EntityKind::Circle { .. }] if p.vertices.len() == 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_spline_edge_without_fit_data_leaves_the_loops_source_count() {
|
||||
// Before R2010 a spline edge has no 97 of its own: the one after it counts the loop's sources.
|
||||
let EntityKind::Hatch(h) = one(
|
||||
"0|HATCH|91|1|92|1|93|1|72|4|94|1|73|0|74|0|95|4|96|2|40|0.0|40|0.0|40|1.0|40|1.0|10|0.0|20|0.0|10|1.0|20|1.0|97|2|330|A1|330|A2|75|0|98|0",
|
||||
)
|
||||
.kind
|
||||
else {
|
||||
panic!("a hatch")
|
||||
};
|
||||
assert_eq!(h.loops[0].sources, [0xA1, 0xA2]);
|
||||
assert!(
|
||||
matches!(&h.loops[0].path, HatchPath::Edges(e) if matches!(&e[0], HatchEdge::Spline { fit, control, .. } if fit.is_empty() && control.len() == 2))
|
||||
);
|
||||
}
|
||||
@@ -13,8 +13,9 @@ workspace = true
|
||||
[dependencies]
|
||||
craft-xml = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
# `deflate-flate2` with flate2's default miniz_oxide backend, as craft-update pins it (zip's own
|
||||
# `deflate` feature would switch a whole app's flate2 to zlib-rs).
|
||||
craft-zip = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# The tests build hostile archives by hand.
|
||||
zip = { version = "9", default-features = false, features = ["deflate-flate2"] }
|
||||
# The backend of zip's deflate: `deflate-flate2` leaves the choice to the build.
|
||||
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
||||
craft-zip = { workspace = true, features = ["test-support"] }
|
||||
|
||||
@@ -94,7 +94,8 @@ for path in &spine {
|
||||
| `max_total_bytes` (all entries read from one package) | 256 MiB | `Error::TooLarge` |
|
||||
| `max_manifest`, `max_spine` | 10,000 each | further items are ignored |
|
||||
|
||||
Sizes are checked against what an entry really expands to, not what the archive declares. The two
|
||||
The ZIP layer is craft-zip: the declared entry count is checked before the archive is indexed, and
|
||||
sizes are checked against what an entry really expands to, not what the archive declares. The two
|
||||
XML documents are read with craft-xml's limits (depth 256, 2,000,000 nodes) and `max_entry_bytes`.
|
||||
|
||||
## Where it came from
|
||||
|
||||
+20
-26
@@ -1,7 +1,6 @@
|
||||
//! An EPUB 2 or 3 package, read: container, package document, and bounded entry reads.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Cursor, Read};
|
||||
|
||||
use craft_xml::{El, Local, Options};
|
||||
|
||||
@@ -224,10 +223,8 @@ pub fn parse_package(xml: &[u8], opf_dir: &str, limits: &Limits) -> Result<Packa
|
||||
/// An open EPUB package. Entries are decompressed only when asked for, within the byte budgets
|
||||
/// of its [`Limits`].
|
||||
pub struct Epub<'a> {
|
||||
zip: zip::ZipArchive<Cursor<&'a [u8]>>,
|
||||
zip: craft_zip::Archive<'a>,
|
||||
limits: Limits,
|
||||
/// Bytes the entries still to be read may expand to.
|
||||
left: u64,
|
||||
/// The archive path of the package document.
|
||||
pub opf_path: String,
|
||||
pub package: Package,
|
||||
@@ -242,11 +239,16 @@ impl<'a> Epub<'a> {
|
||||
/// Reads the container and the package document. The `mimetype` entry is not required:
|
||||
/// packages without one, or with it compressed, are common.
|
||||
pub fn open_with(bytes: &'a [u8], limits: Limits) -> Result<Self, Error> {
|
||||
let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| Error::NotEpub(e.to_string()))?;
|
||||
if zip.len() > limits.max_entries {
|
||||
return Err(Error::TooManyEntries { found: zip.len(), limit: limits.max_entries });
|
||||
}
|
||||
let mut epub = Epub { zip, limits, left: limits.max_total_bytes, opf_path: String::new(), package: Package::default() };
|
||||
let zip =
|
||||
craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries as u64, max_total_bytes: limits.max_total_bytes })
|
||||
.map_err(|e| match e {
|
||||
craft_zip::Error::TooManyEntries { declared, limit } => Error::TooManyEntries {
|
||||
found: usize::try_from(declared).unwrap_or(usize::MAX),
|
||||
limit: usize::try_from(limit).unwrap_or(usize::MAX),
|
||||
},
|
||||
e => Error::NotEpub(e.to_string()),
|
||||
})?;
|
||||
let mut epub = Epub { zip, limits, opf_path: String::new(), package: Package::default() };
|
||||
let container = epub.read("META-INF/container.xml")?.ok_or_else(|| Error::NotEpub("META-INF/container.xml is missing".into()))?;
|
||||
let opf_path = parse_container(&container)?.ok_or_else(|| Error::NotEpub("container.xml names no package document".into()))?;
|
||||
// The container gives a path from the archive's root; resolving it also drops a leading `/`.
|
||||
@@ -262,28 +264,20 @@ impl<'a> Epub<'a> {
|
||||
|
||||
/// Does the archive have an entry `path`?
|
||||
pub fn contains(&self, path: &str) -> bool {
|
||||
self.zip.index_for_name(path).is_some()
|
||||
self.zip.contains(path)
|
||||
}
|
||||
|
||||
/// The bytes of archive entry `path`, or `None` when the archive has no such entry. An entry
|
||||
/// past the limits is an error, and so is one that expands to more than it declared.
|
||||
pub fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>, Error> {
|
||||
let Some(index) = self.zip.index_for_name(path).filter(|_| !path.ends_with('/')) else { return Ok(None) };
|
||||
let part = |msg: String| Error::Part { part: path.to_string(), msg };
|
||||
let f = self.zip.by_index(index).map_err(|e| part(e.to_string()))?;
|
||||
let limit = self.limits.max_entry_bytes.min(self.left);
|
||||
let too_large = || Error::TooLarge { part: path.to_string(), limit };
|
||||
if f.size() > limit {
|
||||
return Err(too_large());
|
||||
if path.ends_with('/') {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut buf = Vec::new();
|
||||
f.take(limit.saturating_add(1)).read_to_end(&mut buf).map_err(|e| part(e.to_string()))?;
|
||||
let n = buf.len() as u64;
|
||||
if n > limit {
|
||||
return Err(too_large());
|
||||
}
|
||||
self.left = self.left.saturating_sub(n);
|
||||
Ok(Some(buf))
|
||||
let limit = self.limits.max_entry_bytes.min(self.zip.budget().remaining());
|
||||
self.zip.read(path, self.limits.max_entry_bytes).map_err(|e| match e {
|
||||
craft_zip::Error::EntryTooLarge { .. } | craft_zip::Error::BudgetSpent { .. } => Error::TooLarge { part: path.to_string(), limit },
|
||||
e => Error::Part { part: path.to_string(), msg: e.to_string() },
|
||||
})
|
||||
}
|
||||
|
||||
/// [`Epub::read`] for what a file of the package refers to: `href` as written in the file at
|
||||
@@ -295,6 +289,6 @@ impl<'a> Epub<'a> {
|
||||
|
||||
/// Bytes the entries still to be read may expand to.
|
||||
pub fn budget_left(&self) -> u64 {
|
||||
self.left
|
||||
self.zip.budget().remaining()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashSet;
|
||||
use std::fmt::Write as _;
|
||||
use std::io::Write as _;
|
||||
|
||||
use craft_xml::{esc, esc_attr, is_name};
|
||||
|
||||
@@ -316,10 +315,8 @@ pub fn write(book: &Book) -> Result<Vec<u8>, Error> {
|
||||
w3cdtf(&book.metadata.modified).ok_or_else(|| Error::Invalid(format!("`{}` is not a modification date", book.metadata.modified)))?;
|
||||
let opf = package_document(book, &modified);
|
||||
let nav = nav_document(book);
|
||||
let fail = |e: &dyn std::fmt::Display| Error::Invalid(format!("the archive could not be written: {e}"));
|
||||
let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
let deflated = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
let fail = |e: craft_zip::Error| Error::Invalid(e.to_string());
|
||||
let mut z = craft_zip::Writer::new();
|
||||
let own = [
|
||||
("mimetype".to_string(), MIMETYPE.as_bytes(), true),
|
||||
("META-INF/container.xml".to_string(), CONTAINER.as_bytes(), false),
|
||||
@@ -328,10 +325,7 @@ pub fn write(book: &Book) -> Result<Vec<u8>, Error> {
|
||||
];
|
||||
let resources = book.resources.iter().map(|r| (format!("{ROOT}/{}", r.path), r.data.as_ref(), r.stored));
|
||||
for (name, data, store) in own.into_iter().chain(resources) {
|
||||
// More than 4 GiB needs the ZIP64 form.
|
||||
let options = if store { stored } else { deflated }.large_file(data.len() >= u32::MAX as usize);
|
||||
z.start_file(name, options).map_err(|e| fail(&e))?;
|
||||
z.write_all(data).map_err(|e| fail(&e))?;
|
||||
z.add(&name, data, if store { craft_zip::Compression::Stored } else { craft_zip::Compression::Deflated }).map_err(fail)?;
|
||||
}
|
||||
z.finish().map(std::io::Cursor::into_inner).map_err(|e| fail(&e))
|
||||
z.finish().map_err(fail)
|
||||
}
|
||||
|
||||
@@ -252,3 +252,11 @@ fn a_package_document_past_the_entry_limit_is_refused() {
|
||||
let limits = Limits { max_entry_bytes: (xml.len() - 1) as u64, ..Limits::DEFAULT };
|
||||
assert!(matches!(parse_package(&xml, "", &limits), Err(Error::Part { .. })));
|
||||
}
|
||||
|
||||
/// A ZIP64 end record can declare millions of entries in a small file; the count is refused on
|
||||
/// the raw bytes, before the archive is indexed.
|
||||
#[test]
|
||||
fn an_archive_declaring_too_many_entries_is_refused_before_it_is_indexed() {
|
||||
let bomb = craft_zip::zip64_entry_count_sample(100_001);
|
||||
assert_eq!(Epub::open(&bomb).err(), Some(Error::TooManyEntries { found: 100_001, limit: 100_000 }));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "craft-ews"
|
||||
description = "Exchange Web Services SOAP layer without sockets: the request envelope and an escaping element builder, and the answer envelope, faults and response messages read with craft-xml."
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-xml = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
proptest = { workspace = true }
|
||||
@@ -0,0 +1,40 @@
|
||||
# craft-ews
|
||||
|
||||
The SOAP layer of Exchange Web Services, without sockets.
|
||||
|
||||
- **Requests**: `Element` builds an operation's elements (`Element::new("m:GetFolder").child(…)`)
|
||||
and escapes everything it is given: attribute values lose every control character, text keeps
|
||||
its tabs and line feeds and writes a carriage return as ` `, names keep only what a
|
||||
qualified name is made of. `envelope(Version, &operation)` writes the SOAP 1.1 envelope with
|
||||
`RequestServerVersion`.
|
||||
- **Answers**: `Answer::parse` reads the envelope with `craft-xml`. A fault is the error it names;
|
||||
`Answer::messages("GetFolder")` gives the `GetFolderResponseMessage`s with their
|
||||
`ResponseClass` and `ResponseCode`, and `ResponseMessage::ok` the element or the error. A busy
|
||||
server (`ErrorServerBusy`, or that code inside `ErrorInternalServerError`) gives its wait,
|
||||
clamped to 1 s to 5 min.
|
||||
|
||||
Names in an answer carry fixed prefixes whatever the server used: `s:` envelope, `m:` messages,
|
||||
`t:` types, `e:` errors.
|
||||
|
||||
Written from Microsoft's public documentation: the EWS reference, [MS-OXWSCDATA] and
|
||||
[MS-OXWSCORE].
|
||||
|
||||
## Hostile input
|
||||
|
||||
A document type declaration, malformed markup, more than 40 levels, more than 100,000 nodes or
|
||||
more than 64 MiB is `Error::Malformed`; an entity XML does not define is left out. Errors hold no
|
||||
text a server wrote except a response code that is a short identifier (64 letters and digits at
|
||||
most). `Debug` output of an answer or a message shows nothing a server chose.
|
||||
|
||||
## What stays in the apps
|
||||
|
||||
HTTP and its status codes, Autodiscover, NTLM and Basic authentication, the operations (which
|
||||
elements to send, what to make of what comes back), sync state and the mapping onto a mailbox.
|
||||
|
||||
## Origin
|
||||
|
||||
MailCraft `crates/ews` (`soap.rs`, `reply.rs`, the response codes of `error.rs`, the escaping of
|
||||
`xml.rs`) at
|
||||
[a44a450](https://github.com/UCS-Care-Group/mailcraft/commit/a44a45013d1c6396229c40ab60172d1882b368d5),
|
||||
by Martin Ball and the MailCraft contributors (MIT OR Apache-2.0), with the tests of that layer.
|
||||
MailCraft's own bounded XML reader (`xml.rs`) is replaced by `craft-xml`.
|
||||
@@ -0,0 +1,210 @@
|
||||
//! Reading an answer: the SOAP envelope, a fault, and the response messages of an operation with
|
||||
//! their classes and codes.
|
||||
//!
|
||||
//! The answer is read with `craft-xml` into a tree whose names carry fixed prefixes whatever the
|
||||
//! document used: `s:` for the envelope ([`SOAP`]), `m:` for the messages ([`MESSAGES`]), `t:` for
|
||||
//! the types ([`TYPES`]), `e:` for the errors ([`ERRORS`]); a name in any other namespace starts
|
||||
//! with `?:`. Elements are found by namespace and local name, never by the document's prefix;
|
||||
//! everything else in an answer (an element not asked about, a header, a remark) is skipped.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use craft_xml::{Canonical, Doctype, El, Limits, Local, Options, UnknownEntity};
|
||||
|
||||
use crate::codes::{DEFAULT_BACK_OFF_MS, MAX_BACK_OFF_MS, MIN_BACK_OFF_MS, ResponseClass, ResponseCode};
|
||||
use crate::soap::{ERRORS, MESSAGES, SOAP, TYPES};
|
||||
|
||||
/// Deepest the elements of an answer may nest.
|
||||
pub const MAX_DEPTH: usize = 40;
|
||||
/// Most nodes (elements and stretches of text) in one answer.
|
||||
pub const MAX_NODES: usize = 100_000;
|
||||
/// Most bytes of an answer: a `GetItem` answer carries whole messages in base64.
|
||||
pub const MAX_BYTES: usize = 64 * 1024 * 1024;
|
||||
|
||||
const NAMESPACES: [(&str, &str); 4] = [("s", SOAP), ("m", MESSAGES), ("t", TYPES), ("e", ERRORS)];
|
||||
const CANONICAL: Canonical<'static> = Canonical::new(&NAMESPACES);
|
||||
|
||||
/// Why an answer says nothing an operation can use.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
/// Not XML, or XML past the limits, or with a document type declaration.
|
||||
Malformed,
|
||||
/// XML, but not the envelope or the answer that was asked for.
|
||||
Unexpected,
|
||||
/// The server is busy: ask again after this many milliseconds.
|
||||
Busy { back_off_ms: u32 },
|
||||
/// The server said no, with this code.
|
||||
Server(ResponseCode),
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Error::Malformed => f.write_str("the answer is not well-formed XML within the limits"),
|
||||
Error::Unexpected => f.write_str("the answer is not the one asked for"),
|
||||
Error::Busy { back_off_ms } => write!(f, "the server is busy: ask again in {back_off_ms} ms"),
|
||||
Error::Server(code) => write!(f, "the server answered {code}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
/// Read `bytes` into a tree with the fixed prefixes (see the module's documentation). No document
|
||||
/// type declaration is read, and a reference to an entity XML does not define is left out.
|
||||
pub fn parse_xml(bytes: &[u8]) -> Result<El, Error> {
|
||||
if bytes.len() > MAX_BYTES {
|
||||
return Err(Error::Malformed);
|
||||
}
|
||||
let limits = Limits { max_input: MAX_BYTES, max_depth: MAX_DEPTH, max_nodes: MAX_NODES, ..Limits::DEFAULT };
|
||||
let options =
|
||||
Options { doctype: Doctype::Reject, unknown_entity: UnknownEntity::Drop, ..Options::canonical(&CANONICAL).strict().with_limits(limits) };
|
||||
options.parse(bytes).map_err(|_| Error::Malformed)
|
||||
}
|
||||
|
||||
/// The body of a SOAP answer that was not a fault.
|
||||
pub struct Answer {
|
||||
body: El,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Answer {
|
||||
/// Nothing of the answer: it may hold someone's mail.
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("Answer(..)")
|
||||
}
|
||||
}
|
||||
|
||||
/// One response message of an operation's answer.
|
||||
pub struct ResponseMessage<'a> {
|
||||
class: ResponseClass,
|
||||
code: ResponseCode,
|
||||
/// The server says it is busy (`ErrorServerBusy`, or inside `ErrorInternalServerError`).
|
||||
busy: bool,
|
||||
back_off_ms: Option<u32>,
|
||||
el: &'a El,
|
||||
}
|
||||
|
||||
impl fmt::Debug for ResponseMessage<'_> {
|
||||
/// The class and the code: the rest is the server's.
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("ResponseMessage").field("class", &self.class).field("code", &self.code).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Answer {
|
||||
/// Reads the document `bytes`: a SOAP envelope whose body is not a fault. A fault is the
|
||||
/// error it names.
|
||||
pub fn parse(bytes: &[u8]) -> Result<Self, Error> {
|
||||
let root = parse_xml(bytes)?;
|
||||
if root.name != "s:Envelope" {
|
||||
return Err(Error::Unexpected);
|
||||
}
|
||||
let body = root.child("s:Body").ok_or(Error::Unexpected)?;
|
||||
if let Some(fault) = body.child("s:Fault") {
|
||||
return Err(read_fault(fault));
|
||||
}
|
||||
Ok(Self { body: body.clone() })
|
||||
}
|
||||
|
||||
/// The `s:Body` element.
|
||||
pub fn body(&self) -> &El {
|
||||
&self.body
|
||||
}
|
||||
|
||||
/// The busy error, when any response message of the answer says the server is busy: it is
|
||||
/// the whole request that is to be made again, whatever the operation, and a busy server
|
||||
/// answers one message however many questions there were.
|
||||
pub fn busy(&self) -> Option<Error> {
|
||||
let response = self.body.elements().next()?;
|
||||
let all = response.child("m:ResponseMessages")?;
|
||||
all.elements().find_map(|child| ResponseMessage::read(child).ok()?.ok().err().filter(|error| matches!(error, Error::Busy { .. })))
|
||||
}
|
||||
|
||||
/// The response messages of the answer to `operation` (`GetFolder`: the
|
||||
/// `GetFolderResponseMessage`s inside `GetFolderResponse`), in order.
|
||||
pub fn messages(&self, operation: &str) -> Result<Vec<ResponseMessage<'_>>, Error> {
|
||||
let response = self.body.child(format!("m:{operation}Response").as_str()).ok_or(Error::Unexpected)?;
|
||||
let all = response.child("m:ResponseMessages").ok_or(Error::Unexpected)?;
|
||||
let name = format!("m:{operation}ResponseMessage");
|
||||
all.elements().filter(|child| child.name == name).map(ResponseMessage::read).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ResponseMessage<'a> {
|
||||
fn read(el: &'a El) -> Result<Self, Error> {
|
||||
let class = el.attr("ResponseClass").and_then(ResponseClass::from_name).ok_or(Error::Unexpected)?;
|
||||
let code = el.child("m:ResponseCode").map(|code| ResponseCode::from_name(&code.text())).ok_or(Error::Unexpected)?;
|
||||
// (`MessageXml` and its values are told by their local names: a server puts them in the
|
||||
// messages' namespace or the types'.)
|
||||
let wait = el.child(Local("BackOffMilliseconds")).map(|wait| wait.text());
|
||||
let (busy, back_off_ms) = busy_of(&code, el.child(Local("MessageXml")), wait.as_deref());
|
||||
Ok(Self { class, code, busy, back_off_ms, el })
|
||||
}
|
||||
|
||||
/// How the server says it went.
|
||||
pub fn class(&self) -> ResponseClass {
|
||||
self.class
|
||||
}
|
||||
|
||||
/// The code the server gave.
|
||||
pub fn code(&self) -> &ResponseCode {
|
||||
&self.code
|
||||
}
|
||||
|
||||
/// The element, for whoever reads what it holds.
|
||||
pub fn el(&self) -> &'a El {
|
||||
self.el
|
||||
}
|
||||
|
||||
/// The element when the message says it was done; the error it names when it was not.
|
||||
pub fn ok(&self) -> Result<&'a El, Error> {
|
||||
if self.class != ResponseClass::Error {
|
||||
return Ok(self.el);
|
||||
}
|
||||
Err(if self.busy { Error::Busy { back_off_ms: self.back_off_ms.unwrap_or(DEFAULT_BACK_OFF_MS) } } else { Error::Server(self.code.clone()) })
|
||||
}
|
||||
}
|
||||
|
||||
/// The text of the `Value` called `name` inside a `MessageXml`.
|
||||
fn value_of(message_xml: &El, name: &str) -> Option<String> {
|
||||
message_xml.children(Local("Value")).find(|value| value.attr("Name") == Some(name)).map(|value| value.text().trim().to_string())
|
||||
}
|
||||
|
||||
/// Whether a message with this `code` and `MessageXml` says the server is busy, and the wait
|
||||
/// it asks for. Busy is `ErrorServerBusy`, and also `ErrorInternalServerError` whose inner
|
||||
/// error is `ErrorServerBusy` (Exchange's throttling says so in a 200 answer). The wait is a
|
||||
/// `BackOffMilliseconds` of its own or a `Value` of that name.
|
||||
fn busy_of(code: &ResponseCode, message_xml: Option<&El>, direct: Option<&str>) -> (bool, Option<u32>) {
|
||||
let inner = message_xml.and_then(|xml| value_of(xml, "InnerErrorResponseCode")).map(|c| ResponseCode::from_name(&c));
|
||||
let busy = *code == ResponseCode::ServerBusy || (*code == ResponseCode::InternalServerError && inner == Some(ResponseCode::ServerBusy));
|
||||
let wait = direct.map(back_off_ms).or_else(|| message_xml.and_then(|xml| value_of(xml, "BackOffMilliseconds")).map(|w| back_off_ms(&w)));
|
||||
(busy, wait)
|
||||
}
|
||||
|
||||
/// A wait in milliseconds, as a server writes it: digits, at least [`MIN_BACK_OFF_MS`] and at
|
||||
/// most [`MAX_BACK_OFF_MS`]; anything else is the default.
|
||||
pub fn back_off_ms(text: &str) -> u32 {
|
||||
let text = text.trim();
|
||||
if text.is_empty() || !text.bytes().all(|b| b.is_ascii_digit()) {
|
||||
return DEFAULT_BACK_OFF_MS;
|
||||
}
|
||||
// (A number of any length: too many digits is simply the most.)
|
||||
let ms = text.parse::<u64>().unwrap_or(u64::MAX);
|
||||
u32::try_from(ms.clamp(u64::from(MIN_BACK_OFF_MS), u64::from(MAX_BACK_OFF_MS))).unwrap_or(MAX_BACK_OFF_MS)
|
||||
}
|
||||
|
||||
/// The error a `Fault` names: the response code of its `detail`, and for a busy server the wait.
|
||||
/// (`ResponseCode` and `MessageXml` are told by their local names, and `Value` too: the service
|
||||
/// writes `MessageXml` in the types' namespace as often as the errors'.)
|
||||
fn read_fault(fault: &El) -> Error {
|
||||
let detail = fault.child(Local("detail"));
|
||||
let code = detail
|
||||
.and_then(|detail| detail.child(Local("ResponseCode")))
|
||||
.map_or_else(|| ResponseCode::Other(String::new()), |code| ResponseCode::from_name(&code.text()));
|
||||
let (busy, wait) = busy_of(&code, detail.and_then(|detail| detail.child(Local("MessageXml"))), None);
|
||||
if busy { Error::Busy { back_off_ms: wait.unwrap_or(DEFAULT_BACK_OFF_MS) } } else { Error::Server(code) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "answer_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,234 @@
|
||||
//! Answers and requests, from the tests of the SOAP layer of the source.
|
||||
|
||||
use proptest::prelude::*;
|
||||
|
||||
use super::*;
|
||||
use crate::codes::{DEFAULT_BACK_OFF_MS, MAX_BACK_OFF_MS, MIN_BACK_OFF_MS};
|
||||
use crate::soap::{Element, Version, envelope, escape};
|
||||
|
||||
fn reply_with(code: &str, extra: &str) -> String {
|
||||
format!(
|
||||
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><m:GetFolderResponse xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\">\
|
||||
<m:ResponseMessages><m:GetFolderResponseMessage ResponseClass=\"Error\"><m:ResponseCode>{code}</m:ResponseCode>{extra}</m:GetFolderResponseMessage></m:ResponseMessages>\
|
||||
</m:GetFolderResponse></s:Body></s:Envelope>"
|
||||
)
|
||||
}
|
||||
|
||||
/// The error the first message of a `GetFolder` answer names.
|
||||
fn folder_error(body: &str) -> Error {
|
||||
let answer = Answer::parse(body.as_bytes()).unwrap();
|
||||
answer.messages("GetFolder").unwrap()[0].ok().unwrap_err()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_busy_server_says_how_long_and_is_believed_only_so_far() {
|
||||
assert_eq!(
|
||||
folder_error(&reply_with("ErrorServerBusy", "<m:BackOffMilliseconds>soon</m:BackOffMilliseconds>")),
|
||||
Error::Busy { back_off_ms: DEFAULT_BACK_OFF_MS }
|
||||
);
|
||||
assert_eq!(
|
||||
folder_error(&reply_with("ErrorServerBusy", "<m:BackOffMilliseconds>7000</m:BackOffMilliseconds>")),
|
||||
Error::Busy { back_off_ms: 7_000 }
|
||||
);
|
||||
assert_eq!(
|
||||
folder_error(&reply_with("ErrorServerBusy", "<m:BackOffMilliseconds>9999999999999999999999</m:BackOffMilliseconds>")),
|
||||
Error::Busy { back_off_ms: MAX_BACK_OFF_MS }
|
||||
);
|
||||
assert_eq!(folder_error(&reply_with("ErrorServerBusy", "")), Error::Busy { back_off_ms: DEFAULT_BACK_OFF_MS });
|
||||
// A wait of nothing is a wait.
|
||||
assert_eq!(
|
||||
folder_error(&reply_with("ErrorServerBusy", "<m:BackOffMilliseconds>0</m:BackOffMilliseconds>")),
|
||||
Error::Busy { back_off_ms: MIN_BACK_OFF_MS }
|
||||
);
|
||||
// The whole answer says so.
|
||||
let answer = Answer::parse(reply_with("ErrorServerBusy", "<m:BackOffMilliseconds>7000</m:BackOffMilliseconds>").as_bytes()).unwrap();
|
||||
assert_eq!(answer.busy(), Some(Error::Busy { back_off_ms: 7_000 }));
|
||||
assert_eq!(Answer::parse(reply_with("ErrorAccessDenied", "").as_bytes()).unwrap().busy(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fault_is_the_error_it_names() {
|
||||
let fault = |code: &str| {
|
||||
format!(
|
||||
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><s:Fault><faultcode>s:Client</faultcode><detail>\
|
||||
<e:ResponseCode xmlns:e=\"http://schemas.microsoft.com/exchange/services/2006/errors\">{code}</e:ResponseCode></detail></s:Fault></s:Body></s:Envelope>"
|
||||
)
|
||||
};
|
||||
assert_eq!(Answer::parse(fault("ErrorSchemaValidation").as_bytes()).unwrap_err(), Error::Server(ResponseCode::SchemaValidation));
|
||||
assert_eq!(Answer::parse(fault("ErrorInvalidRequest").as_bytes()).unwrap_err(), Error::Server(ResponseCode::InvalidRequest));
|
||||
assert_eq!(Answer::parse(fault("not a code").as_bytes()).unwrap_err(), Error::Server(ResponseCode::Other(String::new())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefixes_are_the_documents_own_choice() {
|
||||
let body = "<a:Envelope xmlns:a=\"http://schemas.xmlsoap.org/soap/envelope/\"><a:Body>\
|
||||
<x:GetFolderResponse xmlns:x=\"http://schemas.microsoft.com/exchange/services/2006/messages\" xmlns:y=\"http://schemas.microsoft.com/exchange/services/2006/types\"><x:ResponseMessages>\
|
||||
<x:GetFolderResponseMessage ResponseClass=\"Success\"><x:ResponseCode>NoError</x:ResponseCode><x:Folders><y:Folder><y:FolderId Id=\"AbC+/=-_\"/><y:DisplayName>Inbox</y:DisplayName></y:Folder></x:Folders>\
|
||||
</x:GetFolderResponseMessage></x:ResponseMessages></x:GetFolderResponse></a:Body></a:Envelope>";
|
||||
let answer = Answer::parse(body.as_bytes()).unwrap();
|
||||
let messages = answer.messages("GetFolder").unwrap();
|
||||
assert_eq!((messages[0].class(), messages[0].code()), (ResponseClass::Success, &ResponseCode::NoError));
|
||||
let folder = messages[0].ok().unwrap().path(&["m:Folders", "t:Folder"]).unwrap();
|
||||
assert_eq!(folder.child_attr("t:FolderId", "Id"), Some("AbC+/=-_"));
|
||||
assert_eq!(folder.child_text("t:DisplayName").as_deref(), Some("Inbox"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_element_in_the_wrong_namespace_is_not_the_one_asked_for() {
|
||||
let body = reply_with("NoError", "").replace("services/2006/messages", "services/2006/elsewhere");
|
||||
assert_eq!(Answer::parse(body.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
|
||||
// Nor is another operation's answer.
|
||||
assert_eq!(Answer::parse(reply_with("NoError", "").as_bytes()).unwrap().messages("GetItem").unwrap_err(), Error::Unexpected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_elements_are_skipped() {
|
||||
let extra = "<m:Surprise><m:Deeper>1</m:Deeper></m:Surprise><x:Other xmlns:x=\"urn:other\"/>";
|
||||
assert_eq!(folder_error(&reply_with("ErrorAccessDenied", extra)), Error::Server(ResponseCode::AccessDenied));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_codes_are_kept_only_as_short_identifiers() {
|
||||
assert_eq!(ResponseCode::from_name(" ErrorServerBusy "), ResponseCode::ServerBusy);
|
||||
assert_eq!(ResponseCode::from_name("ErrorSomethingNew"), ResponseCode::Other("ErrorSomethingNew".into()));
|
||||
assert_eq!(ResponseCode::from_name("bad code\r\nX: y"), ResponseCode::Other(String::new()));
|
||||
assert_eq!(ResponseCode::from_name(&"A".repeat(500)), ResponseCode::Other(String::new()));
|
||||
assert_eq!(ResponseCode::from_name("bad\r\nX: y").to_string(), "an unnamed error");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_message_without_a_class_or_a_code_is_unexpected() {
|
||||
let no_class = reply_with("NoError", "").replace(" ResponseClass=\"Error\"", "");
|
||||
assert_eq!(Answer::parse(no_class.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
|
||||
let odd_class = reply_with("NoError", "").replace("\"Error\"", "\"Maybe\"");
|
||||
assert_eq!(Answer::parse(odd_class.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
|
||||
let no_code = reply_with("NoError", "").replace("<m:ResponseCode>NoError</m:ResponseCode>", "");
|
||||
assert_eq!(Answer::parse(no_code.as_bytes()).unwrap().messages("GetFolder").unwrap_err(), Error::Unexpected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_answers_are_errors_never_panics() {
|
||||
for body in [
|
||||
"<!DOCTYPE x [<!ENTITY a 'b'>]><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body/></s:Envelope>".to_string(),
|
||||
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body>".to_string(),
|
||||
"not xml at all".to_string(),
|
||||
format!(
|
||||
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body>{}{}</s:Body></s:Envelope>",
|
||||
"<x>".repeat(MAX_DEPTH),
|
||||
"</x>".repeat(MAX_DEPTH)
|
||||
),
|
||||
format!("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body>{}</s:Body></s:Envelope>", "<x/>".repeat(MAX_NODES)),
|
||||
] {
|
||||
assert_eq!(Answer::parse(body.as_bytes()).unwrap_err(), Error::Malformed, "{}", &body[..body.len().min(80)]);
|
||||
}
|
||||
assert_eq!(Answer::parse(&vec![b' '; MAX_BYTES + 1]).unwrap_err(), Error::Malformed);
|
||||
assert_eq!(Answer::parse(b"<html><body>Sign in</body></html>").unwrap_err(), Error::Unexpected);
|
||||
assert_eq!(Answer::parse(b"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'/>").unwrap_err(), Error::Unexpected);
|
||||
// An entity XML does not define is left out, and the rest is read.
|
||||
let body = reply_with("Error&nonsense;AccessDenied", "");
|
||||
assert_eq!(folder_error(&body), Error::Server(ResponseCode::AccessDenied));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_busy_fault_with_its_wait_in_the_types_namespace_is_read() {
|
||||
let fault = |wait: &str| {
|
||||
format!(
|
||||
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><s:Fault><faultcode>s:Server</faultcode><detail>\
|
||||
<e:ResponseCode xmlns:e=\"http://schemas.microsoft.com/exchange/services/2006/errors\">ErrorServerBusy</e:ResponseCode>\
|
||||
<t:MessageXml xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\"><t:Value Name=\"BackOffMilliseconds\">{wait}</t:Value></t:MessageXml>\
|
||||
</detail></s:Fault></s:Body></s:Envelope>"
|
||||
)
|
||||
};
|
||||
assert_eq!(Answer::parse(fault("297749").as_bytes()).unwrap_err(), Error::Busy { back_off_ms: 297_749 });
|
||||
assert_eq!(Answer::parse(fault("9999999").as_bytes()).unwrap_err(), Error::Busy { back_off_ms: MAX_BACK_OFF_MS });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_busy_server_inside_an_internal_server_error_is_busy() {
|
||||
let inner = "<m:MessageXml><t:Value xmlns:t=\"x\" Name=\"InnerErrorResponseCode\">ErrorServerBusy</t:Value><t:Value xmlns:t=\"x\" Name=\"BackOffMilliseconds\">4000</t:Value></m:MessageXml>";
|
||||
assert_eq!(folder_error(&reply_with("ErrorInternalServerError", inner)), Error::Busy { back_off_ms: 4_000 });
|
||||
// Without the inner error it is the server's own error, and not a reason to wait.
|
||||
let body =
|
||||
reply_with("ErrorInternalServerError", "<m:MessageXml><t:Value xmlns:t=\"x\" Name=\"BackOffMilliseconds\">4000</t:Value></m:MessageXml>");
|
||||
assert_eq!(folder_error(&body), Error::Server(ResponseCode::InternalServerError));
|
||||
// The same in a fault.
|
||||
let fault = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><s:Fault><faultcode>s:Server</faultcode><detail>\
|
||||
<e:ResponseCode xmlns:e=\"http://schemas.microsoft.com/exchange/services/2006/errors\">ErrorInternalServerError</e:ResponseCode>\
|
||||
<e:MessageXml xmlns:e=\"http://schemas.microsoft.com/exchange/services/2006/errors\"><e:Value Name=\"InnerErrorResponseCode\">ErrorServerBusy</e:Value><e:Value Name=\"BackOffMilliseconds\">2500</e:Value></e:MessageXml></detail></s:Fault></s:Body></s:Envelope>";
|
||||
assert_eq!(Answer::parse(fault.as_bytes()).unwrap_err(), Error::Busy { back_off_ms: 2_500 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn debug_output_shows_nothing_a_server_chose() {
|
||||
let body = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><m:GetFolderResponse xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\"><m:ResponseMessages>\
|
||||
<m:GetFolderResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Folders><t:Folder><t:FolderId Id=\"SecretId\" ChangeKey=\"SecretKey\"/><t:DisplayName>Secret Name</t:DisplayName></t:Folder></m:Folders></m:GetFolderResponseMessage>\
|
||||
</m:ResponseMessages></m:GetFolderResponse></s:Body></s:Envelope>";
|
||||
let answer = Answer::parse(body.as_bytes()).unwrap();
|
||||
let messages = answer.messages("GetFolder").unwrap();
|
||||
let shown = format!("{answer:?} {messages:?} {:?}", Element::new("t:DisplayName").attr("Id", "SecretId").text("Secret Name"));
|
||||
assert!(!shown.contains("Secret") && !shown.contains("Folders"), "{shown}");
|
||||
}
|
||||
|
||||
// ---- the request ----
|
||||
|
||||
#[test]
|
||||
fn the_request_is_a_soap_envelope_with_the_version_and_one_operation() {
|
||||
let operation = Element::new("m:GetFolder").child(Element::new("m:FolderIds").child(Element::new("t:DistinguishedFolderId").attr("Id", "inbox")));
|
||||
let text = envelope(Version::Exchange2016, &operation);
|
||||
let tree = parse_xml(text.as_bytes()).unwrap();
|
||||
assert_eq!(tree.name, "s:Envelope");
|
||||
assert_eq!(tree.path(&["s:Header", "t:RequestServerVersion"]).and_then(|v| v.attr("Version")), Some("Exchange2016"));
|
||||
let body = tree.child("s:Body").unwrap();
|
||||
assert_eq!(body.elements().map(|e| e.name.as_str()).collect::<Vec<_>>(), ["m:GetFolder"]);
|
||||
assert!(envelope(Version::Exchange2010Sp2, &operation).contains("Exchange2010_SP2"));
|
||||
assert_eq!(Version::NAMES.len(), 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nothing_written_can_break_out_of_its_place() {
|
||||
let nasty = "a\r\nX-Injected: 1\0</t:BaseShape><m:Evil/>\"'&";
|
||||
let element = Element::new("m:Op\r\n<x").attr("Na\nme", nasty).text(nasty).child(Element::new("t:Child").text(nasty));
|
||||
let text = element.write();
|
||||
// (A line feed in a text stays; a carriage return is a reference; an attribute has neither.)
|
||||
assert!(!text.contains(['\r', '\0']), "{text:?}");
|
||||
let attribute = text.split_once("Name=\"").and_then(|(_, rest)| rest.split_once('"')).map(|(value, _)| value).unwrap_or_default();
|
||||
assert!(!attribute.is_empty() && !attribute.contains(['\r', '\n', '\0']), "{text:?}");
|
||||
assert!(!text.contains("<m:Evil/>") && !text.contains("<x"));
|
||||
// What is written reads back as the one element, holding the text as text.
|
||||
let tree = parse_xml(envelope(Version::default(), &element).as_bytes()).unwrap();
|
||||
let op = tree.child("s:Body").unwrap().elements().next().unwrap();
|
||||
assert_eq!(op.name, "m:Opx");
|
||||
assert!(op.text().contains("</t:BaseShape><m:Evil/>"));
|
||||
assert_eq!(op.elements().count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_text_keeps_its_lines() {
|
||||
let text = "first\r\nsecond\tindented\nthird\0\u{1}\u{FFFE}<&>";
|
||||
let written = Element::new("t:Body").text(text).write();
|
||||
assert_eq!(written, "<t:Body>first \nsecond\tindented\nthird<&></t:Body>");
|
||||
assert!(!written.contains(['\r', '\0']));
|
||||
let tree = parse_xml(envelope(Version::default(), &Element::new("m:Op").text(text)).as_bytes()).unwrap();
|
||||
let op = tree.child("s:Body").unwrap().elements().next().unwrap();
|
||||
assert_eq!(op.text(), "first\r\nsecond\tindented\nthird<&>");
|
||||
// An attribute, or a header, keeps none of it.
|
||||
assert_eq!(escape("a\r\nb\tc\0"), "abc");
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn arbitrary_bytes_never_panic(bytes in proptest::collection::vec(any::<u8>(), 0..4096)) {
|
||||
if let Ok(answer) = Answer::parse(&bytes) {
|
||||
let _ = (answer.busy(), answer.messages("GetFolder").map(|m| m.len()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_text_written_reads_back_as_itself(text in "\\PC{0,200}", name in "[a-zA-Z][a-zA-Z0-9]{0,10}") {
|
||||
let tree = parse_xml(envelope(Version::default(), &Element::new(&format!("m:{name}")).attr("A", &text).text(&text)).as_bytes()).unwrap();
|
||||
let op = tree.child("s:Body").unwrap().elements().next().unwrap();
|
||||
let kept: String = text.chars().filter(|c| !c.is_control() && *c != '\u{FFFE}' && *c != '\u{FFFF}').collect();
|
||||
prop_assert_eq!(op.attr("A"), Some(kept.as_str()));
|
||||
prop_assert_eq!(op.text().into_owned(), kept);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
//! How a response message says it went, and why not ([MS-OXWSCDATA]), and the waits a busy
|
||||
//! server asks for.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// The most a server may ask the client to wait, in milliseconds (five minutes); more is
|
||||
/// taken as this.
|
||||
pub const MAX_BACK_OFF_MS: u32 = 300_000;
|
||||
/// The shortest wait a busy server is given (one second): a server that says 0 is not to be
|
||||
/// asked again at once.
|
||||
pub const MIN_BACK_OFF_MS: u32 = 1_000;
|
||||
/// How long to wait when a server is busy and does not say for how long (one second).
|
||||
pub const DEFAULT_BACK_OFF_MS: u32 = 1_000;
|
||||
/// The longest response code kept.
|
||||
const MAX_CODE: usize = 64;
|
||||
|
||||
/// How a response message says it went (`ResponseClass`, [MS-OXWSCDATA]).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ResponseClass {
|
||||
/// It was done.
|
||||
Success,
|
||||
/// It was done, with a remark.
|
||||
Warning,
|
||||
/// It was not done.
|
||||
Error,
|
||||
}
|
||||
|
||||
impl ResponseClass {
|
||||
/// The class a document names; none for a word the schema does not have.
|
||||
pub fn from_name(name: &str) -> Option<Self> {
|
||||
match name {
|
||||
"Success" => Some(Self::Success),
|
||||
"Warning" => Some(Self::Warning),
|
||||
"Error" => Some(Self::Error),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a response message says no (`ResponseCodeType`, [MS-OXWSCDATA]): the ones the client
|
||||
/// acts on by name, and any other as the identifier it was.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum ResponseCode {
|
||||
/// `NoError`.
|
||||
NoError,
|
||||
/// `ErrorServerBusy`: try again after the back-off.
|
||||
ServerBusy,
|
||||
/// `ErrorAccessDenied`.
|
||||
AccessDenied,
|
||||
/// `ErrorFolderNotFound`.
|
||||
FolderNotFound,
|
||||
/// `ErrorItemNotFound`.
|
||||
ItemNotFound,
|
||||
/// `ErrorNonExistentMailbox`.
|
||||
NonExistentMailbox,
|
||||
/// `ErrorInvalidSyncStateData`: the sync state is no longer known; read afresh.
|
||||
InvalidSyncState,
|
||||
/// `ErrorInvalidPropertyRequest`: the server doesn't know a property that was asked for.
|
||||
InvalidPropertyRequest,
|
||||
/// `ErrorSchemaValidation`: the request did not fit the schema (a bug here).
|
||||
SchemaValidation,
|
||||
/// `ErrorInvalidServerVersion`: the server does not know the version asked for.
|
||||
InvalidServerVersion,
|
||||
/// `ErrorInvalidRequest`.
|
||||
InvalidRequest,
|
||||
/// `ErrorInternalServerError` or `ErrorInternalServerTransientError`.
|
||||
InternalServerError,
|
||||
/// `ErrorMailboxStoreUnavailable` or `ErrorMailboxMoveInProgress`: the mailbox cannot be
|
||||
/// reached for now.
|
||||
MailboxUnavailable,
|
||||
/// `ErrorTimeoutExpired`.
|
||||
TimeoutExpired,
|
||||
/// Any other code, as the short identifier it was.
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl ResponseCode {
|
||||
/// The code a document names. A name that is not a short identifier (letters and digits)
|
||||
/// is no code and reads as `Other("")`.
|
||||
pub fn from_name(name: &str) -> Self {
|
||||
match name.trim() {
|
||||
"NoError" => Self::NoError,
|
||||
"ErrorServerBusy" => Self::ServerBusy,
|
||||
"ErrorAccessDenied" => Self::AccessDenied,
|
||||
"ErrorFolderNotFound" => Self::FolderNotFound,
|
||||
"ErrorItemNotFound" => Self::ItemNotFound,
|
||||
"ErrorNonExistentMailbox" => Self::NonExistentMailbox,
|
||||
"ErrorInvalidSyncStateData" => Self::InvalidSyncState,
|
||||
"ErrorInvalidPropertyRequest" => Self::InvalidPropertyRequest,
|
||||
"ErrorSchemaValidation" => Self::SchemaValidation,
|
||||
"ErrorInvalidServerVersion" => Self::InvalidServerVersion,
|
||||
"ErrorInvalidRequest" => Self::InvalidRequest,
|
||||
"ErrorInternalServerError" | "ErrorInternalServerTransientError" => Self::InternalServerError,
|
||||
"ErrorMailboxStoreUnavailable" | "ErrorMailboxMoveInProgress" => Self::MailboxUnavailable,
|
||||
"ErrorTimeoutExpired" => Self::TimeoutExpired,
|
||||
other if !other.is_empty() && other.len() <= MAX_CODE && other.bytes().all(|b| b.is_ascii_alphanumeric()) => {
|
||||
Self::Other(other.to_owned())
|
||||
}
|
||||
_ => Self::Other(String::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ResponseCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::NoError => f.write_str("NoError"),
|
||||
Self::ServerBusy => f.write_str("ErrorServerBusy"),
|
||||
Self::AccessDenied => f.write_str("ErrorAccessDenied"),
|
||||
Self::FolderNotFound => f.write_str("ErrorFolderNotFound"),
|
||||
Self::ItemNotFound => f.write_str("ErrorItemNotFound"),
|
||||
Self::NonExistentMailbox => f.write_str("ErrorNonExistentMailbox"),
|
||||
Self::InvalidSyncState => f.write_str("ErrorInvalidSyncStateData"),
|
||||
Self::InvalidPropertyRequest => f.write_str("ErrorInvalidPropertyRequest"),
|
||||
Self::SchemaValidation => f.write_str("ErrorSchemaValidation"),
|
||||
Self::InvalidServerVersion => f.write_str("ErrorInvalidServerVersion"),
|
||||
Self::InvalidRequest => f.write_str("ErrorInvalidRequest"),
|
||||
Self::InternalServerError => f.write_str("ErrorInternalServerError"),
|
||||
Self::MailboxUnavailable => f.write_str("ErrorMailboxStoreUnavailable"),
|
||||
Self::TimeoutExpired => f.write_str("ErrorTimeoutExpired"),
|
||||
Self::Other(code) if code.is_empty() => f.write_str("an unnamed error"),
|
||||
Self::Other(code) => f.write_str(code),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
//! Exchange Web Services: the SOAP layer, without sockets.
|
||||
//!
|
||||
//! - [`soap`]: the request envelope ([`envelope`]) with the schema version asked for ([`Version`]),
|
||||
//! and [`Element`], a builder of the operation's elements that escapes everything it is given, so
|
||||
//! nothing a caller hands in can end an element, open another or break a line of the protocol.
|
||||
//! - [`answer`]: the answer envelope read with `craft-xml` ([`Answer::parse`]): a fault as the error
|
||||
//! it names, the response messages of an operation ([`Answer::messages`]) with their class and code
|
||||
//! ([`codes`]), and a busy server's wait.
|
||||
//!
|
||||
//! Written from Microsoft's public documentation: the EWS reference and the Open Specifications
|
||||
//! [MS-OXWSCDATA] (types) and [MS-OXWSCORE] (operations and their messages).
|
||||
//!
|
||||
//! What stays with the caller: HTTP, Autodiscover, authentication, the operations themselves (which
|
||||
//! elements to send, what to make of the elements that come back) and any sync state.
|
||||
|
||||
pub mod answer;
|
||||
pub mod codes;
|
||||
pub mod soap;
|
||||
|
||||
pub use answer::{Answer, Error, ResponseMessage};
|
||||
pub use codes::{ResponseClass, ResponseCode};
|
||||
pub use soap::{Element, Version, envelope};
|
||||
@@ -0,0 +1,225 @@
|
||||
//! The SOAP envelope of a request, and the elements it carries.
|
||||
//!
|
||||
//! Everything written here is escaped: attribute values pass through [`escape`] and
|
||||
//! lose every control character (CR, LF and NUL among them); text passes through
|
||||
//! [`escape_text`], which keeps its tabs and line feeds (a body of text keeps its
|
||||
//! lines), writes a carriage return as a character reference and drops NUL and the other
|
||||
//! controls XML forbids; and a name keeps only the characters a qualified name is made of.
|
||||
//! Nothing a caller hands in can therefore end an element, open another, or break a line of the
|
||||
//! protocol.
|
||||
|
||||
/// The SOAP 1.1 envelope's namespace.
|
||||
pub const SOAP: &str = "http://schemas.xmlsoap.org/soap/envelope/";
|
||||
/// The namespace of the types ([MS-OXWSCDATA]).
|
||||
pub const TYPES: &str = "http://schemas.microsoft.com/exchange/services/2006/types";
|
||||
/// The namespace of the messages (the operations and their answers).
|
||||
pub const MESSAGES: &str = "http://schemas.microsoft.com/exchange/services/2006/messages";
|
||||
/// The namespace of the errors a fault carries.
|
||||
pub const ERRORS: &str = "http://schemas.microsoft.com/exchange/services/2006/errors";
|
||||
|
||||
/// The version of the schema a request asks the server to answer in (`RequestServerVersion`,
|
||||
/// [MS-OXWSCDATA] `ExchangeVersionType`).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Version {
|
||||
/// Exchange 2007.
|
||||
Exchange2007,
|
||||
/// Exchange 2007 with service pack 1.
|
||||
Exchange2007Sp1,
|
||||
/// Exchange 2010.
|
||||
Exchange2010,
|
||||
/// Exchange 2010 with service pack 1.
|
||||
Exchange2010Sp1,
|
||||
/// Exchange 2010 with service pack 2.
|
||||
Exchange2010Sp2,
|
||||
/// Exchange 2013: what a request asks for unless told otherwise, as every server that
|
||||
/// speaks the service today understands it.
|
||||
#[default]
|
||||
Exchange2013,
|
||||
/// Exchange 2013 with service pack 1.
|
||||
Exchange2013Sp1,
|
||||
/// Exchange 2015.
|
||||
Exchange2015,
|
||||
/// Exchange 2016.
|
||||
Exchange2016,
|
||||
}
|
||||
|
||||
impl Version {
|
||||
/// Every name the schema has for a version.
|
||||
pub const NAMES: [&'static str; 9] = [
|
||||
"Exchange2007",
|
||||
"Exchange2007_SP1",
|
||||
"Exchange2010",
|
||||
"Exchange2010_SP1",
|
||||
"Exchange2010_SP2",
|
||||
"Exchange2013",
|
||||
"Exchange2013_SP1",
|
||||
"Exchange2015",
|
||||
"Exchange2016",
|
||||
];
|
||||
|
||||
/// The name the schema gives it.
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Exchange2007 => "Exchange2007",
|
||||
Self::Exchange2007Sp1 => "Exchange2007_SP1",
|
||||
Self::Exchange2010 => "Exchange2010",
|
||||
Self::Exchange2010Sp1 => "Exchange2010_SP1",
|
||||
Self::Exchange2010Sp2 => "Exchange2010_SP2",
|
||||
Self::Exchange2013 => "Exchange2013",
|
||||
Self::Exchange2013Sp1 => "Exchange2013_SP1",
|
||||
Self::Exchange2015 => "Exchange2015",
|
||||
Self::Exchange2016 => "Exchange2016",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A name as a qualified name may be written: letters, digits, `_`, `-`, `.` and one `:`
|
||||
/// between the prefix and the local name; anything else is dropped.
|
||||
fn clean_name(name: &str) -> String {
|
||||
let mut colon = false;
|
||||
name.chars()
|
||||
.filter(|c| match c {
|
||||
':' if !colon => {
|
||||
colon = true;
|
||||
true
|
||||
}
|
||||
c => c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One element of a request: a name (with its prefix, `m:GetFolder`), attributes, and text and
|
||||
/// children.
|
||||
#[derive(Clone, Default, PartialEq, Eq)]
|
||||
pub struct Element {
|
||||
name: String,
|
||||
attributes: Vec<(String, String)>,
|
||||
text: String,
|
||||
children: Vec<Element>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Element {
|
||||
/// The name and how much is inside: the text and the attributes may be somebody's mail.
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Element")
|
||||
.field("name", &self.name)
|
||||
.field("attributes", &self.attributes.len())
|
||||
.field("children", &self.children.len())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Element {
|
||||
/// An element called `name`.
|
||||
pub fn new(name: &str) -> Self {
|
||||
Self { name: clean_name(name), ..Self::default() }
|
||||
}
|
||||
|
||||
/// With the attribute `name` set to `value`.
|
||||
#[must_use]
|
||||
pub fn attr(mut self, name: &str, value: &str) -> Self {
|
||||
self.attributes.push((clean_name(name), escape(value)));
|
||||
self
|
||||
}
|
||||
|
||||
/// With `text` inside it.
|
||||
#[must_use]
|
||||
pub fn text(mut self, text: &str) -> Self {
|
||||
self.text = escape_text(text);
|
||||
self
|
||||
}
|
||||
|
||||
/// With `child` inside it, after the ones before.
|
||||
#[must_use]
|
||||
pub fn child(mut self, child: Element) -> Self {
|
||||
self.children.push(child);
|
||||
self
|
||||
}
|
||||
|
||||
/// The element as text.
|
||||
pub fn write(&self) -> String {
|
||||
let mut out = String::new();
|
||||
self.write_into(&mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn write_into(&self, out: &mut String) {
|
||||
out.push('<');
|
||||
out.push_str(&self.name);
|
||||
for (name, value) in &self.attributes {
|
||||
out.push(' ');
|
||||
out.push_str(name);
|
||||
out.push_str("=\"");
|
||||
out.push_str(value);
|
||||
out.push('"');
|
||||
}
|
||||
if self.text.is_empty() && self.children.is_empty() {
|
||||
out.push_str("/>");
|
||||
return;
|
||||
}
|
||||
out.push('>');
|
||||
out.push_str(&self.text);
|
||||
for child in &self.children {
|
||||
child.write_into(out);
|
||||
}
|
||||
out.push_str("</");
|
||||
out.push_str(&self.name);
|
||||
out.push('>');
|
||||
}
|
||||
}
|
||||
|
||||
/// The whole request: a SOAP envelope whose header names `version` and whose body holds
|
||||
/// `operation` (an element of the messages' namespace, `m:GetFolder`).
|
||||
pub fn envelope(version: Version, operation: &Element) -> String {
|
||||
let header = Element::new("t:RequestServerVersion").attr("Version", version.as_str());
|
||||
format!(
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"{SOAP}\" xmlns:m=\"{MESSAGES}\" xmlns:t=\"{TYPES}\">\
|
||||
<s:Header>{}</s:Header><s:Body>{}</s:Body></s:Envelope>",
|
||||
header.write(),
|
||||
operation.write()
|
||||
)
|
||||
}
|
||||
|
||||
/// `text` as it is written inside an attribute (or anywhere a line of a protocol could end): the
|
||||
/// five characters XML has names for by those names, and nothing that could end a line or a
|
||||
/// header: every control character (CR, LF, NUL and the tab too), and the characters XML 1.0
|
||||
/// forbids (U+FFFE, U+FFFF).
|
||||
pub fn escape(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
for c in text.chars() {
|
||||
match c {
|
||||
'&' => out.push_str("&"),
|
||||
'<' => out.push_str("<"),
|
||||
'>' => out.push_str(">"),
|
||||
'"' => out.push_str("""),
|
||||
'\'' => out.push_str("'"),
|
||||
c if c.is_control() || c == '\u{FFFE}' || c == '\u{FFFF}' => {}
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `text` as it is written inside an element: as [`escape`], except that the tab and the line
|
||||
/// feed stay (a body of text keeps its lines) and a carriage return is written as ` `, which
|
||||
/// is not one on the wire. NUL and the other C0 controls, U+FFFE and U+FFFF are dropped.
|
||||
/// (A `&str` holds no unpaired surrogate.)
|
||||
pub fn escape_text(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
for c in text.chars() {
|
||||
match c {
|
||||
'&' => out.push_str("&"),
|
||||
'<' => out.push_str("<"),
|
||||
'>' => out.push_str(">"),
|
||||
'\r' => out.push_str(" "),
|
||||
c if allowed(c) => out.push(c),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether XML 1.0 can carry `c` in a text.
|
||||
fn allowed(c: char) -> bool {
|
||||
!((c as u32) < 0x20 && !matches!(c, '\t' | '\n' | '\r')) && c != '\u{FFFE}' && c != '\u{FFFF}'
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "craft-exr"
|
||||
description = "OpenEXR pieces shared by the Craft apps: channel and layer naming, colour channel selection and Cryptomatte (IDs, manifests, rank channels)."
|
||||
description = "OpenEXR for the Craft apps: channel and layer naming, colour channel selection, Cryptomatte, and (feature `decode`) bounded part listing, flat decoding and writing on top of the exr crate."
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
@@ -10,5 +10,13 @@ repository.workspace = true
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[features]
|
||||
# Reading and writing pixels through the `exr` crate (BSD-3-Clause). The naming and Cryptomatte
|
||||
# modules need none of it.
|
||||
decode = ["dep:exr", "dep:half", "dep:thiserror"]
|
||||
|
||||
[dependencies]
|
||||
serde_json = { workspace = true }
|
||||
exr = { version = "1.73", default-features = false, optional = true }
|
||||
half = { version = "2", optional = true }
|
||||
thiserror = { workspace = true, optional = true }
|
||||
|
||||
@@ -0,0 +1,546 @@
|
||||
//! Bounded OpenEXR reading and writing on top of the `exr` crate (feature `decode`).
|
||||
//!
|
||||
//! - [`parts`] lists every part from the headers alone (size, windows, channels, compression,
|
||||
//! text attributes), after checking the declared sizes against [`Limits`] and the file's size.
|
||||
//! - [`pick_part`] chooses the part a plain picture decode shows: the best colour channel group
|
||||
//! ([`crate::channels::color_channels`]), the earliest part on a tie.
|
||||
//! - [`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.
|
||||
//! - [`write`] writes one scan-line part.
|
||||
//!
|
||||
//! The `exr` crate sizes buffers from headers before it reads pixels, so everything that reaches
|
||||
//! it is checked first: preview attributes against their own size, each part's samples against
|
||||
//! [`Limits`], all parts together against the budget, and the samples against what a file of
|
||||
//! this size can decompress to.
|
||||
|
||||
use std::io::Cursor;
|
||||
|
||||
use exr::block::UncompressedBlock;
|
||||
use exr::block::chunk::TileCoordinates;
|
||||
use exr::image::read::image::ReadImage;
|
||||
use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels};
|
||||
use exr::meta::MetaData;
|
||||
use exr::meta::attribute::{AttributeValue, SampleType as ExrSample};
|
||||
use exr::meta::header::Header;
|
||||
use exr::prelude::{
|
||||
AnyChannel, AnyChannels, Blocks, Compression as ExrCompression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, WritableImage,
|
||||
};
|
||||
pub use half::f16;
|
||||
|
||||
use crate::channels::color_channels;
|
||||
|
||||
/// Why a file could not be read or written.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// The file is damaged or not OpenEXR.
|
||||
#[error("OpenEXR: {0}")]
|
||||
Malformed(String),
|
||||
/// A valid file this reader does not cover (deep data in a flat read, HTJ2K, subsampled
|
||||
/// channels kept by the filter).
|
||||
#[error("unsupported OpenEXR: {0}")]
|
||||
Unsupported(String),
|
||||
/// The file declares more than [`Limits`] allow.
|
||||
#[error("OpenEXR exceeds a limit: {0}")]
|
||||
LimitExceeded(String),
|
||||
/// The picture handed to [`write`] cannot be stored.
|
||||
#[error("OpenEXR: cannot write: {0}")]
|
||||
Encode(String),
|
||||
}
|
||||
|
||||
/// Result of this module's functions.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
fn malformed(e: impl std::fmt::Display) -> Error {
|
||||
Error::Malformed(e.to_string())
|
||||
}
|
||||
|
||||
/// An `exr`-crate error, its `NotSupported` kind kept apart from damaged data.
|
||||
fn map_exr(e: exr::error::Error) -> Error {
|
||||
match e {
|
||||
exr::error::Error::NotSupported(message) => Error::Unsupported(message.to_string()),
|
||||
e => malformed(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Caps on what a file may ask the reader to allocate. `#[non_exhaustive]`: start from
|
||||
/// [`Limits::default`] and set the fields you change.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct Limits {
|
||||
/// Largest data window width and height of a part.
|
||||
pub max_width: u32,
|
||||
pub max_height: u32,
|
||||
/// Largest data window of a part, in pixels.
|
||||
pub max_pixels: u64,
|
||||
/// Most bytes of samples all parts together may declare, counting 4 bytes a sample. A decode
|
||||
/// allocates at most this for the channels it keeps.
|
||||
pub max_bytes: u64,
|
||||
/// The most a compressed byte is taken to expand to (DWA parts get 4 times as much). A file
|
||||
/// declaring more samples than its size times this, plus 1 MiB, is refused before reading.
|
||||
pub max_ratio: u64,
|
||||
}
|
||||
|
||||
impl Default for Limits {
|
||||
fn default() -> Self {
|
||||
let max_bytes = if cfg!(target_pointer_width = "64") { 8 << 30 } else { 1 << 30 };
|
||||
Limits { max_width: 1 << 18, max_height: 1 << 18, max_pixels: 1 << 30, max_bytes, max_ratio: 1 << 16 }
|
||||
}
|
||||
}
|
||||
|
||||
/// How a channel's samples are stored.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SampleType {
|
||||
F16,
|
||||
F32,
|
||||
U32,
|
||||
}
|
||||
|
||||
impl SampleType {
|
||||
fn of(t: ExrSample) -> Self {
|
||||
match t {
|
||||
ExrSample::F16 => SampleType::F16,
|
||||
ExrSample::F32 => SampleType::F32,
|
||||
ExrSample::U32 => SampleType::U32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A channel as the header declares it.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ChannelInfo {
|
||||
/// The name as stored (`diffuse.R`); the part name is not prefixed.
|
||||
pub name: String,
|
||||
pub sample: SampleType,
|
||||
/// Horizontal and vertical sampling (1, 1 for a full-resolution channel).
|
||||
pub sampling: (usize, usize),
|
||||
}
|
||||
|
||||
/// A window: position and size.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Window {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
/// Header facts of one part.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PartInfo {
|
||||
/// Position in the file.
|
||||
pub index: usize,
|
||||
/// The part's name (`name` attribute), the prefix renderers give its channels.
|
||||
pub name: Option<String>,
|
||||
/// The `view` attribute of a stereo part.
|
||||
pub view: Option<String>,
|
||||
pub data_window: Window,
|
||||
pub display_window: Window,
|
||||
pub deep: bool,
|
||||
pub tiled: bool,
|
||||
/// The compression's name as the `exr` crate spells it (`ZIP16`, `PIZ`, `DWAA(None)`, …).
|
||||
pub compression: String,
|
||||
pub channels: Vec<ChannelInfo>,
|
||||
/// Every text attribute of the part and of the shared header (`cryptomatte/…/manifest`,
|
||||
/// `owner`, …), name and value, sorted by name.
|
||||
pub text_attributes: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl PartInfo {
|
||||
/// The value of the text attribute called `name`.
|
||||
pub fn text(&self, name: &str) -> Option<&str> {
|
||||
self.text_attributes.iter().find(|(k, _)| k == name).map(|(_, v)| v.as_str())
|
||||
}
|
||||
|
||||
/// How much the part looks like a colour picture: 4 RGBA, 3 RGB, 2 YA, 1 Y (or a lone
|
||||
/// channel), 0 anything else.
|
||||
pub fn color_rank(&self) -> usize {
|
||||
color_channels(self.channels.iter().map(|c| c.name.as_str())).map_or(0, |(layout, _)| layout.channels())
|
||||
}
|
||||
}
|
||||
|
||||
/// Refuses headers whose `preview` attributes declare more pixels than their value holds.
|
||||
///
|
||||
/// The `exr` crate sizes a preview's buffer from its declared dimensions (up to 4 MiB, zero
|
||||
/// filled) before it finds the attribute too short, and in its non-pedantic mode then skips it
|
||||
/// and goes on: thousands of tiny malformed previews would each cost that allocation. This walks
|
||||
/// the raw headers and checks each preview against its own size. Anything else odd is left to
|
||||
/// the crate to report.
|
||||
fn check_previews(b: &[u8]) -> Result<()> {
|
||||
if b.get(..4) != Some(&[0x76, 0x2f, 0x31, 0x01][..]) {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(flags) = b.get(4..8).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes) else { return Ok(()) };
|
||||
let multipart = flags & 0x1000 != 0;
|
||||
let cstr = |at: usize| -> Option<(&[u8], usize)> {
|
||||
let rest = b.get(at..)?;
|
||||
let n = rest.iter().position(|&c| c == 0)?;
|
||||
Some((rest.get(..n)?, at + n + 1))
|
||||
};
|
||||
let u32_at = |at: usize| b.get(at..at.checked_add(4)?).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes);
|
||||
let mut at = 8;
|
||||
loop {
|
||||
// One header's attributes: name, type, size, value; an empty name ends the header.
|
||||
loop {
|
||||
let Some((name, next)) = cstr(at) else { return Ok(()) };
|
||||
at = next;
|
||||
if name.is_empty() {
|
||||
break;
|
||||
}
|
||||
let Some((kind, next)) = cstr(at) else { return Ok(()) };
|
||||
let Some(size) = u32_at(next).and_then(|n| i32::try_from(n).ok()).and_then(|n| usize::try_from(n).ok()) else { return Ok(()) };
|
||||
let value = next + 4;
|
||||
let end = match value.checked_add(size) {
|
||||
Some(end) if end <= b.len() => end,
|
||||
_ => return Ok(()),
|
||||
};
|
||||
if kind == b"preview" {
|
||||
let declared = u32_at(value)
|
||||
.zip(u32_at(value + 4))
|
||||
.and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4))
|
||||
.filter(|_| size >= 8);
|
||||
if declared.is_none_or(|n| n > (size - 8) as u64) {
|
||||
return Err(malformed("a preview attribute declares more pixels than it holds"));
|
||||
}
|
||||
}
|
||||
at = end;
|
||||
}
|
||||
// A multi-part header list ends with an empty header (a lone NUL).
|
||||
if !multipart || b.get(at) == Some(&0) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn window_size(size: exr::math::Vec2<usize>) -> (u32, u32) {
|
||||
(u32::try_from(size.0).unwrap_or(u32::MAX), u32::try_from(size.1).unwrap_or(u32::MAX))
|
||||
}
|
||||
|
||||
/// 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<MetaData> {
|
||||
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 (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)));
|
||||
}
|
||||
let pixels = u64::from(w) * u64::from(h);
|
||||
if pixels > limits.max_pixels {
|
||||
return Err(Error::LimitExceeded(format!("a part of {pixels} pixels exceeds {}", limits.max_pixels)));
|
||||
}
|
||||
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 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())));
|
||||
}
|
||||
}
|
||||
Ok(meta)
|
||||
}
|
||||
|
||||
fn text_attributes(header: &Header) -> Vec<(String, String)> {
|
||||
let mut out: Vec<(String, String)> = header
|
||||
.shared_attributes
|
||||
.other
|
||||
.iter()
|
||||
.chain(header.own_attributes.other.iter())
|
||||
.filter_map(|(k, v)| match v {
|
||||
AttributeValue::Text(t) => Some((k.to_string(), t.to_string())),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
out.sort();
|
||||
out.dedup_by(|a, b| a.0 == b.0);
|
||||
out
|
||||
}
|
||||
|
||||
fn part_info(index: usize, header: &Header) -> PartInfo {
|
||||
let (w, h) = window_size(header.layer_size);
|
||||
let display = &header.shared_attributes.display_window;
|
||||
let (dw, dh) = window_size(display.size);
|
||||
PartInfo {
|
||||
index,
|
||||
name: header.own_attributes.layer_name.as_ref().map(|n| n.to_string()),
|
||||
view: header.own_attributes.view_name.as_ref().map(|v| v.to_string()),
|
||||
data_window: Window { x: header.own_attributes.layer_position.0, y: header.own_attributes.layer_position.1, width: w, height: h },
|
||||
display_window: Window { x: display.position.0, y: display.position.1, width: dw, height: dh },
|
||||
deep: header.deep,
|
||||
tiled: matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)),
|
||||
compression: format!("{:?}", header.compression),
|
||||
channels: header
|
||||
.channels
|
||||
.list
|
||||
.iter()
|
||||
.map(|c| ChannelInfo { name: c.name.to_string(), sample: SampleType::of(c.sample_type), sampling: (c.sampling.0, c.sampling.1) })
|
||||
.collect(),
|
||||
text_attributes: text_attributes(header),
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists every part of the file from its headers, after checking the declared sizes.
|
||||
pub fn parts(bytes: &[u8], limits: &Limits) -> Result<Vec<PartInfo>> {
|
||||
let meta = read_meta(bytes, limits)?;
|
||||
Ok(meta.headers.iter().enumerate().map(|(i, h)| part_info(i, h)).collect())
|
||||
}
|
||||
|
||||
/// The part a plain picture decode shows: the highest [`PartInfo::color_rank`], the earliest on a
|
||||
/// tie. `None` when no part has colour channels.
|
||||
pub fn pick_part(parts: &[PartInfo]) -> Option<usize> {
|
||||
let mut best: Option<(usize, usize)> = None;
|
||||
for p in parts {
|
||||
let rank = p.color_rank();
|
||||
if rank > 0 && best.is_none_or(|(_, r)| rank > r) {
|
||||
best = Some((p.index, rank));
|
||||
}
|
||||
}
|
||||
best.map(|(i, _)| i)
|
||||
}
|
||||
|
||||
/// The samples of a decoded channel, in their stored type.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Samples {
|
||||
F16(Vec<f16>),
|
||||
F32(Vec<f32>),
|
||||
U32(Vec<u32>),
|
||||
}
|
||||
|
||||
impl Samples {
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
Samples::F16(v) => v.len(),
|
||||
Samples::F32(v) => v.len(),
|
||||
Samples::U32(v) => v.len(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Sample `i` as `f32` (U32 IDs exact up to 2^24). 0 past the end.
|
||||
pub fn get_f32(&self, i: usize) -> f32 {
|
||||
match self {
|
||||
Samples::F16(v) => v.get(i).map_or(0.0, |s| s.to_f32()),
|
||||
Samples::F32(v) => v.get(i).copied().unwrap_or(0.0),
|
||||
Samples::U32(v) => v.get(i).map_or(0.0, |&s| s as f32),
|
||||
}
|
||||
}
|
||||
|
||||
/// All samples as `f32`.
|
||||
pub fn to_f32(&self) -> Vec<f32> {
|
||||
match self {
|
||||
Samples::F16(v) => v.iter().map(|s| s.to_f32()).collect(),
|
||||
Samples::F32(v) => v.clone(),
|
||||
Samples::U32(v) => v.iter().map(|&s| s as f32).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A decoded channel.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Channel {
|
||||
/// The name as stored.
|
||||
pub name: String,
|
||||
/// `width × height` samples of the part's data window, top row first.
|
||||
pub samples: Samples,
|
||||
}
|
||||
|
||||
/// A decoded flat part: its header facts and the channels the filter kept, in file order.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Part {
|
||||
pub info: PartInfo,
|
||||
pub channels: Vec<Channel>,
|
||||
}
|
||||
|
||||
impl Part {
|
||||
/// The kept channel called `name`.
|
||||
pub fn channel(&self, name: &str) -> Option<&Channel> {
|
||||
self.channels.iter().find(|c| c.name == name)
|
||||
}
|
||||
}
|
||||
|
||||
/// Places a data-window plane in a display window: samples outside the display window are cut,
|
||||
/// the display window's pixels the data window does not cover are `fill`. `None` when the plane
|
||||
/// is not `data.width × data.height` samples or the display window has more than `max_pixels`
|
||||
/// pixels.
|
||||
pub fn to_display_window(plane: &[f32], data: Window, display: Window, fill: f32, max_pixels: u64) -> Option<Vec<f32>> {
|
||||
let (w, h) = (data.width as usize, data.height as usize);
|
||||
if plane.len() != w.checked_mul(h)? || u64::from(display.width) * u64::from(display.height) > max_pixels {
|
||||
return None;
|
||||
}
|
||||
let (dw, dh) = (display.width as usize, display.height as usize);
|
||||
let mut out = vec![fill; dw.checked_mul(dh)?];
|
||||
let (ox, oy) = (i64::from(data.x) - i64::from(display.x), i64::from(data.y) - i64::from(display.y));
|
||||
for (y, row) in plane.chunks_exact(w.max(1)).enumerate() {
|
||||
let Some(ty) = usize::try_from(y as i64 + oy).ok().filter(|ty| *ty < dh) else { continue };
|
||||
let skip = usize::try_from(-ox).unwrap_or(0).min(w);
|
||||
let tx = usize::try_from(ox.max(0)).unwrap_or(usize::MAX);
|
||||
let take = (w - skip).min(dw.saturating_sub(tx));
|
||||
if take == 0 {
|
||||
continue;
|
||||
}
|
||||
let at = ty * dw + tx;
|
||||
if let (Some(dst), Some(src)) = (out.get_mut(at..at + take), row.get(skip..skip + take)) {
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Which channels of which part a decode keeps.
|
||||
struct Select<'k> {
|
||||
index: usize,
|
||||
keep: &'k dyn Fn(&str) -> bool,
|
||||
seen: std::cell::Cell<usize>,
|
||||
}
|
||||
|
||||
/// One part's kept channels while it is decoded: each line goes straight into its channel's plane.
|
||||
struct SelectReader {
|
||||
/// Per channel of the part: its plane, `None` when not kept.
|
||||
planes: Vec<Option<Samples>>,
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
impl<'s> ReadChannels<'s> for Select<'_> {
|
||||
type Reader = SelectReader;
|
||||
|
||||
fn create_channels_reader(&'s self, header: &Header) -> exr::error::Result<SelectReader> {
|
||||
let position = self.seen.get();
|
||||
self.seen.set(position.saturating_add(1));
|
||||
let (width, height) = (header.layer_size.0, header.layer_size.1);
|
||||
if position != self.index {
|
||||
// Another part: keep nothing, so its chunks are skipped unread.
|
||||
return Ok(SelectReader { planes: header.channels.list.iter().map(|_| None).collect(), width, height });
|
||||
}
|
||||
let n = width.checked_mul(height).ok_or(exr::error::Error::Invalid("part size".into()))?;
|
||||
let mut planes = Vec::with_capacity(header.channels.list.len());
|
||||
for c in header.channels.list.iter() {
|
||||
if !(self.keep)(&c.name.to_string()) {
|
||||
planes.push(None);
|
||||
continue;
|
||||
}
|
||||
if c.sampling.0 != 1 || c.sampling.1 != 1 {
|
||||
return Err(exr::error::Error::NotSupported("subsampled channels".into()));
|
||||
}
|
||||
planes.push(Some(match c.sample_type {
|
||||
ExrSample::F16 => Samples::F16(vec![f16::ZERO; n]),
|
||||
ExrSample::F32 => Samples::F32(vec![0.0; n]),
|
||||
ExrSample::U32 => Samples::U32(vec![0; n]),
|
||||
}));
|
||||
}
|
||||
Ok(SelectReader { planes, width, height })
|
||||
}
|
||||
}
|
||||
|
||||
impl ChannelsReader for SelectReader {
|
||||
type Channels = Vec<Option<Samples>>;
|
||||
|
||||
fn filter_block(&self, tile: TileCoordinates) -> bool {
|
||||
tile.is_largest_resolution_level() && self.planes.iter().any(Option::is_some)
|
||||
}
|
||||
|
||||
fn read_block(&mut self, header: &Header, block: UncompressedBlock) -> exr::error::UnitResult {
|
||||
for line in block.lines(&header.channels) {
|
||||
let Some(Some(plane)) = self.planes.get_mut(line.location.channel) else { continue };
|
||||
let (x, y, count) = (line.location.position.0, line.location.position.1, line.location.sample_count);
|
||||
if y >= self.height || x.checked_add(count).is_none_or(|end| end > self.width) {
|
||||
continue;
|
||||
}
|
||||
let at = y * self.width + x;
|
||||
match plane {
|
||||
Samples::F16(v) => line.read_samples_into_slice(v.get_mut(at..at + count).unwrap_or_default())?,
|
||||
Samples::F32(v) => line.read_samples_into_slice(v.get_mut(at..at + count).unwrap_or_default())?,
|
||||
Samples::U32(v) => line.read_samples_into_slice(v.get_mut(at..at + count).unwrap_or_default())?,
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn into_channels(self) -> Vec<Option<Samples>> {
|
||||
self.planes
|
||||
}
|
||||
}
|
||||
|
||||
/// 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`].
|
||||
pub fn decode_part(bytes: &[u8], index: usize, keep: &dyn Fn(&str) -> bool, limits: &Limits) -> Result<Part> {
|
||||
let meta = read_meta(bytes, limits)?;
|
||||
if meta.requirements.has_deep_data {
|
||||
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 channels = info.channels.iter().zip(planes).filter_map(|(c, p)| Some(Channel { name: c.name.clone(), samples: p? })).collect();
|
||||
Ok(Part { info, channels })
|
||||
}
|
||||
|
||||
/// Compression for [`write`].
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum Compression {
|
||||
None,
|
||||
Rle,
|
||||
Zip1,
|
||||
#[default]
|
||||
Zip16,
|
||||
Piz,
|
||||
}
|
||||
|
||||
/// Writes one scan-line part of `width × height` with these channels (each `width × height`
|
||||
/// samples, top row first; the order given does not matter, the file stores them sorted).
|
||||
pub fn write(width: u32, height: u32, channels: &[Channel], compression: Compression) -> Result<Vec<u8>> {
|
||||
let enc = |m: &str| Error::Encode(m.into());
|
||||
if width == 0 || height == 0 || channels.is_empty() {
|
||||
return Err(enc("empty picture"));
|
||||
}
|
||||
let n = (width as usize).checked_mul(height as usize).ok_or_else(|| enc("picture too large"))?;
|
||||
if channels.iter().any(|c| c.samples.len() != n) {
|
||||
return Err(enc("a channel has the wrong number of samples"));
|
||||
}
|
||||
let mut list = Vec::with_capacity(channels.len());
|
||||
for c in channels {
|
||||
let name = exr::meta::attribute::Text::new_or_none(c.name.as_str()).ok_or_else(|| enc("a channel name is not valid OpenEXR text"))?;
|
||||
let samples = match &c.samples {
|
||||
Samples::F16(v) => FlatSamples::F16(v.clone()),
|
||||
Samples::F32(v) => FlatSamples::F32(v.clone()),
|
||||
Samples::U32(v) => FlatSamples::U32(v.clone()),
|
||||
};
|
||||
list.push(AnyChannel::new(name, samples));
|
||||
}
|
||||
let compression = match compression {
|
||||
Compression::None => ExrCompression::Uncompressed,
|
||||
Compression::Rle => ExrCompression::RLE,
|
||||
Compression::Zip1 => ExrCompression::ZIP1,
|
||||
Compression::Zip16 => ExrCompression::ZIP16,
|
||||
Compression::Piz => ExrCompression::PIZ,
|
||||
};
|
||||
let encoding = Encoding { compression, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing };
|
||||
let layer = Layer::new((width as usize, height as usize), LayerAttributes::default(), encoding, AnyChannels::sort(list.into_iter().collect()));
|
||||
let mut out = Cursor::new(Vec::new());
|
||||
exr::image::Image::from_layer(layer).write().to_buffered(&mut out).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
Ok(out.into_inner())
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
//! The preview check is PhotoCraft's test; the rest builds files with the `exr` crate.
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use exr::meta::attribute::{Preview, Text};
|
||||
use exr::prelude::{
|
||||
AnyChannel, AnyChannels, Blocks, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, WritableImage,
|
||||
};
|
||||
|
||||
use super::decode::*;
|
||||
|
||||
fn ramp(n: usize, k: f32) -> Vec<f32> {
|
||||
(0..n).map(|i| i as f32 * k).collect()
|
||||
}
|
||||
|
||||
/// A layer of `w × h` with these channels of f32 ramps, scan lines or 4 × 4 tiles.
|
||||
fn layer(
|
||||
name: Option<&str>,
|
||||
w: usize,
|
||||
h: usize,
|
||||
channels: &[&str],
|
||||
tiles: bool,
|
||||
compression: exr::prelude::Compression,
|
||||
) -> Layer<AnyChannels<FlatSamples>> {
|
||||
let mut attrs = name.map_or_else(LayerAttributes::default, LayerAttributes::named);
|
||||
attrs.other.insert(Text::from("cryptomatte/abc1234/name"), exr::meta::attribute::AttributeValue::Text(Text::from("CryptoObject")));
|
||||
let list = channels.iter().enumerate().map(|(i, n)| AnyChannel::new(*n, FlatSamples::F32(ramp(w * h, i as f32 + 1.0)))).collect();
|
||||
let blocks = if tiles { Blocks::Tiles(exr::math::Vec2(4, 4)) } else { Blocks::ScanLines };
|
||||
Layer::new((w, h), attrs, Encoding { compression, blocks, line_order: LineOrder::Increasing }, AnyChannels::sort(list))
|
||||
}
|
||||
|
||||
fn file(layers: Vec<Layer<AnyChannels<FlatSamples>>>, display: (usize, usize)) -> Vec<u8> {
|
||||
let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), display)), layers);
|
||||
let mut out = std::io::Cursor::new(Vec::new());
|
||||
image.write().to_buffered(&mut out).unwrap();
|
||||
out.into_inner()
|
||||
}
|
||||
|
||||
fn all(_: &str) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_compression_scan_lines_and_tiles_decode_exactly() {
|
||||
use exr::prelude::Compression::*;
|
||||
for compression in [Uncompressed, RLE, ZIP1, ZIP16, PIZ] {
|
||||
for tiles in [false, true] {
|
||||
let bytes = file(vec![layer(None, 9, 7, &["R", "G", "B"], tiles, compression)], (9, 7));
|
||||
let part = decode_part(&bytes, 0, &all, &Limits::default()).unwrap();
|
||||
assert_eq!((part.info.tiled, part.info.data_window.width, part.info.data_window.height), (tiles, 9, 7));
|
||||
// Each channel kept its own ramp, whatever order the file stores them in.
|
||||
for (name, k) in [("R", 1.0), ("G", 2.0), ("B", 3.0)] {
|
||||
assert_eq!(part.channel(name).unwrap().samples, Samples::F32(ramp(63, k)), "{compression:?} tiles {tiles} {name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Lossy ones decode too (values not exact).
|
||||
for compression in [PXR24, B44, B44A, DWAA(None), DWAB(None)] {
|
||||
let bytes = file(vec![layer(None, 16, 16, &["R"], false, compression)], (16, 16));
|
||||
assert_eq!(decode_part(&bytes, 0, &all, &Limits::default()).unwrap().channels[0].samples.len(), 256, "{compression:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_part_files_list_and_decode_one_part() {
|
||||
let bytes = file(
|
||||
vec![
|
||||
layer(Some("depth"), 4, 2, &["Z"], false, exr::prelude::Compression::ZIP1),
|
||||
layer(Some("beauty"), 4, 2, &["A", "B", "G", "R"], true, exr::prelude::Compression::PIZ),
|
||||
],
|
||||
(4, 2),
|
||||
);
|
||||
let parts = parts(&bytes, &Limits::default()).unwrap();
|
||||
assert_eq!(parts.len(), 2);
|
||||
assert_eq!((parts[0].name.as_deref(), parts[1].name.as_deref(), parts[1].tiled), (Some("depth"), Some("beauty"), true));
|
||||
assert_eq!(parts[0].channels, [ChannelInfo { name: "Z".into(), sample: SampleType::F32, sampling: (1, 1) }]);
|
||||
assert_eq!((parts[0].color_rank(), parts[1].color_rank(), parts[1].compression.as_str()), (1, 4, "PIZ"));
|
||||
assert_eq!(parts[1].text("cryptomatte/abc1234/name"), Some("CryptoObject"));
|
||||
assert_eq!(pick_part(&parts), Some(1));
|
||||
// Only the kept channels of the chosen part come back.
|
||||
let part = decode_part(&bytes, 1, &|n| n == "R" || n == "A", &Limits::default()).unwrap();
|
||||
assert_eq!(part.channels.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(), ["A", "R"]);
|
||||
let z = decode_part(&bytes, 0, &all, &Limits::default()).unwrap();
|
||||
assert_eq!(z.channels[0].samples.to_f32(), ramp(8, 1.0));
|
||||
assert!(matches!(decode_part(&bytes, 2, &all, &Limits::default()), Err(Error::Malformed(_))));
|
||||
assert_eq!(pick_part(&[]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn written_files_read_back() {
|
||||
let h: Vec<f16> = (0..6).map(|i| f16::from_f32(i as f32 / 4.0)).collect();
|
||||
let channels = vec![
|
||||
Channel { name: "Y".into(), samples: Samples::F16(h.clone()) },
|
||||
Channel { name: "A".into(), samples: Samples::F32(ramp(6, 0.5)) },
|
||||
Channel { name: "id".into(), samples: Samples::U32((0..6).collect()) },
|
||||
];
|
||||
for c in [Compression::None, Compression::Rle, Compression::Zip1, Compression::Zip16, Compression::Piz] {
|
||||
let bytes = write(3, 2, &channels, c).unwrap();
|
||||
let part = decode_part(&bytes, 0, &all, &Limits::default()).unwrap();
|
||||
assert_eq!(part.channel("Y").unwrap().samples, Samples::F16(h.clone()));
|
||||
assert_eq!(part.channel("A").unwrap().samples, Samples::F32(ramp(6, 0.5)));
|
||||
assert_eq!(part.channel("id").unwrap().samples, Samples::U32((0..6).collect()));
|
||||
assert_eq!(part.channel("id").unwrap().samples.get_f32(5), 5.0);
|
||||
}
|
||||
assert!(matches!(write(0, 2, &channels, Compression::None), Err(Error::Encode(_))));
|
||||
assert!(matches!(write(2, 2, &channels, Compression::None), Err(Error::Encode(_))));
|
||||
assert!(matches!(write(3, 2, &[], Compression::None), Err(Error::Encode(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_window_placement() {
|
||||
let data = Window { x: 1, y: -1, width: 2, height: 2 };
|
||||
let display = Window { x: 0, y: 0, width: 3, height: 2 };
|
||||
// Row -1 is cut; row 0 lands at display row 0 from column 1.
|
||||
assert_eq!(to_display_window(&[1.0, 2.0, 3.0, 4.0], data, display, 9.0, 100).unwrap(), [9.0, 3.0, 4.0, 9.0, 9.0, 9.0]);
|
||||
assert_eq!(to_display_window(&[1.0], data, display, 0.0, 100), None);
|
||||
assert_eq!(to_display_window(&[1.0, 2.0, 3.0, 4.0], data, display, 0.0, 5), None);
|
||||
let far = Window { x: -10, y: 0, width: 2, height: 2 };
|
||||
assert_eq!(to_display_window(&[1.0, 2.0, 3.0, 4.0], far, display, 0.0, 100).unwrap(), [0.0; 6]);
|
||||
}
|
||||
|
||||
/// A 2x2 single-part file whose layer carries a 2x1 preview.
|
||||
fn with_preview() -> Vec<u8> {
|
||||
let mut attrs = LayerAttributes::named("rgb");
|
||||
attrs.preview = Some(Preview { size: exr::math::Vec2(2, 1), pixel_data: vec![7; 8] });
|
||||
let list = ["R", "G", "B"].into_iter().map(|n| AnyChannel::new(n, FlatSamples::F32(vec![0.5; 4]))).collect();
|
||||
let layer = Layer::new(
|
||||
(2, 2),
|
||||
attrs,
|
||||
Encoding { compression: exr::prelude::Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing },
|
||||
AnyChannels::sort(list),
|
||||
);
|
||||
file(vec![layer], (2, 2))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_preview_declaring_more_pixels_than_it_holds_is_refused_before_the_crate_reads_it() {
|
||||
let good = with_preview();
|
||||
assert_eq!(decode_part(&good, 0, &all, &Limits::default()).unwrap().channels.len(), 3);
|
||||
// Patch the preview to 1024x1024 (4 MiB) while its value still holds 8 bytes.
|
||||
let tag = b"preview\0preview\0";
|
||||
let at = good.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len() + 4;
|
||||
let mut bad = good.clone();
|
||||
bad[at..at + 4].copy_from_slice(&1024u32.to_le_bytes());
|
||||
bad[at + 4..at + 8].copy_from_slice(&1024u32.to_le_bytes());
|
||||
for r in [parts(&bad, &Limits::default()).map(|_| ()), decode_part(&bad, 0, &all, &Limits::default()).map(|_| ())] {
|
||||
assert!(matches!(&r, Err(Error::Malformed(m)) if m.contains("preview")), "{r:?}");
|
||||
}
|
||||
// A header cut off mid-attribute is left to the crate to report.
|
||||
assert!(parts(&good[..at], &Limits::default()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn limits_are_checked_from_the_headers() {
|
||||
let bytes = file(vec![layer(None, 8, 4, &["R", "G"], false, exr::prelude::Compression::ZIP16)], (8, 4));
|
||||
let ok = |l: Limits| parts(&bytes, &l).is_ok();
|
||||
let d = Limits::default();
|
||||
assert!(ok(Limits { max_width: 8, max_height: 4, max_pixels: 32, max_bytes: 8 * 4 * 2 * 4, ..d }));
|
||||
assert!(matches!(parts(&bytes, &Limits { max_width: 7, ..d }), Err(Error::LimitExceeded(_))));
|
||||
assert!(matches!(parts(&bytes, &Limits { max_height: 3, ..d }), Err(Error::LimitExceeded(_))));
|
||||
assert!(matches!(parts(&bytes, &Limits { max_pixels: 31, ..d }), Err(Error::LimitExceeded(_))));
|
||||
assert!(matches!(decode_part(&bytes, 0, &all, &Limits { max_bytes: 255, ..d }), Err(Error::LimitExceeded(_))));
|
||||
// A file too small for what it declares: 256 bytes of samples against a ratio of 0 and no slack
|
||||
// is over; the 1 MiB slack always admits small pictures.
|
||||
assert!(ok(Limits { max_ratio: 0, ..d }));
|
||||
// A header claiming a huge data window over a few bytes of file is refused by the ratio.
|
||||
let big = file(vec![layer(None, 4096, 4096, &["R"], false, exr::prelude::Compression::ZIP16)], (4096, 4096));
|
||||
assert!(ok_big(&big));
|
||||
assert!(matches!(parts(&big, &Limits { max_ratio: 1, ..d }), Err(Error::LimitExceeded(_))));
|
||||
}
|
||||
|
||||
fn ok_big(b: &[u8]) -> bool {
|
||||
parts(b, &Limits::default()).is_ok()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_files_never_panic() {
|
||||
let good = file(
|
||||
vec![
|
||||
layer(Some("a"), 5, 3, &["R", "G", "B"], false, exr::prelude::Compression::ZIP1),
|
||||
layer(Some("b"), 5, 3, &["Y"], true, exr::prelude::Compression::RLE),
|
||||
],
|
||||
(5, 3),
|
||||
);
|
||||
let limits = Limits { max_bytes: 1 << 24, ..Limits::default() };
|
||||
for n in 0..good.len() {
|
||||
let _ = parts(&good[..n], &limits);
|
||||
let _ = decode_part(&good[..n], 1, &all, &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, 0, &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));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(parts(b"not exr", &limits).is_err());
|
||||
}
|
||||
@@ -10,7 +10,14 @@
|
||||
//! - [`cryptomatte`]: Cryptomatte (specification 1.2) IDs, metadata keys, manifests and rank
|
||||
//! channel names.
|
||||
//!
|
||||
//! Nothing here reads pixels or depends on a decoder: the functions take names and attribute text.
|
||||
//! Those take names and attribute text and depend on no decoder. With the `decode` feature:
|
||||
//!
|
||||
//! - [`decode`]: bounded part listing, flat decoding (scan lines and tiles, every compression the
|
||||
//! `exr` crate reads) and writing, on top of the `exr` crate.
|
||||
|
||||
pub mod channels;
|
||||
pub mod cryptomatte;
|
||||
#[cfg(feature = "decode")]
|
||||
pub mod decode;
|
||||
#[cfg(all(test, feature = "decode"))]
|
||||
mod decode_tests;
|
||||
|
||||
@@ -22,6 +22,19 @@ const MAX_SCAN_READ: u64 = 256 << 20;
|
||||
/// The largest font file loaded.
|
||||
const MAX_LOAD: u64 = 512 << 20;
|
||||
|
||||
/// The bytes of the file at `path`, at most `max`: the size was checked before, but a file can
|
||||
/// grow between that check and the read, so the read itself stops one byte past the limit.
|
||||
pub(crate) fn read_capped(path: &Path, max: u64) -> Result<Vec<u8>, FontFileError> {
|
||||
use std::io::Read as _;
|
||||
let mut bytes = Vec::new();
|
||||
std::fs::File::open(path)?.take(max.saturating_add(1)).read_to_end(&mut bytes)?;
|
||||
let size = bytes.len() as u64;
|
||||
if size > max {
|
||||
return Err(FontFileError::TooLarge { size, max });
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// Why a font file or font data wasn't added or loaded.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum FontFileError {
|
||||
@@ -544,7 +557,7 @@ impl Catalog {
|
||||
if size > MAX_LOAD {
|
||||
return Err(FontFileError::TooLarge { size, max: MAX_LOAD });
|
||||
}
|
||||
let bytes: Arc<[u8]> = std::fs::read(path)?.into();
|
||||
let bytes: Arc<[u8]> = read_capped(path, MAX_LOAD)?.into();
|
||||
let mut files = lock(&self.files);
|
||||
files.retain(|_, w| w.strong_count() > 0);
|
||||
files.insert(path.to_path_buf(), Arc::downgrade(&bytes));
|
||||
|
||||
@@ -556,3 +556,15 @@ fn repeated_name_records_are_bounded_before_pairing() {
|
||||
assert!(!face.keys.legacy.is_empty());
|
||||
assert_eq!(face.keys.legacy.len(), 256, "the pair budget counts distinct pairs");
|
||||
}
|
||||
|
||||
/// A read stops at its cap, whatever the file has grown to since its size was checked.
|
||||
#[test]
|
||||
fn a_read_is_capped_where_the_file_has_grown() {
|
||||
use crate::catalog::read_capped;
|
||||
let path = std::env::temp_dir().join(format!("craft-font-catalog-cap-{}", std::process::id()));
|
||||
std::fs::write(&path, b"0123456789").unwrap();
|
||||
assert_eq!(read_capped(&path, 10).unwrap(), b"0123456789");
|
||||
assert!(matches!(read_capped(&path, 9), Err(FontFileError::TooLarge { size: 10, max: 9 })));
|
||||
assert!(matches!(read_capped(&path.with_extension("missing"), 9), Err(FontFileError::Io(_))));
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "craft-ical"
|
||||
description = "iCalendar (RFC 5545) building blocks: content lines, values, recurrence rules and VTIMEZONE definitions, read and written."
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
chrono = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
proptest = { workspace = true }
|
||||
@@ -0,0 +1,56 @@
|
||||
# craft-ical
|
||||
|
||||
iCalendar (RFC 5545) building blocks, read and written. Times are `chrono` values; instants are
|
||||
`i64` seconds since 1970-01-01T00:00:00Z.
|
||||
|
||||
- **`lines`**: `Lines` unfolds a text (CRLF, LF or CR; a byte order mark is skipped);
|
||||
`ContentLine::parse` splits `NAME;PARAM="quoted",x:value` into an upper-cased name,
|
||||
parameters (RFC 6868 caret escapes undone) and the value as written; `fold` writes a line in
|
||||
75-octet pieces without splitting a character; `param_value` quotes and escapes a parameter.
|
||||
- **`values`**: `When` (`DATE`, floating or zoned `DATE-TIME`, UTC `DATE-TIME`, with the ISO 8601
|
||||
punctuation some writers add), `parse_duration`, `escape_text` / `unescape_text`, `split_list`,
|
||||
`format_date` / `format_local` / `format_utc`, and the wall-clock helpers they rest on.
|
||||
- **`rrule`**: `Recurrence`, the subset of RRULE calendars show and edit (daily, weekly, monthly,
|
||||
yearly; interval; weekdays; the nth or last weekday; count; until), `parse_rrule` and
|
||||
`format_rrule`, and `format_for` / `format_zoned`, which spell out the start's weekday and
|
||||
month and give `UNTIL` the start's value type. A rule outside the subset is read as the nearest
|
||||
it holds.
|
||||
- **`zones`**: `TimeZones::collect` reads a text's `VTIMEZONE`s (yearly rules by weekday or day
|
||||
of the month, `UNTIL`, `COUNT`, `RDATE`s); a `VTimezone` answers `offset_at(instant)`,
|
||||
`offset_for_wall(wall)` and `instant(wall)`, with RFC 5545's reading of skipped and repeated
|
||||
hours. `write_vtimezone` writes a zone from the caller's `ZoneRules` (offset at an instant,
|
||||
transitions): one `DAYLIGHT` and one `STANDARD` part with yearly rules where the zone keeps one
|
||||
rule, else change by change.
|
||||
|
||||
No time-zone database is here: matching a `TZID` to an IANA or Windows zone, and which zone an
|
||||
event is shown in, are the caller's. So is what events and to-dos mean: an app reads them from
|
||||
`ContentLine`s with these values and maps them onto its own model.
|
||||
|
||||
## Hostile input
|
||||
|
||||
Nothing panics on any text.
|
||||
|
||||
| Limit | Value |
|
||||
|---|---|
|
||||
| unfolded line (`lines::MAX_LINE`) | 1 MiB |
|
||||
| parameters on a line, values of a parameter | 64 each |
|
||||
| recurrence rule | 4096 bytes, 64 parts |
|
||||
| durations | about 10,000 years |
|
||||
| zones per text (`zones::MAX_ZONES`) | 64 |
|
||||
| parts per zone, dates per part | 64 each |
|
||||
| `TZID` kept (`zones::MAX_TZID`) | 128 bytes |
|
||||
| offsets | at most 18 hours |
|
||||
| work on zone definitions per text (`zones::WORK_BUDGET`) | 20,000,000 units, spent by the caller through `TimeZones::spend` |
|
||||
|
||||
Every date and time written is in the years 1 to 9999; a value outside them is moved to the
|
||||
nearest end.
|
||||
|
||||
## Origin
|
||||
|
||||
MailCraft `crates/ical` (`lines.rs`, `values.rs`, `rrule.rs`, `zones.rs`) at
|
||||
[a44a450](https://github.com/UCS-Care-Group/mailcraft/commit/a44a45013d1c6396229c40ab60172d1882b368d5),
|
||||
by Martin Ball and the MailCraft contributors (MIT OR Apache-2.0), with their tests. MailCraft's
|
||||
own date types are replaced by `chrono`, its time-zone database by the `ZoneRules` trait (the
|
||||
writer's tests use rule zones built in the test), and its recurrence model by `Recurrence`.
|
||||
MailCraft's event and to-do reader and writer (`read.rs`, `write.rs`), and the matching of a
|
||||
`TZID` against its zone database, stay in MailCraft as the mapping onto its model.
|
||||
@@ -0,0 +1,7 @@
|
||||
# Seeds for failure cases proptest has generated in the past. It is
|
||||
# automatically read and these particular cases re-run before any
|
||||
# novel cases are generated.
|
||||
#
|
||||
# It is recommended to check this file in to source control so that
|
||||
# everyone who runs the test benefits from these saved cases.
|
||||
cc 960cb45b7c8511f655aea177ca4d647864fc5cf713e5fbdd611ddba6ed077047 # shrinks to line = "\u{feff}\u{feff}"
|
||||
@@ -0,0 +1,33 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
//! iCalendar (RFC 5545): the layers a calendar program reads and writes `.ics` text with.
|
||||
//!
|
||||
//! - [`lines`]: content lines. [`Lines`] unfolds a text, [`ContentLine::parse`] splits a line
|
||||
//! into its name, parameters (RFC 6868 caret escapes undone) and value, [`fold`] and
|
||||
//! [`param_value`] write them.
|
||||
//! - [`values`]: DATE and DATE-TIME values ([`When`]), durations, TEXT escaping, lists.
|
||||
//! - [`rrule`]: the common subset of recurrence rules ([`Recurrence`]), read and written, with the
|
||||
//! start's weekday and month spelled out where a rule leans on them.
|
||||
//! - [`zones`]: `VTIMEZONE` definitions a text brings ([`TimeZones`]), which answer the offset at
|
||||
//! an instant and the instant of a wall-clock time, and [`write_vtimezone`], which writes a
|
||||
//! definition from the caller's zone rules ([`ZoneRules`]).
|
||||
//!
|
||||
//! Times are `chrono` values; instants are seconds since 1970-01-01T00:00:00Z. There is no
|
||||
//! time-zone database here: what a `TZID` means beyond the text is the caller's.
|
||||
//!
|
||||
//! What events and to-dos mean (which properties become which fields, which zone a time is shown
|
||||
//! in) is the caller's too: it reads them from [`ContentLine`]s with these values.
|
||||
//!
|
||||
//! Input is hostile. Nothing here panics on any text: lines, parameters, rules, zones, parts and
|
||||
//! dates are capped, and work on zone definitions is budgeted ([`TimeZones::spend`]).
|
||||
|
||||
pub mod lines;
|
||||
pub mod rrule;
|
||||
pub mod values;
|
||||
pub mod zones;
|
||||
|
||||
pub use lines::{ContentLine, Lines, Param, fold, param_value};
|
||||
pub use rrule::{Freq, Recurrence, format_rrule, parse_rrule};
|
||||
pub use values::When;
|
||||
pub use zones::{TimeZones, Transition, VTimezone, ZoneRules, write_vtimezone};
|
||||
@@ -0,0 +1,321 @@
|
||||
//! Content lines (RFC 5545 §3.1–3.2): unfolding on the way in, folding on the way out, and the
|
||||
//! `NAME;PARAM=value;PARAM="quoted":value` shape in between.
|
||||
|
||||
/// Longest logical line kept, in bytes; the rest of a longer line is dropped.
|
||||
pub const MAX_LINE: usize = 1 << 20;
|
||||
/// Most parameters read from one line, and most values of one parameter.
|
||||
const MAX_PARAMS: usize = 64;
|
||||
/// Octets per physical line when writing.
|
||||
const FOLD_AT: usize = 75;
|
||||
|
||||
/// The longest prefix of `s` of at most `max` bytes that ends on a char boundary.
|
||||
pub fn prefix(s: &str, max: usize) -> &str {
|
||||
if s.len() <= max {
|
||||
return s;
|
||||
}
|
||||
let mut end = max;
|
||||
while end > 0 && !s.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
s.get(..end).unwrap_or("")
|
||||
}
|
||||
|
||||
/// The logical (unfolded) lines of a text. Lines end at CRLF, LF or CR; a line that starts with a
|
||||
/// space or a tab continues the one before.
|
||||
pub struct Lines<'a> {
|
||||
rest: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> Lines<'a> {
|
||||
pub fn new(text: &'a str) -> Lines<'a> {
|
||||
Lines { rest: text.strip_prefix('\u{feff}').unwrap_or(text) }
|
||||
}
|
||||
|
||||
/// The next physical line, without its line break.
|
||||
fn physical(&mut self) -> Option<&'a str> {
|
||||
if self.rest.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let bytes = self.rest.as_bytes();
|
||||
let end = bytes.iter().position(|b| matches!(b, b'\r' | b'\n')).unwrap_or(bytes.len());
|
||||
let skip = match (bytes.get(end), bytes.get(end.saturating_add(1))) {
|
||||
(Some(b'\r'), Some(b'\n')) => 2,
|
||||
(Some(_), _) => 1,
|
||||
(None, _) => 0,
|
||||
};
|
||||
let line = self.rest.get(..end).unwrap_or("");
|
||||
self.rest = self.rest.get(end.saturating_add(skip)..).unwrap_or("");
|
||||
Some(line)
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Lines<'_> {
|
||||
type Item = String;
|
||||
|
||||
fn next(&mut self) -> Option<String> {
|
||||
let mut line = String::new();
|
||||
push_capped(&mut line, self.physical()?);
|
||||
while self.rest.starts_with([' ', '\t']) {
|
||||
let Some(more) = self.physical() else { break };
|
||||
push_capped(&mut line, more.get(1..).unwrap_or(""));
|
||||
}
|
||||
Some(line)
|
||||
}
|
||||
}
|
||||
|
||||
fn push_capped(line: &mut String, more: &str) {
|
||||
line.push_str(prefix(more, MAX_LINE.saturating_sub(line.len())));
|
||||
}
|
||||
|
||||
/// One property parameter: `CN="Ada Lovelace"`, `ROLE=OPT-PARTICIPANT`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Param {
|
||||
/// Upper-cased.
|
||||
pub name: String,
|
||||
pub values: Vec<String>,
|
||||
}
|
||||
|
||||
/// One property: `ATTENDEE;CN=Ada:mailto:ada@example.org`.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ContentLine {
|
||||
/// Upper-cased.
|
||||
pub name: String,
|
||||
pub params: Vec<Param>,
|
||||
/// As written: still escaped.
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
impl ContentLine {
|
||||
/// Parse an unfolded line; `None` for a line without a name or without a `:`.
|
||||
pub fn parse(line: &str) -> Option<ContentLine> {
|
||||
// Every delimiter is ASCII, so the byte positions found here are char boundaries.
|
||||
let bytes = line.as_bytes();
|
||||
let stop = |from: usize, at: &[u8]| {
|
||||
bytes.get(from..).and_then(|b| b.iter().position(|c| at.contains(c))).map_or(bytes.len(), |i| from.saturating_add(i))
|
||||
};
|
||||
let mut pos = stop(0, b";:");
|
||||
let name = line.get(..pos)?.trim();
|
||||
if name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut params = Vec::new();
|
||||
while bytes.get(pos) == Some(&b';') {
|
||||
let start = pos.saturating_add(1);
|
||||
pos = stop(start, b"=;:");
|
||||
let param = line.get(start..pos)?.trim().to_ascii_uppercase();
|
||||
let mut values = Vec::new();
|
||||
while matches!(bytes.get(pos), Some(b'=' | b',')) {
|
||||
let from = pos.saturating_add(1);
|
||||
let value = if bytes.get(from) == Some(&b'"') {
|
||||
let open = from.saturating_add(1);
|
||||
let close = stop(open, b"\"");
|
||||
// A quote that never closes: the line has no value.
|
||||
bytes.get(close)?;
|
||||
pos = stop(close, b",;:");
|
||||
line.get(open..close)?
|
||||
} else {
|
||||
pos = stop(from, b",;:");
|
||||
line.get(from..pos)?
|
||||
};
|
||||
if values.len() < MAX_PARAMS {
|
||||
values.push(caret_decode(value));
|
||||
}
|
||||
}
|
||||
if !param.is_empty() && params.len() < MAX_PARAMS {
|
||||
params.push(Param { name: param, values });
|
||||
}
|
||||
}
|
||||
if bytes.get(pos) != Some(&b':') {
|
||||
return None;
|
||||
}
|
||||
Some(ContentLine { name: name.to_ascii_uppercase(), params, value: line.get(pos.saturating_add(1)..)?.to_string() })
|
||||
}
|
||||
|
||||
/// The first value of the parameter `name` (upper case).
|
||||
pub fn param(&self, name: &str) -> Option<&str> {
|
||||
self.params.iter().find(|p| p.name == name).and_then(|p| p.values.first()).map(String::as_str)
|
||||
}
|
||||
}
|
||||
|
||||
/// Undo RFC 6868: `^n` is a line break, `^'` a double quote, `^^` a caret.
|
||||
fn caret_decode(s: &str) -> String {
|
||||
if !s.contains('^') {
|
||||
return s.to_string();
|
||||
}
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut chars = s.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
let decoded = match (c, chars.peek()) {
|
||||
('^', Some('n')) => Some('\n'),
|
||||
('^', Some('\'')) => Some('"'),
|
||||
('^', Some('^')) => Some('^'),
|
||||
_ => None,
|
||||
};
|
||||
match decoded {
|
||||
Some(d) => {
|
||||
out.push(d);
|
||||
chars.next();
|
||||
}
|
||||
None => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A parameter value ready to write: RFC 6868 escapes for what a parameter can't hold, other
|
||||
/// control characters dropped, and quotes around it when it needs them.
|
||||
pub fn param_value(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len().saturating_add(2));
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'^' => out.push_str("^^"),
|
||||
'"' => out.push_str("^'"),
|
||||
'\n' => out.push_str("^n"),
|
||||
'\t' => out.push(c),
|
||||
c if c.is_control() => {}
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
if out.contains([';', ':', ',']) || out.starts_with(' ') || out.ends_with(' ') { format!("\"{out}\"") } else { out }
|
||||
}
|
||||
|
||||
/// Append `line` to `out` with CRLF, folded so no physical line is longer than 75 octets and no
|
||||
/// character is split.
|
||||
pub fn fold(out: &mut String, line: &str) {
|
||||
let mut rest = line;
|
||||
let mut room = FOLD_AT;
|
||||
while rest.len() > room {
|
||||
let head = prefix(rest, room);
|
||||
if head.is_empty() {
|
||||
break;
|
||||
}
|
||||
out.push_str(head);
|
||||
out.push_str("\r\n ");
|
||||
rest = rest.get(head.len()..).unwrap_or("");
|
||||
// The leading space counts.
|
||||
room = FOLD_AT - 1;
|
||||
}
|
||||
out.push_str(rest);
|
||||
out.push_str("\r\n");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn lines(text: &str) -> Vec<String> {
|
||||
Lines::new(text).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unfolds_every_kind_of_line_break() {
|
||||
assert_eq!(lines("A:1\r\nB:2\nC:3\rD:4"), ["A:1", "B:2", "C:3", "D:4"]);
|
||||
assert_eq!(lines("SUMMARY:Lunch \r\n with Ada\r\n"), ["SUMMARY:Lunch with Ada"]);
|
||||
assert_eq!(lines("SUMMARY:Lun\n\tch\n twice\n"), ["SUMMARY:Lunch twice"]);
|
||||
// Only the one space is the fold.
|
||||
assert_eq!(lines("X:a\r\n b"), ["X:a b"]);
|
||||
assert_eq!(lines("X:a \r\nY:b"), ["X:a ", "Y:b"]);
|
||||
assert!(lines("").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_byte_order_mark_and_blank_lines_are_harmless() {
|
||||
assert_eq!(lines("\u{feff}BEGIN:VCALENDAR\r\n\r\nEND:VCALENDAR"), ["BEGIN:VCALENDAR", "", "END:VCALENDAR"]);
|
||||
assert_eq!(ContentLine::parse(""), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_names_parameters_and_values() {
|
||||
let l = ContentLine::parse("dtstart;tzid=Example/Riverton;Value=DATE-TIME:20261012T090000").unwrap();
|
||||
assert_eq!(l.name, "DTSTART");
|
||||
assert_eq!(l.param("TZID"), Some("Example/Riverton"));
|
||||
assert_eq!(l.param("VALUE"), Some("DATE-TIME"));
|
||||
assert_eq!(l.param("CN"), None);
|
||||
assert_eq!(l.value, "20261012T090000");
|
||||
let plain = ContentLine::parse("SUMMARY:Budget: phase two; hiring").unwrap();
|
||||
assert_eq!((plain.name.as_str(), plain.params.len(), plain.value.as_str()), ("SUMMARY", 0, "Budget: phase two; hiring"));
|
||||
assert_eq!(ContentLine::parse("DESCRIPTION:").unwrap().value, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quoted_parameters_hide_delimiters() {
|
||||
let l = ContentLine::parse("ATTENDEE;CN=\"Okafor, Nadia: lead; planning\";ROLE=REQ-PARTICIPANT:mailto:nadia@example.org").unwrap();
|
||||
assert_eq!(l.param("CN"), Some("Okafor, Nadia: lead; planning"));
|
||||
assert_eq!(l.param("ROLE"), Some("REQ-PARTICIPANT"));
|
||||
assert_eq!(l.value, "mailto:nadia@example.org");
|
||||
let several = ContentLine::parse("X-TEST;MEMBER=\"mailto:a@example.org\",\"mailto:b@example.org\",c:v").unwrap();
|
||||
assert_eq!(several.params[0].values, ["mailto:a@example.org", "mailto:b@example.org", "c"]);
|
||||
// A parameter without a value, and an empty one.
|
||||
let bare = ContentLine::parse("X-TEST;FLAG;EMPTY=:v").unwrap();
|
||||
assert_eq!(bare.params.len(), 2);
|
||||
assert_eq!(bare.param("FLAG"), None);
|
||||
assert_eq!(bare.param("EMPTY"), Some(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broken_lines_are_refused() {
|
||||
for bad in ["no colon here", ":value without a name", ";CN=x:y", "ATTENDEE;CN=\"never closed:mailto:a@example.org", "ATTENDEE;CN=x", " "] {
|
||||
assert_eq!(ContentLine::parse(bad), None, "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parameter_values_escape_and_quote() {
|
||||
assert_eq!(param_value("Ada Lovelace"), "Ada Lovelace");
|
||||
assert_eq!(param_value("Lovelace, Ada"), "\"Lovelace, Ada\"");
|
||||
assert_eq!(param_value("Ada \"the Countess\" ^1\nLine\r\u{7}"), "Ada ^'the Countess^' ^^1^nLine");
|
||||
for name in ["Ada", "Lovelace, Ada", "a\"b^c\nd", "x:y;z", "^n^'^^", "Zoë — 東京"] {
|
||||
let l = ContentLine::parse(&format!("ORGANIZER;CN={}:mailto:a@example.org", param_value(name))).unwrap();
|
||||
assert_eq!(l.param("CN"), Some(name));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folds_at_75_octets_without_splitting_characters() {
|
||||
let line = format!("SUMMARY:{}", "é東🎉x".repeat(40));
|
||||
let mut out = String::new();
|
||||
fold(&mut out, &line);
|
||||
assert!(out.ends_with("\r\n"));
|
||||
let physical: Vec<&str> = out.trim_end_matches("\r\n").split("\r\n").collect();
|
||||
assert!(physical.len() > 3);
|
||||
assert!(physical.iter().all(|l| l.len() <= 75), "{physical:?}");
|
||||
assert!(physical.iter().skip(1).all(|l| l.starts_with(' ')));
|
||||
assert_eq!(lines(&out), [line]);
|
||||
// A short line is left alone, and an exact fit isn't folded.
|
||||
let mut short = String::new();
|
||||
fold(&mut short, &"x".repeat(75));
|
||||
assert_eq!(short, format!("{}\r\n", "x".repeat(75)));
|
||||
let mut one_more = String::new();
|
||||
fold(&mut one_more, &"x".repeat(76));
|
||||
assert_eq!(one_more, format!("{}\r\n x\r\n", "x".repeat(75)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enormous_lines_are_capped() {
|
||||
let text = format!("SUMMARY:{}\r\n {}\r\nUID:after", "é".repeat(MAX_LINE), "x".repeat(1000));
|
||||
let got = lines(&text);
|
||||
assert_eq!(got.len(), 2);
|
||||
assert!(got[0].len() <= MAX_LINE);
|
||||
assert_eq!(got[1], "UID:after");
|
||||
assert_eq!(prefix("héllo", 2), "h");
|
||||
assert_eq!(prefix("héllo", 0), "");
|
||||
assert_eq!(prefix("héllo", 99), "héllo");
|
||||
}
|
||||
|
||||
proptest::proptest! {
|
||||
#[test]
|
||||
fn folding_then_unfolding_gives_the_line_back(line in "[^\r\n]{0,400}") {
|
||||
let mut out = String::new();
|
||||
fold(&mut out, &line);
|
||||
proptest::prop_assert!(out.split("\r\n").all(|l| l.len() <= 75));
|
||||
// A byte order mark at the very start of a text isn't part of its first line.
|
||||
proptest::prop_assert_eq!(Lines::new(&out).collect::<Vec<_>>(), vec![line.strip_prefix('\u{feff}').unwrap_or(&line).to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_line_parses_or_is_refused(line in proptest::prelude::any::<String>()) {
|
||||
let _ = ContentLine::parse(&line);
|
||||
let _ = Lines::new(&line).count();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,516 @@
|
||||
//! Recurrence rules (RFC 5545 §3.3.10): the common subset a calendar keeps.
|
||||
|
||||
use chrono::{Datelike, NaiveDate, NaiveDateTime, Weekday};
|
||||
|
||||
use crate::lines::prefix;
|
||||
use crate::values::{When, format_date, valid_time};
|
||||
|
||||
type Timestamp = i64;
|
||||
|
||||
/// How often a rule repeats. `SECONDLY`, `MINUTELY` and `HOURLY` are not held.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub enum Freq {
|
||||
Daily,
|
||||
#[default]
|
||||
Weekly,
|
||||
Monthly,
|
||||
Yearly,
|
||||
}
|
||||
|
||||
impl Freq {
|
||||
/// From `DAILY`, `weekly`…: any letter case.
|
||||
pub fn parse(s: &str) -> Option<Freq> {
|
||||
match s.trim().to_ascii_uppercase().as_str() {
|
||||
"DAILY" => Some(Freq::Daily),
|
||||
"WEEKLY" => Some(Freq::Weekly),
|
||||
"MONTHLY" => Some(Freq::Monthly),
|
||||
"YEARLY" => Some(Freq::Yearly),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `DAILY`, `WEEKLY`, `MONTHLY`, `YEARLY`.
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Freq::Daily => "DAILY",
|
||||
Freq::Weekly => "WEEKLY",
|
||||
Freq::Monthly => "MONTHLY",
|
||||
Freq::Yearly => "YEARLY",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How an event repeats: the subset of RRULE that calendars show and edit.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Recurrence {
|
||||
pub freq: Freq,
|
||||
/// Every `interval` days/weeks/months/years; 0 reads as 1.
|
||||
pub interval: u32,
|
||||
/// Weekly: the days of the week (none: the start's own). Daily: only on these days. Monthly and
|
||||
/// yearly with `nth`: the weekday(s) counted.
|
||||
pub by_day: Vec<Weekday>,
|
||||
/// Monthly and yearly: the `nth` `by_day` weekday of the month (1–5, or -1 for the last)
|
||||
/// instead of the start's day of the month.
|
||||
pub nth: Option<i8>,
|
||||
/// Ends after this many occurrences.
|
||||
pub count: Option<u32>,
|
||||
/// Ends on this day (inclusive).
|
||||
pub until: Option<NaiveDate>,
|
||||
}
|
||||
|
||||
impl Recurrence {
|
||||
/// Every day, week, month or year.
|
||||
pub fn every(freq: Freq) -> Recurrence {
|
||||
Recurrence { freq, interval: 1, ..Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
/// The days of the week, Monday first.
|
||||
pub const WEEKDAYS: [Weekday; 7] = [Weekday::Mon, Weekday::Tue, Weekday::Wed, Weekday::Thu, Weekday::Fri, Weekday::Sat, Weekday::Sun];
|
||||
|
||||
/// `MO`, `TU`… as iCalendar writes a weekday.
|
||||
pub fn weekday_code(day: Weekday) -> &'static str {
|
||||
match day {
|
||||
Weekday::Mon => "MO",
|
||||
Weekday::Tue => "TU",
|
||||
Weekday::Wed => "WE",
|
||||
Weekday::Thu => "TH",
|
||||
Weekday::Fri => "FR",
|
||||
Weekday::Sat => "SA",
|
||||
Weekday::Sun => "SU",
|
||||
}
|
||||
}
|
||||
|
||||
/// The weekday of `MO`, `tu`…; `None` for anything else.
|
||||
pub fn weekday_of_code(s: &str) -> Option<Weekday> {
|
||||
WEEKDAYS.into_iter().find(|d| weekday_code(*d).eq_ignore_ascii_case(s.trim()))
|
||||
}
|
||||
|
||||
/// Longest rule read, in bytes, and most parts (and most `BYDAY` days) of one rule.
|
||||
const MAX_RULE: usize = 4096;
|
||||
const MAX_PARTS: usize = 64;
|
||||
|
||||
/// `FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE` as a [`Recurrence`]; `None` for a rule the model can't hold
|
||||
/// (`SECONDLY`, `MINUTELY`, `HOURLY`, junk).
|
||||
///
|
||||
/// `UNTIL` gives its date, whether it is written as a date or a date-time. `BYDAY=2TU`, `BYDAY=-1FR`
|
||||
/// and `BYDAY=TU;BYSETPOS=2` give `nth`. Parts that only restate the start (`BYMONTH`,
|
||||
/// `BYMONTHDAY`, `WKST`) and parts the model has no place for are ignored.
|
||||
pub fn parse_rrule(rule: &str) -> Option<Recurrence> {
|
||||
parse_with(rule, None)
|
||||
}
|
||||
|
||||
/// [`parse_rrule`] for a rule read from a file: with `offset_at` (minutes east of UTC at an
|
||||
/// instant), a UTC `UNTIL` gives its local day.
|
||||
pub fn parse_with(rule: &str, offset_at: Option<&dyn Fn(Timestamp) -> i32>) -> Option<Recurrence> {
|
||||
let rule = prefix(rule.trim(), MAX_RULE);
|
||||
let rule = match rule.get(..6) {
|
||||
Some(name) if name.eq_ignore_ascii_case("RRULE:") => rule.get(6..).unwrap_or(""),
|
||||
_ => rule,
|
||||
};
|
||||
let mut freq = None;
|
||||
let mut interval = 1u32;
|
||||
let (mut count, mut until, mut set_pos) = (None, None, None);
|
||||
let mut days: Vec<(Option<i32>, Weekday)> = Vec::new();
|
||||
// Parts that pick days in a way the model can't: with them, a bare BYDAY means something else.
|
||||
let mut limited = false;
|
||||
for part in rule.split(';').take(MAX_PARTS) {
|
||||
let Some((key, value)) = part.split_once('=') else { continue };
|
||||
let value = value.trim();
|
||||
match key.trim().to_ascii_uppercase().as_str() {
|
||||
"FREQ" => freq = Some(Freq::parse(value)?),
|
||||
"INTERVAL" => interval = value.parse().ok().filter(|n| *n > 0).unwrap_or(1),
|
||||
"COUNT" => count = value.parse::<u32>().ok(),
|
||||
"UNTIL" => {
|
||||
until = When::parse(value).map(|w| match offset_at {
|
||||
Some(offset_at) => w.local(offset_at).date(),
|
||||
None => w.wall().date(),
|
||||
})
|
||||
}
|
||||
"BYDAY" => days = value.split(',').take(MAX_PARTS).filter_map(by_day).collect(),
|
||||
"BYSETPOS" => set_pos = value.parse::<i32>().ok(),
|
||||
"BYMONTH" | "BYMONTHDAY" | "BYYEARDAY" | "BYWEEKNO" => limited = true,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut freq = freq?;
|
||||
let by_month = matches!(freq, Freq::Monthly | Freq::Yearly);
|
||||
// `1MO,3MO` isn't one position: the first one is kept.
|
||||
let ordinal = days.iter().find_map(|(ordinal, _)| *ordinal);
|
||||
if by_month && ordinal.is_some() {
|
||||
days.retain(|(o, _)| *o == ordinal);
|
||||
}
|
||||
let mut by_day: Vec<Weekday> = Vec::new();
|
||||
for (_, day) in &days {
|
||||
if !by_day.contains(day) {
|
||||
by_day.push(*day);
|
||||
}
|
||||
}
|
||||
let mut nth = None;
|
||||
if by_month {
|
||||
nth = match (ordinal, set_pos) {
|
||||
(Some(ordinal), _) => position(ordinal),
|
||||
// A position in a set of several weekdays ("the last working day") isn't a weekday's position.
|
||||
(None, Some(pos)) if by_day.len() == 1 || (by_day.is_empty() && !limited) => position(pos),
|
||||
_ => None,
|
||||
};
|
||||
if nth.is_none() {
|
||||
if freq == Freq::Monthly && interval == 1 && !by_day.is_empty() && ordinal.is_none() && set_pos.is_none() && !limited {
|
||||
// Every Monday of every month is every Monday.
|
||||
freq = Freq::Weekly;
|
||||
} else {
|
||||
by_day.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Recurrence { freq, interval, by_day, nth, count, until })
|
||||
}
|
||||
|
||||
/// `MO`, `2TU`, `-1FR`: the position, when there is one, and the day.
|
||||
fn by_day(item: &str) -> Option<(Option<i32>, Weekday)> {
|
||||
let item = item.trim();
|
||||
let split = item.len().checked_sub(2)?;
|
||||
let day = weekday_of_code(item.get(split..)?)?;
|
||||
let ordinal = item.get(..split)?;
|
||||
Some((if ordinal.is_empty() { None } else { Some(ordinal.parse::<i32>().ok()?) }, day))
|
||||
}
|
||||
|
||||
/// A position the model holds: the first to the fifth, or the last.
|
||||
fn position(n: i32) -> Option<i8> {
|
||||
if (1..=5).contains(&n) || n == -1 { i8::try_from(n).ok() } else { None }
|
||||
}
|
||||
|
||||
/// A [`Recurrence`] as an RRULE value: `FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE`.
|
||||
///
|
||||
/// An `nth` without a `by_day` means "the start's weekday", which a rule alone can't say: it is
|
||||
/// written as a bare `BYSETPOS`, which [`parse_rrule`] reads back but other programs may not.
|
||||
/// [`format_for`] knows the start and writes the weekday (and the month of a yearly rule) out. For monthly and yearly rules without an `nth` the model ignores `by_day`, so it isn't
|
||||
/// written.
|
||||
pub fn format_rrule(r: &Recurrence) -> String {
|
||||
format(r, None, None)
|
||||
}
|
||||
|
||||
/// The rule of an event that starts at `start`: the start's weekday and month are spelled out
|
||||
/// where the rule leans on them, and `UNTIL` has the start's value type.
|
||||
pub fn format_for(r: &Recurrence, start: NaiveDateTime, all_day: bool) -> String {
|
||||
format(r, Some((valid_time(start), all_day)), None)
|
||||
}
|
||||
|
||||
/// The rule of a timed event whose start is written in a zone: as [`format_for`], with `UNTIL`
|
||||
/// as the instant `until` in UTC, which is how RFC 5545 has it beside such a start.
|
||||
pub fn format_zoned(r: &Recurrence, start: NaiveDateTime, until: Option<Timestamp>) -> String {
|
||||
format(r, Some((valid_time(start), false)), until)
|
||||
}
|
||||
|
||||
fn format(r: &Recurrence, start: Option<(NaiveDateTime, bool)>, until_at: Option<Timestamp>) -> String {
|
||||
let mut out = format!("FREQ={}", r.freq.as_str());
|
||||
if r.interval > 1 {
|
||||
out.push_str(&format!(";INTERVAL={}", r.interval));
|
||||
}
|
||||
let mut days: Vec<Weekday> = Vec::new();
|
||||
for day in &r.by_day {
|
||||
if !days.contains(day) {
|
||||
days.push(*day);
|
||||
}
|
||||
}
|
||||
let list = |days: &[Weekday], position: &str| days.iter().map(|d| format!("{position}{}", weekday_code(*d))).collect::<Vec<_>>().join(",");
|
||||
match (r.freq, r.nth.filter(|n| *n != 0)) {
|
||||
(Freq::Daily | Freq::Weekly, _) => {
|
||||
if !days.is_empty() {
|
||||
out.push_str(&format!(";BYDAY={}", list(&days, "")));
|
||||
}
|
||||
}
|
||||
(_, None) => {}
|
||||
(freq, Some(nth)) => {
|
||||
// Any negative position is "the last" to the model.
|
||||
let nth = nth.max(-1);
|
||||
if let Some((start, _)) = start {
|
||||
if days.is_empty() {
|
||||
days.push(start.date().weekday());
|
||||
}
|
||||
if freq == Freq::Yearly {
|
||||
out.push_str(&format!(";BYMONTH={}", start.date().month()));
|
||||
}
|
||||
}
|
||||
if days.is_empty() {
|
||||
out.push_str(&format!(";BYSETPOS={nth}"));
|
||||
} else {
|
||||
out.push_str(&format!(";BYDAY={}", list(&days, &nth.to_string())));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(count) = r.count {
|
||||
out.push_str(&format!(";COUNT={count}"));
|
||||
}
|
||||
if let (Some(_), Some(at)) = (r.until, until_at) {
|
||||
out.push_str(&format!(";UNTIL={}", crate::values::format_utc(at)));
|
||||
} else if let Some(until) = r.until {
|
||||
out.push_str(&format!(";UNTIL={}", format_date(until)));
|
||||
// The whole last day of a timed series.
|
||||
if matches!(start, Some((_, false))) {
|
||||
out.push_str("T235959");
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use proptest::prelude::*;
|
||||
|
||||
use super::*;
|
||||
use crate::values::at_midnight;
|
||||
|
||||
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(y, m, day).unwrap()
|
||||
}
|
||||
|
||||
fn rule(freq: Freq) -> Recurrence {
|
||||
Recurrence::every(freq)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_the_common_rules() {
|
||||
assert_eq!(parse_rrule("FREQ=DAILY"), Some(rule(Freq::Daily)));
|
||||
assert_eq!(
|
||||
parse_rrule("FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE"),
|
||||
Some(Recurrence { interval: 2, by_day: vec![Weekday::Mon, Weekday::Wed], ..rule(Freq::Weekly) })
|
||||
);
|
||||
assert_eq!(
|
||||
parse_rrule("freq=weekly;byday=fr,mo;count=10;wkst=su"),
|
||||
Some(Recurrence { by_day: vec![Weekday::Fri, Weekday::Mon], count: Some(10), ..rule(Freq::Weekly) })
|
||||
);
|
||||
assert_eq!(
|
||||
parse_rrule("FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"),
|
||||
Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Tue, Weekday::Wed, Weekday::Thu, Weekday::Fri], ..rule(Freq::Daily) })
|
||||
);
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;INTERVAL=3"), Some(Recurrence { interval: 3, ..rule(Freq::Monthly) }));
|
||||
assert_eq!(parse_rrule("FREQ=YEARLY"), Some(rule(Freq::Yearly)));
|
||||
// The property name and stray spaces are let through.
|
||||
assert_eq!(parse_rrule(" RRULE:FREQ=DAILY; INTERVAL = 4 ;"), Some(Recurrence { interval: 4, ..rule(Freq::Daily) }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn positions_come_from_ordinals_or_bysetpos() {
|
||||
let second_tue = Recurrence { by_day: vec![Weekday::Tue], nth: Some(2), ..rule(Freq::Monthly) };
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=2TU"), Some(second_tue.clone()));
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=+2TU"), Some(second_tue.clone()));
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=TU;BYSETPOS=2"), Some(second_tue));
|
||||
let last_fri = Recurrence { by_day: vec![Weekday::Fri], nth: Some(-1), ..rule(Freq::Monthly) };
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=-1FR"), Some(last_fri.clone()));
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;BYSETPOS=-1;BYDAY=FR"), Some(last_fri));
|
||||
// The fourth Thursday of November: the month restates the start.
|
||||
assert_eq!(
|
||||
parse_rrule("FREQ=YEARLY;BYMONTH=11;BYDAY=4TH"),
|
||||
Some(Recurrence { by_day: vec![Weekday::Thu], nth: Some(4), ..rule(Freq::Yearly) })
|
||||
);
|
||||
assert_eq!(
|
||||
parse_rrule("FREQ=MONTHLY;BYDAY=1MO,1FR"),
|
||||
Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Fri], nth: Some(1), ..rule(Freq::Monthly) })
|
||||
);
|
||||
// Positions mean nothing in a weekly rule.
|
||||
assert_eq!(parse_rrule("FREQ=WEEKLY;BYDAY=2TU"), Some(Recurrence { by_day: vec![Weekday::Tue], ..rule(Freq::Weekly) }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rules_the_model_cannot_hold_fall_back() {
|
||||
// Two positions: the first is kept.
|
||||
assert_eq!(
|
||||
parse_rrule("FREQ=MONTHLY;BYDAY=1MO,3MO,1WE"),
|
||||
Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Wed], nth: Some(1), ..rule(Freq::Monthly) })
|
||||
);
|
||||
// The last working day, the second-to-last Friday, Friday the 13th and the like: plain monthly.
|
||||
for r in [
|
||||
"FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1",
|
||||
"FREQ=MONTHLY;BYDAY=-2FR",
|
||||
"FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13",
|
||||
"FREQ=MONTHLY;BYMONTHDAY=15",
|
||||
"FREQ=MONTHLY;BYMONTHDAY=1,2,3;BYSETPOS=1",
|
||||
] {
|
||||
assert_eq!(parse_rrule(r), Some(rule(Freq::Monthly)), "{r}");
|
||||
}
|
||||
// The 20th Monday of the year.
|
||||
assert_eq!(parse_rrule("FREQ=YEARLY;BYDAY=20MO"), Some(rule(Freq::Yearly)));
|
||||
// Every Monday and Thursday of every month is weekly; every other month isn't.
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=MO,TH"), Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Thu], ..rule(Freq::Weekly) }));
|
||||
assert_eq!(parse_rrule("FREQ=MONTHLY;INTERVAL=2;BYDAY=MO"), Some(Recurrence { interval: 2, ..rule(Freq::Monthly) }));
|
||||
assert_eq!(parse_rrule("FREQ=YEARLY;BYMONTH=3,6,9"), Some(rule(Freq::Yearly)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn until_is_a_date_whatever_its_form() {
|
||||
let until = |r: &str| parse_rrule(r).and_then(|r| r.until);
|
||||
assert_eq!(until("FREQ=DAILY;UNTIL=20261231"), Some(d(2026, 12, 31)));
|
||||
assert_eq!(until("FREQ=DAILY;UNTIL=20261231T235959"), Some(d(2026, 12, 31)));
|
||||
assert_eq!(until("FREQ=DAILY;UNTIL=20261231T030000Z"), Some(d(2026, 12, 31)));
|
||||
assert_eq!(until("FREQ=DAILY;UNTIL=someday"), None);
|
||||
// Read from a file, a UTC time is a local day.
|
||||
let west = parse_with("FREQ=DAILY;UNTIL=20261231T030000Z", Some(&|_| -300)).unwrap();
|
||||
assert_eq!(west.until, Some(d(2026, 12, 30)));
|
||||
let east = parse_with("FREQ=DAILY;UNTIL=20261230T220000Z", Some(&|_| 600)).unwrap();
|
||||
assert_eq!(east.until, Some(d(2026, 12, 31)));
|
||||
assert_eq!(parse_with("FREQ=DAILY;UNTIL=20261231", Some(&|_| -300)).unwrap().until, Some(d(2026, 12, 31)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn junk_and_fast_rules_are_none() {
|
||||
for bad in [
|
||||
"",
|
||||
"FREQ=",
|
||||
"FREQ=HOURLY",
|
||||
"FREQ=MINUTELY;INTERVAL=5",
|
||||
"FREQ=SECONDLY",
|
||||
"FREQ=SOMETIMES",
|
||||
"INTERVAL=2;BYDAY=MO",
|
||||
"weekly",
|
||||
";;;===",
|
||||
"RRULE:",
|
||||
] {
|
||||
assert_eq!(parse_rrule(bad), None, "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn junk_parts_are_ignored() {
|
||||
let got = parse_rrule("FREQ=WEEKLY;INTERVAL=0;COUNT=many;BYDAY=MO,XX,,7,MO,é;BYSETPOS=x;X-THING=1;novalue").unwrap();
|
||||
assert_eq!(got, Recurrence { by_day: vec![Weekday::Mon], ..rule(Freq::Weekly) });
|
||||
assert_eq!(parse_rrule("FREQ=DAILY;INTERVAL=99999999999;COUNT=99999999999").unwrap(), rule(Freq::Daily));
|
||||
assert_eq!(parse_rrule("FREQ=DAILY;COUNT=0").unwrap().count, Some(0));
|
||||
let long = format!("FREQ=WEEKLY;BYDAY={}", "MO,".repeat(100_000));
|
||||
assert_eq!(parse_rrule(&long).unwrap().by_day, [Weekday::Mon]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_rules() {
|
||||
assert_eq!(format_rrule(&rule(Freq::Daily)), "FREQ=DAILY");
|
||||
assert_eq!(
|
||||
format_rrule(&Recurrence { interval: 2, by_day: vec![Weekday::Mon, Weekday::Wed, Weekday::Mon], ..rule(Freq::Weekly) }),
|
||||
"FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE"
|
||||
);
|
||||
assert_eq!(format_rrule(&Recurrence { count: Some(10), ..rule(Freq::Yearly) }), "FREQ=YEARLY;COUNT=10");
|
||||
assert_eq!(format_rrule(&Recurrence { until: Some(d(2026, 12, 31)), ..rule(Freq::Monthly) }), "FREQ=MONTHLY;UNTIL=20261231");
|
||||
assert_eq!(format_rrule(&Recurrence { by_day: vec![Weekday::Tue], nth: Some(2), ..rule(Freq::Monthly) }), "FREQ=MONTHLY;BYDAY=2TU");
|
||||
assert_eq!(
|
||||
format_rrule(&Recurrence { by_day: vec![Weekday::Mon, Weekday::Fri], nth: Some(-1), ..rule(Freq::Monthly) }),
|
||||
"FREQ=MONTHLY;BYDAY=-1MO,-1FR"
|
||||
);
|
||||
assert_eq!(format_rrule(&Recurrence { nth: Some(3), ..rule(Freq::Monthly) }), "FREQ=MONTHLY;BYSETPOS=3");
|
||||
// Interval 0 reads as 1, and the model ignores the days of a monthly rule without a position.
|
||||
assert_eq!(format_rrule(&Recurrence { interval: 0, by_day: vec![Weekday::Mon], nth: Some(0), ..rule(Freq::Monthly) }), "FREQ=MONTHLY");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_event_s_rule_spells_out_what_the_start_implies() {
|
||||
let tuesday = d(2026, 10, 13).and_hms_opt(9, 0, 0).unwrap();
|
||||
let r = Recurrence { nth: Some(2), until: Some(d(2027, 6, 30)), ..rule(Freq::Monthly) };
|
||||
assert_eq!(format_for(&r, tuesday, false), "FREQ=MONTHLY;BYDAY=2TU;UNTIL=20270630T235959");
|
||||
assert_eq!(format_for(&r, tuesday, true), "FREQ=MONTHLY;BYDAY=2TU;UNTIL=20270630");
|
||||
let thanksgiving = Recurrence { by_day: vec![Weekday::Thu], nth: Some(4), ..rule(Freq::Yearly) };
|
||||
assert_eq!(format_for(&thanksgiving, at_midnight(d(2026, 11, 26)), true), "FREQ=YEARLY;BYMONTH=11;BYDAY=4TH");
|
||||
assert_eq!(format_for(&Recurrence { nth: Some(i8::MIN), ..rule(Freq::Yearly) }, tuesday, false), "FREQ=YEARLY;BYMONTH=10;BYDAY=-1TU");
|
||||
// What it writes reads back as the same series.
|
||||
assert_eq!(parse_rrule("FREQ=YEARLY;BYMONTH=11;BYDAY=4TH"), Some(thanksgiving));
|
||||
// A start in a year iCalendar can't write is moved into the years it can.
|
||||
let written = format_for(&Recurrence { nth: Some(1), until: Some(NaiveDate::MAX), ..rule(Freq::Yearly) }, NaiveDateTime::MAX, true);
|
||||
assert_eq!(written, "FREQ=YEARLY;BYMONTH=12;BYDAY=1FR;UNTIL=99991231");
|
||||
}
|
||||
|
||||
/// Every shape the model gives a meaning: weekly and daily rules with any days, monthly and
|
||||
/// yearly rules with a position (and then any days) or without.
|
||||
fn shapes() -> Vec<Recurrence> {
|
||||
let day_sets: [&[Weekday]; 5] = [&[], &[Weekday::Tue], &[Weekday::Sun, Weekday::Mon], &[Weekday::Fri, Weekday::Mon, Weekday::Wed], &WEEKDAYS];
|
||||
let mut out = Vec::new();
|
||||
for freq in [Freq::Daily, Freq::Weekly, Freq::Monthly, Freq::Yearly] {
|
||||
for interval in [1, 2, 13, u32::MAX] {
|
||||
for count in [None, Some(0), Some(1), Some(u32::MAX)] {
|
||||
for until in [None, Some(d(2030, 1, 31)), Some(d(1, 1, 1)), Some(d(9999, 12, 31))] {
|
||||
for days in day_sets {
|
||||
let positions: &[Option<i8>] = if matches!(freq, Freq::Monthly | Freq::Yearly) {
|
||||
&[None, Some(1), Some(2), Some(3), Some(4), Some(5), Some(-1)]
|
||||
} else {
|
||||
&[None]
|
||||
};
|
||||
for nth in positions {
|
||||
if nth.is_none() && !days.is_empty() && matches!(freq, Freq::Monthly | Freq::Yearly) {
|
||||
continue;
|
||||
}
|
||||
out.push(Recurrence { freq, interval, by_day: days.to_vec(), nth: *nth, count, until });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_shape_the_model_supports_round_trips() {
|
||||
let shapes = shapes();
|
||||
assert!(shapes.len() > 1000);
|
||||
for r in shapes {
|
||||
let text = format_rrule(&r);
|
||||
assert_eq!(parse_rrule(&text), Some(r.clone()), "{text}");
|
||||
// And as an event writes it: the start is a Tuesday, so a missing day reads back as Tuesday.
|
||||
for all_day in [false, true] {
|
||||
let text = format_for(&r, d(2026, 10, 13).and_hms_opt(9, 30, 0).unwrap(), all_day);
|
||||
let mut want = r.clone();
|
||||
if want.nth.is_some() && want.by_day.is_empty() {
|
||||
want.by_day = vec![Weekday::Tue];
|
||||
}
|
||||
assert_eq!(parse_rrule(&text), Some(want), "{text}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shapes_without_a_meaning_are_normalised() {
|
||||
let normal = |r: &Recurrence| parse_rrule(&format_rrule(r)).unwrap();
|
||||
// Interval 0 is 1, duplicate days are one, position 0 is none, any negative position is the last.
|
||||
assert_eq!(normal(&Recurrence { interval: 0, ..rule(Freq::Weekly) }).interval, 1);
|
||||
assert_eq!(normal(&Recurrence { by_day: vec![Weekday::Mon; 100], ..rule(Freq::Weekly) }).by_day, [Weekday::Mon]);
|
||||
assert_eq!(normal(&Recurrence { nth: Some(0), by_day: vec![Weekday::Mon], ..rule(Freq::Monthly) }), rule(Freq::Monthly));
|
||||
assert_eq!(normal(&Recurrence { nth: Some(-7), by_day: vec![Weekday::Mon], ..rule(Freq::Monthly) }).nth, Some(-1));
|
||||
// A position no month has isn't read back.
|
||||
assert_eq!(normal(&Recurrence { nth: Some(9), by_day: vec![Weekday::Mon], ..rule(Freq::Monthly) }), rule(Freq::Monthly));
|
||||
// A position means nothing in a weekly rule.
|
||||
assert_eq!(normal(&Recurrence { nth: Some(2), ..rule(Freq::Weekly) }), rule(Freq::Weekly));
|
||||
}
|
||||
|
||||
fn recurrence() -> impl Strategy<Value = Recurrence> {
|
||||
let freq = prop_oneof![Just(Freq::Daily), Just(Freq::Weekly), Just(Freq::Monthly), Just(Freq::Yearly)];
|
||||
let days = proptest::sample::subsequence(WEEKDAYS.to_vec(), 0..=7).prop_shuffle();
|
||||
let nth = proptest::option::of(prop_oneof![1i8..=5, Just(-1i8)]);
|
||||
let until = proptest::option::of((1i32..=9999, 1u32..=12, 1u32..=28).prop_map(|(y, m, day)| d(y, m, day)));
|
||||
(freq, 1u32.., days, nth, proptest::option::of(any::<u32>()), until).prop_map(|(freq, interval, by_day, nth, count, until)| {
|
||||
let monthly = matches!(freq, Freq::Monthly | Freq::Yearly);
|
||||
let nth = if monthly { nth } else { None };
|
||||
let by_day = if monthly && nth.is_none() { Vec::new() } else { by_day };
|
||||
Recurrence { freq, interval, by_day, nth, count, until }
|
||||
})
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn any_supported_rule_round_trips(r in recurrence()) {
|
||||
prop_assert_eq!(parse_rrule(&format_rrule(&r)), Some(r));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_rule_at_all_formats_and_reads_back_stably(freq in 0u8..4, interval in any::<u32>(), days in proptest::collection::vec(0usize..7, 0..12), nth in proptest::option::of(any::<i8>()), count in proptest::option::of(any::<u32>())) {
|
||||
let freq = [Freq::Daily, Freq::Weekly, Freq::Monthly, Freq::Yearly][usize::from(freq)];
|
||||
let r = Recurrence { freq, interval, by_day: days.into_iter().filter_map(|i| WEEKDAYS.get(i).copied()).collect(), nth, count, until: None };
|
||||
let text = format_rrule(&r);
|
||||
let back = parse_rrule(&text).unwrap();
|
||||
// Reading normalises; a normal rule then round-trips.
|
||||
prop_assert_eq!(parse_rrule(&format_rrule(&back)), Some(back));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rule_text_never_panics(s in any::<String>(), parts in proptest::collection::vec("(FREQ|INTERVAL|COUNT|UNTIL|BYDAY|BYSETPOS|BYMONTH|X)=[-+A-Z0-9,]{0,12}", 0..8)) {
|
||||
let _ = parse_rrule(&s);
|
||||
let _ = parse_rrule(&parts.join(";"));
|
||||
let _ = parse_with(&parts.join(";"), Some(&|_| i32::MIN));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,419 @@
|
||||
//! Property values: dates and times, durations and escaped text.
|
||||
//!
|
||||
//! Times are `chrono` values. An instant is an `i64` of seconds since 1970-01-01T00:00:00Z, and an
|
||||
//! offset of a zone is minutes east of UTC where this crate takes one from a caller and seconds
|
||||
//! where a `VTIMEZONE` gives one. Every date and time this crate makes lies in the years 1 to 9999,
|
||||
//! the years iCalendar can write.
|
||||
|
||||
use chrono::{DateTime, NaiveDate, NaiveDateTime};
|
||||
|
||||
/// Seconds in a day.
|
||||
pub const DAY: i64 = 86_400;
|
||||
|
||||
/// Longest duration read, in seconds (some 10 000 years): sums of it can't overflow.
|
||||
const MAX_DURATION: i64 = 10_000 * 366 * DAY;
|
||||
|
||||
/// The first and the last second iCalendar can write, as seconds since 1970 on the same clock.
|
||||
const MIN_CIVIL: i64 = -62_135_596_800;
|
||||
const MAX_CIVIL: i64 = 253_402_300_799;
|
||||
|
||||
/// Seconds since 1970-01-01T00:00:00 on the same wall clock (no zone applied).
|
||||
pub fn civil_seconds(t: NaiveDateTime) -> i64 {
|
||||
t.and_utc().timestamp()
|
||||
}
|
||||
|
||||
/// The wall-clock time `secs` seconds after 1970-01-01T00:00:00, kept in the years 1 to 9999.
|
||||
pub fn from_civil_seconds(secs: i64) -> NaiveDateTime {
|
||||
DateTime::from_timestamp(secs.clamp(MIN_CIVIL, MAX_CIVIL), 0).map(|t| t.naive_utc()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The instant a wall clock shows in a zone `offset_minutes` east of UTC.
|
||||
pub fn to_timestamp(t: NaiveDateTime, offset_minutes: i32) -> i64 {
|
||||
civil_seconds(t).saturating_sub(i64::from(offset_minutes).saturating_mul(60))
|
||||
}
|
||||
|
||||
/// The wall clock at the instant `ts` in a zone `offset_minutes` east of UTC.
|
||||
pub fn from_timestamp(ts: i64, offset_minutes: i32) -> NaiveDateTime {
|
||||
from_civil_seconds(ts.saturating_add(i64::from(offset_minutes).saturating_mul(60)))
|
||||
}
|
||||
|
||||
/// Midnight at the start of `d`.
|
||||
pub fn at_midnight(d: NaiveDate) -> NaiveDateTime {
|
||||
d.and_time(chrono::NaiveTime::MIN)
|
||||
}
|
||||
|
||||
/// A date and time in the years iCalendar can write: one outside them is moved to the nearest end.
|
||||
pub(crate) fn valid_time(t: NaiveDateTime) -> NaiveDateTime {
|
||||
from_civil_seconds(civil_seconds(t))
|
||||
}
|
||||
|
||||
/// A date from `YYYY-MM-DD` or `YYYYMMDD`, years 1 to 9999.
|
||||
fn parse_iso_date(s: &str) -> Option<NaiveDate> {
|
||||
let s = s.trim();
|
||||
let digits: String = s.chars().filter(char::is_ascii_digit).collect();
|
||||
if digits.len() != 8 || s.chars().any(|c| !c.is_ascii_digit() && c != '-') {
|
||||
return None;
|
||||
}
|
||||
let year: i32 = digits.get(0..4)?.parse().ok()?;
|
||||
if year < 1 {
|
||||
return None;
|
||||
}
|
||||
NaiveDate::from_ymd_opt(year, digits.get(4..6)?.parse().ok()?, digits.get(6..8)?.parse().ok()?)
|
||||
}
|
||||
|
||||
/// A DATE or DATE-TIME value.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum When {
|
||||
/// `20261012`: a whole day.
|
||||
Date(NaiveDate),
|
||||
/// `20261012T090000`: a wall-clock time, floating or in a named zone.
|
||||
Local(NaiveDateTime),
|
||||
/// `20261012T090000Z`.
|
||||
Utc(NaiveDateTime),
|
||||
}
|
||||
|
||||
impl When {
|
||||
/// From `YYYYMMDD`, `YYYYMMDDTHHMMSS` or `YYYYMMDDTHHMMSSZ`; the seconds may be missing, and
|
||||
/// the ISO 8601 punctuation some writers add (`2026-10-12T09:00:00Z`) is let through.
|
||||
pub fn parse(s: &str) -> Option<When> {
|
||||
let s = s.trim();
|
||||
if s.len() > 32 || !s.is_ascii() {
|
||||
return None;
|
||||
}
|
||||
let (date, time) = match s.find(['T', 't']) {
|
||||
Some(i) => (s.get(..i)?, Some(s.get(i.saturating_add(1)..)?)),
|
||||
None => (s, None),
|
||||
};
|
||||
let date = parse_iso_date(date)?;
|
||||
let Some(time) = time else { return Some(When::Date(date)) };
|
||||
let (clock, utc) = match time.strip_suffix(['Z', 'z']) {
|
||||
Some(clock) => (clock, true),
|
||||
None => (time, false),
|
||||
};
|
||||
let digits: String = clock.chars().filter(|c| *c != ':').collect();
|
||||
if !matches!(digits.len(), 4 | 6) || !digits.bytes().all(|b| b.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
let part = |at: usize| digits.get(at..at.saturating_add(2)).and_then(|d| d.parse::<u32>().ok());
|
||||
// A leap second (60) is read as the last second of the minute.
|
||||
let second = if digits.len() == 6 { part(4)?.min(59) } else { 0 };
|
||||
let t = date.and_hms_opt(part(0)?, part(2)?, second)?;
|
||||
Some(if utc { When::Utc(t) } else { When::Local(t) })
|
||||
}
|
||||
|
||||
/// The date and time as written, whatever the zone.
|
||||
pub fn wall(self) -> NaiveDateTime {
|
||||
match self {
|
||||
When::Date(d) => at_midnight(d),
|
||||
When::Local(t) | When::Utc(t) => t,
|
||||
}
|
||||
}
|
||||
|
||||
/// The local wall-clock time: a UTC time is converted, anything else is taken as written.
|
||||
/// `offset_at(instant)` is minutes east of UTC.
|
||||
pub fn local(self, offset_at: &dyn Fn(i64) -> i32) -> NaiveDateTime {
|
||||
match self {
|
||||
When::Utc(t) => {
|
||||
let ts = to_timestamp(t, 0);
|
||||
from_timestamp(ts, offset_at(ts))
|
||||
}
|
||||
other => other.wall(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The instant: anything but a UTC time is read in the local zone.
|
||||
pub fn timestamp(self, offset_at: &dyn Fn(i64) -> i32) -> i64 {
|
||||
match self {
|
||||
When::Utc(t) => to_timestamp(t, 0),
|
||||
other => {
|
||||
let t = other.wall();
|
||||
// Asked twice, so a time near a daylight-saving change still gets its own offset.
|
||||
let guess = to_timestamp(t, offset_at(to_timestamp(t, 0)));
|
||||
to_timestamp(t, offset_at(guess))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A duration in seconds from `PT15M`, `-PT1H30M`, `P1D`, `P1DT12H`, `P2W`; `None` for junk.
|
||||
pub fn parse_duration(s: &str) -> Option<i64> {
|
||||
let s = s.trim();
|
||||
let (negative, body) = match s.strip_prefix('-') {
|
||||
Some(body) => (true, body),
|
||||
None => (false, s.strip_prefix('+').unwrap_or(s)),
|
||||
};
|
||||
let body = body.strip_prefix(['P', 'p'])?;
|
||||
let (mut total, mut number, mut in_time, mut any) = (0i64, None::<i64>, false, false);
|
||||
for c in body.chars() {
|
||||
match c.to_ascii_uppercase() {
|
||||
'0'..='9' => {
|
||||
let digit = i64::from(c.to_digit(10)?);
|
||||
number = Some(number.unwrap_or(0).checked_mul(10)?.checked_add(digit)?);
|
||||
}
|
||||
'T' if number.is_none() => in_time = true,
|
||||
unit => {
|
||||
let seconds = match (unit, in_time) {
|
||||
('W', false) => 7 * DAY,
|
||||
('D', false) => DAY,
|
||||
('H', true) => 3600,
|
||||
('M', true) => 60,
|
||||
('S', true) => 1,
|
||||
_ => return None,
|
||||
};
|
||||
total = total.checked_add(number.take()?.checked_mul(seconds)?)?;
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if number.is_some() || !any || total > MAX_DURATION {
|
||||
return None;
|
||||
}
|
||||
Some(if negative { -total } else { total })
|
||||
}
|
||||
|
||||
/// `20261012`.
|
||||
pub fn format_date(d: NaiveDate) -> String {
|
||||
valid_time(at_midnight(d)).format("%Y%m%d").to_string()
|
||||
}
|
||||
|
||||
/// `20261012T090000` (floating).
|
||||
pub fn format_local(t: NaiveDateTime) -> String {
|
||||
valid_time(t).format("%Y%m%dT%H%M%S").to_string()
|
||||
}
|
||||
|
||||
/// `20261012T130000Z`.
|
||||
pub fn format_utc(ts: i64) -> String {
|
||||
format!("{}Z", format_local(from_timestamp(ts, 0)))
|
||||
}
|
||||
|
||||
/// A TEXT value ready to write: `\`, `;`, `,` and line breaks escaped (`\r\n` and `\r` count as
|
||||
/// one line break), other control characters except tab dropped.
|
||||
pub fn escape_text(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len().saturating_add(8));
|
||||
let mut after_cr = false;
|
||||
for c in s.chars() {
|
||||
let cr = c == '\r';
|
||||
match c {
|
||||
'\n' if after_cr => {}
|
||||
'\r' | '\n' => out.push_str("\\n"),
|
||||
'\\' => out.push_str("\\\\"),
|
||||
';' => out.push_str("\\;"),
|
||||
',' => out.push_str("\\,"),
|
||||
'\t' => out.push(c),
|
||||
c if c.is_control() => {}
|
||||
c => out.push(c),
|
||||
}
|
||||
after_cr = cr;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Undo [`escape_text`]: `\n` and `\N` are line breaks, `\\`, `\;`, `\,` (and the `\:` and `\"`
|
||||
/// some writers add) their second character; any other backslash is kept. Control characters
|
||||
/// except tab are dropped.
|
||||
pub fn unescape_text(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut chars = s.chars();
|
||||
while let Some(c) = chars.next() {
|
||||
match c {
|
||||
'\\' => match chars.next() {
|
||||
Some('n' | 'N') => out.push('\n'),
|
||||
Some(x @ ('\\' | ';' | ',' | ':' | '"')) => out.push(x),
|
||||
Some(x) => {
|
||||
out.push('\\');
|
||||
if !x.is_control() || x == '\t' {
|
||||
out.push(x);
|
||||
}
|
||||
}
|
||||
None => out.push('\\'),
|
||||
},
|
||||
'\t' => out.push(c),
|
||||
c if c.is_control() => {}
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The first `max` items of a list value, split on its unescaped commas; the items are still
|
||||
/// escaped.
|
||||
pub fn split_list(s: &str, max: usize) -> Vec<&str> {
|
||||
let mut out = Vec::new();
|
||||
let (mut start, mut escaped) = (0usize, false);
|
||||
for (i, c) in s.char_indices() {
|
||||
if out.len() >= max {
|
||||
return out;
|
||||
}
|
||||
match c {
|
||||
_ if escaped => escaped = false,
|
||||
'\\' => escaped = true,
|
||||
',' => {
|
||||
out.push(s.get(start..i).unwrap_or(""));
|
||||
start = i.saturating_add(1);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if out.len() < max {
|
||||
out.push(s.get(start..).unwrap_or(""));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(y, m, day).unwrap()
|
||||
}
|
||||
|
||||
fn at(y: i32, m: u32, day: u32, h: u32, min: u32, s: u32) -> NaiveDateTime {
|
||||
d(y, m, day).and_hms_opt(h, min, s).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dates_and_times() {
|
||||
assert_eq!(When::parse("20261012"), Some(When::Date(d(2026, 10, 12))));
|
||||
assert_eq!(When::parse("20261012T090000"), Some(When::Local(at(2026, 10, 12, 9, 0, 0))));
|
||||
assert_eq!(When::parse(" 20261012T133015Z "), Some(When::Utc(at(2026, 10, 12, 13, 30, 15))));
|
||||
assert_eq!(When::parse("20261012t0900z"), Some(When::Utc(at(2026, 10, 12, 9, 0, 0))));
|
||||
assert_eq!(When::parse("2026-10-12T09:00:00Z"), Some(When::Utc(at(2026, 10, 12, 9, 0, 0))));
|
||||
// A leap second.
|
||||
assert_eq!(When::parse("20161231T235960Z"), Some(When::Utc(at(2016, 12, 31, 23, 59, 59))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn junk_dates_are_none() {
|
||||
for bad in [
|
||||
"",
|
||||
"tomorrow",
|
||||
"2026101",
|
||||
"202610123",
|
||||
"20261312",
|
||||
"20260230",
|
||||
"00000101",
|
||||
"20261012T",
|
||||
"20261012T25",
|
||||
"20261012T240000",
|
||||
"20261012T0960",
|
||||
"20261012T090000+0200",
|
||||
"20261012",
|
||||
"20261012T09000000000000000000000000000000000000",
|
||||
] {
|
||||
assert_eq!(When::parse(bad), None, "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn utc_times_become_local_and_local_times_become_instants() {
|
||||
let utc = When::parse("20261012T130000Z").unwrap();
|
||||
assert_eq!(utc.local(&|_| -240), at(2026, 10, 12, 9, 0, 0));
|
||||
assert_eq!(utc.local(&|_| 330), at(2026, 10, 12, 18, 30, 0));
|
||||
assert_eq!(utc.wall(), at(2026, 10, 12, 13, 0, 0));
|
||||
// Across midnight.
|
||||
assert_eq!(When::parse("20261012T020000Z").unwrap().local(&|_| -300).date(), d(2026, 10, 11));
|
||||
let local = When::parse("20261012T090000").unwrap();
|
||||
assert_eq!(local.local(&|_| -240), at(2026, 10, 12, 9, 0, 0));
|
||||
assert_eq!(local.timestamp(&|_| -240), utc.timestamp(&|_| 600));
|
||||
assert_eq!(When::Date(d(1970, 1, 2)).timestamp(&|_| 60), DAY - 3600);
|
||||
// A zone that changes its offset at an instant: each side gets its own.
|
||||
let change = to_timestamp(at(2026, 11, 1, 6, 0, 0), 0);
|
||||
let zone = |ts: i64| if ts < change { -240 } else { -300 };
|
||||
assert_eq!(When::parse("20261101T000000").unwrap().timestamp(&zone), to_timestamp(at(2026, 11, 1, 4, 0, 0), 0));
|
||||
assert_eq!(When::parse("20261101T120000").unwrap().timestamp(&zone), to_timestamp(at(2026, 11, 1, 17, 0, 0), 0));
|
||||
// A hostile offset is survived.
|
||||
let _ = utc.local(&|_| i32::MIN);
|
||||
let _ = local.timestamp(&|_| i32::MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn durations() {
|
||||
assert_eq!(parse_duration("PT15M"), Some(900));
|
||||
assert_eq!(parse_duration("-PT15M"), Some(-900));
|
||||
assert_eq!(parse_duration("+PT1H30M"), Some(5400));
|
||||
assert_eq!(parse_duration("P1D"), Some(DAY));
|
||||
assert_eq!(parse_duration("P1DT12H"), Some(DAY + 12 * 3600));
|
||||
assert_eq!(parse_duration("P2W"), Some(14 * DAY));
|
||||
assert_eq!(parse_duration("pt0s"), Some(0));
|
||||
assert_eq!(parse_duration("-PT0M"), Some(0));
|
||||
assert_eq!(parse_duration("PT1H2M3S"), Some(3723));
|
||||
for bad in ["", "P", "PT", "15M", "PT15", "P15M", "PT1D", "PTM", "P1X", "P-1D", "P99999999999999999999D", "P9999999999W", "PT1H T"] {
|
||||
assert_eq!(parse_duration(bad), None, "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_and_keeps_dates_in_writable_years() {
|
||||
assert_eq!(format_date(d(2026, 1, 2)), "20260102");
|
||||
assert_eq!(format_date(d(987, 6, 5)), "09870605");
|
||||
assert_eq!(format_local(at(2026, 10, 12, 9, 5, 0)), "20261012T090500");
|
||||
assert_eq!(format_utc(0), "19700101T000000Z");
|
||||
assert_eq!(format_utc(1_760_082_785), "20251010T075305Z");
|
||||
assert_eq!(format_utc(i64::MAX), "99991231T235959Z");
|
||||
assert_eq!(format_utc(i64::MIN), "00010101T000000Z");
|
||||
// Years iCalendar can't write are moved to the nearest it can.
|
||||
for hostile in [NaiveDate::MIN, NaiveDate::MAX, d(0, 1, 1), d(-4, 2, 29), d(10_000, 2, 29)] {
|
||||
assert!(When::parse(&format_date(hostile)).is_some(), "{hostile:?}");
|
||||
assert!(When::parse(&format_local(at_midnight(hostile))).is_some(), "{hostile:?}");
|
||||
}
|
||||
assert_eq!(format_date(NaiveDate::MAX), "99991231");
|
||||
assert_eq!(format_local(NaiveDateTime::MIN), "00010101T000000");
|
||||
// Civil seconds are the same clock read as UTC, and are kept in the years 1 to 9999.
|
||||
assert_eq!(civil_seconds(at(1970, 1, 2, 0, 0, 1)), DAY + 1);
|
||||
assert_eq!(from_civil_seconds(DAY + 1), at(1970, 1, 2, 0, 0, 1));
|
||||
assert_eq!(from_civil_seconds(i64::MAX), at(9999, 12, 31, 23, 59, 59));
|
||||
assert_eq!(from_civil_seconds(i64::MIN), at(1, 1, 1, 0, 0, 0));
|
||||
assert_eq!(from_timestamp(0, -300), at(1969, 12, 31, 19, 0, 0));
|
||||
assert_eq!(to_timestamp(at(1969, 12, 31, 19, 0, 0), -300), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_escapes() {
|
||||
assert_eq!(escape_text("Budget, phase two; \\ done\nNext"), "Budget\\, phase two\\; \\\\ done\\nNext");
|
||||
assert_eq!(escape_text("a\r\nb\rc\n\nd\u{0}\u{7f}\te"), "a\\nb\\nc\\n\\nd\te");
|
||||
assert_eq!(unescape_text("Budget\\, phase two\\; \\\\ done\\nNext\\NLast"), "Budget, phase two; \\ done\nNext\nLast");
|
||||
// What other writers escape without being asked to, and what isn't an escape at all.
|
||||
assert_eq!(unescape_text("10\\:30 \\\"quoted\\\" C:\\Users\\x tail\\"), "10:30 \"quoted\" C:\\Users\\x tail\\");
|
||||
assert_eq!(unescape_text("a\u{0}b\u{1b}c\td"), "abc\td");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_split_on_unescaped_commas() {
|
||||
assert_eq!(split_list("Work,Travel\\, abroad,,Home\\\\,x", 10), ["Work", "Travel\\, abroad", "", "Home\\\\", "x"]);
|
||||
assert_eq!(split_list("", 10), [""]);
|
||||
assert_eq!(split_list("a,b,c,d", 2), ["a", "b"]);
|
||||
assert_eq!(split_list("a,b", 2), ["a", "b"]);
|
||||
assert!(split_list("a,b", 0).is_empty());
|
||||
}
|
||||
|
||||
proptest::proptest! {
|
||||
#[test]
|
||||
fn clean_text_survives_escaping(s in "(\\PC|[\n\t;,\\\\:\"]){0,80}") {
|
||||
proptest::prop_assert_eq!(unescape_text(&escape_text(&s)), s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_text_escapes_to_one_clean_line(s in proptest::prelude::any::<String>()) {
|
||||
let escaped = escape_text(&s);
|
||||
proptest::prop_assert!(!escaped.contains(['\r', '\n']));
|
||||
// Escaping what was read gives the same line again.
|
||||
proptest::prop_assert_eq!(escape_text(&unescape_text(&escaped)), escaped);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values_never_panic(s in proptest::prelude::any::<String>(), max in 0usize..5) {
|
||||
let _ = When::parse(&s);
|
||||
let _ = parse_duration(&s);
|
||||
let _ = unescape_text(&s);
|
||||
let _ = split_list(&s, max);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_civil_second_reads_back_in_writable_years(secs in proptest::prelude::any::<i64>()) {
|
||||
let t = from_civil_seconds(secs);
|
||||
proptest::prop_assert_eq!(civil_seconds(t), secs.clamp(MIN_CIVIL, MAX_CIVIL));
|
||||
proptest::prop_assert!(When::parse(&format_local(t)).is_some());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,498 @@
|
||||
//! Time zones in iCalendar text (RFC 5545 §3.6.5): the `VTIMEZONE` definitions a text brings with
|
||||
//! it, read ([`TimeZones`], [`VTimezone`]) and written ([`write_vtimezone`]).
|
||||
//!
|
||||
//! This crate has no time-zone database. A definition read from a text answers for itself (the
|
||||
//! offset at an instant, the instant of a wall-clock time); what a `TZID` means beyond the text
|
||||
//! (an IANA name, a Windows name, which database zone keeps the same clock) is the caller's. To
|
||||
//! write a definition, the caller hands over its zone's rules through [`ZoneRules`].
|
||||
//!
|
||||
//! Definitions are hostile text: offsets of more than eighteen hours are not offsets, and the
|
||||
//! zones, their parts and their dates are all capped. [`TimeZones::spend`] keeps a budget of the
|
||||
//! work a caller does with them.
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
use chrono::{Datelike, NaiveDate, NaiveDateTime, Weekday};
|
||||
|
||||
use crate::lines::{ContentLine, Lines, fold, prefix};
|
||||
use crate::rrule::{weekday_code, weekday_of_code};
|
||||
use crate::values::{When, at_midnight, civil_seconds, format_local, from_civil_seconds, from_timestamp, to_timestamp};
|
||||
|
||||
/// Most zones one text defines, most parts (`STANDARD`, `DAYLIGHT`) of one zone, and most dates
|
||||
/// (`RDATE`) of one part.
|
||||
pub const MAX_ZONES: usize = 64;
|
||||
pub const MAX_PARTS: usize = 64;
|
||||
pub const MAX_DATES: usize = 64;
|
||||
/// The work one text's times may cost, in units of one look at one part or one date of a
|
||||
/// definition ([`VTimezone::cost`]). Past it, [`TimeZones::spend`] says no.
|
||||
pub const WORK_BUDGET: u64 = 20_000_000;
|
||||
/// The furthest from UTC an offset may be, in seconds.
|
||||
const MAX_OFFSET: i32 = 18 * 3600;
|
||||
/// Longest `TZID` (and `X-LIC-LOCATION`) kept, in bytes.
|
||||
pub const MAX_TZID: usize = 128;
|
||||
|
||||
/// On which day of each year a part of a zone begins.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Yearly {
|
||||
/// The `nth` (1–5, or negative from the end) `weekday` of `month`.
|
||||
Nth { month: u32, nth: i8, weekday: Weekday },
|
||||
/// Day `day` of `month`.
|
||||
Day { month: u32, day: u32 },
|
||||
}
|
||||
|
||||
/// The `nth` (1–5, or negative from the end) `weekday` of a month, when the month has one.
|
||||
fn nth_weekday(year: i32, month: u32, weekday: Weekday, nth: i8) -> Option<NaiveDate> {
|
||||
let first = NaiveDate::from_ymd_opt(year, month, 1)?;
|
||||
if nth > 0 {
|
||||
let offset = (i64::from(weekday.num_days_from_monday()) - i64::from(first.weekday().num_days_from_monday())).rem_euclid(7);
|
||||
NaiveDate::from_ymd_opt(year, month, u32::try_from(1 + offset + 7 * (i64::from(nth) - 1)).ok()?)
|
||||
} else {
|
||||
let last = first.checked_add_months(chrono::Months::new(1))?.pred_opt()?;
|
||||
let back = (i64::from(last.weekday().num_days_from_monday()) - i64::from(weekday.num_days_from_monday())).rem_euclid(7);
|
||||
let day = last.checked_sub_days(chrono::Days::new(u64::try_from(back + 7 * (i64::from(nth.saturating_neg()) - 1)).ok()?))?;
|
||||
(day.month() == month).then_some(day)
|
||||
}
|
||||
}
|
||||
|
||||
impl Yearly {
|
||||
fn date(self, year: i32) -> Option<NaiveDate> {
|
||||
match self {
|
||||
Yearly::Day { month, day } => NaiveDate::from_ymd_opt(year, month, day),
|
||||
Yearly::Nth { month, nth, weekday } => nth_weekday(year, month, weekday, nth),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One `STANDARD` or `DAYLIGHT` part of a definition: from `start` on (a wall-clock time by the
|
||||
/// offset `from`), and then again every year or on the listed dates, the offset is `to`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct Part {
|
||||
start: NaiveDateTime,
|
||||
/// Seconds east of UTC before and after.
|
||||
from: i32,
|
||||
to: i32,
|
||||
yearly: Option<Yearly>,
|
||||
/// The rule holds up to this instant, and for this many beginnings.
|
||||
until: Option<i64>,
|
||||
count: Option<u32>,
|
||||
dates: Vec<NaiveDateTime>,
|
||||
}
|
||||
|
||||
impl Part {
|
||||
/// The wall-clock second from which `to` holds for a beginning written as `start`: a time
|
||||
/// the clocks skip is still by the offset before, and so is the first of an hour that comes
|
||||
/// twice (RFC 5545 §3.3.5).
|
||||
fn wall_from(&self, start: NaiveDateTime) -> i64 {
|
||||
civil_seconds(start).saturating_add(i64::from(self.to.saturating_sub(self.from).max(0)))
|
||||
}
|
||||
|
||||
fn instant_of(&self, start: NaiveDateTime) -> i64 {
|
||||
civil_seconds(start).saturating_sub(i64::from(self.from))
|
||||
}
|
||||
|
||||
/// The beginning of the rule's year `year`, when the rule has one then.
|
||||
fn in_year(&self, year: i32) -> Option<NaiveDateTime> {
|
||||
let rule = self.yearly?;
|
||||
let first = self.start.year();
|
||||
if year < first || self.count.is_some_and(|count| i64::from(year) - i64::from(first) >= i64::from(count)) {
|
||||
return None;
|
||||
}
|
||||
let begins = rule.date(year)?.and_time(self.start.time());
|
||||
(begins >= self.start && self.until.is_none_or(|until| self.instant_of(begins) <= until)).then_some(begins)
|
||||
}
|
||||
|
||||
/// The last beginning at or before `limit`, as `key` measures beginnings.
|
||||
fn latest(&self, limit: i64, year: i32, key: &dyn Fn(&Part, NaiveDateTime) -> i64) -> Option<i64> {
|
||||
let mut best: Option<i64> = None;
|
||||
let mut take = |begins: NaiveDateTime| {
|
||||
let at = key(self, begins);
|
||||
if at <= limit && best.is_none_or(|have| at > have) {
|
||||
best = Some(at);
|
||||
}
|
||||
};
|
||||
take(self.start);
|
||||
self.dates.iter().copied().for_each(&mut take);
|
||||
// The year of the time asked about and the years beside it; and where the rule ends.
|
||||
let last = self.until.map(|until| from_timestamp(until, 0).year());
|
||||
let counted = self.count.map(|count| self.start.year().saturating_add(i32::try_from(count).unwrap_or(i32::MAX)).saturating_sub(1));
|
||||
for year in [Some(year.saturating_add(1)), Some(year), Some(year.saturating_sub(1)), last, last.map(|y| y.saturating_sub(1)), counted]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
if let Some(begins) = self.in_year(year) {
|
||||
take(begins);
|
||||
}
|
||||
}
|
||||
best
|
||||
}
|
||||
}
|
||||
|
||||
/// A zone as a text defines it (`VTIMEZONE`).
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct VTimezone {
|
||||
tzid: String,
|
||||
/// `X-LIC-LOCATION`: the place the writer says the zone is (an IANA name, as a rule).
|
||||
place: String,
|
||||
parts: Vec<Part>,
|
||||
}
|
||||
|
||||
impl VTimezone {
|
||||
/// The `TZID`, at most [`MAX_TZID`] bytes.
|
||||
pub fn tzid(&self) -> &str {
|
||||
&self.tzid
|
||||
}
|
||||
|
||||
/// `X-LIC-LOCATION`, empty when the text gives none.
|
||||
pub fn place(&self) -> &str {
|
||||
&self.place
|
||||
}
|
||||
|
||||
/// What one look at the definition costs, in units of [`WORK_BUDGET`].
|
||||
pub fn cost(&self) -> u64 {
|
||||
self.parts.iter().fold(0u64, |sum, part| sum.saturating_add(8).saturating_add(part.dates.len() as u64))
|
||||
}
|
||||
|
||||
fn offset_by(&self, limit: i64, year: i32, key: &dyn Fn(&Part, NaiveDateTime) -> i64) -> i32 {
|
||||
let mut best: Option<(i64, i32)> = None;
|
||||
for part in &self.parts {
|
||||
if let Some(at) = part.latest(limit, year, key)
|
||||
&& best.is_none_or(|(have, _)| at >= have)
|
||||
{
|
||||
best = Some((at, part.to));
|
||||
}
|
||||
}
|
||||
match best {
|
||||
Some((_, to)) => to,
|
||||
// Before the first beginning: the offset the first one changes from.
|
||||
None => self.parts.iter().min_by_key(|part| part.start).map_or(0, |part| part.from),
|
||||
}
|
||||
}
|
||||
|
||||
/// Seconds east of UTC for a wall-clock time written in the zone. A time the clocks skip is
|
||||
/// read by the offset before the change; a time that comes twice is the first of the two.
|
||||
pub fn offset_for_wall(&self, wall: NaiveDateTime) -> i32 {
|
||||
self.offset_by(civil_seconds(wall), wall.year(), &|part, begins| part.wall_from(begins))
|
||||
}
|
||||
|
||||
/// Seconds east of UTC at an instant (seconds since 1970-01-01T00:00:00Z).
|
||||
pub fn offset_at(&self, instant: i64) -> i32 {
|
||||
self.offset_by(instant, from_timestamp(instant, 0).year(), &|part, begins| part.instant_of(begins))
|
||||
}
|
||||
|
||||
/// The instant of a wall-clock time written in the zone.
|
||||
pub fn instant(&self, wall: NaiveDateTime) -> i64 {
|
||||
civil_seconds(wall).saturating_sub(i64::from(self.offset_for_wall(wall)))
|
||||
}
|
||||
}
|
||||
|
||||
/// `+0100`, `-0500`, `+053000`: seconds east of UTC; `None` for anything else and for more than
|
||||
/// an offset can be.
|
||||
pub fn parse_utc_offset(text: &str) -> Option<i32> {
|
||||
let text = text.trim();
|
||||
let (sign, digits) = match (text.strip_prefix('+'), text.strip_prefix('-')) {
|
||||
(Some(rest), _) => (1, rest),
|
||||
(None, Some(rest)) => (-1, rest),
|
||||
(None, None) => return None,
|
||||
};
|
||||
let digits: String = digits.chars().filter(|c| *c != ':').collect();
|
||||
if !matches!(digits.len(), 4 | 6) || !digits.bytes().all(|b| b.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
let part = |at: usize| digits.get(at..at.saturating_add(2)).and_then(|d| d.parse::<i32>().ok());
|
||||
let (hours, minutes, seconds) = (part(0)?, part(2)?, if digits.len() == 6 { part(4)? } else { 0 });
|
||||
let total = hours * 3600 + minutes * 60 + seconds;
|
||||
(minutes < 60 && seconds < 60 && total <= MAX_OFFSET).then_some(sign * total)
|
||||
}
|
||||
|
||||
/// An offset in seconds as `+0100`, `-0930`, `+045602`.
|
||||
pub fn format_utc_offset(seconds: i32) -> String {
|
||||
let sign = if seconds < 0 { '-' } else { '+' };
|
||||
let a = seconds.unsigned_abs();
|
||||
if a.is_multiple_of(60) {
|
||||
format!("{sign}{:02}{:02}", a / 3600, a / 60 % 60)
|
||||
} else {
|
||||
format!("{sign}{:02}{:02}{:02}", a / 3600, a / 60 % 60, a % 60)
|
||||
}
|
||||
}
|
||||
|
||||
/// The rule of a part: `FREQ=YEARLY;BYMONTH=3;BYDAY=2SU`, with the day of the month or the
|
||||
/// part's own day when no weekday is named. `None` for a rule that isn't yearly.
|
||||
fn yearly(rule: &str, start: NaiveDateTime) -> Option<(Yearly, Option<i64>, Option<u32>)> {
|
||||
let (mut yearly, mut month, mut by_day, mut month_day) = (false, None, None, None);
|
||||
let (mut until, mut count) = (None, None);
|
||||
for part in prefix(rule, 512).split(';').take(16) {
|
||||
let Some((key, value)) = part.split_once('=') else { continue };
|
||||
let value = value.trim();
|
||||
match key.trim().to_ascii_uppercase().as_str() {
|
||||
"FREQ" => yearly = value.eq_ignore_ascii_case("YEARLY"),
|
||||
"INTERVAL" if value != "1" => return None,
|
||||
"BYMONTH" => month = value.parse::<u32>().ok().filter(|m| (1..=12).contains(m)),
|
||||
"BYMONTHDAY" => month_day = value.parse::<u32>().ok().filter(|d| (1..=31).contains(d)),
|
||||
"BYDAY" => {
|
||||
let split = value.len().checked_sub(2)?;
|
||||
let weekday = weekday_of_code(value.get(split..)?)?;
|
||||
let nth = match value.get(..split)? {
|
||||
"" => return None,
|
||||
nth => nth.parse::<i8>().ok().filter(|n| (1..=5).contains(n) || (-5..=-1).contains(n))?,
|
||||
};
|
||||
by_day = Some((nth, weekday));
|
||||
}
|
||||
"UNTIL" => until = When::parse(value).map(|at| to_timestamp(at.wall(), 0)),
|
||||
"COUNT" => count = value.parse::<u32>().ok(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if !yearly {
|
||||
return None;
|
||||
}
|
||||
let month = month.unwrap_or(start.month());
|
||||
let rule = match (by_day, month_day) {
|
||||
(Some((nth, weekday)), _) => Yearly::Nth { month, nth, weekday },
|
||||
(None, day) => Yearly::Day { month, day: day.unwrap_or(start.day()) },
|
||||
};
|
||||
Some((rule, until, count))
|
||||
}
|
||||
|
||||
/// A `STANDARD` or `DAYLIGHT` part being read.
|
||||
#[derive(Default)]
|
||||
struct PartBuilder {
|
||||
start: Option<NaiveDateTime>,
|
||||
from: Option<i32>,
|
||||
to: Option<i32>,
|
||||
rule: Option<String>,
|
||||
dates: Vec<NaiveDateTime>,
|
||||
}
|
||||
|
||||
impl PartBuilder {
|
||||
fn finish(self) -> Option<Part> {
|
||||
let (start, from, to) = (self.start?, self.from?, self.to?);
|
||||
let (yearly, until, count) = match self.rule.as_deref().and_then(|rule| yearly(rule, start)) {
|
||||
Some((rule, until, count)) => (Some(rule), until, count),
|
||||
None => (None, None, None),
|
||||
};
|
||||
Some(Part { start, from, to, yearly, until, count, dates: self.dates })
|
||||
}
|
||||
}
|
||||
|
||||
/// The zones a text defines, and the budget of work spent on them.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TimeZones {
|
||||
zones: Vec<VTimezone>,
|
||||
/// Work spent so far (see [`WORK_BUDGET`]), spends granted so far, and whether the budget ran out.
|
||||
spent: Cell<u64>,
|
||||
granted: Cell<u32>,
|
||||
stopped: Cell<bool>,
|
||||
}
|
||||
|
||||
impl TimeZones {
|
||||
/// Read the `VTIMEZONE`s of a text: at most [`MAX_ZONES`], the first of each `TZID`. A zone
|
||||
/// without a `TZID` or without a part that has a start and both offsets is not one.
|
||||
pub fn collect(text: &str) -> TimeZones {
|
||||
let mut out = TimeZones::default();
|
||||
// Nothing to look for in most texts.
|
||||
let named = text.as_bytes().windows(9).any(|bytes| bytes.eq_ignore_ascii_case(b"VTIMEZONE"));
|
||||
if !named {
|
||||
return out;
|
||||
}
|
||||
let mut zone: Option<VTimezone> = None;
|
||||
let mut part: Option<PartBuilder> = None;
|
||||
for raw in Lines::new(text) {
|
||||
let Some(line) = ContentLine::parse(&raw) else { continue };
|
||||
let value = prefix(line.value.trim(), MAX_TZID);
|
||||
match (line.name.as_str(), value.to_ascii_uppercase().as_str()) {
|
||||
("BEGIN", "VTIMEZONE") => {
|
||||
if out.zones.len() >= MAX_ZONES {
|
||||
break;
|
||||
}
|
||||
zone = Some(VTimezone::default());
|
||||
part = None;
|
||||
}
|
||||
("END", "VTIMEZONE") => {
|
||||
part = None;
|
||||
if let Some(definition) = zone.take()
|
||||
&& !definition.tzid.is_empty()
|
||||
&& !definition.parts.is_empty()
|
||||
&& !out.zones.iter().any(|have| have.tzid == definition.tzid)
|
||||
{
|
||||
out.zones.push(definition);
|
||||
}
|
||||
}
|
||||
("BEGIN", "STANDARD" | "DAYLIGHT") if zone.is_some() => part = Some(PartBuilder::default()),
|
||||
("END", "STANDARD" | "DAYLIGHT") => {
|
||||
if let (Some(definition), Some(built)) = (&mut zone, part.take().and_then(PartBuilder::finish))
|
||||
&& definition.parts.len() < MAX_PARTS
|
||||
{
|
||||
definition.parts.push(built);
|
||||
}
|
||||
}
|
||||
// Anything else that begins inside a zone is not a part of it.
|
||||
("BEGIN", _) => part = None,
|
||||
(name, _) => match (&mut zone, &mut part) {
|
||||
(Some(_), Some(part)) => match name {
|
||||
"DTSTART" => part.start = part.start.or_else(|| When::parse(&line.value).map(When::wall)),
|
||||
"TZOFFSETFROM" => part.from = part.from.or_else(|| parse_utc_offset(&line.value)),
|
||||
"TZOFFSETTO" => part.to = part.to.or_else(|| parse_utc_offset(&line.value)),
|
||||
"RRULE" if part.rule.is_none() => part.rule = Some(prefix(&line.value, 512).to_string()),
|
||||
"RDATE" => {
|
||||
let room = MAX_DATES.saturating_sub(part.dates.len());
|
||||
part.dates.extend(line.value.split(',').take(room).filter_map(When::parse).map(When::wall));
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
(Some(definition), None) => match name {
|
||||
"TZID" if definition.tzid.is_empty() => definition.tzid = value.to_string(),
|
||||
"X-LIC-LOCATION" if definition.place.is_empty() => definition.place = value.to_string(),
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The definition of `tzid`: the one of that name, else one whose name differs only in letter
|
||||
/// case. The name is looked at to [`MAX_TZID`] bytes.
|
||||
pub fn get(&self, tzid: &str) -> Option<&VTimezone> {
|
||||
let tzid = prefix(tzid.trim(), MAX_TZID);
|
||||
self.zones.iter().find(|zone| zone.tzid == tzid).or_else(|| self.zones.iter().find(|zone| zone.tzid.eq_ignore_ascii_case(tzid)))
|
||||
}
|
||||
|
||||
/// Every definition, in the order of the text.
|
||||
pub fn iter(&self) -> impl Iterator<Item = &VTimezone> {
|
||||
self.zones.iter()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.zones.is_empty()
|
||||
}
|
||||
|
||||
/// Spend `units` of the text's work budget; `false` (and the budget is out for good) when
|
||||
/// there is not enough left.
|
||||
pub fn spend(&self, units: u64) -> bool {
|
||||
let total = self.spent.get().saturating_add(units);
|
||||
if self.stopped.get() || total > WORK_BUDGET {
|
||||
self.stopped.set(true);
|
||||
return false;
|
||||
}
|
||||
self.spent.set(total);
|
||||
self.granted.set(self.granted.get().saturating_add(1));
|
||||
true
|
||||
}
|
||||
|
||||
/// When the budget ran out: how many spends were granted before it did.
|
||||
pub fn stopped_after(&self) -> Option<u32> {
|
||||
self.stopped.get().then(|| self.granted.get())
|
||||
}
|
||||
}
|
||||
|
||||
// ---- writing ----
|
||||
|
||||
/// A change of a zone's offset: at the instant `at` (seconds since 1970-01-01T00:00:00Z), from
|
||||
/// `before` to `after` seconds east of UTC.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Transition {
|
||||
pub at: i64,
|
||||
pub before: i32,
|
||||
pub after: i32,
|
||||
}
|
||||
|
||||
/// What [`write_vtimezone`] needs to know of a zone: the caller's time-zone database.
|
||||
pub trait ZoneRules {
|
||||
/// Seconds east of UTC at an instant.
|
||||
fn offset_at(&self, instant: i64) -> i32;
|
||||
/// The changes of offset from `from` up to `to` (instants), in order.
|
||||
fn transitions(&self, from: i64, to: i64) -> Vec<Transition>;
|
||||
}
|
||||
|
||||
/// The rule a change of clocks follows when it falls on the same weekday of the same month each
|
||||
/// year at the same time of day: the `nth` weekday (the last, when it is in the month's last week
|
||||
/// in each of the years).
|
||||
fn yearly_rule(changes: &[(NaiveDateTime, i32, i32)]) -> Option<Yearly> {
|
||||
let (first, first_from, first_to) = changes.first().copied()?;
|
||||
let alike = changes.iter().all(|(at, from, to)| {
|
||||
at.month() == first.month() && at.weekday() == first.weekday() && at.time() == first.time() && (*from, *to) == (first_from, first_to)
|
||||
});
|
||||
if !alike {
|
||||
return None;
|
||||
}
|
||||
let nth = |at: &NaiveDateTime| (at.day() - 1) / 7 + 1;
|
||||
let last = |at: &NaiveDateTime| at.date().checked_add_days(chrono::Days::new(7)).is_none_or(|week_on| week_on.month() != at.month());
|
||||
let (month, weekday) = (first.month(), first.weekday());
|
||||
if changes.iter().all(|(at, _, _)| nth(at) == nth(&first)) && nth(&first) < 5 && !changes.iter().all(|(at, _, _)| last(at)) {
|
||||
Some(Yearly::Nth { month, nth: i8::try_from(nth(&first)).unwrap_or(1), weekday })
|
||||
} else if changes.iter().all(|(at, _, _)| last(at)) {
|
||||
Some(Yearly::Nth { month, nth: -1, weekday })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn write_part(out: &mut String, kind: &str, start: NaiveDateTime, from: i32, to: i32, rule: Option<Yearly>) {
|
||||
fold(out, &format!("BEGIN:{kind}"));
|
||||
fold(out, &format!("DTSTART:{}", format_local(start)));
|
||||
fold(out, &format!("TZOFFSETFROM:{}", format_utc_offset(from)));
|
||||
fold(out, &format!("TZOFFSETTO:{}", format_utc_offset(to)));
|
||||
if let Some(Yearly::Nth { month, nth, weekday }) = rule {
|
||||
fold(out, &format!("RRULE:FREQ=YEARLY;BYMONTH={month};BYDAY={nth}{}", weekday_code(weekday)));
|
||||
}
|
||||
fold(out, &format!("END:{kind}"));
|
||||
}
|
||||
|
||||
/// Write the definition of a zone named `tzid` (control characters dropped, at most [`MAX_TZID`]
|
||||
/// bytes) for times in the years `from`–`to` (at most 120
|
||||
/// years are written; the years are kept in 2 to 9990).
|
||||
///
|
||||
/// A zone that changes its clocks twice a year by one rule is written as most programs write it,
|
||||
/// and all of them read it: one `STANDARD` and one `DAYLIGHT` part, each with its yearly rule. A
|
||||
/// zone that doesn't change is one `STANDARD` part. Any other (a year in which the rule changed,
|
||||
/// a zone that follows the moon) is written out change by change for those years.
|
||||
pub fn write_vtimezone(out: &mut String, tzid: &str, zone: &dyn ZoneRules, from: i32, to: i32) {
|
||||
let (from, to) = (from.clamp(2, 9_990), to.clamp(2, 9_990).max(from.clamp(2, 9_990)));
|
||||
// A few hundred changes at most are looked at.
|
||||
let to = to.min(from.saturating_add(120));
|
||||
let instant = |year: i32| NaiveDate::from_ymd_opt(year, 1, 1).map_or(0, |day| to_timestamp(at_midnight(day), 0));
|
||||
let before = zone.offset_at(instant(from - 1));
|
||||
// Each change as it is written: the wall-clock time by the offset before it.
|
||||
let changes: Vec<(NaiveDateTime, i32, i32)> = zone
|
||||
.transitions(instant(from - 1), instant(to + 1))
|
||||
.into_iter()
|
||||
.map(|change| (from_civil_seconds(change.at.saturating_add(i64::from(change.before))), change.before, change.after))
|
||||
.collect();
|
||||
let midnight = |year: i32| NaiveDate::from_ymd_opt(year, 1, 1).map(at_midnight).unwrap_or_default();
|
||||
fold(out, "BEGIN:VTIMEZONE");
|
||||
// A name can't start another line.
|
||||
let tzid: String = prefix(tzid, MAX_TZID).chars().filter(|c| !c.is_control()).collect();
|
||||
fold(out, &format!("TZID:{tzid}"));
|
||||
if changes.is_empty() {
|
||||
write_part(out, "STANDARD", midnight(1601), before, before, None);
|
||||
fold(out, "END:VTIMEZONE");
|
||||
return;
|
||||
}
|
||||
// Clocks forward is the beginning of daylight time, clocks back the end of it.
|
||||
let (forward, back): (Vec<_>, Vec<_>) = changes.iter().copied().partition(|(_, from, to)| to > from);
|
||||
let years = usize::try_from(i64::from(to) - i64::from(from) + 2).unwrap_or(0);
|
||||
let regular = forward.len() == years && back.len() == years;
|
||||
match (yearly_rule(&forward).filter(|_| regular), yearly_rule(&back).filter(|_| regular)) {
|
||||
(Some(spring), Some(autumn)) => {
|
||||
for (kind, list, rule) in [("DAYLIGHT", &forward, spring), ("STANDARD", &back, autumn)] {
|
||||
if let Some((start, from, to)) = list.first().copied() {
|
||||
write_part(out, kind, start, from, to, Some(rule));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// What holds before the first change, then each change.
|
||||
let first = changes.first().map_or(NaiveDateTime::default(), |(at, _, _)| *at);
|
||||
write_part(out, "STANDARD", midnight(first.year().saturating_sub(1).max(1)), before, before, None);
|
||||
for (start, from, to) in changes {
|
||||
write_part(out, if to > from { "DAYLIGHT" } else { "STANDARD" }, start, from, to, None);
|
||||
}
|
||||
}
|
||||
}
|
||||
fold(out, "END:VTIMEZONE");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "zones_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,293 @@
|
||||
//! `VTIMEZONE` reading and writing. The zones written are rule zones built here (the United States'
|
||||
//! rules, the European Union's, Sydney's, a zone without changes, the United States' change of
|
||||
//! rule in 2007), standing in for a caller's time-zone database.
|
||||
|
||||
use proptest::prelude::*;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(y, m, day).unwrap()
|
||||
}
|
||||
|
||||
fn at(y: i32, m: u32, day: u32, h: u32, min: u32) -> NaiveDateTime {
|
||||
d(y, m, day).and_hms_opt(h, min, 0).unwrap()
|
||||
}
|
||||
|
||||
fn utc(t: NaiveDateTime) -> i64 {
|
||||
to_timestamp(t, 0)
|
||||
}
|
||||
|
||||
/// A zone by its list of changes.
|
||||
struct Rules {
|
||||
before: i32,
|
||||
changes: Vec<Transition>,
|
||||
}
|
||||
|
||||
impl ZoneRules for Rules {
|
||||
fn offset_at(&self, instant: i64) -> i32 {
|
||||
self.changes.iter().rev().find(|c| c.at <= instant).map_or(self.before, |c| c.after)
|
||||
}
|
||||
|
||||
fn transitions(&self, from: i64, to: i64) -> Vec<Transition> {
|
||||
self.changes.iter().copied().filter(|c| c.at >= from && c.at < to).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// The change on the `nth` `weekday` of `month` at `hour` UTC (`day_before`: the evening before).
|
||||
fn change(year: i32, month: u32, nth: i8, hour: u32, day_before: bool, before: i32, after: i32) -> Transition {
|
||||
let day = nth_weekday(year, month, Weekday::Sun, nth).unwrap();
|
||||
let day = if day_before { day.pred_opt().unwrap() } else { day };
|
||||
Transition { at: utc(day.and_hms_opt(hour, 0, 0).unwrap()), before, after }
|
||||
}
|
||||
|
||||
const H: i32 = 3600;
|
||||
|
||||
/// The United States' rule since 2007 (and, with `history`, the one before it up to 2006).
|
||||
fn us_eastern(years: std::ops::RangeInclusive<i32>, history: bool) -> Rules {
|
||||
let mut changes = Vec::new();
|
||||
for y in years {
|
||||
if history && y < 2007 {
|
||||
changes.push(change(y, 4, 1, 7, false, -5 * H, -4 * H));
|
||||
changes.push(change(y, 10, -1, 6, false, -4 * H, -5 * H));
|
||||
} else {
|
||||
changes.push(change(y, 3, 2, 7, false, -5 * H, -4 * H));
|
||||
changes.push(change(y, 11, 1, 6, false, -4 * H, -5 * H));
|
||||
}
|
||||
}
|
||||
Rules { before: -5 * H, changes }
|
||||
}
|
||||
|
||||
fn berlin(years: std::ops::RangeInclusive<i32>) -> Rules {
|
||||
let changes = years.flat_map(|y| [change(y, 3, -1, 1, false, H, 2 * H), change(y, 10, -1, 1, false, 2 * H, H)]).collect();
|
||||
Rules { before: H, changes }
|
||||
}
|
||||
|
||||
fn sydney(years: std::ops::RangeInclusive<i32>) -> Rules {
|
||||
let changes = years.flat_map(|y| [change(y, 4, 1, 16, true, 11 * H, 10 * H), change(y, 10, 1, 16, true, 10 * H, 11 * H)]).collect();
|
||||
Rules { before: 11 * H, changes }
|
||||
}
|
||||
|
||||
fn fixed(seconds: i32) -> Rules {
|
||||
Rules { before: seconds, changes: Vec::new() }
|
||||
}
|
||||
|
||||
/// The zone as a big mail program writes it: today's rule, from the year 1601.
|
||||
const EASTERN: &str = "BEGIN:VTIMEZONE\r\nTZID:Eastern Standard Time\r\nBEGIN:STANDARD\r\nDTSTART:16011104T020000\r\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11\r\n\
|
||||
TZOFFSETFROM:-0400\r\nTZOFFSETTO:-0500\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:16010311T020000\r\nRRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3\r\n\
|
||||
TZOFFSETFROM:-0500\r\nTZOFFSETTO:-0400\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\n";
|
||||
|
||||
fn only(text: &str) -> VTimezone {
|
||||
let zones = TimeZones::collect(text);
|
||||
assert_eq!(zones.iter().count(), 1, "{text}");
|
||||
zones.iter().next().unwrap().clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_definition_gives_each_time_its_offset() {
|
||||
let eastern = only(EASTERN);
|
||||
assert_eq!(eastern.tzid(), "Eastern Standard Time");
|
||||
assert_eq!(eastern.offset_for_wall(at(2026, 1, 15, 9, 0)), -5 * 3600);
|
||||
assert_eq!(eastern.offset_for_wall(at(2026, 7, 15, 9, 0)), -4 * 3600);
|
||||
// March 8, 2026: 1:59 is winter time, 3:00 summer time, and 2:30 (which no clock shows)
|
||||
// is still read by the offset before.
|
||||
assert_eq!(eastern.offset_for_wall(at(2026, 3, 8, 1, 59)), -5 * 3600);
|
||||
assert_eq!(eastern.offset_for_wall(at(2026, 3, 8, 2, 30)), -5 * 3600);
|
||||
assert_eq!(eastern.offset_for_wall(at(2026, 3, 8, 3, 0)), -4 * 3600);
|
||||
// November 1: 1:30 comes twice, and is the first of the two.
|
||||
assert_eq!(eastern.offset_for_wall(at(2026, 11, 1, 1, 30)), -4 * 3600);
|
||||
assert_eq!(eastern.offset_for_wall(at(2026, 11, 1, 2, 0)), -5 * 3600);
|
||||
// By instants, every half hour of two years agrees with the rules.
|
||||
let rules = us_eastern(2025..=2028, false);
|
||||
let mut wall = at(2026, 1, 1, 0, 0);
|
||||
while wall < at(2028, 1, 1, 0, 0) {
|
||||
let instant = utc(wall);
|
||||
assert_eq!(eastern.offset_at(instant), rules.offset_at(instant), "{wall}");
|
||||
wall += chrono::Duration::minutes(30);
|
||||
}
|
||||
assert_eq!(eastern.instant(at(2026, 7, 15, 9, 0)), utc(at(2026, 7, 15, 13, 0)));
|
||||
// Before there were any rules: what the first change changes from.
|
||||
assert_eq!(eastern.offset_for_wall(at(1500, 6, 1, 0, 0)), -5 * 3600);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn definitions_of_every_shape_are_read() {
|
||||
// Listed dates, a rule that ends, a rule by the day of the month, a part with no rule.
|
||||
let text = "BEGIN:VTIMEZONE\nTZID:Example/Odd\nX-LIC-LOCATION:Europe/Berlin\n\
|
||||
BEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n\
|
||||
BEGIN:DAYLIGHT\nDTSTART:20200329T020000\nRDATE:20210328T020000,20220327T020000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0200\nEND:DAYLIGHT\n\
|
||||
BEGIN:STANDARD\nDTSTART:20201025T030000\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU;UNTIL=20221030T010000Z\nTZOFFSETFROM:+0200\nTZOFFSETTO:+0100\nEND:STANDARD\n\
|
||||
BEGIN:DAYLIGHT\nDTSTART:20240401T000000\nRRULE:FREQ=YEARLY;BYMONTHDAY=1;BYMONTH=4;COUNT=2\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0130\nEND:DAYLIGHT\n\
|
||||
BEGIN:STANDARD\nDTSTART:20241001T000000\nRRULE:FREQ=YEARLY\nTZOFFSETFROM:+0130\nTZOFFSETTO:+0100\nEND:STANDARD\n\
|
||||
END:VTIMEZONE\n";
|
||||
let odd = only(text);
|
||||
let offset = |wall: NaiveDateTime| odd.offset_for_wall(wall) / 60;
|
||||
assert_eq!(offset(at(2019, 7, 1, 12, 0)), 60);
|
||||
assert_eq!(offset(at(2020, 7, 1, 12, 0)), 120);
|
||||
assert_eq!(offset(at(2021, 1, 1, 12, 0)), 60);
|
||||
assert_eq!(offset(at(2022, 7, 1, 12, 0)), 120);
|
||||
assert_eq!(offset(at(2023, 7, 1, 12, 0)), 60);
|
||||
// Two Aprils of half an hour, and no third.
|
||||
assert_eq!(offset(at(2024, 7, 1, 12, 0)), 90);
|
||||
assert_eq!(offset(at(2025, 7, 1, 12, 0)), 90);
|
||||
assert_eq!(offset(at(2025, 12, 1, 12, 0)), 60);
|
||||
assert_eq!(offset(at(2026, 7, 1, 12, 0)), 60);
|
||||
// It says where it is, and is found by its name in any letter case.
|
||||
let zones = TimeZones::collect(text);
|
||||
assert_eq!(zones.get("Example/Odd").map(VTimezone::place), Some("Europe/Berlin"));
|
||||
assert!(zones.get("example/odd").is_some());
|
||||
assert!(zones.get("Example/Even").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonsense_definitions_are_no_definitions() {
|
||||
for text in [
|
||||
"BEGIN:VTIMEZONE\nEND:VTIMEZONE\n",
|
||||
"BEGIN:VTIMEZONE\nTZID:Empty\nEND:VTIMEZONE\n",
|
||||
"BEGIN:VTIMEZONE\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n",
|
||||
// Offsets of days, and no offsets at all.
|
||||
"BEGIN:VTIMEZONE\nTZID:Far\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+9900\nTZOFFSETTO:-7200\nEND:STANDARD\nEND:VTIMEZONE\n",
|
||||
"BEGIN:VTIMEZONE\nTZID:Far\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+1801\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n",
|
||||
"BEGIN:VTIMEZONE\nTZID:None\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:soon\nTZOFFSETTO:later\nEND:STANDARD\nEND:VTIMEZONE\n",
|
||||
"BEGIN:VTIMEZONE\nTZID:NoStart\nBEGIN:STANDARD\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n",
|
||||
// A part outside a zone, and a zone that never ends.
|
||||
"BEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n",
|
||||
"BEGIN:VTIMEZONE\nTZID:Open\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n",
|
||||
] {
|
||||
assert!(TimeZones::collect(text).is_empty(), "{text}");
|
||||
}
|
||||
assert_eq!(parse_utc_offset("+053000"), Some(5 * 3600 + 30 * 60));
|
||||
assert_eq!(parse_utc_offset("-00:30"), Some(-30 * 60));
|
||||
for bad in ["", "0100", "+1", "+01", "+0160", "+010060", "+2400", "+١٢٠٠", "++0100"] {
|
||||
assert_eq!(parse_utc_offset(bad), None, "{bad}");
|
||||
}
|
||||
assert_eq!((format_utc_offset(-570 * 60), format_utc_offset(0), format_utc_offset(16_562)), ("-0930".into(), "+0000".into(), "+043602".into()));
|
||||
// A megabyte of name, hundreds of zones, thousands of parts and dates: all bounded.
|
||||
let long = format!(
|
||||
"BEGIN:VTIMEZONE\nTZID:{}\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n",
|
||||
"Z".repeat(1 << 20)
|
||||
);
|
||||
let zones = TimeZones::collect(&long);
|
||||
assert!(only(&long).tzid().len() <= MAX_TZID);
|
||||
assert!(zones.get(&"Z".repeat(1 << 20)).is_some());
|
||||
let many: String = (0..500)
|
||||
.map(|n| format!("BEGIN:VTIMEZONE\nTZID:Zone{n}\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n"))
|
||||
.collect();
|
||||
assert_eq!(TimeZones::collect(&many).iter().count(), MAX_ZONES);
|
||||
let part = format!(
|
||||
"BEGIN:STANDARD\nDTSTART:19700101T000000\nRDATE:{}\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n",
|
||||
"19800101T000000,".repeat(5_000)
|
||||
);
|
||||
let crowded = only(&format!("BEGIN:VTIMEZONE\nTZID:Crowded\n{}END:VTIMEZONE\n", part.repeat(1_000)));
|
||||
assert_eq!((crowded.parts.len(), crowded.parts[0].dates.len()), (MAX_PARTS, MAX_DATES));
|
||||
assert_eq!(crowded.cost(), (MAX_PARTS * (8 + MAX_DATES)) as u64);
|
||||
let _ = crowded.offset_for_wall(at(2026, 1, 1, 0, 0));
|
||||
// Hostile times ask nothing that panics.
|
||||
let eastern = only(EASTERN);
|
||||
for wall in [NaiveDateTime::MAX, NaiveDateTime::MIN, NaiveDateTime::default()] {
|
||||
let _ = (eastern.offset_for_wall(wall), eastern.instant(wall));
|
||||
}
|
||||
let _ = (eastern.offset_at(i64::MIN), eastern.offset_at(i64::MAX));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_budget_runs_out_for_good() {
|
||||
let zones = TimeZones::collect(EASTERN);
|
||||
assert_eq!(zones.stopped_after(), None);
|
||||
assert!(zones.spend(WORK_BUDGET - 10));
|
||||
assert!(zones.spend(10));
|
||||
assert!(!zones.spend(1));
|
||||
// Once out, out: even a spend of nothing is refused.
|
||||
assert!(!zones.spend(0));
|
||||
assert_eq!(zones.stopped_after(), Some(2));
|
||||
}
|
||||
|
||||
/// What a written definition says, read back.
|
||||
fn written(name: &str, zone: &dyn ZoneRules, from: i32, to: i32) -> (String, VTimezone) {
|
||||
let mut out = String::new();
|
||||
write_vtimezone(&mut out, name, zone, from, to);
|
||||
let read = only(&out);
|
||||
(out, read)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn written_definitions_are_the_usual_two_rules_where_a_zone_has_them() {
|
||||
let (text, _) = written("America/New_York", &us_eastern(2025..=2027, false), 2026, 2026);
|
||||
assert_eq!(
|
||||
text.replace("\r\n", "\n"),
|
||||
"BEGIN:VTIMEZONE\nTZID:America/New_York\n\
|
||||
BEGIN:DAYLIGHT\nDTSTART:20250309T020000\nTZOFFSETFROM:-0500\nTZOFFSETTO:-0400\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\nEND:DAYLIGHT\n\
|
||||
BEGIN:STANDARD\nDTSTART:20251102T020000\nTZOFFSETFROM:-0400\nTZOFFSETTO:-0500\nRRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\nEND:STANDARD\n\
|
||||
END:VTIMEZONE\n"
|
||||
);
|
||||
let (text, _) = written("Europe/Berlin", &berlin(2025..=2031), 2026, 2030);
|
||||
assert!(text.contains("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU") && text.contains("RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU"), "{text}");
|
||||
assert!(text.contains("DTSTART:20250330T020000") && text.contains("DTSTART:20251026T030000"), "{text}");
|
||||
// The south: summer time begins in October.
|
||||
let (text, _) = written("Australia/Sydney", &sydney(2025..=2027), 2026, 2026);
|
||||
assert!(text.contains("RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1SU") && text.contains("RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU"), "{text}");
|
||||
// No changes: one part. An offset is a zone too.
|
||||
let (text, _) = written("Asia/Kolkata", &fixed(5 * H + 1800), 2026, 2026);
|
||||
assert_eq!(
|
||||
text.replace("\r\n", "\n"),
|
||||
"BEGIN:VTIMEZONE\nTZID:Asia/Kolkata\nBEGIN:STANDARD\nDTSTART:16010101T000000\nTZOFFSETFROM:+0530\nTZOFFSETTO:+0530\nEND:STANDARD\nEND:VTIMEZONE\n"
|
||||
);
|
||||
let (text, _) = written("UTC-09:30", &fixed(-570 * 60), 2026, 2026);
|
||||
assert!(text.contains("TZID:UTC-09:30") && text.contains("TZOFFSETTO:-0930"), "{text}");
|
||||
// The year the United States changed its rule: written out change by change.
|
||||
let history = us_eastern(2004..=2009, true);
|
||||
let (text, read) = written("America/New_York", &history, 2006, 2007);
|
||||
assert!(!text.contains("RRULE") && text.contains("DTSTART:20060402T020000") && text.contains("DTSTART:20070311T020000"), "{text}");
|
||||
assert_eq!(read.instant(at(2006, 3, 20, 9, 0)), utc(at(2006, 3, 20, 14, 0)));
|
||||
assert_eq!(read.instant(at(2007, 3, 20, 9, 0)), utc(at(2007, 3, 20, 13, 0)));
|
||||
// Hostile years are years, and a hostile name can't start a line.
|
||||
for (from, to) in [(i32::MIN, i32::MAX), (9_999, 1), (0, 0)] {
|
||||
let _ = written("Europe/Berlin", &berlin(2020..=2030), from, to);
|
||||
}
|
||||
let (text, read) = written("Evil\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT", &fixed(0), 2026, 2026);
|
||||
assert_eq!(read.tzid(), "EvilEND:VTIMEZONEBEGIN:VEVENT");
|
||||
assert_eq!(text.matches("VTIMEZONE").count(), 3, "{text}");
|
||||
}
|
||||
|
||||
fn any_zone() -> impl Strategy<Value = (usize, i32)> {
|
||||
(0usize..6, -12 * 60i32..=14 * 60)
|
||||
}
|
||||
|
||||
fn zone_of(which: usize, minutes: i32) -> Rules {
|
||||
match which {
|
||||
0 => us_eastern(1960..=2100, false),
|
||||
1 => us_eastern(1960..=2100, true),
|
||||
2 => berlin(1960..=2100),
|
||||
3 => sydney(1960..=2100),
|
||||
4 => fixed(minutes * 60),
|
||||
_ => fixed(0),
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn a_written_definition_reads_as_the_zone_it_was_written_for((which, minutes_east) in any_zone(), year in 1971i32..2090, span in 0i32..4, minutes in 0i64..365 * 24 * 60) {
|
||||
let zone = zone_of(which, minutes_east);
|
||||
let (text, read) = written("Example/Zone", &zone, year, year + span);
|
||||
// Any time of those years is the instant the rules say.
|
||||
let wall = at(year, 1, 1, 0, 0) + chrono::Duration::minutes(minutes) + chrono::Duration::days(i64::from(span) * 365);
|
||||
let instant = utc(wall);
|
||||
prop_assert_eq!(read.offset_at(instant), zone.offset_at(instant), "{} at {}\n{}", which, wall, text);
|
||||
// And a wall-clock time is the instant it shows, where the clock shows it once.
|
||||
let wall_instant = read.instant(wall);
|
||||
if read.offset_at(wall_instant) == zone.offset_at(wall_instant) && zone.offset_at(wall_instant - 2 * i64::from(H)) == zone.offset_at(wall_instant + 2 * i64::from(H)) {
|
||||
prop_assert_eq!(wall_instant + i64::from(zone.offset_at(wall_instant)), civil_seconds(wall));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_text_is_definitions_or_none(text in proptest::prelude::any::<String>(), parts in proptest::collection::vec("(BEGIN|END|DTSTART|TZOFFSETFROM|TZOFFSETTO|RRULE|RDATE|TZID):[-+A-Z0-9=;,]{0,24}", 0..40), wall in proptest::num::i64::ANY) {
|
||||
let _ = TimeZones::collect(&text);
|
||||
let zones = TimeZones::collect(&format!("BEGIN:VTIMEZONE\nTZID:X\n{}\nEND:VTIMEZONE\n", parts.join("\n")));
|
||||
let wall = from_civil_seconds(wall);
|
||||
if let Some(definition) = zones.get("X") {
|
||||
let _ = (definition.offset_for_wall(wall), definition.offset_at(utc(wall)), definition.instant(wall));
|
||||
}
|
||||
let _ = zones.get(&text);
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,9 @@ workspace = true
|
||||
[dependencies]
|
||||
craft-xml = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
# `deflate-flate2` with flate2's default miniz_oxide backend, as craft-update pins it (zip's own
|
||||
# `deflate` feature would switch a whole app's flate2 to zlib-rs).
|
||||
craft-zip = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# The tests build hostile archives by hand.
|
||||
zip = { version = "9", default-features = false, features = ["deflate-flate2"] }
|
||||
# The backend of zip's deflate: `deflate-flate2` leaves the choice to the build.
|
||||
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
||||
craft-zip = { workspace = true, features = ["test-support"] }
|
||||
|
||||
@@ -53,7 +53,8 @@ for line in &doc.warnings { /* a part designmap.xml lists and the package lacks
|
||||
| `xml.max_depth` | 256 | `Error::Part` |
|
||||
| `xml.max_nodes`, `xml.max_attrs_total` (one part) | 8,000,000 and 32,000,000 | `Error::Part` |
|
||||
|
||||
Sizes are checked against what a part really expands to, not what the archive declares.
|
||||
The ZIP layer is craft-zip: the declared entry count is checked before the archive is indexed, and
|
||||
sizes are checked against what a part really expands to, not what the archive declares.
|
||||
|
||||
## Writing
|
||||
|
||||
|
||||
+31
-45
@@ -1,7 +1,6 @@
|
||||
//! The ZIP container: bounded part reads, lenient part names, designmap includes, and the writer.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Cursor, Read, Write};
|
||||
|
||||
use craft_xml::El;
|
||||
|
||||
@@ -34,12 +33,10 @@ impl Default for Limits {
|
||||
/// An IDML package whose parts are decompressed only when asked for, within byte budgets that
|
||||
/// don't trust the sizes the archive claims.
|
||||
pub struct Package<'a> {
|
||||
zip: zip::ZipArchive<Cursor<&'a [u8]>>,
|
||||
zip: craft_zip::Archive<'a>,
|
||||
/// Normalised part name → entry name, first entry wins (zip order, so the choice is stable).
|
||||
lenient: HashMap<String, String>,
|
||||
limits: Limits,
|
||||
/// Bytes the parts still to be read may expand to.
|
||||
left: u64,
|
||||
}
|
||||
|
||||
/// `designmap.xml` with the parts it includes.
|
||||
@@ -63,46 +60,40 @@ impl<'a> Package<'a> {
|
||||
Self::open_with(bytes, Limits::DEFAULT)
|
||||
}
|
||||
|
||||
/// The declared entry count is checked against [`Limits::max_entries`] before the archive
|
||||
/// is indexed.
|
||||
pub fn open_with(bytes: &'a [u8], limits: Limits) -> Result<Self> {
|
||||
let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| Error::NotIdml(e.to_string()))?;
|
||||
if zip.len() > limits.max_entries {
|
||||
return Err(Error::TooManyEntries { found: zip.len(), limit: limits.max_entries });
|
||||
}
|
||||
let zip =
|
||||
craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries as u64, max_total_bytes: limits.max_total_bytes })
|
||||
.map_err(|e| match e {
|
||||
craft_zip::Error::TooManyEntries { declared, limit } => Error::TooManyEntries {
|
||||
found: usize::try_from(declared).unwrap_or(usize::MAX),
|
||||
limit: usize::try_from(limit).unwrap_or(usize::MAX),
|
||||
},
|
||||
e => Error::NotIdml(e.to_string()),
|
||||
})?;
|
||||
let mut lenient: HashMap<String, String> = HashMap::new();
|
||||
for i in 0..zip.len() {
|
||||
let Some(Ok(name)) = zip.name_for_index(i) else { continue };
|
||||
if name.ends_with('/') {
|
||||
continue;
|
||||
}
|
||||
for name in zip.names().filter(|n| !n.ends_with('/')) {
|
||||
lenient.entry(normalize_part_name(&name)).or_insert_with(|| name.into_owned());
|
||||
}
|
||||
Ok(Package { zip, lenient, limits, left: limits.max_total_bytes })
|
||||
Ok(Package { zip, lenient, limits })
|
||||
}
|
||||
|
||||
/// The bytes of part `name` (matched exactly, then leniently: writers differ in
|
||||
/// percent-encoding, case, path separators and a leading `./` or `/`), or `None` if the
|
||||
/// package has no such part.
|
||||
pub fn part(&mut self, name: &str) -> Result<Option<Vec<u8>>> {
|
||||
let exact = self.zip.index_for_name(name).filter(|_| !name.ends_with('/'));
|
||||
let Some(i) = exact.or_else(|| self.lenient.get(&normalize_part_name(name)).and_then(|n| self.zip.index_for_name(n))) else {
|
||||
let exact = (self.zip.contains(name) && !name.ends_with('/')).then_some(name);
|
||||
let Some(stored) = exact.or_else(|| self.lenient.get(&normalize_part_name(name)).map(String::as_str)) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let part = self.zip.name_for_index(i).and_then(|n| n.ok()).map_or_else(|| name.to_string(), |n| n.into_owned());
|
||||
let f = self.zip.by_index(i).map_err(|e| Error::NotIdml(e.to_string()))?;
|
||||
let limit = self.limits.max_part_bytes.min(self.left);
|
||||
if f.size() > limit {
|
||||
return Err(Error::TooLarge { part, limit });
|
||||
}
|
||||
let mut buf = Vec::new();
|
||||
if let Err(e) = f.take(limit.saturating_add(1)).read_to_end(&mut buf) {
|
||||
return Err(Error::Part { part, msg: e.to_string() });
|
||||
}
|
||||
let n = buf.len() as u64;
|
||||
if n > limit {
|
||||
return Err(Error::TooLarge { part, limit });
|
||||
}
|
||||
self.left = self.left.saturating_sub(n);
|
||||
Ok(Some(buf))
|
||||
let limit = self.limits.max_part_bytes.min(self.zip.budget().remaining());
|
||||
let stored = stored.to_string();
|
||||
self.zip.read(&stored, self.limits.max_part_bytes).map_err(|e| match e {
|
||||
craft_zip::Error::EntryTooLarge { .. } | craft_zip::Error::BudgetSpent { .. } => Error::TooLarge { part: stored.clone(), limit },
|
||||
craft_zip::Error::Entry { msg, .. } => Error::Part { part: stored.clone(), msg },
|
||||
e => Error::NotIdml(e.to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Part `name` as an element tree ([`crate::parse_part`]).
|
||||
@@ -114,7 +105,7 @@ impl<'a> Package<'a> {
|
||||
|
||||
/// Bytes the parts still to be read may expand to.
|
||||
pub fn budget_left(&self) -> u64 {
|
||||
self.left
|
||||
self.zip.budget().remaining()
|
||||
}
|
||||
|
||||
/// Reads `designmap.xml` and every part it includes.
|
||||
@@ -173,19 +164,14 @@ fn normalize_part_name(name: &str) -> String {
|
||||
}
|
||||
|
||||
/// Zips an IDML package: the stored `mimetype` first, then `parts` in order as (name, bytes).
|
||||
/// Equal parts give equal bytes.
|
||||
pub fn write_package<N: AsRef<str>, D: AsRef<[u8]>>(parts: &[(N, D)]) -> Result<Vec<u8>> {
|
||||
use zip::write::SimpleFileOptions;
|
||||
let fail = |e: &dyn std::fmt::Display| Error::Write(e.to_string());
|
||||
let mut w = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let stored = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
let deflated = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
w.start_file("mimetype", stored).map_err(|e| fail(&e))?;
|
||||
w.write_all(MIMETYPE.as_bytes()).map_err(|e| fail(&e))?;
|
||||
use craft_zip::Compression;
|
||||
let fail = |e: craft_zip::Error| Error::Write(e.to_string());
|
||||
let mut w = craft_zip::Writer::new();
|
||||
w.add("mimetype", MIMETYPE.as_bytes(), Compression::Stored).map_err(fail)?;
|
||||
for (name, data) in parts {
|
||||
let data = data.as_ref();
|
||||
// More than 4 GiB needs the ZIP64 form.
|
||||
w.start_file(name.as_ref(), deflated.large_file(data.len() >= u32::MAX as usize)).map_err(|e| fail(&e))?;
|
||||
w.write_all(data).map_err(|e| fail(&e))?;
|
||||
w.add(name.as_ref(), data.as_ref(), Compression::Deflated).map_err(fail)?;
|
||||
}
|
||||
w.finish().map(Cursor::into_inner).map_err(|e| fail(&e))
|
||||
w.finish().map_err(fail)
|
||||
}
|
||||
|
||||
@@ -244,3 +244,16 @@ fn parts_are_written_one_element_per_line_without_touching_text() {
|
||||
);
|
||||
assert!(with_props(El::new("A"), Vec::new()).children.is_empty());
|
||||
}
|
||||
|
||||
/// A ZIP64 end record can declare millions of entries in a small file; the count is refused on
|
||||
/// the raw bytes, before the archive is indexed.
|
||||
#[test]
|
||||
fn a_package_declaring_too_many_entries_is_refused_before_it_is_indexed() {
|
||||
let bomb = craft_zip::zip64_entry_count_sample(100_001);
|
||||
assert_eq!(Package::open(&bomb).err(), Some(Error::TooManyEntries { found: 100_001, limit: 100_000 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equal_parts_give_equal_bytes() {
|
||||
assert_eq!(sample(), sample());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "craft-jmap"
|
||||
description = "JMAP (RFC 8620) protocol core without sockets: session, requests of method calls with result references, responses, ids, address templates and the judgement of HTTP answers, over any HTTP implementation."
|
||||
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 }
|
||||
@@ -0,0 +1,37 @@
|
||||
# craft-jmap
|
||||
|
||||
The JMAP protocol core (RFC 8620) without sockets: a program brings an HTTP implementation
|
||||
(`HttpExchange`), this crate describes each request and judges each answer.
|
||||
|
||||
- **Session**: `Client`, `fetch_session` / `read_session`, `Session`, `Account`, `CoreLimits`,
|
||||
the capabilities (`CAPABILITY_CORE`, `CAPABILITY_MAIL`).
|
||||
- **Requests**: `ApiRequest` of method calls with `CallId`s and `ResultReference`s
|
||||
(back-references), checked against the session's limits.
|
||||
- **Responses**: `ApiResponse`, `MethodResponse`, `MethodError` (RFC 8620 §3.6.2), per call.
|
||||
- **Addresses**: `Address` (absolute `http(s)` URLs, RFC 3986 and the WHATWG URL Standard), the
|
||||
transport rule (`https`, or plain `http` to loopback only), `session_address` for what a person
|
||||
types, `AddressTemplate` (RFC 6570 level 1, the download and upload templates).
|
||||
- **HTTP**: `HttpRequest`, `HttpResponse`, `judge_response` (status codes, problem details,
|
||||
JSON), `RetryAfter`, `Credentials` (Basic, Bearer), `Redactor` (excerpts of what a server says,
|
||||
with the credentials and addresses taken out).
|
||||
- `Id`: the JMAP id alphabet.
|
||||
|
||||
Method calls' arguments and results are `serde_json::Value`s: what a program asks
|
||||
(`Mailbox/get`, `Email/query`…) and what it makes of the answers stays in the program.
|
||||
|
||||
## Hostile input
|
||||
|
||||
Every size, count, nesting depth and string length a server sends is bounded; ids are held to
|
||||
the JMAP alphabet; nothing indexes into what came over the wire, and the protocol modules are
|
||||
checked with `clippy::indexing_slicing` and `clippy::arithmetic_side_effects`. Credentials and
|
||||
the addresses of the session and the account never reach error text or `Debug` output.
|
||||
|
||||
## Origin
|
||||
|
||||
MailCraft `crates/jmap` (the protocol core: `address`, `classify`, `client`, `credentials`,
|
||||
`discovery`, `error`, `host`, `http`, `id`, `percent`, `redact`, `request`, `response`, `retry`,
|
||||
`session`, `template`) at
|
||||
[a44a450](https://github.com/UCS-Care-Group/mailcraft/commit/a44a45013d1c6396229c40ab60172d1882b368d5),
|
||||
by Martin Ball and the MailCraft contributors (MIT OR Apache-2.0), with all of its tests. The mail
|
||||
client that sits on it (the mailbox and email reads into MailCraft's sync model, the fetch of
|
||||
whole messages, the in-process test server) stays in MailCraft.
|
||||
@@ -0,0 +1,533 @@
|
||||
//! Absolute `http` and `https` addresses (RFC 3986, RFC 9110 §4.2), reference resolution (RFC 3986 §5.2),
|
||||
//! origins, and the transport rule that decides whether an address may carry credentials.
|
||||
//!
|
||||
//! An [`Address`] is always absolute, always `http` or `https`, always has a host, never has user information
|
||||
//! or a fragment, and is kept in a normal form (RFC 3986 §6.2.2 and §6.2.3, RFC 9110 §4.2.3): lower-case scheme
|
||||
//! and host, no default port, percent-escapes in upper case with escaped unreserved characters decoded, no
|
||||
//! dot-segments, and `/` for an empty path.
|
||||
//!
|
||||
//! Its `Debug` form shows the origin only, so that a path or a query holding something private never reaches
|
||||
//! a log by accident. [`Address::as_str`] gives the full form, for the request itself.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::host::Host;
|
||||
use crate::percent::{self, is_sub_delim, is_unreserved};
|
||||
|
||||
/// The longest address accepted, in octets.
|
||||
pub const MAX_ADDRESS_LEN: usize = 8 * 1024;
|
||||
|
||||
/// An address scheme this crate can use.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Scheme {
|
||||
/// `http` (RFC 9110 §4.2.1); only usable towards a loopback host.
|
||||
Http,
|
||||
/// `https` (RFC 9110 §4.2.2).
|
||||
Https,
|
||||
}
|
||||
|
||||
impl Scheme {
|
||||
/// The scheme's name, lower case.
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Http => "http",
|
||||
Self::Https => "https",
|
||||
}
|
||||
}
|
||||
|
||||
/// The TCP port used when an address names none (RFC 9110 §4.2.1 and §4.2.2).
|
||||
pub fn default_port(self) -> u16 {
|
||||
match self {
|
||||
Self::Http => 80,
|
||||
Self::Https => 443,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a text is not a usable address.
|
||||
///
|
||||
/// No variant carries the text that was refused: it may hold a password or a token.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum AddressError {
|
||||
/// Nothing was given.
|
||||
Empty,
|
||||
/// Longer than [`MAX_ADDRESS_LEN`].
|
||||
TooLong,
|
||||
/// A space, a control character, a character outside ASCII, or a character no URI may contain.
|
||||
ForbiddenCharacter,
|
||||
/// A backslash, which browsers read as a slash and RFC 3986 does not allow.
|
||||
Backslash,
|
||||
/// No scheme where an absolute address was needed.
|
||||
MissingScheme,
|
||||
/// A scheme other than `http` or `https`.
|
||||
UnsupportedScheme,
|
||||
/// No `//` and authority after the scheme.
|
||||
MissingAuthority,
|
||||
/// The host is empty (RFC 9110 §4.2.1).
|
||||
EmptyHost,
|
||||
/// User information (`name@` or `name:password@`) before the host (RFC 9110 §4.2.4).
|
||||
UserInfo,
|
||||
/// The host is not a usable name.
|
||||
InvalidHost,
|
||||
/// The host is outside ASCII; it must be given in its `xn--` form.
|
||||
InternationalHost,
|
||||
/// A numeric host that is not a plain dotted IPv4 address (`127.1`, `0x7f.0.0.1`, …).
|
||||
AmbiguousNumericHost,
|
||||
/// A bracketed IPv6 literal that does not parse.
|
||||
InvalidIpv6,
|
||||
/// The port is not a number from 1 to 65535.
|
||||
InvalidPort,
|
||||
/// The path holds a character or an escape that RFC 3986 §3.3 does not allow.
|
||||
InvalidPath,
|
||||
/// The query holds a character or an escape that RFC 3986 §3.4 does not allow.
|
||||
InvalidQuery,
|
||||
/// The fragment holds a character or an escape that RFC 3986 §3.5 does not allow.
|
||||
InvalidFragment,
|
||||
/// Plain `http` to a host that is not loopback.
|
||||
InsecureTransport,
|
||||
/// An address on this computer or its local network, given by a host farther away (see
|
||||
/// [`HostScope`](crate::host::HostScope)).
|
||||
LocalFromRemote,
|
||||
}
|
||||
|
||||
impl fmt::Display for AddressError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Empty => "no address was entered; type the JMAP server's address, for example example.com",
|
||||
Self::TooLong => "the address is longer than 8 KiB (8192 characters), more than any server address needs; check that nothing else was pasted with it",
|
||||
Self::ForbiddenCharacter => "the address contains a space, a control character, a non-ASCII character or one of \" < > ^ ` { | }; remove it or percent-encode it",
|
||||
Self::Backslash => "the address contains a backslash (\\); addresses use forward slashes (/)",
|
||||
Self::MissingScheme => "the address does not say how to connect; start it with https://",
|
||||
Self::UnsupportedScheme => "the address uses a scheme other than https; enter an https:// address (http is only for this computer)",
|
||||
Self::MissingAuthority => "the scheme is not followed by // and a server name; write it as https://server.example/path",
|
||||
Self::EmptyHost => "the address has no server name after https://; add the server's name",
|
||||
Self::UserInfo => "the address contains a user name followed by @ before the server name; remove it and enter the user name in the sign-in fields",
|
||||
Self::InvalidHost => "the server name contains characters a DNS name cannot hold, or an empty or overlong label; check its spelling",
|
||||
Self::InternationalHost => "the server name has non-ASCII letters; enter its ASCII form, which starts with xn--",
|
||||
Self::AmbiguousNumericHost => "the server name looks numeric but is not a plain IPv4 address; write four numbers such as 192.0.2.1, without leading zeros, or use the server's name",
|
||||
Self::InvalidIpv6 => "the IPv6 address in brackets cannot be read; zone suffixes such as %eth0 and IPvFuture forms are not supported",
|
||||
Self::InvalidPort => "the port after the server name is not a number from 1 to 65535",
|
||||
Self::InvalidPath => "the path contains a character or a % sign that addresses do not allow there; percent-encode it",
|
||||
Self::InvalidQuery => "the part after ? contains a character or a % sign that addresses do not allow there; percent-encode it",
|
||||
Self::InvalidFragment => "the part after # contains a character or a % sign that addresses do not allow there",
|
||||
Self::InsecureTransport => "this address would send the password unencrypted over plain http to another computer; use https:// (plain http is only accepted for localhost, 127.0.0.0/8 and [::1])",
|
||||
Self::LocalFromRemote => "the server tried to send this program to this computer or its local network, which a remote server may not do; its configuration may be wrong or tampered with",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for AddressError {}
|
||||
|
||||
/// An absolute `http` or `https` address in normal form.
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Address {
|
||||
text: String,
|
||||
scheme: Scheme,
|
||||
host: Host,
|
||||
port: Option<u16>,
|
||||
path_start: usize,
|
||||
query_start: Option<usize>,
|
||||
}
|
||||
|
||||
/// The origin of an address: scheme, host and port (the tuple origin of the WHATWG URL Standard §4.7 for
|
||||
/// `http` and `https`). Its `Display` form is `scheme://host` with `:port` only when it is not the default.
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Origin {
|
||||
scheme: Scheme,
|
||||
host: Host,
|
||||
port: Option<u16>,
|
||||
}
|
||||
|
||||
impl Origin {
|
||||
/// The scheme.
|
||||
pub fn scheme(&self) -> Scheme {
|
||||
self.scheme
|
||||
}
|
||||
|
||||
/// The host.
|
||||
pub fn host(&self) -> &Host {
|
||||
&self.host
|
||||
}
|
||||
|
||||
/// The port, the scheme's default when none was given.
|
||||
pub fn port(&self) -> u16 {
|
||||
self.port.unwrap_or_else(|| self.scheme.default_port())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Origin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}://{}", self.scheme.as_str(), self.host)?;
|
||||
if let Some(port) = self.port {
|
||||
write!(f, ":{port}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Origin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// The parts of a URI reference (RFC 3986 Appendix B), the fragment already set aside.
|
||||
struct Parts<'a> {
|
||||
scheme: Option<&'a str>,
|
||||
authority: Option<&'a str>,
|
||||
path: &'a str,
|
||||
query: Option<&'a str>,
|
||||
}
|
||||
|
||||
fn refuse_unusable_characters(input: &str) -> Result<(), AddressError> {
|
||||
if input.is_empty() {
|
||||
return Err(AddressError::Empty);
|
||||
}
|
||||
if input.len() > MAX_ADDRESS_LEN {
|
||||
return Err(AddressError::TooLong);
|
||||
}
|
||||
for b in input.bytes() {
|
||||
if b == b'\\' {
|
||||
return Err(AddressError::Backslash);
|
||||
}
|
||||
if !(0x21..=0x7E).contains(&b) || matches!(b, b'"' | b'<' | b'>' | b'^' | b'`' | b'{' | b'|' | b'}') {
|
||||
return Err(AddressError::ForbiddenCharacter);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn looks_like_scheme(text: &str) -> bool {
|
||||
let mut bytes = text.bytes();
|
||||
matches!(bytes.next(), Some(b) if b.is_ascii_alphabetic()) && bytes.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
|
||||
}
|
||||
|
||||
/// Splits a reference into its components (RFC 3986 Appendix B); the fragment is dropped, as RFC 9110 §7.1
|
||||
/// leaves it out of every target.
|
||||
fn reference_parts(reference: &str) -> Result<Parts<'_>, AddressError> {
|
||||
let (without_fragment, fragment) = match reference.split_once('#') {
|
||||
Some((head, fragment)) => (head, Some(fragment)),
|
||||
None => (reference, None),
|
||||
};
|
||||
if let Some(fragment) = fragment
|
||||
&& !chars_allowed(fragment, true)
|
||||
{
|
||||
return Err(AddressError::InvalidFragment);
|
||||
}
|
||||
let (scheme, rest) = match without_fragment.find([':', '/', '?']) {
|
||||
Some(index) if without_fragment.get(index..).is_some_and(|tail| tail.starts_with(':')) && index > 0 => {
|
||||
let (scheme, rest) = without_fragment.split_at_checked(index).ok_or(AddressError::MissingScheme)?;
|
||||
(Some(scheme), rest.get(1..).unwrap_or(""))
|
||||
}
|
||||
_ => (None, without_fragment),
|
||||
};
|
||||
let (authority, rest) = match rest.strip_prefix("//") {
|
||||
Some(after) => {
|
||||
let end = after.find(['/', '?']).unwrap_or(after.len());
|
||||
let (authority, rest) = after.split_at_checked(end).ok_or(AddressError::MissingAuthority)?;
|
||||
(Some(authority), rest)
|
||||
}
|
||||
None => (None, rest),
|
||||
};
|
||||
let (path, query) = match rest.split_once('?') {
|
||||
Some((path, query)) => (path, Some(query)),
|
||||
None => (rest, None),
|
||||
};
|
||||
Ok(Parts { scheme, authority, path, query })
|
||||
}
|
||||
|
||||
/// Whether every character of a path (`allow_question` false) or a query/fragment (`allow_question` true) is
|
||||
/// allowed by RFC 3986 §3.3–§3.5, escapes included.
|
||||
fn chars_allowed(text: &str, allow_question: bool) -> bool {
|
||||
let mut bytes = text.bytes();
|
||||
while let Some(b) = bytes.next() {
|
||||
let ok = match b {
|
||||
b'%' => bytes.next().and_then(percent::hex_value).is_some() && bytes.next().and_then(percent::hex_value).is_some(),
|
||||
b':' | b'@' | b'/' => true,
|
||||
b'?' => allow_question,
|
||||
_ => is_unreserved(b) || is_sub_delim(b),
|
||||
};
|
||||
if !ok {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn port_number(text: &str, scheme: Scheme) -> Result<Option<u16>, AddressError> {
|
||||
if text.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut value: u32 = 0;
|
||||
for b in text.bytes() {
|
||||
let digit = match b {
|
||||
b'0'..=b'9' => u32::from(b.saturating_sub(b'0')),
|
||||
_ => return Err(AddressError::InvalidPort),
|
||||
};
|
||||
value = value.checked_mul(10).and_then(|v| v.checked_add(digit)).filter(|v| *v <= u32::from(u16::MAX)).ok_or(AddressError::InvalidPort)?;
|
||||
}
|
||||
let port = u16::try_from(value).map_err(|_| AddressError::InvalidPort)?;
|
||||
if port == 0 {
|
||||
return Err(AddressError::InvalidPort);
|
||||
}
|
||||
Ok(if port == scheme.default_port() { None } else { Some(port) })
|
||||
}
|
||||
|
||||
/// Separates an authority's host text (brackets kept for IPv6) from its port text, empty when absent.
|
||||
fn separate_port(authority: &str) -> Result<(&str, &str), AddressError> {
|
||||
if authority.starts_with('[') {
|
||||
let close = authority.find(']').ok_or(AddressError::InvalidIpv6)?;
|
||||
let end = close.checked_add(1).ok_or(AddressError::InvalidIpv6)?;
|
||||
let (host, rest) = authority.split_at_checked(end).ok_or(AddressError::InvalidIpv6)?;
|
||||
if rest.is_empty() {
|
||||
return Ok((host, ""));
|
||||
}
|
||||
let port = rest.strip_prefix(':').ok_or(AddressError::InvalidHost)?;
|
||||
return Ok((host, port));
|
||||
}
|
||||
if authority.contains(['[', ']']) {
|
||||
return Err(AddressError::InvalidHost);
|
||||
}
|
||||
Ok(authority.split_once(':').unwrap_or((authority, "")))
|
||||
}
|
||||
|
||||
/// Removes the `.` and `..` segments of a path (RFC 3986 §5.2.4).
|
||||
pub(crate) fn collapse_dot_segments(path: &str) -> String {
|
||||
let mut input = path;
|
||||
let mut output = String::with_capacity(path.len());
|
||||
while !input.is_empty() {
|
||||
if let Some(rest) = input.strip_prefix("../") {
|
||||
input = rest;
|
||||
} else if let Some(rest) = input.strip_prefix("./") {
|
||||
input = rest;
|
||||
} else if input.starts_with("/./") {
|
||||
input = input.get(2..).unwrap_or("");
|
||||
} else if input == "/." {
|
||||
input = "/";
|
||||
} else if input.starts_with("/../") {
|
||||
input = input.get(3..).unwrap_or("");
|
||||
drop_last_segment(&mut output);
|
||||
} else if input == "/.." {
|
||||
input = "/";
|
||||
drop_last_segment(&mut output);
|
||||
} else if input == "." || input == ".." {
|
||||
input = "";
|
||||
} else {
|
||||
let end = input.get(1..).and_then(|after_first| after_first.find('/')).and_then(|index| index.checked_add(1)).unwrap_or(input.len());
|
||||
let (segment, rest) = input.split_at_checked(end).unwrap_or((input, ""));
|
||||
output.push_str(segment);
|
||||
input = rest;
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn drop_last_segment(output: &mut String) {
|
||||
match output.rfind('/') {
|
||||
Some(index) => output.truncate(index),
|
||||
None => output.clear(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Merges a relative path with the path of a base that has an authority (RFC 3986 §5.2.3).
|
||||
fn base_directory_plus(base_path: &str, reference_path: &str) -> String {
|
||||
let directory = match base_path.rfind('/') {
|
||||
Some(index) => base_path.get(..=index).unwrap_or("/"),
|
||||
None => "/",
|
||||
};
|
||||
let mut merged = String::with_capacity(directory.len().saturating_add(reference_path.len()));
|
||||
merged.push_str(directory);
|
||||
merged.push_str(reference_path);
|
||||
merged
|
||||
}
|
||||
|
||||
impl Address {
|
||||
/// Parses an absolute `http` or `https` address.
|
||||
///
|
||||
/// This checks the syntax only; [`Address::parse_secure`] also applies the transport rule. A fragment is
|
||||
/// accepted and dropped (RFC 9110 §7.1).
|
||||
pub fn parse(input: &str) -> Result<Self, AddressError> {
|
||||
refuse_unusable_characters(input)?;
|
||||
let parts = reference_parts(input)?;
|
||||
let scheme_text = parts.scheme.ok_or(AddressError::MissingScheme)?;
|
||||
if !looks_like_scheme(scheme_text) {
|
||||
return Err(AddressError::MissingScheme);
|
||||
}
|
||||
let scheme = match scheme_text.to_ascii_lowercase().as_str() {
|
||||
"http" => Scheme::Http,
|
||||
"https" => Scheme::Https,
|
||||
_ => return Err(AddressError::UnsupportedScheme),
|
||||
};
|
||||
let authority = parts.authority.ok_or(AddressError::MissingAuthority)?;
|
||||
if authority.contains('@') {
|
||||
return Err(AddressError::UserInfo);
|
||||
}
|
||||
let (host_text, port_text) = separate_port(authority)?;
|
||||
let host = Host::parse(host_text)?;
|
||||
let port = port_number(port_text, scheme)?;
|
||||
if !chars_allowed(parts.path, false) {
|
||||
return Err(AddressError::InvalidPath);
|
||||
}
|
||||
let path = percent::normalise_escapes(parts.path).ok_or(AddressError::InvalidPath)?;
|
||||
let mut path = collapse_dot_segments(&path);
|
||||
if path.is_empty() {
|
||||
path.push('/');
|
||||
}
|
||||
let query = match parts.query {
|
||||
Some(query) => {
|
||||
if !chars_allowed(query, true) {
|
||||
return Err(AddressError::InvalidQuery);
|
||||
}
|
||||
Some(percent::normalise_escapes(query).ok_or(AddressError::InvalidQuery)?)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
Self::assemble(scheme, host, port, &path, query.as_deref())
|
||||
}
|
||||
|
||||
/// Parses an absolute address and applies the transport rule ([`Address::require_secure_transport`]).
|
||||
pub fn parse_secure(input: &str) -> Result<Self, AddressError> {
|
||||
let address = Self::parse(input)?;
|
||||
address.require_secure_transport()?;
|
||||
Ok(address)
|
||||
}
|
||||
|
||||
fn assemble(scheme: Scheme, host: Host, port: Option<u16>, path: &str, query: Option<&str>) -> Result<Self, AddressError> {
|
||||
let origin = Origin { scheme, host, port };
|
||||
let mut text = origin.to_string();
|
||||
let path_start = text.len();
|
||||
text.push_str(path);
|
||||
let query_start = match query {
|
||||
Some(query) => {
|
||||
let start = text.len();
|
||||
text.push('?');
|
||||
text.push_str(query);
|
||||
Some(start)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
if text.len() > MAX_ADDRESS_LEN {
|
||||
return Err(AddressError::TooLong);
|
||||
}
|
||||
let Origin { scheme, host, port } = origin;
|
||||
Ok(Self { text, scheme, host, port, path_start, query_start })
|
||||
}
|
||||
|
||||
/// Resolves a reference, relative or absolute, against this address (RFC 3986 §5.2.2, strict).
|
||||
///
|
||||
/// The result is checked like [`Address::parse`]; the transport rule is not applied.
|
||||
pub fn resolve(&self, reference: &str) -> Result<Self, AddressError> {
|
||||
if reference.is_empty() {
|
||||
return Ok(self.clone());
|
||||
}
|
||||
refuse_unusable_characters(reference)?;
|
||||
let parts = reference_parts(reference)?;
|
||||
if parts.scheme.is_some() {
|
||||
return Self::parse(reference);
|
||||
}
|
||||
let without_fragment = reference.split_once('#').map_or(reference, |(head, _)| head);
|
||||
if parts.authority.is_some() {
|
||||
return Self::parse(&format!("{}:{}", self.scheme.as_str(), without_fragment));
|
||||
}
|
||||
let (path, query) = if parts.path.is_empty() {
|
||||
(self.path().to_owned(), parts.query.or(self.query()))
|
||||
} else if parts.path.starts_with('/') {
|
||||
(parts.path.to_owned(), parts.query)
|
||||
} else {
|
||||
(base_directory_plus(self.path(), parts.path), parts.query)
|
||||
};
|
||||
// Dot-segment removal (§5.2.2, §5.2.4) runs in `parse`, after escapes are normalised (§6.2.2.2), so that
|
||||
// `%2e` segments are treated the same way whether the reference was relative or absolute.
|
||||
let mut target = self.origin().to_string();
|
||||
target.push_str(&path);
|
||||
if let Some(query) = query {
|
||||
target.push('?');
|
||||
target.push_str(query);
|
||||
}
|
||||
Self::parse(&target)
|
||||
}
|
||||
|
||||
/// The full address, for the request line. Do not log it: use [`Address::origin`] for diagnostics.
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
|
||||
/// The scheme.
|
||||
pub fn scheme(&self) -> Scheme {
|
||||
self.scheme
|
||||
}
|
||||
|
||||
/// The host.
|
||||
pub fn host(&self) -> &Host {
|
||||
&self.host
|
||||
}
|
||||
|
||||
/// The port, the scheme's default when the address names none.
|
||||
pub fn port(&self) -> u16 {
|
||||
self.port.unwrap_or_else(|| self.scheme.default_port())
|
||||
}
|
||||
|
||||
/// The path, starting with `/`.
|
||||
pub fn path(&self) -> &str {
|
||||
let end = self.query_start.unwrap_or(self.text.len());
|
||||
self.text.get(self.path_start..end).unwrap_or("/")
|
||||
}
|
||||
|
||||
/// The query without its `?`, if there is one.
|
||||
pub fn query(&self) -> Option<&str> {
|
||||
let start = self.query_start?.checked_add(1)?;
|
||||
self.text.get(start..)
|
||||
}
|
||||
|
||||
/// The origin: scheme, host and port.
|
||||
pub fn origin(&self) -> Origin {
|
||||
Origin { scheme: self.scheme, host: self.host.clone(), port: self.port }
|
||||
}
|
||||
|
||||
/// Whether both addresses have the same origin.
|
||||
pub fn same_origin(&self, other: &Self) -> bool {
|
||||
self.scheme == other.scheme && self.host == other.host && self.port == other.port
|
||||
}
|
||||
|
||||
/// Whether this address may carry credentials: `https`, or `http` to a loopback host
|
||||
/// (see [`Host::is_loopback`]).
|
||||
pub fn uses_secure_transport(&self) -> bool {
|
||||
self.scheme == Scheme::Https || self.host.is_loopback()
|
||||
}
|
||||
|
||||
/// The transport rule as a `Result`.
|
||||
pub fn require_secure_transport(&self) -> Result<(), AddressError> {
|
||||
if self.uses_secure_transport() { Ok(()) } else { Err(AddressError::InsecureTransport) }
|
||||
}
|
||||
|
||||
/// Checks an address that `self` handed out (a session's endpoint, a template's expansion, a redirect's
|
||||
/// target): it must satisfy the transport rule, and it may not be closer to this computer than `self`
|
||||
/// ([`Host::scope`]), so that a remote server cannot point the program, credentials and all, at a service
|
||||
/// on this computer or its local network. Only the address text is judged: a name that resolves to such an
|
||||
/// address can only be caught by the program performing the request.
|
||||
pub fn check_link(&self, target: &Self) -> Result<(), AddressError> {
|
||||
target.require_secure_transport()?;
|
||||
if target.host.scope() > self.host.scope() {
|
||||
return Err(AddressError::LocalFromRemote);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The same origin with another path (and no query).
|
||||
pub(crate) fn with_path(&self, path: &str) -> Result<Self, AddressError> {
|
||||
let mut text = self.origin().to_string();
|
||||
text.push_str(path);
|
||||
Self::parse(&text)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Address {
|
||||
/// Shows the origin only (requirement 1.8): a path or query may hold something private.
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Address({})", self.origin())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,243 @@
|
||||
use super::*;
|
||||
|
||||
fn parsed(input: &str) -> String {
|
||||
Address::parse(input).unwrap().as_str().to_owned()
|
||||
}
|
||||
|
||||
// RFC 3986 §3 and RFC 9110 §4.2.1–§4.2.2: the components of http and https addresses.
|
||||
#[test]
|
||||
fn parses_components() {
|
||||
let address = Address::parse("https://jmap.example.com:8443/api/v1?x=1&y=2").unwrap();
|
||||
assert_eq!(address.scheme(), Scheme::Https);
|
||||
assert_eq!(address.host(), &Host::Name("jmap.example.com".into()));
|
||||
assert_eq!(address.port(), 8443);
|
||||
assert_eq!(address.path(), "/api/v1");
|
||||
assert_eq!(address.query(), Some("x=1&y=2"));
|
||||
assert_eq!(address.as_str(), "https://jmap.example.com:8443/api/v1?x=1&y=2");
|
||||
}
|
||||
|
||||
// RFC 3986 §6.2.2 and §6.2.3, RFC 9110 §4.2.3: the three equivalent forms normalise to one.
|
||||
#[test]
|
||||
fn normal_form() {
|
||||
assert_eq!(parsed("http://example.com:80/~smith/home.html"), "http://example.com/~smith/home.html");
|
||||
assert_eq!(parsed("http://EXAMPLE.com/%7Esmith/home.html"), "http://example.com/~smith/home.html");
|
||||
assert_eq!(parsed("http://EXAMPLE.com:/%7esmith/home.html"), "http://example.com/~smith/home.html");
|
||||
assert_eq!(parsed("HTTPS://Example.ORG"), "https://example.org/");
|
||||
assert_eq!(parsed("https://example.org:443/a/./b/../c"), "https://example.org/a/c");
|
||||
assert_eq!(parsed("https://example.org/a%2fb?q=%2f%41"), "https://example.org/a%2Fb?q=%2FA");
|
||||
assert_eq!(parsed("https://example.org/a/%2e%2E/b"), "https://example.org/b");
|
||||
}
|
||||
|
||||
// RFC 9110 §7.1: the fragment is not part of the target.
|
||||
#[test]
|
||||
fn drops_fragment() {
|
||||
assert_eq!(parsed("https://example.com/a?b#c"), "https://example.com/a?b");
|
||||
assert_eq!(Address::parse("https://example.com/#a b"), Err(AddressError::ForbiddenCharacter));
|
||||
assert_eq!(Address::parse("https://example.com/#a#b"), Err(AddressError::InvalidFragment));
|
||||
}
|
||||
|
||||
// Requirement 1.1: what is not a usable absolute http/https address is refused.
|
||||
#[test]
|
||||
fn refuses_unusable_addresses() {
|
||||
let cases: &[(&str, AddressError)] = &[
|
||||
("", AddressError::Empty),
|
||||
("https://exa mple.com/", AddressError::ForbiddenCharacter),
|
||||
("https://example.com/\u{7f}", AddressError::ForbiddenCharacter),
|
||||
("https://example.com/\n", AddressError::ForbiddenCharacter),
|
||||
("https://example.com/é", AddressError::ForbiddenCharacter),
|
||||
("https://example.com/{x}", AddressError::ForbiddenCharacter),
|
||||
("https://example.com\\@example.org/", AddressError::Backslash),
|
||||
("https:\\\\example.com/", AddressError::Backslash),
|
||||
("//example.com/", AddressError::MissingScheme),
|
||||
("example.com/path", AddressError::MissingScheme),
|
||||
("ftp://example.com/", AddressError::UnsupportedScheme),
|
||||
("javascript:alert(1)", AddressError::UnsupportedScheme),
|
||||
("https:example.com", AddressError::MissingAuthority),
|
||||
("https:///path", AddressError::EmptyHost),
|
||||
("https://:443/", AddressError::EmptyHost),
|
||||
("https://user@example.com/", AddressError::UserInfo),
|
||||
("https://user:secret@example.com/", AddressError::UserInfo),
|
||||
("https://[::1/", AddressError::InvalidIpv6),
|
||||
("https://[::g]/", AddressError::InvalidIpv6),
|
||||
("https://[::1]x/", AddressError::InvalidHost),
|
||||
("https://example.com:65536/", AddressError::InvalidPort),
|
||||
("https://example.com:0/", AddressError::InvalidPort),
|
||||
("https://example.com:-1/", AddressError::InvalidPort),
|
||||
("https://example.com:12a/", AddressError::InvalidPort),
|
||||
("https://example.com:1:2/", AddressError::InvalidPort),
|
||||
("https://example.com/%zz", AddressError::InvalidPath),
|
||||
("https://example.com/[x]", AddressError::InvalidPath),
|
||||
("https://example.com/?%", AddressError::InvalidQuery),
|
||||
("https://127.1/", AddressError::AmbiguousNumericHost),
|
||||
];
|
||||
for (input, expected) in cases {
|
||||
assert_eq!(Address::parse(input).as_ref().map(Address::as_str), Err(expected), "{input:?}");
|
||||
}
|
||||
let long = format!("https://example.com/{}", "a".repeat(MAX_ADDRESS_LEN));
|
||||
assert_eq!(Address::parse(&long), Err(AddressError::TooLong));
|
||||
}
|
||||
|
||||
// RFC 3986 §3.2.3: an empty port means the default one; leading zeros are still the same number.
|
||||
#[test]
|
||||
fn ports() {
|
||||
assert_eq!(parsed("https://example.com:/"), "https://example.com/");
|
||||
assert_eq!(parsed("https://example.com:00443/"), "https://example.com/");
|
||||
assert_eq!(parsed("http://localhost:8080"), "http://localhost:8080/");
|
||||
assert_eq!(Address::parse("https://example.com:65535/").unwrap().port(), 65535);
|
||||
assert_eq!(parsed("https://[::1]:8443/x"), "https://[::1]:8443/x");
|
||||
}
|
||||
|
||||
// Requirement 1.2: https always; http only to localhost, 127.0.0.0/8 or [::1].
|
||||
#[test]
|
||||
fn transport_rule() {
|
||||
for ok in ["https://example.com/", "http://localhost/", "http://LocalHost:8080/", "http://127.0.0.1/", "http://127.9.8.7:1/", "http://[::1]/"] {
|
||||
assert!(Address::parse_secure(ok).is_ok(), "{ok}");
|
||||
}
|
||||
for refused in [
|
||||
"http://example.com/",
|
||||
"http://sub.localhost/",
|
||||
"http://localhost./",
|
||||
"http://192.0.2.1/",
|
||||
"http://[2001:db8::2]/",
|
||||
"http://[::ffff:127.0.0.1]/",
|
||||
] {
|
||||
assert_eq!(Address::parse_secure(refused), Err(AddressError::InsecureTransport), "{refused}");
|
||||
}
|
||||
}
|
||||
|
||||
// Requirement 1.2 hardened, RFC 9110 §4.2.1 and RFC 8620 §8.3: a remote host cannot hand out a loopback address.
|
||||
#[test]
|
||||
fn links_from_remote_to_loopback_are_refused() {
|
||||
let remote = Address::parse("https://jmap.example.com/").unwrap();
|
||||
let local = Address::parse("http://127.0.0.1:8080/").unwrap();
|
||||
let other = Address::parse("https://api.example.net/").unwrap();
|
||||
let plain = Address::parse("http://example.org/").unwrap();
|
||||
assert_eq!(remote.check_link(&local), Err(AddressError::LocalFromRemote));
|
||||
for sneaky in [
|
||||
"https://localhost./",
|
||||
"https://x.localhost/",
|
||||
"https://0.0.0.0/",
|
||||
"https://[::]/",
|
||||
"https://[::ffff:7f00:1]/",
|
||||
"https://192.168.1.10/",
|
||||
"https://[fe80::1]/",
|
||||
] {
|
||||
let target = Address::parse(sneaky).unwrap();
|
||||
assert_eq!(remote.check_link(&target), Err(AddressError::LocalFromRemote), "{sneaky}");
|
||||
}
|
||||
let lan = Address::parse("https://192.168.1.10/jmap").unwrap();
|
||||
assert_eq!(lan.check_link(&Address::parse("https://192.168.1.11/api").unwrap()), Ok(()));
|
||||
assert_eq!(lan.check_link(&Address::parse("https://localhost/api").unwrap()), Err(AddressError::LocalFromRemote));
|
||||
assert_eq!(remote.check_link(&plain), Err(AddressError::InsecureTransport));
|
||||
assert_eq!(remote.check_link(&other), Ok(()));
|
||||
assert_eq!(local.check_link(&local), Ok(()));
|
||||
assert_eq!(local.check_link(&remote), Ok(()));
|
||||
}
|
||||
|
||||
// RFC 3986 §5.4.1: the normal examples, hosts moved under example domains (the non-http "g:h" is
|
||||
// refused, as this crate only knows http and https).
|
||||
#[test]
|
||||
fn resolves_normal_examples() {
|
||||
let base = Address::parse("http://a.example.com/b/c/d;p?q").unwrap();
|
||||
let cases = [
|
||||
("g", "http://a.example.com/b/c/g"),
|
||||
("./g", "http://a.example.com/b/c/g"),
|
||||
("g/", "http://a.example.com/b/c/g/"),
|
||||
("/g", "http://a.example.com/g"),
|
||||
("//g.example.org", "http://g.example.org/"),
|
||||
("?y", "http://a.example.com/b/c/d;p?y"),
|
||||
("g?y", "http://a.example.com/b/c/g?y"),
|
||||
("#s", "http://a.example.com/b/c/d;p?q"),
|
||||
("g#s", "http://a.example.com/b/c/g"),
|
||||
("g?y#s", "http://a.example.com/b/c/g?y"),
|
||||
(";x", "http://a.example.com/b/c/;x"),
|
||||
("g;x", "http://a.example.com/b/c/g;x"),
|
||||
("g;x?y#s", "http://a.example.com/b/c/g;x?y"),
|
||||
("", "http://a.example.com/b/c/d;p?q"),
|
||||
(".", "http://a.example.com/b/c/"),
|
||||
("./", "http://a.example.com/b/c/"),
|
||||
("..", "http://a.example.com/b/"),
|
||||
("../", "http://a.example.com/b/"),
|
||||
("../g", "http://a.example.com/b/g"),
|
||||
("../..", "http://a.example.com/"),
|
||||
("../../", "http://a.example.com/"),
|
||||
("../../g", "http://a.example.com/g"),
|
||||
];
|
||||
for (reference, expected) in cases {
|
||||
assert_eq!(base.resolve(reference).unwrap().as_str(), expected, "{reference:?}");
|
||||
}
|
||||
assert_eq!(base.resolve("g:h"), Err(AddressError::UnsupportedScheme));
|
||||
}
|
||||
|
||||
// RFC 3986 §5.4.2: the abnormal examples (hosts under example domains), strict for "http:g".
|
||||
#[test]
|
||||
fn resolves_abnormal_examples() {
|
||||
let base = Address::parse("http://a.example.com/b/c/d;p?q").unwrap();
|
||||
let cases = [
|
||||
("../../../g", "http://a.example.com/g"),
|
||||
("../../../../g", "http://a.example.com/g"),
|
||||
("/./g", "http://a.example.com/g"),
|
||||
("/../g", "http://a.example.com/g"),
|
||||
("g.", "http://a.example.com/b/c/g."),
|
||||
(".g", "http://a.example.com/b/c/.g"),
|
||||
("g..", "http://a.example.com/b/c/g.."),
|
||||
("..g", "http://a.example.com/b/c/..g"),
|
||||
("./../g", "http://a.example.com/b/g"),
|
||||
("./g/.", "http://a.example.com/b/c/g/"),
|
||||
("g/./h", "http://a.example.com/b/c/g/h"),
|
||||
("g/../h", "http://a.example.com/b/c/h"),
|
||||
("g;x=1/./y", "http://a.example.com/b/c/g;x=1/y"),
|
||||
("g;x=1/../y", "http://a.example.com/b/c/y"),
|
||||
("g?y/./x", "http://a.example.com/b/c/g?y/./x"),
|
||||
("g?y/../x", "http://a.example.com/b/c/g?y/../x"),
|
||||
("g#s/./x", "http://a.example.com/b/c/g"),
|
||||
("g#s/../x", "http://a.example.com/b/c/g"),
|
||||
];
|
||||
for (reference, expected) in cases {
|
||||
assert_eq!(base.resolve(reference).unwrap().as_str(), expected, "{reference:?}");
|
||||
}
|
||||
assert_eq!(base.resolve("http:g"), Err(AddressError::MissingAuthority));
|
||||
// RFC 3986 §6.2.2.2: an escaped dot is a dot, whether the reference is relative or absolute.
|
||||
assert_eq!(base.resolve("/a/%2e%2e/../b"), Address::parse("http://a.example.com/a/%2e%2e/../b"));
|
||||
assert_eq!(base.resolve("/a/%2e%2e/../b").unwrap().path(), "/b");
|
||||
}
|
||||
|
||||
// RFC 3986 §5.2.4: the two worked examples of dot-segment removal.
|
||||
#[test]
|
||||
fn removes_dot_segments() {
|
||||
assert_eq!(collapse_dot_segments("/a/b/c/./../../g"), "/a/g");
|
||||
assert_eq!(collapse_dot_segments("mid/content=5/../6"), "mid/6");
|
||||
assert_eq!(collapse_dot_segments("/.."), "/");
|
||||
assert_eq!(collapse_dot_segments("//x/../y"), "//y");
|
||||
}
|
||||
|
||||
// Requirement 1.4: the origin elides the default port; the full form keeps everything else.
|
||||
#[test]
|
||||
fn origin_and_full_form() {
|
||||
let address = Address::parse("https://example.com:443/session?token=abc").unwrap();
|
||||
assert_eq!(address.origin().to_string(), "https://example.com");
|
||||
assert_eq!(address.origin().port(), 443);
|
||||
let address = Address::parse("http://[::1]:8080/x").unwrap();
|
||||
assert_eq!(address.origin().to_string(), "http://[::1]:8080");
|
||||
assert!(address.same_origin(&Address::parse("http://[0::1]:8080/y").unwrap()));
|
||||
assert!(!address.same_origin(&Address::parse("http://[::1]:8081/x").unwrap()));
|
||||
}
|
||||
|
||||
// Requirement 1.8: diagnostics never show more than the origin.
|
||||
#[test]
|
||||
fn debug_shows_origin_only() {
|
||||
let address = Address::parse("https://example.com/private/path?token=s3cr3t").unwrap();
|
||||
let shown = format!("{address:?}");
|
||||
assert_eq!(shown, "Address(https://example.com)");
|
||||
assert!(!shown.contains("s3cr3t") && !shown.contains("private"));
|
||||
}
|
||||
|
||||
// RFC 3986 §6.2.2: normalisation is idempotent, so parsing the full form gives the same address.
|
||||
#[test]
|
||||
fn reparse_is_stable() {
|
||||
for input in ["https://EXAMPLE.com:443/a/../b/%7e?Q=%2f", "http://[0:0::1]:80//x/./y", "https://example.org"] {
|
||||
let first = Address::parse(input).unwrap();
|
||||
let second = Address::parse(first.as_str()).unwrap();
|
||||
assert_eq!(first, second);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
//! Judging a response: status codes (RFC 9110 §15), problem details (RFC 9457, which obsoletes RFC 7807 and
|
||||
//! which JMAP's request-level errors use, RFC 8620 §3.6.1), content types (RFC 9110 §8.3) and JSON (RFC 8259).
|
||||
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use crate::error::{Error, LimitName, RequestProblem, Unusable};
|
||||
use crate::http::{HttpRequest, HttpResponse, PROBLEM_BODY_LIMIT};
|
||||
use crate::redact::{Excerpt, Redactor};
|
||||
use crate::retry::RetryAfter;
|
||||
|
||||
/// The URN prefix of JMAP's request-level problem types (RFC 8620 §3.6.1, §9.3).
|
||||
const JMAP_PROBLEM_PREFIX: &str = "urn:ietf:params:jmap:error:";
|
||||
/// The longest JMAP problem name kept.
|
||||
const MAX_PROBLEM_NAME_LEN: usize = 64;
|
||||
|
||||
/// Whether a `Content-Type` value names `expected` (RFC 9110 §8.3.1: type and subtype are case-insensitive;
|
||||
/// parameters such as `charset` are ignored, JSON being UTF-8 by RFC 8259 §8.1).
|
||||
pub(crate) fn media_type_is(content_type: Option<&str>, expected: &str) -> bool {
|
||||
content_type.is_some_and(|value| {
|
||||
let essence = value.split(';').next().unwrap_or("").trim_matches([' ', '\t']);
|
||||
essence.eq_ignore_ascii_case(expected)
|
||||
})
|
||||
}
|
||||
|
||||
/// The most JSON values a body may hold before it is parsed into memory.
|
||||
pub const MAX_JSON_VALUES: usize = 1_000_000;
|
||||
/// The most JSON objects a body may hold before it is parsed into memory.
|
||||
pub const MAX_JSON_OBJECTS: usize = 100_000;
|
||||
|
||||
/// Counts, without building anything, an upper bound of the values and the objects of a JSON text, so that a
|
||||
/// small text cannot expand into a huge tree (RFC 8259 §9 lets a parser limit what it accepts; RFC 8620 §8.4).
|
||||
/// Every value but the first follows `[`, `,` or `:`; strings are skipped with their escapes.
|
||||
fn within_budget(body: &[u8]) -> bool {
|
||||
let mut in_string = false;
|
||||
let mut escaped = false;
|
||||
let mut values = 1usize;
|
||||
let mut objects = 0usize;
|
||||
for &b in body {
|
||||
if in_string {
|
||||
if escaped {
|
||||
escaped = false;
|
||||
} else if b == b'\\' {
|
||||
escaped = true;
|
||||
} else if b == b'"' {
|
||||
in_string = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
match b {
|
||||
b'"' => in_string = true,
|
||||
b'{' => objects = objects.saturating_add(1),
|
||||
b'[' | b',' | b':' => values = values.saturating_add(1),
|
||||
_ => {}
|
||||
}
|
||||
if values > MAX_JSON_VALUES || objects > MAX_JSON_OBJECTS {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Parses a JSON text (RFC 8259), skipping a leading byte order mark (RFC 8259 §8.1 allows ignoring one).
|
||||
/// Nesting is bounded by `serde_json`'s recursion limit (128); the number of values and objects by
|
||||
/// [`MAX_JSON_VALUES`] and [`MAX_JSON_OBJECTS`].
|
||||
pub(crate) fn parse_json(body: &[u8]) -> Result<Value, Unusable> {
|
||||
let body = body.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(body);
|
||||
if !within_budget(body) {
|
||||
return Err(Unusable::TooComplex);
|
||||
}
|
||||
serde_json::from_slice(body).map_err(|_| Unusable::InvalidJson)
|
||||
}
|
||||
|
||||
/// What was understood of a problem details object.
|
||||
#[derive(Debug, Default)]
|
||||
struct Problem {
|
||||
jmap: Option<RequestProblem>,
|
||||
status: Option<u16>,
|
||||
limit: Option<LimitName>,
|
||||
text: Option<String>,
|
||||
names_bad_token: bool,
|
||||
}
|
||||
|
||||
fn jmap_problem(kind: &str) -> Option<RequestProblem> {
|
||||
let prefix = kind.get(..JMAP_PROBLEM_PREFIX.len())?;
|
||||
if !prefix.eq_ignore_ascii_case(JMAP_PROBLEM_PREFIX) {
|
||||
return None;
|
||||
}
|
||||
let name = kind.get(JMAP_PROBLEM_PREFIX.len()..)?;
|
||||
Some(match name {
|
||||
"unknownCapability" => RequestProblem::UnknownCapability,
|
||||
"notJSON" => RequestProblem::NotJson,
|
||||
"notRequest" => RequestProblem::NotRequest,
|
||||
"limit" => RequestProblem::Limit,
|
||||
other if !other.is_empty() && other.len() <= MAX_PROBLEM_NAME_LEN && other.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') => {
|
||||
RequestProblem::Other(other.to_owned())
|
||||
}
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Reads a problem details object (RFC 9457 §3.1). Members of the wrong type are ignored, as §3.1 requires.
|
||||
fn read_problem(response: &HttpResponse, limit: usize) -> Option<Problem> {
|
||||
let body = response.body();
|
||||
if body.is_empty() || body.len() > PROBLEM_BODY_LIMIT.min(limit) {
|
||||
return None;
|
||||
}
|
||||
let content_type = response.content_type();
|
||||
if content_type.is_some() && !media_type_is(content_type, "application/problem+json") && !media_type_is(content_type, "application/json") {
|
||||
return None;
|
||||
}
|
||||
let value = parse_json(body).ok()?;
|
||||
let object: &Map<String, Value> = value.as_object()?;
|
||||
let mut problem = Problem::default();
|
||||
if let Some(kind) = object.get("type").and_then(Value::as_str) {
|
||||
problem.jmap = jmap_problem(kind);
|
||||
let last = kind.rsplit([':', '/', '#']).next().unwrap_or("");
|
||||
problem.names_bad_token = last.eq_ignore_ascii_case("invalid_token") || last.eq_ignore_ascii_case("insufficient_scope");
|
||||
}
|
||||
problem.status = object.get("status").and_then(Value::as_u64).and_then(|status| u16::try_from(status).ok());
|
||||
problem.limit = object.get("limit").and_then(Value::as_str).and_then(LimitName::new);
|
||||
problem.text = object.get("detail").and_then(Value::as_str).or_else(|| object.get("title").and_then(Value::as_str)).map(str::to_owned);
|
||||
Some(problem)
|
||||
}
|
||||
|
||||
fn excerpt(problem: Option<&Problem>, redactor: &Redactor) -> Option<Excerpt> {
|
||||
let text = problem?.text.as_deref()?;
|
||||
let excerpt = redactor.excerpt(text);
|
||||
if excerpt.as_str().is_empty() { None } else { Some(excerpt) }
|
||||
}
|
||||
|
||||
/// Judges a response to `request`.
|
||||
///
|
||||
/// `Ok(())` means the body may be read as the request's kind expects (for session and API requests: a 2xx
|
||||
/// status, `application/json`, and a body within the size limit). Everything else becomes the [`Error`]
|
||||
/// variant the mail client can act on. Server text appears only as an [`Excerpt`] cleaned by `redactor`.
|
||||
pub fn judge_response(request: &HttpRequest, response: &HttpResponse, redactor: &Redactor) -> Result<(), Error> {
|
||||
let status = response.status();
|
||||
if (200..=299).contains(&status) {
|
||||
if response.body().len() > request.max_response_bytes() {
|
||||
return Err(Error::Unusable(Unusable::BodyTooLarge));
|
||||
}
|
||||
if request.kind().expects_json() && !media_type_is(response.content_type(), "application/json") {
|
||||
return Err(Error::Unusable(Unusable::WrongContentType));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if (300..=399).contains(&status) {
|
||||
return Err(judge_redirect(request, response));
|
||||
}
|
||||
let problem = read_problem(response, request.max_response_bytes());
|
||||
let detail = excerpt(problem.as_ref(), redactor);
|
||||
let retry_after = || response.retry_after().and_then(RetryAfter::parse);
|
||||
// A server-chosen identifier is shown in messages: drop it when it echoes a secret.
|
||||
let jmap = problem.as_ref().and_then(|problem| problem.jmap.clone()).map(|jmap| match jmap {
|
||||
RequestProblem::Other(name) if redactor.mentions_secret(&name) => RequestProblem::Other(String::new()),
|
||||
other => other,
|
||||
});
|
||||
let limit = problem.as_ref().and_then(|problem| problem.limit.clone()).filter(|limit| !redactor.mentions_secret(limit.as_str()));
|
||||
// RFC 9457 §3.1.2: the status member tells what the origin server meant when an intermediary changed it.
|
||||
let says_unauthorised = problem.as_ref().is_some_and(|problem| problem.status == Some(401) || problem.names_bad_token);
|
||||
Err(match status {
|
||||
401 => Error::CredentialsRefused,
|
||||
403 if says_unauthorised => Error::CredentialsRefused,
|
||||
429 | 503 => Error::Busy { retry_after: retry_after() },
|
||||
413 => Error::TooLarge { limit },
|
||||
_ if jmap == Some(RequestProblem::Limit) => match limit {
|
||||
Some(limit) if limit.is_concurrency() => Error::Busy { retry_after: retry_after() },
|
||||
limit => Error::TooLarge { limit },
|
||||
},
|
||||
403 => Error::Forbidden { detail },
|
||||
404 | 410 => Error::NotFound,
|
||||
400..=499 if jmap.is_some() => Error::Refused { problem: jmap, detail },
|
||||
400 | 405 | 406 | 415 | 422 => Error::Refused { problem: None, detail },
|
||||
_ => Error::Http { status, detail },
|
||||
})
|
||||
}
|
||||
|
||||
/// A redirect (RFC 9110 §15.4) is never followed here: its target is checked and handed to the caller.
|
||||
fn judge_redirect(request: &HttpRequest, response: &HttpResponse) -> Error {
|
||||
let status = response.status();
|
||||
let Some(location) = response.location() else {
|
||||
return Error::Http { status, detail: None };
|
||||
};
|
||||
if !matches!(status, 301 | 302 | 303 | 307 | 308) {
|
||||
return Error::Http { status, detail: None };
|
||||
}
|
||||
let from = request.address();
|
||||
let target = match from.resolve(location) {
|
||||
Ok(target) => target,
|
||||
Err(error) => return Error::RedirectRefused(error),
|
||||
};
|
||||
if let Err(error) = from.check_link(&target) {
|
||||
return Error::RedirectRefused(error);
|
||||
}
|
||||
Error::Redirected { same_origin: from.same_origin(&target), target }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,199 @@
|
||||
use super::*;
|
||||
use crate::address::{Address, AddressError};
|
||||
use crate::credentials::Credentials;
|
||||
use crate::http::{API_RESPONSE_LIMIT, HttpRequest, Method, RequestKind};
|
||||
use std::time::Duration;
|
||||
|
||||
fn request(kind: RequestKind) -> HttpRequest {
|
||||
let credentials = Credentials::bearer("token-abcdef").unwrap();
|
||||
let address = Address::parse("https://jmap.example.com/api/").unwrap();
|
||||
HttpRequest::new(Method::Post, address, kind, credentials.authorization())
|
||||
}
|
||||
|
||||
fn judge(response: HttpResponse) -> Result<(), Error> {
|
||||
let mut redactor = Redactor::new();
|
||||
redactor.add("token-abcdef");
|
||||
judge_response(&request(RequestKind::Api), &response, &redactor)
|
||||
}
|
||||
|
||||
fn problem(status: u16, body: &str) -> HttpResponse {
|
||||
HttpResponse::new(status, body.as_bytes().to_vec()).with_field("Content-Type", "application/problem+json")
|
||||
}
|
||||
|
||||
// RFC 8620 §3.1: a successful API response is application/json.
|
||||
#[test]
|
||||
fn success_needs_json() {
|
||||
let ok = HttpResponse::new(200, b"{}".to_vec()).with_field("Content-Type", "Application/JSON; charset=utf-8");
|
||||
assert_eq!(judge(ok), Ok(()));
|
||||
let html = HttpResponse::new(200, b"<html>".to_vec()).with_field("Content-Type", "text/html");
|
||||
assert_eq!(judge(html), Err(Error::Unusable(Unusable::WrongContentType)));
|
||||
assert_eq!(judge(HttpResponse::new(200, b"{}".to_vec())), Err(Error::Unusable(Unusable::WrongContentType)));
|
||||
let download = HttpResponse::new(200, vec![1, 2, 3]).with_field("Content-Type", "application/pdf");
|
||||
assert_eq!(judge_response(&request(RequestKind::Download), &download, &Redactor::new()), Ok(()));
|
||||
}
|
||||
|
||||
// Requirement 2.2: a limit the caller set for one request is enforced too, at the octet.
|
||||
#[test]
|
||||
fn per_request_body_limit() {
|
||||
let limited = request(RequestKind::Download).with_response_limit(10);
|
||||
let exact = HttpResponse::new(200, vec![0; 10]);
|
||||
assert_eq!(judge_response(&limited, &exact, &Redactor::new()), Ok(()));
|
||||
let over = HttpResponse::new(200, vec![0; 11]);
|
||||
assert_eq!(judge_response(&limited, &over, &Redactor::new()), Err(Error::Unusable(Unusable::BodyTooLarge)));
|
||||
}
|
||||
|
||||
// Requirement 2.2: the crate refuses a body above the limit it states for the kind of request.
|
||||
#[test]
|
||||
fn success_body_limit() {
|
||||
let big = HttpResponse::new(200, vec![b' '; API_RESPONSE_LIMIT + 1]).with_field("Content-Type", "application/json");
|
||||
assert_eq!(judge(big), Err(Error::Unusable(Unusable::BodyTooLarge)));
|
||||
}
|
||||
|
||||
// RFC 9110 §15.5.2 and §15.5.4, RFC 9457 §3.1.2: 401, and 403 when the problem says so.
|
||||
#[test]
|
||||
fn credentials_refused() {
|
||||
assert_eq!(judge(HttpResponse::new(401, Vec::new())), Err(Error::CredentialsRefused));
|
||||
assert_eq!(judge(problem(403, r#"{"status":401,"title":"Unauthorized"}"#)), Err(Error::CredentialsRefused));
|
||||
assert_eq!(judge(problem(403, r#"{"type":"https://example.com/probs/invalid_token"}"#)), Err(Error::CredentialsRefused));
|
||||
assert!(matches!(judge(problem(403, r#"{"status":403,"detail":"no access to this account"}"#)), Err(Error::Forbidden { detail: Some(_) })));
|
||||
assert!(matches!(judge(HttpResponse::new(403, Vec::new())), Err(Error::Forbidden { detail: None })));
|
||||
}
|
||||
|
||||
// Requirement 2.4 (429 and 503), RFC 9110 §15.6.4 and §10.2.3: busy, with the delay asked for.
|
||||
#[test]
|
||||
fn busy() {
|
||||
let response = HttpResponse::new(503, Vec::new()).with_field("Retry-After", "120");
|
||||
assert_eq!(judge(response), Err(Error::Busy { retry_after: Some(RetryAfter::Delay(Duration::from_secs(120))) }));
|
||||
let response = HttpResponse::new(429, Vec::new()).with_field("Retry-After", "soon");
|
||||
assert_eq!(judge(response), Err(Error::Busy { retry_after: None }));
|
||||
let concurrency = problem(400, r#"{"type":"urn:ietf:params:jmap:error:limit","limit":"maxConcurrentRequests","status":400}"#);
|
||||
assert_eq!(judge(concurrency), Err(Error::Busy { retry_after: None }));
|
||||
}
|
||||
|
||||
// RFC 9110 §15.5.14 and RFC 8620 §3.6.1: 413 and the JMAP limit problem with its limit name.
|
||||
#[test]
|
||||
fn too_large() {
|
||||
assert_eq!(judge(HttpResponse::new(413, Vec::new())), Err(Error::TooLarge { limit: None }));
|
||||
let limit = problem(400, r#"{"type":"urn:ietf:params:jmap:error:limit","limit":"maxSizeRequest","status":400,"detail":"too big"}"#);
|
||||
assert_eq!(judge(limit), Err(Error::TooLarge { limit: LimitName::new("maxSizeRequest") }));
|
||||
let odd = problem(400, r#"{"type":"urn:ietf:params:jmap:error:limit","limit":"max size\n"}"#);
|
||||
assert_eq!(judge(odd), Err(Error::TooLarge { limit: None }));
|
||||
}
|
||||
|
||||
// RFC 8620 §3.6.1: the other request-level problems are recognised; RFC 9110 §15.5.5 and §15.5.11.
|
||||
#[test]
|
||||
fn refused_and_not_found() {
|
||||
let unknown = problem(
|
||||
400,
|
||||
r#"{"type":"urn:ietf:params:jmap:error:unknownCapability","status":400,"detail":"The Request object used capability 'https://example.com/apis/foobar', which is not supported by this server."}"#,
|
||||
);
|
||||
match judge(unknown) {
|
||||
Err(Error::Refused { problem: Some(RequestProblem::UnknownCapability), detail: Some(detail) }) => {
|
||||
assert!(detail.as_str().starts_with("The Request object used capability"));
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
assert!(matches!(
|
||||
judge(problem(400, r#"{"type":"urn:ietf:params:jmap:error:notJSON"}"#)),
|
||||
Err(Error::Refused { problem: Some(RequestProblem::NotJson), .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
judge(problem(400, r#"{"type":"urn:ietf:params:jmap:error:notRequest"}"#)),
|
||||
Err(Error::Refused { problem: Some(RequestProblem::NotRequest), .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
judge(problem(400, r#"{"type":"URN:IETF:params:jmap:error:somethingNew"}"#)),
|
||||
Err(Error::Refused { problem: Some(RequestProblem::Other(_)), .. })
|
||||
));
|
||||
assert!(matches!(judge(HttpResponse::new(400, b"not json".to_vec())), Err(Error::Refused { problem: None, detail: None })));
|
||||
assert_eq!(judge(HttpResponse::new(404, Vec::new())), Err(Error::NotFound));
|
||||
assert_eq!(judge(HttpResponse::new(410, Vec::new())), Err(Error::NotFound));
|
||||
}
|
||||
|
||||
// Requirement 3.5: a server-chosen identifier that echoes a secret is not shown.
|
||||
#[test]
|
||||
fn identifiers_echoing_secrets_are_dropped() {
|
||||
let mut redactor = Redactor::new();
|
||||
redactor.add("hunter22");
|
||||
let judge = |body: &str| judge_response(&request(RequestKind::Api), &problem(400, body), &redactor);
|
||||
assert_eq!(
|
||||
judge(r#"{"type":"urn:ietf:params:jmap:error:hunter22"}"#),
|
||||
Err(Error::Refused { problem: Some(RequestProblem::Other(String::new())), detail: None })
|
||||
);
|
||||
assert_eq!(judge(r#"{"type":"urn:ietf:params:jmap:error:limit","limit":"xhunter22"}"#), Err(Error::TooLarge { limit: None }));
|
||||
}
|
||||
|
||||
// RFC 9457 §3.1: a member of the wrong type is ignored.
|
||||
#[test]
|
||||
fn wrong_member_types_are_ignored() {
|
||||
let response = problem(400, r#"{"type":42,"status":"401","limit":[],"detail":{"x":1},"title":"Bad"}"#);
|
||||
match judge(response) {
|
||||
Err(Error::Refused { problem: None, detail: Some(detail) }) => assert_eq!(detail.as_str(), "Bad"),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
// Requirement 2.4: server text is a short, clean excerpt without secrets.
|
||||
#[test]
|
||||
fn detail_is_cleaned() {
|
||||
let body = format!(r#"{{"detail":"bad token token-abcdef\n\u001b[2J{}"}}"#, "x".repeat(500));
|
||||
match judge(problem(500, &body)) {
|
||||
Err(Error::Http { status: 500, detail: Some(detail) }) => {
|
||||
assert!(detail.as_str().starts_with("bad token [redacted] [2J"));
|
||||
assert!(!detail.as_str().contains('\n') && !detail.as_str().contains('\u{1b}'));
|
||||
assert!(detail.as_str().chars().count() <= crate::redact::EXCERPT_MAX_CHARS + 1);
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
let html = HttpResponse::new(502, b"<h1>Bad gateway</h1>".to_vec()).with_field("Content-Type", "text/html");
|
||||
assert_eq!(judge(html), Err(Error::Http { status: 502, detail: None }));
|
||||
}
|
||||
|
||||
// RFC 9110 §10.2.2 and §15.4: redirects are judged, not followed; http and loopback targets are refused.
|
||||
#[test]
|
||||
fn redirects() {
|
||||
let response = HttpResponse::new(307, Vec::new()).with_field("Location", "/api/v2/");
|
||||
match judge(response) {
|
||||
Err(Error::Redirected { target, same_origin: true }) => assert_eq!(target.as_str(), "https://jmap.example.com/api/v2/"),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
let response = HttpResponse::new(301, Vec::new()).with_field("Location", "https://jmap.example.net/api/");
|
||||
assert!(matches!(judge(response), Err(Error::Redirected { same_origin: false, .. })));
|
||||
let response = HttpResponse::new(302, Vec::new()).with_field("Location", "http://jmap.example.com/api/");
|
||||
assert_eq!(judge(response), Err(Error::RedirectRefused(AddressError::InsecureTransport)));
|
||||
let response = HttpResponse::new(308, Vec::new()).with_field("Location", "http://127.0.0.1:8080/");
|
||||
assert_eq!(judge(response), Err(Error::RedirectRefused(AddressError::LocalFromRemote)));
|
||||
let response = HttpResponse::new(302, Vec::new()).with_field("Location", "https://user@example.com/");
|
||||
assert_eq!(judge(response), Err(Error::RedirectRefused(AddressError::UserInfo)));
|
||||
assert_eq!(judge(HttpResponse::new(302, Vec::new())), Err(Error::Http { status: 302, detail: None }));
|
||||
assert_eq!(judge(HttpResponse::new(304, Vec::new())), Err(Error::Http { status: 304, detail: None }));
|
||||
}
|
||||
|
||||
// RFC 9110 §15: anything else is an HTTP error with its status.
|
||||
#[test]
|
||||
fn other_statuses() {
|
||||
assert_eq!(judge(HttpResponse::new(500, Vec::new())), Err(Error::Http { status: 500, detail: None }));
|
||||
assert_eq!(judge(HttpResponse::new(418, Vec::new())), Err(Error::Http { status: 418, detail: None }));
|
||||
assert_eq!(judge(HttpResponse::new(100, Vec::new())), Err(Error::Http { status: 100, detail: None }));
|
||||
assert_eq!(judge(HttpResponse::new(0, Vec::new())), Err(Error::Http { status: 0, detail: None }));
|
||||
}
|
||||
|
||||
// RFC 8259 §8.1: a byte order mark may be ignored.
|
||||
#[test]
|
||||
fn json_with_bom() {
|
||||
assert!(parse_json(b"\xEF\xBB\xBF{\"a\":1}").is_ok());
|
||||
assert_eq!(parse_json(b"{\"a\":"), Err(Unusable::InvalidJson));
|
||||
let deep = "[".repeat(10_000);
|
||||
assert_eq!(parse_json(deep.as_bytes()), Err(Unusable::InvalidJson));
|
||||
}
|
||||
|
||||
// RFC 8259 §9 and RFC 8620 §8.4: a text that would expand into too many values is refused before parsing.
|
||||
#[test]
|
||||
fn json_budget() {
|
||||
let many_objects = format!("[{}{{}}]", "{},".repeat(MAX_JSON_OBJECTS));
|
||||
assert_eq!(parse_json(many_objects.as_bytes()), Err(Unusable::TooComplex));
|
||||
let many_values = format!("[{}0]", "0,".repeat(MAX_JSON_VALUES));
|
||||
assert_eq!(parse_json(many_values.as_bytes()), Err(Unusable::TooComplex));
|
||||
let in_strings = format!("[\"{}\"]", "{},:[\\\"".repeat(200_000));
|
||||
assert!(parse_json(in_strings.as_bytes()).is_ok());
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
//! Fetching the session and talking to the API through the program's HTTP exchange.
|
||||
//!
|
||||
//! Each operation exists in two forms: a synchronous pair (build the [`HttpRequest`], then read the
|
||||
//! [`HttpResponse`]) for programs that drive the exchange themselves, and an `async` helper that does both
|
||||
//! through an [`HttpExchange`]. Neither follows redirects: a redirect comes back as
|
||||
//! [`Error::Redirected`] with a checked target, and the caller decides.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::address::Address;
|
||||
use crate::classify::judge_response;
|
||||
use crate::credentials::Credentials;
|
||||
use crate::error::{BuildError, Error};
|
||||
use crate::http::{HttpExchange, HttpRequest, HttpResponse, Method, RequestKind};
|
||||
use crate::id::Id;
|
||||
use crate::redact::{Excerpt, Redactor};
|
||||
use crate::request::ApiRequest;
|
||||
use crate::response::ApiResponse;
|
||||
use crate::session::{Account, CAPABILITY_MAIL, CoreLimits, Session};
|
||||
|
||||
/// A redactor knowing the credentials and the session address.
|
||||
fn base_redactor(address: &Address, credentials: &Credentials) -> Redactor {
|
||||
let mut redactor = Redactor::new();
|
||||
credentials.register_secrets(&mut redactor);
|
||||
redactor.add_personal(address.as_str());
|
||||
if let Some(query) = address.query() {
|
||||
redactor.add_personal(query);
|
||||
// A query parameter may carry a token: each value, as written and decoded.
|
||||
for pair in query.split('&') {
|
||||
let value = pair.split_once('=').map_or(pair, |(_, value)| value);
|
||||
redactor.add_personal(value);
|
||||
if let Ok(decoded) = crate::percent::decode(value) {
|
||||
let decoded = crate::credentials::Secret::new(decoded);
|
||||
redactor.add_personal(decoded.expose());
|
||||
}
|
||||
}
|
||||
}
|
||||
redactor
|
||||
}
|
||||
|
||||
/// Builds the request that fetches the session object (RFC 8620 §2). The address must satisfy the transport
|
||||
/// rule, since the credentials travel with the request.
|
||||
pub fn session_request(address: &Address, credentials: &Credentials) -> Result<HttpRequest, Error> {
|
||||
address.require_secure_transport()?;
|
||||
Ok(HttpRequest::new(Method::Get, address.clone(), RequestKind::Session, credentials.authorization()))
|
||||
}
|
||||
|
||||
/// Reads the response to a [`session_request`].
|
||||
pub fn read_session(request: &HttpRequest, response: &HttpResponse, credentials: &Credentials) -> Result<Session, Error> {
|
||||
let redactor = base_redactor(request.address(), credentials);
|
||||
judge_response(request, response, &redactor)?;
|
||||
Session::parse(response.body(), request.address())
|
||||
}
|
||||
|
||||
/// Fetches and reads the session object through `exchange`.
|
||||
pub async fn fetch_session<E: HttpExchange>(exchange: &E, address: &Address, credentials: &Credentials) -> Result<Session, Error> {
|
||||
let request = session_request(address, credentials)?;
|
||||
let response = exchange.exchange(&request).await?;
|
||||
read_session(&request, &response, credentials)
|
||||
}
|
||||
|
||||
/// A JMAP client for one session and one mail account.
|
||||
#[derive(Clone)]
|
||||
pub struct Client {
|
||||
session: Session,
|
||||
credentials: Credentials,
|
||||
account: Id,
|
||||
redactor: Redactor,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// A client for the session's mail account ([`Session::mail_account`]).
|
||||
pub fn new(session: Session, credentials: Credentials) -> Result<Self, Error> {
|
||||
let account = session.mail_account().cloned().ok_or(Error::NoMailAccount)?;
|
||||
Self::for_account(session, credentials, account)
|
||||
}
|
||||
|
||||
/// A client for a chosen account of the session.
|
||||
pub fn for_account(session: Session, credentials: Credentials, account: Id) -> Result<Self, Error> {
|
||||
let account_name = session.account(&account).ok_or(Error::NoMailAccount)?.name().to_owned();
|
||||
let mut redactor = base_redactor(session.address(), &credentials);
|
||||
redactor.add_personal(session.username());
|
||||
redactor.add_personal(&account_name);
|
||||
Ok(Self { session, credentials, account, redactor })
|
||||
}
|
||||
|
||||
/// The session.
|
||||
pub fn session(&self) -> &Session {
|
||||
&self.session
|
||||
}
|
||||
|
||||
/// The account id in use.
|
||||
pub fn account_id(&self) -> &Id {
|
||||
&self.account
|
||||
}
|
||||
|
||||
/// The account in use.
|
||||
pub fn account(&self) -> Option<&Account> {
|
||||
self.session.account(&self.account)
|
||||
}
|
||||
|
||||
/// The core limits, defaulted and capped.
|
||||
pub fn limits(&self) -> &CoreLimits {
|
||||
self.session.core_limits()
|
||||
}
|
||||
|
||||
/// Whether the server advertises `capability`.
|
||||
pub fn has_capability(&self, capability: &str) -> bool {
|
||||
self.session.has_capability(capability)
|
||||
}
|
||||
|
||||
/// Whether the account in use offers `capability`.
|
||||
pub fn account_has_capability(&self, capability: &str) -> bool {
|
||||
self.account().is_some_and(|account| account.has_capability(capability))
|
||||
}
|
||||
|
||||
/// Whether the account offers mail.
|
||||
pub fn has_mail(&self) -> bool {
|
||||
self.account_has_capability(CAPABILITY_MAIL)
|
||||
}
|
||||
|
||||
/// Cleans a server's text for an error message, with every secret this client knows replaced.
|
||||
pub fn excerpt(&self, text: &str) -> Excerpt {
|
||||
self.redactor.excerpt(text)
|
||||
}
|
||||
|
||||
/// The redactor holding this client's secrets.
|
||||
pub fn redactor(&self) -> &Redactor {
|
||||
&self.redactor
|
||||
}
|
||||
|
||||
/// Whether `response` reports a session state other than the one this client holds (RFC 8620 §3.4): the
|
||||
/// session should then be fetched again.
|
||||
pub fn is_session_stale(&self, response: &ApiResponse) -> bool {
|
||||
response.session_state() != self.session.state()
|
||||
}
|
||||
|
||||
/// The address to download a blob (RFC 8620 §6.2), with `media_type` as `type` and `name` as `name`.
|
||||
pub fn download_address(&self, blob: &Id, media_type: &str, name: &str) -> Result<Address, Error> {
|
||||
let values = [("accountId", self.account.as_str()), ("blobId", blob.as_str()), ("type", media_type), ("name", name)];
|
||||
Ok(self.session.download_template().expand(&values)?)
|
||||
}
|
||||
|
||||
/// The address to upload a blob to the account (RFC 8620 §6.1).
|
||||
pub fn upload_address(&self) -> Result<Address, Error> {
|
||||
Ok(self.session.upload_template().expand(&[("accountId", self.account.as_str())])?)
|
||||
}
|
||||
|
||||
/// The request that fetches the session again.
|
||||
pub fn session_request(&self) -> Result<HttpRequest, Error> {
|
||||
session_request(self.session.address(), &self.credentials)
|
||||
}
|
||||
|
||||
/// The HTTP request for an API request (RFC 8620 §3.1). Checked first: the capabilities used must be
|
||||
/// advertised, the number of calls must be within `maxCallsInRequest`, and the body within
|
||||
/// `maxSizeRequest`.
|
||||
pub fn api_request(&self, request: &ApiRequest) -> Result<HttpRequest, Error> {
|
||||
if request.capabilities().any(|capability| !self.session.has_capability(capability)) {
|
||||
return Err(BuildError::UnsupportedCapability.into());
|
||||
}
|
||||
let calls = u64::try_from(request.len()).unwrap_or(u64::MAX);
|
||||
let max_calls = self.limits().max_calls_in_request();
|
||||
if calls > max_calls {
|
||||
return Err(BuildError::TooManyCalls { limit: max_calls }.into());
|
||||
}
|
||||
let body = request.to_json()?;
|
||||
let max_size = self.limits().max_size_request();
|
||||
if u64::try_from(body.len()).unwrap_or(u64::MAX) > max_size {
|
||||
return Err(BuildError::TooLarge { limit: max_size }.into());
|
||||
}
|
||||
let address = self.session.api_address().clone();
|
||||
let http = HttpRequest::new(Method::Post, address, RequestKind::Api, self.credentials.authorization()).with_json_body(body);
|
||||
Ok(match request.response_limit() {
|
||||
Some(limit) => http.with_response_limit(limit),
|
||||
None => http,
|
||||
})
|
||||
}
|
||||
|
||||
/// Reads the response to an [`Client::api_request`].
|
||||
pub fn read_api_response(&self, request: &HttpRequest, response: &HttpResponse) -> Result<ApiResponse, Error> {
|
||||
judge_response(request, response, &self.redactor)?;
|
||||
ApiResponse::parse(response.body(), &self.redactor)
|
||||
}
|
||||
|
||||
/// The HTTP request that downloads a blob (RFC 8620 §6.2).
|
||||
pub fn download_request(&self, blob: &Id, media_type: &str, name: &str) -> Result<HttpRequest, Error> {
|
||||
let address = self.download_address(blob, media_type, name)?;
|
||||
Ok(HttpRequest::new(Method::Get, address, RequestKind::Download, self.credentials.authorization()))
|
||||
}
|
||||
|
||||
/// Reads the response to a [`Client::download_request`] and hands back the blob's bytes.
|
||||
pub fn read_download(&self, request: &HttpRequest, response: HttpResponse) -> Result<Vec<u8>, Error> {
|
||||
judge_response(request, &response, &self.redactor)?;
|
||||
Ok(response.into_body())
|
||||
}
|
||||
|
||||
/// Sends an API request through `exchange` and reads the response.
|
||||
pub async fn call<E: HttpExchange>(&self, exchange: &E, request: &ApiRequest) -> Result<ApiResponse, Error> {
|
||||
let http = self.api_request(request)?;
|
||||
let response = exchange.exchange(&http).await?;
|
||||
self.read_api_response(&http, &response)
|
||||
}
|
||||
|
||||
/// Downloads a blob through `exchange`.
|
||||
///
|
||||
/// `max_bytes`, when given, lowers the largest body accepted for this download below
|
||||
/// [`DOWNLOAD_RESPONSE_LIMIT`](crate::http::DOWNLOAD_RESPONSE_LIMIT); the exchange sees it before
|
||||
/// reading the body.
|
||||
pub async fn download<E: HttpExchange>(
|
||||
&self,
|
||||
exchange: &E,
|
||||
blob: &Id,
|
||||
media_type: &str,
|
||||
name: &str,
|
||||
max_bytes: Option<usize>,
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
let mut http = self.download_request(blob, media_type, name)?;
|
||||
if let Some(limit) = max_bytes {
|
||||
http = http.with_response_limit(limit);
|
||||
}
|
||||
let response = exchange.exchange(&http).await?;
|
||||
self.read_download(&http, response)
|
||||
}
|
||||
|
||||
/// Fetches the session again through `exchange`, for when [`Client::is_session_stale`] says so.
|
||||
pub async fn refetch_session<E: HttpExchange>(&self, exchange: &E) -> Result<Session, Error> {
|
||||
fetch_session(exchange, self.session.address(), &self.credentials).await
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Client {
|
||||
/// Only what says nothing about the person: the authentication scheme, how many capabilities the
|
||||
/// server offers, and the core limits. No account id, no address or origin (a personal domain names its
|
||||
/// owner), no user name, no account name, no secret.
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Client")
|
||||
.field("scheme", &self.credentials.scheme_name())
|
||||
.field("capabilities", &self.session.capabilities().count())
|
||||
.field("limits", self.session.core_limits())
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,245 @@
|
||||
use super::*;
|
||||
use crate::address::AddressError;
|
||||
use crate::error::Unusable;
|
||||
use crate::http::TransportError;
|
||||
use crate::retry::RetryAfter;
|
||||
use crate::session::tests::{EXAMPLE, session_address};
|
||||
use crate::test_support::{FakeExchange, block_on, made_secret};
|
||||
use serde_json::{Map, Value, json};
|
||||
use std::time::Duration;
|
||||
|
||||
fn credentials() -> Credentials {
|
||||
Credentials::basic("john@example.com", &made_secret("open-sesame-42")).unwrap()
|
||||
}
|
||||
|
||||
fn json_response(status: u16, body: &Value) -> HttpResponse {
|
||||
HttpResponse::new(status, body.to_string().into_bytes()).with_field("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
fn client() -> Client {
|
||||
let session = Session::parse(EXAMPLE.as_bytes(), &session_address()).unwrap();
|
||||
Client::new(session, credentials()).unwrap()
|
||||
}
|
||||
|
||||
// RFC 8620 §2: the session is fetched with an authenticated GET and parsed.
|
||||
#[test]
|
||||
fn fetches_session() {
|
||||
let exchange = FakeExchange::new(vec![Ok(HttpResponse::new(200, EXAMPLE.as_bytes().to_vec()).with_field("Content-Type", "application/json"))]);
|
||||
let session = block_on(fetch_session(&exchange, &session_address(), &credentials())).unwrap();
|
||||
assert_eq!(session.state(), "75128aab4b1b");
|
||||
let sent = exchange.sent();
|
||||
assert_eq!(sent.len(), 1);
|
||||
assert_eq!(sent[0].method(), Method::Get);
|
||||
assert_eq!(sent[0].url(), "https://jmap.example.com/.well-known/jmap");
|
||||
assert!(sent[0].fields().any(|(name, value)| name == "Authorization" && value == "Basic am9obkBleGFtcGxlLmNvbTpvcGVuLXNlc2FtZS00Mg=="));
|
||||
}
|
||||
|
||||
// Requirement 1.2: credentials are never sent over plain http to a remote host.
|
||||
#[test]
|
||||
fn refuses_insecure_session_address() {
|
||||
let exchange = FakeExchange::new(Vec::new());
|
||||
let address = Address::parse("http://jmap.example.com/").unwrap();
|
||||
assert_eq!(block_on(fetch_session(&exchange, &address, &credentials())), Err(Error::Address(AddressError::InsecureTransport)));
|
||||
assert!(exchange.sent().is_empty());
|
||||
}
|
||||
|
||||
// RFC 8620 §2.2 ("following any redirects") with requirement 3.1: the redirect comes back to the caller.
|
||||
#[test]
|
||||
fn session_redirect_is_returned() {
|
||||
let exchange = FakeExchange::new(vec![Ok(HttpResponse::new(301, Vec::new()).with_field("Location", "https://mail.example.net/jmap/session"))]);
|
||||
match block_on(fetch_session(&exchange, &session_address(), &credentials())) {
|
||||
Err(Error::Redirected { target, same_origin: false }) => assert_eq!(target.as_str(), "https://mail.example.net/jmap/session"),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
let exchange = FakeExchange::new(vec![Ok(HttpResponse::new(302, Vec::new()).with_field("Location", "http://mail.example.net/"))]);
|
||||
assert_eq!(block_on(fetch_session(&exchange, &session_address(), &credentials())), Err(Error::RedirectRefused(AddressError::InsecureTransport)));
|
||||
}
|
||||
|
||||
// Requirement 3.2: the account, limits, capabilities, download and upload addresses.
|
||||
#[test]
|
||||
fn client_exposes_what_mail_code_needs() {
|
||||
let client = client();
|
||||
assert_eq!(client.account_id().as_str(), "A13824");
|
||||
assert_eq!(client.limits().max_calls_in_request(), 32);
|
||||
assert!(client.has_capability(CAPABILITY_MAIL));
|
||||
assert!(client.has_mail());
|
||||
assert!(!client.has_capability("urn:ietf:params:jmap:calendars"));
|
||||
let blob = Id::new("Gb1").unwrap();
|
||||
assert_eq!(
|
||||
client.download_address(&blob, "image/png", "photo 1.png").unwrap().as_str(),
|
||||
"https://jmap.example.com/download/A13824/Gb1/photo%201.png?accept=image%2Fpng"
|
||||
);
|
||||
assert_eq!(client.upload_address().unwrap().as_str(), "https://jmap.example.com/upload/A13824/");
|
||||
assert!(matches!(client.download_address(&blob, "image/png", ".."), Err(Error::Template(_))));
|
||||
}
|
||||
|
||||
// RFC 8620 §3.1, §3.3, §3.4 and §3.7: a request with a result reference, and its per-call results.
|
||||
#[test]
|
||||
fn calls_the_api() {
|
||||
let client = client();
|
||||
let mut request = ApiRequest::new();
|
||||
request.using(CAPABILITY_MAIL).unwrap();
|
||||
let query = request.add_call("Email/query", Map::from_iter([("accountId".to_owned(), json!("A13824"))])).unwrap();
|
||||
let ids = request.reference(&query, "Email/query", "/ids").unwrap();
|
||||
let get = request.add_call_with_references("Email/get", Map::from_iter([("accountId".to_owned(), json!("A13824"))]), vec![("ids", ids)]).unwrap();
|
||||
let answer = json!({
|
||||
"methodResponses": [
|
||||
["Email/query", {"accountId": "A13824", "ids": ["M1"], "position": 0}, "c0"],
|
||||
["error", {"type": "invalidResultReference"}, "c1"]
|
||||
],
|
||||
"sessionState": "changed"
|
||||
});
|
||||
let exchange = FakeExchange::new(vec![Ok(json_response(200, &answer))]);
|
||||
let mut response = block_on(client.call(&exchange, &request)).unwrap();
|
||||
assert!(client.is_session_stale(&response));
|
||||
assert_eq!(response.take_named(&query, "Email/query").unwrap()["ids"], json!(["M1"]));
|
||||
assert!(matches!(response.take(&get), Err(crate::response::CallFailure::Method(_))));
|
||||
let sent = &exchange.sent()[0];
|
||||
assert_eq!(sent.method(), Method::Post);
|
||||
assert_eq!(sent.url(), "https://jmap.example.com/api/");
|
||||
let body: Value = serde_json::from_slice(sent.body()).unwrap();
|
||||
assert_eq!(body["using"], json!(["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"]));
|
||||
}
|
||||
|
||||
// RFC 8620 §2 limits: maxCallsInRequest and maxSizeRequest are enforced before sending.
|
||||
#[test]
|
||||
fn enforces_limits_before_sending() {
|
||||
let client = client();
|
||||
let mut request = ApiRequest::new();
|
||||
for _ in 0..33 {
|
||||
request.add_call("Core/echo", Map::new()).unwrap();
|
||||
}
|
||||
assert_eq!(client.api_request(&request).unwrap_err(), Error::Build(BuildError::TooManyCalls { limit: 32 }));
|
||||
let mut request = ApiRequest::new();
|
||||
request.add_call("Core/echo", Map::from_iter([("blob".to_owned(), json!("x".repeat(10_000_001)))])).unwrap();
|
||||
assert_eq!(client.api_request(&request).unwrap_err(), Error::Build(BuildError::TooLarge { limit: 10_000_000 }));
|
||||
let mut request = ApiRequest::new();
|
||||
request.using("urn:ietf:params:jmap:calendars").unwrap();
|
||||
request.add_call("Core/echo", Map::new()).unwrap();
|
||||
assert_eq!(client.api_request(&request).unwrap_err(), Error::Build(BuildError::UnsupportedCapability));
|
||||
}
|
||||
|
||||
// Requirement 2.4 through the client: busy, credentials, transport overload, unusable answers.
|
||||
#[test]
|
||||
fn classifies_api_failures() {
|
||||
let client = client();
|
||||
let mut request = ApiRequest::new();
|
||||
request.add_call("Core/echo", Map::new()).unwrap();
|
||||
let cases: Vec<(Result<HttpResponse, TransportError>, Error)> = vec![
|
||||
(
|
||||
Ok(HttpResponse::new(429, Vec::new()).with_field("Retry-After", "7")),
|
||||
Error::Busy { retry_after: Some(RetryAfter::Delay(Duration::from_secs(7))) },
|
||||
),
|
||||
(Ok(HttpResponse::new(401, Vec::new())), Error::CredentialsRefused),
|
||||
(Err(TransportError::Overloaded), Error::Busy { retry_after: None }),
|
||||
(Err(TransportError::TimedOut), Error::Transport(TransportError::TimedOut)),
|
||||
(Ok(HttpResponse::new(200, b"<html>".to_vec()).with_field("Content-Type", "text/html")), Error::Unusable(Unusable::WrongContentType)),
|
||||
(Ok(HttpResponse::new(200, b"{\"x\":".to_vec()).with_field("Content-Type", "application/json")), Error::Unusable(Unusable::InvalidJson)),
|
||||
];
|
||||
for (answer, expected) in cases {
|
||||
let exchange = FakeExchange::new(vec![answer]);
|
||||
assert_eq!(block_on(client.call(&exchange, &request)).unwrap_err(), expected);
|
||||
}
|
||||
}
|
||||
|
||||
// Requirement 3.5 through the client: the user name, the account name and the password never show.
|
||||
#[test]
|
||||
fn errors_hide_secrets() {
|
||||
let client = client();
|
||||
let mut request = ApiRequest::new();
|
||||
request.add_call("Core/echo", Map::new()).unwrap();
|
||||
let problem = json!({"status": 500, "detail": "user john@example.com with password open-sesame-42 at https://jmap.example.com/.well-known/jmap"});
|
||||
let response = HttpResponse::new(500, problem.to_string().into_bytes()).with_field("Content-Type", "application/problem+json");
|
||||
let exchange = FakeExchange::new(vec![Ok(response)]);
|
||||
let error = block_on(client.call(&exchange, &request)).unwrap_err();
|
||||
let shown = error.to_string();
|
||||
assert!(!shown.contains("john") && !shown.contains("open-sesame") && !shown.contains("well-known"));
|
||||
}
|
||||
|
||||
// Requirement 2.3: the client's debug form shows no secret, no user name, no account id and no address.
|
||||
#[test]
|
||||
fn debug_shows_nothing_personal() {
|
||||
let client = client();
|
||||
let debug = format!("{client:?}");
|
||||
for hidden in ["john", "jane", "open-sesame", "A13824", "jmap.example.com", "75128aab4b1b", "am9obk"] {
|
||||
assert!(!debug.contains(hidden), "{hidden} shows in the client's debug form");
|
||||
}
|
||||
assert!(debug.starts_with("Client { scheme: \"Basic\""));
|
||||
}
|
||||
|
||||
// Requirement 3.5: a token in the session address's query is redacted on its own, and a short server-chosen
|
||||
// user name does not blank every message.
|
||||
#[test]
|
||||
fn session_address_parts_are_secrets() {
|
||||
let address = Address::parse("https://jmap.example.com/.well-known/jmap?apikey=SECRETKEY123").unwrap();
|
||||
let text = EXAMPLE.replace(r#""username": "john@example.com""#, r#""username": "jo""#);
|
||||
let session = Session::parse(text.as_bytes(), &address).unwrap();
|
||||
let client = Client::new(session, Credentials::bearer("token-0123456789").unwrap()).unwrap();
|
||||
assert_eq!(client.excerpt("invalid key SECRETKEY123 for jo").as_str(), "invalid key [redacted] for jo");
|
||||
assert_eq!(client.excerpt("bad token-0123456789").as_str(), "bad [redacted]");
|
||||
}
|
||||
|
||||
// RFC 8620 §6.2: a download is an authenticated GET of the expanded template; the body comes back as is.
|
||||
#[test]
|
||||
fn downloads() {
|
||||
let client = client();
|
||||
let exchange = FakeExchange::new(vec![Ok(HttpResponse::new(200, vec![0, 159, 146, 150]).with_field("Content-Type", "application/octet-stream"))]);
|
||||
let bytes = block_on(client.download(&exchange, &Id::new("B1").unwrap(), "application/octet-stream", "a.bin", None)).unwrap();
|
||||
assert_eq!(bytes, vec![0, 159, 146, 150]);
|
||||
assert_eq!(exchange.sent()[0].url(), "https://jmap.example.com/download/A13824/B1/a.bin?accept=application%2Foctet-stream");
|
||||
let exchange = FakeExchange::new(vec![Ok(HttpResponse::new(404, Vec::new()))]);
|
||||
assert_eq!(block_on(client.download(&exchange, &Id::new("B1").unwrap(), "", "a.bin", None)), Err(Error::NotFound));
|
||||
}
|
||||
|
||||
// Requirement 2.2: a limit the caller sets for one request reaches the exchange before the body is read,
|
||||
// and a body above it is refused, whether the exchange stops early or hands the whole body over.
|
||||
#[test]
|
||||
fn per_request_limits() {
|
||||
let client = client();
|
||||
let blob = Id::new("B1").unwrap();
|
||||
let body = || Ok(HttpResponse::new(200, vec![7; 5]));
|
||||
let careful = FakeExchange::capping(vec![body()]);
|
||||
assert_eq!(block_on(client.download(&careful, &blob, "", "a.bin", Some(4))), Err(Error::Unusable(Unusable::BodyTooLarge)));
|
||||
assert_eq!(careful.sent()[0].max_response_bytes(), 4);
|
||||
let careless = FakeExchange::new(vec![body()]);
|
||||
assert_eq!(block_on(client.download(&careless, &blob, "", "a.bin", Some(4))), Err(Error::Unusable(Unusable::BodyTooLarge)));
|
||||
let fits = FakeExchange::capping(vec![body()]);
|
||||
assert_eq!(block_on(client.download(&fits, &blob, "", "a.bin", Some(5))).unwrap(), vec![7; 5]);
|
||||
let unlimited = FakeExchange::new(vec![body()]);
|
||||
block_on(client.download(&unlimited, &blob, "", "a.bin", None)).unwrap();
|
||||
assert_eq!(unlimited.sent()[0].max_response_bytes(), crate::http::DOWNLOAD_RESPONSE_LIMIT);
|
||||
|
||||
let mut request = ApiRequest::new();
|
||||
request.add_call("Core/echo", Map::new()).unwrap();
|
||||
request.set_response_limit(64);
|
||||
assert_eq!(client.api_request(&request).unwrap().max_response_bytes(), 64);
|
||||
let answer = json!({"methodResponses": [["Core/echo", {"pad": "x".repeat(100)}, "c0"]], "sessionState": "s"});
|
||||
let exchange = FakeExchange::capping(vec![Ok(json_response(200, &answer))]);
|
||||
assert_eq!(block_on(client.call(&exchange, &request)).unwrap_err(), Error::Unusable(Unusable::BodyTooLarge));
|
||||
request.set_response_limit(usize::MAX);
|
||||
assert_eq!(client.api_request(&request).unwrap().max_response_bytes(), crate::http::API_RESPONSE_LIMIT);
|
||||
}
|
||||
|
||||
// Requirement 3.2: no mail account, no client.
|
||||
#[test]
|
||||
fn needs_a_mail_account() {
|
||||
let text = EXAMPLE.replace("urn:ietf:params:jmap:mail\": {", "urn:example:other\": {");
|
||||
let session = Session::parse(text.as_bytes(), &session_address()).unwrap();
|
||||
assert_eq!(session.mail_account(), None);
|
||||
assert_eq!(Client::new(session, credentials()).unwrap_err(), Error::NoMailAccount);
|
||||
}
|
||||
|
||||
// Requirement 2.6 context (RFC 8620 §3.1 calls made through the caller's exchange): the helpers' futures are
|
||||
// Send when the exchange's are, so native programs can spawn them.
|
||||
#[test]
|
||||
fn futures_are_send() {
|
||||
fn assert_send<T: Send>(_: &T) {}
|
||||
let client = client();
|
||||
let exchange = FakeExchange::new(Vec::new());
|
||||
let request = ApiRequest::new();
|
||||
let future = client.call(&exchange, &request);
|
||||
assert_send(&future);
|
||||
let address = session_address();
|
||||
let credentials = credentials();
|
||||
assert_send(&fetch_session(&exchange, &address, &credentials));
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
//! Credentials: HTTP Basic (RFC 7617) and Bearer tokens (RFC 6750).
|
||||
//!
|
||||
//! Credentials live in memory only. Their `Debug` form names the scheme and nothing else: no secret and no
|
||||
//! user name, which is usually an e-mail address and so personal data. The memory holding a secret is
|
||||
//! overwritten when it is dropped; this is best effort (the allocator, the operating system or an earlier
|
||||
//! copy made by the caller are out of reach without `unsafe`), not a guarantee.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::redact::Redactor;
|
||||
|
||||
/// The longest user name, password or token accepted, in octets.
|
||||
pub const MAX_CREDENTIAL_LEN: usize = 8 * 1024;
|
||||
|
||||
/// A piece of text that must not be shown, and that is overwritten when dropped.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub(crate) struct Secret(String);
|
||||
|
||||
impl Secret {
|
||||
pub(crate) fn new(text: String) -> Self {
|
||||
Self(text)
|
||||
}
|
||||
|
||||
pub(crate) fn expose(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Secret {
|
||||
fn drop(&mut self) {
|
||||
let mut bytes = std::mem::take(&mut self.0).into_bytes();
|
||||
bytes.iter_mut().for_each(|b| *b = 0);
|
||||
std::hint::black_box(&bytes);
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Secret {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("<redacted>")
|
||||
}
|
||||
}
|
||||
|
||||
/// Why credentials were refused.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum CredentialsError {
|
||||
/// The user name is empty.
|
||||
EmptyUser,
|
||||
/// The token is empty.
|
||||
EmptyToken,
|
||||
/// The user name holds a colon, which Basic cannot carry (RFC 7617 §2).
|
||||
ColonInUser,
|
||||
/// A control character in the user name or password (RFC 7617 §2).
|
||||
ControlCharacter,
|
||||
/// The token holds a character RFC 6750 §2.1 does not allow.
|
||||
InvalidToken,
|
||||
/// Longer than [`MAX_CREDENTIAL_LEN`].
|
||||
TooLong,
|
||||
}
|
||||
|
||||
impl fmt::Display for CredentialsError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::EmptyUser => "the user name is empty; enter the user name of the mail account",
|
||||
Self::EmptyToken => "the access token is empty; sign in again to obtain one",
|
||||
Self::ColonInUser => {
|
||||
"the user name contains a colon (:), which password sign-in cannot carry; use a token instead, or the user name without it"
|
||||
}
|
||||
Self::ControlCharacter => "the user name or password contains an invisible control character, often from a paste; type it again",
|
||||
Self::InvalidToken => {
|
||||
"the access token contains a space, a comma or another character a token cannot hold; copy it again without extra characters"
|
||||
}
|
||||
Self::TooLong => "the user name, password or token is longer than 8 KiB, more than this program accepts; check what was pasted",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CredentialsError {}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Kind {
|
||||
Basic { user: Secret, password: Secret },
|
||||
Bearer { token: Secret },
|
||||
}
|
||||
|
||||
/// Credentials for the JMAP server.
|
||||
#[derive(Clone)]
|
||||
pub struct Credentials {
|
||||
kind: Kind,
|
||||
authorization: Secret,
|
||||
}
|
||||
|
||||
const BASE64_ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
fn base64_char(index: u8) -> char {
|
||||
char::from(BASE64_ALPHABET.get(usize::from(index & 0x3F)).copied().unwrap_or(b'A'))
|
||||
}
|
||||
|
||||
/// Base64 with padding (RFC 4648 §4), written into `out`.
|
||||
fn base64_into(input: &[u8], out: &mut String) {
|
||||
for chunk in input.chunks(3) {
|
||||
let (a, b, c) = match *chunk {
|
||||
[a, b, c] => (a, Some(b), Some(c)),
|
||||
[a, b] => (a, Some(b), None),
|
||||
[a] => (a, None, None),
|
||||
_ => continue,
|
||||
};
|
||||
let b0 = b.unwrap_or(0);
|
||||
let c0 = c.unwrap_or(0);
|
||||
out.push(base64_char(a >> 2));
|
||||
out.push(base64_char(((a & 0x03) << 4) | (b0 >> 4)));
|
||||
out.push(if b.is_some() { base64_char(((b0 & 0x0F) << 2) | (c0 >> 6)) } else { '=' });
|
||||
out.push(if c.is_some() { base64_char(c0 & 0x3F) } else { '=' });
|
||||
}
|
||||
}
|
||||
|
||||
/// The part an octet may play in a bearer token (RFC 6750 §2.1, `b64token`).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum TokenOctet {
|
||||
/// A letter, a digit, or one of `-` `.` `_` `~` `+` `/`: allowed anywhere before the padding.
|
||||
Body,
|
||||
/// `=`: allowed only at the end, any number of times.
|
||||
Padding,
|
||||
/// Anything else, spaces, commas, quotes, controls and non-ASCII octets among them.
|
||||
Refused,
|
||||
}
|
||||
|
||||
/// Sorts one octet into its [`TokenOctet`] part.
|
||||
const fn token_octet(octet: u8) -> TokenOctet {
|
||||
match octet {
|
||||
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' => TokenOctet::Body,
|
||||
b'-' | b'.' | b'_' | b'~' | b'+' | b'/' => TokenOctet::Body,
|
||||
b'=' => TokenOctet::Padding,
|
||||
_ => TokenOctet::Refused,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads a bearer token in one pass: at least one body octet, then nothing but padding once padding began.
|
||||
fn is_bearer_token(token: &str) -> bool {
|
||||
let mut body_octets = 0usize;
|
||||
let mut padding_started = false;
|
||||
for octet in token.bytes() {
|
||||
match (token_octet(octet), padding_started) {
|
||||
(TokenOctet::Body, false) => body_octets = body_octets.saturating_add(1),
|
||||
(TokenOctet::Padding, _) => padding_started = true,
|
||||
(TokenOctet::Body, true) | (TokenOctet::Refused, _) => return false,
|
||||
}
|
||||
}
|
||||
body_octets > 0
|
||||
}
|
||||
|
||||
impl Credentials {
|
||||
/// Basic credentials (RFC 7617 §2): `user:password` in UTF-8, base64-encoded.
|
||||
///
|
||||
/// The user name may not be empty or hold a colon; neither part may hold a control character. The text is
|
||||
/// sent as given: Unicode normalisation (RFC 7617 §2.1 mentions NFC) is left to the caller.
|
||||
pub fn basic(user: &str, password: &str) -> Result<Self, CredentialsError> {
|
||||
if user.is_empty() {
|
||||
return Err(CredentialsError::EmptyUser);
|
||||
}
|
||||
if user.len() > MAX_CREDENTIAL_LEN || password.len() > MAX_CREDENTIAL_LEN {
|
||||
return Err(CredentialsError::TooLong);
|
||||
}
|
||||
if user.contains(':') {
|
||||
return Err(CredentialsError::ColonInUser);
|
||||
}
|
||||
if user.chars().chain(password.chars()).any(char::is_control) {
|
||||
return Err(CredentialsError::ControlCharacter);
|
||||
}
|
||||
let mut pair = Vec::with_capacity(user.len().saturating_add(password.len()).saturating_add(1));
|
||||
pair.extend_from_slice(user.as_bytes());
|
||||
pair.push(b':');
|
||||
pair.extend_from_slice(password.as_bytes());
|
||||
let encoded_len = pair.len().div_ceil(3).saturating_mul(4);
|
||||
let mut header = String::with_capacity(encoded_len.saturating_add(6));
|
||||
header.push_str("Basic ");
|
||||
base64_into(&pair, &mut header);
|
||||
pair.iter_mut().for_each(|b| *b = 0);
|
||||
std::hint::black_box(&pair);
|
||||
Ok(Self {
|
||||
kind: Kind::Basic { user: Secret::new(user.to_owned()), password: Secret::new(password.to_owned()) },
|
||||
authorization: Secret::new(header),
|
||||
})
|
||||
}
|
||||
|
||||
/// Bearer credentials (RFC 6750 §2.1). The token must match `b64token`.
|
||||
pub fn bearer(token: &str) -> Result<Self, CredentialsError> {
|
||||
if token.is_empty() {
|
||||
return Err(CredentialsError::EmptyToken);
|
||||
}
|
||||
if token.len() > MAX_CREDENTIAL_LEN {
|
||||
return Err(CredentialsError::TooLong);
|
||||
}
|
||||
if !is_bearer_token(token) {
|
||||
return Err(CredentialsError::InvalidToken);
|
||||
}
|
||||
let mut header = String::with_capacity(token.len().saturating_add(7));
|
||||
header.push_str("Bearer ");
|
||||
header.push_str(token);
|
||||
Ok(Self { kind: Kind::Bearer { token: Secret::new(token.to_owned()) }, authorization: Secret::new(header) })
|
||||
}
|
||||
|
||||
/// The authentication scheme's name: `Basic` or `Bearer`.
|
||||
pub fn scheme_name(&self) -> &'static str {
|
||||
match self.kind {
|
||||
Kind::Basic { .. } => "Basic",
|
||||
Kind::Bearer { .. } => "Bearer",
|
||||
}
|
||||
}
|
||||
|
||||
/// The value of the `Authorization` header field.
|
||||
pub(crate) fn authorization(&self) -> &Secret {
|
||||
&self.authorization
|
||||
}
|
||||
|
||||
/// Teaches a redactor every secret these credentials hold.
|
||||
pub fn register_secrets(&self, redactor: &mut Redactor) {
|
||||
match &self.kind {
|
||||
Kind::Basic { user, password } => {
|
||||
redactor.add_personal(user.expose());
|
||||
redactor.add(password.expose());
|
||||
}
|
||||
Kind::Bearer { token } => redactor.add(token.expose()),
|
||||
}
|
||||
let header = self.authorization.expose();
|
||||
let value = header.split_once(' ').map_or(header, |(_, value)| value);
|
||||
redactor.add(value);
|
||||
if let Kind::Basic { .. } = self.kind {
|
||||
// The base64 forms a server might echo: without padding, and in the URL-safe alphabet (RFC 4648 §5).
|
||||
let unpadded = value.trim_end_matches('=');
|
||||
redactor.add(unpadded);
|
||||
let url_safe = Secret::new(unpadded.replace('+', "-").replace('/', "_"));
|
||||
redactor.add(url_safe.expose());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Credentials {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Credentials({}, <redacted>)", self.scheme_name())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,107 @@
|
||||
use super::*;
|
||||
use crate::test_support::made_secret;
|
||||
|
||||
// RFC 7617 §2: the "Aladdin" / "open sesame" example.
|
||||
#[test]
|
||||
fn basic_example() {
|
||||
let credentials = Credentials::basic("Aladdin", &made_secret("open sesame")).unwrap();
|
||||
assert_eq!(credentials.authorization().expose(), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
|
||||
}
|
||||
|
||||
// RFC 7617 §2.1: the UTF-8 example ("test" / "123£").
|
||||
#[test]
|
||||
fn basic_utf8_example() {
|
||||
let credentials = Credentials::basic("test", &made_secret("123\u{A3}")).unwrap();
|
||||
assert_eq!(credentials.authorization().expose(), "Basic dGVzdDoxMjPCow==");
|
||||
}
|
||||
|
||||
// RFC 4648 §10: the base64 test vectors.
|
||||
#[test]
|
||||
fn base64_vectors() {
|
||||
for (input, expected) in
|
||||
[("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")]
|
||||
{
|
||||
let mut out = String::new();
|
||||
base64_into(input.as_bytes(), &mut out);
|
||||
assert_eq!(out, expected);
|
||||
}
|
||||
}
|
||||
|
||||
// RFC 7617 §2: no colon in the user name, no control characters anywhere.
|
||||
#[test]
|
||||
fn basic_refusals() {
|
||||
assert_eq!(Credentials::basic("", &made_secret("x")).unwrap_err(), CredentialsError::EmptyUser);
|
||||
assert_eq!(Credentials::basic("a:b", &made_secret("x")).unwrap_err(), CredentialsError::ColonInUser);
|
||||
assert_eq!(Credentials::basic("user", &made_secret("pa\nss")).unwrap_err(), CredentialsError::ControlCharacter);
|
||||
assert_eq!(Credentials::basic("us\u{7f}er", &made_secret("pass")).unwrap_err(), CredentialsError::ControlCharacter);
|
||||
assert!(Credentials::basic("user", &made_secret("with:colon")).is_ok());
|
||||
}
|
||||
|
||||
// RFC 6750 §2.1: the specification's example token, and an empty token.
|
||||
#[test]
|
||||
fn bearer_tokens() {
|
||||
let credentials = Credentials::bearer("mF_9.B5f-4.1JqM").unwrap();
|
||||
assert_eq!(credentials.authorization().expose(), "Bearer mF_9.B5f-4.1JqM");
|
||||
assert_eq!(Credentials::bearer("").unwrap_err(), CredentialsError::EmptyToken);
|
||||
}
|
||||
|
||||
// RFC 6750 §2.1, rule 1: the body is made of ALPHA, DIGIT and the six marks - . _ ~ + /.
|
||||
#[test]
|
||||
fn token_body_octets() {
|
||||
for token in ["AZaz09", "a-b", "a.b", "a_b", "a~b", "a+b", "a/b", "-._~+/"] {
|
||||
assert!(is_bearer_token(token), "{token}");
|
||||
}
|
||||
for octet in 0u8..=255 {
|
||||
let expected = octet.is_ascii_alphanumeric() || b"-._~+/".contains(&octet);
|
||||
assert_eq!(token_octet(octet) == TokenOctet::Body, expected, "{octet}");
|
||||
}
|
||||
}
|
||||
|
||||
// RFC 6750 §2.1, rule 2: at least one body octet ("1*").
|
||||
#[test]
|
||||
fn token_needs_a_body() {
|
||||
for token in ["", "=", "==="] {
|
||||
assert!(!is_bearer_token(token), "{token:?}");
|
||||
}
|
||||
assert_eq!(Credentials::bearer("==").unwrap_err(), CredentialsError::InvalidToken);
|
||||
}
|
||||
|
||||
// RFC 6750 §2.1, rule 3: "=" only at the end, any number of times ("*\"=\"").
|
||||
#[test]
|
||||
fn token_padding_only_at_the_end() {
|
||||
for token in ["abc=", "abc==", "abc===="] {
|
||||
assert!(is_bearer_token(token), "{token}");
|
||||
}
|
||||
for token in ["=abc", "a=b", "ab=c=", "ab==c"] {
|
||||
assert!(!is_bearer_token(token), "{token}");
|
||||
}
|
||||
}
|
||||
|
||||
// RFC 6750 §2.1, rule 4: nothing else, which also rules out header injection (RFC 9110 §5.5).
|
||||
#[test]
|
||||
fn token_refused_octets() {
|
||||
for token in ["a b", "a\r\nX-Injected: 1", "a,b", "a\"b", "a:b", "tok\u{e9}n", "a\tb", "a;b"] {
|
||||
assert!(!is_bearer_token(token), "{token:?}");
|
||||
assert_eq!(Credentials::bearer(token).unwrap_err(), CredentialsError::InvalidToken, "{token:?}");
|
||||
}
|
||||
}
|
||||
|
||||
// Requirement 2.3: no secret and no user name in the debug form.
|
||||
#[test]
|
||||
fn debug_shows_no_secret() {
|
||||
let basic = Credentials::basic("john@example.com", &made_secret("hunter2-long")).unwrap();
|
||||
let shown = format!("{basic:?}");
|
||||
assert_eq!(shown, "Credentials(Basic, <redacted>)");
|
||||
let bearer = Credentials::bearer("tok-123456").unwrap();
|
||||
assert!(!format!("{bearer:?}").contains("tok-123456"));
|
||||
}
|
||||
|
||||
// Requirement 3.5: the redactor learns the user name, the password and the encoded header value.
|
||||
#[test]
|
||||
fn registers_secrets() {
|
||||
let credentials = Credentials::basic("john@example.com", &made_secret("open sesame")).unwrap();
|
||||
let mut redactor = Redactor::new();
|
||||
credentials.register_secrets(&mut redactor);
|
||||
let excerpt = redactor.excerpt("john@example.com open sesame am9obkBleGFtcGxlLmNvbTpvcGVuIHNlc2FtZQ==");
|
||||
assert_eq!(excerpt.as_str(), "[redacted] [redacted] [redacted]");
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
//! Turning what a person types into the JMAP session address to fetch (requirement 1.6, RFC 8620 §2.2).
|
||||
//!
|
||||
//! A person may type `example.com`, `example.com/jmap/session`, `example.com:8443`, `https://example.com` or a
|
||||
//! full address. The result defaults to `https`, and to [`WELL_KNOWN_PATH`] when only a host was given. Plain
|
||||
//! `http` is kept only when it was typed and the host is loopback (requirement 1.2); it is never chosen by
|
||||
//! this function, and `http` typed for a remote host is refused rather than silently upgraded, so that the
|
||||
//! person sees that what they typed cannot be used.
|
||||
|
||||
use crate::address::{Address, AddressError, MAX_ADDRESS_LEN};
|
||||
|
||||
/// The well-known path of the JMAP session resource (RFC 8620 §2.2, registered by §9.2).
|
||||
pub const WELL_KNOWN_PATH: &str = "/.well-known/jmap";
|
||||
|
||||
/// Whether `text` begins with `scheme://`, the scheme being well formed (RFC 3986 §3.1).
|
||||
fn has_explicit_scheme(text: &str) -> bool {
|
||||
match text.split_once("://") {
|
||||
Some((scheme, _)) => {
|
||||
let mut bytes = scheme.bytes();
|
||||
matches!(bytes.next(), Some(b) if b.is_ascii_alphabetic()) && bytes.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `text` starts with `http:` or `https:` without the `//` that must follow (`https:example.com`).
|
||||
fn has_scheme_without_slashes(text: &str) -> bool {
|
||||
let lower = text.get(..6).unwrap_or(text).to_ascii_lowercase();
|
||||
(lower.starts_with("http:") || lower.starts_with("https:")) && !text.contains("://")
|
||||
}
|
||||
|
||||
/// Turns a session address typed by a person into the address to fetch.
|
||||
///
|
||||
/// Surrounding white space is ignored; anything else that is not a valid address is refused with the same
|
||||
/// errors as [`Address::parse`]. The result always satisfies the transport rule.
|
||||
pub fn session_address(input: &str) -> Result<Address, AddressError> {
|
||||
let trimmed = input.trim_matches(|c: char| c.is_ascii_whitespace());
|
||||
if trimmed.is_empty() {
|
||||
return Err(AddressError::Empty);
|
||||
}
|
||||
if trimmed.len() > MAX_ADDRESS_LEN {
|
||||
return Err(AddressError::TooLong);
|
||||
}
|
||||
if has_scheme_without_slashes(trimmed) {
|
||||
return Err(AddressError::MissingAuthority);
|
||||
}
|
||||
let address = if has_explicit_scheme(trimmed) {
|
||||
Address::parse(trimmed)?
|
||||
} else {
|
||||
let mut with_scheme = String::with_capacity(trimmed.len().saturating_add(8));
|
||||
with_scheme.push_str("https://");
|
||||
with_scheme.push_str(trimmed);
|
||||
Address::parse(&with_scheme)?
|
||||
};
|
||||
let address = if address.path() == "/" && address.query().is_none() { address.with_path(WELL_KNOWN_PATH)? } else { address };
|
||||
address.require_secure_transport()?;
|
||||
Ok(address)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,53 @@
|
||||
use super::*;
|
||||
|
||||
fn fetched(input: &str) -> String {
|
||||
session_address(input).unwrap().as_str().to_owned()
|
||||
}
|
||||
|
||||
// RFC 8620 §2.2: a bare host means https and the well-known path.
|
||||
#[test]
|
||||
fn bare_host_gets_well_known_path() {
|
||||
assert_eq!(fetched("example.com"), "https://example.com/.well-known/jmap");
|
||||
assert_eq!(fetched(" Example.COM \n"), "https://example.com/.well-known/jmap");
|
||||
assert_eq!(fetched("example.com/"), "https://example.com/.well-known/jmap");
|
||||
assert_eq!(fetched("example.com:8443"), "https://example.com:8443/.well-known/jmap");
|
||||
assert_eq!(fetched("https://example.com"), "https://example.com/.well-known/jmap");
|
||||
assert_eq!(fetched("[::1]:8080"), "https://[::1]:8080/.well-known/jmap");
|
||||
}
|
||||
|
||||
// Requirement 1.6: a host with a path keeps the path; https is the default scheme.
|
||||
#[test]
|
||||
fn host_with_path_keeps_it() {
|
||||
assert_eq!(fetched("example.com/jmap/session"), "https://example.com/jmap/session");
|
||||
assert_eq!(fetched("jmap.example.org:8443/api/session?x=1"), "https://jmap.example.org:8443/api/session?x=1");
|
||||
assert_eq!(fetched("HTTPS://example.net/jmap/"), "https://example.net/jmap/");
|
||||
assert_eq!(fetched("example.com/?a=b"), "https://example.com/?a=b");
|
||||
}
|
||||
|
||||
// Requirement 1.2 and 1.6: never a downgrade; http only as typed, and only to loopback.
|
||||
#[test]
|
||||
fn never_downgrades() {
|
||||
assert_eq!(fetched("localhost:8080"), "https://localhost:8080/.well-known/jmap");
|
||||
assert_eq!(fetched("http://localhost:8080"), "http://localhost:8080/.well-known/jmap");
|
||||
assert_eq!(fetched("http://127.0.0.1:8080/jmap"), "http://127.0.0.1:8080/jmap");
|
||||
assert_eq!(session_address("http://example.com"), Err(AddressError::InsecureTransport));
|
||||
assert_eq!(session_address("http://sub.localhost/"), Err(AddressError::InsecureTransport));
|
||||
}
|
||||
|
||||
// Requirement 1.1 applied to typed input: what is not an address is refused.
|
||||
#[test]
|
||||
fn refuses_junk() {
|
||||
assert_eq!(session_address(""), Err(AddressError::Empty));
|
||||
assert_eq!(session_address(" "), Err(AddressError::Empty));
|
||||
assert_eq!(session_address("https:example.com"), Err(AddressError::MissingAuthority));
|
||||
assert_eq!(session_address("http:localhost"), Err(AddressError::MissingAuthority));
|
||||
assert_eq!(session_address("ftp://example.com"), Err(AddressError::UnsupportedScheme));
|
||||
assert_eq!(session_address("user@example.com"), Err(AddressError::UserInfo));
|
||||
assert_eq!(session_address("exa mple.com"), Err(AddressError::ForbiddenCharacter));
|
||||
assert_eq!(session_address("example.com\\jmap"), Err(AddressError::Backslash));
|
||||
assert_eq!(session_address("bücher.example.com"), Err(AddressError::ForbiddenCharacter));
|
||||
assert_eq!(
|
||||
session_address("example.com/path?u=http://example.org"),
|
||||
Ok(Address::parse("https://example.com/path?u=http://example.org").unwrap())
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,412 @@
|
||||
//! The crate's error type, arranged by what the mail client can do about each case.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::address::{Address, AddressError};
|
||||
use crate::credentials::CredentialsError;
|
||||
use crate::http::TransportError;
|
||||
use crate::id::IdError;
|
||||
use crate::redact::Excerpt;
|
||||
use crate::retry::RetryAfter;
|
||||
use crate::template::TemplateError;
|
||||
|
||||
/// The longest limit name kept from a server.
|
||||
const MAX_LIMIT_NAME_LEN: usize = 64;
|
||||
|
||||
/// The name of a JMAP limit a server says was exceeded (`maxSizeRequest`, `maxCallsInRequest`, …), checked to
|
||||
/// be a short identifier (letters, digits and `_`).
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct LimitName(String);
|
||||
|
||||
impl LimitName {
|
||||
/// Checks a limit name received from a server.
|
||||
pub fn new(text: &str) -> Option<Self> {
|
||||
if text.is_empty() || text.len() > MAX_LIMIT_NAME_LEN || !text.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') {
|
||||
return None;
|
||||
}
|
||||
Some(Self(text.to_owned()))
|
||||
}
|
||||
|
||||
/// The name.
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Whether the limit is about concurrency (`maxConcurrentRequests`, `maxConcurrentUpload`) rather than size.
|
||||
pub fn is_concurrency(&self) -> bool {
|
||||
self.0.starts_with("maxConcurrent")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LimitName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for LimitName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "LimitName({})", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A request-level JMAP problem (RFC 8620 §3.6.1), the `urn:ietf:params:jmap:error:` prefix removed.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum RequestProblem {
|
||||
/// `unknownCapability`: the request used a capability the server does not support.
|
||||
UnknownCapability,
|
||||
/// `notJSON`: the request was not `application/json` or not I-JSON.
|
||||
NotJson,
|
||||
/// `notRequest`: the request did not have the shape of a Request object.
|
||||
NotRequest,
|
||||
/// `limit`: a request limit was exceeded.
|
||||
Limit,
|
||||
/// Another JMAP problem type, checked to be a short identifier.
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for RequestProblem {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::UnknownCapability => f.write_str("it uses a capability this server does not support"),
|
||||
Self::NotJson => f.write_str("the server could not read it as JSON"),
|
||||
Self::NotRequest => f.write_str("its JSON is not shaped like a JMAP request"),
|
||||
Self::Limit => f.write_str("it goes beyond one of the server's request limits"),
|
||||
Self::Other(name) if name.is_empty() => f.write_str("the server reported an unnamed JMAP problem"),
|
||||
Self::Other(name) => write!(f, "the server reported the JMAP problem {name}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a server's successful-looking answer cannot be used.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum Unusable {
|
||||
/// The content type is not the one expected (`application/json`).
|
||||
WrongContentType,
|
||||
/// The body is larger than the request's limit ([`HttpRequest::max_response_bytes`](crate::HttpRequest::max_response_bytes):
|
||||
/// the kind's, or the lower one the caller set), whether the crate or the transport found it.
|
||||
BodyTooLarge,
|
||||
/// The body is not valid JSON (RFC 8259), or nests too deeply.
|
||||
InvalidJson,
|
||||
/// The body holds more JSON values or objects than the crate accepts.
|
||||
TooComplex,
|
||||
/// The JSON does not have the expected shape; the text says which part, in the crate's own words.
|
||||
Shape(&'static str),
|
||||
/// An address the server gave cannot be used; the text names the field.
|
||||
Address(&'static str, AddressError),
|
||||
/// An address template the server gave cannot be used; the text names the field.
|
||||
Template(&'static str, TemplateError),
|
||||
}
|
||||
|
||||
impl fmt::Display for Unusable {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::WrongContentType => f.write_str("its content type is not application/json"),
|
||||
Self::BodyTooLarge => f.write_str("it is larger than the size limit set for this request"),
|
||||
Self::InvalidJson => f.write_str("it is not valid JSON, or nests more than 128 levels deep"),
|
||||
Self::TooComplex => f.write_str("it holds more JSON values or objects than this program reads at once"),
|
||||
Self::Shape(what) => write!(f, "{what}"),
|
||||
Self::Address(field, error) => write!(f, "its {field} cannot be used: {error}"),
|
||||
Self::Template(field, error) => write!(f, "its {field} cannot be used: {error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A mistake in a request being built, found before anything is sent.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum BuildError {
|
||||
/// A method name that is empty, too long, or not printable ASCII.
|
||||
InvalidMethodName,
|
||||
/// A capability identifier that is empty, too long, or not printable ASCII.
|
||||
InvalidCapability,
|
||||
/// A capability the session does not advertise.
|
||||
UnsupportedCapability,
|
||||
/// An argument given both plainly and as a result reference (RFC 8620 §3.7).
|
||||
DuplicateArgument,
|
||||
/// An argument name that is empty or starts with `#`.
|
||||
InvalidArgumentName,
|
||||
/// A result reference to a call that is not earlier in this request (RFC 8620 §3.7).
|
||||
UnknownCall,
|
||||
/// A result reference path that is not a JSON Pointer (RFC 6901, with JMAP's `*`).
|
||||
InvalidPath,
|
||||
/// More calls than the server accepts (`maxCallsInRequest`) or than the crate builds.
|
||||
TooManyCalls {
|
||||
/// The limit that applies.
|
||||
limit: u64,
|
||||
},
|
||||
/// A request body larger than the server accepts (`maxSizeRequest`).
|
||||
TooLarge {
|
||||
/// The limit that applies, in octets.
|
||||
limit: u64,
|
||||
},
|
||||
/// A request without any method call.
|
||||
NoCalls,
|
||||
/// The request could not be written as JSON.
|
||||
Serialisation,
|
||||
}
|
||||
|
||||
impl fmt::Display for BuildError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::InvalidMethodName => f.write_str("the method name is empty, longer than 255 characters, or holds a space or non-ASCII character"),
|
||||
Self::InvalidCapability => {
|
||||
f.write_str("the capability identifier is empty, longer than 256 characters, or holds a space or non-ASCII character")
|
||||
}
|
||||
Self::UnsupportedCapability => {
|
||||
f.write_str("the request uses a capability this server's session does not offer; remove it or check the server")
|
||||
}
|
||||
Self::DuplicateArgument => f.write_str("an argument is given both directly and as a result reference (#name); keep only one"),
|
||||
Self::InvalidArgumentName => {
|
||||
f.write_str("a result reference argument name is empty or starts with #; give the plain name, the # is added")
|
||||
}
|
||||
Self::UnknownCall => f.write_str("a result reference names a call that is not earlier in this same request"),
|
||||
Self::InvalidPath => {
|
||||
f.write_str("a result reference path is not a JSON pointer: it must be empty or start with /, and use ~ only as ~0 or ~1")
|
||||
}
|
||||
Self::TooManyCalls { limit } => {
|
||||
write!(f, "the request holds more than {limit} method calls, the server's limit; send it in several requests")
|
||||
}
|
||||
Self::TooLarge { limit } => {
|
||||
write!(f, "the request body is larger than the server's limit of {limit} octets; send it in several requests")
|
||||
}
|
||||
Self::NoCalls => f.write_str("the request holds no method call, so there is nothing to send"),
|
||||
Self::Serialisation => f.write_str("the request could not be written out as JSON"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything that can go wrong, arranged by what the mail client can do about it.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
/// The program's HTTP exchange failed before a response came (offline, timeout, TLS, …).
|
||||
Transport(TransportError),
|
||||
/// The server is busy or limiting the rate (429, 503, connection-level overload, a concurrency limit):
|
||||
/// try again later, after at least the delay it asked for when there is one.
|
||||
Busy {
|
||||
/// What the server asked for, if anything usable.
|
||||
retry_after: Option<RetryAfter>,
|
||||
},
|
||||
/// The credentials were refused (401, or 403 when the problem details say so): ask for them again.
|
||||
CredentialsRefused,
|
||||
/// The request was too large for the server (413, or a JMAP `limit` problem): split it.
|
||||
TooLarge {
|
||||
/// The limit the server named, if any.
|
||||
limit: Option<LimitName>,
|
||||
},
|
||||
/// The server refuses access to this resource (403).
|
||||
Forbidden {
|
||||
/// What the server said, cleaned.
|
||||
detail: Option<Excerpt>,
|
||||
},
|
||||
/// Nothing at that address (404, 410).
|
||||
NotFound,
|
||||
/// The server rejected the request as malformed or unsupported (400 and other client errors, or a JMAP
|
||||
/// request-level problem).
|
||||
Refused {
|
||||
/// The JMAP problem, if one was recognised.
|
||||
problem: Option<RequestProblem>,
|
||||
/// What the server said, cleaned.
|
||||
detail: Option<Excerpt>,
|
||||
},
|
||||
/// The server redirected; the crate follows nothing. The target has passed the transport rule and the
|
||||
/// loopback check; whether to follow (and whether to send the credentials to another origin) is the
|
||||
/// caller's decision.
|
||||
Redirected {
|
||||
/// Where the server points.
|
||||
target: Address,
|
||||
/// Whether the target has the same origin as the request.
|
||||
same_origin: bool,
|
||||
},
|
||||
/// The server redirected to an address that may not be used (plain http, loopback from a remote host,
|
||||
/// malformed).
|
||||
RedirectRefused(AddressError),
|
||||
/// The server answered something that cannot be used.
|
||||
Unusable(Unusable),
|
||||
/// Any other HTTP status.
|
||||
Http {
|
||||
/// The status code.
|
||||
status: u16,
|
||||
/// What the server said, cleaned.
|
||||
detail: Option<Excerpt>,
|
||||
},
|
||||
/// An address given to the crate is not usable.
|
||||
Address(AddressError),
|
||||
/// An address template could not be expanded.
|
||||
Template(TemplateError),
|
||||
/// Credentials given to the crate are not usable.
|
||||
Credentials(CredentialsError),
|
||||
/// An id given to the crate is not valid.
|
||||
Id(IdError),
|
||||
/// A request being built is wrong.
|
||||
Build(BuildError),
|
||||
/// The session offers no account for mail (RFC 8621 `urn:ietf:params:jmap:mail`).
|
||||
NoMailAccount,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// The delay before trying again, for [`Error::Busy`].
|
||||
pub fn retry_after(&self) -> Option<&RetryAfter> {
|
||||
match self {
|
||||
Self::Busy { retry_after } => retry_after.as_ref(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn with_detail(f: &mut fmt::Formatter<'_>, text: &str, detail: Option<&Excerpt>) -> fmt::Result {
|
||||
f.write_str(text)?;
|
||||
if let Some(detail) = detail {
|
||||
write!(f, " (server said: \"{detail}\")")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Transport(error) => write!(f, "the request did not complete: {error}"),
|
||||
Self::Busy {
|
||||
retry_after: Some(RetryAfter::Delay(delay)),
|
||||
} => write!(
|
||||
f,
|
||||
"the server is busy and asks to wait {} s before the next attempt",
|
||||
delay.as_secs()
|
||||
),
|
||||
Self::Busy {
|
||||
retry_after: Some(RetryAfter::At(_)),
|
||||
} => f.write_str("the server is busy and named a time before which not to try again"),
|
||||
Self::Busy { retry_after: None } => {
|
||||
f.write_str("the server is busy or limiting requests; wait a little before trying again")
|
||||
}
|
||||
Self::CredentialsRefused => f.write_str(
|
||||
"the server rejected the user name and password (or the access token) for this account; check them, or sign in again if they were changed or have expired",
|
||||
),
|
||||
Self::TooLarge { limit: Some(limit) } => write!(
|
||||
f,
|
||||
"the request went beyond the server's limit {limit}; send it in smaller parts"
|
||||
),
|
||||
Self::TooLarge { limit: None } => {
|
||||
f.write_str("the request was larger than the server accepts; send it in smaller parts")
|
||||
}
|
||||
Self::Forbidden { detail } => with_detail(
|
||||
f,
|
||||
"the server does not let this account reach that resource",
|
||||
detail.as_ref(),
|
||||
),
|
||||
Self::NotFound => f.write_str(
|
||||
"the server has nothing at that address; it may have been moved or deleted, so refresh and try again",
|
||||
),
|
||||
Self::Refused {
|
||||
problem: Some(problem),
|
||||
detail,
|
||||
} => with_detail(
|
||||
f,
|
||||
&format!("the server rejected the request: {problem}"),
|
||||
detail.as_ref(),
|
||||
),
|
||||
Self::Refused {
|
||||
problem: None,
|
||||
detail,
|
||||
} => with_detail(f, "the server rejected the request as invalid", detail.as_ref()),
|
||||
Self::Redirected {
|
||||
target,
|
||||
same_origin: true,
|
||||
} => write!(
|
||||
f,
|
||||
"the server moved this resource to another path on {}; it was not followed automatically",
|
||||
target.origin()
|
||||
),
|
||||
Self::Redirected { target, .. } => write!(
|
||||
f,
|
||||
"the server sent this request on to {}; confirm before following, since the credentials would go there too",
|
||||
target.origin()
|
||||
),
|
||||
Self::RedirectRefused(error) => write!(
|
||||
f,
|
||||
"the server redirected to an address that cannot be used: {error}"
|
||||
),
|
||||
Self::Unusable(why) => write!(f, "the server's answer cannot be used: {why}"),
|
||||
Self::Http { status, detail } => with_detail(
|
||||
f,
|
||||
&format!("the server answered with HTTP status {status}, which this program cannot act on"),
|
||||
detail.as_ref(),
|
||||
),
|
||||
Self::Address(error) => fmt::Display::fmt(error, f),
|
||||
Self::Template(error) => fmt::Display::fmt(error, f),
|
||||
Self::Credentials(error) => fmt::Display::fmt(error, f),
|
||||
Self::Id(error) => fmt::Display::fmt(error, f),
|
||||
Self::Build(error) => fmt::Display::fmt(error, f),
|
||||
Self::NoMailAccount => f.write_str(
|
||||
"the server offers no mail account to these credentials; check that mail is enabled for this account",
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::Transport(error) => Some(error),
|
||||
Self::RedirectRefused(error) | Self::Address(error) => Some(error),
|
||||
Self::Template(error) => Some(error),
|
||||
Self::Credentials(error) => Some(error),
|
||||
Self::Id(error) => Some(error),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TransportError> for Error {
|
||||
/// A connection-level overload is the server being busy; a body cut at the request's limit is the same
|
||||
/// failure as one the crate refuses after reading; anything else stays a transport failure.
|
||||
fn from(error: TransportError) -> Self {
|
||||
match error {
|
||||
TransportError::Overloaded => Self::Busy { retry_after: None },
|
||||
TransportError::BodyTooLarge => Self::Unusable(Unusable::BodyTooLarge),
|
||||
other => Self::Transport(other),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AddressError> for Error {
|
||||
fn from(error: AddressError) -> Self {
|
||||
Self::Address(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TemplateError> for Error {
|
||||
fn from(error: TemplateError) -> Self {
|
||||
Self::Template(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CredentialsError> for Error {
|
||||
fn from(error: CredentialsError) -> Self {
|
||||
Self::Credentials(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IdError> for Error {
|
||||
fn from(error: IdError) -> Self {
|
||||
Self::Id(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BuildError> for Error {
|
||||
fn from(error: BuildError) -> Self {
|
||||
Self::Build(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Unusable> for Error {
|
||||
fn from(why: Unusable) -> Self {
|
||||
Self::Unusable(why)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,39 @@
|
||||
use super::*;
|
||||
use std::time::Duration;
|
||||
|
||||
// RFC 8620 §3.6.1: the limit property names the limit; only short identifiers are kept.
|
||||
#[test]
|
||||
fn limit_names() {
|
||||
assert_eq!(LimitName::new("maxSizeRequest").unwrap().as_str(), "maxSizeRequest");
|
||||
assert!(LimitName::new("maxConcurrentRequests").unwrap().is_concurrency());
|
||||
assert!(!LimitName::new("maxCallsInRequest").unwrap().is_concurrency());
|
||||
for bad in ["", "max size", "x\ny", &"a".repeat(65), "<script>"] {
|
||||
assert_eq!(LimitName::new(bad), None, "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
// Requirement 2.4: messages are the crate's own and one line.
|
||||
#[test]
|
||||
fn messages() {
|
||||
let busy = Error::Busy { retry_after: Some(RetryAfter::Delay(Duration::from_secs(30))) };
|
||||
assert_eq!(busy.to_string(), "the server is busy and asks to wait 30 s before the next attempt");
|
||||
assert_eq!(Error::from(TransportError::Overloaded), Error::Busy { retry_after: None });
|
||||
assert_eq!(Error::from(TransportError::TimedOut), Error::Transport(TransportError::TimedOut));
|
||||
assert_eq!(Error::from(TransportError::BodyTooLarge), Error::Unusable(Unusable::BodyTooLarge));
|
||||
// The cause and the remedy, not a stock phrase.
|
||||
let refused = Error::CredentialsRefused.to_string();
|
||||
assert!(refused.contains("user name and password") && refused.contains("sign in again"));
|
||||
let long = Error::Address(AddressError::TooLong).to_string();
|
||||
assert!(long.contains("8 KiB") && long.contains("pasted"));
|
||||
for error in [
|
||||
Error::CredentialsRefused,
|
||||
Error::NotFound,
|
||||
Error::NoMailAccount,
|
||||
Error::Address(AddressError::UserInfo),
|
||||
Error::Transport(TransportError::Other),
|
||||
Error::Build(BuildError::TooManyCalls { limit: 16 }),
|
||||
] {
|
||||
let text = error.to_string();
|
||||
assert!(!text.contains('\n') && text.len() > 40, "{text}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
//! Hosts of `http` and `https` addresses: RFC 3986 §3.2.2, read with the WHATWG URL Standard's host rules
|
||||
//! (§3.5) where RFC 3986 leaves room.
|
||||
//!
|
||||
//! What is accepted is deliberately the part on which both documents and DNS agree:
|
||||
//!
|
||||
//! * a registered name, percent-decoded, in ASCII, lower-cased, made of letters, digits, `-`, `_` and dots,
|
||||
//! with labels of 1 to 63 octets and at most 253 octets in all (one final dot allowed);
|
||||
//! * an IPv4 address written exactly as `std::net::Ipv4Addr` writes it: four decimal numbers without leading
|
||||
//! zeros (RFC 3986 `IPv4address`);
|
||||
//! * an IPv6 address in brackets, read by `std::net::Ipv6Addr` (RFC 3986 `IPv6address`), without zone
|
||||
//! identifiers, without `IPvFuture`, and with an embedded IPv4 address only as its last 32 bits in plain
|
||||
//! dotted form.
|
||||
//!
|
||||
//! A name whose last label is numeric (the WHATWG "ends in a number" test) but which is not a plain dotted
|
||||
//! IPv4 address, such as `127.1`, `0x7f.0.0.1` or `2130706433`, is refused: the WHATWG parser reads those
|
||||
//! as IPv4 addresses while RFC 3986 reads them as names, and RFC 3986 §7.4 warns about exactly this.
|
||||
|
||||
use std::fmt;
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
use crate::address::AddressError;
|
||||
use crate::percent;
|
||||
|
||||
/// The longest registered name, in octets, without a final dot (the DNS limit; RFC 3986 §3.2.2 notes that
|
||||
/// a registered name meant for DNS follows DNS syntax).
|
||||
const MAX_NAME_LEN: usize = 253;
|
||||
/// The longest label of a registered name, in octets.
|
||||
const MAX_LABEL_LEN: usize = 63;
|
||||
/// The longest text accepted between the brackets of an IPv6 literal: eight groups of four digits and seven
|
||||
/// colons are 39 characters; a trailing dotted IPv4 address adds at most 6 more.
|
||||
const MAX_IPV6_TEXT_LEN: usize = 45;
|
||||
|
||||
/// How close to this computer a host is, as far as its text tells (no name is resolved).
|
||||
///
|
||||
/// The order matters: [`HostScope::Public`] < [`HostScope::PrivateNetwork`] < [`HostScope::ThisComputer`].
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum HostScope {
|
||||
/// Any registered name other than the `localhost` family, and every other IP address.
|
||||
Public,
|
||||
/// A private, shared or link-local network: IPv4 `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`,
|
||||
/// `100.64.0.0/10` (RFC 6598), `169.254.0.0/16`; IPv6 `fc00::/7`, `fe80::/10`, `fec0::/10`.
|
||||
PrivateNetwork,
|
||||
/// This computer: `localhost` and names under it (with or without a final dot), `127.0.0.0/8`,
|
||||
/// `0.0.0.0/8`, `::1`, `::`, and those IPv4 addresses mapped or embedded in IPv6 (`::ffff:0:0/96`,
|
||||
/// `::/96`, `64:ff9b::/96`).
|
||||
ThisComputer,
|
||||
}
|
||||
|
||||
fn ipv4_scope(address: Ipv4Addr) -> HostScope {
|
||||
let [first, second, _, _] = address.octets();
|
||||
if address.is_loopback() || first == 0 {
|
||||
HostScope::ThisComputer
|
||||
} else if address.is_private() || address.is_link_local() || (first == 100 && (64..=127).contains(&second)) {
|
||||
HostScope::PrivateNetwork
|
||||
} else {
|
||||
HostScope::Public
|
||||
}
|
||||
}
|
||||
|
||||
fn ipv6_scope(address: Ipv6Addr) -> HostScope {
|
||||
// `to_ipv4` covers ::, ::1, the IPv4-compatible ::/96 and the IPv4-mapped ::ffff:0:0/96 forms; :: and ::1
|
||||
// come out as 0.0.0.0 and 0.0.0.1, both on this computer.
|
||||
if let Some(embedded) = address.to_ipv4() {
|
||||
return ipv4_scope(embedded);
|
||||
}
|
||||
let [s0, s1, s2, s3, s4, s5, s6, s7] = address.segments();
|
||||
if [s0, s1, s2, s3, s4, s5] == [0x0064, 0xFF9B, 0, 0, 0, 0] {
|
||||
// The NAT64 well-known prefix (RFC 6052 §2.1) carries an IPv4 address in its last 32 bits.
|
||||
let [a, b] = s6.to_be_bytes();
|
||||
let [c, d] = s7.to_be_bytes();
|
||||
return ipv4_scope(Ipv4Addr::new(a, b, c, d));
|
||||
}
|
||||
// Unique local fc00::/7, link-local fe80::/10 and the deprecated site-local fec0::/10.
|
||||
if address.is_unique_local() || address.is_unicast_link_local() || s0 & 0xFFC0 == 0xFEC0 { HostScope::PrivateNetwork } else { HostScope::Public }
|
||||
}
|
||||
|
||||
/// The host of an address.
|
||||
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Host {
|
||||
/// A registered name, lower-case ASCII (`example.com`).
|
||||
Name(String),
|
||||
/// An IPv4 address.
|
||||
Ipv4(Ipv4Addr),
|
||||
/// An IPv6 address.
|
||||
Ipv6(Ipv6Addr),
|
||||
}
|
||||
|
||||
impl Host {
|
||||
/// Reads the host part of an authority (RFC 3986 §3.2.2), brackets included for IPv6.
|
||||
pub(crate) fn parse(input: &str) -> Result<Self, AddressError> {
|
||||
if input.is_empty() {
|
||||
return Err(AddressError::EmptyHost);
|
||||
}
|
||||
if let Some(inner) = input.strip_prefix('[') {
|
||||
let inner = inner.strip_suffix(']').ok_or(AddressError::InvalidIpv6)?;
|
||||
return ipv6_between_brackets(inner).map(Self::Ipv6);
|
||||
}
|
||||
let decoded = percent::decode_bytes(input).map_err(|_| AddressError::InvalidHost)?;
|
||||
if decoded.iter().any(|b| !b.is_ascii()) {
|
||||
return Err(AddressError::InternationalHost);
|
||||
}
|
||||
let name = String::from_utf8(decoded).map_err(|_| AddressError::InvalidHost)?.to_ascii_lowercase();
|
||||
if name.is_empty() {
|
||||
return Err(AddressError::EmptyHost);
|
||||
}
|
||||
if !name.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.')) {
|
||||
return Err(AddressError::InvalidHost);
|
||||
}
|
||||
if last_label_is_numeric(&name) {
|
||||
return plain_dotted_ipv4(&name).map(Self::Ipv4).ok_or(AddressError::AmbiguousNumericHost);
|
||||
}
|
||||
check_dns_lengths(&name)?;
|
||||
Ok(Self::Name(name))
|
||||
}
|
||||
|
||||
/// Whether this is a loopback host in the sense of the transport rule: the name `localhost` exactly, an
|
||||
/// IPv4 address in `127.0.0.0/8`, or the IPv6 address `::1`.
|
||||
///
|
||||
/// `sub.localhost`, `localhost.` and IPv4-mapped IPv6 addresses are not loopback here.
|
||||
pub fn is_loopback(&self) -> bool {
|
||||
match self {
|
||||
Self::Name(name) => name == "localhost",
|
||||
Self::Ipv4(address) => address.is_loopback(),
|
||||
Self::Ipv6(address) => address.is_loopback(),
|
||||
}
|
||||
}
|
||||
|
||||
/// How close to this computer the host is ([`HostScope`]). Broader than [`Host::is_loopback`]: it serves
|
||||
/// to stop a remote server from sending the program towards this computer or its network, not to allow
|
||||
/// plain `http`.
|
||||
pub fn scope(&self) -> HostScope {
|
||||
match self {
|
||||
Self::Name(name) => {
|
||||
let name = name.strip_suffix('.').unwrap_or(name);
|
||||
if name == "localhost" || name.ends_with(".localhost") { HostScope::ThisComputer } else { HostScope::Public }
|
||||
}
|
||||
Self::Ipv4(address) => ipv4_scope(*address),
|
||||
Self::Ipv6(address) => ipv6_scope(*address),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Host {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Name(name) => f.write_str(name),
|
||||
Self::Ipv4(address) => write!(f, "{address}"),
|
||||
Self::Ipv6(address) => {
|
||||
f.write_str("[")?;
|
||||
write_ipv6_in_hex(f, address)?;
|
||||
f.write_str("]")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Host {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a name would be read as a number by the WHATWG host parser ("ends in a number", §3.5): its last
|
||||
/// label, a final dot aside, is all decimal digits or `0x` followed by hex digits.
|
||||
fn last_label_is_numeric(name: &str) -> bool {
|
||||
let trimmed = name.strip_suffix('.').unwrap_or(name);
|
||||
let last = trimmed.rsplit('.').next().unwrap_or("");
|
||||
if last.is_empty() {
|
||||
return false;
|
||||
}
|
||||
if last.bytes().all(|b| b.is_ascii_digit()) {
|
||||
return true;
|
||||
}
|
||||
match last.strip_prefix("0x").or_else(|| last.strip_prefix("0X")) {
|
||||
Some(hex) => hex.bytes().all(|b| b.is_ascii_hexdigit()),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// An IPv4 host, accepted only when the text is exactly the address's own dotted form: `std::net` reads it,
|
||||
/// and writing it back must give the same text. That refuses leading zeros, a final dot, and every shortened,
|
||||
/// octal or hexadecimal spelling (RFC 3986 §3.2.2 `dec-octet`, §7.4).
|
||||
fn plain_dotted_ipv4(text: &str) -> Option<Ipv4Addr> {
|
||||
let address: Ipv4Addr = text.parse().ok()?;
|
||||
(address.to_string() == text).then_some(address)
|
||||
}
|
||||
|
||||
/// An IPv6 literal, the text between the brackets, read by `std::net` once the forms this crate refuses are
|
||||
/// set aside: zone identifiers (RFC 6874's `%25…`, which the WHATWG parser refuses too), `IPvFuture`
|
||||
/// (`v1.…`), and an embedded IPv4 part that is not the last 32 bits in plain dotted form.
|
||||
fn ipv6_between_brackets(text: &str) -> Result<Ipv6Addr, AddressError> {
|
||||
if text.is_empty() || text.len() > MAX_IPV6_TEXT_LEN || text.contains('%') || text.starts_with(['v', 'V']) {
|
||||
return Err(AddressError::InvalidIpv6);
|
||||
}
|
||||
if text.contains('.') {
|
||||
let (head, dotted) = text.rsplit_once(':').ok_or(AddressError::InvalidIpv6)?;
|
||||
if head.contains('.') || plain_dotted_ipv4(dotted).is_none() {
|
||||
return Err(AddressError::InvalidIpv6);
|
||||
}
|
||||
}
|
||||
text.parse::<Ipv6Addr>().map_err(|_| AddressError::InvalidIpv6)
|
||||
}
|
||||
|
||||
fn check_dns_lengths(name: &str) -> Result<(), AddressError> {
|
||||
let body = name.strip_suffix('.').unwrap_or(name);
|
||||
if body.is_empty() || body.len() > MAX_NAME_LEN {
|
||||
return Err(AddressError::InvalidHost);
|
||||
}
|
||||
if body.split('.').any(|label| label.is_empty() || label.len() > MAX_LABEL_LEN) {
|
||||
return Err(AddressError::InvalidHost);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes an IPv6 address in the hexadecimal form of RFC 5952 §4, as the WHATWG IPv6 serializer (§3.6) does:
|
||||
/// lower-case hex without leading zeros, the first longest run of two or more zero groups written as `::`,
|
||||
/// and no dotted IPv4 tail (`std`'s `Display` writes IPv4-mapped addresses with one).
|
||||
fn write_ipv6_in_hex(f: &mut fmt::Formatter<'_>, address: &Ipv6Addr) -> fmt::Result {
|
||||
let groups = address.segments();
|
||||
let mut best: Option<(usize, usize)> = None;
|
||||
let mut run_start: Option<usize> = None;
|
||||
for (index, group) in groups.iter().enumerate() {
|
||||
if *group == 0 {
|
||||
let start = *run_start.get_or_insert(index);
|
||||
let len = index.saturating_sub(start).saturating_add(1);
|
||||
if len >= 2 && best.is_none_or(|(_, best_len)| len > best_len) {
|
||||
best = Some((start, len));
|
||||
}
|
||||
} else {
|
||||
run_start = None;
|
||||
}
|
||||
}
|
||||
let mut index = 0usize;
|
||||
let mut need_colon = false;
|
||||
while let Some(group) = groups.get(index) {
|
||||
if let Some((start, len)) = best
|
||||
&& index == start
|
||||
{
|
||||
f.write_str("::")?;
|
||||
index = index.saturating_add(len);
|
||||
need_colon = false;
|
||||
continue;
|
||||
}
|
||||
if need_colon {
|
||||
f.write_str(":")?;
|
||||
}
|
||||
write!(f, "{group:x}")?;
|
||||
need_colon = true;
|
||||
index = index.saturating_add(1);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,135 @@
|
||||
use super::*;
|
||||
|
||||
fn shown(input: &str) -> String {
|
||||
Host::parse(input).unwrap().to_string()
|
||||
}
|
||||
|
||||
// RFC 3986 §3.2.2 and §6.2.2.1: registered names are case-insensitive and normalised to lower case.
|
||||
#[test]
|
||||
fn names_are_lower_cased() {
|
||||
assert_eq!(shown("Example.COM"), "example.com");
|
||||
assert_eq!(shown("mail.example.org."), "mail.example.org.");
|
||||
assert_eq!(shown("under_score.example.net"), "under_score.example.net");
|
||||
}
|
||||
|
||||
// RFC 3986 §3.2.2 (pct-encoded in reg-name) and WHATWG URL §3.5: the host is percent-decoded first.
|
||||
#[test]
|
||||
fn names_are_percent_decoded() {
|
||||
assert_eq!(shown("ex%61mple.com"), "example.com");
|
||||
assert_eq!(Host::parse("ex%20ample.com"), Err(AddressError::InvalidHost));
|
||||
assert_eq!(Host::parse("ex%2"), Err(AddressError::InvalidHost));
|
||||
}
|
||||
|
||||
// WHATWG URL §3.2 (forbidden domain code points) and §3.3: what DNS cannot carry is refused, and a name
|
||||
// outside ASCII needs IDNA processing, which this crate does not do.
|
||||
#[test]
|
||||
fn refuses_bad_names() {
|
||||
for bad in ["exa mple.com", "exa*mple.com", "example..com", ".example.com", "a%2fb"] {
|
||||
assert!(Host::parse(bad).is_err(), "{bad}");
|
||||
}
|
||||
assert_eq!(Host::parse("bücher.example.com"), Err(AddressError::InternationalHost));
|
||||
assert_eq!(Host::parse("b%C3%BCcher.example.com"), Err(AddressError::InternationalHost));
|
||||
let long_label = format!("{}.example.com", "a".repeat(64));
|
||||
assert_eq!(Host::parse(&long_label), Err(AddressError::InvalidHost));
|
||||
}
|
||||
|
||||
// RFC 3986 §3.2.2 IPv4address, and WHATWG URL §3.5 "ends in a number": only the plain dotted form passes.
|
||||
#[test]
|
||||
fn ipv4_only_in_plain_form() {
|
||||
assert_eq!(Host::parse("127.0.0.1").unwrap(), Host::Ipv4(Ipv4Addr::LOCALHOST));
|
||||
assert_eq!(shown("192.0.2.255"), "192.0.2.255");
|
||||
for ambiguous in ["127.1", "0x7f.0.0.1", "2130706433", "127.0.0.01", "127.0.0.1.", "256.0.0.1", "1.2.3.4.5"] {
|
||||
assert_eq!(Host::parse(ambiguous), Err(AddressError::AmbiguousNumericHost), "{ambiguous}");
|
||||
}
|
||||
assert!(Host::parse("example.123").is_err());
|
||||
assert_eq!(shown("v4.example.com"), "v4.example.com");
|
||||
}
|
||||
|
||||
// RFC 3986 §3.2.2 IP-literal read by std::net, WHATWG URL §3.5 (no zones) and §3.6 (hex output, RFC 5952 §4).
|
||||
#[test]
|
||||
fn ipv6_forms() {
|
||||
assert_eq!(shown("[::1]"), "[::1]");
|
||||
assert_eq!(shown("[0:0:0:0:0:0:0:1]"), "[::1]");
|
||||
assert_eq!(shown("[2001:DB8::0:1]"), "[2001:db8::1]");
|
||||
assert_eq!(shown("[2001:db8:0:0:1:0:0:1]"), "[2001:db8::1:0:0:1]");
|
||||
assert_eq!(shown("[::ffff:192.0.2.1]"), "[::ffff:c000:201]");
|
||||
assert_eq!(shown("[::]"), "[::]");
|
||||
assert_eq!(shown("[2001:db8:3:4:5:6:7:8]"), "[2001:db8:3:4:5:6:7:8]");
|
||||
assert_eq!(shown("[1:2:3:4:5:6:192.0.2.1]"), "[1:2:3:4:5:6:c000:201]");
|
||||
assert_eq!(shown("[2001:db8:0:1:1:1:1:1]"), "[2001:db8:0:1:1:1:1:1]");
|
||||
assert_eq!(shown("[1:0:0:2:0:0:0:3]"), "[1:0:0:2::3]");
|
||||
for bad in [
|
||||
"[]",
|
||||
"[:::1]",
|
||||
"[1::2::3]",
|
||||
"[12345::]",
|
||||
"[1:2:3:4:5:6:7]",
|
||||
"[1:2:3:4:5:6:7:8:9]",
|
||||
"[::1%25eth0]",
|
||||
"[v1.abc]",
|
||||
"[::1",
|
||||
"[1:2:3:4:5:6:7::8:9]",
|
||||
"[::1.2.3]",
|
||||
"[1.2.3.4::]",
|
||||
"[::01.2.3.4]",
|
||||
"[::1.2.3.4:5]",
|
||||
"[1.2.3.4]",
|
||||
"[fe80::1%eth0]",
|
||||
"[V1.abc]",
|
||||
"[::ffff:1.2.3.4.]",
|
||||
] {
|
||||
assert_eq!(Host::parse(bad), Err(AddressError::InvalidIpv6), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
// RFC 6890 (special-purpose registries), RFC 1918, RFC 6598, RFC 4193, RFC 4291 §2.5.5–§2.5.6, RFC 6052
|
||||
// §2.1, RFC 6761 §6.3 (localhost names): how close to this computer a host is.
|
||||
#[test]
|
||||
fn scopes() {
|
||||
let scope = |text: &str| Host::parse(text).unwrap().scope();
|
||||
for host in [
|
||||
"localhost",
|
||||
"localhost.",
|
||||
"x.localhost",
|
||||
"x.localhost.",
|
||||
"127.0.0.1",
|
||||
"0.0.0.0",
|
||||
"[::1]",
|
||||
"[::]",
|
||||
"[::ffff:127.0.0.1]",
|
||||
"[::ffff:7f00:1]",
|
||||
"[::127.0.0.1]",
|
||||
"[64:ff9b::127.0.0.1]",
|
||||
"[::ffff:0.0.0.0]",
|
||||
] {
|
||||
assert_eq!(scope(host), HostScope::ThisComputer, "{host}");
|
||||
}
|
||||
for host in [
|
||||
"10.1.2.3",
|
||||
"172.16.0.1",
|
||||
"172.31.255.255",
|
||||
"192.168.1.1",
|
||||
"169.254.0.1",
|
||||
"100.64.0.1",
|
||||
"[fe80::1]",
|
||||
"[fd00::1]",
|
||||
"[fc00::1]",
|
||||
"[::ffff:192.168.1.1]",
|
||||
] {
|
||||
assert_eq!(scope(host), HostScope::PrivateNetwork, "{host}");
|
||||
}
|
||||
for host in ["example.com", "192.0.2.1", "172.32.0.1", "100.128.0.1", "[2001:db8::1]", "localhost.example.com"] {
|
||||
assert_eq!(scope(host), HostScope::Public, "{host}");
|
||||
}
|
||||
}
|
||||
|
||||
// Requirement 1.2: the loopback hosts are `localhost` exactly, 127.0.0.0/8 and ::1.
|
||||
#[test]
|
||||
fn loopback_hosts() {
|
||||
for host in ["localhost", "LOCALHOST", "127.0.0.1", "127.255.0.9", "[::1]", "[0::1]"] {
|
||||
assert!(Host::parse(host).unwrap().is_loopback(), "{host}");
|
||||
}
|
||||
for host in ["sub.localhost", "localhost.", "localhost.example.com", "192.0.2.1", "[::ffff:127.0.0.1]", "[2001:db8::2]"] {
|
||||
assert!(!Host::parse(host).unwrap().is_loopback(), "{host}");
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user