From 00c4a86ac335836f6cf2efcfdab6425a04698d46 Mon Sep 17 00:00:00 2001 From: mtlcl410 <2368091+mtlcl410@users.noreply.github.com> Date: Sun, 25 Oct 2026 01:23:18 +0800 Subject: [PATCH] M2: an indirect layer label in /Order is decoded once A nested /Order array may start with a text string label (ISO 32000-2 Table 99). One indirect string can head any number of direct arrays, and each row decoded the whole string before cutting it to 256 characters, so the work grew with rows times string length. Keep each indirect label decoded once, by object number. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0175FWj4xFhMhSH3UpMU8DSC --- crates/render/src/inspect.rs | 2 ++ crates/render/src/inspect/resources.rs | 28 +++++++++++++++++++++----- crates/render/src/inspect/tests.rs | 9 +++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/crates/render/src/inspect.rs b/crates/render/src/inspect.rs index 63b40f7fdc..9a9e3c015b 100644 --- a/crates/render/src/inspect.rs +++ b/crates/render/src/inspect.rs @@ -41,6 +41,8 @@ pub use model::{ }; #[cfg(test)] use navigation::{OUTLINE_DEPTH, OUTLINE_WINDOW}; +#[cfg(test)] +use resources::LABEL_DECODES; pub use resources::attachment_data; #[cfg(test)] use resources::{MAX_LAYER_LABEL_CHARS, MAX_LAYER_ORDER_ROWS, xfa_config_dynamic_render, xfa_packet_text}; diff --git a/crates/render/src/inspect/resources.rs b/crates/render/src/inspect/resources.rs index 7085ccb85a..27b46c78d4 100644 --- a/crates/render/src/inspect/resources.rs +++ b/crates/render/src/inspect/resources.rs @@ -25,6 +25,12 @@ pub(super) const MAX_LAYER_ORDER_ROWS: usize = 4096; const MAX_LAYER_ORDER_DEPTH: usize = 32; pub(super) const MAX_LAYER_LABEL_CHARS: usize = 256; +#[cfg(test)] +thread_local! { + /// Layer labels decoded by [`Inspector::layer_order`] on this thread. + pub(super) static LABEL_DECODES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + impl<'a> Inspector<'a> { // ── object helpers ────────────────────────────────────────────────────────────────────── @@ -183,8 +189,9 @@ impl<'a> Inspector<'a> { /// a nested array may start with a text string, a label that is not a group. Groups not /// listed aren't presented, and `/Order` defaults to the empty array in the default /// configuration, so a document without one presents no layers. An entry that is neither a - /// group in `/OCGs` nor an array is skipped. Bounded by `MAX_LAYER_ORDER_*`, and an indirect - /// array is read once, so a cycle ends. + /// group in `/OCGs` nor an array is skipped. Bounded by `MAX_LAYER_ORDER_*`, an indirect + /// array is read once, so a cycle ends, and an indirect label is decoded once, however many + /// arrays it heads. pub(super) fn layer_order(&self, catalog: &Dictionary, layers: &[Layer]) -> Vec { let mut out = Vec::new(); let Some(config) = catalog.get(b"OCProperties").ok().and_then(|o| self.dict(o)).and_then(|p| p.get(b"D").ok()).and_then(|o| self.dict(o)) @@ -198,17 +205,20 @@ impl<'a> Inspector<'a> { seen.insert(*id); } if let Ok(items) = self.resolve(order).as_array() { - self.layer_order_rows(items, 0, &index, &mut seen, &mut out); + self.layer_order_rows(items, 0, &index, &mut seen, &mut HashMap::new(), &mut out); } out } + /// `labels` holds each indirect label already decoded: one string object can head any + /// number of direct arrays, and each decode costs its full length before the cut. fn layer_order_rows( &self, items: &'a [Object], depth: usize, index: &HashMap, seen: &mut HashSet, + labels: &mut HashMap, out: &mut Vec, ) { if depth > MAX_LAYER_ORDER_DEPTH { @@ -231,13 +241,21 @@ impl<'a> Inspector<'a> { let Ok(nested) = self.resolve(item).as_array() else { continue }; let rest = match nested.split_first() { Some((first, rest)) if let Object::String(bytes, _) = self.resolve(first) => { - let label: String = text_string(bytes).chars().take(MAX_LAYER_LABEL_CHARS).collect(); + let decode = || { + #[cfg(test)] + LABEL_DECODES.with(|n| n.set(n.get().saturating_add(1))); + text_string(bytes).chars().take(MAX_LAYER_LABEL_CHARS).collect::() + }; + let label = match first { + Object::Reference(id) => labels.entry(*id).or_insert_with(decode).clone(), + _ => decode(), + }; out.push(LayerRow { depth, item: LayerItem::Label(label) }); rest } _ => nested.as_slice(), }; - self.layer_order_rows(rest, depth.saturating_add(1), index, seen, out); + self.layer_order_rows(rest, depth.saturating_add(1), index, seen, labels, out); } } diff --git a/crates/render/src/inspect/tests.rs b/crates/render/src/inspect/tests.rs index 1fc93170df..310ff171ef 100644 --- a/crates/render/src/inspect/tests.rs +++ b/crates/render/src/inspect/tests.rs @@ -821,6 +821,15 @@ trailer << /Root 1 0 R >> // Many rows stop at the cap. let many = format!("/Order [{}]", "5 0 R ".repeat(MAX_LAYER_ORDER_ROWS + 10)); assert_eq!(rows(&many, "").len(), MAX_LAYER_ORDER_ROWS); + // One long indirect label heading many direct arrays is decoded once, and cut short in + // every row. + let shared = format!("/Order [{}]", "[13 0 R 5 0 R] ".repeat(100)); + let label = format!("13 0 obj ({}) endobj", "y".repeat(10_000)); + let before = LABEL_DECODES.with(std::cell::Cell::get); + let got = rows(&shared, &label); + assert_eq!(LABEL_DECODES.with(std::cell::Cell::get) - before, 1); + assert_eq!(got.len(), 200); + assert!(got.iter().step_by(2).all(|s| s.len() == MAX_LAYER_LABEL_CHARS + 2), "{:?}", got.first()); } /// ISO 32000-2 §8.11.4.4: the View auto state (`/AS`) decides the initial state of the groups