Support an odd number of digits in hexadecimal strings (issue 18645)

See https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf#G6.1840792
This commit is contained in:
Jonas Jenwald 2024-08-23 16:06:54 +02:00
parent 908f453384
commit 8728f7f134
5 changed files with 28 additions and 20 deletions

View file

@ -1163,8 +1163,8 @@ class Lexer {
const strBuf = this.strBuf; const strBuf = this.strBuf;
strBuf.length = 0; strBuf.length = 0;
let ch = this.currentChar; let ch = this.currentChar;
let isFirstHex = true; let firstDigit = -1,
let firstDigit, secondDigit; digit = -1;
this._hexStringNumWarn = 0; this._hexStringNumWarn = 0;
while (true) { while (true) {
@ -1178,26 +1178,25 @@ class Lexer {
ch = this.nextChar(); ch = this.nextChar();
continue; continue;
} else { } else {
if (isFirstHex) { digit = toHexDigit(ch);
firstDigit = toHexDigit(ch); if (digit === -1) {
if (firstDigit === -1) { this._hexStringWarn(ch);
this._hexStringWarn(ch); } else if (firstDigit === -1) {
ch = this.nextChar(); firstDigit = digit;
continue;
}
} else { } else {
secondDigit = toHexDigit(ch); strBuf.push(String.fromCharCode((firstDigit << 4) | digit));
if (secondDigit === -1) { firstDigit = -1;
this._hexStringWarn(ch);
ch = this.nextChar();
continue;
}
strBuf.push(String.fromCharCode((firstDigit << 4) | secondDigit));
} }
isFirstHex = !isFirstHex;
ch = this.nextChar(); 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(""); return strBuf.join("");
} }

View file

@ -564,6 +564,7 @@
!poppler-90-0-fuzzed.pdf !poppler-90-0-fuzzed.pdf
!issue14415.pdf !issue14415.pdf
!issue14307.pdf !issue14307.pdf
!issue18645.pdf
!issue14497.pdf !issue14497.pdf
!bug1799927.pdf !bug1799927.pdf
!issue14502.pdf !issue14502.pdf

BIN
test/pdfs/issue18645.pdf Normal file

Binary file not shown.

View file

@ -7986,6 +7986,13 @@
} }
} }
}, },
{
"id": "issue18645",
"file": "pdfs/issue18645.pdf",
"md5": "ad05b63db4f21f612adb0900093a3e34",
"rounds": 1,
"type": "eq"
},
{ {
"id": "bug857031", "id": "bug857031",
"file": "pdfs/bug857031.pdf", "file": "pdfs/bug857031.pdf",

View file

@ -201,11 +201,12 @@ describe("parser", function () {
}); });
describe("getHexString", function () { describe("getHexString", function () {
it("should not throw exception on bad input", function () { 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'. // '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 input = new StringStream("<7 0 2 15 5 2 2 2 4 3 2 4>");
const lexer = new Lexer(input); const lexer = new Lexer(input);
expect(lexer.getHexString()).toEqual('p!U"$2'); expect(lexer.getHexString()).toEqual('p!U"$2@');
}); });
}); });