Merge pull request #17862 from Snuffleupagus/fingerprints-toHex

Improve the implementation of the `PDFDocument.fingerprints`-getter
This commit is contained in:
Jonas Jenwald 2024-10-29 15:34:02 +01:00 committed by GitHub
commit 25cf4add05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 26 deletions

View File

@ -26,6 +26,7 @@ import {
stringToBytes, stringToBytes,
stringToPDFString, stringToPDFString,
stringToUTF8String, stringToUTF8String,
toHexUtil,
unreachable, unreachable,
Util, Util,
warn, warn,
@ -862,10 +863,6 @@ const STARTXREF_SIGNATURE = new Uint8Array([
]); ]);
const ENDOBJ_SIGNATURE = new Uint8Array([0x65, 0x6e, 0x64, 0x6f, 0x62, 0x6a]); const ENDOBJ_SIGNATURE = new Uint8Array([0x65, 0x6e, 0x64, 0x6f, 0x62, 0x6a]);
const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT =
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
function find(stream, signature, limit = 1024, backwards = false) { function find(stream, signature, limit = 1024, backwards = false) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(limit > 0, 'The "limit" must be a positive integer.'); assert(limit > 0, 'The "limit" must be a positive integer.');
@ -1548,30 +1545,24 @@ class PDFDocument {
} }
get fingerprints() { get fingerprints() {
const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT = "\x00".repeat(16);
function validate(data) { function validate(data) {
return ( return (
typeof data === "string" && typeof data === "string" &&
data.length > 0 && data.length === 16 &&
data !== EMPTY_FINGERPRINT data !== EMPTY_FINGERPRINT
); );
} }
function hexString(hash) { const id = this.xref.trailer.get("ID");
const buf = [];
for (const num of hash) {
const hex = num.toString(16);
buf.push(hex.padStart(2, "0"));
}
return buf.join("");
}
const idArray = this.xref.trailer.get("ID");
let hashOriginal, hashModified; let hashOriginal, hashModified;
if (Array.isArray(idArray) && validate(idArray[0])) { if (Array.isArray(id) && validate(id[0])) {
hashOriginal = stringToBytes(idArray[0]); hashOriginal = stringToBytes(id[0]);
if (idArray[1] !== idArray[0] && validate(idArray[1])) { if (id[1] !== id[0] && validate(id[1])) {
hashModified = stringToBytes(idArray[1]); hashModified = stringToBytes(id[1]);
} }
} else { } else {
hashOriginal = calculateMD5( hashOriginal = calculateMD5(
@ -1582,8 +1573,8 @@ class PDFDocument {
} }
return shadow(this, "fingerprints", [ return shadow(this, "fingerprints", [
hexString(hashOriginal), toHexUtil(hashOriginal),
hashModified ? hexString(hashModified) : null, hashModified ? toHexUtil(hashModified) : null,
]); ]);
} }

View File

@ -90,6 +90,7 @@ import {
XFAObject, XFAObject,
XFAObjectArray, XFAObjectArray,
} from "./xfa_object.js"; } from "./xfa_object.js";
import { fromBase64Util, Util, warn } from "../../shared/util.js";
import { import {
getBBox, getBBox,
getColor, getColor,
@ -102,7 +103,6 @@ import {
getStringOption, getStringOption,
HTMLResult, HTMLResult,
} from "./utils.js"; } from "./utils.js";
import { stringToBytes, Util, warn } from "../../shared/util.js";
import { getMetrics } from "./fonts.js"; import { getMetrics } from "./fonts.js";
import { recoverJsURL } from "../core_utils.js"; import { recoverJsURL } from "../core_utils.js";
import { searchNode } from "./som.js"; import { searchNode } from "./som.js";
@ -3427,7 +3427,7 @@ class Image extends StringObject {
} }
if (!buffer && this.transferEncoding === "base64") { if (!buffer && this.transferEncoding === "base64") {
buffer = stringToBytes(atob(this[$content])); buffer = fromBase64Util(this[$content]);
} }
if (!buffer) { if (!buffer) {

View File

@ -15,11 +15,11 @@
import { import {
assert, assert,
bytesToString,
FontRenderOps, FontRenderOps,
isNodeJS, isNodeJS,
shadow, shadow,
string32, string32,
toBase64Util,
unreachable, unreachable,
warn, warn,
} from "../shared/util.js"; } from "../shared/util.js";
@ -399,9 +399,8 @@ class FontFaceObject {
if (!this.data || this.disableFontFace) { if (!this.data || this.disableFontFace) {
return null; return null;
} }
const data = bytesToString(this.data);
// Add the @font-face rule to the document. // Add the @font-face rule to the document.
const url = `url(data:${this.mimetype};base64,${btoa(data)});`; const url = `url(data:${this.mimetype};base64,${toBase64Util(this.data)});`;
let rule; let rule;
if (!this.cssFontInfo) { if (!this.cssFontInfo) {
rule = `@font-face {font-family:"${this.loadedName}";src:${url}}`; rule = `@font-face {font-family:"${this.loadedName}";src:${url}}`;

View File

@ -1097,6 +1097,35 @@ const FontRenderOps = {
TRANSLATE: 8, TRANSLATE: 8,
}; };
// TODO: Remove this once `Uint8Array.prototype.toHex` is generally available.
function toHexUtil(arr) {
if (Uint8Array.prototype.toHex) {
return arr.toHex();
}
const buf = [];
for (const num of arr) {
buf.push(num.toString(16).padStart(2, "0"));
}
return buf.join("");
}
// TODO: Remove this once `Uint8Array.prototype.toBase64` is generally
// available.
function toBase64Util(arr) {
if (Uint8Array.prototype.toBase64) {
return arr.toBase64();
}
return btoa(bytesToString(arr));
}
// TODO: Remove this once `Uint8Array.fromBase64` is generally available.
function fromBase64Util(str) {
if (Uint8Array.fromBase64) {
return Uint8Array.fromBase64(str);
}
return stringToBytes(atob(str));
}
export { export {
AbortException, AbortException,
AnnotationActionEventType, AnnotationActionEventType,
@ -1120,6 +1149,7 @@ export {
FONT_IDENTITY_MATRIX, FONT_IDENTITY_MATRIX,
FontRenderOps, FontRenderOps,
FormatError, FormatError,
fromBase64Util,
getModificationDate, getModificationDate,
getUuid, getUuid,
getVerbosityLevel, getVerbosityLevel,
@ -1149,6 +1179,8 @@ export {
stringToPDFString, stringToPDFString,
stringToUTF8String, stringToUTF8String,
TextRenderingMode, TextRenderingMode,
toBase64Util,
toHexUtil,
UnexpectedResponseException, UnexpectedResponseException,
UnknownErrorException, UnknownErrorException,
unreachable, unreachable,