From 5737c9987ac0d4184dec7dedac60680dca480809 Mon Sep 17 00:00:00 2001 From: Raphael Luba Date: Tue, 21 Apr 2020 17:00:55 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20don=E2=80=99t=20fail=20if=20scan=20conta?= =?UTF-8?q?ins=20trailing=20bytes=20(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/decoder.js | 12 ++++++++++++ test/fixtures/redbox-with-trailing-bytes.jpg | Bin 0 -> 2225 bytes test/index.js | 9 +++++++++ 3 files changed, 21 insertions(+) create mode 100644 test/fixtures/redbox-with-trailing-bytes.jpg diff --git a/lib/decoder.js b/lib/decoder.js index cac42f1..6f1b752 100644 --- a/lib/decoder.js +++ b/lib/decoder.js @@ -325,6 +325,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]; 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);