Skip to content

Commit

Permalink
Make DecodeBase{32,64} return optional instead of taking bool*
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and PastaPastaPasta committed Jan 11, 2025
1 parent 765ec3f commit 9519ace
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 77 deletions.
8 changes: 4 additions & 4 deletions src/httprpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,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(':'));
Expand Down
7 changes: 3 additions & 4 deletions src/i2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
20 changes: 9 additions & 11 deletions src/netaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,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;
Expand Down Expand Up @@ -281,15 +280,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;
}
Expand Down
7 changes: 3 additions & 4 deletions src/psbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,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)
Expand Down
6 changes: 3 additions & 3 deletions src/qt/walletframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,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(),
Expand Down
20 changes: 7 additions & 13 deletions src/test/base32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
20 changes: 7 additions & 13 deletions src/test/base64_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

{
Expand All @@ -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()
13 changes: 6 additions & 7 deletions src/test/fuzz/base_encode_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ FUZZ_TARGET(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)));
}
Expand Down
7 changes: 3 additions & 4 deletions src/util/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 8 additions & 10 deletions src/util/strencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,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,
Expand Down Expand Up @@ -170,18 +170,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)
Expand All @@ -204,7 +203,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,
Expand Down Expand Up @@ -245,18 +244,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 {
Expand Down
8 changes: 4 additions & 4 deletions src/util/strencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9519ace

Please sign in to comment.