Skip to content

Commit

Permalink
Merge pull request mozilla#18646 from Snuffleupagus/issue-18645
Browse files Browse the repository at this point in the history
Support an odd number of digits in hexadecimal strings (issue 18645)
  • Loading branch information
Snuffleupagus authored Aug 23, 2024
2 parents 908f453 + 8728f7f commit 584fef5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
33 changes: 16 additions & 17 deletions src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,8 @@ class Lexer {
const strBuf = this.strBuf;
strBuf.length = 0;
let ch = this.currentChar;
let isFirstHex = true;
let firstDigit, secondDigit;
let firstDigit = -1,
digit = -1;
this._hexStringNumWarn = 0;

while (true) {
Expand All @@ -1178,26 +1178,25 @@ class Lexer {
ch = this.nextChar();
continue;
} else {
if (isFirstHex) {
firstDigit = toHexDigit(ch);
if (firstDigit === -1) {
this._hexStringWarn(ch);
ch = this.nextChar();
continue;
}
digit = toHexDigit(ch);
if (digit === -1) {
this._hexStringWarn(ch);
} else if (firstDigit === -1) {
firstDigit = digit;
} else {
secondDigit = toHexDigit(ch);
if (secondDigit === -1) {
this._hexStringWarn(ch);
ch = this.nextChar();
continue;
}
strBuf.push(String.fromCharCode((firstDigit << 4) | secondDigit));
strBuf.push(String.fromCharCode((firstDigit << 4) | digit));
firstDigit = -1;
}
isFirstHex = !isFirstHex;
ch = this.nextChar();
}
}

// According to the PDF spec, section "7.3.4.3 Hexadecimal Strings":
// "If the final digit of a hexadecimal string is missing—that is, if there
// is an odd number of digits—the final digit shall be assumed to be 0."
if (firstDigit !== -1) {
strBuf.push(String.fromCharCode(firstDigit << 4));
}
return strBuf.join("");
}

Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@
!poppler-90-0-fuzzed.pdf
!issue14415.pdf
!issue14307.pdf
!issue18645.pdf
!issue14497.pdf
!bug1799927.pdf
!issue14502.pdf
Expand Down
Binary file added test/pdfs/issue18645.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7986,6 +7986,13 @@
}
}
},
{
"id": "issue18645",
"file": "pdfs/issue18645.pdf",
"md5": "ad05b63db4f21f612adb0900093a3e34",
"rounds": 1,
"type": "eq"
},
{
"id": "bug857031",
"file": "pdfs/bug857031.pdf",
Expand Down
7 changes: 4 additions & 3 deletions test/unit/parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ describe("parser", function () {
});

describe("getHexString", function () {
it("should not throw exception on bad input", function () {
// '7 0 2 15 5 2 2 2 4 3 2 4' should be parsed as '70 21 55 22 24 32'.
it("should handle an odd number of digits", function () {
// '7 0 2 15 5 2 2 2 4 3 2 4' should be parsed as
// '70 21 55 22 24 32 40'.
const input = new StringStream("<7 0 2 15 5 2 2 2 4 3 2 4>");
const lexer = new Lexer(input);
expect(lexer.getHexString()).toEqual('p!U"$2');
expect(lexer.getHexString()).toEqual('p!U"$2@');
});
});

Expand Down

0 comments on commit 584fef5

Please sign in to comment.