Make DecodeBase{32,64} return optional instead of taking bool*

This commit is contained in:
Pieter Wuille
2022-04-04 13:52:06 -04:00
committed by MacroFake
parent a65931e3ce
commit 78f3ac51b7
11 changed files with 57 additions and 77 deletions

View File

@@ -35,14 +35,13 @@ MessageVerificationResult MessageVerify(
return MessageVerificationResult::ERR_ADDRESS_NO_KEY;
}
bool invalid = false;
std::vector<unsigned char> signature_bytes = DecodeBase64(signature, &invalid);
if (invalid) {
auto signature_bytes = DecodeBase64(signature);
if (!signature_bytes) {
return MessageVerificationResult::ERR_MALFORMED_SIGNATURE;
}
CPubKey pubkey;
if (!pubkey.RecoverCompact(MessageHash(message), signature_bytes)) {
if (!pubkey.RecoverCompact(MessageHash(message), *signature_bytes)) {
return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED;
}

View File

@@ -126,7 +126,7 @@ std::string EncodeBase64(Span<const unsigned char> input)
return str;
}
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
std::optional<std::vector<unsigned char>> DecodeBase64(const char* p)
{
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,
@@ -167,18 +167,17 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
++p;
}
valid = valid && (p - e) % 4 == 0 && p - q < 4;
*pf_invalid = !valid;
if (!valid) return {};
return ret;
}
std::vector<unsigned char> DecodeBase64(const std::string& str, bool* pf_invalid)
std::optional<std::vector<unsigned char>> DecodeBase64(const std::string& str)
{
if (!ValidAsCString(str)) {
*pf_invalid = true;
return {};
}
return DecodeBase64(str.c_str(), pf_invalid);
return DecodeBase64(str.c_str());
}
std::string EncodeBase32(Span<const unsigned char> input, bool pad)
@@ -201,7 +200,7 @@ std::string EncodeBase32(const std::string& str, bool pad)
return EncodeBase32(MakeUCharSpan(str), pad);
}
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
std::optional<std::vector<unsigned char>> DecodeBase32(const char* p)
{
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,
@@ -242,18 +241,17 @@ std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
++p;
}
valid = valid && (p - e) % 8 == 0 && p - q < 8;
*pf_invalid = !valid;
if (!valid) return {};
return ret;
}
std::vector<unsigned char> DecodeBase32(const std::string& str, bool* pf_invalid)
std::optional<std::vector<unsigned char>> DecodeBase32(const std::string& str)
{
if (!ValidAsCString(str)) {
*pf_invalid = true;
return {};
}
return DecodeBase32(str.c_str(), pf_invalid);
return DecodeBase32(str.c_str());
}
namespace {

View File

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