Skip to content

Commit a65931e

Browse files
sipaMacroFake
authored andcommitted
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.
1 parent a4377a0 commit a65931e

13 files changed

+31
-30
lines changed

src/httprpc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
133133
return false;
134134
std::string strUserPass64 = TrimString(strAuth.substr(6));
135135
bool invalid;
136-
std::string strUserPass = DecodeBase64(strUserPass64, &invalid);
136+
std::vector<unsigned char> userpass_data = DecodeBase64(strUserPass64, &invalid);
137137
if (invalid) return false;
138+
std::string strUserPass(userpass_data.begin(), userpass_data.end());
138139

139140
if (strUserPass.find(':') != std::string::npos)
140141
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));

src/i2p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static Binary DecodeI2PBase64(const std::string& i2p_b64)
7070
{
7171
const std::string& std_b64 = SwapBase64(i2p_b64);
7272
bool invalid;
73-
Binary decoded = DecodeBase64(std_b64.c_str(), &invalid);
73+
Binary decoded = DecodeBase64(std_b64, &invalid);
7474
if (invalid) {
7575
throw std::runtime_error(strprintf("Cannot decode Base64: \"%s\"", i2p_b64));
7676
}

src/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ bool CNetAddr::SetTor(const std::string& addr)
235235
}
236236

237237
bool invalid;
238-
const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
238+
const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len), &invalid);
239239

240240
if (invalid) {
241241
return false;

src/psbt.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,17 +389,17 @@ std::string PSBTRoleName(PSBTRole role) {
389389
bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
390390
{
391391
bool invalid;
392-
std::string tx_data = DecodeBase64(base64_tx, &invalid);
392+
auto tx_data = DecodeBase64(base64_tx, &invalid);
393393
if (invalid) {
394394
error = "invalid base64";
395395
return false;
396396
}
397-
return DecodeRawPSBT(psbt, tx_data, error);
397+
return DecodeRawPSBT(psbt, MakeByteSpan(tx_data), error);
398398
}
399399

400-
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, const std::string& tx_data, std::string& error)
400+
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span<const std::byte> tx_data, std::string& error)
401401
{
402-
CDataStream ss_data(MakeByteSpan(tx_data), SER_NETWORK, PROTOCOL_VERSION);
402+
CDataStream ss_data(tx_data, SER_NETWORK, PROTOCOL_VERSION);
403403
try {
404404
ss_data >> psbt;
405405
if (!ss_data.empty()) {

src/psbt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,6 @@ bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransacti
988988
//! Decode a base64ed PSBT into a PartiallySignedTransaction
989989
[[nodiscard]] bool DecodeBase64PSBT(PartiallySignedTransaction& decoded_psbt, const std::string& base64_psbt, std::string& error);
990990
//! Decode a raw (binary blob) PSBT into a PartiallySignedTransaction
991-
[[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, const std::string& raw_psbt, std::string& error);
991+
[[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, Span<const std::byte> raw_psbt, std::string& error);
992992

993993
#endif // BITCOIN_PSBT_H

src/qt/walletframe.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void WalletFrame::gotoVerifyMessageTab(QString addr)
194194

195195
void WalletFrame::gotoLoadPSBT(bool from_clipboard)
196196
{
197-
std::string data;
197+
std::vector<unsigned char> data;
198198

199199
if (from_clipboard) {
200200
std::string raw = QApplication::clipboard()->text().toStdString();
@@ -214,12 +214,12 @@ void WalletFrame::gotoLoadPSBT(bool from_clipboard)
214214
return;
215215
}
216216
std::ifstream in{filename.toLocal8Bit().data(), std::ios::binary};
217-
data = std::string(std::istreambuf_iterator<char>{in}, {});
217+
data.assign(std::istream_iterator<unsigned char>{in}, {});
218218
}
219219

220220
std::string error;
221221
PartiallySignedTransaction psbtx;
222-
if (!DecodeRawPSBT(psbtx, data, error)) {
222+
if (!DecodeRawPSBT(psbtx, MakeByteSpan(data), error)) {
223223
Q_EMIT message(tr("Error"), tr("Unable to decode PSBT") + "\n" + QString::fromStdString(error), CClientUIInterface::MSG_ERROR);
224224
return;
225225
}

src/test/base32_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
2323
strEnc = EncodeBase32(vstrIn[i], false);
2424
BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
2525
bool invalid;
26-
std::string strDec = DecodeBase32(vstrOut[i], &invalid);
26+
auto dec = DecodeBase32(vstrOut[i], &invalid);
2727
BOOST_CHECK(!invalid);
28-
BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
28+
BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
2929
}
3030

3131
// Decoding strings with embedded NUL characters should fail

src/test/base64_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
2020
std::string strEnc = EncodeBase64(vstrIn[i]);
2121
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
2222
bool invalid;
23-
std::string strDec = DecodeBase64(strEnc, &invalid);
23+
auto dec = DecodeBase64(strEnc, &invalid);
2424
BOOST_CHECK(!invalid);
25-
BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
25+
BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
2626
}
2727

2828
{

src/test/fuzz/base_encode_decode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
3737
}
3838

3939
bool pf_invalid;
40-
std::string decoded_string = DecodeBase32(random_encoded_string, &pf_invalid);
40+
decoded = DecodeBase32(random_encoded_string, &pf_invalid);
4141
if (!pf_invalid) {
42-
const std::string encoded_string = EncodeBase32(decoded_string);
42+
const std::string encoded_string = EncodeBase32(decoded);
4343
assert(encoded_string == TrimString(encoded_string));
4444
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
4545
}
4646

47-
decoded_string = DecodeBase64(random_encoded_string, &pf_invalid);
47+
decoded = DecodeBase64(random_encoded_string, &pf_invalid);
4848
if (!pf_invalid) {
49-
const std::string encoded_string = EncodeBase64(decoded_string);
49+
const std::string encoded_string = EncodeBase64(decoded);
5050
assert(encoded_string == TrimString(encoded_string));
5151
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
5252
}

src/test/fuzz/psbt.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ FUZZ_TARGET_INIT(psbt, initialize_psbt)
3232
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
3333
PartiallySignedTransaction psbt_mut;
3434
std::string error;
35-
if (!DecodeRawPSBT(psbt_mut, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
35+
auto str = fuzzed_data_provider.ConsumeRandomLengthString();
36+
if (!DecodeRawPSBT(psbt_mut, MakeByteSpan(str), error)) {
3637
return;
3738
}
3839
const PartiallySignedTransaction psbt = psbt_mut;
@@ -79,7 +80,8 @@ FUZZ_TARGET_INIT(psbt, initialize_psbt)
7980
}
8081

8182
PartiallySignedTransaction psbt_merge;
82-
if (!DecodeRawPSBT(psbt_merge, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
83+
str = fuzzed_data_provider.ConsumeRandomLengthString();
84+
if (!DecodeRawPSBT(psbt_merge, MakeByteSpan(str), error)) {
8385
psbt_merge = psbt;
8486
}
8587
psbt_mut = psbt;

0 commit comments

Comments
 (0)