-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHexParser.cs
More file actions
100 lines (88 loc) · 3.04 KB
/
Copy pathHexParser.cs
File metadata and controls
100 lines (88 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Text;
namespace DynaCall;
/// <summary>
/// Converts DynamicWrapperX-style hex strings into raw byte arrays, stripping
/// whitespace and two comment formats before conversion.
/// </summary>
/// <remarks>
/// <para><b>Supported comment styles</b></para>
/// <list type="bullet">
/// <item>
/// <b>Parenthetical</b> — <c>4889C8 (mov rax,rcx) 48F7EA C3</c>
/// </item>
/// <item>
/// <b>End-of-line</b> — <c>4889C8 ; mov rax,rcx</c> (semicolon to next newline)
/// </item>
/// </list>
/// Spaces, tabs, CR, and LF between hex pairs are silently ignored.
/// </remarks>
internal static class HexParser
{
/// <summary>
/// Parses a (potentially commented, whitespace-separated) hex string into bytes.
/// </summary>
/// <exception cref="FormatException">
/// The stripped string contains a non-hex character or an odd number of digits.
/// </exception>
public static byte[] Parse(string hexStr)
{
if (string.IsNullOrEmpty(hexStr)) return Array.Empty<byte>();
var digits = ExtractDigits(hexStr);
if (digits.Length % 2 != 0)
throw new FormatException(
"Hex string has an odd number of digits after stripping comments and whitespace.");
var result = new byte[digits.Length / 2];
for (var i = 0; i < result.Length; i++)
result[i] = Convert.ToByte(digits.Substring(i * 2, 2), 16);
return result;
}
// ── Private ───────────────────────────────────────────────────────────────
private static string ExtractDigits(string input)
{
var sb = new StringBuilder(input.Length);
var i = 0;
while (i < input.Length)
{
var c = input[i];
if (c == '(')
{
// Parenthetical comment — skip to matching ')'
i++;
var depth = 1;
while (i < input.Length && depth > 0)
{
if (input[i] == '(') depth++;
else if (input[i] == ')') depth--;
i++;
}
}
else if (c == ';')
{
// End-of-line comment — skip to next newline
while (i < input.Length && input[i] != '\n')
i++;
}
else if (char.IsWhiteSpace(c))
{
i++;
}
else if (IsHexDigit(c))
{
sb.Append(c);
i++;
}
else
{
throw new FormatException(
"Unexpected character '" + c + "' at index " + i.ToString() +
" in hex string.");
}
}
return sb.ToString();
}
private static bool IsHexDigit(char c)
=> (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F');
}