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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0175FWj4xFhMhSH3UpMU8DSC
This commit is contained in:
mtlcl410
2026-10-31 14:13:47 +08:00
co-authored by Claude Opus 5.5
parent 5f757262f9
commit 00c4a86ac3
3 changed files with 34 additions and 5 deletions
+2
View File
@@ -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};
+23 -5
View File
@@ -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<usize> = 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<LayerRow> {
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<ObjectId, usize>,
seen: &mut HashSet<ObjectId>,
labels: &mut HashMap<ObjectId, String>,
out: &mut Vec<LayerRow>,
) {
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::<String>()
};
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);
}
}
+9
View File
@@ -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