From c339a9e4715bb0856603de7af897efcb43430f59 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:16:52 +0000 Subject: [PATCH] Share TIFF decode preflight fields --- crates/image-codecs/src/tiff_probe.rs | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs index 1a542e412..05bd1e4db 100644 --- a/crates/image-codecs/src/tiff_probe.rs +++ b/crates/image-codecs/src/tiff_probe.rs @@ -19,6 +19,42 @@ pub enum Error { Malformed(String), } +/// First-directory values needed to preflight decoding. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct DecodeInfo { + /// Stored width. + pub width: u32, + /// Stored height. + pub height: u32, + /// First sample's bit depth, defaulting to eight. + pub bit_depth: u8, + /// Raw orientation tag, if present. + pub orientation: Option, +} + +/// Parse the first directory and enforce the pixel limit before decoding. +/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder. +pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { + 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 }); + } + Ok(DecodeInfo { + width: info.width, + height: info.height, + bit_depth: info.bits_per_sample.first().copied().unwrap_or(8) as u8, + orientation: ifd.u16(craft_tiff::tags::ORIENTATION), + }) +} + /// 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> {