Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

ORF: a file cut short inside IFD0 is malformed, not the compressed variant (#1008) (#1090)

wstock9d9e512d40a2
diff --git a/crates/raw/src/orf.rs b/crates/raw/src/orf.rs
index 7f489cea9..84670fe6b 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<Sensor> {
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 cd5624dae..a2b193efe 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<u16> = (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);