Merge pull request #20 from sbarman/streamrewrite2

Changed flate stream to get the entire buffer instead of calling getBytes()
This commit is contained in:
Chris Jones 2011-06-20 16:17:21 -07:00
commit 08f6ae438e

53
pdf.js
View File

@ -74,7 +74,7 @@ var Stream = (function() {
var pos = this.pos; var pos = this.pos;
var end = pos + length; var end = pos + length;
var strEnd = this.end; var strEnd = this.end;
if (end > strEnd) if (!end || end > strEnd)
end = strEnd; end = strEnd;
this.pos = end; this.pos = end;
@ -233,10 +233,12 @@ var FlateStream = (function() {
]), 5]; ]), 5];
function constructor(stream) { function constructor(stream) {
this.stream = stream; var bytes = stream.getBytes();
var bytesPos = 0;
this.dict = stream.dict; this.dict = stream.dict;
var cmf = stream.getByte(); var cmf = bytes[bytesPos++];
var flg = stream.getByte(); var flg = bytes[bytesPos++];
if (cmf == -1 || flg == -1) if (cmf == -1 || flg == -1)
error("Invalid header in flate stream"); error("Invalid header in flate stream");
if ((cmf & 0x0f) != 0x08) if ((cmf & 0x0f) != 0x08)
@ -245,6 +247,9 @@ var FlateStream = (function() {
error("Bad FCHECK in flate stream"); error("Bad FCHECK in flate stream");
if (flg & 0x20) if (flg & 0x20)
error("FDICT bit set in flate stream"); error("FDICT bit set in flate stream");
this.bytes = bytes;
this.bytesPos = bytesPos;
this.eof = false; this.eof = false;
this.codeSize = 0; this.codeSize = 0;
this.codeBuf = 0; this.codeBuf = 0;
@ -255,12 +260,14 @@ var FlateStream = (function() {
constructor.prototype = { constructor.prototype = {
getBits: function(bits) { getBits: function(bits) {
var stream = this.stream;
var codeSize = this.codeSize; var codeSize = this.codeSize;
var codeBuf = this.codeBuf; var codeBuf = this.codeBuf;
var bytes = this.bytes;
var bytesPos = this.bytesPos;
var b; var b;
while (codeSize < bits) { while (codeSize < bits) {
if ((b = stream.getByte()) == -1) if (typeof (b = bytes[bytesPos++]) == "undefined")
error("Bad encoding in flate stream"); error("Bad encoding in flate stream");
codeBuf |= b << codeSize; codeBuf |= b << codeSize;
codeSize += 8; codeSize += 8;
@ -268,6 +275,7 @@ var FlateStream = (function() {
b = codeBuf & ((1 << bits) - 1); b = codeBuf & ((1 << bits) - 1);
this.codeBuf = codeBuf >> bits; this.codeBuf = codeBuf >> bits;
this.codeSize = codeSize -= bits; this.codeSize = codeSize -= bits;
this.bytesPos = bytesPos;
return b; return b;
}, },
getCode: function(table) { getCode: function(table) {
@ -275,10 +283,12 @@ var FlateStream = (function() {
var maxLen = table[1]; var maxLen = table[1];
var codeSize = this.codeSize; var codeSize = this.codeSize;
var codeBuf = this.codeBuf; var codeBuf = this.codeBuf;
var stream = this.stream; var bytes = this.bytes;
var bytesPos = this.bytesPos;
while (codeSize < maxLen) { while (codeSize < maxLen) {
var b; var b;
if ((b = stream.getByte()) == -1) if (typeof (b = bytes[bytesPos++]) == "undefined")
error("Bad encoding in flate stream"); error("Bad encoding in flate stream");
codeBuf |= (b << codeSize); codeBuf |= (b << codeSize);
codeSize += 8; codeSize += 8;
@ -290,6 +300,7 @@ var FlateStream = (function() {
error("Bad encoding in flate stream"); error("Bad encoding in flate stream");
this.codeBuf = (codeBuf >> codeLen); this.codeBuf = (codeBuf >> codeLen);
this.codeSize = (codeSize - codeLen); this.codeSize = (codeSize - codeLen);
this.bytesPos = bytesPos;
return codeVal; return codeVal;
}, },
ensureBuffer: function(requested) { ensureBuffer: function(requested) {
@ -306,9 +317,8 @@ var FlateStream = (function() {
return this.buffer = buffer2; return this.buffer = buffer2;
}, },
getByte: function() { getByte: function() {
var bufferLength = this.bufferLength;
var pos = this.pos; var pos = this.pos;
if (bufferLength <= pos) { while (this.bufferLength <= pos) {
if (this.eof) if (this.eof)
return; return;
this.readBlock(); this.readBlock();
@ -331,9 +341,8 @@ var FlateStream = (function() {
return this.buffer.subarray(pos, end) return this.buffer.subarray(pos, end)
}, },
lookChar: function() { lookChar: function() {
var bufferLength = this.bufferLength;
var pos = this.pos; var pos = this.pos;
if (bufferLength <= pos) { while (this.bufferLength <= pos) {
if (this.eof) if (this.eof)
return; return;
this.readBlock(); this.readBlock();
@ -342,16 +351,15 @@ var FlateStream = (function() {
}, },
getChar: function() { getChar: function() {
var ch = this.lookChar(); var ch = this.lookChar();
if (!ch) // shouldnt matter what the position is if we get past the eof
return; // so no need to check if ch is undefined
this.pos++; this.pos++;
return ch; return ch;
}, },
skip: function(n) { skip: function(n) {
if (!n) if (!n)
n = 1; n = 1;
while (n-- > 0) this.pos += n;
this.getChar();
}, },
generateHuffmanTable: function(lengths) { generateHuffmanTable: function(lengths) {
var n = lengths.length; var n = lengths.length;
@ -397,7 +405,8 @@ var FlateStream = (function() {
array[i++] = what; array[i++] = what;
} }
var stream = this.stream; var bytes = this.bytes;
var bytesPos = this.bytesPos;
// read block header // read block header
var hdr = this.getBits(3); var hdr = this.getBits(3);
@ -407,16 +416,16 @@ var FlateStream = (function() {
var b; var b;
if (hdr == 0) { // uncompressed block if (hdr == 0) { // uncompressed block
if ((b = stream.getByte()) == -1) if (typeof (b = bytes[bytesPos++]) == "undefined")
error("Bad block header in flate stream"); error("Bad block header in flate stream");
var blockLen = b; var blockLen = b;
if ((b = stream.getByte()) == -1) if (typeof (b = bytes[bytesPos++]) == "undefined")
error("Bad block header in flate stream"); error("Bad block header in flate stream");
blockLen |= (b << 8); blockLen |= (b << 8);
if ((b = stream.getByte()) == -1) if (typeof (b = bytes[bytesPos++]) == "undefined")
error("Bad block header in flate stream"); error("Bad block header in flate stream");
var check = b; var check = b;
if ((b = stream.getByte()) == -1) if (typeof (b = bytes[bytesPos++]) == "undefined")
error("Bad block header in flate stream"); error("Bad block header in flate stream");
check |= (b << 8); check |= (b << 8);
if (check != (~this.blockLen & 0xffff)) if (check != (~this.blockLen & 0xffff))
@ -425,7 +434,7 @@ var FlateStream = (function() {
var buffer = this.ensureBuffer(bufferLength + blockLen); var buffer = this.ensureBuffer(bufferLength + blockLen);
this.bufferLength = bufferLength + blockLen; this.bufferLength = bufferLength + blockLen;
for (var n = bufferLength; n < blockLen; ++n) { for (var n = bufferLength; n < blockLen; ++n) {
if ((b = stream.getByte()) == -1) { if (typeof (b = bytes[bytesPos++]) == "undefined") {
this.eof = true; this.eof = true;
break; break;
} }