From 7bb9165c6bc46652db41ef5e19054a2bb017435f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 21:58:44 -0500 Subject: [PATCH] Delegate TIFF RAW classification to shared codecs (#4311) --- crates/codecs/src/exif.rs | 1 + crates/codecs/src/sniff.rs | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/codecs/src/exif.rs b/crates/codecs/src/exif.rs index b3e45fef68..79f4d52753 100644 --- a/crates/codecs/src/exif.rs +++ b/crates/codecs/src/exif.rs @@ -57,6 +57,7 @@ impl<'a> Tiff<'a> { Some((v, next)) } /// First value of a SHORT/LONG entry. + #[cfg(test)] pub fn uint(&self, e: &Entry) -> Option { match e.typ { 3 => self.u16_at(e.value_pos).map(u32::from), diff --git a/crates/codecs/src/sniff.rs b/crates/codecs/src/sniff.rs index 5bc5c40278..181f271038 100644 --- a/crates/codecs/src/sniff.rs +++ b/crates/codecs/src/sniff.rs @@ -1,5 +1,6 @@ //! Format detection from leading bytes. +#[cfg(test)] use crate::exif::Tiff; use serde::{Deserialize, Serialize}; @@ -125,7 +126,7 @@ pub fn sniff(b: &[u8]) -> Option { return Some(Format::RawTiffLike); } if b.starts_with(b"II*\0") || b.starts_with(b"MM\0*") { - return Some(if tiff_is_raw(b) { Format::RawTiffLike } else { Format::Tiff }); + return Some(if craft_image_codecs::legacy_tiff_raw_hint(b) { Format::RawTiffLike } else { Format::Tiff }); } if b.starts_with(b"II+\0") || b.starts_with(b"MM\0+") { return Some(Format::Tiff); @@ -167,6 +168,7 @@ fn sniff_bmff(b: &[u8]) -> Option { /// Heuristic camera-raw detection for classic TIFF: DNGVersion, the CR2 signature, CFA/LinearRaw /// photometric or raw-only compressions in IFD0 or its SubIFDs, or a camera Make with 10–16-bit data /// and no ordinary photometric. +#[cfg(test)] fn tiff_is_raw(b: &[u8]) -> bool { if b.len() >= 10 && &b[8..10] == b"CR" { return true; @@ -221,6 +223,28 @@ fn tiff_is_raw(b: &[u8]) -> bool { mod tests { use super::*; + #[test] + fn raw_tiff_classifier_matches_legacy_on_mutations() { + let fixtures: &[&[u8]] = &[ + b"II*\0\x08\0\0\0\x01\0\x12\xc6\x01\0\x04\0\0\0\x01\x04\0\0\0\0\0\0", + b"II*\0\x08\0\0\0\x01\0\x03\x01\x03\0\x01\0\0\0\x09\x80\0\0\0\0\0\0", + b"II*\0\x10\0\0\0CR\x02\0", + ]; + for fixture in fixtures { + for end in 0..=fixture.len() { + let bytes = &fixture[..end]; + assert_eq!(craft_image_codecs::legacy_tiff_raw_hint(bytes), tiff_is_raw(bytes), "truncation {end}"); + for i in 0..bytes.len() { + for value in [0, 255] { + let mut changed = bytes.to_vec(); + changed[i] = value; + assert_eq!(craft_image_codecs::legacy_tiff_raw_hint(&changed), tiff_is_raw(&changed), "byte {i} value {value} length {end}"); + } + } + } + } + } + #[test] fn magic_numbers() { assert_eq!(sniff(b"\xFF\xD8\xFF\xE0\0\x10JFIF"), Some(Format::Jpeg));