Skip to content

Commit 0a8aa62

Browse files
committedAug 6, 2020
refactor: Make HexStr take a span
Make HexStr take a span of bytes, instead of an awkward pair of templated iterators.
1 parent 34eb236 commit 0a8aa62

16 files changed

+50
-70
lines changed
 

‎src/core_write.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ std::string FormatScript(const CScript& script)
4848
}
4949
}
5050
if (vch.size() > 0) {
51-
ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
51+
ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
52+
HexStr(std::vector<uint8_t>(it - vch.size(), it)));
5253
} else {
53-
ret += strprintf("0x%x ", HexStr(it2, it));
54+
ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
5455
}
5556
continue;
5657
}
57-
ret += strprintf("0x%x ", HexStr(it2, script.end()));
58+
ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
5859
break;
5960
}
6061
return ret.substr(0, ret.size() - 1);

‎src/net.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ CNetMessage V1TransportDeserializer::GetMessage(const CMessageHeader::MessageSta
722722
if (!msg.m_valid_checksum) {
723723
LogPrint(BCLog::NET, "CHECKSUM ERROR (%s, %u bytes), expected %s was %s\n",
724724
SanitizeString(msg.m_command), msg.m_message_size,
725-
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
726-
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
725+
HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
726+
HexStr(hdr.pchChecksum));
727727
}
728728

729729
// store receive time

‎src/rpc/rawtransaction.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ UniValue finalizepsbt(const JSONRPCRequest& request)
13481348

13491349
if (complete && extract) {
13501350
ssTx << mtx;
1351-
result_str = HexStr(ssTx.str());
1351+
result_str = HexStr(ssTx);
13521352
result.pushKV("hex", result_str);
13531353
} else {
13541354
ssTx << psbtx;

‎src/rpc/request.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ bool GenerateAuthCookie(std::string *cookie_out)
7878
const size_t COOKIE_SIZE = 32;
7979
unsigned char rand_pwd[COOKIE_SIZE];
8080
GetRandBytes(rand_pwd, COOKIE_SIZE);
81-
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
81+
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
8282

8383
/** the umask determines what permissions are used to create this file -
8484
* these are set to 077 in init.cpp unless overridden with -sysperms.

‎src/rpc/util.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue>
260260
UniValue obj(UniValue::VOBJ);
261261
obj.pushKV("iswitness", true);
262262
obj.pushKV("witness_version", (int)id.version);
263-
obj.pushKV("witness_program", HexStr(id.program, id.program + id.length));
263+
obj.pushKV("witness_program", HexStr(Span<const unsigned char>(id.program, id.length)));
264264
return obj;
265265
}
266266
};

‎src/script/descriptor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class OriginPubkeyProvider final : public PubkeyProvider
190190

191191
std::string OriginString() const
192192
{
193-
return HexStr(std::begin(m_origin.fingerprint), std::end(m_origin.fingerprint)) + FormatHDKeypath(m_origin.path);
193+
return HexStr(m_origin.fingerprint) + FormatHDKeypath(m_origin.path);
194194
}
195195

196196
public:

‎src/test/crypto_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &sa
183183
CHKDF_HMAC_SHA256_L32 hkdf32(initial_key_material.data(), initial_key_material.size(), salt_stringified);
184184
unsigned char out[32];
185185
hkdf32.Expand32(info_stringified, out);
186-
BOOST_CHECK(HexStr(out, out + 32) == okm_check_hex);
186+
BOOST_CHECK(HexStr(out) == okm_check_hex);
187187
}
188188

189189
static std::string LongTestString()

‎src/test/settings_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ BOOST_FIXTURE_TEST_CASE(Merge, MergeTestingSetup)
241241

242242
unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
243243
out_sha.Finalize(out_sha_bytes);
244-
std::string out_sha_hex = HexStr(std::begin(out_sha_bytes), std::end(out_sha_bytes));
244+
std::string out_sha_hex = HexStr(out_sha_bytes);
245245

246246
// If check below fails, should manually dump the results with:
247247
//

‎src/test/util_tests.cpp

+9-32
Original file line numberDiff line numberDiff line change
@@ -105,47 +105,24 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
105105
BOOST_AUTO_TEST_CASE(util_HexStr)
106106
{
107107
BOOST_CHECK_EQUAL(
108-
HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
108+
HexStr(ParseHex_expected),
109109
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
110110

111111
BOOST_CHECK_EQUAL(
112-
HexStr(ParseHex_expected + sizeof(ParseHex_expected),
113-
ParseHex_expected + sizeof(ParseHex_expected)),
112+
HexStr(Span<const unsigned char>(
113+
ParseHex_expected + sizeof(ParseHex_expected),
114+
ParseHex_expected + sizeof(ParseHex_expected))),
114115
"");
115116

116117
BOOST_CHECK_EQUAL(
117-
HexStr(ParseHex_expected, ParseHex_expected),
118+
HexStr(Span<const unsigned char>(ParseHex_expected, ParseHex_expected)),
118119
"");
119120

120121
std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
121122

122123
BOOST_CHECK_EQUAL(
123-
HexStr(ParseHex_vec.rbegin(), ParseHex_vec.rend()),
124-
"b0fd8a6704"
125-
);
126-
127-
BOOST_CHECK_EQUAL(
128-
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected),
129-
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
130-
""
131-
);
132-
133-
BOOST_CHECK_EQUAL(
134-
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 1),
135-
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
136-
"04"
137-
);
138-
139-
BOOST_CHECK_EQUAL(
140-
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 5),
141-
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
142-
"b0fd8a6704"
143-
);
144-
145-
BOOST_CHECK_EQUAL(
146-
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 65),
147-
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
148-
"5f1df16b2b704c8a578d0bbaf74d385cde12c11ee50455f3c438ef4c3fbcf649b6de611feae06279a60939e028a8d65c10b73071a6f16719274855feb0fd8a6704"
124+
HexStr(ParseHex_vec),
125+
"04678afdb0"
149126
);
150127
}
151128

@@ -1022,7 +999,7 @@ BOOST_FIXTURE_TEST_CASE(util_ArgsMerge, ArgsMergeTestingSetup)
1022999

10231000
unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
10241001
out_sha.Finalize(out_sha_bytes);
1025-
std::string out_sha_hex = HexStr(std::begin(out_sha_bytes), std::end(out_sha_bytes));
1002+
std::string out_sha_hex = HexStr(out_sha_bytes);
10261003

10271004
// If check below fails, should manually dump the results with:
10281005
//
@@ -1125,7 +1102,7 @@ BOOST_FIXTURE_TEST_CASE(util_ChainMerge, ChainMergeTestingSetup)
11251102

11261103
unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
11271104
out_sha.Finalize(out_sha_bytes);
1128-
std::string out_sha_hex = HexStr(std::begin(out_sha_bytes), std::end(out_sha_bytes));
1105+
std::string out_sha_hex = HexStr(out_sha_bytes);
11291106

11301107
// If check below fails, should manually dump the results with:
11311108
//

‎src/uint256.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
1919
template <unsigned int BITS>
2020
std::string base_blob<BITS>::GetHex() const
2121
{
22-
return HexStr(std::reverse_iterator<const uint8_t*>(m_data + sizeof(m_data)), std::reverse_iterator<const uint8_t*>(m_data));
22+
uint8_t m_data_rev[WIDTH];
23+
for (int i = 0; i < WIDTH; ++i) {
24+
m_data_rev[i] = m_data[WIDTH - 1 - i];
25+
}
26+
return HexStr(m_data_rev);
2327
}
2428

2529
template <unsigned int BITS>

‎src/util/strencodings.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -569,3 +569,16 @@ std::string Capitalize(std::string str)
569569
str[0] = ToUpper(str.front());
570570
return str;
571571
}
572+
573+
std::string HexStr(const Span<const uint8_t> s)
574+
{
575+
std::string rv;
576+
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
577+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
578+
rv.reserve(s.size() * 2);
579+
for (uint8_t v: s) {
580+
rv.push_back(hexmap[v >> 4]);
581+
rv.push_back(hexmap[v & 15]);
582+
}
583+
return rv;
584+
}

‎src/util/strencodings.h

+6-21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define BITCOIN_UTIL_STRENCODINGS_H
1111

1212
#include <attributes.h>
13+
#include <span.h>
1314

1415
#include <cstdint>
1516
#include <iterator>
@@ -119,27 +120,11 @@ NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
119120
*/
120121
NODISCARD bool ParseDouble(const std::string& str, double *out);
121122

122-
template<typename T>
123-
std::string HexStr(const T itbegin, const T itend)
124-
{
125-
std::string rv;
126-
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
127-
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
128-
rv.reserve(std::distance(itbegin, itend) * 2);
129-
for(T it = itbegin; it < itend; ++it)
130-
{
131-
unsigned char val = (unsigned char)(*it);
132-
rv.push_back(hexmap[val>>4]);
133-
rv.push_back(hexmap[val&15]);
134-
}
135-
return rv;
136-
}
137-
138-
template<typename T>
139-
inline std::string HexStr(const T& vch)
140-
{
141-
return HexStr(vch.begin(), vch.end());
142-
}
123+
/**
124+
* Convert a span of bytes to a lower-case hexadecimal string.
125+
*/
126+
std::string HexStr(const Span<const uint8_t> s);
127+
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
143128

144129
/**
145130
* Format a paragraph of text to a fixed width, adding spaces for

‎src/validation.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1198,8 +1198,8 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
11981198

11991199
if (memcmp(blk_start, message_start, CMessageHeader::MESSAGE_START_SIZE)) {
12001200
return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
1201-
HexStr(blk_start, blk_start + CMessageHeader::MESSAGE_START_SIZE),
1202-
HexStr(message_start, message_start + CMessageHeader::MESSAGE_START_SIZE));
1201+
HexStr(blk_start),
1202+
HexStr(message_start));
12031203
}
12041204

12051205
if (blk_size > MAX_SIZE) {

‎src/wallet/bdb.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void CheckUniqueFileid(const BerkeleyEnvironment& env, const std::string& filena
3838
for (const auto& item : env.m_fileids) {
3939
if (fileid == item.second && &fileid != &item.second) {
4040
throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (duplicates fileid %s from %s)", filename,
41-
HexStr(std::begin(item.second.value), std::end(item.second.value)), item.first));
41+
HexStr(item.second.value), item.first));
4242
}
4343
}
4444
}

‎src/wallet/rpcdump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ std::string static EncodeDumpString(const std::string &str) {
3434
std::stringstream ret;
3535
for (const unsigned char c : str) {
3636
if (c <= 32 || c >= 128 || c == '%') {
37-
ret << '%' << HexStr(&c, &c + 1);
37+
ret << '%' << HexStr(Span<const unsigned char>(&c, 1));
3838
} else {
3939
ret << c;
4040
}

‎src/wallet/rpcwallet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3688,7 +3688,7 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
36883688
if (meta->has_key_origin) {
36893689
ret.pushKV("hdkeypath", WriteHDKeypath(meta->key_origin.path));
36903690
ret.pushKV("hdseedid", meta->hd_seed_id.GetHex());
3691-
ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint, meta->key_origin.fingerprint + 4));
3691+
ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint));
36923692
}
36933693
}
36943694
}

0 commit comments

Comments
 (0)
Please sign in to comment.