LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
#2284: document the HEIF reader's test helpers and the shared box readers
tecari060843a0c668a
diff --git a/crates/meta/src/cr3.rs b/crates/meta/src/cr3.rsindex 77d8dec4a..a95b8baad 100644--- a/crates/meta/src/cr3.rs+++ b/crates/meta/src/cr3.rs@@ -407,12 +407,15 @@ fn sample_entry(bytes: &[u8], e: &BoxRef, budget: &mut usize) -> Cr3TrackKind {Cr3TrackKind::Raw { width, height, cmp1, iad1 }}+/// A big-endian `u16` at `at`, or `None` when the buffer is shorter than that.pub(crate) fn be16(b: &[u8], at: usize) -> Option<u16> {b.get(at..at.checked_add(2)?).map(|s| u16::from_be_bytes([s[0], s[1]]))}+/// A big-endian `u32` at `at`, or `None` when the buffer is shorter than that.pub(crate) fn be32(b: &[u8], at: usize) -> Option<u32> {b.get(at..at.checked_add(4)?).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]]))}+/// A big-endian `u64` at `at`, or `None` when the buffer is shorter than that.pub(crate) fn be64(b: &[u8], at: usize) -> Option<u64> {let s = b.get(at..at.checked_add(8)?)?;let mut a = [0u8; 8];diff --git a/crates/meta/src/heif.rs b/crates/meta/src/heif.rsindex 98f3f0a3e..1d768f7a6 100644--- a/crates/meta/src/heif.rs+++ b/crates/meta/src/heif.rs@@ -200,6 +200,7 @@ mod tests {use crate::embedded;use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};+ /// One box: its own size, its four-letter kind, then its payload.fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec<u8> {let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec();v.extend_from_slice(kind);@@ -207,6 +208,7 @@ mod tests {v}+ /// A FullBox: a box whose payload begins with a version byte and three flag bytes.fn full_box(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec<u8> {bx(kind, &[&[version, 0, 0, 0][..], payload].concat())}@@ -289,6 +291,7 @@ mod tests {[&(skip as u32).to_be_bytes()[..], &vec![0u8; skip], tiff].concat()}+ /// One `Exif` item holding `tiff`, the only item in the file.fn exif_only(tiff: &[u8]) -> Vec<Item<'static>> {vec![Item { kind: b"Exif", content_type: "", data: exif_item(tiff, 0) }]}@@ -308,6 +311,7 @@ mod tests {assert_eq!(m.iso, Some(64));}+ /// The XMP packet is the `mime` item that declares `application/rdf+xml`, not any other one.#[test]fn the_xmp_packet_is_the_rdf_mime_item() {let xmp = crate::write_xmp(&crate::Metadata { title: Some("Cusco".into()), ..Default::default() }, None);@@ -334,6 +338,8 @@ mod tests {}}+ /// The `Exif` item's leading offset may point past padding, not only straight at the TIFF+ /// header, and the reader has to follow it.#[test]fn the_exif_items_tiff_header_offset_is_honoured() {let tiff = iphone_exif("2026:10:18 13:00:00", "-05:00");@@ -341,6 +347,7 @@ mod tests {assert_eq!(embedded(&heic(&items, 1, false)).exif.as_deref(), Some(tiff.as_slice()));}+ /// Which `ftyp` brands count as a still image in this container, major or compatible.#[test]fn brands() {for brand in ["heic", "heix", "mif1", "avif", "hevc", "msf1"] {diff --git a/crates/meta/tests/robust.rs b/crates/meta/tests/robust.rsindex e1e1efcbf..7a3179d57 100644--- a/crates/meta/tests/robust.rs+++ b/crates/meta/tests/robust.rs@@ -4,6 +4,8 @@ use proptest::prelude::*;proptest! {#![proptest_config(ProptestConfig { cases: 1500, .. ProptestConfig::default() })]+ /// Random bytes behind each container's magic: reading them yields whatever can be read and+ /// never panics.#[test]fn random_containers_never_panic(mut data in proptest::collection::vec(any::<u8>(), 0..600), kind in 0usize..7) {let heads: [&[u8]; 7] = [b"\xff\xd8\xff\xe1", b"\x89PNG\r\n\x1a\n", b"RIFF\0\0\0\0WEBP", b"II*\0", b"MM\0*", b"\0\0\0\x18ftypcrx ", b"\0\0\0\x18ftypheic"];