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:
parent
3a85479c67
commit
f9fc477080
@ -862,10 +862,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 +1544,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 +1572,8 @@ class PDFDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return shadow(this, "fingerprints", [
|
return shadow(this, "fingerprints", [
|
||||||
hexString(hashOriginal),
|
hashOriginal.toHex(),
|
||||||
hashModified ? hexString(hashModified) : null,
|
hashModified?.toHex() ?? null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user