Skip to content

Simplify stream boundary recovery - #561

Merged
Velli20 merged 1 commit into
mainfrom
simplify-stream-recovery
Oct 11, 2026
Merged

Velli20 merged 1 commit into
mainfrom
simplify-stream-recovery

Conversation

@Velli20

@Velli20 Velli20 commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Collapse the three stream recovery entry points into one recover_stream_range(length: Option<usize>). parse_stream_recovering and parse_stream_recovering_direct_length become thin wrappers that differ only in how /Length is read. skip_stream_recovering is removed; the linear object scanner calls the core method directly, so it no longer copies the stream.
  • Replace the code that moved the parser and restored it on failure with pure functions over the input, built around a small StreamTerminator { start, end } type.
  • Speed: with a stale /Length hint, the terminator scan stops at the first valid candidate past the declared boundary instead of scanning to the end of the file, since every later candidate lies farther away. Previously, recovering a document whose streams had wrong lengths took time quadratic in the file size.
  • Simplify line-ending trimming to a suffix strip, drop a dead special case for a match at the very start of the data, and match the truncated endstrea with one optional m.

endstream stays required. endobj marks where the object ends, but only endstream marks where the data ends, and requiring both is the main guard against matches inside binary stream data.

Apart from the speed fix, parsing gives the same results. stream_recovery.rs goes from 334 to 233 lines.

Test plan

  • cargo test -p pdf-parser -p pdf-document
  • cargo clippy: no new warnings compared with the base commit
  • Read-only conformance on the PDFium corpus: 840 documents, 0 read worse, 0 read better than the base commit
  • Read-only check of the pdf.js corpus (left to CI; needs a local PDFium library)

🤖 Generated with Claude Code

Collapse the three recovery entry points into one recover_stream_range
method with thin wrappers that differ only in how /Length is read, and
replace the parser save/restore dance with pure functions over the input.

With a stale /Length hint, the terminator scan now stops at the first
valid candidate past the declared boundary instead of scanning to the end
of the file, since every later candidate lies farther away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Velli20
Velli20 merged commit 519b4eb into main Oct 11, 2026
10 checks passed
@Velli20
Velli20 deleted the simplify-stream-recovery branch October 11, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant