From 43847d7ff8da1876975edf741f889342a816e216 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Sat, 7 Dec 2013 23:19:50 +0100 Subject: [PATCH] Set eof to true at the end of a FlateStream At the initialization of `Lexer_getObj` (in `parser.js`), there's a loop that skips whitespace and breaks out whenever EOF is encountered. (https://github.com/mozilla/pdf.js/blob/88ec2bd1a/src/core/parser.js#L586-L599) Whenever the current character is not a whitespace character, `ch = this.nextChar();` is used to find the next character (using `return this.currentChar = this.stream.getByte())`). The aforementioned `getByte` method retrieves the next byte using (https://github.com/mozilla/pdf.js/blob/88ec2bd1a/src/core/stream.js#L122-L128) var pos = this.pos; while (this.bufferLength <= pos) { if (this.eof) return -1; this.readBlock(); } return this.buffer[this.pos++]; This piece of code relies on this.eof to detect whether the last character has been read. When the stream is a `FlateStream`, and the end of the stream has been reached, then **`this.eof` is not set to `true`**, because this check is done inside a loop that does not occur when the read block size is zero: (https://github.com/mozilla/pdf.js/blob/88ec2bd1ac/src/core/stream.js#L511-L517) for (var n = bufferLength; n < end; ++n) { if (typeof (b = bytes[bytesPos++]) == 'undefined') { this.eof = true; break; } buffer[n] = b; } This commit fixes the issue by setting this.eof to true whenever the loop is not going to run (i.e. when bufferLength === end, i.e. blockLen === 0). --- src/core/stream.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core/stream.js b/src/core/stream.js index 61f1af544..6e25b36c2 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -508,12 +508,18 @@ var FlateStream = (function FlateStreamClosure() { var buffer = this.ensureBuffer(bufferLength + blockLen); var end = bufferLength + blockLen; this.bufferLength = end; - for (var n = bufferLength; n < end; ++n) { - if (typeof (b = bytes[bytesPos++]) == 'undefined') { + if (blockLen === 0) { + if (typeof bytes[bytesPos] == 'undefined') { this.eof = true; - break; } - buffer[n] = b; + } else { + for (var n = bufferLength; n < end; ++n) { + if (typeof (b = bytes[bytesPos++]) == 'undefined') { + this.eof = true; + break; + } + buffer[n] = b; + } } this.bytesPos = bytesPos; return;