From 3c1a08a3e6a0ce607963ef68d066d645613adffa Mon Sep 17 00:00:00 2001 From: Raphael Luba Date: Wed, 4 Mar 2020 17:27:27 +0100 Subject: [PATCH 1/2] Add failing test for decoding an image with trailing scan bytes --- test/fixtures/redbox-with-trailing-bytes.jpg | Bin 0 -> 2225 bytes test/index.js | 9 +++++++++ 2 files changed, 9 insertions(+) create mode 100644 test/fixtures/redbox-with-trailing-bytes.jpg diff --git a/test/fixtures/redbox-with-trailing-bytes.jpg b/test/fixtures/redbox-with-trailing-bytes.jpg new file mode 100644 index 0000000000000000000000000000000000000000..35bce415f1f6406cce72de77f87ebccf1009b711 GIT binary patch literal 2225 zcmex=X!XqN1l2cOC(lau%ic3n%$}1|Xnp;}i z+B-VCCQY6)b=ve9GiNPYykzOJeA&aSFc^a zar4&0M~|O8efIpt%U2&ieg5+G+xH(oe=#yJL%ahdAs#~V576U`Oe`$SEbJivFfx?` zF|!~GtD+&BkYgZwVxh2-Q6q_0Zp(e6? z1osf!KmTtr@Gvt1BaB&)!JgshdY#_~7BDq%F=#N1QlmjInhHiU!f07AS{#y9Bm4(8 HO>P1J?lcQj literal 0 HcmV?d00001 diff --git a/test/index.js b/test/index.js index 0f9313e..e77e66c 100644 --- a/test/index.js +++ b/test/index.js @@ -54,6 +54,15 @@ it('should be able to decode a JPEG with RST intervals', function(t) { t.end(); }); +it('should be able to decode a JPEG with trailing bytes', function(t) { + var jpegData = fixture('redbox-with-trailing-bytes.jpg'); + var rawImageData = jpeg.decode(jpegData); + var expected = fixture('redbox.jpg'); + var rawExpectedImageData = jpeg.decode(expected); + t.deepEqual(rawImageData.data, rawExpectedImageData.data); + t.end(); +}); + it('should be able to decode a grayscale JPEG', function(t) { var jpegData = fixture('apsara.jpg'); var rawImageData = jpeg.decode(jpegData); From d550fa769e31cedac14becb7890c8d394d7068a4 Mon Sep 17 00:00:00 2001 From: Raphael Luba Date: Wed, 4 Mar 2020 17:27:52 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Fix:=20don=E2=80=99t=20fail=20if=20scan=20u?= =?UTF-8?q?nit=20contains=20trailing=20bytes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/decoder.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/decoder.js b/lib/decoder.js index 2f120ed..529aed4 100644 --- a/lib/decoder.js +++ b/lib/decoder.js @@ -319,6 +319,18 @@ var JpegImage = (function jpegImage() { } } + if (mcu === mcuExpected) { + // Skip trailing bytes at the end of the scan - until we reach the next marker + do { + if (data[offset] === 0xFF) { + if (data[offset + 1] !== 0x00) { + break; + } + } + offset += 1; + } while (offset < data.length - 2); + } + // find marker bitsCount = 0; marker = (data[offset] << 8) | data[offset + 1];