Skip trailing null bytes when searching for EOF marker#586
Open
Skip trailing null bytes when searching for EOF marker#586
Conversation
Some PDF generators (e.g. Atos/Fonet) append thousands of null bytes after %%EOF. Since null bytes are not line terminators, they prevented the EOF marker from being found within the trailing byte scan window. This change scans backwards from the end of the file to skip any trailing null bytes before reading the final bytes to locate the %%EOF marker and xref offset. Fixes yob#585
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.
Problem
PDFs generated by some tools (e.g. Atos/Fonet) append thousands of null bytes (
\x00) after%%EOF. Since null bytes are not line terminators, they are not split by the line parser, preventing the EOF marker from being found within the trailing byte scan window (TRAILING_BYTECOUNT = 5000).This causes
PDF::Reader::MalformedPDFError: PDF does not contain EOF marker.Solution
Scan backwards from the end of the file to skip any trailing null bytes before reading the final bytes to locate the
%%EOFmarker and xref offset. This handles any amount of trailing null bytes regardless ofTRAILING_BYTECOUNT.The existing
NULL_BYTEconstant defined inBufferis reused.Test
Added
trailing_null_bytes.pdf(extended_eof.pdf + 6000 null bytes) and a spec that verifies the correct xref offset (145) is still found.Fixes #585