Skip to content

Commit

Permalink
util: make EncodeBase32 consume Spans
Browse files Browse the repository at this point in the history
  • Loading branch information
theStack authored and furszy committed Aug 10, 2021
1 parent 1f67e30 commit 8e50853
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/netaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ enum Network CNetAddr::GetNetwork() const
std::string CNetAddr::ToStringIP() const
{
if (IsTor())
return EncodeBase32(m_addr.data(), m_addr.size()) + ".onion";
return EncodeBase32(m_addr) + ".onion";
if (IsInternal())
return EncodeBase32(m_addr.data(), m_addr.size()) + ".internal";
return EncodeBase32(m_addr) + ".internal";
CService serv(*this, 0);
struct sockaddr_storage sockaddr;
socklen_t socklen = sizeof(sockaddr);
Expand Down
8 changes: 4 additions & 4 deletions src/utilstrencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,20 @@ std::string DecodeBase64(const std::string& str)
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";

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

std::string EncodeBase32(const std::string& str)
{
return EncodeBase32((const unsigned char*)str.c_str(), str.size());
return EncodeBase32(MakeUCharSpan(str));
}

std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
Expand Down
2 changes: 1 addition & 1 deletion src/utilstrencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ std::string EncodeBase64(Span<const unsigned char> input);
std::string EncodeBase64(const std::string& str);
std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
std::string DecodeBase32(const std::string& str);
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 i64tostr(int64_t n);
Expand Down

0 comments on commit 8e50853

Please sign in to comment.