From 891be5f473b70e1636676b00bf164e97beada4db Mon Sep 17 00:00:00 2001 From: wstock <1394687+wstock@users.noreply.github.com> Date: Thu, 8 Oct 2026 07:45:33 +0000 Subject: [PATCH] ORF: a file cut short inside IFD0 is malformed, not the compressed variant (#1008) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "is this the undecoded compressed/packed ORF" guard summed the strip byte counts and compared the sum with width × height × 2. Tiff::tag_uints returns an empty list when the entry is missing or unreadable, so a file whose IFD0 entry table is truncated (a partial download, a damaged card) summed to zero and was reported as "Olympus compressed (or packed) ORF is not decoded" — a format the user's camera does not produce — while the same file cut a few bytes earlier or later was correctly reported as malformed. Missing or unreadable strip byte counts are now RawError::Malformed before the compression check. Regression test: a synthetic uncompressed ORF cut right before its StripByteCounts entry is Malformed, and no prefix of the file reads as the compressed variant. Co-Authored-By: Claude Fable 5.1 --- crates/raw/src/orf.rs | 9 ++++++++- crates/raw/tests/vendor_codecs.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/orf.rs b/crates/raw/src/orf.rs index 7f489cea9e..84670fe6b2 100644 --- a/crates/raw/src/orf.rs +++ b/crates/raw/src/orf.rs @@ -72,7 +72,14 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result { let raw = t.ifd_at(t.first_ifd, 0).ok_or_else(|| RawError::malformed("ORF has no IFD0"))?; let width = u64::from(t.tag_uint(&raw, tag::IMAGE_WIDTH).unwrap_or(0)); let height = u64::from(t.tag_uint(&raw, tag::IMAGE_LENGTH).unwrap_or(0)); - let stored: u64 = t.tag_uints(&raw, tag::STRIP_BYTE_COUNTS).iter().map(|&c| u64::from(c)).sum(); + // Every ORF, compressed or not, declares its strip byte counts. Missing or unreadable counts + // mean IFD0 is cut short (a partial download or a damaged file), not the compressed variant: + // an empty sum must not be mistaken for "fewer than 16 bits per sample". + let counts = t.tag_uints(&raw, tag::STRIP_BYTE_COUNTS); + if counts.is_empty() { + return Err(RawError::malformed("ORF IFD0 has no readable strip byte counts")); + } + let stored: u64 = counts.iter().map(|&c| u64::from(c)).sum(); if t.tag_uint(&raw, tag::COMPRESSION).unwrap_or(1) != 1 || stored < width.saturating_mul(height).saturating_mul(2) { return Err(RawError::unsupported("Olympus compressed (or packed) ORF is not decoded")); } diff --git a/crates/raw/tests/vendor_codecs.rs b/crates/raw/tests/vendor_codecs.rs index cd5624daef..a2b193efed 100644 --- a/crates/raw/tests/vendor_codecs.rs +++ b/crates/raw/tests/vendor_codecs.rs @@ -177,6 +177,33 @@ fn orf_uncompressed_round_trip() { assert_eq!((p.width, p.height), (16, 8)); } +/// A file cut short inside IFD0 keeps its width, height and compression entries but loses the +/// strip byte counts; their empty sum used to read as "fewer than 16 bits per sample" and the +/// damaged file was reported as the undecoded compressed variant. Damage is `Malformed`, +/// whichever byte it lands on. +#[test] +fn orf_truncated_in_ifd0_is_malformed_not_the_compressed_variant() { + let (w, h) = (40, 24); + let data = mosaic(&scene(w, h), w, [1, 0, 2, 1], 64, 4095); + let b = orf(w, h, &data); + assert!(decode(&b, &Limits::default()).is_ok()); + let ifd = u32::from_le_bytes(b[4..8].try_into().unwrap()) as usize; + let n = u16::from_le_bytes([b[ifd], b[ifd + 1]]) as usize; + let entries: Vec = (0..n).map(|i| u16::from_le_bytes([b[ifd + 2 + 12 * i], b[ifd + 3 + 12 * i]])).collect(); + let at = |tag: u16| ifd + 2 + 12 * entries.iter().position(|&t| t == tag).unwrap(); + // Cut right before the StripByteCounts entry: width, height and compression survive. + let cut = at(279); + assert!(cut > at(259), "compression precedes the byte counts"); + assert!(matches!(decode(&b[..cut], &Limits::default()), Err(RawError::Malformed(_))), "{:?}", decode(&b[..cut], &Limits::default()).err()); + // No prefix of the file reads as the compressed variant: the compression declaration is + // intact or gone, never "compressed". + for cut in 0..b.len() { + if let Err(RawError::Unsupported(m)) = decode(&b[..cut], &Limits::default()) { + assert!(!m.contains("compressed"), "prefix {cut}: {m}"); + } + } +} + #[test] fn orf_packed_or_compressed_falls_back() { let (w, h) = (40, 24);