Skip to content

Commit 23cc8dd

Browse files
committed
util: move HexStr and HexDigit from util to crypto
Move HexStr and HexDigit functions from util to crypto. The crypto library does not actually use these functions, but the consensus library does. The consensus and util libraries not allowed to depend on each other, but are allowed to depend on the cryto library, so the crypto library is a reasonable put these. The consensus library uses HexStr and HexDigit in script.cpp, transaction.cpp, and uint256.cpp. The util library does not use HexStr but does use HexDigit in strencodings.cpp to parse integers.
1 parent 6861f95 commit 23cc8dd

File tree

7 files changed

+100
-71
lines changed

7 files changed

+100
-71
lines changed

src/Makefile.am

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ BITCOIN_CORE_H = \
319319
util/signalinterrupt.h \
320320
util/sock.h \
321321
util/spanparsing.h \
322+
util/strencodings.h \
322323
util/string.h \
323324
util/subprocess.h \
324325
util/syserror.h \
@@ -565,6 +566,8 @@ crypto_libbitcoin_crypto_base_la_SOURCES = \
565566
crypto/chacha20poly1305.h \
566567
crypto/chacha20poly1305.cpp \
567568
crypto/common.h \
569+
crypto/hex_base.cpp \
570+
crypto/hex_base.h \
568571
crypto/hkdf_sha256_32.cpp \
569572
crypto/hkdf_sha256_32.h \
570573
crypto/hmac_sha256.cpp \
@@ -658,9 +661,7 @@ libbitcoin_consensus_a_SOURCES = \
658661
span.h \
659662
tinyformat.h \
660663
uint256.cpp \
661-
uint256.h \
662-
util/strencodings.cpp \
663-
util/strencodings.h
664+
uint256.h
664665
#
665666

666667
# common #

src/crypto/hex_base.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2009-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <crypto/hex_base.h>
6+
7+
#include <array>
8+
#include <cstring>
9+
#include <string>
10+
11+
namespace {
12+
13+
using ByteAsHex = std::array<char, 2>;
14+
15+
constexpr std::array<ByteAsHex, 256> CreateByteToHexMap()
16+
{
17+
constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
18+
19+
std::array<ByteAsHex, 256> byte_to_hex{};
20+
for (size_t i = 0; i < byte_to_hex.size(); ++i) {
21+
byte_to_hex[i][0] = hexmap[i >> 4];
22+
byte_to_hex[i][1] = hexmap[i & 15];
23+
}
24+
return byte_to_hex;
25+
}
26+
27+
} // namespace
28+
29+
std::string HexStr(const Span<const uint8_t> s)
30+
{
31+
std::string rv(s.size() * 2, '\0');
32+
static constexpr auto byte_to_hex = CreateByteToHexMap();
33+
static_assert(sizeof(byte_to_hex) == 512);
34+
35+
char* it = rv.data();
36+
for (uint8_t v : s) {
37+
std::memcpy(it, byte_to_hex[v].data(), 2);
38+
it += 2;
39+
}
40+
41+
assert(it == rv.data() + rv.size());
42+
return rv;
43+
}
44+
45+
const signed char p_util_hexdigit[256] =
46+
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
47+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
48+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
49+
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
50+
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
51+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
52+
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
53+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
54+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
55+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
56+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
57+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
58+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
59+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
60+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
61+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
62+
63+
signed char HexDigit(char c)
64+
{
65+
return p_util_hexdigit[(unsigned char)c];
66+
}
67+

src/crypto/hex_base.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2009-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_CRYPTO_HEX_BASE_H
6+
#define BITCOIN_CRYPTO_HEX_BASE_H
7+
8+
#include <span.h>
9+
10+
#include <cstddef>
11+
#include <cstdint>
12+
#include <string>
13+
14+
/**
15+
* Convert a span of bytes to a lower-case hexadecimal string.
16+
*/
17+
std::string HexStr(const Span<const uint8_t> s);
18+
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
19+
inline std::string HexStr(const Span<const std::byte> s) { return HexStr(MakeUCharSpan(s)); }
20+
21+
signed char HexDigit(char c);
22+
23+
#endif // BITCOIN_CRYPTO_HEX_BASE_H

