Skip to content

Commit d61f410

Browse files
oltolmelad335
authored andcommitted
utils: replace hex_to_u64 with std::from_chars
1 parent ec70c96 commit d61f410

File tree

3 files changed

+19
-43
lines changed

3 files changed

+19
-43
lines changed

rpcs3/Crypto/key_vault.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ SELF_KEY::SELF_KEY(u64 ver_start, u64 ver_end, u16 rev, u32 type, const std::str
1111
version_end = ver_end;
1212
revision = rev;
1313
self_type = type;
14-
hex_to_bytes(erk, e.c_str(), 0);
15-
hex_to_bytes(riv, r.c_str(), 0);
16-
hex_to_bytes(pub, pb.c_str(), 0);
17-
hex_to_bytes(priv, pr.c_str(), 0);
14+
hex_to_bytes(erk, e, 0);
15+
hex_to_bytes(riv, r, 0);
16+
hex_to_bytes(pub, pb, 0);
17+
hex_to_bytes(priv, pr, 0);
1818
curve_type = ct;
1919
}
2020

rpcs3/Crypto/utils.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include "sha1.h"
88
#include "sha256.h"
99
#include "key_vault.h"
10+
#include <charconv>
11+
#include <cstdlib>
1012
#include <cstring>
11-
#include <stdio.h>
12-
#include <time.h>
13+
#include <cstdio>
14+
#include <ctime>
1315
#include "Utilities/StrUtil.h"
1416
#include "Utilities/File.h"
1517

@@ -21,50 +23,24 @@
2123
// Auxiliary functions (endian swap, xor).
2224

2325
// Hex string conversion auxiliary functions.
24-
u64 hex_to_u64(const char* hex_str)
26+
void hex_to_bytes(unsigned char* data, std::string_view hex_str, unsigned int str_length)
2527
{
26-
auto length = std::strlen(hex_str);
27-
u64 tmp = 0;
28-
u64 result = 0;
29-
char c;
30-
31-
while (length--)
32-
{
33-
c = *hex_str++;
34-
if((c >= '0') && (c <= '9'))
35-
tmp = c - '0';
36-
else if((c >= 'a') && (c <= 'f'))
37-
tmp = c - 'a' + 10;
38-
else if((c >= 'A') && (c <= 'F'))
39-
tmp = c - 'A' + 10;
40-
else
41-
tmp = 0;
42-
result |= (tmp << (length * 4));
43-
}
44-
45-
return result;
46-
}
47-
48-
void hex_to_bytes(unsigned char* data, const char* hex_str, unsigned int str_length)
49-
{
50-
const auto strn_length = (str_length > 0) ? str_length : std::strlen(hex_str);
51-
auto data_length = strn_length / 2;
52-
char tmp_buf[3] = {0, 0, 0};
28+
const auto strn_length = (str_length > 0) ? str_length : hex_str.size();
5329

5430
// Don't convert if the string length is odd.
5531
if ((strn_length % 2) == 0)
5632
{
57-
while (data_length--)
33+
for (size_t i = 0; i < strn_length; i += 2)
5834
{
59-
tmp_buf[0] = *hex_str++;
60-
tmp_buf[1] = *hex_str++;
61-
62-
*data++ = static_cast<u8>(hex_to_u64(tmp_buf) & 0xFF);
35+
const auto [ptr, err] = std::from_chars(hex_str.data() + i, hex_str.data() + i + 2, *data++, 16);
36+
if (err != std::errc())
37+
{
38+
fmt::throw_exception("Failed to read hex string: %s", std::make_error_code(err).message());
39+
}
6340
}
6441
}
6542
}
6643

67-
6844
// Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC).
6945
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len)
7046
{

rpcs3/Crypto/utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
#include "util/types.hpp"
88

9-
#include <stdlib.h>
9+
#include <cstdlib>
10+
#include <string_view>
1011

1112
enum { CRYPTO_MAX_PATH = 4096 };
1213

@@ -15,8 +16,7 @@ char* extract_file_name(const char* file_path, char real_file_name[CRYPTO_MAX_PA
1516
std::string sha256_get_hash(const char* data, usz size, bool lower_case);
1617

1718
// Hex string conversion auxiliary functions.
18-
u64 hex_to_u64(const char* hex_str);
19-
void hex_to_bytes(unsigned char *data, const char *hex_str, unsigned int str_length);
19+
void hex_to_bytes(unsigned char* data, std::string_view hex_str, unsigned int str_length);
2020

2121
// Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC).
2222
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len);

0 commit comments

Comments
 (0)