1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 16:48:08 +02:00

Improve the implementation of the PDFDocument.fingerprints-getter

- Add explicit `length` validation of the /ID entries. Given the `EMPTY_FINGERPRINT` constant we're already *implicitly* assuming a particular length.

 - Move the constants into the `fingerprints`-getter, since they're not used anywhere else.

 - Replace the `hexString` helper function with the standard `Uint8Array.prototype.toHex` method; see https://github.com/tc39/proposal-arraybuffer-base64
This commit is contained in:
Jonas Jenwald 2024-03-31 12:43:07 +02:00
parent 3a85479c67
commit f9fc477080

View file

@ -862,10 +862,6 @@ const STARTXREF_SIGNATURE = new Uint8Array([
]);
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) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(limit > 0, 'The "limit" must be a positive integer.');
@ -1548,30 +1544,24 @@ class PDFDocument {
}
get fingerprints() {
const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT = "\x00".repeat(16);
function validate(data) {
return (
typeof data === "string" &&
data.length > 0 &&
data.length === 16 &&
data !== EMPTY_FINGERPRINT
);
}
function hexString(hash) {
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");
const id = this.xref.trailer.get("ID");
let hashOriginal, hashModified;
if (Array.isArray(idArray) && validate(idArray[0])) {
hashOriginal = stringToBytes(idArray[0]);
if (Array.isArray(id) && validate(id[0])) {
hashOriginal = stringToBytes(id[0]);
if (idArray[1] !== idArray[0] && validate(idArray[1])) {
hashModified = stringToBytes(idArray[1]);
if (id[1] !== id[0] && validate(id[1])) {
hashModified = stringToBytes(id[1]);
}
} else {
hashOriginal = calculateMD5(
@ -1582,8 +1572,8 @@ class PDFDocument {
}
return shadow(this, "fingerprints", [
hexString(hashOriginal),
hashModified ? hexString(hashModified) : null,
hashOriginal.toHex(),
hashModified?.toHex() ?? null,
]);
}