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/strencodings.cpp | |
parent | a65931e3ce66d87b8f83d67ecdbb46f137e6a670 (diff) | |
download | bitcoin-78f3ac51b7d073d12da6a3b9b7d80d91e04ce3a7.tar.xz |
Make DecodeBase{32,64} return optional instead of taking bool*
Diffstat (limited to 'src/util/strencodings.cpp')
-rw-r--r-- | src/util/strencodings.cpp | 18 |
1 files changed, 8 insertions, 10 deletions
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 { |