mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 14:48:08 +02:00
Merge pull request #18646 from Snuffleupagus/issue-18645
Support an odd number of digits in hexadecimal strings (issue 18645)
This commit is contained in:
commit
584fef5823
5 changed files with 28 additions and 20 deletions
|
@ -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) {
|
||||
|
@ -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("");
|
||||
}
|
||||
|
||||
|
|
1
test/pdfs/.gitignore
vendored
1
test/pdfs/.gitignore
vendored
|
@ -564,6 +564,7 @@
|
|||
!poppler-90-0-fuzzed.pdf
|
||||
!issue14415.pdf
|
||||
!issue14307.pdf
|
||||
!issue18645.pdf
|
||||
!issue14497.pdf
|
||||
!bug1799927.pdf
|
||||
!issue14502.pdf
|
||||
|
|
BIN
test/pdfs/issue18645.pdf
Normal file
BIN
test/pdfs/issue18645.pdf
Normal file
Binary file not shown.
|
@ -7986,6 +7986,13 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "issue18645",
|
||||
"file": "pdfs/issue18645.pdf",
|
||||
"md5": "ad05b63db4f21f612adb0900093a3e34",
|
||||
"rounds": 1,
|
||||
"type": "eq"
|
||||
},
|
||||
{
|
||||
"id": "bug857031",
|
||||
"file": "pdfs/bug857031.pdf",
|
||||
|
|
|
@ -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@');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue