Skip to content

fix: ASCII85 decoder drops 'z' zero-groups and never signals EOF at ~> marker - #84

Open
chappihappymeal wants to merge 1 commit into
ledongthuc:masterfrom
chappihappymeal:fix-ascii85-decoder
Open

fix: ASCII85 decoder drops 'z' zero-groups and never signals EOF at ~> marker#84
chappihappymeal wants to merge 1 commit into
ledongthuc:masterfrom
chappihappymeal:fix-ascii85-decoder

Conversation

@chappihappymeal

Copy link
Copy Markdown

Fixes #83.

alphaReader (the sanitizing reader in front of encoding/ascii85) had two bugs:

  1. It stripped z — the standard ASCII85 shorthand for an all-zero 4-byte group (PDF 32000-1:2008, §7.4.3), emitted by Ghostscript, Adobe tools, and Go's own ascii85.Encode. Every z silently dropped 4 bytes from the decoded stream: with [/ASCII85Decode /FlateDecode] this corrupted the flate stream (GetPlainText()malformed PDF: unexpected EOF, Page.Content() → panic escaping to the caller); with plain /ASCII85Decode the corruption was silent.
  2. On the ~> end-of-data marker it only breaked out of the sanitizing loop and returned n, nil — it never signaled io.EOF, never truncated at the marker, and kept feeding post-marker bytes from later Read chunks to the decoder.

The rewritten Read filters in place (no per-call allocation), passes z through, and returns the sanitized byte count with io.EOF as soon as ~ is seen — per spec ~ can only start the EOD marker, which also handles a marker split across two reads for free. After that every Read returns 0, io.EOF without touching the underlying reader.

Tests: 6 unit tests for alphaReader (zero-group, EOF at marker, marker split across reads, garbage after marker, line-wrapped input, reads after EOF) and 2 integration tests with minimal synthetic PDFs in testdata/ — one with a z group inside a text string (plain /ASCII85Decode), one with an Exstream-style [/ASCII85Decode /FlateDecode] chain. Both fixtures are read correctly by poppler's pdftotext and Python's base64.a85decode, and both failed on the old code.


On a separate note: I noticed several open PRs fixing hangs/OOMs (#46, #58, #64, #76, #78, #79) have been waiting for review for a while. I understand maintaining this alone is a lot. I use this library in production for high-volume PDF processing (bank receipts, dozens of layouts) and have a large regression corpus for it — I'd be happy to help with triage and reviews as a co-maintainer if that's useful to you. If you'd rather keep it as is, no problem at all — feel free to just take the fix.

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.

ASCII85 decoder corrupts data and breaks valid PDFs: 'z' zero-group stripped, ~> EOD marker never signals EOF

1 participant