Skip to content

Commit

Permalink
Merge pull request mozilla#8829 from Snuffleupagus/issue-8823
Browse files Browse the repository at this point in the history
Attempt to improve the `EI` detection heuristics, for inline images, in streams containing `NUL` bytes (issue 8823)
  • Loading branch information
timvandermeij committed Aug 27, 2017
2 parents 8cd1ec4 + e7c9cb0 commit 83b8d22
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
20 changes: 17 additions & 3 deletions src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ var Parser = (function ParserClosure() {
* @returns {number} The inline stream length.
*/
findDefaultInlineStreamEnd(stream) {
const E = 0x45, I = 0x49, SPACE = 0x20, LF = 0xA, CR = 0xD, n = 5;
const E = 0x45, I = 0x49, SPACE = 0x20, LF = 0xA, CR = 0xD;
const n = 10, NUL = 0x0;
let startPos = stream.pos, state = 0, ch, maybeEIPos;
while ((ch = stream.getByte()) !== -1) {
if (state === 0) {
Expand All @@ -159,10 +160,23 @@ var Parser = (function ParserClosure() {
assert(state === 2);
if (ch === SPACE || ch === LF || ch === CR) {
maybeEIPos = stream.pos;
// Let's check the next `n` bytes are ASCII... just be sure.
// Let's check that the next `n` bytes are ASCII... just to be sure.
let followingBytes = stream.peekBytes(n);
for (let i = 0; i < n; i++) {
for (let i = 0, ii = followingBytes.length; i < ii; i++) {
ch = followingBytes[i];
if (ch === NUL && followingBytes[i + 1] !== NUL) {
// NUL bytes are not supposed to occur *outside* of inline
// images, but some PDF generators violate that assumption,
// thus breaking the EI detection heuristics used below.
//
// However, we can't unconditionally treat NUL bytes as "ASCII",
// since that *could* result in inline images being truncated.
//
// To attempt to address this, we'll still treat any *sequence*
// of NUL bytes as non-ASCII, but for a *single* NUL byte we'll
// continue checking the `followingBytes` (fixes issue8823.pdf).
continue;
}
if (ch !== LF && ch !== CR && (ch < SPACE || ch > 0x7F)) {
// Not a LF, CR, SPACE or any visible ASCII character, i.e.
// it's binary stuff. Resetting the state.
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
!issue8697.pdf
!issue8707.pdf
!issue8798r.pdf
!issue8823.pdf
!bad-PageLabels.pdf
!filled-background.pdf
!ArabicCIDTrueType.pdf
Expand Down
Binary file added test/pdfs/issue8823.pdf
Binary file not shown.
10 changes: 8 additions & 2 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2976,9 +2976,15 @@
{ "id": "issue8798",
"file": "pdfs/issue8798r.pdf",
"md5": "3a0e29f013d9edcceb5d852e37738a77",
"link": false,
"rounds": 1,
"type": "eq"
},
{ "id": "issue8823",
"file": "pdfs/issue8823.pdf",
"md5": "ad02d4aa374b315bf1766038d002d57a",
"link": false,
"rounds": 1,
"lastPage": 1,
"link": true,
"type": "eq"
},
{ "id": "issue8613",
Expand Down

0 comments on commit 83b8d22

Please sign in to comment.