Make DecodeBase{32,64} take string_view arguments

This commit is contained in:
Pieter Wuille 2022-04-04 14:10:10 -04:00 committed by MacroFake
parent 1a72d62152
commit 8ffbd1412d
3 changed files with 27 additions and 64 deletions

View File

@ -234,7 +234,7 @@ bool CNetAddr::SetTor(const std::string& addr)
return false; return false;
} }
auto input = DecodeBase32(addr.substr(0, addr.size() - suffix_len)); auto input = DecodeBase32(std::string_view{addr}.substr(0, addr.size() - suffix_len));
if (!input) { if (!input) {
return false; return false;

View File

@ -126,7 +126,7 @@ std::string EncodeBase64(Span<const unsigned char> input)
return str; return str;
} }
std::optional<std::vector<unsigned char>> DecodeBase64(const char* p) std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str)
{ {
static const int8_t decode64_table[256]{ static const int8_t 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,
@ -144,42 +144,23 @@ std::optional<std::vector<unsigned char>> DecodeBase64(const char* p)
-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
}; };
const char* e = p; if (str.size() % 4 != 0) return {};
std::vector<uint8_t> val; /* One or two = characters at the end are permitted. */
val.reserve(strlen(p)); if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
while (*p != 0) { if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
int x = decode64_table[(unsigned char)*p];
if (x == -1) break;
val.push_back(uint8_t(x));
++p;
}
std::vector<unsigned char> ret; std::vector<unsigned char> ret;
ret.reserve((val.size() * 3) / 4); ret.reserve((str.size() * 3) / 4);
bool valid = ConvertBits<6, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end()); bool valid = ConvertBits<6, 8, false>(
[&](unsigned char c) { ret.push_back(c); },
const char* q = p; str.begin(), str.end(),
while (valid && *p != 0) { [](char c) { return decode64_table[uint8_t(c)]; }
if (*p != '=') { );
valid = false;
break;
}
++p;
}
valid = valid && (p - e) % 4 == 0 && p - q < 4;
if (!valid) return {}; if (!valid) return {};
return ret; return ret;
} }
std::optional<std::vector<unsigned char>> DecodeBase64(const std::string& str)
{
if (!ValidAsCString(str)) {
return {};
}
return DecodeBase64(str.c_str());
}
std::string EncodeBase32(Span<const unsigned char> input, bool pad) std::string EncodeBase32(Span<const unsigned char> input, bool pad)
{ {
static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
@ -200,7 +181,7 @@ std::string EncodeBase32(const std::string& str, bool pad)
return EncodeBase32(MakeUCharSpan(str), pad); return EncodeBase32(MakeUCharSpan(str), pad);
} }
std::optional<std::vector<unsigned char>> DecodeBase32(const char* p) std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str)
{ {
static const int8_t decode32_table[256]{ static const int8_t 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,
@ -218,42 +199,26 @@ std::optional<std::vector<unsigned char>> DecodeBase32(const char* p)
-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
}; };
const char* e = p; if (str.size() % 8 != 0) return {};
std::vector<uint8_t> val; /* 1, 3, 4, or 6 padding '=' suffix characters are permitted. */
val.reserve(strlen(p)); if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
while (*p != 0) { if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
int x = decode32_table[(unsigned char)*p]; if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
if (x == -1) break; if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
val.push_back(uint8_t(x));
++p;
}
std::vector<unsigned char> ret; std::vector<unsigned char> ret;
ret.reserve((val.size() * 5) / 8); ret.reserve((str.size() * 5) / 8);
bool valid = ConvertBits<5, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end()); bool valid = ConvertBits<5, 8, false>(
[&](unsigned char c) { ret.push_back(c); },
str.begin(), str.end(),
[](char c) { return decode32_table[uint8_t(c)]; }
);
const char* q = p;
while (valid && *p != 0) {
if (*p != '=') {
valid = false;
break;
}
++p;
}
valid = valid && (p - e) % 8 == 0 && p - q < 8;
if (!valid) return {}; if (!valid) return {};
return ret; return ret;
} }
std::optional<std::vector<unsigned char>> DecodeBase32(const std::string& str)
{
if (!ValidAsCString(str)) {
return {};
}
return DecodeBase32(str.c_str());
}
namespace { namespace {
template <typename T> template <typename T>
bool ParseIntegral(const std::string& str, T* out) bool ParseIntegral(const std::string& str, T* out)

View File

@ -64,13 +64,11 @@ bool IsHex(std::string_view str);
* Return true if the string is a hex number, optionally prefixed with "0x" * Return true if the string is a hex number, optionally prefixed with "0x"
*/ */
bool IsHexNumber(std::string_view str); bool IsHexNumber(std::string_view str);
std::optional<std::vector<unsigned char>> DecodeBase64(const char* p); std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
std::optional<std::vector<unsigned char>> DecodeBase64(const std::string& str);
std::string EncodeBase64(Span<const unsigned char> input); std::string EncodeBase64(Span<const unsigned char> input);
inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); } inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
inline std::string EncodeBase64(const std::string& str) { return EncodeBase64(MakeUCharSpan(str)); } inline std::string EncodeBase64(const std::string& str) { return EncodeBase64(MakeUCharSpan(str)); }
std::optional<std::vector<unsigned char>> DecodeBase32(const char* p); std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
std::optional<std::vector<unsigned char>> DecodeBase32(const std::string& str);
/** /**
* Base32 encode. * Base32 encode.