Skip to content

Commit a12d9e5

Browse files
author
MarcoFalke
committed
Merge #19687: refactor: make EncodeBase{32,64} consume Spans
e2aa1a5 util: make EncodeBase64 consume Spans (Sebastian Falbesoner) 2bc2071 util: make EncodeBase32 consume Spans (Sebastian Falbesoner) Pull request description: To simplify the interface of the Base32/Base64 encoding functions for raw data, this PR changes them from taking two arguments (pointer and length) to just one Span. Most calls to `EncodeBase64` pass data from `CDataStream` instances, which unfortunately internally work with `char*` pointers rather than `unsigned char*`, but thanks to the recently introduced `MakeUCharSpan` helper, converting them is quite easy. ACKs for top commit: MarcoFalke: ACK e2aa1a5 🐮 vasild: ACK e2aa1a5 Tree-SHA512: 43bd3bd2ee8e3be2474db0a81dae9d9e88fac2464b96d1b042147106ed7433799dcba3000c69990511ecfc697b0c7306ce85f2ecb2293e2e44fd356c9694b150
2 parents e80e5b3 + e2aa1a5 commit a12d9e5

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

src/netaddress.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <netaddress.h>
7+
78
#include <hash.h>
9+
#include <tinyformat.h>
810
#include <util/strencodings.h>
911
#include <util/asmap.h>
10-
#include <tinyformat.h>
1112

1213
#include <algorithm>
1314
#include <array>
@@ -341,9 +342,9 @@ enum Network CNetAddr::GetNetwork() const
341342
std::string CNetAddr::ToStringIP() const
342343
{
343344
if (IsTor())
344-
return EncodeBase32(m_addr.data(), m_addr.size()) + ".onion";
345+
return EncodeBase32(m_addr) + ".onion";
345346
if (IsInternal())
346-
return EncodeBase32(m_addr.data(), m_addr.size()) + ".internal";
347+
return EncodeBase32(m_addr) + ".internal";
347348
CService serv(*this, 0);
348349
struct sockaddr_storage sockaddr;
349350
socklen_t socklen = sizeof(sockaddr);

src/rpc/rawtransaction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ UniValue combinepsbt(const JSONRPCRequest& request)
13001300

13011301
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
13021302
ssTx << merged_psbt;
1303-
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size());
1303+
return EncodeBase64(MakeUCharSpan(ssTx));
13041304
}
13051305

13061306
UniValue finalizepsbt(const JSONRPCRequest& request)
@@ -1435,7 +1435,7 @@ UniValue createpsbt(const JSONRPCRequest& request)
14351435
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
14361436
ssTx << psbtx;
14371437

1438-
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size());
1438+
return EncodeBase64(MakeUCharSpan(ssTx));
14391439
}
14401440

14411441
UniValue converttopsbt(const JSONRPCRequest& request)
@@ -1502,7 +1502,7 @@ UniValue converttopsbt(const JSONRPCRequest& request)
15021502
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
15031503
ssTx << psbtx;
15041504

1505-
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size());
1505+
return EncodeBase64(MakeUCharSpan(ssTx));
15061506
}
15071507

15081508
UniValue utxoupdatepsbt(const JSONRPCRequest& request)
@@ -1590,7 +1590,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
15901590

15911591
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
15921592
ssTx << psbtx;
1593-
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size());
1593+
return EncodeBase64(MakeUCharSpan(ssTx));
15941594
}
15951595

15961596
UniValue joinpsbts(const JSONRPCRequest& request)
@@ -1683,7 +1683,7 @@ UniValue joinpsbts(const JSONRPCRequest& request)
16831683

16841684
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
16851685
ssTx << shuffled_psbt;
1686-
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size());
1686+
return EncodeBase64(MakeUCharSpan(ssTx));
16871687
}
16881688

16891689
UniValue analyzepsbt(const JSONRPCRequest& request)

src/util/message.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool MessageSign(
6464
return false;
6565
}
6666

67-
signature = EncodeBase64(signature_bytes.data(), signature_bytes.size());
67+
signature = EncodeBase64(signature_bytes);
6868

6969
return true;
7070
}

src/util/strencodings.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,20 @@ void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
126126
hostOut = in;
127127
}
128128

129-
std::string EncodeBase64(const unsigned char* pch, size_t len)
129+
std::string EncodeBase64(Span<const unsigned char> input)
130130
{
131131
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
132132

133133
std::string str;
134-
str.reserve(((len + 2) / 3) * 4);
135-
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, pch, pch + len);
134+
str.reserve(((input.size() + 2) / 3) * 4);
135+
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
136136
while (str.size() % 4) str += '=';
137137
return str;
138138
}
139139

140140
std::string EncodeBase64(const std::string& str)
141141
{
142-
return EncodeBase64((const unsigned char*)str.data(), str.size());
142+
return EncodeBase64(MakeUCharSpan(str));
143143
}
144144

145145
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
@@ -201,20 +201,20 @@ std::string DecodeBase64(const std::string& str, bool* pf_invalid)
201201
return std::string((const char*)vchRet.data(), vchRet.size());
202202
}
203203

204-
std::string EncodeBase32(const unsigned char* pch, size_t len)
204+
std::string EncodeBase32(Span<const unsigned char> input)
205205
{
206206
static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
207207

208208
std::string str;
209-
str.reserve(((len + 4) / 5) * 8);
210-
ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, pch, pch + len);
209+
str.reserve(((input.size() + 4) / 5) * 8);
210+
ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
211211
while (str.size() % 8) str += '=';
212212
return str;
213213
}
214214

215215
std::string EncodeBase32(const std::string& str)
216216
{
217-
return EncodeBase32((const unsigned char*)str.data(), str.size());
217+
return EncodeBase32(MakeUCharSpan(str));
218218
}
219219

220220
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)

src/util/strencodings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ bool IsHex(const std::string& str);
4848
bool IsHexNumber(const std::string& str);
4949
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
5050
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
51-
std::string EncodeBase64(const unsigned char* pch, size_t len);
51+
std::string EncodeBase64(Span<const unsigned char> input);
5252
std::string EncodeBase64(const std::string& str);
5353
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
5454
std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
55-
std::string EncodeBase32(const unsigned char* pch, size_t len);
55+
std::string EncodeBase32(Span<const unsigned char> input);
5656
std::string EncodeBase32(const std::string& str);
5757

5858
void SplitHostPort(std::string in, int& portOut, std::string& hostOut);

0 commit comments

Comments
 (0)