Restore JPEG metadata preflight parity (#7930)

This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-19 23:22:57 -05:00
1 parent e1d2f32e58
commit 006e79b5ab
1 file changed
+27 -5
+27 -5
View File
@@ -113,12 +113,34 @@ pub(crate) fn without_extended_xmp(bytes: &[u8]) -> Vec<u8> {
/// Calls `f` with the byte range of each Extended XMP chunk (APP1 segment) before the image data.
fn each_extended_xmp(bytes: &[u8], mut f: impl FnMut(std::ops::Range<usize>)) {
const PREFIX: &[u8] = b"http://ns.adobe.com/xmp/extension/\0";
let _ = crate::image_probe::walk_jpeg_segments(bytes, |marker, span, seg| {
if marker == 0xE1 && seg.starts_with(PREFIX) {
f(span);
let mut i = 2usize;
while let Some(&byte) = bytes.get(i) {
// Stray bytes between segments, which the decoder skips too.
if byte != 0xFF {
i += 1;
continue;
}
Ok(crate::image_probe::JpegVisit::Continue)
});
let Some(&marker) = bytes.get(i.saturating_add(1)) else { break };
match marker {
// Fill bytes before a marker.
0xFF => i += 1,
// Markers without a segment.
0x00 | 0x01 | 0xD0..=0xD7 => i += 2,
// The image data starts (or ends): no headers after it.
0xDA | 0xD9 => break,
_ => {
let Some(&[hi, lo]) = bytes.get(i.saturating_add(2)..i.saturating_add(4)) else { break };
let len = usize::from(u16::from_be_bytes([hi, lo]));
let end = i.saturating_add(2).saturating_add(len.max(2));
// The prefix must start the APP1 segment's own payload: a zero-length header
// followed by prefix bytes is not a chunk.
if marker == 0xE1 && bytes.get(i.saturating_add(4)..end.min(bytes.len())).is_some_and(|seg| seg.starts_with(PREFIX)) {
f(i..end.min(bytes.len()));
}
i = end;
}
}
}
}
fn jfif_density_dpi(bytes: &[u8]) -> Option<(f32, f32)> {