From de184e05302467cd36f2758cc5fef547e6f5f0c2 Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 17 Jun 2011 10:10:29 -0700 Subject: [PATCH 1/4] stored bytes in flatestream --- pdf.js | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/pdf.js b/pdf.js index 5bf559fcf..656453524 100644 --- a/pdf.js +++ b/pdf.js @@ -73,7 +73,7 @@ var Stream = (function() { var pos = this.pos; var end = pos + length; var strEnd = this.end; - if (end > strEnd) + if (!end || end > strEnd) end = strEnd; this.pos = end; @@ -232,10 +232,12 @@ var FlateStream = (function() { ]), 5]; function constructor(stream) { - this.stream = stream; + var bytes = stream.getBytes(); + var bytesIdx = 0; + this.dict = stream.dict; - var cmf = stream.getByte(); - var flg = stream.getByte(); + var cmf = bytes[bytesIdx++]; + var flg = bytes[bytesIdx++]; if (cmf == -1 || flg == -1) error("Invalid header in flate stream"); if ((cmf & 0x0f) != 0x08) @@ -244,6 +246,9 @@ var FlateStream = (function() { error("Bad FCHECK in flate stream"); if (flg & 0x20) error("FDICT bit set in flate stream"); + + this.bytes = bytes; + this.bytesIdx = bytesIdx; this.eof = false; this.codeSize = 0; this.codeBuf = 0; @@ -254,12 +259,14 @@ var FlateStream = (function() { constructor.prototype = { getBits: function(bits) { - var stream = this.stream; var codeSize = this.codeSize; var codeBuf = this.codeBuf; + var bytes = this.bytes; + var bytesIdx = this.bytesIdx; + var b; while (codeSize < bits) { - if ((b = stream.getByte()) == -1) + if ((b = bytes[bytesIdx++]) == undefined) error("Bad encoding in flate stream"); codeBuf |= b << codeSize; codeSize += 8; @@ -267,6 +274,7 @@ var FlateStream = (function() { b = codeBuf & ((1 << bits) - 1); this.codeBuf = codeBuf >> bits; this.codeSize = codeSize -= bits; + this.bytesIdx = bytesIdx; return b; }, getCode: function(table) { @@ -274,10 +282,12 @@ var FlateStream = (function() { var maxLen = table[1]; var codeSize = this.codeSize; var codeBuf = this.codeBuf; - var stream = this.stream; + var bytes = this.bytes; + var bytesIdx = this.bytesIdx; + while (codeSize < maxLen) { var b; - if ((b = stream.getByte()) == -1) + if ((b = bytes[bytesIdx++]) == undefined) error("Bad encoding in flate stream"); codeBuf |= (b << codeSize); codeSize += 8; @@ -289,6 +299,7 @@ var FlateStream = (function() { error("Bad encoding in flate stream"); this.codeBuf = (codeBuf >> codeLen); this.codeSize = (codeSize - codeLen); + this.bytesIdx = bytesIdx; return codeVal; }, ensureBuffer: function(requested) { @@ -390,7 +401,8 @@ var FlateStream = (function() { return [codes, maxLen]; }, readBlock: function() { - var stream = this.stream; + var bytes = this.bytes; + var bytesIdx = this.bytesIdx; // read block header var hdr = this.getBits(3); @@ -400,16 +412,16 @@ var FlateStream = (function() { var b; if (hdr == 0) { // uncompressed block - if ((b = stream.getByte()) == -1) + if ((b = bytes[bytesIdx++]) == undefined) error("Bad block header in flate stream"); var blockLen = b; - if ((b = stream.getByte()) == -1) + if ((b = bytes[bytesIdx++]) == undefined) error("Bad block header in flate stream"); blockLen |= (b << 8); - if ((b = stream.getByte()) == -1) + if ((b = bytes[bytesIdx++]) == undefined) error("Bad block header in flate stream"); var check = b; - if ((b = stream.getByte()) == -1) + if ((b = bytes[bytesIdx++]) == undefined) error("Bad block header in flate stream"); check |= (b << 8); if (check != (~this.blockLen & 0xffff)) @@ -418,7 +430,7 @@ var FlateStream = (function() { var buffer = this.ensureBuffer(bufferLength + blockLen); this.bufferLength = bufferLength + blockLen; for (var n = bufferLength; n < blockLen; ++n) { - if ((b = stream.getByte()) == -1) { + if ((b = bytes[bytesIdx++]) == undefined) { this.eof = true; break; } From 558ac50d72b1c8dae5aa84ae4b6c34527ce57f1a Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 17 Jun 2011 13:13:25 -0700 Subject: [PATCH 2/4] changed skip in FlateStream to not call getChar --- pdf.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pdf.js b/pdf.js index 03cf6598a..e6dc776df 100644 --- a/pdf.js +++ b/pdf.js @@ -316,9 +316,8 @@ var FlateStream = (function() { return this.buffer = buffer2; }, getByte: function() { - var bufferLength = this.bufferLength; var pos = this.pos; - if (bufferLength == pos) { + while (this.bufferLength <= pos) { if (this.eof) return; this.readBlock(); @@ -341,9 +340,8 @@ var FlateStream = (function() { return this.buffer.subarray(pos, end) }, lookChar: function() { - var bufferLength = this.bufferLength; var pos = this.pos; - if (bufferLength == pos) { + while (this.bufferLength <= pos) { if (this.eof) return; this.readBlock(); @@ -352,16 +350,15 @@ var FlateStream = (function() { }, getChar: function() { var ch = this.lookChar(); - if (!ch) - return; + // shouldnt matter what the position is if we get past the eof + // so no need to check if ch is undefined this.pos++; return ch; }, skip: function(n) { if (!n) n = 1; - while (n-- > 0) - this.getChar(); + this.pos += n; }, generateHuffmanTable: function(lengths) { var n = lengths.length; From 011f7129bec57b29233bc96cbce358b3a4098a6f Mon Sep 17 00:00:00 2001 From: sbarman Date: Mon, 20 Jun 2011 14:10:10 -0700 Subject: [PATCH 3/4] used typeof b == undefined --- pdf.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pdf.js b/pdf.js index 4fd3fedce..5b57d0db6 100644 --- a/pdf.js +++ b/pdf.js @@ -288,7 +288,7 @@ var FlateStream = (function() { while (codeSize < maxLen) { var b; - if ((b = bytes[bytesPos++]) == undefined) + if (typeof (b = bytes[bytesPos++]) == "undefined") error("Bad encoding in flate stream"); codeBuf |= (b << codeSize); codeSize += 8; @@ -416,16 +416,16 @@ var FlateStream = (function() { var b; if (hdr == 0) { // uncompressed block - if ((b = bytes[bytesPos++]) == undefined) + if (typeof (b = bytes[bytesPos++]) == "undefined") error("Bad block header in flate stream"); var blockLen = b; - if ((b = bytes[bytesPos++]) == undefined) + if (typeof (b = bytes[bytesPos++]) == "undefined") error("Bad block header in flate stream"); blockLen |= (b << 8); - if ((b = bytes[bytesPos++]) == undefined) + if (typeof (b = bytes[bytesPos++]) == "undefined") error("Bad block header in flate stream"); var check = b; - if ((b = bytes[bytesPos++]) == undefined) + if (typeof (b = bytes[bytesPos++]) == "undefined") error("Bad block header in flate stream"); check |= (b << 8); if (check != (~this.blockLen & 0xffff)) @@ -434,7 +434,7 @@ var FlateStream = (function() { var buffer = this.ensureBuffer(bufferLength + blockLen); this.bufferLength = bufferLength + blockLen; for (var n = bufferLength; n < blockLen; ++n) { - if ((b = bytes[bytesPos++]) == undefined) { + if (typeof (b = bytes[bytesPos++]) == "undefined") { this.eof = true; break; } From 96e82daf16f5491cfa02b6591db2c9bccb9b885f Mon Sep 17 00:00:00 2001 From: sbarman Date: Mon, 20 Jun 2011 14:14:28 -0700 Subject: [PATCH 4/4] forgot to changed to typeof b == undefined at one location --- pdf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdf.js b/pdf.js index 5b57d0db6..8a844be6c 100644 --- a/pdf.js +++ b/pdf.js @@ -267,7 +267,7 @@ var FlateStream = (function() { var b; while (codeSize < bits) { - if ((b = bytes[bytesPos++]) == undefined) + if (typeof (b = bytes[bytesPos++]) == "undefined") error("Bad encoding in flate stream"); codeBuf |= b << codeSize; codeSize += 8;