diff options
author | Pieter Wuille <pieter@wuille.net> | 2022-04-04 13:52:06 -0400 |
---|---|---|
committer | MacroFake <falke.marco@gmail.com> | 2022-04-27 14:12:55 +0200 |
commit | 78f3ac51b7d073d12da6a3b9b7d80d91e04ce3a7 (patch) | |
tree | 60a138e61a7281fbba6391da8d0d73bd0c1f9fee /src/util | |
parent | a65931e3ce66d87b8f83d67ecdbb46f137e6a670 (diff) | |
download | bitcoin-78f3ac51b7d073d12da6a3b9b7d80d91e04ce3a7.tar.xz |
Make DecodeBase{32,64} return optional instead of taking bool*
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/message.cpp | 7 | ||||
-rw-r--r-- | src/util/strencodings.cpp | 18 | ||||
-rw-r--r-- | src/util/strencodings.h | 8 |
3 files changed, 15 insertions, 18 deletions
diff --git a/src/util/message.cpp b/src/util/message.cpp index a4e495c1d1..f58876f915 100644 --- a/src/util/message.cpp +++ b/src/util/message.cpp @@ -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; } diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index a861885269..c7c9870a02 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -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 { diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 5d85f0580b..27f6ec6e00 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -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. |