Share Samsung SEF repair and JPEG EOI inspection (#1640)

* Expose tolerant JPEG marker inspection

* Export JPEG inspection API

* Factor JPEG inspection fields from shared walker

* Share ICC chunk collection between JPEG inspection modes

* Cover JPEG MPF and gain-map inspection

* Expose MPF index inspection for located payloads

* Cover located MPF index parity

* Preserve bounded MPF index directory validation

* Share Samsung SEF repair and JPEG EOI inspection

* Cover JPEG scan byte search
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-15 13:57:49 -05:00
1 parent e7c96161dd
commit cae7c1d8a6
3 files changed
+124 -2

No files matched your search

+41 -2
View File
@@ -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<usize> {
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).
@@ -653,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<u8> = (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();
+82
View File
@@ -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<usize> {
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]));
}
}
+1
View File
@@ -26,6 +26,7 @@ mod fidelity;
mod format;
mod image;
mod inspection;
pub mod jpeg_sef;
mod lab;
mod options;
pub mod orientation;