#[non_exhaustive]pub enum StreamLengthPolicy {
Strict,
RecoverFromEndstream,
}Expand description
How a stream whose /Length disagrees with the file should be handled.
This is the one deliberate strictness knob on the parser, and it exists
because §7.3.8.2’s own definition of /Length is phrased in terms of
the endstream keyword: the value “shall be the number of bytes from
the beginning of the line following the keyword stream to the last
byte just before the keyword endstream”. When the stored number and
the keyword disagree, the file contains two statements of the same
fact and one of them is wrong. Which one a reader believes is a policy
choice, not a spec choice — so pdfcer makes it an explicit parameter
rather than a hidden default.
The default is and must stay StreamLengthPolicy::Strict: on a
cleanly-loading file the stored /Length is authoritative, a
disagreement is real damage the operator should hear about (§7.3.8.2
calls it “an error”), and silently re-deriving extents would put a
guessed span into the writer’s byte-identical re-emission path and
break the round-trip/minimal-diff invariant (ARCHITECTURE.md §5).
StreamLengthPolicy::RecoverFromEndstream is reachable only from
the rebuild-by-scan recovery path (crate::recover and the recovered
branch of crate::document::Document::from_bytes), where the file’s
cross-reference machinery has already been proven unparseable, no
byte-identical re-emission is on the table (a recovered document
refuses incremental save and always normalizes), and refusing an object
costs the operator the whole document.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Strict
Believe /Length. A disagreement with endstream is
ParseErrorKind::StreamExtentMismatch; a missing/invalid
/Length is ParseErrorKind::BadStreamLength. The default, and
the only policy the clean load path ever uses.
RecoverFromEndstream
Believe endstream. When (and only when) the stored /Length
cannot be used — absent, non-integer, unresolvable, past the end of
the buffer, or simply not landing on endstream — re-derive the
data extent by scanning forward from the start of the data for the
endstream keyword, then backing off the one end-of-line marker
§7.3.8.1 says “should” precede it. Every such repair is counted in
Parser::stream_lengths_recovered so it can be disclosed (R20,
fuzzy-never-sneaky) rather than silently absorbed.