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