diff options
| author | Pieter Wuille <[email protected]> | 2017-08-25 19:55:52 -0700 |
|---|---|---|
| committer | Pieter Wuille <[email protected]> | 2017-09-28 16:24:30 -0700 |
| commit | c091b99379b97cb314c9fa123beabdbc324cf7a4 (patch) | |
| tree | f1cc720f195b18842831f964a3dd1fc71d29a4ff /src/utilstrencodings.h | |
| parent | Add regtest testing to base58_tests (diff) | |
| download | discoin-c091b99379b97cb314c9fa123beabdbc324cf7a4.tar.xz discoin-c091b99379b97cb314c9fa123beabdbc324cf7a4.zip | |
Implement BIP173 addresses and tests
Diffstat (limited to 'src/utilstrencodings.h')
| -rw-r--r-- | src/utilstrencodings.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/utilstrencodings.h b/src/utilstrencodings.h index 192f33fb2..af33f0e5f 100644 --- a/src/utilstrencodings.h +++ b/src/utilstrencodings.h @@ -149,4 +149,28 @@ bool TimingResistantEqual(const T& a, const T& b) */ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); +/** Convert from one power-of-2 number base to another. */ +template<int frombits, int tobits, bool pad, typename O, typename I> +bool ConvertBits(O& out, I it, I end) { + size_t acc = 0; + size_t bits = 0; + constexpr size_t maxv = (1 << tobits) - 1; + constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1; + while (it != end) { + acc = ((acc << frombits) | *it) & max_acc; + bits += frombits; + while (bits >= tobits) { + bits -= tobits; + out.push_back((acc >> bits) & maxv); + } + ++it; + } + if (pad) { + if (bits) out.push_back((acc << (tobits - bits)) & maxv); + } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { + return false; + } + return true; +} + #endif // BITCOIN_UTILSTRENCODINGS_H |