Skip to content

Commit

Permalink
💩 Prepare to implement readXrefStreamAt
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuch committed Nov 29, 2023
1 parent 3f67d7b commit 661e8ff
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
10 changes: 9 additions & 1 deletion packages/eslint-config/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ module.exports = {
"jest/no-conditional-expect": "off",
"import/prefer-default-export": "off",
"no-bitwise": "off",
"jest/valid-title": ["error", {"ignoreTypeOfDescribeName": true}]
"jest/valid-title": ["error", {"ignoreTypeOfDescribeName": true}],
"no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
},
settings: {
"import/resolver": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ exports[`getXref Throws an error when size has unexpected value 1`] = `"Failed t

exports[`getXref Throws an error when size is not found 1`] = `"Size not found in xref table."`;

exports[`getXref Throws an error when xref is not at its expected position 1`] = `"Expected xref at 2 but found other content."`;
exports[`getXref Throws an error when xref is not at its expected position 1`] = `"Cross-Reference Streams not yet implemented."`;

exports[`getXref Throws an error when xref is not found at position 1`] = `"Could not find xref anywhere at or after 0."`;
exports[`getXref Throws an error when xref is not found at position 1`] = `"Cross-Reference Streams not yet implemented."`;

exports[`readRefTable Expects to merge correctly the refTable of resources 1`] = `
Object {
Expand Down
68 changes: 47 additions & 21 deletions packages/placeholder-plain/src/readRefTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,26 @@ export const getLastTrailerPosition = (pdf) => {
return parseInt(xRefPosition);
};

export const getXref = (pdf, position) => {
let refTable = pdf.slice(position); // slice starting from where xref starts
/**
* @param {Buffer} pdfSlice
* @param {number} position
* @returns {GetXRefReturnType | null}
*/
const readXrefTableAt = (pdfSlice, position) => {
let refTable = pdfSlice.subarray(position); // slice starting from where xref starts
const realPosition = refTable.indexOf(Buffer.from('xref', 'utf8'));
if (realPosition === -1) {
throw new SignPdfError(
`Could not find xref anywhere at or after ${position}.`,
SignPdfError.TYPE_PARSE,
);
return null;
}
if (realPosition > 0) {
const prefix = refTable.slice(0, realPosition);
const prefix = refTable.subarray(0, realPosition);
if (prefix.toString().replace(/\s*/g, '') !== '') {
throw new SignPdfError(
`Expected xref at ${position} but found other content.`,
SignPdfError.TYPE_PARSE,
);
return null;
}
}

const nextEofPosition = refTable.indexOf(Buffer.from('%%EOF', 'utf8'));
if (nextEofPosition === -1) {
throw new SignPdfError(
'Expected EOF after xref and trailer but could not find one.',
SignPdfError.TYPE_PARSE,
);
}
refTable = refTable.slice(0, nextEofPosition);
refTable = refTable.slice(realPosition + 4); // move ahead with the "xref"
refTable = refTable.slice(refTable.indexOf('\n') + 1); // move after the next new line
// move ahead with the "xref\n"
refTable = refTable.subarray(realPosition + 5);

// extract the size
let size = refTable.toString().split('/Size')[1];
Expand Down Expand Up @@ -81,6 +72,41 @@ export const getXref = (pdf, position) => {
};
};

/**
* @param {Buffer} _pdfSlice
* @param {number} _position
* @returns {GetXRefReturnType | null}
*/
const readXrefStreamAt = (_pdfSlice, _position) => {
throw new SignPdfError(
'Cross-Reference Streams not yet implemented.',
SignPdfError.TYPE_PARSE,
);
};

/**
* @typedef {object} GetXRefReturnType
* // TODO
*/

/**
* @param {Buffer} pdf
* @param {number} position
* @returns {GetXRefReturnType}
* @throws {SignPdfError}
*/
export const getXref = (pdf, position) => {
const table = readXrefTableAt(pdf, position)
|| readXrefStreamAt(pdf, position);
if (!table) {
throw new SignPdfError(
`Could not find xref anywhere at or after startxref position ${position}.`,
SignPdfError.TYPE_PARSE,
);
}
return table;
};

/**
* @typedef {Map<*, *>} GetFullXrefTableReturnType
*/
Expand Down

0 comments on commit 661e8ff

Please sign in to comment.