Repository navigation
Tokenize Type 4 programs with a byte-level lexer - #607
Merged
Merged
Conversation
Type 4 programs were tokenized by padding braces with spaces on a
lossy UTF-8 copy of the stream and splitting on Unicode whitespace.
A % comment reached the number parser and failed with InvalidNumber,
NUL was not a separator, and the outer { } was never checked.
The parser now takes the program bytes. A lexer skips PDF whitespace
and % comments, emits { and } as their own tokens, and rejects the
other PostScript delimiters. parse checks that the program is one
balanced { } procedure and that nested procedures are only operands of
if and ifelse, tracking nesting on an explicit stack. Literals parse
straight from bytes, including radix (16#FF) and exponent forms, and an
integer outside the 32-bit range reads as a real.
The Type 4 decoder, evaluate_postscript and the bench call parse
directly. The disabled test module that still used the removed
parse_tokens is deleted.
Fixes #590
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The DeviceN and image decoder tests built Type 4 function streams
without the outer { } procedure that a Type 4 program requires, so the
parser now rejects them with MissingProgramProcedure.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conformance readsDocuments that Safe-PDF reads differently from the base commit, compared with PDFium. A read pdf.js corpus1009 documents compared with Regressions
840 documents of the PDFium corpus read as on Details are in the job summaries of the workflow run. Rerun a case locally with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #590.
Summary
%comments.{and}are their own tokens, so no spaces are needed around braces.( ) < > [ ] /) are rejected withUnexpectedDelimiter.parser::parse(&[u8])replacesparse_tokens(&[&str]).{ … }procedure and that nested procedures only appear as operands ofif/ifelse.MissingProgramProcedure,UnbalancedBraces,UnexpectedProcedure,UnexpectedDelimiter. The unusedEmptyBlockStackandTokenIndexOverfloware removed.TryFrom<&[u8]>onValueandOperator).16#FF) are read as unsigned 32-bit values, so16#FFFFFFFFis -1.1e2,.5) parse as reals.evaluate_postscriptand the bench callparsedirectly. There is noString,replaceor tokenVecanywhere.#[cfg(all(test, not(test)))]test module, which still used the removed API, is deleted. The existing tests now wrap their programs in{ }.The issue's side notes needed no changes:
neg,exp,ln,ceilingandroundalready landed in Implement missing Type 4 operators and speed up execution #606.raw_data()is already decoded.Test plan
cargo test -p pdf-postscript -p pdf-functionpasses.cargo clippy -p pdf-postscript -p pdf-function --all-targets -- -D warningsis clean.cargo fmt --checkpasses.cargo bench -p pdf-postscript --no-runbuilds.%comment, NUL/FF separators, radix and exponent literals, and comments before and after the program.(delimiter, and trailing tokens.--all-featureswas not run locally because the Skia download was blocked; CI covers it.🤖 Generated with Claude Code