From d2bb681f96fb327b4c4d5b2b113692ca22fdffbf Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Mon, 31 Aug 2020 10:39:00 +0200 Subject: util: move HasPrefix() so it can be reused Move the function `HasPrefix()` from `netaddress.cpp` to `util/string.h` so it can be reused by `CNetAddr` methods (and possibly others). --- src/util/string.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/util') diff --git a/src/util/string.h b/src/util/string.h index cdb41630c6..a0c87bd00c 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -7,6 +7,8 @@ #include +#include +#include #include #include #include @@ -74,4 +76,15 @@ std::string ToString(const T& t) return oss.str(); } +/** + * Check whether a container begins with the given prefix. + */ +template +NODISCARD inline bool HasPrefix(const T1& obj, + const std::array& prefix) +{ + return obj.size() >= PREFIX_LEN && + std::equal(std::begin(prefix), std::end(prefix), std::begin(obj)); +} + #endif // BITCOIN_UTIL_STRENCODINGS_H -- cgit v1.2.3 From 7be6ff61875a8d5d2335bff5d1f16ba40557adb0 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Thu, 27 Aug 2020 11:03:21 +0200 Subject: net: recognize TORv3/I2P/CJDNS networks Recognizing addresses from those networks allows us to accept and gossip them, even though we don't know how to connect to them (yet). Co-authored-by: eriknylund --- src/util/strencodings.cpp | 12 ++++++++---- src/util/strencodings.h | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'src/util') diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 079a4529a3..3236184b0b 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -201,20 +201,24 @@ std::string DecodeBase64(const std::string& str, bool* pf_invalid) return std::string((const char*)vchRet.data(), vchRet.size()); } -std::string EncodeBase32(Span input) +std::string EncodeBase32(Span input, bool pad) { static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; std::string str; 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 += '='; + if (pad) { + while (str.size() % 8) { + str += '='; + } + } return str; } -std::string EncodeBase32(const std::string& str) +std::string EncodeBase32(const std::string& str, bool pad) { - return EncodeBase32(MakeUCharSpan(str)); + return EncodeBase32(MakeUCharSpan(str), pad); } std::vector DecodeBase32(const char* p, bool* pf_invalid) diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 1519214140..1a217dd12d 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -52,8 +52,20 @@ std::string EncodeBase64(Span input); std::string EncodeBase64(const std::string& str); std::vector DecodeBase32(const char* p, bool* pf_invalid = nullptr); std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr); -std::string EncodeBase32(Span input); -std::string EncodeBase32(const std::string& str); + +/** + * Base32 encode. + * If `pad` is true, then the output will be padded with '=' so that its length + * is a multiple of 8. + */ +std::string EncodeBase32(Span input, bool pad = true); + +/** + * Base32 encode. + * If `pad` is true, then the output will be padded with '=' so that its length + * is a multiple of 8. + */ +std::string EncodeBase32(const std::string& str, bool pad = true); void SplitHostPort(std::string in, int& portOut, std::string& hostOut); int64_t atoi64(const std::string& str); -- cgit v1.2.3