From 13930e52028e4117a52f4ac82461b86ab9e32467 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 13 Jan 2020 13:32:23 +0100 Subject: [PATCH] Simplify the handling of unsupported/incorrect markers in `src/core/jpg.js` - Re-factor the "incorrect encoding" check, since this can be easily achieved using the general `findNextFileMarker` helper function (with a suitable `startPos` argument). - Tweak a condition, to make it easier to see that the end of the data has been reached. - Add a reference test for issue 1877, since it's what prompted the "incorrect encoding" check. --- src/core/jpg.js | 21 +++++++++------------ test/pdfs/issue1877.pdf.link | 1 + test/test_manifest.json | 8 ++++++++ 3 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 test/pdfs/issue1877.pdf.link diff --git a/src/core/jpg.js b/src/core/jpg.js index 12d732814..2aa37a9c3 100644 --- a/src/core/jpg.js +++ b/src/core/jpg.js @@ -1031,17 +1031,14 @@ var JpegImage = (function JpegImageClosure() { break; default: - if ( - data[offset - 3] === 0xff && - data[offset - 2] >= 0xc0 && - data[offset - 2] <= 0xfe - ) { - // could be incorrect encoding -- last 0xFF byte of the previous - // block was eaten by the encoder - offset -= 3; - break; - } - const nextFileMarker = findNextFileMarker(data, offset - 2); + // Could be incorrect encoding -- the last 0xFF byte of the previous + // block could have been eaten by the encoder, hence we fallback to + // `startPos = offset - 3` when looking for the next valid marker. + const nextFileMarker = findNextFileMarker( + data, + /* currentPos = */ offset - 2, + /* startPos = */ offset - 3 + ); if (nextFileMarker && nextFileMarker.invalid) { warn( "JpegImage.parse - unexpected data, current marker is: " + @@ -1050,7 +1047,7 @@ var JpegImage = (function JpegImageClosure() { offset = nextFileMarker.offset; break; } - if (offset > data.length - 2) { + if (offset >= data.length - 1) { warn( "JpegImage.parse - reached the end of the image data " + "without finding an EOI marker (0xFFD9)." diff --git a/test/pdfs/issue1877.pdf.link b/test/pdfs/issue1877.pdf.link new file mode 100644 index 000000000..68ab075bf --- /dev/null +++ b/test/pdfs/issue1877.pdf.link @@ -0,0 +1 @@ +https://web.archive.org/web/20160313045333/http://brianmckenna.org/files/IC-Roy.pdf diff --git a/test/test_manifest.json b/test/test_manifest.json index 1bd45a6d1..7d3e8fbdd 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -2927,6 +2927,14 @@ "link": true, "type": "eq" }, + { "id": "issue1877", + "file": "pdfs/issue1877.pdf", + "md5": "feac01f414f2e6792e4d3174944622f5", + "rounds": 1, + "link": true, + "lastPage": 1, + "type": "eq" + }, { "id": "issue7308", "file": "pdfs/issue7308.pdf", "md5": "ba2e23d3af93ac2c634d77ccbe2e79d5",