Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF names need to be escaped when saving #12357

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,33 @@ function parseXFAPath(path) {
});
}

function escapePDFName(str) {
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
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("");
}

export {
escapePDFName,
getLookupTableFactory,
MissingDataException,
XRefEntryException,
Expand Down
4 changes: 2 additions & 2 deletions src/core/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import { bytesToString, escapeString, warn } from "../shared/util.js";
import { Dict, isDict, isName, isRef, isStream, Name } from "./primitives.js";
import { escapePDFName, parseXFAPath } from "./core_utils.js";
import { SimpleDOMNode, SimpleXMLParser } from "../shared/xml_parser.js";
import { calculateMD5 } from "./crypto.js";
import { parseXFAPath } from "./core_utils.js";

function writeDict(dict, buffer, transform) {
buffer.push("<<");
Expand Down Expand Up @@ -73,7 +73,7 @@ function numberToString(value) {

function writeValue(value, buffer, transform) {
if (isName(value)) {
buffer.push(`/${value.name}`);
buffer.push(`/${escapePDFName(value.name)}`);
calixteman marked this conversation as resolved.
Show resolved Hide resolved
} else if (isRef(value)) {
buffer.push(`${value.num} ${value.gen} R`);
} else if (Array.isArray(value)) {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/core_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import { Dict, Ref } from "../../src/core/primitives.js";
import {
escapePDFName,
getInheritableProperty,
isWhiteSpace,
log2,
Expand Down Expand Up @@ -226,4 +227,16 @@ describe("core_utils", function () {
]);
});
});

describe("escapePDFName", function () {
it("should escape PDF name", function () {
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
expect(escapePDFName("hello")).toEqual("hello");
expect(escapePDFName("\xfehello")).toEqual("#fehello");
expect(escapePDFName("he\xfell\xffo")).toEqual("he#fell#ffo");
expect(escapePDFName("\xfehe\xfell\xffo\xff")).toEqual(
"#fehe#fell#ffo#ff"
);
expect(escapePDFName("#h#e#l#l#o")).toEqual("#23h#23e#23l#23l#23o");
});
});
});
15 changes: 15 additions & 0 deletions test/unit/writer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,20 @@ describe("Writer", function () {
expect(buffer.join("")).toEqual(expected);
done();
});

it("should write a Dict in escaping PDF names", function (done) {
const dict = new Dict(null);
dict.set("A", Name.get("hello"));
dict.set("B", Name.get("#hello"));
dict.set("C", Name.get("he\xfello\xff"));

const buffer = [];
writeDict(dict, buffer, null);

const expected = "<< /A /hello /B /#23hello /C /he#fello#ff>>";

expect(buffer.join("")).toEqual(expected);
done();
});
});
});