File tree Expand file tree Collapse file tree 3 files changed +11
-4
lines changed Expand file tree Collapse file tree 3 files changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ FUZZ_TARGET_INIT(hex, initialize_hex)
25
25
{
26
26
const std::string random_hex_string (buffer.begin (), buffer.end ());
27
27
const std::vector<unsigned char > data = ParseHex (random_hex_string);
28
+ const std::vector<std::byte> bytes{ParseHex<std::byte>(random_hex_string)};
29
+ assert (AsBytes (Span{data}) == Span{bytes});
28
30
const std::string hex_data = HexStr (data);
29
31
if (IsHex (random_hex_string)) {
30
32
assert (ToLower (random_hex_string) == hex_data);
Original file line number Diff line number Diff line change @@ -76,9 +76,10 @@ bool IsHexNumber(std::string_view str)
76
76
return str.size () > 0 ;
77
77
}
78
78
79
- std::vector<unsigned char > ParseHex (std::string_view str)
79
+ template <typename Byte>
80
+ std::vector<Byte> ParseHex (std::string_view str)
80
81
{
81
- std::vector<unsigned char > vch;
82
+ std::vector<Byte > vch;
82
83
auto it = str.begin ();
83
84
while (it != str.end () && it + 1 != str.end ()) {
84
85
if (IsSpace (*it)) {
@@ -88,10 +89,12 @@ std::vector<unsigned char> ParseHex(std::string_view str)
88
89
auto c1 = HexDigit (*(it++));
89
90
auto c2 = HexDigit (*(it++));
90
91
if (c1 < 0 || c2 < 0 ) break ;
91
- vch.push_back (uint8_t (c1 << 4 ) | c2 );
92
+ vch.push_back (Byte (c1 << 4 ) | Byte (c2) );
92
93
}
93
94
return vch;
94
95
}
96
+ template std::vector<std::byte> ParseHex (std::string_view);
97
+ template std::vector<uint8_t > ParseHex (std::string_view);
95
98
96
99
void SplitHostPort (std::string_view in, uint16_t & portOut, std::string& hostOut)
97
100
{
Original file line number Diff line number Diff line change @@ -55,7 +55,9 @@ enum class ByteUnit : uint64_t {
55
55
* @return A new string without unsafe chars
56
56
*/
57
57
std::string SanitizeString (std::string_view str, int rule = SAFE_CHARS_DEFAULT);
58
- std::vector<unsigned char > ParseHex (std::string_view str);
58
+ /* * Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. */
59
+ template <typename Byte = uint8_t >
60
+ std::vector<Byte> ParseHex (std::string_view str);
59
61
signed char HexDigit (char c);
60
62
/* Returns true if each character in str is a hex character, and has an even
61
63
* number of hex digits.*/
You can’t perform that action at this time.
0 commit comments