Compare commits

...

4 Commits

Author SHA1 Message Date
Jonas Jenwald
8f4c0169a0
Merge pull request #19692 from Snuffleupagus/writer-improve
Improve the implementation in `src/core/writer.js` a little bit
2025-03-20 21:43:21 +01:00
Jonas Jenwald
50eb97afb3 Improve the implementation in src/core/writer.js a little bit
- Let the `writeString` helper function return the new offset, to avoid having to recompute that in multiple spots.

 - In the `computeMD5` helper function we can create the `md5Buffer` via Array-destructuring, rather than using a manual loop.
2025-03-20 21:13:34 +01:00
Jonas Jenwald
a229914b46
Merge pull request #19693 from Snuffleupagus/writer-fix-md5
Fix the MD5 computation in the `src/core/writer.js` file
2025-03-20 16:30:57 +01:00
Jonas Jenwald
e6c869c127 Fix the MD5 computation in the src/core/writer.js file
Currently the MD5 computation doesn't actually work (at all?), since we're invoking the `calculateMD5` function without providing all of the necessary parameters and the PDF-data thus isn't taken into account.

Fixing this caused unit-tests to fail, which isn't that surprising since the current date/time is used in the MD5 computation, and we thus utilize Jasmine to work-around that.
2025-03-20 14:41:25 +01:00
2 changed files with 26 additions and 16 deletions

View File

@ -168,28 +168,30 @@ function writeInt(number, size, offset, buffer) {
}
function writeString(string, offset, buffer) {
for (let i = 0, len = string.length; i < len; i++) {
const ii = string.length;
for (let i = 0; i < ii; i++) {
buffer[offset + i] = string.charCodeAt(i) & 0xff;
}
return offset + ii;
}
function computeMD5(filesize, xrefInfo) {
const time = Math.floor(Date.now() / 1000);
const filename = xrefInfo.filename || "";
const md5Buffer = [time.toString(), filename, filesize.toString()];
let md5BufferLen = md5Buffer.reduce((a, str) => a + str.length, 0);
for (const value of Object.values(xrefInfo.info)) {
md5Buffer.push(value);
md5BufferLen += value.length;
}
const md5Buffer = [
time.toString(),
filename,
filesize.toString(),
...Object.values(xrefInfo.info),
];
const md5BufferLen = md5Buffer.reduce((a, str) => a + str.length, 0);
const array = new Uint8Array(md5BufferLen);
let offset = 0;
for (const str of md5Buffer) {
writeString(str, offset, array);
offset += str.length;
offset = writeString(str, offset, array);
}
return bytesToString(calculateMD5(array));
return bytesToString(calculateMD5(array, 0, array.length));
}
function writeXFADataForAcroform(str, changes) {
@ -477,8 +479,7 @@ async function incrementalUpdate({
// New data
for (const str of buffer) {
writeString(str, offset, array);
offset += str.length;
offset = writeString(str, offset, array);
}
return array;

View File

@ -19,6 +19,15 @@ import { bytesToString } from "../../src/shared/util.js";
import { StringStream } from "../../src/core/stream.js";
describe("Writer", function () {
beforeAll(function () {
jasmine.clock().install();
jasmine.clock().mockDate(new Date(0));
});
afterAll(function () {
jasmine.clock().uninstall();
});
describe("Incremental update", function () {
it("should update a file with new objects", async function () {
const originalData = new Uint8Array();
@ -50,7 +59,7 @@ describe("Writer", function () {
"defg\n" +
"789 0 obj\n" +
"<< /Prev 314 /Size 790 /Type /XRef /Index [123 1 456 1 789 1] " +
"/W [1 1 1] /ID [(id) (\x01#Eg\x89\xab\xcd\xef\xfe\xdc\xba\x98vT2\x10)] " +
"/W [1 1 1] /ID [(id) (\xeb\x4b\x2a\xe7\x31\x36\xf0\xcd\x83\x35\x94\x2a\x36\xcf\xaa\xb0)] " +
"/Length 9>> stream\n" +
"\x01\x01\x2d" +
"\x01\x05\x4e" +
@ -83,7 +92,7 @@ describe("Writer", function () {
"0000000010 00000 n\r\n" +
"trailer\n" +
"<< /Prev 314 /Size 789 " +
"/ID [(id) (\x01#Eg\x89\xab\xcd\xef\xfe\xdc\xba\x98vT2\x10)]>>\n" +
"/ID [(id) (\xeb\x4b\x2a\xe7\x31\x36\xf0\xcd\x83\x35\x94\x2a\x36\xcf\xaa\xb0)]>>\n" +
"startxref\n" +
"10\n" +
"%%EOF\n";
@ -279,7 +288,7 @@ describe("Writer", function () {
"\nabc\n" +
"789 0 obj\n" +
"<< /Prev 314 /Size 790 /Type /XRef /Index [123 1 456 1 789 1] " +
"/W [1 1 1] /ID [(id) (\x01#Eg\x89\xab\xcd\xef\xfe\xdc\xba\x98vT2\x10)] " +
"/W [1 1 1] /ID [(id) (\x5f\xd1\x43\x8e\xf8\x62\x79\x80\xbb\xd6\xf7\xb6\xd2\xb5\x6f\xd8)] " +
"/Length 9>> stream\n" +
"\x00\x00\x2e" +
"\x01\x01\x4e" +
@ -311,7 +320,7 @@ describe("Writer", function () {
"0000000005 00000 n\r\n" +
"trailer\n" +
"<< /Prev 314 /Size 789 " +
"/ID [(id) (\x01#Eg\x89\xab\xcd\xef\xfe\xdc\xba\x98vT2\x10)]>>\n" +
"/ID [(id) (\x5f\xd1\x43\x8e\xf8\x62\x79\x80\xbb\xd6\xf7\xb6\xd2\xb5\x6f\xd8)]>>\n" +
"startxref\n" +
"5\n" +
"%%EOF\n";