From da7007385609e66337f9ba37c5aca8ca6215b2f2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:23:46 +0000 Subject: [PATCH] Expose tolerant JPEG marker inspection --- crates/metadata/src/container.rs | 146 +++++++++++++++++++++++++++++-- 1 file changed, 137 insertions(+), 9 deletions(-) diff --git a/crates/metadata/src/container.rs b/crates/metadata/src/container.rs index 842020683..c972867e1 100644 --- a/crates/metadata/src/container.rs +++ b/crates/metadata/src/container.rs @@ -84,22 +84,83 @@ pub fn embedded(bytes: &[u8]) -> Embedded { const XMP_SIG: &[u8] = b"http://ns.adobe.com/xap/1.0/\0"; const XMP_EXT_SIG: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; +/// JPEG frame, marker and metadata details collected before the first scan. +#[derive(Debug, Default)] +pub struct JpegInspection { + /// Stored frame width. + pub width: u32, + /// Stored frame height. + pub height: u32, + /// Number of frame components. + pub components: u8, + /// Sample precision. + pub precision: u8, + /// Start-of-frame marker byte. + pub sof: u8, + /// Components in the first scan. + pub first_scan_components: u8, + /// Number of APP1 segments. + pub app1: u32, + /// First EXIF TIFF payload. + pub exif: Option>, + /// Offset of the EXIF TIFF payload. + pub exif_offset: usize, + /// First standard XMP packet. + pub xmp: Option, + /// Reassembled ICC profile. + pub icc: Option>, + /// ICC chunks were malformed or inconsistent. + pub icc_invalid: bool, + /// Adobe APP14 transform byte. + pub adobe_transform: Option, + /// MPF TIFF offset and payload. + pub mpf: Option<(usize, Vec)>, + /// ISO gain-map APP2 payload following its identifier. + pub iso_gainmap: Option>, + valid: bool, +} + +impl JpegInspection { + /// Whether metadata identifies this image as a gain map. + pub fn is_gain_map(&self) -> bool { + self.iso_gainmap.as_ref().is_some_and(|p| p.len() > 4) || self.xmp.as_deref().is_some_and(|x| x.contains("hdrgm:GainMapMax")) + } +} + +/// Inspect markers before the first scan, tolerating garbage between segments and truncated +/// final payloads. Returns `None` for a missing SOI or an invalid segment length. +pub fn inspect_jpeg(b: &[u8]) -> Option { + if !b.starts_with(&[0xff, 0xd8]) { + return None; + } + let (embedded, _, mut inspection) = jpeg_segments_inner(b, false, true); + if !inspection.valid { + return None; + } + inspection.exif = embedded.exif; + inspection.xmp = embedded.xmp.map(|x| x.trim_end_matches('\0').to_string()); + inspection.icc = embedded.icc; + Some(inspection) +} + /// Walk JPEG markers up to SOS and collect APP1 Exif / XMP, APP2 ICC and APP13 IPTC. pub fn jpeg_segments(b: &[u8]) -> Embedded { - jpeg_segments_inner(b, false).0 + jpeg_segments_inner(b, false, false).0 } /// Walk JPEG metadata and return every ordinary XMP APP1 packet in segment order, along with the /// usual [`Embedded`] projection (which retains only the first ordinary packet). pub fn jpeg_segments_with_all_xmp(b: &[u8]) -> (Embedded, Vec) { - jpeg_segments_inner(b, true) + let (embedded, packets, _) = jpeg_segments_inner(b, true, false); + (embedded, packets) } -fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec) { +fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embedded, Vec, JpegInspection) { let mut out = Embedded::default(); let mut xmp_packets = Vec::new(); + let mut inspection = JpegInspection { valid: true, ..Default::default() }; if !b.starts_with(&[0xff, 0xd8]) { - return (out, xmp_packets); + return (out, xmp_packets, inspection); } let mut icc_parts: [Option<&[u8]>; 255] = [None; 255]; let mut icc_total = None; @@ -110,6 +171,10 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec (Embedded, Vec (Embedded, Vec= 6 && inspection.components == 0 { + inspection.precision = payload[0]; + inspection.sof = marker; + inspection.height = u32::from(u16::from_be_bytes([payload[1], payload[2]])); + inspection.width = u32::from(u16::from_be_bytes([payload[3], payload[4]])); + inspection.components = payload[5]; + } + if marker == 0xe1 { + inspection.app1 = inspection.app1.saturating_add(1); + } + if marker == 0xe1 && payload.starts_with(b"Exif\0\0") && out.exif.is_none() { + inspection.exif_offset = start + 6; + } + if marker == 0xe2 && payload.starts_with(b"MPF\0") && inspection.mpf.is_none() { + inspection.mpf = Some((start + 4, payload[4..].to_vec())); + } + if marker == 0xe2 && inspection.iso_gainmap.is_none() { + if let Some(data) = payload.strip_prefix(b"urn:iso:std:iso:ts:21496:-1\0") { + inspection.iso_gainmap = Some(data.to_vec()); + } + } + if marker == 0xee && payload.starts_with(b"Adobe") && payload.len() >= 12 { + inspection.adobe_transform = Some(payload[11]); + } + } match marker { - 0xe1 if payload.starts_with(b"Exif\0") && out.exif.is_none() => out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()), + 0xe1 if payload.starts_with(if inspect { b"Exif\0\0" } else { b"Exif\0" }) && out.exif.is_none() => { + out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()) + } 0xe1 if payload.starts_with(XMP_SIG) && out.xmp.is_none() => { let packet = String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned(); if collect_all_xmp { @@ -145,7 +242,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec { xmp_packets.push(String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned()); } - 0xe1 if payload.starts_with(XMP_EXT_SIG) => { + 0xe1 if !inspect && payload.starts_with(XMP_EXT_SIG) => { // GUID (32) + full length (4) + offset (4) + data let p = &payload[XMP_EXT_SIG.len()..]; if p.len() >= 40 { @@ -184,7 +281,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec out.iptc = photoshop_iptc(&payload[14..]), + 0xed if !inspect && payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]), _ => {} } i = end; @@ -214,8 +311,11 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec, marker: u8, data: &[u8]) { + out.extend_from_slice(&[0xff, marker]); + out.extend_from_slice(&((data.len() as u16) + 2).to_be_bytes()); + out.extend_from_slice(data); + } + #[test] + fn tolerant_walk_keeps_offsets_frame_and_icc_chunks() { + let mut b = vec![0xff, 0xd8, 0x44]; + segment(&mut b, 0xe1, b"Exif\0\0II"); + segment(&mut b, 0xe2, b"ICC_PROFILE\0\x02\x02B"); + segment(&mut b, 0xe2, b"ICC_PROFILE\0\x01\x02A"); + segment(&mut b, 0xc0, &[8, 0, 2, 0, 3, 3]); + b.extend_from_slice(&[0xff, 0xda, 0, 3, 3]); + let info = inspect_jpeg(&b).unwrap(); + assert_eq!((info.width, info.height, info.components, info.first_scan_components), (3, 2, 3, 3)); + assert_eq!(info.app1, 1); + assert_eq!(&b[info.exif_offset..info.exif_offset + 2], b"II"); + assert_eq!(info.icc.as_deref(), Some(&b"AB"[..])); + assert!(!info.icc_invalid); + // The existing strict API still stops at the garbage byte. + assert!(jpeg_segments(&b).exif.is_none()); + assert!(inspect_jpeg(&[0xff, 0xd8, 0xff, 0xe1, 0, 1]).is_none()); + } +}