Inline the isDict, isRef, and isStream checks in the src/core/xref.js file

This commit is contained in:
Jonas Jenwald 2021-11-26 14:11:34 +01:00
parent 680e0efb9d
commit a669fce762

View File

@ -21,15 +21,7 @@ import {
InvalidPDFException,
warn,
} from "../shared/util.js";
import {
Cmd,
Dict,
isCmd,
isDict,
isRef,
isStream,
Ref,
} from "./primitives.js";
import { Cmd, Dict, isCmd, Ref } from "./primitives.js";
import {
DocStats,
MissingDataException,
@ -38,6 +30,7 @@ import {
XRefParseException,
} from "./core_utils.js";
import { Lexer, Parser } from "./parser.js";
import { BaseStream } from "./base_stream.js";
import { CipherTransformFactory } from "./crypto.js";
class XRef {
@ -88,7 +81,7 @@ class XRef {
}
warn(`XRef.parse - Invalid "Encrypt" reference: "${ex}".`);
}
if (isDict(encrypt)) {
if (encrypt instanceof Dict) {
const ids = trailerDict.get("ID");
const fileId = ids && ids.length ? ids[0] : "";
// The 'Encrypt' dictionary itself should not be encrypted, and by
@ -113,7 +106,7 @@ class XRef {
}
warn(`XRef.parse - Invalid "Root" reference: "${ex}".`);
}
if (isDict(root) && root.has("Pages")) {
if (root instanceof Dict && root.has("Pages")) {
this.root = root;
} else {
if (!recoveryMode) {
@ -155,10 +148,10 @@ class XRef {
let dict = parser.getObj();
// The pdflib PDF generator can generate a nested trailer dictionary
if (!isDict(dict) && dict.dict) {
if (!(dict instanceof Dict) && dict.dict) {
dict = dict.dict;
}
if (!isDict(dict)) {
if (!(dict instanceof Dict)) {
throw new FormatError(
"Invalid XRef table: could not parse trailer dictionary"
);
@ -564,7 +557,7 @@ class XRef {
}
// read the trailer dictionary
const dict = parser.getObj();
if (!isDict(dict)) {
if (!(dict instanceof Dict)) {
continue;
}
// Do some basic validation of the trailer/root dictionary candidate.
@ -656,7 +649,7 @@ class XRef {
if (
!Number.isInteger(parser.getObj()) ||
!isCmd(parser.getObj(), "obj") ||
!isStream((obj = parser.getObj()))
!((obj = parser.getObj()) instanceof BaseStream)
) {
throw new FormatError("Invalid XRef stream");
}
@ -675,7 +668,7 @@ class XRef {
obj = dict.get("Prev");
if (Number.isInteger(obj)) {
this.startXRefQueue.push(obj);
} else if (isRef(obj)) {
} else if (obj instanceof Ref) {
// The spec says Prev must not be a reference, i.e. "/Prev NNN"
// This is a fallback for non-compliant PDFs, i.e. "/Prev NNN 0 R"
this.startXRefQueue.push(obj.num);
@ -746,9 +739,9 @@ class XRef {
} else {
xrefEntry = this.fetchCompressed(ref, xrefEntry, suppressEncryption);
}
if (isDict(xrefEntry)) {
if (xrefEntry instanceof Dict) {
xrefEntry.objId = ref.toString();
} else if (isStream(xrefEntry)) {
} else if (xrefEntry instanceof BaseStream) {
xrefEntry.dict.objId = ref.toString();
}
return xrefEntry;
@ -790,7 +783,7 @@ class XRef {
} else {
xrefEntry = parser.getObj();
}
if (!isStream(xrefEntry)) {
if (!(xrefEntry instanceof BaseStream)) {
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
@ -808,7 +801,7 @@ class XRef {
fetchCompressed(ref, xrefEntry, suppressEncryption = false) {
const tableOffset = xrefEntry.offset;
const stream = this.fetch(Ref.get(tableOffset, 0));
if (!isStream(stream)) {
if (!(stream instanceof BaseStream)) {
throw new FormatError("bad ObjStm stream");
}
const first = stream.dict.get("First");
@ -859,7 +852,7 @@ class XRef {
const obj = parser.getObj();
entries[i] = obj;
if (isStream(obj)) {
if (obj instanceof BaseStream) {
continue;
}
const num = nums[i],