Optimization for FlateStream_getCode, making more pdfs parsable.

This commit cleans up the FlateStream_getCode method, and removes a few error
conditions.
Previously it would fail if the codeSize is less than maxLen if end of stream
is reached. However in the document linked below there is a sub-stream
(the one starting at pos 337) which has maxLen set to 11, but actually
contains only 10. After breaking the sanity check still applies, and in this
case passes validating codeSize(10)==codeLen(10).

 http://www.cafeculture.com/wp-content/uploads/2014/03/V-CM-BR-086-04002-1346-0258-GP-Brazil-Fazenda-Cafe-Cambara-Terra-Preta-Microlot-Sample-0460-13-Pulped-Natural-60Kg.pdf
This commit is contained in:
Fabian Lange 2014-06-05 16:49:29 +02:00
parent 43a103d5d3
commit 22a0e7fe65

View File

@ -430,10 +430,12 @@ var FlateStream = (function FlateStreamClosure() {
var codeSize = this.codeSize; var codeSize = this.codeSize;
var codeBuf = this.codeBuf; var codeBuf = this.codeBuf;
while (codeSize < maxLen) {
var b; var b;
while (codeSize < maxLen) {
if ((b = str.getByte()) === -1) { if ((b = str.getByte()) === -1) {
error('Bad encoding in flate stream'); // premature end of stream. code might however still be valid.
// codeSize < codeLen check below guards against incomplete codeVal.
break;
} }
codeBuf |= (b << codeSize); codeBuf |= (b << codeSize);
codeSize += 8; codeSize += 8;
@ -441,7 +443,7 @@ var FlateStream = (function FlateStreamClosure() {
var code = codes[codeBuf & ((1 << maxLen) - 1)]; var code = codes[codeBuf & ((1 << maxLen) - 1)];
var codeLen = code >> 16; var codeLen = code >> 16;
var codeVal = code & 0xffff; var codeVal = code & 0xffff;
if (codeSize === 0 || codeSize < codeLen || codeLen === 0) { if (codeLen < 1 || codeSize < codeLen) {
error('Bad encoding in flate stream'); error('Bad encoding in flate stream');
} }
this.codeBuf = (codeBuf >> codeLen); this.codeBuf = (codeBuf >> codeLen);