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.
This commit is contained in:
dotCooCoo committed 2026-12-02 14:11:32 -08:00
1 parent 937817609d
commit f24eff2963
1 file changed
+22 -3
+22 -3
View File
@@ -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<usize> {
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<</Length 5/Tag/obj>>\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<</Length 9>>\nstream\nabc\nendstream\nendobj\n2 0\0obj\n<</Length 5>>\nstream\n";
assert_eq!(stream_length(text, text), Some(5));
let pdf = b"1 0 obj\n<</Length 13/Tag/obj>>\nstream\nendstream /BM\nendstream\nendobj\n";
assert_eq!(transparency(pdf), Vec::<&str>::new());
}