mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
util: make EncodeBase58 consume Spans
This commit is contained in:
parent
bd00d3b1f2
commit
f0fce0675d
5 changed files with 13 additions and 23 deletions
|
@ -84,21 +84,21 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_
|
|||
return true;
|
||||
}
|
||||
|
||||
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
|
||||
std::string EncodeBase58(Span<const unsigned char> input)
|
||||
{
|
||||
// Skip & count leading zeroes.
|
||||
int zeroes = 0;
|
||||
int length = 0;
|
||||
while (pbegin != pend && *pbegin == 0) {
|
||||
pbegin++;
|
||||
while (input.size() > 0 && input[0] == 0) {
|
||||
input = input.subspan(1);
|
||||
zeroes++;
|
||||
}
|
||||
// Allocate enough space in big-endian base58 representation.
|
||||
int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up.
|
||||
int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up.
|
||||
std::vector<unsigned char> b58(size);
|
||||
// Process the bytes.
|
||||
while (pbegin != pend) {
|
||||
int carry = *pbegin;
|
||||
while (input.size() > 0) {
|
||||
int carry = input[0];
|
||||
int i = 0;
|
||||
// Apply "b58 = b58 * 256 + ch".
|
||||
for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
|
||||
|
@ -109,7 +109,7 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
|
|||
|
||||
assert(carry == 0);
|
||||
length = i;
|
||||
pbegin++;
|
||||
input = input.subspan(1);
|
||||
}
|
||||
// Skip leading zeroes in base58 result.
|
||||
std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
|
||||
|
@ -124,11 +124,6 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
|
|||
return str;
|
||||
}
|
||||
|
||||
std::string EncodeBase58(const std::vector<unsigned char>& vch)
|
||||
{
|
||||
return EncodeBase58(vch.data(), vch.data() + vch.size());
|
||||
}
|
||||
|
||||
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len)
|
||||
{
|
||||
if (!ValidAsCString(str)) {
|
||||
|
|
11
src/base58.h
11
src/base58.h
|
@ -15,20 +15,15 @@
|
|||
#define BITCOIN_BASE58_H
|
||||
|
||||
#include <attributes.h>
|
||||
#include <span.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* Encode a byte sequence as a base58-encoded string.
|
||||
* pbegin and pend cannot be nullptr, unless both are.
|
||||
* Encode a byte span as a base58-encoded string
|
||||
*/
|
||||
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
|
||||
|
||||
/**
|
||||
* Encode a byte vector as a base58-encoded string
|
||||
*/
|
||||
std::string EncodeBase58(const std::vector<unsigned char>& vch);
|
||||
std::string EncodeBase58(Span<const unsigned char> input);
|
||||
|
||||
/**
|
||||
* Decode a base58-encoded string (psz) into a byte vector (vchRet).
|
||||
|
|
|
@ -20,7 +20,7 @@ static void Base58Encode(benchmark::Bench& bench)
|
|||
}
|
||||
};
|
||||
bench.batch(buff.size()).unit("byte").run([&] {
|
||||
EncodeBase58(buff.data(), buff.data() + buff.size());
|
||||
EncodeBase58(buff);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -94,7 +94,7 @@ static std::string DummyAddress(const CChainParams ¶ms)
|
|||
std::vector<unsigned char> sourcedata = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
|
||||
sourcedata.insert(sourcedata.end(), dummydata, dummydata + sizeof(dummydata));
|
||||
for(int i=0; i<256; ++i) { // Try every trailing byte
|
||||
std::string s = EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size());
|
||||
std::string s = EncodeBase58(sourcedata);
|
||||
if (!IsValidDestinationString(s)) {
|
||||
return s;
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
|
|||
std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
|
||||
std::string base58string = test[1].get_str();
|
||||
BOOST_CHECK_MESSAGE(
|
||||
EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
|
||||
EncodeBase58(sourcedata) == base58string,
|
||||
strTest);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue