diff --git a/src/core/jbig2_stream.js b/src/core/jbig2_stream.js index e94c1edb1..3b16f8674 100644 --- a/src/core/jbig2_stream.js +++ b/src/core/jbig2_stream.js @@ -51,17 +51,37 @@ class Jbig2Stream extends DecodeStream { return true; } + // The JBIG2 file header is defined in ITU-T T.88, Annex D.4: + // https://www.itu.int/rec/T-REC-T.88 + static stripFileHeader(bytes) { + if ( + bytes.length >= 9 && + bytes[0] === 0x97 && + bytes[1] === 0x4a && + bytes[2] === 0x42 && + bytes[3] === 0x32 && + bytes[4] === 0x0d && + bytes[5] === 0x0a && + bytes[6] === 0x1a && + bytes[7] === 0x0a + ) { + const headerLength = (bytes[8] & 2) === 0 ? 13 : 9; + return bytes.subarray(headerLength); + } + return bytes; + } + async decodeImage(bytes, length, _decoderOptions) { if (this.eof) { return this.buffer; } - bytes ||= this.bytes; + bytes = Jbig2Stream.stripFileHeader(bytes || this.bytes); let globals = null; if (this.params instanceof Dict) { const globalsStream = this.params.get("JBIG2Globals"); if (globalsStream instanceof BaseStream) { - globals = globalsStream.getBytes(); + globals = Jbig2Stream.stripFileHeader(globalsStream.getBytes()); } } this.buffer = await JBig2CCITTFaxImage.instance.decode( diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index 9db3f6b26..8bb7acc70 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -930,3 +930,4 @@ !90ms_rksj_h_sample.pdf !issue21346.pdf !cidfont_cmap_overflow.pdf +!jbig2_file_header.pdf diff --git a/test/pdfs/jbig2_file_header.pdf b/test/pdfs/jbig2_file_header.pdf new file mode 100644 index 000000000..99fff7640 Binary files /dev/null and b/test/pdfs/jbig2_file_header.pdf differ diff --git a/test/test_manifest.json b/test/test_manifest.json index 2824af541..e64216b03 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -14357,5 +14357,12 @@ "md5": "8ce57e23b42b3232b0f2060bdd2c8b2a", "rounds": 1, "type": "eq" + }, + { + "id": "jbig2_file_header", + "file": "pdfs/jbig2_file_header.pdf", + "md5": "702be2459f9bdbe93338ab3a584babc4", + "rounds": 1, + "type": "eq" } ]