Convert the XRef to a "normal" class

This commit is contained in:
Jonas Jenwald 2021-04-13 18:26:12 +02:00
parent e8750cfe95
commit bc828cd41f

View File

@ -39,9 +39,8 @@ import {
} from "./core_utils.js";
import { CipherTransformFactory } from "./crypto.js";
var XRef = (function XRefClosure() {
// eslint-disable-next-line no-shadow
function XRef(stream, pdfManager) {
class XRef {
constructor(stream, pdfManager) {
this.stream = stream;
this.pdfManager = pdfManager;
this.entries = [];
@ -54,25 +53,24 @@ var XRef = (function XRefClosure() {
this._newRefNum = null;
}
XRef.prototype = {
getNewRef: function XRef_getNewRef() {
getNewRef() {
if (this._newRefNum === null) {
this._newRefNum = this.entries.length;
}
return Ref.get(this._newRefNum++, 0);
},
}
resetNewRef: function XRef_resetNewRef() {
resetNewRef() {
this._newRefNum = null;
},
}
setStartXRef: function XRef_setStartXRef(startXRef) {
setStartXRef(startXRef) {
// Store the starting positions of xref tables as we process them
// so we can recover from missing data errors
this.startXRefQueue = [startXRef];
},
}
parse: function XRef_parse(recoveryMode) {
parse(recoveryMode = false) {
var trailerDict;
if (!recoveryMode) {
trailerDict = this.readXRef();
@ -125,9 +123,9 @@ var XRef = (function XRefClosure() {
}
throw new FormatError("Invalid root reference");
}
},
}
processXRefTable: function XRef_processXRefTable(parser) {
processXRefTable(parser) {
if (!("tableState" in this)) {
// Stores state of the table as we process it so we can resume
// from middle of table in case of missing data error
@ -170,9 +168,9 @@ var XRef = (function XRefClosure() {
delete this.tableState;
return dict;
},
}
readXRefTable: function XRef_readXRefTable(parser) {
readXRefTable(parser) {
// Example of cross-reference table:
// xref
// 0 1 <-- subsection header (first obj #, obj count)
@ -266,9 +264,9 @@ var XRef = (function XRefClosure() {
throw new FormatError("Invalid XRef table: unexpected first object");
}
return obj;
},
}
processXRefStream: function XRef_processXRefStream(stream) {
processXRefStream(stream) {
if (!("streamState" in this)) {
// Stores state of the stream as we process it so we can resume
// from middle of stream in case of missing data error
@ -290,9 +288,9 @@ var XRef = (function XRefClosure() {
delete this.streamState;
return stream.dict;
},
}
readXRefStream: function XRef_readXRefStream(stream) {
readXRefStream(stream) {
var i, j;
var streamState = this.streamState;
stream.pos = streamState.streamPos;
@ -363,9 +361,9 @@ var XRef = (function XRefClosure() {
streamState.streamPos = stream.pos;
entryRanges.splice(0, 2);
}
},
}
indexObjects: function XRef_indexObjects() {
indexObjects() {
// Simple scan through the PDF content to find objects,
// trailers and XRef streams.
var TAB = 0x9,
@ -500,10 +498,7 @@ var XRef = (function XRefClosure() {
// checking XRef stream suspect
// (it shall have '/XRef' and next char is not a letter)
var xrefTagOffset = skipUntil(content, 0, xrefBytes);
if (
xrefTagOffset < contentLength &&
content[xrefTagOffset + 5] < 64
) {
if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
xrefStms.push(position - stream.start);
this.xrefstms[position - stream.start] = 1; // Avoid recursion
}
@ -574,9 +569,9 @@ var XRef = (function XRefClosure() {
}
// nothing helps
throw new InvalidPDFException("Invalid PDF structure.");
},
}
readXRef: function XRef_readXRef(recoveryMode) {
readXRef(recoveryMode = false) {
var stream = this.stream;
// Keep track of already parsed XRef tables, to prevent an infinite loop
// when parsing corrupt PDF files where e.g. the /Prev entries create a
@ -668,24 +663,24 @@ var XRef = (function XRefClosure() {
return undefined;
}
throw new XRefParseException();
},
}
getEntry: function XRef_getEntry(i) {
getEntry(i) {
var xrefEntry = this.entries[i];
if (xrefEntry && !xrefEntry.free && xrefEntry.offset) {
return xrefEntry;
}
return null;
},
}
fetchIfRef: function XRef_fetchIfRef(obj, suppressEncryption) {
fetchIfRef(obj, suppressEncryption = false) {
if (obj instanceof Ref) {
return this.fetch(obj, suppressEncryption);
}
return obj;
},
}
fetch: function XRef_fetch(ref, suppressEncryption) {
fetch(ref, suppressEncryption = false) {
if (!(ref instanceof Ref)) {
throw new Error("ref object is not a reference");
}
@ -722,7 +717,7 @@ var XRef = (function XRefClosure() {
xrefEntry.dict.objId = ref.toString();
}
return xrefEntry;
},
}
fetchUncompressed(ref, xrefEntry, suppressEncryption = false) {
var gen = ref.gen;
@ -773,7 +768,7 @@ var XRef = (function XRefClosure() {
this._cacheMap.set(num, xrefEntry);
}
return xrefEntry;
},
}
fetchCompressed(ref, xrefEntry, suppressEncryption = false) {
const tableOffset = xrefEntry.offset;
@ -784,9 +779,7 @@ var XRef = (function XRefClosure() {
const first = stream.dict.get("First");
const n = stream.dict.get("N");
if (!Number.isInteger(first) || !Number.isInteger(n)) {
throw new FormatError(
"invalid first and n parameters for ObjStm stream"
);
throw new FormatError("invalid first and n parameters for ObjStm stream");
}
let parser = new Parser({
lexer: new Lexer(stream),
@ -854,14 +847,14 @@ var XRef = (function XRefClosure() {
throw new XRefEntryException(`Bad (compressed) XRef entry: ${ref}`);
}
return xrefEntry;
},
}
async fetchIfRefAsync(obj, suppressEncryption) {
if (obj instanceof Ref) {
return this.fetchAsync(obj, suppressEncryption);
}
return obj;
},
}
async fetchAsync(ref, suppressEncryption) {
try {
@ -873,14 +866,11 @@ var XRef = (function XRefClosure() {
await this.pdfManager.requestRange(ex.begin, ex.end);
return this.fetchAsync(ref, suppressEncryption);
}
},
}
getCatalogObj: function XRef_getCatalogObj() {
getCatalogObj() {
return this.root;
},
};
return XRef;
})();
}
}
export { XRef };