mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
craft-exr: deep chunk offsets must follow every part's offset table
In a multipart file the chunks start after the offset tables of all parts. The deep readers in craft-exr and craft-image-codecs took the end of the deep part's own table as the start of the chunk area, so an offset into a later part's table passed the chunk-area check and its bytes were parsed as a chunk. Both readers now compute where the deep part's table starts and where the last table ends with checked arithmetic, and refuse a file whose tables run past its end before reading any offsets. The earlier tables' sizes are no longer capped or saturated. Tests build a two-part file from scratch (a deep part and a 1x1 flat part, in both orders): the deep part decodes as in a single-part file, an offset into the second table or one byte before the end of the tables is refused, and a file that ends inside the second table is refused.
This commit is contained in:
+22
-7
@@ -391,19 +391,14 @@ fn decode_deep_meta(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Result<De
|
||||
let bytes_per_sample: usize = exr_types.iter().map(|t| sample_bytes(*t)).sum();
|
||||
let multipart = meta.requirements.has_multiple_layers;
|
||||
|
||||
// The offset tables follow the headers: `chunk_count` u64 per header, in header order.
|
||||
// Our part's table starts after the tables of all earlier parts.
|
||||
let mut table_pos = header_end(bytes)?;
|
||||
for h_ in meta.headers.iter().take(header_index) {
|
||||
table_pos = table_pos.saturating_add((h_.chunk_count.min(1 << 24) as u64).saturating_mul(8));
|
||||
}
|
||||
// A chunk offset before the end of the last offset table points into table data.
|
||||
let (table_pos, tables_end) = offset_tables(meta, bytes, header_index)?;
|
||||
let mut cur = Cursor::new(bytes);
|
||||
seek_to(&mut cur, table_pos)?;
|
||||
let mut offsets = Vec::with_capacity(header.chunk_count.min(1 << 20));
|
||||
for _ in 0..header.chunk_count {
|
||||
offsets.push(u64(&mut cur)?);
|
||||
}
|
||||
let tables_end = cur.position();
|
||||
|
||||
// Block geometry.
|
||||
let (tiles_x, tile_w, tile_h, slots) = match &header.blocks {
|
||||
@@ -631,6 +626,26 @@ fn header_end(bytes: &[u8]) -> Result<u64> {
|
||||
Ok(cur.position())
|
||||
}
|
||||
|
||||
/// Where part `header_index`'s offset table starts and where the last part's table ends. The
|
||||
/// tables follow the headers, `chunk_count` u64 per header in header order, and the chunks
|
||||
/// follow the last table. Sizes that overflow, or tables that run past the end of the file,
|
||||
/// are errors.
|
||||
fn offset_tables(meta: &MetaData, bytes: &[u8], header_index: usize) -> Result<(u64, u64)> {
|
||||
let mut table_pos = header_end(bytes)?;
|
||||
let mut tables_end = table_pos;
|
||||
for (i, h) in meta.headers.iter().enumerate() {
|
||||
let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8));
|
||||
tables_end = len.and_then(|len| tables_end.checked_add(len)).ok_or_else(|| err("the offset tables are too large"))?;
|
||||
if i < header_index {
|
||||
table_pos = tables_end;
|
||||
}
|
||||
}
|
||||
if tables_end > bytes.len() as u64 {
|
||||
return Err(err(format!("the offset tables end at byte {tables_end}, past the end of the file")));
|
||||
}
|
||||
Ok((table_pos, tables_end))
|
||||
}
|
||||
|
||||
/// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the
|
||||
/// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied
|
||||
/// into straight alpha at the end. Without any alpha channel the front-most sample wins,
|
||||
|
||||
@@ -886,6 +886,106 @@ fn inconsistent_count_table_errors() {
|
||||
assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed(_))));
|
||||
}
|
||||
|
||||
/// A two-part file made from the single-part deep file `deep` and a 1×1 flat scan-line part,
|
||||
/// the deep part first when `deep_first`: each header gains `name` and `chunkCount`, the
|
||||
/// version field the multipart bit, and each chunk its part number. The offset tables follow
|
||||
/// the headers in header order, then the chunks. Returns the file, where the first table
|
||||
/// starts, where the second table starts and where the last table ends.
|
||||
fn two_part(deep: &[u8], deep_first: bool) -> (Vec<u8>, usize, usize, usize) {
|
||||
let end = header_end_of(deep);
|
||||
let first = u64::from_le_bytes(deep[end..end + 8].try_into().expect("8")) as usize;
|
||||
let offsets: Vec<usize> =
|
||||
(0..(first - end) / 8).map(|i| u64::from_le_bytes(deep[end + 8 * i..end + 8 * i + 8].try_into().expect("8")) as usize).collect();
|
||||
let deep_chunks: Vec<&[u8]> = offsets.iter().enumerate().map(|(i, &o)| &deep[o..offsets.get(i + 1).copied().unwrap_or(deep.len())]).collect();
|
||||
|
||||
let mut deep_header = deep[8..end - 1].to_vec();
|
||||
attr(&mut deep_header, "name", "string", b"deep");
|
||||
attr(&mut deep_header, "chunkCount", "int", &(deep_chunks.len() as i32).to_le_bytes());
|
||||
let mut flat_header = Vec::new();
|
||||
let mut chlist = b"R\0".to_vec();
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes()); // half
|
||||
chlist.extend_from_slice(&[0u8; 4]); // pLinear, reserved
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes());
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes());
|
||||
chlist.push(0);
|
||||
attr(&mut flat_header, "channels", "chlist", &chlist);
|
||||
attr(&mut flat_header, "compression", "compression", &[0]);
|
||||
let dw: Vec<u8> = [0i32; 4].iter().flat_map(|v| v.to_le_bytes()).collect();
|
||||
attr(&mut flat_header, "dataWindow", "box2i", &dw);
|
||||
attr(&mut flat_header, "displayWindow", "box2i", &dw);
|
||||
attr(&mut flat_header, "lineOrder", "lineOrder", &[0]);
|
||||
attr(&mut flat_header, "pixelAspectRatio", "float", &1f32.to_le_bytes());
|
||||
attr(&mut flat_header, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat());
|
||||
attr(&mut flat_header, "screenWindowWidth", "float", &1f32.to_le_bytes());
|
||||
attr(&mut flat_header, "name", "string", b"flat");
|
||||
attr(&mut flat_header, "type", "string", b"scanlineimage");
|
||||
attr(&mut flat_header, "chunkCount", "int", &1i32.to_le_bytes());
|
||||
// The flat part's one chunk: y, the data size, one half sample.
|
||||
let mut flat_chunk = 0i32.to_le_bytes().to_vec();
|
||||
flat_chunk.extend_from_slice(&2i32.to_le_bytes());
|
||||
flat_chunk.extend_from_slice(&f16::from_f32(0.5).to_le_bytes());
|
||||
let flat_chunks: Vec<&[u8]> = vec![&flat_chunk];
|
||||
|
||||
let parts: [(&[u8], &[&[u8]]); 2] = if deep_first {
|
||||
[(&deep_header, &deep_chunks), (&flat_header, &flat_chunks)]
|
||||
} else {
|
||||
[(&flat_header, &flat_chunks), (&deep_header, &deep_chunks)]
|
||||
};
|
||||
let mut out = deep[..4].to_vec();
|
||||
out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes());
|
||||
for (header, _) in &parts {
|
||||
out.extend_from_slice(header);
|
||||
out.push(0); // end of this header
|
||||
}
|
||||
out.push(0); // end of the headers
|
||||
let first_table = out.len();
|
||||
let second_table = first_table + parts[0].1.len() * 8;
|
||||
let tables_end = second_table + parts[1].1.len() * 8;
|
||||
let mut pos = tables_end;
|
||||
for (_, chunks) in &parts {
|
||||
for c in chunks.iter() {
|
||||
out.extend_from_slice(&(pos as u64).to_le_bytes());
|
||||
pos += 4 + c.len();
|
||||
}
|
||||
}
|
||||
for (part, (_, chunks)) in parts.iter().enumerate() {
|
||||
for c in chunks.iter() {
|
||||
out.extend_from_slice(&(part as i32).to_le_bytes());
|
||||
out.extend_from_slice(c);
|
||||
}
|
||||
}
|
||||
(out, first_table, second_table, tables_end)
|
||||
}
|
||||
|
||||
/// The deep part of a two-part file decodes as the single-part file does, first or second.
|
||||
/// Its chunks start after every part's offset table: a chunk offset into the other part's
|
||||
/// table, or one byte before the end of the last table, is refused as table data, and a file
|
||||
/// that ends inside the last table is refused even when the deep part's own table is whole.
|
||||
#[test]
|
||||
fn multipart_deep_chunks_start_after_every_offset_table() {
|
||||
let single = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba);
|
||||
let want = decode_deep_exr(&single, &Limits::none()).expect("single part");
|
||||
for deep_first in [true, false] {
|
||||
let (bytes, ..) = two_part(&single, deep_first);
|
||||
let deep = decode_deep_exr(&bytes, &Limits::none()).expect("two parts");
|
||||
assert_eq!(deep.counts, want.counts);
|
||||
assert_eq!(deep.channel("Z").expect("Z").samples, want.channel("Z").expect("Z").samples);
|
||||
}
|
||||
|
||||
let refused = |bytes: &[u8], what: &str| match decode_deep_exr(bytes, &Limits::none()) {
|
||||
Err(e @ CodecError::Malformed(_)) => assert!(e.to_string().contains(what), "{e}"),
|
||||
Err(e) => panic!("{e}, want an error about {what}"),
|
||||
Ok(_) => panic!("decoded, want an error about {what}"),
|
||||
};
|
||||
let (bytes, first_table, second_table, tables_end) = two_part(&single, true);
|
||||
for off in [second_table, tables_end - 1] {
|
||||
let mut into_table = bytes.clone();
|
||||
into_table[first_table..first_table + 8].copy_from_slice(&(off as u64).to_le_bytes());
|
||||
refused(&into_table, "outside the chunk area");
|
||||
}
|
||||
refused(&bytes[..tables_end - 4], "past the end of the file");
|
||||
}
|
||||
|
||||
/// `gen_deep` for a cropped render: the data window sits at `(dwx, dwy)` inside a larger
|
||||
/// display window of `sw × sh` (the stage the renderer composed onto). Scanline chunk Y
|
||||
/// coordinates are absolute display rows, so they carry the data-window offset.
|
||||
|
||||
@@ -304,19 +304,14 @@ pub(crate) fn decode_deep(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Res
|
||||
let bytes_per_sample: usize = exr_types.iter().map(|t| sample_bytes(*t)).sum();
|
||||
let multipart = meta.requirements.has_multiple_layers;
|
||||
|
||||
// The offset tables follow the headers: `chunk_count` u64 per header, in header order.
|
||||
// Our part's table starts after the tables of all earlier parts.
|
||||
let mut table_pos = header_end(bytes)?;
|
||||
for h_ in meta.headers.iter().take(header_index) {
|
||||
table_pos = table_pos.saturating_add((h_.chunk_count.min(1 << 24) as u64).saturating_mul(8));
|
||||
}
|
||||
// A chunk offset before the end of the last offset table points into table data.
|
||||
let (table_pos, tables_end) = offset_tables(meta, bytes, header_index)?;
|
||||
let mut cur = Cursor::new(bytes);
|
||||
seek_to(&mut cur, table_pos)?;
|
||||
let mut offsets = Vec::with_capacity(header.chunk_count.min(1 << 20));
|
||||
for _ in 0..header.chunk_count {
|
||||
offsets.push(u64(&mut cur)?);
|
||||
}
|
||||
let tables_end = cur.position();
|
||||
|
||||
// Block geometry.
|
||||
let (tiles_x, tile_w, tile_h, slots) = match &header.blocks {
|
||||
@@ -565,6 +560,26 @@ fn header_end(bytes: &[u8]) -> Result<u64, CodecError> {
|
||||
Ok(cur.position())
|
||||
}
|
||||
|
||||
/// Where part `header_index`'s offset table starts and where the last part's table ends. The
|
||||
/// tables follow the headers, `chunk_count` u64 per header in header order, and the chunks
|
||||
/// follow the last table. Sizes that overflow, or tables that run past the end of the file,
|
||||
/// are errors.
|
||||
fn offset_tables(meta: &MetaData, bytes: &[u8], header_index: usize) -> Result<(u64, u64), CodecError> {
|
||||
let mut table_pos = header_end(bytes)?;
|
||||
let mut tables_end = table_pos;
|
||||
for (i, h) in meta.headers.iter().enumerate() {
|
||||
let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8));
|
||||
tables_end = len.and_then(|len| tables_end.checked_add(len)).ok_or_else(|| err("the offset tables are too large"))?;
|
||||
if i < header_index {
|
||||
table_pos = tables_end;
|
||||
}
|
||||
}
|
||||
if tables_end > bytes.len() as u64 {
|
||||
return Err(err(format!("the offset tables end at byte {tables_end}, past the end of the file")));
|
||||
}
|
||||
Ok((table_pos, tables_end))
|
||||
}
|
||||
|
||||
/// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the
|
||||
/// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied
|
||||
/// into straight alpha at the end. Without any alpha channel the front-most sample wins,
|
||||
|
||||
@@ -799,6 +799,106 @@ fn inconsistent_count_table_errors() {
|
||||
assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed { .. })));
|
||||
}
|
||||
|
||||
/// A two-part file made from the single-part deep file `deep` and a 1×1 flat scan-line part,
|
||||
/// the deep part first when `deep_first`: each header gains `name` and `chunkCount`, the
|
||||
/// version field the multipart bit, and each chunk its part number. The offset tables follow
|
||||
/// the headers in header order, then the chunks. Returns the file, where the first table
|
||||
/// starts, where the second table starts and where the last table ends.
|
||||
fn two_part(deep: &[u8], deep_first: bool) -> (Vec<u8>, usize, usize, usize) {
|
||||
let end = header_end_of(deep);
|
||||
let first = u64::from_le_bytes(deep[end..end + 8].try_into().expect("8")) as usize;
|
||||
let offsets: Vec<usize> =
|
||||
(0..(first - end) / 8).map(|i| u64::from_le_bytes(deep[end + 8 * i..end + 8 * i + 8].try_into().expect("8")) as usize).collect();
|
||||
let deep_chunks: Vec<&[u8]> = offsets.iter().enumerate().map(|(i, &o)| &deep[o..offsets.get(i + 1).copied().unwrap_or(deep.len())]).collect();
|
||||
|
||||
let mut deep_header = deep[8..end - 1].to_vec();
|
||||
attr(&mut deep_header, "name", "string", b"deep");
|
||||
attr(&mut deep_header, "chunkCount", "int", &(deep_chunks.len() as i32).to_le_bytes());
|
||||
let mut flat_header = Vec::new();
|
||||
let mut chlist = b"R\0".to_vec();
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes()); // half
|
||||
chlist.extend_from_slice(&[0u8; 4]); // pLinear, reserved
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes());
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes());
|
||||
chlist.push(0);
|
||||
attr(&mut flat_header, "channels", "chlist", &chlist);
|
||||
attr(&mut flat_header, "compression", "compression", &[0]);
|
||||
let dw: Vec<u8> = [0i32; 4].iter().flat_map(|v| v.to_le_bytes()).collect();
|
||||
attr(&mut flat_header, "dataWindow", "box2i", &dw);
|
||||
attr(&mut flat_header, "displayWindow", "box2i", &dw);
|
||||
attr(&mut flat_header, "lineOrder", "lineOrder", &[0]);
|
||||
attr(&mut flat_header, "pixelAspectRatio", "float", &1f32.to_le_bytes());
|
||||
attr(&mut flat_header, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat());
|
||||
attr(&mut flat_header, "screenWindowWidth", "float", &1f32.to_le_bytes());
|
||||
attr(&mut flat_header, "name", "string", b"flat");
|
||||
attr(&mut flat_header, "type", "string", b"scanlineimage");
|
||||
attr(&mut flat_header, "chunkCount", "int", &1i32.to_le_bytes());
|
||||
// The flat part's one chunk: y, the data size, one half sample.
|
||||
let mut flat_chunk = 0i32.to_le_bytes().to_vec();
|
||||
flat_chunk.extend_from_slice(&2i32.to_le_bytes());
|
||||
flat_chunk.extend_from_slice(&f16::from_f32(0.5).to_le_bytes());
|
||||
let flat_chunks: Vec<&[u8]> = vec![&flat_chunk];
|
||||
|
||||
let parts: [(&[u8], &[&[u8]]); 2] = if deep_first {
|
||||
[(&deep_header, &deep_chunks), (&flat_header, &flat_chunks)]
|
||||
} else {
|
||||
[(&flat_header, &flat_chunks), (&deep_header, &deep_chunks)]
|
||||
};
|
||||
let mut out = deep[..4].to_vec();
|
||||
out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes());
|
||||
for (header, _) in &parts {
|
||||
out.extend_from_slice(header);
|
||||
out.push(0); // end of this header
|
||||
}
|
||||
out.push(0); // end of the headers
|
||||
let first_table = out.len();
|
||||
let second_table = first_table + parts[0].1.len() * 8;
|
||||
let tables_end = second_table + parts[1].1.len() * 8;
|
||||
let mut pos = tables_end;
|
||||
for (_, chunks) in &parts {
|
||||
for c in chunks.iter() {
|
||||
out.extend_from_slice(&(pos as u64).to_le_bytes());
|
||||
pos += 4 + c.len();
|
||||
}
|
||||
}
|
||||
for (part, (_, chunks)) in parts.iter().enumerate() {
|
||||
for c in chunks.iter() {
|
||||
out.extend_from_slice(&(part as i32).to_le_bytes());
|
||||
out.extend_from_slice(c);
|
||||
}
|
||||
}
|
||||
(out, first_table, second_table, tables_end)
|
||||
}
|
||||
|
||||
/// The deep part of a two-part file decodes as the single-part file does, first or second.
|
||||
/// Its chunks start after every part's offset table: a chunk offset into the other part's
|
||||
/// table, or one byte before the end of the last table, is refused as table data, and a file
|
||||
/// that ends inside the last table is refused even when the deep part's own table is whole.
|
||||
#[test]
|
||||
fn multipart_deep_chunks_start_after_every_offset_table() {
|
||||
let single = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba);
|
||||
let want = decode_deep_exr(&single, &Limits::none()).expect("single part");
|
||||
for deep_first in [true, false] {
|
||||
let (bytes, ..) = two_part(&single, deep_first);
|
||||
let deep = decode_deep_exr(&bytes, &Limits::none()).expect("two parts");
|
||||
assert_eq!(deep.counts, want.counts);
|
||||
assert_eq!(deep.channel("Z").expect("Z").samples, want.channel("Z").expect("Z").samples);
|
||||
}
|
||||
|
||||
let refused = |bytes: &[u8], what: &str| match decode_deep_exr(bytes, &Limits::none()) {
|
||||
Err(CodecError::Malformed { message, .. }) => assert!(message.contains(what), "{message}"),
|
||||
Err(e) => panic!("{e}, want an error about {what}"),
|
||||
Ok(_) => panic!("decoded, want an error about {what}"),
|
||||
};
|
||||
let (bytes, first_table, second_table, tables_end) = two_part(&single, true);
|
||||
for off in [second_table, tables_end - 1] {
|
||||
let mut into_table = bytes.clone();
|
||||
into_table[first_table..first_table + 8].copy_from_slice(&(off as u64).to_le_bytes());
|
||||
refused(&into_table, "outside the chunk area");
|
||||
}
|
||||
refused(&bytes[..tables_end - 4], "past the end of the file");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Real reference files: the deep test images of the OpenEXR repository at commit
|
||||
// `69b2604fc76e370615438bdc8d2cd95b9349c12e`, read from `corpus/exr/` at the repo root with the
|
||||
|
||||
Reference in New Issue
Block a user