Skip to content

Commit

Permalink
fix: handle 0x00E1 / 0x00E0 segments from Pixel phones (jpeg-js#84)
Browse files Browse the repository at this point in the history
* add unit test to reproduce jpeg-js#82

* Added new case to deal with e0 and e1 unused byte markers

* Popular invalid filemarkers now treated as exceptions

Co-authored-by: Hisham Al-Shurafa <halshura@gmail.com>
Co-authored-by: Anthony Tang <a.t.1592653@gmail.com>
  • Loading branch information
3 people committed Jan 11, 2021
1 parent 05bb97a commit df0d8c0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/decoder.js
Expand Up @@ -623,6 +623,7 @@ var JpegImage = (function jpegImage() {
var quantizationTables = [], frames = [];
var huffmanTablesAC = [], huffmanTablesDC = [];
var fileMarker = readUint16();
var malformedDataOffset = -1;
this.comments = [];
if (fileMarker != 0xFFD8) { // SOI (Start of Image)
throw new Error("SOI not found");
Expand Down Expand Up @@ -813,7 +814,6 @@ var JpegImage = (function jpegImage() {
offset--;
}
break;

default:
if (data[offset - 3] == 0xFF &&
data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
Expand All @@ -822,6 +822,19 @@ var JpegImage = (function jpegImage() {
offset -= 3;
break;
}
else if (fileMarker === 0xE0 || fileMarker == 0xE1) {
// Recover from malformed APP1 markers popular in some phone models.
// See https://github.com/eugeneware/jpeg-js/issues/82
if (malformedDataOffset !== -1) {
throw new Error(`first unknown JPEG marker at offset ${malformedDataOffset.toString(16)}, second unknown JPEG marker ${fileMarker.toString(16)} at offset ${(offset - 1).toString(16)}`);
}
malformedDataOffset = offset - 1;
const nextOffset = readUint16();
if (data[offset + nextOffset - 2] === 0xFF) {
offset += nextOffset - 2;
break;
}
}
throw new Error("unknown JPEG marker " + fileMarker.toString(16));
}
fileMarker = readUint16();
Expand Down
Binary file added test/fixtures/table-with-bad-e1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/table-with-good-e1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/index.js
Expand Up @@ -12,6 +12,14 @@ const SUPER_LARGE_JPEG_BASE64 =

const SUPER_LARGE_JPEG_BUFFER = Buffer.from(SUPER_LARGE_JPEG_BASE64, 'base64');

it('should be able read image with a bad e1 marker not preceeded by ff', function () {
var jpegData = fixture('table-with-bad-e1.jpg');
var rawImageData = jpeg.decode(jpegData);
var expected = fixture('table-with-good-e1.jpg');
var rawExpectedImageData = jpeg.decode(expected);
expect(rawImageData.data).toEqual(rawExpectedImageData.data);
});

it('should be able to decode a JPEG', function () {
var jpegData = fixture('grumpycat.jpg');
var rawImageData = jpeg.decode(jpegData);
Expand Down

0 comments on commit df0d8c0

Please sign in to comment.