avoid frequent calls to ensureBuffer during decompression

This commit is contained in:
Andreas Gal 2011-06-22 01:12:27 -04:00
parent d64691a95b
commit 7bac97e0fb

48
pdf.js
View File

@ -61,7 +61,7 @@ var Stream = (function() {
this.bytes = Uint8Array(arrayBuffer); this.bytes = Uint8Array(arrayBuffer);
this.start = start || 0; this.start = start || 0;
this.pos = this.start; this.pos = this.start;
this.end = (start + length) || this.bytes.byteLength; this.end = (start + length) || this.bytes.length;
this.dict = dict; this.dict = dict;
} }
@ -516,33 +516,41 @@ var FlateStream = (function() {
error("Unknown block type in flate stream"); error("Unknown block type in flate stream");
} }
var buffer = this.buffer;
var limit = buffer ? buffer.length : 0;
var pos = this.bufferLength; var pos = this.bufferLength;
while (true) { while (true) {
var code1 = this.getCode(litCodeTable); var code1 = this.getCode(litCodeTable);
if (code1 < 256) {
if (pos + 1 >= limit) {
buffer = this.ensureBuffer(pos + 1);
limit = buffer.length;
}
buffer[pos++] = code1;
continue;
}
if (code1 == 256) { if (code1 == 256) {
this.bufferLength = pos; this.bufferLength = pos;
return; return;
} }
if (code1 < 256) { code1 -= 257;
var buffer = this.ensureBuffer(pos + 1); code1 = lengthDecode[code1];
buffer[pos++] = code1; var code2 = code1 >> 16;
} else { if (code2 > 0)
code1 -= 257; code2 = this.getBits(code2);
code1 = lengthDecode[code1]; var len = (code1 & 0xffff) + code2;
var code2 = code1 >> 16; code1 = this.getCode(distCodeTable);
if (code2 > 0) code1 = distDecode[code1];
code2 = this.getBits(code2); code2 = code1 >> 16;
var len = (code1 & 0xffff) + code2; if (code2 > 0)
code1 = this.getCode(distCodeTable); code2 = this.getBits(code2);
code1 = distDecode[code1]; var dist = (code1 & 0xffff) + code2;
code2 = code1 >> 16; if (pos + len >= limit) {
if (code2 > 0) buffer = this.ensureBuffer(pos + len);
code2 = this.getBits(code2); limit = buffer.length;
var dist = (code1 & 0xffff) + code2;
var buffer = this.ensureBuffer(pos + len);
for (var k = 0; k < len; ++k, ++pos)
buffer[pos] = buffer[pos - dist];
} }
for (var k = 0; k < len; ++k, ++pos)
buffer[pos] = buffer[pos - dist];
} }
}; };