Skip to content

Commit

Permalink
šŸ› [placeholder-plain] Do not search for a trailer word once the xref ā€¦
Browse files Browse the repository at this point in the history
ā€¦position is known

fixes #209
  • Loading branch information
vbuch committed Feb 23, 2024
1 parent 11d3364 commit 169910e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,39 @@ Object {
"startingIndex": 0,
}
`;

exports[`readRefTable Expects to merge correctly the refTable of resources 8`] = `
Object {
"maxIndex": 27,
"offsets": Map {
1 => 15,
2 => 89780,
3 => 265,
4 => 302,
5 => 27161,
6 => 55428,
7 => 72070,
8 => 378,
9 => 592,
10 => 764,
11 => 4338,
12 => 89711,
13 => 4443,
14 => 26241,
15 => 26484,
16 => 26817,
17 => 27302,
18 => 54210,
19 => 54444,
20 => 55060,
21 => 55560,
22 => 71288,
23 => 71522,
24 => 71742,
25 => 72781,
26 => 89525,
27 => 89643,
},
"startingIndex": 0,
}
`;
25 changes: 17 additions & 8 deletions packages/placeholder-plain/src/readRefTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ export const getXref = (pdf, position) => {
};

/**
* @typedef {Map<*, *>} GetFullXrefTableReturnType
* @typedef {Map<number, number>} FullXrefTable
*/

/**
* @param {Buffer} pdf
* @returns {GetFullXrefTableReturnType}
* @param {number} xRefPosition
* @returns {FullXrefTable}
*/
export const getFullXrefTable = (pdf) => {
const lastTrailerPosition = getLastTrailerPosition(pdf);
const lastXrefTable = getXref(pdf, lastTrailerPosition);
const getFullXref = (pdf, xRefPosition) => {
const lastXrefTable = getXref(pdf, xRefPosition);

if (lastXrefTable.prev === undefined) {
return lastXrefTable.xRefContent;
}
const pdfWithoutLastTrailer = pdf.slice(0, lastTrailerPosition);
const partOfXrefTable = getFullXrefTable(pdfWithoutLastTrailer);

const partOfXrefTable = getFullXref(pdf, lastXrefTable.prev);

const mergedXrefTable = new Map([
...partOfXrefTable,
Expand All @@ -107,11 +107,20 @@ export const getFullXrefTable = (pdf) => {
return mergedXrefTable;
};

/**
* @param {Buffer} pdf
* @returns {FullXrefTable}
*/
export const getFullXrefTable = (pdf) => {
const lastTrailerPosition = getLastTrailerPosition(pdf);
return getFullXref(pdf, lastTrailerPosition);
};

/**
* @typedef {object} ReadRefTableReturnType
* @prop {number} startingIndex
* @prop {number} maxIndex
* @prop {GetFullXrefTableReturnType} offsets
* @prop {FullXrefTable} offsets
*/

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/placeholder-plain/src/xrefToRefMap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {SignPdfError} from '@signpdf/utils';

/**
* @param {string} xrefString
* @returns {Map<number, number>}
*/
const xrefToRefMap = (xrefString) => {
const lines = xrefString.split('\n').filter((l) => l !== '');

Expand Down

0 comments on commit 169910e

Please sign in to comment.