fix: bound CR3 sample tables and accept supported RAF Bayer layouts

This commit is contained in:
Terra854 committed 2026-11-14 22:47:42 +08:00
1 parent 15cd3900e1
commit eec8408c53
2 files changed
+65 -3

No files matched your search

+22 -2
View File
@@ -371,9 +371,13 @@ fn trak(bytes: &[u8], t: &BoxRef, budget: &mut usize) -> Option<Cr3Track> {
b"stsz" => {
sample_count = be32(body, 8)?;
size = match be32(body, 4)? {
0 if be32(body, 8)? > 0 => be32(body, 12),
// A first entry cannot justify an incomplete per-sample
// table, nor an impossible constant-size sample count.
0 if sample_count > 0 && sample_count as usize <= body.len().saturating_sub(12) / 4 => be32(body, 12),
0 => None,
n if be32(body, 8)? > 0 => Some(n),
n if sample_count > 0 && u64::from(n).checked_mul(u64::from(sample_count)).is_some_and(|total| total <= bytes.len() as u64) => {
Some(n)
}
_ => None,
}
}
@@ -587,6 +591,22 @@ mod tests {
}
}
#[test]
fn first_sample_size_requires_a_complete_bounded_sample_table() {
let valid = trak(craw(16, 16, &bx(b"JPEG", &[0; 4])), 4096, 16);
let at = valid.windows(4).position(|bytes| bytes == b"stsz").unwrap();
for (constant, count, accepted) in [(0u32, 1u32, true), (16, 1, true), (0, 2, false), (0, u32::MAX, false), (16, u32::MAX, false)] {
let mut track = valid.clone();
track[at + 8..at + 12].copy_from_slice(&constant.to_be_bytes());
track[at + 12..at + 16].copy_from_slice(&count.to_be_bytes());
let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom");
file.extend(bx(b"moov", &track));
file.resize(4112, 0);
let parsed = parse_cr3(&file).unwrap();
assert_eq!(parsed.tracks[0].data, accepted.then_some((4096, 16)), "size {constant}, count {count}");
}
}
#[test]
fn hevc_preview_is_distinct_from_an_equal_size_raw_track() {
let preview = trak(craw(6000, 4000, &bx(b"HEVC", &[0; 4])), 4096, 16);
+43 -1
View File
@@ -155,6 +155,12 @@ fn baseline_exposure(h: &Header) -> f64 {
.unwrap_or(0.0)
}
/// The codec fixes the green-site geometry; exchanging the two non-green
/// planes only exchanges their colour labels, so RGGB and BGGR are both valid.
fn compressed_layout_supported(cfa: &Cfa) -> bool {
*cfa == Cfa::xtrans() || *cfa == Cfa::bayer_static("RGGB") || *cfa == Cfa::bayer_static("BGGR")
}
/// Unpack one row of `bits`-bit samples in Fujifilm's packing. `words32`: the raw IFD's `0xf006` flag is set, the
/// samples are MSB-first in little-endian 32-bit words whatever the depth; otherwise 16-bit words, a 12-bit
/// LSB-first bit stream, or (other depths) the same 32-bit words.
@@ -203,9 +209,14 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result<RawIma
};
let mut data = Vec::new();
if compressed {
if cfa != if cfa.width == 6 { Cfa::xtrans() } else { Cfa::bayer_static("RGGB") } {
if !compressed_layout_supported(&cfa) {
return Err(RawError::Unsupported("Fujifilm compressed RAF with an unsupported CFA layout".into()));
}
if !src.starts_with(b"IS") {
// A recognized RAF compression mode can wrap an unknown packing. Do not
// feed it to the IS codec; damage inside an identified IS stream stays Corrupt.
return Err(RawError::Unsupported("Fujifilm compressed RAF without a supported IS signature".into()));
}
let flag = if compression == 2 { 1 } else { 0 };
if src.get(2) != Some(&flag) {
return Err(RawError::Corrupt("RAF container and stream compression modes disagree".into()));
@@ -625,6 +636,37 @@ pub(super) mod tests {
}
}
#[test]
fn compressed_layout_accepts_both_red_blue_orders_without_accepting_wrong_green_phase() {
for name in ["RGGB", "BGGR"] {
assert!(compressed_layout_supported(&Cfa::bayer_static(name)), "{name}");
}
for name in ["GRBG", "GBRG"] {
assert!(!compressed_layout_supported(&Cfa::bayer_static(name)), "{name}");
}
let mut other = Cfa::xtrans();
assert!(compressed_layout_supported(&other));
other.pattern.swap(0, 2);
assert!(!compressed_layout_supported(&other));
}
#[test]
fn unknown_compressed_signature_is_unsupported_but_identified_stream_damage_is_corrupt() {
let header = b"IS\x01\x00\x0e\0\x12\x06\0\x03\x0c\x03\0\x02\0\x03".to_vec();
let known = raf(780, 18, 14, header, None, &[]);
let raw_at = be32(&known, 100).unwrap();
let strip_at = raw_at + raw_ifd(&known[raw_at..]).unwrap().u64(0xf007).unwrap() as usize;
let mut unknown = known.clone();
unknown[strip_at..strip_at + 2].copy_from_slice(b"XX");
for mode in [Mode::Header, Mode::Full] {
assert!(matches!(decode(&unknown, mode, &Limits::default()), Err(RawError::Unsupported(why)) if why.contains("signature")));
assert!(
matches!(decode(&known, mode, &Limits::default()), Err(RawError::Corrupt(_))),
"identified but truncated IS stream must remain corrupt"
);
}
}
/// Raw IFD flag `0xf006` selects the 32-bit-word packing at 12 bits; without it 12 bits are an LSB-first stream.
#[test]
fn flag_selects_word_packed_12_bit() {