Make DecodeBase{32,64} return optional instead of taking bool*

This commit is contained in:
Pieter Wuille
2022-04-04 13:52:06 -04:00
committed by MacroFake
parent a65931e3ce
commit 78f3ac51b7
11 changed files with 57 additions and 77 deletions

View File

@@ -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;
}