From f24eff296329cf96f3fe3ae54833a2a84205738d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 14:11:32 -0800 Subject: [PATCH] PDF: stream length test helper ignores names ending in obj stream_length took the last "obj" substring in the text as the object header, so a stream dictionary name such as /Tag/obj moved the scan past the outer dictionary and lost its /Length. The keyword now counts only at the start of the text or after PDF white space. --- crates/engine/src/cmd/fileio/tests_pdf13.rs | 25 ++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/fileio/tests_pdf13.rs b/crates/engine/src/cmd/fileio/tests_pdf13.rs index f3493555c9..fc386ac09c 100644 --- a/crates/engine/src/cmd/fileio/tests_pdf13.rs +++ b/crates/engine/src/cmd/fileio/tests_pdf13.rs @@ -115,10 +115,16 @@ fn xref_offset(pdf: &[u8], number: usize, generation: usize, startxref: usize) - } /// The `/Length` of the stream dictionary that ends `text`, read from the outermost dictionary -/// after the object's `obj` keyword. An indirect length is read from its object in `pdf`. `None` -/// when it is missing or cannot be read. +/// after the object's `obj` keyword. The keyword counts only at the start of `text` or after white +/// space, so a name such as `/Tag/obj` is not taken for it. An indirect length is read from its +/// object in `pdf`. `None` when it is missing or cannot be read. fn stream_length(text: &[u8], pdf: &[u8]) -> Option { - let from = text.windows(3).rposition(|w| w == b"obj").map_or(0, |at| at + 3); + let from = text + .windows(3) + .enumerate() + .rev() + .find(|&(at, w)| w == b"obj" && (at == 0 || matches!(text[at - 1], b'\0' | b'\t' | b'\n' | b'\x0c' | b'\r' | b' '))) + .map_or(0, |(at, _)| at + 3); let dict = &text[from..]; let mut i = dict.windows(2).position(|w| w == b"<<")? + 2; let mut depth = 1usize; @@ -404,3 +410,16 @@ fn the_transparency_check_skips_strings_and_metadata() { assert_eq!(message, Some("stream /Length is inconsistent with endstream"), "length {length}"); } } + +/// A name that ends in `obj`, such as `/Tag/obj`, is not the object's `obj` keyword, so the +/// stream's `/Length` is still read from the outer dictionary. +#[test] +fn the_stream_length_skips_names_that_end_in_obj() { + let text = b"1 0 obj\n<>\nstream\n"; + assert_eq!(stream_length(text, text), Some(5)); + // NUL is PDF white space, so it may separate the generation number from `obj`. + let text = b"1 0 obj\n<>\nstream\nabc\nendstream\nendobj\n2 0\0obj\n<>\nstream\n"; + assert_eq!(stream_length(text, text), Some(5)); + let pdf = b"1 0 obj\n<>\nstream\nendstream /BM\nendstream\nendobj\n"; + assert_eq!(transparency(pdf), Vec::<&str>::new()); +}