1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Simplify writeObject function

It'll avoid to have the duplication of the code to get the encrypt transform,
and last but not least, it'll avoid to forget about encryption.
This commit is contained in:
Calixte Denizet 2023-09-08 19:29:29 +02:00
parent b903b3030a
commit 52cc1220e4
3 changed files with 24 additions and 98 deletions

View file

@ -25,7 +25,8 @@ import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js";
import { BaseStream } from "./base_stream.js";
import { calculateMD5 } from "./crypto.js";
async function writeObject(ref, obj, buffer, transform) {
async function writeObject(ref, obj, buffer, { encrypt = null }) {
const transform = encrypt?.createCipherTransform(ref.num, ref.gen);
buffer.push(`${ref.num} ${ref.gen} obj\n`);
if (obj instanceof Dict) {
await writeDict(obj, buffer, transform);
@ -101,7 +102,7 @@ async function writeStream(stream, buffer, transform) {
}
}
if (transform !== null) {
if (transform) {
string = transform.encryptString(string);
}
@ -132,7 +133,7 @@ async function writeValue(value, buffer, transform) {
} else if (Array.isArray(value)) {
await writeArray(value, buffer, transform);
} else if (typeof value === "string") {
if (transform !== null) {
if (transform) {
value = transform.encryptString(value);
}
buffer.push(`(${escapeString(value)})`);
@ -253,14 +254,8 @@ async function updateAcroform({
dict.set("NeedAppearances", true);
}
const encrypt = xref.encrypt;
let transform = null;
if (encrypt) {
transform = encrypt.createCipherTransform(acroFormRef.num, acroFormRef.gen);
}
const buffer = [];
await writeObject(acroFormRef, dict, buffer, transform);
await writeObject(acroFormRef, dict, buffer, xref);
newRefs.push({ ref: acroFormRef, data: buffer.join("") });
}