From cb55506b95f67985e17dd1378b952b0ec7feaae9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 21 Aug 2017 14:34:15 +0200 Subject: [PATCH] Try to recover if we reach the end of the stream when searching for the `EI` marker of an inline image (issue 8798) --- src/core/parser.js | 24 ++++++++++++++++-------- test/pdfs/issue8798.pdf.link | 1 + test/test_manifest.json | 8 ++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 test/pdfs/issue8798.pdf.link diff --git a/src/core/parser.js b/src/core/parser.js index 6e0b6d184..db0c4533d 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -147,10 +147,9 @@ var Parser = (function ParserClosure() { * Find the end of the stream by searching for the /EI\s/. * @returns {number} The inline stream length. */ - findDefaultInlineStreamEnd: - function Parser_findDefaultInlineStreamEnd(stream) { - var E = 0x45, I = 0x49, SPACE = 0x20, LF = 0xA, CR = 0xD; - var startPos = stream.pos, state = 0, ch, i, n, followingBytes; + findDefaultInlineStreamEnd(stream) { + const E = 0x45, I = 0x49, SPACE = 0x20, LF = 0xA, CR = 0xD, n = 5; + let startPos = stream.pos, state = 0, ch, maybeEIPos; while ((ch = stream.getByte()) !== -1) { if (state === 0) { state = (ch === E) ? 1 : 0; @@ -159,10 +158,10 @@ var Parser = (function ParserClosure() { } else { assert(state === 2); if (ch === SPACE || ch === LF || ch === CR) { - // Let's check the next five bytes are ASCII... just be sure. - n = 5; - followingBytes = stream.peekBytes(n); - for (i = 0; i < n; i++) { + maybeEIPos = stream.pos; + // Let's check the next `n` bytes are ASCII... just be sure. + let followingBytes = stream.peekBytes(n); + for (let i = 0; i < n; i++) { ch = followingBytes[i]; if (ch !== LF && ch !== CR && (ch < SPACE || ch > 0x7F)) { // Not a LF, CR, SPACE or any visible ASCII character, i.e. @@ -179,6 +178,15 @@ var Parser = (function ParserClosure() { } } } + + if (ch === -1) { + warn('findDefaultInlineStreamEnd: ' + + 'Reached the end of the stream without finding a valid EI marker'); + if (maybeEIPos) { + warn('... trying to recover by using the last "EI" occurrence.'); + stream.skip(-(stream.pos - maybeEIPos)); // Reset the stream position. + } + } return ((stream.pos - 4) - startPos); }, /** diff --git a/test/pdfs/issue8798.pdf.link b/test/pdfs/issue8798.pdf.link new file mode 100644 index 000000000..e4b05c86a --- /dev/null +++ b/test/pdfs/issue8798.pdf.link @@ -0,0 +1 @@ +https://github.com/mozilla/pdf.js/files/1241137/output_34.pdf diff --git a/test/test_manifest.json b/test/test_manifest.json index d4d6aec6f..c07060278 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -2966,6 +2966,14 @@ "type": "eq", "about": "Inline image with ASCII85Decode filter." }, + { "id": "issue8798", + "file": "pdfs/issue8798.pdf", + "md5": "e9e60ea3f8f33850b0333fb04e074e64", + "rounds": 1, + "lastPage": 1, + "link": true, + "type": "eq" + }, { "id": "issue8613", "file": "pdfs/issue8613.pdf", "md5": "bc7ad2db75710aa9916c5769e0c02123",