stored bytes in flatestream

This commit is contained in:
sbarman 2011-06-17 10:10:29 -07:00
parent f9dae73122
commit 9621727db5

40
pdf.js
View File

@ -73,7 +73,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;
@ -232,10 +232,12 @@ var FlateStream = (function() {
]), 5]; ]), 5];
function constructor(stream) { function constructor(stream) {
this.stream = stream; var bytes = stream.getBytes();
var bytesIdx = 0;
this.dict = stream.dict; this.dict = stream.dict;
var cmf = stream.getByte(); var cmf = bytes[bytesIdx++];
var flg = stream.getByte(); var flg = bytes[bytesIdx++];
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)
@ -244,6 +246,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.bytesIdx = bytesIdx;
this.eof = false; this.eof = false;
this.codeSize = 0; this.codeSize = 0;
this.codeBuf = 0; this.codeBuf = 0;
@ -254,12 +259,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 bytesIdx = this.bytesIdx;
var b; var b;
while (codeSize < bits) { while (codeSize < bits) {
if ((b = stream.getByte()) == -1) if ((b = bytes[bytesIdx++]) == undefined)
error("Bad encoding in flate stream"); error("Bad encoding in flate stream");
codeBuf |= b << codeSize; codeBuf |= b << codeSize;
codeSize += 8; codeSize += 8;
@ -267,6 +274,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.bytesIdx = bytesIdx;
return b; return b;
}, },
getCode: function(table) { getCode: function(table) {
@ -274,10 +282,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 bytesIdx = this.bytesIdx;
while (codeSize < maxLen) { while (codeSize < maxLen) {
var b; var b;
if ((b = stream.getByte()) == -1) if ((b = bytes[bytesIdx++]) == undefined)
error("Bad encoding in flate stream"); error("Bad encoding in flate stream");
codeBuf |= (b << codeSize); codeBuf |= (b << codeSize);
codeSize += 8; codeSize += 8;
@ -289,6 +299,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.bytesIdx = bytesIdx;
return codeVal; return codeVal;
}, },
ensureBuffer: function(requested) { ensureBuffer: function(requested) {
@ -390,7 +401,8 @@ var FlateStream = (function() {
return [codes, maxLen]; return [codes, maxLen];
}, },
readBlock: function() { readBlock: function() {
var stream = this.stream; var bytes = this.bytes;
var bytesIdx = this.bytesIdx;
// read block header // read block header
var hdr = this.getBits(3); var hdr = this.getBits(3);
@ -400,16 +412,16 @@ var FlateStream = (function() {
var b; var b;
if (hdr == 0) { // uncompressed block if (hdr == 0) { // uncompressed block
if ((b = stream.getByte()) == -1) if ((b = bytes[bytesIdx++]) == 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 ((b = bytes[bytesIdx++]) == 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 ((b = bytes[bytesIdx++]) == 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 ((b = bytes[bytesIdx++]) == 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))
@ -418,7 +430,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 ((b = bytes[bytesIdx++]) == undefined) {
this.eof = true; this.eof = true;
break; break;
} }