aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/httprpc.cpp8
-rw-r--r--src/i2p.cpp7
-rw-r--r--src/netaddress.cpp20
-rw-r--r--src/psbt.cpp7
-rw-r--r--src/qt/walletframe.cpp6
-rw-r--r--src/test/base32_tests.cpp20
-rw-r--r--src/test/base64_tests.cpp20
-rw-r--r--src/test/fuzz/base_encode_decode.cpp13
-rw-r--r--src/util/message.cpp7
-rw-r--r--src/util/strencodings.cpp18
-rw-r--r--src/util/strencodings.h8
11 files changed, 57 insertions, 77 deletions
diff --git a/src/httprpc.cpp b/src/httprpc.cpp
index 96cccd8de9..b9041227be 100644
--- a/src/httprpc.cpp
+++ b/src/httprpc.cpp
@@ -132,10 +132,10 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
if (strAuth.substr(0, 6) != "Basic ")
return false;
std::string strUserPass64 = TrimString(strAuth.substr(6));
- bool invalid;
- std::vector<unsigned char> userpass_data = DecodeBase64(strUserPass64, &invalid);
- if (invalid) return false;
- std::string strUserPass(userpass_data.begin(), userpass_data.end());
+ auto userpass_data = DecodeBase64(strUserPass64);
+ std::string strUserPass;
+ if (!userpass_data) return false;
+ strUserPass.assign(userpass_data->begin(), userpass_data->end());
if (strUserPass.find(':') != std::string::npos)
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));
diff --git a/src/i2p.cpp b/src/i2p.cpp
index 66f19b90d0..e08b5461fe 100644
--- a/src/i2p.cpp
+++ b/src/i2p.cpp
@@ -69,12 +69,11 @@ static std::string SwapBase64(const std::string& from)
static Binary DecodeI2PBase64(const std::string& i2p_b64)
{
const std::string& std_b64 = SwapBase64(i2p_b64);
- bool invalid;
- Binary decoded = DecodeBase64(std_b64, &invalid);
- if (invalid) {
+ auto decoded = DecodeBase64(std_b64);
+ if (!decoded) {
throw std::runtime_error(strprintf("Cannot decode Base64: \"%s\"", i2p_b64));
}
- return decoded;
+ return std::move(*decoded);
}
/**
diff --git a/src/netaddress.cpp b/src/netaddress.cpp
index 18f56c9bbe..7943eaf257 100644
--- a/src/netaddress.cpp
+++ b/src/netaddress.cpp
@@ -234,17 +234,16 @@ bool CNetAddr::SetTor(const std::string& addr)
return false;
}
- bool invalid;
- const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len), &invalid);
+ auto input = DecodeBase32(addr.substr(0, addr.size() - suffix_len));
- if (invalid) {
+ if (!input) {
return false;
}
- if (input.size() == torv3::TOTAL_LEN) {
- Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
- Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
- Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
+ if (input->size() == torv3::TOTAL_LEN) {
+ Span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
+ Span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
+ Span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
if (input_version != torv3::VERSION) {
return false;
@@ -280,15 +279,14 @@ bool CNetAddr::SetI2P(const std::string& addr)
// can decode it.
const std::string b32_padded = addr.substr(0, b32_len) + "====";
- bool invalid;
- const auto& address_bytes = DecodeBase32(b32_padded.c_str(), &invalid);
+ auto address_bytes = DecodeBase32(b32_padded);
- if (invalid || address_bytes.size() != ADDR_I2P_SIZE) {
+ if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
return false;
}
m_net = NET_I2P;
- m_addr.assign(address_bytes.begin(), address_bytes.end());
+ m_addr.assign(address_bytes->begin(), address_bytes->end());
return true;
}
diff --git a/src/psbt.cpp b/src/psbt.cpp
index 9b8f909349..6465e353be 100644
--- a/src/psbt.cpp
+++ b/src/psbt.cpp
@@ -388,13 +388,12 @@ std::string PSBTRoleName(PSBTRole role) {
bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
{
- bool invalid;
- auto tx_data = DecodeBase64(base64_tx, &invalid);
- if (invalid) {
+ auto tx_data = DecodeBase64(base64_tx);
+ if (!tx_data) {
error = "invalid base64";
return false;
}
- return DecodeRawPSBT(psbt, MakeByteSpan(tx_data), error);
+ return DecodeRawPSBT(psbt, MakeByteSpan(*tx_data), error);
}
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span<const std::byte> tx_data, std::string& error)
diff --git a/src/qt/walletframe.cpp b/src/qt/walletframe.cpp
index 663e86aa29..dc4e25a02b 100644
--- a/src/qt/walletframe.cpp
+++ b/src/qt/walletframe.cpp
@@ -198,12 +198,12 @@ void WalletFrame::gotoLoadPSBT(bool from_clipboard)
if (from_clipboard) {
std::string raw = QApplication::clipboard()->text().toStdString();
- bool invalid;
- data = DecodeBase64(raw, &invalid);
- if (invalid) {
+ auto result = DecodeBase64(raw);
+ if (!result) {
Q_EMIT message(tr("Error"), tr("Unable to decode PSBT from clipboard (invalid base64)"), CClientUIInterface::MSG_ERROR);
return;
}
+ data = std::move(*result);
} else {
QString filename = GUIUtil::getOpenFileName(this,
tr("Load Transaction Data"), QString(),
diff --git a/src/test/base32_tests.cpp b/src/test/base32_tests.cpp
index 2e5f6a2378..c6109dfeb0 100644
--- a/src/test/base32_tests.cpp
+++ b/src/test/base32_tests.cpp
@@ -22,22 +22,16 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
strEnc = EncodeBase32(vstrIn[i], false);
BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
- bool invalid;
- auto dec = DecodeBase32(vstrOut[i], &invalid);
- BOOST_CHECK(!invalid);
- BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
+ auto dec = DecodeBase32(vstrOut[i]);
+ BOOST_REQUIRE(dec);
+ BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
}
// Decoding strings with embedded NUL characters should fail
- bool failure;
- (void)DecodeBase32("invalid\0"s, &failure); // correct size, invalid due to \0
- BOOST_CHECK(failure);
- (void)DecodeBase32("AWSX3VPP"s, &failure); // valid
- BOOST_CHECK(!failure);
- (void)DecodeBase32("AWSX3VPP\0invalid"s, &failure); // correct size, invalid due to \0
- BOOST_CHECK(failure);
- (void)DecodeBase32("AWSX3VPPinvalid"s, &failure); // invalid size
- BOOST_CHECK(failure);
+ BOOST_CHECK(!DecodeBase32("invalid\0"s)); // correct size, invalid due to \0
+ BOOST_CHECK(DecodeBase32("AWSX3VPP"s)); // valid
+ BOOST_CHECK(!DecodeBase32("AWSX3VPP\0invalid"s)); // correct size, invalid due to \0
+ BOOST_CHECK(!DecodeBase32("AWSX3VPPinvalid"s)); // invalid size
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/base64_tests.cpp b/src/test/base64_tests.cpp
index 5727b09838..54a02c6bf8 100644
--- a/src/test/base64_tests.cpp
+++ b/src/test/base64_tests.cpp
@@ -19,10 +19,9 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
{
std::string strEnc = EncodeBase64(vstrIn[i]);
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
- bool invalid;
- auto dec = DecodeBase64(strEnc, &invalid);
- BOOST_CHECK(!invalid);
- BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
+ auto dec = DecodeBase64(strEnc);
+ BOOST_REQUIRE(dec);
+ BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
}
{
@@ -36,15 +35,10 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
}
// Decoding strings with embedded NUL characters should fail
- bool failure;
- (void)DecodeBase64("invalid\0"s, &failure);
- BOOST_CHECK(failure);
- (void)DecodeBase64("nQB/pZw="s, &failure);
- BOOST_CHECK(!failure);
- (void)DecodeBase64("nQB/pZw=\0invalid"s, &failure);
- BOOST_CHECK(failure);
- (void)DecodeBase64("nQB/pZw=invalid\0"s, &failure);
- BOOST_CHECK(failure);
+ BOOST_CHECK(!DecodeBase64("invalid\0"s));
+ BOOST_CHECK(DecodeBase64("nQB/pZw="s));
+ BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"s));
+ BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"s));
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/fuzz/base_encode_decode.cpp b/src/test/fuzz/base_encode_decode.cpp
index 0d48a140f7..11dd820e4f 100644
--- a/src/test/fuzz/base_encode_decode.cpp
+++ b/src/test/fuzz/base_encode_decode.cpp
@@ -36,17 +36,16 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
}
- bool pf_invalid;
- decoded = DecodeBase32(random_encoded_string, &pf_invalid);
- if (!pf_invalid) {
- const std::string encoded_string = EncodeBase32(decoded);
+ auto result = DecodeBase32(random_encoded_string);
+ if (result) {
+ const std::string encoded_string = EncodeBase32(*result);
assert(encoded_string == TrimString(encoded_string));
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
}
- decoded = DecodeBase64(random_encoded_string, &pf_invalid);
- if (!pf_invalid) {
- const std::string encoded_string = EncodeBase64(decoded);
+ result = DecodeBase64(random_encoded_string);
+ if (result) {
+ const std::string encoded_string = EncodeBase64(*result);
assert(encoded_string == TrimString(encoded_string));
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
}
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.