From ee44af6a45c2d12c4e63058db08a9f417002636d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:14:00 +0000 Subject: [PATCH] Share first-directory TIFF inspection --- crates/image-codecs/src/lib.rs | 1 + crates/image-codecs/src/tiff_probe.rs | 68 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 crates/image-codecs/src/tiff_probe.rs diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..b94eb6606 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -30,6 +30,7 @@ mod options; pub mod orientation; pub mod resolution; pub mod source_settings; +pub mod tiff_probe; pub mod web; pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs new file mode 100644 index 000000000..1a542e412 --- /dev/null +++ b/crates/image-codecs/src/tiff_probe.rs @@ -0,0 +1,68 @@ +//! First-directory TIFF inspection without decoding pixels. + +/// A header inspection failure, distinguishing caller limits from unsupported decoder layouts. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// Dimensions exceed the caller's pixel limit. + #[error("TIFF dimensions {width}x{height} exceed the pixel limit")] + TooLarge { + /// Stored width. + width: u32, + /// Stored height. + height: u32, + }, + /// The shared TIFF decoder does not support this layout. + #[error("unsupported TIFF: {0}")] + Unsupported(&'static str), + /// The file's header, directory or image-data bounds are malformed. + #[error("malformed TIFF: {0}")] + Malformed(String), +} + +/// Stored dimensions and orientation of the first directory, after checking the layouts and +/// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed. +pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> { + let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; + if !complete { + return Err(Error::Malformed("truncated TIFF".into())); + } + let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; + let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; + if info.width == 0 || info.height == 0 { + return Err(Error::Malformed("zero dimension".into())); + } + if u64::from(info.width) * u64::from(info.height) > max_pixels { + return Err(Error::TooLarge { width: info.width, height: info.height }); + } + let bits = info.bits_per_sample.first().copied().unwrap_or(1); + if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 { + return Err(Error::Unsupported("planar TIFF with samples narrower than a byte")); + } + if !(matches!(info.photometric, 0 | 1 | 2 | 3 | 5) || info.photometric == 6 && info.compression == 7) { + return Err(Error::Unsupported("colour type")); + } + if !matches!(info.compression, 1 | 4 | 5 | 7 | 8 | 32773 | 32946) { + return Err(Error::Unsupported("compression method")); + } + let len = bytes.len() as u64; + if info.offsets.is_empty() + || info.offsets.iter().zip(&info.byte_counts).any(|(&o, &c)| c > 0 && (o >= len || (info.compression == 1 && o.saturating_add(c) > len))) + { + return Err(Error::Malformed("truncated: image data past the end of the file".into())); + } + let orientation = ifd.u16(craft_tiff::tags::ORIENTATION).filter(|o| (1..=8).contains(o)).unwrap_or(1); + Ok((info.width, info.height, orientation)) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn reads_first_directory_and_enforces_limits_and_bounds() { + let image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); + let bytes = crate::encode(&image, crate::Format::Tiff, &crate::EncodeOptions::default()).unwrap(); + assert_eq!(header(&bytes, 6).unwrap(), (2, 3, 1)); + assert!(matches!(header(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 }))); + assert!(header(&bytes[..8], 6).is_err()); + } +}