fix length/end of stream handling

This commit is contained in:
Andreas Gal 2011-06-03 14:16:04 -07:00
parent 41f46ca346
commit 267f312f13

17
pdf.js
View File

@ -51,20 +51,23 @@ var Stream = (function() {
this.bytes = new Uint8Array(arrayBuffer); this.bytes = new Uint8Array(arrayBuffer);
this.start = start || 0; this.start = start || 0;
this.pos = this.start; this.pos = this.start;
this.length = (start + length) || arrayBuffer.byteLength; this.end = (start + length) || arrayBuffer.byteLength;
this.dict = dict; this.dict = dict;
} }
constructor.prototype = { constructor.prototype = {
get length() {
return this.end - this.start;
},
getByte: function() { getByte: function() {
var bytes = this.bytes; var bytes = this.bytes;
if (this.pos >= this.length) if (this.pos >= this.end)
return -1; return -1;
return bytes[this.pos++]; return bytes[this.pos++];
}, },
lookChar: function() { lookChar: function() {
var bytes = this.bytes; var bytes = this.bytes;
if (this.pos >= this.length) if (this.pos >= this.end)
return; return;
return String.fromCharCode(bytes[this.pos]); return String.fromCharCode(bytes[this.pos]);
}, },
@ -1913,11 +1916,11 @@ var PDFDoc = (function() {
} }
function find(stream, needle, limit, backwards) { function find(stream, needle, limit, backwards) {
var length = stream.length;
var pos = stream.pos; var pos = stream.pos;
var end = stream.end;
var str = ""; var str = "";
if (pos + limit > length) if (pos + limit > end)
limit = length - pos; limit = end - pos;
for (var n = 0; n < limit; ++n) for (var n = 0; n < limit; ++n)
str += stream.getChar(); str += stream.getChar();
stream.pos = pos; stream.pos = pos;
@ -1951,7 +1954,7 @@ var PDFDoc = (function() {
startXRef = stream.pos + 6; startXRef = stream.pos + 6;
} else { } else {
// Find startxref at the end of the file. // Find startxref at the end of the file.
var start = stream.length - 1024; var start = stream.end - 1024;
if (start < 0) if (start < 0)
start = 0; start = 0;
stream.pos = start; stream.pos = start;