.cdr import: read the chunks of a style list before version 7

Before version 7 a LIST stlt holds chunks, not the style table, but the
index left every such list unsplit, so fills and outlines kept there
were not found. The index now splits it by the drawing's version, the
one its first vrsn chunk names, once all of the file's own chunks are
read.
This commit is contained in:
dotCooCoo
2026-11-12 14:27:57 -08:00
parent 75ac55b32a
commit fdbdbe8696
3 changed files with 77 additions and 18 deletions
+6 -16
View File
@@ -62,7 +62,7 @@ const MAX_CLIP_DEPTH: usize = 64;
pub(crate) fn drawing(ix: &Index, budget: &mut Budget, warn: &mut Vec<Warning>) -> Result<Drawing, Error> {
let root = ix.root().ok_or(Error::NotCdr)?;
let version = version(ix, root);
let version = ix.version();
// A template whose `vrsn` chunk doesn't name a version read (R28).
if version == 0 {
return Err(Error::NotCdr);
@@ -133,20 +133,6 @@ pub(crate) fn drawing(ix: &Index, budget: &mut Budget, warn: &mut Vec<Warning>)
Ok(Drawing { version, page_size, pages, master, guides, warnings: std::mem::take(cx.warn) })
}
/// The version: the `vrsn` chunk's when it names one from 3 to 27, else the form type's (R1, R2).
/// A template's form type names no version, so its `vrsn` chunk decides whatever it names (R28).
/// Letters count differently from the version 24 update on (R1), so when both name a version
/// after 27 the `vrsn` chunk's is the one refused.
fn version(ix: &Index, root: &Chunk) -> u16 {
let vrsn = ix.children(root).iter().find(|c| &c.id == b"vrsn").and_then(|c| Rd::new(ix.data(c)).u16());
match vrsn {
Some(v) if (layout::OLDEST_RIFF..=layout::NEWEST).contains(&v) => v,
Some(v) if ix.form_version == 0 => v,
Some(v) if v > layout::NEWEST && ix.form_version > layout::NEWEST => v,
_ => ix.form_version,
}
}
#[derive(Default)]
struct Found<'i> {
fills: Vec<&'i Chunk>,
@@ -168,7 +154,11 @@ fn collect<'i>(ix: &'i Index, root: &'i Chunk, found: &mut Found<'i>) {
b"outl" => found.outlines.push(c),
b"mcfg" => found.mcfg = found.mcfg.or(Some(c)),
b"guid" => found.guid = found.guid.or(Some(c)),
_ if c.is_list(b"stlt") => found.stlt = found.stlt.or(Some(c)),
// Before version 7 the list holds chunks, which are looked through.
_ if c.is_list(b"stlt") => {
found.stlt = found.stlt.or(Some(c));
stack.extend(ix.children(c).iter().rev());
}
_ if c.is_list(b"clpt") => found.clpt.push(c),
_ if c.is_list(b"page") => found.pages.push(c),
_ => stack.extend(ix.children(c).iter().rev()),
+29 -2
View File
@@ -7,6 +7,7 @@ use std::collections::BTreeMap;
use std::io::Read as _;
use crate::bytes::{Rd, u32_at};
use crate::layout;
use crate::limits::Budget;
use crate::package::Streams;
use crate::{Error, Warning};
@@ -58,6 +59,9 @@ pub(crate) struct Index<'a> {
leaves: Cell<BTreeMap<(usize, usize), usize>>,
/// Bytes that redirects to bytes another chunk already took may still read.
shared_left: Cell<usize>,
/// The first `vrsn` chunk among the file's own chunks, once seen, and the version it names
/// (`None` when it can't be read) (R2).
vrsn: Option<Option<u16>>,
}
/// A list still to be split into its children.
@@ -139,6 +143,7 @@ impl<'a> Index<'a> {
lists: Cell::default(),
leaves: Cell::default(),
shared_left: Cell::new(budget.limits.member),
vrsn: None,
};
ix.chunks.push(Chunk { id: *b"RIFF", form, src: 0, start: 12, len: 0, first: 0, count: 0 });
let mut queue = vec![Pending { entry: 0, src: 0, start: 12, end, pool: None, depth: 0 }];
@@ -195,6 +200,19 @@ impl<'a> Index<'a> {
self.chunks.first()
}
/// The version: the `vrsn` chunk's when it names one from 3 to 27, else the form type's (R1,
/// R2). A template's form type names no version, so its `vrsn` chunk decides whatever it names
/// (R28). Letters count differently from the version 24 update on (R1), so when both name a
/// version after 27 the `vrsn` chunk's is the one refused.
pub(crate) fn version(&self) -> u16 {
match self.vrsn.flatten() {
Some(v) if (layout::OLDEST_RIFF..=layout::NEWEST).contains(&v) => v,
Some(v) if self.form_version == 0 => v,
Some(v) if v > layout::NEWEST && self.form_version > layout::NEWEST => v,
_ => self.form_version,
}
}
/// The warnings for data streams that were left out.
pub(crate) fn stream_warnings(&self, warn: &mut Vec<Warning>) {
if let Some(s) = &self.streams {
@@ -228,8 +246,11 @@ impl<'a> Index<'a> {
continue;
}
};
// A `LIST stlt` holds the style tables, not chunks (R4); its body is read whole (R45).
if chunk.id == LIST && chunk.form != *b"stlt" {
if p.entry == 0 && chunk.id == *b"vrsn" && self.vrsn.is_none() {
let at = chunk.start as usize;
self.vrsn = Some(self.span(chunk.src, at, at + chunk.len as usize).and_then(|b| Rd::new(b).u16()));
}
if chunk.id == LIST {
lists.push(self.chunks.len());
}
self.chunks.push(chunk);
@@ -241,6 +262,12 @@ impl<'a> Index<'a> {
}
for i in lists {
let Some(c) = self.chunks.get(i).copied() else { continue };
// From version 7 a `LIST stlt` holds the style tables, not chunks (R4); its body is read
// whole (R45). Before version 7 it holds chunks. The file's own chunks, its `vrsn`
// among them, are all read before this.
if c.form == *b"stlt" && self.version() >= 700 {
continue;
}
if p.depth + 1 > budget.limits.depth {
push(warn, Warning::TooDeep);
continue;
+42
View File
@@ -1136,6 +1136,28 @@ fn a_style_table_list_is_not_read_as_chunks() {
assert!(d.page_size.is_some_and(|(w, h)| (w - 100.0).abs() < 0.01 && (h - 50.0).abs() < 0.01));
}
/// Before version 7 a `LIST stlt` holds chunks (R4), which are read like the document's others.
#[test]
fn a_style_list_before_version_7_is_read_as_chunks() {
for version in [400, 500, 600] {
let v = V(version);
let red = if version < 500 { synth::color4(1, [0, 100, 100, 0]) } else { synth::color(5, [0, 0, 255, 0]) };
let styles = list(b"stlt", vec![list(b"filt", vec![chunk(b"fill", v.fill(1, &red))])]);
let rect = synth::object(v, 1, &[(20, 1u32.to_le_bytes().to_vec()), (30, v.rect(10.0, 10.0, 0.0))], None);
let page = synth::page(false, vec![synth::layer(v, [0, 0, 0, 0x98], "L", vec![rect])]);
let content = vec![list(b"doc ", vec![styles]), page];
let d = read(&synth::file_of(version, content.clone()), Limits::default()).unwrap();
let s = shapes(&d.pages[0].layers[0].objects);
assert!(s[0].fill.is_some(), "{version}");
assert!(d.warnings.is_empty(), "{version}: {:?}", d.warnings);
// A template, whose form type names no version, by its `vrsn` chunk.
let mut template = synth::file_of(version, content);
template[8..12].copy_from_slice(b"CDTA");
let d = read(&template, Limits::default()).unwrap();
assert!(shapes(&d.pages[0].layers[0].objects)[0].fill.is_some(), "{version}");
}
}
/// The objects of a styled drawing: a rectangle with only its style (argument 200), one whose
/// style leaves the fill and outline to its parent, and one with an outline argument of its own.
fn graphic_style_drawing(version: u16, table: synth::Node) -> Vec<u8> {
@@ -1191,6 +1213,26 @@ fn objects_without_their_own_fill_or_outline_take_their_styles() {
}
}
/// A template whose style table comes before its `vrsn` chunk still reads the table by the
/// version that chunk names.
#[test]
fn a_style_table_before_the_version_chunk_reads_by_that_version() {
let styles = [synth::Style { id: 0x10, parent: 0, fill: 0x100, outline: 0x200 }, synth::Style { id: 0x11, parent: 0x10, fill: 0, outline: 0 }];
let table = synth::stlt(V(1000), &[(0x100, 1)], &[(0x200, 2)], &styles, true);
let mut file = graphic_style_drawing(1000, chunk(b"junk", vec![]));
// The drawing's chunks, the table put first, ahead of `vrsn`.
let body = file.split_off(12);
file.extend(synth::plain(&table));
file.extend(body);
file[8..12].copy_from_slice(b"CDTA");
let len = (file.len() - 8) as u32;
file[4..8].copy_from_slice(&len.to_le_bytes());
let d = read(&file, Limits::default()).unwrap();
let s = shapes(&d.pages[0].layers[0].objects);
assert_eq!(s[0].fill, Some(Color::Rgb([1.0, 0.0, 0.0])));
assert!(d.warnings.is_empty(), "{:?}", d.warnings);
}
/// A style table that doesn't parse to its end is damage when an object needs a style from it;
/// the object is drawn with what it holds itself.
#[test]