0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-13 11:25:02 -05:00

Merge #19687: refactor: make EncodeBase{32,64} consume Spans

e2aa1a585a util: make EncodeBase64 consume Spans (Sebastian Falbesoner)
2bc207190e 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 e2aa1a585a 🐮
  vasild:
    ACK e2aa1a585

Tree-SHA512: 43bd3bd2ee8e3be2474db0a81dae9d9e88fac2464b96d1b042147106ed7433799dcba3000c69990511ecfc697b0c7306ce85f2ecb2293e2e44fd356c9694b150
This commit is contained in:
MarcoFalke 2020-08-26 11:52:26 +02:00
commit a12d9e5fd2
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
5 changed files with 20 additions and 19 deletions

View file

@ -4,10 +4,11 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <netaddress.h> #include <netaddress.h>
#include <hash.h> #include <hash.h>
#include <tinyformat.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/asmap.h> #include <util/asmap.h>
#include <tinyformat.h>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
@ -341,9 +342,9 @@ enum Network CNetAddr::GetNetwork() const
std::string CNetAddr::ToStringIP() const std::string CNetAddr::ToStringIP() const
{ {
if (IsTor()) if (IsTor())
return EncodeBase32(m_addr.data(), m_addr.size()) + ".onion"; return EncodeBase32(m_addr) + ".onion";
if (IsInternal()) if (IsInternal())
return EncodeBase32(m_addr.data(), m_addr.size()) + ".internal"; return EncodeBase32(m_addr) + ".internal";
CService serv(*this, 0); CService serv(*this, 0);
struct sockaddr_storage sockaddr; struct sockaddr_storage sockaddr;
socklen_t socklen = sizeof(sockaddr); socklen_t socklen = sizeof(sockaddr);

View file

@ -1300,7 +1300,7 @@ UniValue combinepsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << merged_psbt; ssTx << merged_psbt;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue finalizepsbt(const JSONRPCRequest& request) UniValue finalizepsbt(const JSONRPCRequest& request)
@ -1435,7 +1435,7 @@ UniValue createpsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << psbtx; ssTx << psbtx;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue converttopsbt(const JSONRPCRequest& request) UniValue converttopsbt(const JSONRPCRequest& request)
@ -1502,7 +1502,7 @@ UniValue converttopsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << psbtx; ssTx << psbtx;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue utxoupdatepsbt(const JSONRPCRequest& request) UniValue utxoupdatepsbt(const JSONRPCRequest& request)
@ -1590,7 +1590,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << psbtx; ssTx << psbtx;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue joinpsbts(const JSONRPCRequest& request) UniValue joinpsbts(const JSONRPCRequest& request)
@ -1683,7 +1683,7 @@ UniValue joinpsbts(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << shuffled_psbt; ssTx << shuffled_psbt;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue analyzepsbt(const JSONRPCRequest& request) UniValue analyzepsbt(const JSONRPCRequest& request)

View file

@ -64,7 +64,7 @@ bool MessageSign(
return false; return false;
} }
signature = EncodeBase64(signature_bytes.data(), signature_bytes.size()); signature = EncodeBase64(signature_bytes);
return true; return true;
} }

View file

@ -126,20 +126,20 @@ void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
hostOut = in; hostOut = in;
} }
std::string EncodeBase64(const unsigned char* pch, size_t len) std::string EncodeBase64(Span<const unsigned char> input)
{ {
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string str; std::string str;
str.reserve(((len + 2) / 3) * 4); str.reserve(((input.size() + 2) / 3) * 4);
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, pch, pch + len); ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
while (str.size() % 4) str += '='; while (str.size() % 4) str += '=';
return str; return str;
} }
std::string EncodeBase64(const std::string& str) std::string EncodeBase64(const std::string& str)
{ {
return EncodeBase64((const unsigned char*)str.data(), str.size()); return EncodeBase64(MakeUCharSpan(str));
} }
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid) 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)
return std::string((const char*)vchRet.data(), vchRet.size()); return std::string((const char*)vchRet.data(), vchRet.size());
} }
std::string EncodeBase32(const unsigned char* pch, size_t len) std::string EncodeBase32(Span<const unsigned char> input)
{ {
static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
std::string str; std::string str;
str.reserve(((len + 4) / 5) * 8); str.reserve(((input.size() + 4) / 5) * 8);
ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, pch, pch + len); ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
while (str.size() % 8) str += '='; while (str.size() % 8) str += '=';
return str; return str;
} }
std::string EncodeBase32(const std::string& str) std::string EncodeBase32(const std::string& str)
{ {
return EncodeBase32((const unsigned char*)str.data(), str.size()); return EncodeBase32(MakeUCharSpan(str));
} }
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid) std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)

View file

@ -48,11 +48,11 @@ bool IsHex(const std::string& str);
bool IsHexNumber(const std::string& str); bool IsHexNumber(const std::string& str);
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr); std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr); std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
std::string EncodeBase64(const unsigned char* pch, size_t len); std::string EncodeBase64(Span<const unsigned char> input);
std::string EncodeBase64(const std::string& str); std::string EncodeBase64(const std::string& str);
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr); std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr); std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
std::string EncodeBase32(const unsigned char* pch, size_t len); std::string EncodeBase32(Span<const unsigned char> input);
std::string EncodeBase32(const std::string& str); std::string EncodeBase32(const std::string& str);
void SplitHostPort(std::string in, int& portOut, std::string& hostOut); void SplitHostPort(std::string in, int& portOut, std::string& hostOut);