Changeset View
Changeset View
Standalone View
Standalone View
src/util/strencodings.cpp
Show First 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | void SplitHostPort(std::string in, int &portOut, std::string &hostOut) { | ||||
} | } | ||||
if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') { | if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') { | ||||
hostOut = in.substr(1, in.size() - 2); | hostOut = in.substr(1, in.size() - 2); | ||||
} else { | } else { | ||||
hostOut = in; | hostOut = in; | ||||
} | } | ||||
} | } | ||||
std::string EncodeBase64(const uint8_t *pch, size_t len) { | std::string EncodeBase64(Span<const uint8_t> input) { | ||||
static const char *pbase64 = | static const char *pbase64 = | ||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | "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) { | while (str.size() % 4) { | ||||
str += '='; | str += '='; | ||||
} | } | ||||
return str; | return str; | ||||
} | } | ||||
std::string EncodeBase64(const std::string &str) { | std::string EncodeBase64(const std::string &str) { | ||||
return EncodeBase64((const uint8_t *)str.data(), str.size()); | return EncodeBase64(MakeUCharSpan(str)); | ||||
} | } | ||||
std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid) { | std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid) { | ||||
static const int decode64_table[256] = { | static const int decode64_table[256] = { | ||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
-1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, | -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, | ||||
58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | if (!ValidAsCString(str)) { | ||||
*pf_invalid = true; | *pf_invalid = true; | ||||
} | } | ||||
return {}; | return {}; | ||||
} | } | ||||
std::vector<uint8_t> vchRet = DecodeBase64(str.c_str(), pf_invalid); | std::vector<uint8_t> vchRet = DecodeBase64(str.c_str(), pf_invalid); | ||||
return std::string((const char *)vchRet.data(), vchRet.size()); | return std::string((const char *)vchRet.data(), vchRet.size()); | ||||
} | } | ||||
std::string EncodeBase32(const uint8_t *pch, size_t len) { | std::string EncodeBase32(Span<const uint8_t> 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) { | while (str.size() % 8) { | ||||
str += '='; | str += '='; | ||||
} | } | ||||
return str; | return str; | ||||
} | } | ||||
std::string EncodeBase32(const std::string &str) { | std::string EncodeBase32(const std::string &str) { | ||||
return EncodeBase32((const uint8_t *)str.data(), str.size()); | return EncodeBase32(MakeUCharSpan(str)); | ||||
} | } | ||||
std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid) { | std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid) { | ||||
static const int decode32_table[256] = { | static const int decode32_table[256] = { | ||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, | ||||
30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | ||||
▲ Show 20 Lines • Show All 425 Lines • Show Last 20 Lines |