From da7007385609e66337f9ba37c5aca8ca6215b2f2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:23:46 +0000 Subject: [PATCH 01/17] 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()); + } +} From e275d533cfacf21817200694e48aa7054a2f5a2c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:24:17 +0000 Subject: [PATCH 02/17] Export JPEG inspection API --- crates/metadata/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index ff2c530fa..bf6700455 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -37,7 +37,7 @@ pub mod tags; mod xmp; mod xmp_merge; -pub use container::{Embedded, embedded, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks}; +pub use container::{Embedded, JpegInspection, embedded, inspect_jpeg, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks}; pub use datetime::{DatePrecision, DateTime}; pub use exif::{from_tiff, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif}; pub use focus::FocusPoint; From e58b7c5a362a05411e98f1f94e1c995f2a888910 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:25:34 +0000 Subject: [PATCH 03/17] Factor JPEG inspection fields from shared walker --- crates/metadata/src/container.rs | 53 +++++++++++++++++--------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/crates/metadata/src/container.rs b/crates/metadata/src/container.rs index c972867e1..4f90cffc2 100644 --- a/crates/metadata/src/container.rs +++ b/crates/metadata/src/container.rs @@ -203,30 +203,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embed let end = declared_end.min(b.len()); let Some(payload) = b.get(start..end) else { break }; if inspect { - if (0xc0..=0xcf).contains(&marker) && !matches!(marker, 0xc4 | 0xc8 | 0xcc) && payload.len() >= 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]); - } + inspect_marker(&mut inspection, marker, payload, start, out.exif.is_none()); } match marker { 0xe1 if payload.starts_with(if inspect { b"Exif\0\0" } else { b"Exif\0" }) && out.exif.is_none() => { @@ -318,6 +295,34 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embed (out, xmp_packets, inspection) } +fn inspect_marker(info: &mut JpegInspection, marker: u8, payload: &[u8], start: usize, needs_exif: bool) { + if (0xc0..=0xcf).contains(&marker) && !matches!(marker, 0xc4 | 0xc8 | 0xcc) && payload.len() >= 6 && info.components == 0 { + info.precision = payload[0]; + info.sof = marker; + info.height = u32::from(u16::from_be_bytes([payload[1], payload[2]])); + info.width = u32::from(u16::from_be_bytes([payload[3], payload[4]])); + info.components = payload[5]; + } + if marker == 0xe1 { + info.app1 = info.app1.saturating_add(1); + } + if marker == 0xe1 && payload.starts_with(b"Exif\0\0") && needs_exif { + info.exif_offset = start + 6; + } + if marker == 0xe2 && payload.starts_with(b"MPF\0") && info.mpf.is_none() { + info.mpf = Some((start + 4, payload[4..].to_vec())); + } + if marker == 0xe2 + && info.iso_gainmap.is_none() + && let Some(data) = payload.strip_prefix(b"urn:iso:std:iso:ts:21496:-1\0") + { + info.iso_gainmap = Some(data.to_vec()); + } + if marker == 0xee && payload.starts_with(b"Adobe") && payload.len() >= 12 { + info.adobe_transform = Some(payload[11]); + } +} + /// Put the Extended XMP of JPEG `b` into `out`: when the standard packet links one, only that GUID's /// chunks count, checked strictly ([`extended_xmp`]); otherwise every chunk, offset-sorted. fn attach_extension(out: &mut Embedded, b: &[u8], mut ext: Vec<(u32, Vec)>, ext_headers: &[(&[u8], u32)]) { From 091baf624fcaa67ad740c2ea9d6df3216b4bddc7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:26:21 +0000 Subject: [PATCH 04/17] Share ICC chunk collection between JPEG inspection modes --- crates/metadata/src/container.rs | 50 +++++++++++++++----------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/crates/metadata/src/container.rs b/crates/metadata/src/container.rs index 4f90cffc2..d8eb261db 100644 --- a/crates/metadata/src/container.rs +++ b/crates/metadata/src/container.rs @@ -230,33 +230,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embed } } 0xe2 if payload.starts_with(b"ICC_PROFILE\0") => { - if !complete || payload.len() < 14 { - icc_invalid = true; - } else if let Some(header) = payload.get(12..14) { - let (Some(&sequence), Some(&total)) = (header.first(), header.get(1)) else { - icc_invalid = true; - i = end; - continue; - }; - if sequence == 0 || total == 0 || sequence > total || icc_total.is_some_and(|expected| expected != total) { - icc_invalid = true; - } else { - icc_total = Some(total); - if let Some(slot) = icc_parts.get_mut(usize::from(sequence - 1)) { - if slot.is_some() { - icc_invalid = true; - } else if let Some(data) = payload.get(14..) { - *slot = Some(data); - } else { - icc_invalid = true; - } - } else { - icc_invalid = true; - } - } - } else { - icc_invalid = true; - } + collect_icc_part(payload, complete, &mut icc_parts, &mut icc_total, &mut icc_invalid); } 0xed if !inspect && payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]), _ => {} @@ -295,6 +269,28 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embed (out, xmp_packets, inspection) } +fn collect_icc_part<'a>(payload: &'a [u8], complete: bool, parts: &mut [Option<&'a [u8]>; 255], total: &mut Option, invalid: &mut bool) { + if !complete || payload.len() < 14 { + *invalid = true; + return; + } + let (sequence, count) = (payload[12], payload[13]); + if sequence == 0 || count == 0 || sequence > count || total.is_some_and(|expected| expected != count) { + *invalid = true; + return; + } + *total = Some(count); + let Some(slot) = parts.get_mut(usize::from(sequence - 1)) else { + *invalid = true; + return; + }; + if slot.is_some() { + *invalid = true; + } else { + *slot = payload.get(14..); + } +} + fn inspect_marker(info: &mut JpegInspection, marker: u8, payload: &[u8], start: usize, needs_exif: bool) { if (0xc0..=0xcf).contains(&marker) && !matches!(marker, 0xc4 | 0xc8 | 0xcc) && payload.len() >= 6 && info.components == 0 { info.precision = payload[0]; From c97bc006df861d97d5da6f0db387693ab5592a65 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:28:10 +0000 Subject: [PATCH 05/17] Cover JPEG MPF and gain-map inspection --- crates/metadata/src/container.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/metadata/src/container.rs b/crates/metadata/src/container.rs index d8eb261db..65a47ba05 100644 --- a/crates/metadata/src/container.rs +++ b/crates/metadata/src/container.rs @@ -975,6 +975,20 @@ mod jpeg_inspection_tests { out.extend_from_slice(&((data.len() as u16) + 2).to_be_bytes()); out.extend_from_slice(data); } + #[test] + fn reports_mpf_gainmap_and_adobe_transform() { + let mut b = vec![0xff, 0xd8]; + segment(&mut b, 0xe2, b"MPF\0IIabc"); + segment(&mut b, 0xe2, b"urn:iso:std:iso:ts:21496:-1\0abcde"); + segment(&mut b, 0xee, b"Adobe\0\0\0\0\0\0\x02"); + b.extend_from_slice(&[0xff, 0xd9]); + let info = inspect_jpeg(&b).unwrap(); + let (offset, data) = info.mpf.unwrap(); + assert_eq!(&b[offset..offset + data.len()], data); + assert_eq!(info.iso_gainmap.as_deref(), Some(&b"abcde"[..])); + assert_eq!(info.adobe_transform, Some(2)); + } + #[test] fn tolerant_walk_keeps_offsets_frame_and_icc_chunks() { let mut b = vec![0xff, 0xd8, 0x44]; From 476599c792851f9f1e00159d6f365d9b6a44d39e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:29:27 +0000 Subject: [PATCH 06/17] Expose MPF index inspection for located payloads --- crates/metadata/src/mpf.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/metadata/src/mpf.rs b/crates/metadata/src/mpf.rs index 602c8f90d..5c7f5a7cd 100644 --- a/crates/metadata/src/mpf.rs +++ b/crates/metadata/src/mpf.rs @@ -113,7 +113,14 @@ pub fn images(jpeg: &[u8]) -> Vec { }; // offsets count from the byte-order field, which follows the signature let Some(base) = segs.iter().find(|s| jpeg.get(s.1..s.2) == Some(payload)).map(|s| s.1 + SIGNATURE.len()) else { return Vec::new() }; - let Some(tiff) = payload.get(SIGNATURE.len()..).and_then(|t| Tiff::parse(t).ok()) else { return Vec::new() }; + let Some(index) = payload.get(SIGNATURE.len()..) else { return Vec::new() }; + images_from_index(index, base) +} + +/// Read an already located MPF TIFF index. `base` is the index's offset in the containing file. +/// Entries are bounded to [`MAX_ENTRIES`] and returned without reading image payloads. +pub fn images_from_index(index: &[u8], base: usize) -> Vec { + let Some(tiff) = Tiff::parse(index).ok() else { return Vec::new() }; let Some(list) = tiff.ifds.first().and_then(|i| i.bytes(MP_ENTRY)) else { return Vec::new() }; let u32_at = |rec: &[u8], at: usize| rec.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).map(|s| tiff.order.u32(s) as usize); let mut out = Vec::new(); From 591061efb488727cb4a38cbb10cb861dec0bc832 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:29:43 +0000 Subject: [PATCH 07/17] Cover located MPF index parity --- crates/metadata/src/mpf.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/metadata/src/mpf.rs b/crates/metadata/src/mpf.rs index 5c7f5a7cd..edeb29348 100644 --- a/crates/metadata/src/mpf.rs +++ b/crates/metadata/src/mpf.rs @@ -233,6 +233,7 @@ mod tests { let segs = segments(&file).unwrap(); assert_eq!(segs.iter().map(|s| s.0).collect::>(), [0xe0, 0xe1, 0xe2, 0xdb]); let (_, a, e) = segs[2]; + assert_eq!(images_from_index(&file[a + SIGNATURE.len()..e], a + SIGNATURE.len()), found); let mut without = file[..a - 4].to_vec(); without.extend_from_slice(&file[e..found[0].offset]); assert_eq!(without, primary); From 149e74d739443d22fabb877745dad665f51c5eff Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:32:40 +0000 Subject: [PATCH 08/17] Preserve bounded MPF index directory validation --- crates/metadata/src/mpf.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/metadata/src/mpf.rs b/crates/metadata/src/mpf.rs index edeb29348..9da7dc8c6 100644 --- a/crates/metadata/src/mpf.rs +++ b/crates/metadata/src/mpf.rs @@ -114,12 +114,38 @@ pub fn images(jpeg: &[u8]) -> Vec { // offsets count from the byte-order field, which follows the signature let Some(base) = segs.iter().find(|s| jpeg.get(s.1..s.2) == Some(payload)).map(|s| s.1 + SIGNATURE.len()) else { return Vec::new() }; let Some(index) = payload.get(SIGNATURE.len()..) else { return Vec::new() }; - images_from_index(index, base) + parse_index(index, base) } /// Read an already located MPF TIFF index. `base` is the index's offset in the containing file. /// Entries are bounded to [`MAX_ENTRIES`] and returned without reading image payloads. pub fn images_from_index(index: &[u8], base: usize) -> Vec { + // The bounded legacy reader requires a complete classic first IFD, while the generic TIFF + // parser can salvage partial directories. Preserve that refusal for located MPF indices. + let le = match index.get(..2) { + Some(b"II") => true, + Some(b"MM") => false, + _ => return Vec::new(), + }; + let read16 = + |at: usize| index.get(at..at.checked_add(2)?).map(|b| if le { u16::from_le_bytes([b[0], b[1]]) } else { u16::from_be_bytes([b[0], b[1]]) }); + let read32 = |at: usize| { + index + .get(at..at.checked_add(4)?) + .map(|b| if le { u32::from_le_bytes([b[0], b[1], b[2], b[3]]) } else { u32::from_be_bytes([b[0], b[1], b[2], b[3]]) }) + }; + if read16(2) != Some(42) { + return Vec::new(); + } + let Some(at) = read32(4).and_then(|v| usize::try_from(v).ok()) else { return Vec::new() }; + let Some(count) = read16(at).map(usize::from).filter(|n| *n <= 4096) else { return Vec::new() }; + if at.checked_add(2).and_then(|p| count.checked_mul(12).and_then(|n| p.checked_add(n))).is_none_or(|end| end > index.len()) { + return Vec::new(); + } + parse_index(index, base) +} + +fn parse_index(index: &[u8], base: usize) -> Vec { let Some(tiff) = Tiff::parse(index).ok() else { return Vec::new() }; let Some(list) = tiff.ifds.first().and_then(|i| i.bytes(MP_ENTRY)) else { return Vec::new() }; let u32_at = |rec: &[u8], at: usize| rec.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).map(|s| tiff.order.u32(s) as usize); From 9fad598f4bd35a173e1ff3d3fdac9c41d3c0004e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:38:18 +0000 Subject: [PATCH 09/17] Share Samsung SEF repair and JPEG EOI inspection --- crates/image-codecs/src/codecs/jpeg.rs | 29 ++++++++- crates/image-codecs/src/jpeg_sef.rs | 82 ++++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 1 + 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 crates/image-codecs/src/jpeg_sef.rs diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index bcd86ad5a..158491134 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -195,6 +195,27 @@ enum DataEnd { /// measured on these, not on the file length, which APP padding can inflate without /// adding image data. fn data_end(b: &[u8]) -> (DataEnd, u64) { + data_end_mode(b, false) +} + +/// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused. +pub(crate) fn reaches_eoi(b: &[u8]) -> bool { + data_end_mode(b, true).0 == DataEnd::Complete +} + +fn find_ff(b: &[u8]) -> Option { + const ONES: u64 = 0x0101_0101_0101_0101; + let (words, tail) = b.as_chunks::<8>(); + for (i, word) in words.iter().enumerate() { + let x = !u64::from_ne_bytes(*word); + if x.wrapping_sub(ONES) & !x & (ONES << 7) != 0 { + return word.iter().position(|&v| v == 0xff).map(|j| i * 8 + j); + } + } + tail.iter().position(|&v| v == 0xff).map(|j| words.len() * 8 + j) +} + +fn data_end_mode(b: &[u8], strict: bool) -> (DataEnd, u64) { // Where the bytes run out: inside the image data once a scan has held some. let cut = |scan_data: bool, scanned: u64| (if scan_data { DataEnd::Truncated } else { DataEnd::Empty }, scanned); let mut i = 2; // past SOI @@ -207,7 +228,7 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) { // segments after the stray bytes (padding among them) are still recognised. Stray // bytes are not scan data, so they never count (junk would buy budget as padding // did); with no marker left, the image data ends here. - let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { return cut(scan_data, scanned) }; + let Some(p) = b.get(i..).and_then(find_ff) else { return cut(scan_data, scanned) }; i += p; continue; } @@ -216,15 +237,19 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) { 0xD9 => return (DataEnd::Complete, scanned), 0xFF => i += 1, 0x01 | 0xD0..=0xD8 => i += 2, + 0x00 if strict => i += 2, _ => { let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else { return cut(scan_data, scanned); }; + if strict && len < 2 { + return cut(scan_data, scanned); + } i = i.saturating_add(2 + usize::from(len)); if code == 0xDA { let start = i; loop { - let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { + let Some(p) = b.get(i..).and_then(find_ff) else { // EOF inside this scan: everything from the scan start to the end // of the file is scan data (inside a declared segment it may not // be, so only count what remains). diff --git a/crates/image-codecs/src/jpeg_sef.rs b/crates/image-codecs/src/jpeg_sef.rs new file mode 100644 index 000000000..6ae878013 --- /dev/null +++ b/crates/image-codecs/src/jpeg_sef.rs @@ -0,0 +1,82 @@ +//! Validated Samsung SEF trailer repair for JPEGs missing their final EOI marker. +use std::borrow::Cow; + +/// Whether the first JPEG image reaches an EOI through its marker and scan structure. +pub fn reaches_eoi(bytes: &[u8]) -> bool { + crate::codecs::jpeg::reaches_eoi(bytes) +} + +/// Borrow ordinary input unchanged. For a validated Samsung SEF trailer following an open JPEG +/// scan, remove that trailer and append the missing EOI. The owned copy is made only on repair. +pub fn repair(bytes: &[u8]) -> Cow<'_, [u8]> { + let Some(image) = image_end(bytes).and_then(|end| bytes.get(..end)) else { return Cow::Borrowed(bytes) }; + if reaches_eoi(image) { + return Cow::Borrowed(bytes); + } + let mut fixed = Vec::with_capacity(image.len().saturating_add(2)); + fixed.extend_from_slice(image); + fixed.extend_from_slice(&[0xff, 0xd9]); + let in_scan = craft_metadata::inspect_jpeg(&fixed).is_some_and(|m| m.first_scan_components > 0) && reaches_eoi(&fixed); + if in_scan { Cow::Owned(fixed) } else { Cow::Borrowed(bytes) } +} + +fn image_end(b: &[u8]) -> Option { + let le32 = |at: usize| b.get(at..at.checked_add(4)?)?.try_into().ok().map(|v| u32::from_le_bytes(v) as usize); + let tail = b.len().checked_sub(8)?; + if b.get(tail.checked_add(4)?..)? != b"SEFT" { + return None; + } + let dir = tail.checked_sub(le32(tail)?)?; + if b.get(dir..dir.checked_add(4)?)? != b"SEFH" { + return None; + } + let count = le32(dir.checked_add(8)?)?; + let entries = dir.checked_add(12)?; + if count == 0 || count > tail.checked_sub(entries)? / 12 { + return None; + } + let mut start = dir; + for i in 0..count { + let entry = entries.checked_add(i.checked_mul(12)?)?; + let block = dir.checked_sub(le32(entry.checked_add(4)?)?)?; + let len = le32(entry.checked_add(8)?)?; + let kind = |at: usize| b.get(at.checked_add(2)?..at.checked_add(4)?); + if len < 4 || block.checked_add(len)? > dir || kind(block)? != kind(entry)? { + return None; + } + start = start.min(block); + } + Some(start) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn repairs_only_valid_sef_after_an_open_scan() { + let image = [0xff, 0xd8, 0xff, 0xc0, 0, 8, 8, 0, 1, 0, 1, 1, 0xff, 0xda, 0, 3, 1, 0x12, 0xff, 0, 0x34]; + let mut b = image.to_vec(); + let block = [0, 0, 0xe0, 0x08, 1, 2, 3, 4]; + b.extend_from_slice(&block); + b.extend_from_slice(b"SEFH"); + b.extend_from_slice(&106u32.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.extend_from_slice(&[0, 0, 0xe0, 0x08]); + b.extend_from_slice(&(block.len() as u32).to_le_bytes()); + b.extend_from_slice(&(block.len() as u32).to_le_bytes()); + b.extend_from_slice(&24u32.to_le_bytes()); + b.extend_from_slice(b"SEFT"); + let mut expected = image.to_vec(); + expected.extend_from_slice(&[0xff, 0xd9]); + assert_eq!(repair(&b).as_ref(), expected); + assert!(matches!(repair(&image), Cow::Borrowed(_))); + b[image.len() + 2] = 0; + assert!(matches!(repair(&b), Cow::Borrowed(_))); + } + #[test] + fn eoi_requires_valid_lengths_and_skips_stuffed_bytes() { + assert!(reaches_eoi(&[0xff, 0xd8, 0xff, 0, 0xff, 0xd9])); + assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 1, 0xff, 0xd9])); + assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 4, 0xff, 0xd9])); + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..74968ccaa 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -25,6 +25,7 @@ mod fidelity; mod format; mod image; mod inspection; +pub mod jpeg_sef; mod lab; mod options; pub mod orientation; From 5da1f0c521143adaeb1829afa65ef36d623823ef Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:39:20 +0000 Subject: [PATCH 10/17] Cover JPEG scan byte search --- crates/image-codecs/src/codecs/jpeg.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 158491134..15346ea56 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -678,6 +678,20 @@ mod tests { } /// `info` reads the frame from the markers, and which decoder the file goes to. + #[test] + fn ff_search_matches_plain_search() { + let mut b: Vec = (0..300u32).map(|i| (i * 7 % 255) as u8).collect(); + for at in [None, Some(0), Some(7), Some(8), Some(15), Some(203), Some(296), Some(299)] { + if let Some(i) = at { + b[i] = 0xff; + } + for start in [0, 1, 5, 9, 290, 299, 300] { + let s = &b[start..]; + assert_eq!(find_ff(s), s.iter().position(|&v| v == 0xff)); + } + } + } + #[test] fn info_reads_the_frame_and_the_decoder() { let img = Image::from_raw(37, 21, ChannelLayout::Rgb, SampleType::U8, vec![128; 37 * 21 * 3]).unwrap(); From b30a9623e163637fa0660bc219482435ec93f35f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:48:29 +0000 Subject: [PATCH 11/17] Share explicit raw EXIF and JPEG APP metadata helpers --- crates/image-codecs/src/lib.rs | 1 + crates/image-codecs/src/raw_metadata.rs | 153 ++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 crates/image-codecs/src/raw_metadata.rs diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 74968ccaa..8f5e7586b 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -29,6 +29,7 @@ pub mod jpeg_sef; mod lab; mod options; pub mod orientation; +pub mod raw_metadata; pub mod resolution; pub mod source_settings; pub mod web; diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs new file mode 100644 index 000000000..e0465776c --- /dev/null +++ b/crates/image-codecs/src/raw_metadata.rs @@ -0,0 +1,153 @@ +//! Explicit verbatim EXIF and JPEG APP metadata helpers for callers that preserve stored orientation. +//! Normal encoder APIs still normalize metadata for upright pixels; callers opt in to these helpers. +use crate::{CodecError, Format}; + +/// Borrowed metadata used to construct JPEG APP segments. +#[derive(Clone, Copy, Default)] +pub struct JpegMetadata<'a> { + /// TIFF-structured EXIF bytes. + pub exif: Option<&'a [u8]>, + /// XMP packet. + pub xmp: Option<&'a str>, + /// ICC profile bytes. + pub icc: Option<&'a [u8]>, + /// Pixels per inch, if nonzero. + pub ppi: Option, +} + +fn error(format: Format, message: &str) -> CodecError { + CodecError::encode(format, message) +} + +/// Build JFIF, EXIF, XMP and ICC APP payloads in that order, preserving bytes and segment limits. +pub fn jpeg_segments(meta: JpegMetadata<'_>) -> Result)>, CodecError> { + let mut segments = Vec::new(); + let density = meta.ppi.filter(|p| *p > 0).map_or([0, 0, 1, 0, 1], |p| { + let [hi, lo] = p.to_be_bytes(); + [1, hi, lo, hi, lo] + }); + segments.push((0xe0, [&b"JFIF\0\x01\x02"[..], &density, &[0, 0]].concat())); + if let Some(exif) = meta.exif { + if exif.len() > 65_527 { + return Err(error(Format::Jpeg, "EXIF larger than one APP1 segment (64 KiB)")); + } + segments.push((0xe1, [&b"Exif\0\0"[..], exif].concat())); + } + if let Some(xmp) = meta.xmp { + segments.push((0xe1, jpeg_xmp(xmp)?)); + } + if let Some(icc) = meta.icc { + let chunks: Vec<_> = icc.chunks(65_519).collect(); + if chunks.len() > 255 { + return Err(error(Format::Jpeg, "ICC profile too large for JPEG")); + } + for (i, chunk) in chunks.iter().enumerate() { + segments.push((0xe2, [&b"ICC_PROFILE\0"[..], &[i as u8 + 1, chunks.len() as u8], chunk].concat())); + } + } + Ok(segments) +} + +/// Build a single standard XMP APP1 payload, rejecting extended-XMP-sized packets. +pub fn jpeg_xmp(xmp: &str) -> Result, CodecError> { + let payload = [&b"http://ns.adobe.com/xap/1.0/\0"[..], xmp.as_bytes()].concat(); + if payload.len() > 65_533 { + return Err(error(Format::Jpeg, "XMP larger than one APP1 segment (extended XMP not supported)")); + } + Ok(payload) +} + +/// Restore bytes in the first existing JPEG EXIF payload when it has room. No segment is inserted. +pub fn replace_jpeg_exif(jpeg: &mut [u8], exif: &[u8]) { + if let Some(pos) = jpeg.windows(6).position(|w| w == b"Exif\0\0") + && let Some(end) = pos.checked_add(6).and_then(|v| v.checked_add(exif.len())) + && let Some(dst) = jpeg.get_mut(pos + 6..end) + { + dst.copy_from_slice(exif); + } +} + +/// Replace the existing PNG eXIf chunk, preserving the other chunks and recomputing its CRC. +/// No chunk is inserted when one is absent. +pub fn replace_png_exif(png: &mut Vec, exif: &[u8]) -> Result<(), CodecError> { + let len = u32::try_from(exif.len()).map_err(|_| error(Format::Png, "EXIF too large"))?; + let mut p = 8usize; + while let Some(bytes) = p.checked_add(4).and_then(|end| png.get(p..end)) { + let size = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize; + let Some(end) = p.checked_add(12).and_then(|v| v.checked_add(size)).filter(|end| *end <= png.len()) else { break }; + if png.get(p + 4..p + 8) == Some(b"eXIf") { + let mut chunk = Vec::new(); + chunk.extend_from_slice(&len.to_be_bytes()); + chunk.extend_from_slice(b"eXIf"); + chunk.extend_from_slice(exif); + let crc = crc32fast::hash(&chunk[4..]); + chunk.extend_from_slice(&crc.to_be_bytes()); + png.splice(p..end, chunk); + break; + } + p = end; + } + Ok(()) +} + +/// Replace the existing WebP EXIF chunk, including padding and the RIFF size. No chunk is inserted +/// when one is absent. The input must be an encoded WebP as emitted by this crate. +pub fn replace_webp_exif(webp: &mut Vec, exif: &[u8]) -> Result<(), CodecError> { + let len = u32::try_from(exif.len()).map_err(|_| error(Format::WebP, "EXIF too large"))?; + let mut p = 12usize; + while let Some(h) = webp.get(p..p.saturating_add(8)) { + let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize; + let end = p.saturating_add(8).saturating_add(size).saturating_add(size & 1); + if &h[..4] == b"EXIF" && end <= webp.len() { + let new_len = webp + .len() + .checked_sub(end - p) + .and_then(|v| v.checked_add(8)) + .and_then(|v| v.checked_add(exif.len())) + .and_then(|v| v.checked_add(exif.len() & 1)) + .ok_or_else(|| error(Format::WebP, "RIFF too large"))?; + let riff_len = u32::try_from(new_len.saturating_sub(8)).map_err(|_| error(Format::WebP, "RIFF too large"))?; + let mut chunk = Vec::new(); + chunk.extend_from_slice(b"EXIF"); + chunk.extend_from_slice(&len.to_le_bytes()); + chunk.extend_from_slice(exif); + if exif.len() & 1 == 1 { + chunk.push(0); + } + webp.splice(p..end, chunk); + webp[4..8].copy_from_slice(&riff_len.to_le_bytes()); + break; + } + p = end; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + const EXIF: &[u8] = b"II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0"; + #[test] + fn explicit_replacements_preserve_raw_orientation_in_real_encodings() { + let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); + image.meta.exif = Some(EXIF.to_vec()); + for format in [Format::Jpeg, Format::Png, Format::WebP] { + let mut bytes = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); + match format { + Format::Jpeg => replace_jpeg_exif(&mut bytes, EXIF), + Format::Png => replace_png_exif(&mut bytes, EXIF).unwrap(), + Format::WebP => replace_webp_exif(&mut bytes, EXIF).unwrap(), + _ => unreachable!(), + } + let decoded = crate::decode_as_with(format, &bytes, &crate::DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(decoded.meta.exif.as_deref().map(crate::exif_orientation), Some(6), "{format:?}"); + } + } + #[test] + fn jpeg_segments_keep_order_and_enforce_limits() { + let parts = jpeg_segments(JpegMetadata { exif: Some(EXIF), xmp: Some(""), icc: Some(&[1, 2]), ppi: Some(300) }).unwrap(); + assert_eq!(parts.iter().map(|v| v.0).collect::>(), [0xe0, 0xe1, 0xe1, 0xe2]); + assert!(jpeg_segments(JpegMetadata { exif: Some(&vec![0; 65_528]), ..Default::default() }).is_err()); + assert!(jpeg_xmp(&"x".repeat(65_533)).is_err()); + } +} From a99e47639e63552863d23696229704fa458c71a7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:48:37 +0000 Subject: [PATCH 12/17] Keep metadata fixture exhaustive without panic --- crates/image-codecs/src/raw_metadata.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs index e0465776c..d7d21b698 100644 --- a/crates/image-codecs/src/raw_metadata.rs +++ b/crates/image-codecs/src/raw_metadata.rs @@ -137,7 +137,7 @@ mod tests { Format::Jpeg => replace_jpeg_exif(&mut bytes, EXIF), Format::Png => replace_png_exif(&mut bytes, EXIF).unwrap(), Format::WebP => replace_webp_exif(&mut bytes, EXIF).unwrap(), - _ => unreachable!(), + _ => {} } let decoded = crate::decode_as_with(format, &bytes, &crate::DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); assert_eq!(decoded.meta.exif.as_deref().map(crate::exif_orientation), Some(6), "{format:?}"); From 8767ba1c7e3198d9aaf2e8bc69320f4b32eb58ea Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:51:06 +0000 Subject: [PATCH 13/17] Compare EXIF replacements on mutated containers --- crates/image-codecs/src/raw_metadata.rs | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs index d7d21b698..0ea3a104e 100644 --- a/crates/image-codecs/src/raw_metadata.rs +++ b/crates/image-codecs/src/raw_metadata.rs @@ -127,6 +127,76 @@ pub fn replace_webp_exif(webp: &mut Vec, exif: &[u8]) -> Result<(), CodecErr mod tests { use super::*; const EXIF: &[u8] = b"II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0"; + #[test] + fn replacements_match_previous_code_on_mutated_containers() { + fn old_png(png: &mut Vec, exif: &[u8]) { + let mut p = 8usize; + while let Some(b) = png.get(p..p + 4) { + let size = u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize; + let end = p.saturating_add(12).saturating_add(size); + if end > png.len() { + break; + } + if png.get(p + 4..p + 8) == Some(b"eXIf") { + let mut chunk = (exif.len() as u32).to_be_bytes().to_vec(); + chunk.extend_from_slice(b"eXIf"); + chunk.extend_from_slice(exif); + let mut crc = 0xffff_ffffu32; + for &v in &chunk[4..] { + crc ^= u32::from(v); + for _ in 0..8 { + crc = if crc & 1 != 0 { (crc >> 1) ^ 0xedb8_8320 } else { crc >> 1 }; + } + } + chunk.extend_from_slice(&(!crc).to_be_bytes()); + png.splice(p..end, chunk); + return; + } + p = end; + } + } + fn old_webp(webp: &mut Vec, exif: &[u8]) { + let mut p = 12usize; + while let Some(h) = webp.get(p..p.saturating_add(8)) { + let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize; + let end = p.saturating_add(8).saturating_add(size).saturating_add(size & 1); + if &h[..4] == b"EXIF" && end <= webp.len() { + let mut chunk = b"EXIF".to_vec(); + chunk.extend_from_slice(&(exif.len() as u32).to_le_bytes()); + chunk.extend_from_slice(exif); + if exif.len() & 1 == 1 { + chunk.push(0); + } + webp.splice(p..end, chunk); + let size = (webp.len().saturating_sub(8)) as u32; + webp[4..8].copy_from_slice(&size.to_le_bytes()); + return; + } + p = end; + } + } + let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); + image.meta.exif = Some(EXIF.to_vec()); + for format in [Format::Png, Format::WebP] { + let encoded = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); + for at in 0..encoded.len() { + for value in [0, 255] { + let mut original = encoded.clone(); + original[at] = value; + let mut shared = original.clone(); + if format == Format::Png { + old_png(&mut original, EXIF); + replace_png_exif(&mut shared, EXIF).unwrap(); + } else { + old_webp(&mut original, EXIF); + replace_webp_exif(&mut shared, EXIF).unwrap(); + } + assert_eq!(shared, original, "{format:?}: byte {at} value {value}"); + } + } + } + } + #[test] fn explicit_replacements_preserve_raw_orientation_in_real_encodings() { let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); From a152abdb8be4817339fb02fabf81bb38d4c4c7ff Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:51:44 +0000 Subject: [PATCH 14/17] Cover JPEG EXIF mutation parity --- crates/image-codecs/src/raw_metadata.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs index 0ea3a104e..6be61992e 100644 --- a/crates/image-codecs/src/raw_metadata.rs +++ b/crates/image-codecs/src/raw_metadata.rs @@ -177,14 +177,21 @@ mod tests { } let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); image.meta.exif = Some(EXIF.to_vec()); - for format in [Format::Png, Format::WebP] { + for format in [Format::Jpeg, Format::Png, Format::WebP] { let encoded = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); for at in 0..encoded.len() { for value in [0, 255] { let mut original = encoded.clone(); original[at] = value; let mut shared = original.clone(); - if format == Format::Png { + if format == Format::Jpeg { + if let Some(pos) = original.windows(6).position(|w| w == b"Exif\0\0") + && let Some(dst) = original.get_mut(pos + 6..pos + 6 + EXIF.len()) + { + dst.copy_from_slice(EXIF); + } + replace_jpeg_exif(&mut shared, EXIF); + } else if format == Format::Png { old_png(&mut original, EXIF); replace_png_exif(&mut shared, EXIF).unwrap(); } else { From 1ef2c5359dc671fb6ca7f851a623a9a919b5867c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:53:32 +0000 Subject: [PATCH 15/17] Verify default EXIF normalization remains intact --- crates/image-codecs/src/raw_metadata.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs index 6be61992e..3a6c13eb7 100644 --- a/crates/image-codecs/src/raw_metadata.rs +++ b/crates/image-codecs/src/raw_metadata.rs @@ -210,6 +210,8 @@ mod tests { image.meta.exif = Some(EXIF.to_vec()); for format in [Format::Jpeg, Format::Png, Format::WebP] { let mut bytes = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); + let normal = crate::decode_as_with(format, &bytes, &crate::DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(normal.meta.exif.as_deref().map(crate::exif_orientation), Some(1), "default {format:?}"); match format { Format::Jpeg => replace_jpeg_exif(&mut bytes, EXIF), Format::Png => replace_png_exif(&mut bytes, EXIF).unwrap(), From 3a28181f0e8aaade4e7dd5fc712dd1fec58499a1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:55:14 +0000 Subject: [PATCH 16/17] Cover JPEG ICC segment boundaries --- crates/image-codecs/src/raw_metadata.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs index 3a6c13eb7..76fa4aa73 100644 --- a/crates/image-codecs/src/raw_metadata.rs +++ b/crates/image-codecs/src/raw_metadata.rs @@ -228,5 +228,10 @@ mod tests { assert_eq!(parts.iter().map(|v| v.0).collect::>(), [0xe0, 0xe1, 0xe1, 0xe2]); assert!(jpeg_segments(JpegMetadata { exif: Some(&vec![0; 65_528]), ..Default::default() }).is_err()); assert!(jpeg_xmp(&"x".repeat(65_533)).is_err()); + let profile = vec![42; 65_520]; + let chunks = jpeg_segments(JpegMetadata { icc: Some(&profile), ..Default::default() }).unwrap(); + assert_eq!(chunks.len(), 3); + assert_eq!(&chunks[1].1[12..14], &[1, 2]); + assert_eq!(&chunks[2].1[12..14], &[2, 2]); } } From e8aed7cfa6f4045330949c804bdc5e2a3392479a Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:30:28 +0100 Subject: [PATCH 17/17] image-codecs: find the JPEG EXIF APP1 by walking segments and bound the copy by its length An Exif signature inside a COM segment was taken for the EXIF APP1, and the copy was checked only against the end of the file, so a replacement longer than the APP1 overwrote the next marker. Walk the header segments to the first EXIF APP1 and copy only within its declared length. --- crates/image-codecs/src/raw_metadata.rs | 89 ++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs index 76fa4aa73..916a0078a 100644 --- a/crates/image-codecs/src/raw_metadata.rs +++ b/crates/image-codecs/src/raw_metadata.rs @@ -58,15 +58,48 @@ pub fn jpeg_xmp(xmp: &str) -> Result, CodecError> { } /// Restore bytes in the first existing JPEG EXIF payload when it has room. No segment is inserted. +/// +/// The EXIF APP1 is found by walking the marker segments before the first scan, so an `Exif\0\0` +/// inside another segment (a comment, say) is never taken for it, and the copy must fit inside +/// the APP1's own declared length, not merely inside the file. pub fn replace_jpeg_exif(jpeg: &mut [u8], exif: &[u8]) { - if let Some(pos) = jpeg.windows(6).position(|w| w == b"Exif\0\0") - && let Some(end) = pos.checked_add(6).and_then(|v| v.checked_add(exif.len())) - && let Some(dst) = jpeg.get_mut(pos + 6..end) + if let Some(payload) = jpeg_exif_payload(jpeg) + && let Some(dst) = jpeg.get_mut(payload).and_then(|p| p.get_mut(..exif.len())) { dst.copy_from_slice(exif); } } +/// The byte range of the TIFF data in the first EXIF APP1 (after `Exif\0\0`), up to the end of +/// that segment; `None` when the header segments end, are damaged, or hold no EXIF APP1. +fn jpeg_exif_payload(jpeg: &[u8]) -> Option> { + if jpeg.get(..2) != Some(&[0xff, 0xd8][..]) { + return None; + } + let mut p = 2usize; + loop { + // fill bytes (0xff) may precede a marker + while jpeg.get(p) == Some(&0xff) && jpeg.get(p + 1) == Some(&0xff) { + p += 1; + } + let (&0xff, &marker) = (jpeg.get(p)?, jpeg.get(p + 1)?) else { return None }; + match marker { + // TEM and RSTn carry no length + 0x01 | 0xd0..=0xd7 => p += 2, + // start of scan, end of image: no header segment follows + 0xda | 0xd9 | 0x00 => return None, + _ => { + let len = usize::from(u16::from_be_bytes([*jpeg.get(p + 2)?, *jpeg.get(p + 3)?])); + let end = p.checked_add(2)?.checked_add(len).filter(|&e| len >= 2 && e <= jpeg.len())?; + if marker == 0xe1 && jpeg.get(p + 4..end)?.starts_with(b"Exif\0\0") { + return Some(p + 10..end); + } + p = end; + } + } + } +} + /// Replace the existing PNG eXIf chunk, preserving the other chunks and recomputing its CRC. /// No chunk is inserted when one is absent. pub fn replace_png_exif(png: &mut Vec, exif: &[u8]) -> Result<(), CodecError> { @@ -185,12 +218,21 @@ mod tests { original[at] = value; let mut shared = original.clone(); if format == Format::Jpeg { + // the old byte search; the segment walk writes where it did or, when the + // mutation broke the segment structure or the APP1 length, not at all + let untouched = original.clone(); if let Some(pos) = original.windows(6).position(|w| w == b"Exif\0\0") && let Some(dst) = original.get_mut(pos + 6..pos + 6 + EXIF.len()) { dst.copy_from_slice(EXIF); } replace_jpeg_exif(&mut shared, EXIF); + assert!(shared == original || shared == untouched, "Jpeg: byte {at} value {value}"); + if at == 0 && value == 255 { + // SOI kept: a real replacement + assert_eq!(shared, original); + } + continue; } else if format == Format::Png { old_png(&mut original, EXIF); replace_png_exif(&mut shared, EXIF).unwrap(); @@ -222,6 +264,47 @@ mod tests { assert_eq!(decoded.meta.exif.as_deref().map(crate::exif_orientation), Some(6), "{format:?}"); } } + #[test] + fn jpeg_exif_is_found_by_segment_not_by_signature() { + fn segment(marker: u8, payload: &[u8]) -> Vec { + [&[0xff, marker][..], &((payload.len() + 2) as u16).to_be_bytes(), payload].concat() + } + let tail = [0xff, 0xda, 0, 2, 0xff, 0xd9]; + let exif_app1 = segment(0xe1, &[&b"Exif\0\0"[..], &[0; 26]].concat()); + // a comment carrying the signature comes first: only the APP1 is rewritten + let comment = segment(0xfe, &[&b"Exif\0\0"[..], &[7; 40]].concat()); + let mut jpeg = [&[0xff, 0xd8, 0xff][..], &comment, &exif_app1, &tail].concat(); + let before = jpeg.clone(); + replace_jpeg_exif(&mut jpeg, &[9; 26]); + let app1_data = 3 + comment.len() + 10; + assert_eq!(&jpeg[..app1_data], &before[..app1_data]); + assert_eq!(&jpeg[app1_data..app1_data + 26], &[9; 26]); + assert_eq!(&jpeg[app1_data + 26..], &before[app1_data + 26..]); + // no EXIF APP1, only the comment: nothing changes + let mut only_comment = [&[0xff, 0xd8][..], &comment, &tail].concat(); + let before = only_comment.clone(); + replace_jpeg_exif(&mut only_comment, &[9; 26]); + assert_eq!(only_comment, before); + // a replacement larger than the APP1 is refused even though the file continues + let mut small = [&[0xff, 0xd8][..], &exif_app1, &segment(0xe2, &[1; 64]), &tail].concat(); + let before = small.clone(); + replace_jpeg_exif(&mut small, &[9; 28]); + assert_eq!(small, before); + // markers without a length, damaged lengths, a scan before any APP1, and a non-JPEG + let mut tem = [&[0xff, 0xd8, 0xff, 0x01][..], &exif_app1, &tail].concat(); + replace_jpeg_exif(&mut tem, &[9; 26]); + assert_eq!(&tem[14..40], &[9; 26]); + for broken in [vec![0xff, 0xd8, 0xff, 0xe1, 0, 1], vec![0xff, 0xd8, 0xff, 0xe1, 0xff, 0xff, 0], tail.to_vec(), vec![0xff, 0xd8, 0xff, 0xda]] { + let mut b = broken.clone(); + replace_jpeg_exif(&mut b, &[9; 2]); + assert_eq!(b, broken); + } + let mut no_marker = [&[0xff, 0xd8, 0x12][..], &exif_app1].concat(); + let before = no_marker.clone(); + replace_jpeg_exif(&mut no_marker, &[9; 26]); + assert_eq!(no_marker, before); + } + #[test] fn jpeg_segments_keep_order_and_enforce_limits() { let parts = jpeg_segments(JpegMetadata { exif: Some(EXIF), xmp: Some(""), icc: Some(&[1, 2]), ppi: Some(300) }).unwrap();