aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPieter Wuille <pieter@wuille.net>2022-04-04 13:19:49 -0400
committerMacroFake <falke.marco@gmail.com>2022-04-27 14:12:55 +0200
commita65931e3ce66d87b8f83d67ecdbb46f137e6a670 (patch)
tree1cdbaa2a15af7b87d7a963c4c759c2a29ddbe437
parenta4377a0843636eae0aaf698510fc6518582545db (diff)
downloadbitcoin-a65931e3ce66d87b8f83d67ecdbb46f137e6a670.tar.xz
Make DecodeBase{32,64} always return vector, not string
Base32/base64 are mechanisms for encoding binary data. That they'd decode to a string is just bizarre. The fact that they'd do that based on the type of input arguments even more so.
-rw-r--r--src/httprpc.cpp3
-rw-r--r--src/i2p.cpp2
-rw-r--r--src/netaddress.cpp2
-rw-r--r--src/psbt.cpp8
-rw-r--r--src/psbt.h2
-rw-r--r--src/qt/walletframe.cpp6
-rw-r--r--src/test/base32_tests.cpp4
-rw-r--r--src/test/base64_tests.cpp4
-rw-r--r--src/test/fuzz/base_encode_decode.cpp8
-rw-r--r--src/test/fuzz/psbt.cpp6
-rw-r--r--src/util/message.cpp2
-rw-r--r--src/util/strencodings.cpp10
-rw-r--r--src/util/strencodings.h4
13 files changed, 31 insertions, 30 deletions
diff --git a/src/httprpc.cpp b/src/httprpc.cpp
index 5d62568343..96cccd8de9 100644
--- a/src/httprpc.cpp
+++ b/src/httprpc.cpp
@@ -133,8 +133,9 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
return false;
std::string strUserPass64 = TrimString(strAuth.substr(6));
bool invalid;
- std::string strUserPass = DecodeBase64(strUserPass64, &invalid);
+ std::vector<unsigned char> userpass_data = DecodeBase64(strUserPass64, &invalid);
if (invalid) return false;
+ std::string strUserPass(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 ccba14d63d..66f19b90d0 100644
--- a/src/i2p.cpp
+++ b/src/i2p.cpp
@@ -70,7 +70,7 @@ static Binary DecodeI2PBase64(const std::string& i2p_b64)
{
const std::string& std_b64 = SwapBase64(i2p_b64);
bool invalid;
- Binary decoded = DecodeBase64(std_b64.c_str(), &invalid);
+ Binary decoded = DecodeBase64(std_b64, &invalid);
if (invalid) {
throw std::runtime_error(strprintf("Cannot decode Base64: \"%s\"", i2p_b64));
}
diff --git a/src/netaddress.cpp b/src/netaddress.cpp
index bc1915aad9..18f56c9bbe 100644
--- a/src/netaddress.cpp
+++ b/src/netaddress.cpp
@@ -235,7 +235,7 @@ bool CNetAddr::SetTor(const std::string& addr)
}
bool invalid;
- const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
+ const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len), &invalid);
if (invalid) {
return false;
diff --git a/src/psbt.cpp b/src/psbt.cpp
index c8c73e130b..9b8f909349 100644
--- a/src/psbt.cpp
+++ b/src/psbt.cpp
@@ -389,17 +389,17 @@ std::string PSBTRoleName(PSBTRole role) {
bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
{
bool invalid;
- std::string tx_data = DecodeBase64(base64_tx, &invalid);
+ auto tx_data = DecodeBase64(base64_tx, &invalid);
if (invalid) {
error = "invalid base64";
return false;
}
- return DecodeRawPSBT(psbt, tx_data, error);
+ return DecodeRawPSBT(psbt, MakeByteSpan(tx_data), error);
}
-bool DecodeRawPSBT(PartiallySignedTransaction& psbt, const std::string& tx_data, std::string& error)
+bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span<const std::byte> tx_data, std::string& error)
{
- CDataStream ss_data(MakeByteSpan(tx_data), SER_NETWORK, PROTOCOL_VERSION);
+ CDataStream ss_data(tx_data, SER_NETWORK, PROTOCOL_VERSION);
try {
ss_data >> psbt;
if (!ss_data.empty()) {
diff --git a/src/psbt.h b/src/psbt.h
index f0ceb02481..8a9cbd33d2 100644
--- a/src/psbt.h
+++ b/src/psbt.h
@@ -988,6 +988,6 @@ bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransacti
//! Decode a base64ed PSBT into a PartiallySignedTransaction
[[nodiscard]] bool DecodeBase64PSBT(PartiallySignedTransaction& decoded_psbt, const std::string& base64_psbt, std::string& error);
//! Decode a raw (binary blob) PSBT into a PartiallySignedTransaction
-[[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, const std::string& raw_psbt, std::string& error);
+[[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, Span<const std::byte> raw_psbt, std::string& error);
#endif // BITCOIN_PSBT_H
diff --git a/src/qt/walletframe.cpp b/src/qt/walletframe.cpp
index 91ce420a33..663e86aa29 100644
--- a/src/qt/walletframe.cpp
+++ b/src/qt/walletframe.cpp
@@ -194,7 +194,7 @@ void WalletFrame::gotoVerifyMessageTab(QString addr)
void WalletFrame::gotoLoadPSBT(bool from_clipboard)
{
- std::string data;
+ std::vector<unsigned char> data;
if (from_clipboard) {
std::string raw = QApplication::clipboard()->text().toStdString();
@@ -214,12 +214,12 @@ void WalletFrame::gotoLoadPSBT(bool from_clipboard)
return;
}
std::ifstream in{filename.toLocal8Bit().data(), std::ios::binary};
- data = std::string(std::istreambuf_iterator<char>{in}, {});
+ data.assign(std::istream_iterator<unsigned char>{in}, {});
}
std::string error;
PartiallySignedTransaction psbtx;
- if (!DecodeRawPSBT(psbtx, data, error)) {
+ if (!DecodeRawPSBT(psbtx, MakeByteSpan(data), error)) {
Q_EMIT message(tr("Error"), tr("Unable to decode PSBT") + "\n" + QString::fromStdString(error), CClientUIInterface::MSG_ERROR);
return;
}
diff --git a/src/test/base32_tests.cpp b/src/test/base32_tests.cpp
index 8a44e65165..2e5f6a2378 100644
--- a/src/test/base32_tests.cpp
+++ b/src/test/base32_tests.cpp
@@ -23,9 +23,9 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
strEnc = EncodeBase32(vstrIn[i], false);
BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
bool invalid;
- std::string strDec = DecodeBase32(vstrOut[i], &invalid);
+ auto dec = DecodeBase32(vstrOut[i], &invalid);
BOOST_CHECK(!invalid);
- BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
+ BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
}
// Decoding strings with embedded NUL characters should fail
diff --git a/src/test/base64_tests.cpp b/src/test/base64_tests.cpp
index 04b5185653..5727b09838 100644
--- a/src/test/base64_tests.cpp
+++ b/src/test/base64_tests.cpp
@@ -20,9 +20,9 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
std::string strEnc = EncodeBase64(vstrIn[i]);
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
bool invalid;
- std::string strDec = DecodeBase64(strEnc, &invalid);
+ auto dec = DecodeBase64(strEnc, &invalid);
BOOST_CHECK(!invalid);
- BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
+ BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
}
{
diff --git a/src/test/fuzz/base_encode_decode.cpp b/src/test/fuzz/base_encode_decode.cpp
index 196410e29c..0d48a140f7 100644
--- a/src/test/fuzz/base_encode_decode.cpp
+++ b/src/test/fuzz/base_encode_decode.cpp
@@ -37,16 +37,16 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
}
bool pf_invalid;
- std::string decoded_string = DecodeBase32(random_encoded_string, &pf_invalid);
+ decoded = DecodeBase32(random_encoded_string, &pf_invalid);
if (!pf_invalid) {
- const std::string encoded_string = EncodeBase32(decoded_string);
+ const std::string encoded_string = EncodeBase32(decoded);
assert(encoded_string == TrimString(encoded_string));
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
}
- decoded_string = DecodeBase64(random_encoded_string, &pf_invalid);
+ decoded = DecodeBase64(random_encoded_string, &pf_invalid);
if (!pf_invalid) {
- const std::string encoded_string = EncodeBase64(decoded_string);
+ const std::string encoded_string = EncodeBase64(decoded);
assert(encoded_string == TrimString(encoded_string));
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
}
diff --git a/src/test/fuzz/psbt.cpp b/src/test/fuzz/psbt.cpp
index 669688a80d..baa64bba0f 100644
--- a/src/test/fuzz/psbt.cpp
+++ b/src/test/fuzz/psbt.cpp
@@ -32,7 +32,8 @@ FUZZ_TARGET_INIT(psbt, initialize_psbt)
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
PartiallySignedTransaction psbt_mut;
std::string error;
- if (!DecodeRawPSBT(psbt_mut, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
+ auto str = fuzzed_data_provider.ConsumeRandomLengthString();
+ if (!DecodeRawPSBT(psbt_mut, MakeByteSpan(str), error)) {
return;
}
const PartiallySignedTransaction psbt = psbt_mut;
@@ -79,7 +80,8 @@ FUZZ_TARGET_INIT(psbt, initialize_psbt)
}
PartiallySignedTransaction psbt_merge;
- if (!DecodeRawPSBT(psbt_merge, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
+ str = fuzzed_data_provider.ConsumeRandomLengthString();
+ if (!DecodeRawPSBT(psbt_merge, MakeByteSpan(str), error)) {
psbt_merge = psbt;
}
psbt_mut = psbt;
diff --git a/src/util/message.cpp b/src/util/message.cpp
index 2c7f0406f0..a4e495c1d1 100644
--- a/src/util/message.cpp
+++ b/src/util/message.cpp
@@ -36,7 +36,7 @@ MessageVerificationResult MessageVerify(
}
bool invalid = false;
- std::vector<unsigned char> signature_bytes = DecodeBase64(signature.c_str(), &invalid);
+ std::vector<unsigned char> signature_bytes = DecodeBase64(signature, &invalid);
if (invalid) {
return MessageVerificationResult::ERR_MALFORMED_SIGNATURE;
}
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp
index 6b6644aa9f..a861885269 100644
--- a/src/util/strencodings.cpp
+++ b/src/util/strencodings.cpp
@@ -172,14 +172,13 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
return ret;
}
-std::string DecodeBase64(const std::string& str, bool* pf_invalid)
+std::vector<unsigned char> DecodeBase64(const std::string& str, bool* pf_invalid)
{
if (!ValidAsCString(str)) {
*pf_invalid = true;
return {};
}
- std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid);
- return std::string((const char*)vchRet.data(), vchRet.size());
+ return DecodeBase64(str.c_str(), pf_invalid);
}
std::string EncodeBase32(Span<const unsigned char> input, bool pad)
@@ -248,14 +247,13 @@ std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
return ret;
}
-std::string DecodeBase32(const std::string& str, bool* pf_invalid)
+std::vector<unsigned char> DecodeBase32(const std::string& str, bool* pf_invalid)
{
if (!ValidAsCString(str)) {
*pf_invalid = true;
return {};
}
- std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid);
- return std::string((const char*)vchRet.data(), vchRet.size());
+ return DecodeBase32(str.c_str(), pf_invalid);
}
namespace {
diff --git a/src/util/strencodings.h b/src/util/strencodings.h
index c87b2d2415..5d85f0580b 100644
--- a/src/util/strencodings.h
+++ b/src/util/strencodings.h
@@ -65,12 +65,12 @@ bool IsHex(std::string_view str);
*/
bool IsHexNumber(std::string_view str);
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid);
-std::string DecodeBase64(const std::string& str, bool* pf_invalid);
+std::vector<unsigned char> DecodeBase64(const std::string& str, bool* pf_invalid);
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::string DecodeBase32(const std::string& str, bool* pf_invalid);
+std::vector<unsigned char> DecodeBase32(const std::string& str, bool* pf_invalid);
/**
* Base32 encode.