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

Fix parsing of hexadecimal strings with odd number of characters #890

Merged
merged 1 commit into from
Sep 1, 2024
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
2 changes: 2 additions & 0 deletions src/UglyToad.PdfPig.Tests/Tokens/HexTokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class HexTokenTests
[InlineData("61", "a")]
[InlineData("0061", "a")]
[InlineData("7465787420736f", "text so")]
[InlineData("6170", "ap")]
[InlineData("617", "ap")]
public void MapsCorrectlyToString(string input, string expected)
{
var token = new HexToken(input.ToCharArray());
Expand Down
5 changes: 4 additions & 1 deletion src/UglyToad.PdfPig.Tokens/HexToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public HexToken(ReadOnlySpan<char> characters)
throw new ArgumentNullException(nameof(characters));
}

var bytes = new byte[characters.Length / 2];
// if the final character is missing, it is considered to be a 0, as per 7.3.4.3
// adding 1 to the characters array length ensure the size of the byte array is correct
// in all situations
var bytes = new byte[(characters.Length+1) / 2];
int index = 0;

for (var i = 0; i < characters.Length; i += 2)
Expand Down
Loading