Merge pull request #15576 from Snuffleupagus/version

Re-factor the PDF version parsing in the worker-thread
This commit is contained in:
Tim van der Meij 2022-10-15 13:03:43 +02:00 committed by GitHub
commit 06599f487f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 26 deletions

View File

@ -16,6 +16,7 @@
import { import {
collectActions, collectActions,
MissingDataException, MissingDataException,
PDF_VERSION_REGEXP,
recoverJsURL, recoverJsURL,
toRomanNumerals, toRomanNumerals,
XRefEntryException, XRefEntryException,
@ -84,11 +85,13 @@ class Catalog {
get version() { get version() {
const version = this._catDict.get("Version"); const version = this._catDict.get("Version");
return shadow( if (version instanceof Name) {
this, if (PDF_VERSION_REGEXP.test(version.name)) {
"version", return shadow(this, "version", version.name);
version instanceof Name ? version.name : null }
); warn(`Invalid PDF catalog version: ${version.name}`);
}
return shadow(this, "version", null);
} }
get lang() { get lang() {

View File

@ -26,6 +26,8 @@ import {
import { Dict, isName, Ref, RefSet } from "./primitives.js"; import { Dict, isName, Ref, RefSet } from "./primitives.js";
import { BaseStream } from "./base_stream.js"; import { BaseStream } from "./base_stream.js";
const PDF_VERSION_REGEXP = /^[1-9]\.\d$/;
function getLookupTableFactory(initializer) { function getLookupTableFactory(initializer) {
let lookup; let lookup;
return function () { return function () {
@ -585,6 +587,7 @@ export {
numberToString, numberToString,
ParserEOFException, ParserEOFException,
parseXFAPath, parseXFAPath,
PDF_VERSION_REGEXP,
readInt8, readInt8,
readUint16, readUint16,
readUint32, readUint32,

View File

@ -37,6 +37,7 @@ import {
getNewAnnotationsMap, getNewAnnotationsMap,
isWhiteSpace, isWhiteSpace,
MissingDataException, MissingDataException,
PDF_VERSION_REGEXP,
validateCSSFont, validateCSSFont,
XRefEntryException, XRefEntryException,
XRefParseException, XRefParseException,
@ -712,8 +713,6 @@ const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT = const EMPTY_FINGERPRINT =
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
const PDF_HEADER_VERSION_REGEXP = /^[1-9]\.\d$/;
function find(stream, signature, limit = 1024, backwards = false) { function find(stream, signature, limit = 1024, backwards = false) {
if ( if (
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
@ -818,14 +817,6 @@ class PDFDocument {
parse(recoveryMode) { parse(recoveryMode) {
this.xref.parse(recoveryMode); this.xref.parse(recoveryMode);
this.catalog = new Catalog(this.pdfManager, this.xref); this.catalog = new Catalog(this.pdfManager, this.xref);
// The `checkHeader` method is called before this method and parses the
// version from the header. The specification states in section 7.5.2
// that the version from the catalog, if present, should overwrite the
// version from the header.
if (this.catalog.version) {
this._version = this.catalog.version;
}
} }
get linearization() { get linearization() {
@ -911,8 +902,11 @@ class PDFDocument {
) { ) {
version += String.fromCharCode(ch); version += String.fromCharCode(ch);
} }
if (!this._version) {
if (PDF_VERSION_REGEXP.test(version)) {
this._version = version; this._version = version;
} else {
warn(`Invalid PDF header version: ${version}`);
} }
} }
@ -1260,6 +1254,14 @@ class PDFDocument {
: null; : null;
} }
/**
* The specification states in section 7.5.2 that the version from
* the catalog, if present, should overwrite the version from the header.
*/
get version() {
return this.catalog.version || this._version;
}
get formInfo() { get formInfo() {
const formInfo = { const formInfo = {
hasFields: false, hasFields: false,
@ -1307,17 +1309,8 @@ class PDFDocument {
} }
get documentInfo() { get documentInfo() {
let version = this._version;
if (
typeof version !== "string" ||
!PDF_HEADER_VERSION_REGEXP.test(version)
) {
warn(`Invalid PDF header version number: ${version}`);
version = null;
}
const docInfo = { const docInfo = {
PDFFormatVersion: version, PDFFormatVersion: this.version,
Language: this.catalog.lang, Language: this.catalog.lang,
EncryptFilterName: this.xref.encrypt EncryptFilterName: this.xref.encrypt
? this.xref.encrypt.filterName ? this.xref.encrypt.filterName