use exceptions instead of error codes in case of errors

This commit is contained in:
Andreas Gal 2011-05-11 00:10:15 -07:00
parent c8ab1c014e
commit 7b5effcdbe

32
pdf.js
View File

@ -762,7 +762,7 @@ var FlateStream = (function() {
n = 1; n = 1;
while (n-- > 0) while (n-- > 0)
this.getChar(); this.getChar();
} },
generateHuffmanTable: function(lengths) { generateHuffmanTable: function(lengths) {
var n = lengths.length; var n = lengths.length;
@ -1589,6 +1589,9 @@ var XRef = (function() {
var ref = this.trailerDict.get("Root"); var ref = this.trailerDict.get("Root");
this.rootNum = ref.num; this.rootNum = ref.num;
this.rootGen = ref.gen; this.rootGen = ref.gen;
// set the xref for the trailer dictionary
this.trailerDict.xref = this;
} }
constructor.prototype = { constructor.prototype = {
@ -1598,20 +1601,20 @@ var XRef = (function() {
if (IsCmd(obj = parser.getObj(), "trailer")) if (IsCmd(obj = parser.getObj(), "trailer"))
break; break;
if (!IsInt(obj)) if (!IsInt(obj))
return false; error("Invalid XRef table");
var first = obj; var first = obj;
if (!IsInt(obj = parser.getObj())) if (!IsInt(obj = parser.getObj()))
return false; error("Invalid XRef table");
var n = obj; var n = obj;
if (first < 0 || n < 0 || (first + n) != ((first + n) | 0)) if (first < 0 || n < 0 || (first + n) != ((first + n) | 0))
return false; error("Invalid XRef table");
for (var i = first; i < first + n; ++i) { for (var i = first; i < first + n; ++i) {
var entry = {}; var entry = {};
if (!IsInt(obj = parser.getObj())) if (!IsInt(obj = parser.getObj()))
return false; error("Invalid XRef table");
entry.offset = obj; entry.offset = obj;
if (!IsInt(obj = parser.getObj())) if (!IsInt(obj = parser.getObj()))
return false; error("Invalid XRef table");
entry.gen = obj; entry.gen = obj;
obj = parser.getObj(); obj = parser.getObj();
if (IsCmd(obj, "n")) { if (IsCmd(obj, "n")) {
@ -1619,7 +1622,7 @@ var XRef = (function() {
} else if (IsCmd(obj, "f")) { } else if (IsCmd(obj, "f")) {
entry.free = true; entry.free = true;
} else { } else {
return false; error("Invalid XRef table");
} }
if (!this.entries[i]) { if (!this.entries[i]) {
// In some buggy PDF files the xref table claims to start at 1 // In some buggy PDF files the xref table claims to start at 1
@ -1636,7 +1639,7 @@ var XRef = (function() {
// read the trailer dictionary // read the trailer dictionary
var dict; var dict;
if (!IsDict(dict = parser.getObj())) if (!IsDict(dict = parser.getObj()))
return false; error("Invalid XRef table");
// get the 'Prev' pointer // get the 'Prev' pointer
var more = false; var more = false;
@ -1656,19 +1659,15 @@ var XRef = (function() {
if (IsInt(obj = dict.get("XRefStm"))) { if (IsInt(obj = dict.get("XRefStm"))) {
var pos = obj; var pos = obj;
if (pos in this.xrefstms) if (pos in this.xrefstms)
return false; error("Invalid XRef table");
this.xrefstms[pos] = 1; // avoid infinite recursion this.xrefstms[pos] = 1; // avoid infinite recursion
this.readXRef(pos); this.readXRef(pos);
} else {
this.ok = true;
} }
return more; return more;
}, },
readXRefStream: function(parser) { readXRefStream: function(parser) {
// TODO error("Invalid XRef stream");
this.ok = true;
return true;
}, },
readXRef: function(startXRef) { readXRef: function(startXRef) {
var stream = this.stream; var stream = this.stream;
@ -1683,11 +1682,11 @@ var XRef = (function() {
if (!IsInt(parser.getObj()) || if (!IsInt(parser.getObj()) ||
!IsCmd(parser.getObj(), "obj") || !IsCmd(parser.getObj(), "obj") ||
!IsStream(obj = parser.getObj())) { !IsStream(obj = parser.getObj())) {
return false; error("Invalid XRef stream");
} }
return this.readXRefStream(obj); return this.readXRefStream(obj);
} }
return false; error("Invalid XRef");
} }
}; };
@ -1786,7 +1785,6 @@ var PDFDoc = (function() {
this.xref = new XRef(this.stream, this.xref = new XRef(this.stream,
this.startXRef, this.startXRef,
this.mainXRefEntriesOffset); this.mainXRefEntriesOffset);
this.ok = this.xref.ok;
} }
}; };