From c2789fff2ae23cb3c981d532b8e9424486563d2b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 12 Nov 2026 18:30:51 -0800 Subject: [PATCH] CDR import: report id arguments cut short and resolve styles in linear time A container, fill, outline or style argument too short for the record id it names now adds the damage warning. Before, a container argument of fewer than four bytes imported the container alone and dropped the objects it clips without a warning. Resolving the style table's parent chains now removes each walk's styles from the visited set one by one instead of clearing the set, whose cost grew with its capacity after one long chain. --- crates/cdr/src/build.rs | 17 +++++++++++++---- crates/cdr/src/styles.rs | 29 +++++++++++++++++++++++------ crates/cdr/tests/it/read.rs | 21 +++++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/crates/cdr/src/build.rs b/crates/cdr/src/build.rs index c2766cad76..df9f11d15d 100644 --- a/crates/cdr/src/build.rs +++ b/crates/cdr/src/build.rs @@ -451,20 +451,20 @@ impl<'a, 'b> Ctx<'a, 'b> { }), }; // Without a fill or outline argument of its own, an object takes its style's (R45). - let style = loda.arg(ARG_STYLE).and_then(id_of); + let style = loda.arg(ARG_STYLE).and_then(|b| self.id(b)); let fill_id = match loda.arg(ARG_FILL) { - Some(b) => id_of(b), + Some(b) => self.id(b), None => style.and_then(|s| self.style_entry(s, Styles::fill)), }; let outline_id = match loda.arg(ARG_OUTLINE) { - Some(b) => id_of(b), + Some(b) => self.id(b), None => style.and_then(|s| self.style_entry(s, Styles::outline)), }; let fill = fill_id.and_then(|id| self.fill(id)); let outline = outline_id.and_then(|id| self.outline(id)); let shape = Box::new(Shape { geometry, matrix, fill, outline }); // A container clips the group its argument 8005 names (R46). - Some(match loda.arg(ARG_CONTAINER).and_then(id_of) { + Some(match loda.arg(ARG_CONTAINER).and_then(|b| self.id(b)) { Some(id) => self.clip(shape, id, guides), None => Object::Shape(shape), }) @@ -492,6 +492,15 @@ impl<'a, 'b> Ctx<'a, 'b> { Object::Clip { container, objects } } + /// The record id that argument `b` names; an argument too short for one is damage. + fn id(&mut self, b: &[u8]) -> Option { + let id = id_of(b); + if id.is_none() { + push(self.warn, Warning::Damaged); + } + id + } + /// The fill or outline record that style `id` names; a style table that doesn't parse is /// damage, and the object is drawn without it. fn style_entry(&mut self, id: u32, entry: fn(&Styles, u32) -> Option) -> Option { diff --git a/crates/cdr/src/styles.rs b/crates/cdr/src/styles.rs index b3a83b6c92..e4022563c2 100644 --- a/crates/cdr/src/styles.rs +++ b/crates/cdr/src/styles.rs @@ -93,14 +93,16 @@ fn parse(b: &[u8], version: u16, take: &mut dyn FnMut(usize) -> bool) -> Option< if r.left() != 0 { return None; } - Some(Styles { fills: resolve(&styles, &fills, |s| s.fill), outlines: resolve(&styles, &outlines, |s| s.outline) }) + let mut work = 0; + Some(Styles { fills: resolve(&styles, &fills, |s| s.fill, &mut work), outlines: resolve(&styles, &outlines, |s| s.outline, &mut work) }) } /// For each style, the record of `table` that its own entry names, or the nearest parent's where -/// its entry is 0. Each style is resolved once, so a chain of any length takes time in proportion -/// to the styles; a chain that comes back to a style on it, or reaches a style that isn't there, -/// gives no record. -fn resolve(styles: &HashMap, table: &HashMap, entry: impl Fn(&Style) -> u32) -> HashMap { +/// its entry is 0. Each style is resolved once, and each path forgets only the styles on it, so a +/// chain of any length takes time in proportion to the styles; a chain that comes back to a style +/// on it, or reaches a style that isn't there, gives no record. `work` counts the styles visited +/// and forgotten. +fn resolve(styles: &HashMap, table: &HashMap, entry: impl Fn(&Style) -> u32, work: &mut usize) -> HashMap { // `None`: resolved to no record. let mut done: HashMap> = HashMap::new(); let mut path = Vec::new(); @@ -108,6 +110,7 @@ fn resolve(styles: &HashMap, table: &HashMap, entry: impl for &start in styles.keys() { let mut id = start; let found = loop { + *work += 1; if let Some(&known) = done.get(&id) { break known; } @@ -122,8 +125,9 @@ fn resolve(styles: &HashMap, table: &HashMap, entry: impl e => break table.get(&e).copied(), } }; - on_path.clear(); for id in path.drain(..) { + *work += 1; + on_path.remove(&id); done.insert(id, found); } } @@ -212,6 +216,19 @@ mod tests { assert_eq!((s.fill(500), s.fill(250), s.outline(500)), (Some(7), Some(7), None)); } + /// A loop of 2,000 styles, which every walk into it follows around, leaves the walks of 20,000 + /// other styles no more work than the styles they visit. + #[test] + fn one_long_walk_does_not_slow_the_walks_after_it() { + let mut styles: HashMap = (1..=2000).map(|id| (id, Style { parent: id % 2000 + 1, fill: 0, outline: 0 })).collect(); + styles.extend((10_001..=30_000).map(|id| (id, Style { parent: 0, fill: 0x100, outline: 0 }))); + let table = HashMap::from([(0x100, 7)]); + let mut work = 0; + let fills = resolve(&styles, &table, |s| s.fill, &mut work); + assert_eq!((fills.get(&1), fills.get(&10_001), fills.len()), (None, Some(&7), 20_000)); + assert!(work <= 4 * styles.len(), "{work}"); + } + #[test] fn an_entry_missing_from_its_table_names_no_record() { let styles = [synth::Style { id: 0x10, parent: 0, fill: 0x101, outline: 0x200 }]; diff --git a/crates/cdr/tests/it/read.rs b/crates/cdr/tests/it/read.rs index 1705e14439..c7c5836073 100644 --- a/crates/cdr/tests/it/read.rs +++ b/crates/cdr/tests/it/read.rs @@ -1326,6 +1326,27 @@ fn a_missing_or_repeating_clipped_group_keeps_the_container() { assert_eq!(d.warnings, vec![Warning::TooDeep]); } +/// A container, fill, outline or style argument too short for the id it names is damage: the +/// shape comes in as a plain shape without that id. +#[test] +fn an_id_argument_cut_short_is_damage() { + for version in [700, 1100, 1600] { + let v = V(version); + let group = vec![(0x77, vec![clip_rect(v, 10.0, &[], [0.0, 0.0])])]; + let container = clip_rect(v, 40.0, &[(8005, vec![0x77, 0, 0])], [0.0, 0.0]); + let d = read(&clip_drawing(version, group, vec![container]), Limits::default()).unwrap_or_else(|e| panic!("{version}: {e}")); + assert!(matches!(d.pages[0].layers[0].objects.as_slice(), [Object::Shape(_)]), "{version}: {:?}", d.pages[0].layers[0].objects); + assert_eq!(d.warnings, vec![Warning::Damaged], "{version}"); + for kind in [20, 10, 200] { + let short = synth::object(v, 1, &[(kind, vec![1, 0]), (30, v.rect(10.0, 10.0, 0.0))], None); + let d = read(&clip_drawing(version, vec![], vec![short]), Limits::default()).unwrap_or_else(|e| panic!("{version} {kind}: {e}")); + let s = shapes(&d.pages[0].layers[0].objects); + assert!(s.len() == 1 && s[0].fill.is_none() && s[0].outline.is_none(), "{version} {kind}"); + assert_eq!(d.warnings, vec![Warning::Damaged], "{version} {kind}"); + } + } +} + /// An object with a lens or transparency (argument 8000) comes in opaque, with the lenses /// warning; one without the argument gives no warning. #[test]