Skip to content

Commit 1591788

Browse files
authored
Merge pull request #225 from packdat/hex-string-robustness
Improved tolerance against malformed hex-strings
2 parents b5cfe18 + f77430f commit 1591788

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

PdfSharpCore/Pdf.IO/Lexer.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,19 @@ public Symbol ScanHexadecimalString()
562562
ScanNextChar(true);
563563
break;
564564
}
565-
if (char.IsLetterOrDigit(_currChar))
565+
if (IsHexChar(_currChar))
566566
{
567-
hex[0] = char.ToUpper(_currChar);
568-
hex[1] = char.ToUpper(_nextChar);
569-
int ch = int.Parse(new string(hex), NumberStyles.AllowHexSpecifier);
567+
hex[0] = _currChar;
568+
hex[1] = IsHexChar(_nextChar) ? _nextChar : ' ';
569+
int ch = int.Parse(new string(hex), NumberStyles.HexNumber);
570570
_token.Append(Convert.ToChar(ch));
571571
ScanNextChar(true);
572-
ScanNextChar(true);
572+
if (_currChar != '>')
573+
ScanNextChar(true);
573574
}
575+
else
576+
// prevent endless loop in case _currChar is neither '>' nor a hex-char nor whitespace
577+
ScanNextChar(true);
574578
}
575579
string chars = _token.ToString();
576580
int count = chars.Length;
@@ -585,6 +589,13 @@ public Symbol ScanHexadecimalString()
585589
return _symbol = Symbol.HexString;
586590
}
587591

592+
internal static bool IsHexChar(char c)
593+
{
594+
return char.IsDigit(c) ||
595+
(c >= 'A' && c <= 'F') ||
596+
(c >= 'a' && c <= 'f');
597+
}
598+
588599
/// <summary>
589600
/// Move current position one character further in PDF stream.
590601
/// </summary>

0 commit comments

Comments
 (0)