Skip to content

Commit

Permalink
Bug 1664190 - PDF names need to be escaped when saving. r=bdahl, a=Ry…
Browse files Browse the repository at this point in the history
…anVM

This is a cherry-pick of the following upstream commits:
mozilla/pdf.js#12357
mozilla/pdf.js#12364

Differential Revision: https://phabricator.services.mozilla.com/D89737
  • Loading branch information
rvandermeulen committed Sep 10, 2020
1 parent 96f1767 commit f71cb92
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions toolkit/components/pdfjs/content/build/pdf.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,7 @@ exports.ChunkedStreamManager = ChunkedStreamManager;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.escapePDFName = escapePDFName;
exports.getLookupTableFactory = getLookupTableFactory;
exports.getInheritableProperty = getInheritableProperty;
exports.toRomanNumerals = toRomanNumerals;
Expand Down Expand Up @@ -2974,6 +2975,34 @@ function isWhiteSpace(ch) {
return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
}

function escapePDFName(str) {
const buffer = [];
let start = 0;

for (let i = 0, ii = str.length; i < ii; i++) {
const char = str.charCodeAt(i);

if (char < 0x21 || char > 0x7e || char === 0x23) {
if (start < i) {
buffer.push(str.substring(start, i));
}

buffer.push(`#${char.toString(16)}`);
start = i + 1;
}
}

if (buffer.length === 0) {
return str;
}

if (start < str.length) {
buffer.push(str.substring(start, str.length));
}

return buffer.join("");
}

/***/ }),
/* 9 */
/***/ (function(module, exports, __w_pdfjs_require__) {
Expand Down Expand Up @@ -21182,6 +21211,8 @@ var _util = __w_pdfjs_require__(2);

var _primitives = __w_pdfjs_require__(5);

var _core_utils = __w_pdfjs_require__(8);

var _xml_parser = __w_pdfjs_require__(28);

var _crypto = __w_pdfjs_require__(22);
Expand All @@ -21190,7 +21221,7 @@ function writeDict(dict, buffer, transform) {
buffer.push("<<");

for (const key of dict.getKeys()) {
buffer.push(` /${key} `);
buffer.push(` /${(0, _core_utils.escapePDFName)(key)} `);
writeValue(dict.getRaw(key), buffer, transform);
}

Expand Down Expand Up @@ -21247,7 +21278,7 @@ function numberToString(value) {

function writeValue(value, buffer, transform) {
if ((0, _primitives.isName)(value)) {
buffer.push(`/${value.name}`);
buffer.push(`/${(0, _core_utils.escapePDFName)(value.name)}`);
} else if ((0, _primitives.isRef)(value)) {
buffer.push(`${value.num} ${value.gen} R`);
} else if (Array.isArray(value)) {
Expand Down

0 comments on commit f71cb92

Please sign in to comment.