From 22a0e7fe65583b73c956080e03067403d4f3a83f Mon Sep 17 00:00:00 2001 From: Fabian Lange Date: Thu, 5 Jun 2014 16:49:29 +0200 Subject: [PATCH] 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 --- src/core/stream.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/stream.js b/src/core/stream.js index 669cab152..8688434d6 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -430,10 +430,12 @@ var FlateStream = (function FlateStreamClosure() { var codeSize = this.codeSize; var codeBuf = this.codeBuf; + var b; while (codeSize < maxLen) { - var b; 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); codeSize += 8; @@ -441,7 +443,7 @@ var FlateStream = (function FlateStreamClosure() { var code = codes[codeBuf & ((1 << maxLen) - 1)]; var codeLen = code >> 16; var codeVal = code & 0xffff; - if (codeSize === 0 || codeSize < codeLen || codeLen === 0) { + if (codeLen < 1 || codeSize < codeLen) { error('Bad encoding in flate stream'); } this.codeBuf = (codeBuf >> codeLen);