src/primitives/transaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#include <primitives/transaction.h>
77

88
#include <consensus/amount.h>
9+
#include <crypto/hex_base.h>
910
#include <hash.h>
1011
#include <script/script.h>
1112
#include <serialize.h>
1213
#include <tinyformat.h>
1314
#include <uint256.h>
14-
#include <util/strencodings.h>
1515
#include <util/transaction_identifier.h>
1616

1717
#include <algorithm>

src/script/script.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#include <script/script.h>
77

88
#include <crypto/common.h>
9+
#include <crypto/hex_base.h>
910
#include <hash.h>
1011
#include <uint256.h>
1112
#include <util/hash_type.h>
12-
#include <util/strencodings.h>
1313

1414
#include <string>
1515

src/util/strencodings.cpp

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#include <span.h>
76
#include <util/strencodings.h>
87

8+
#include <crypto/hex_base.h>
9+
#include <span.h>
10+
911
#include <array>
1012
#include <cassert>
1113
#include <cstring>
@@ -36,29 +38,6 @@ std::string SanitizeString(std::string_view str, int rule)
3638
return result;
3739
}
3840

39-
const signed char p_util_hexdigit[256] =
40-
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
41-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
42-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
43-
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
44-
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
45-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
46-
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
47-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
48-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
49-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
50-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
51-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
52-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
53-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
54-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
55-
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
56-
57-
signed char HexDigit(char c)
58-
{
59-
return p_util_hexdigit[(unsigned char)c];
60-
}
61-
6241
bool IsHex(std::string_view str)
6342
{
6443
for (char c : str) {
@@ -466,40 +445,6 @@ std::string Capitalize(std::string str)
466445
return str;
467446
}
468447

469-
namespace {
470-
471-
using ByteAsHex = std::array<char, 2>;
472-
473-
constexpr std::array<ByteAsHex, 256> CreateByteToHexMap()
474-
{
475-
constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
476-
477-
std::array<ByteAsHex, 256> byte_to_hex{};
478-
for (size_t i = 0; i < byte_to_hex.size(); ++i) {
479-
byte_to_hex[i][0] = hexmap[i >> 4];
480-
byte_to_hex[i][1] = hexmap[i & 15];
481-
}
482-
return byte_to_hex;
483-
}
484-
485-
} // namespace
486-
487-
std::string HexStr(const Span<const uint8_t> s)
488-
{
489-
std::string rv(s.size() * 2, '\0');
490-
static constexpr auto byte_to_hex = CreateByteToHexMap();
491-
static_assert(sizeof(byte_to_hex) == 512);
492-
493-
char* it = rv.data();
494-
for (uint8_t v : s) {
495-
std::memcpy(it, byte_to_hex[v].data(), 2);
496-
it += 2;
497-
}
498-
499-
assert(it == rv.data() + rv.size());
500-
return rv;
501-
}
502-
503448
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
504449
{
505450
if (str.empty()) {

src/util/strencodings.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef BITCOIN_UTIL_STRENCODINGS_H
1010
#define BITCOIN_UTIL_STRENCODINGS_H
1111

12+
#include <crypto/hex_base.h> // IWYU pragma: export
1213
#include <span.h>
1314
#include <util/string.h>
1415

@@ -66,7 +67,6 @@ std::vector<Byte> ParseHex(std::string_view hex_str)
6667
{
6768
return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
6869
}
69-
signed char HexDigit(char c);
7070
/* Returns true if each character in str is a hex character, and has an even
7171
* number of hex digits.*/
7272
bool IsHex(std::string_view str);
@@ -231,13 +231,6 @@ std::optional<T> ToIntegral(std::string_view str)
231231
*/
232232
[[nodiscard]] bool ParseUInt64(std::string_view str, uint64_t *out);
233233

234-
/**
235-
* Convert a span of bytes to a lower-case hexadecimal string.
236-
*/
237-
std::string HexStr(const Span<const uint8_t> s);
238-
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
239-
inline std::string HexStr(const Span<const std::byte> s) { return HexStr(MakeUCharSpan(s)); }
240-
241234
/**
242235
* Format a paragraph of text to a fixed width, adding spaces for
243236
* indentation to any added line.

0 commit comments

Comments
 (0)