diff options
author | Pieter Wuille <pieter@wuille.net> | 2022-04-04 11:58:54 -0400 |
---|---|---|
committer | MacroFake <falke.marco@gmail.com> | 2022-04-27 14:12:55 +0200 |
commit | 1a72d62152bfdd7c5c2b2704b679f894e7d35e37 (patch) | |
tree | 41327debd70464cb053d04d37d79baf3f4b99f8a /src | |
parent | 78f3ac51b7d073d12da6a3b9b7d80d91e04ce3a7 (diff) |
Generalize ConvertBits to permit transforming the input
Diffstat (limited to 'src')
-rw-r--r-- | src/util/strencodings.h | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 27f6ec6e00..8a277bac4a 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -249,15 +249,26 @@ bool TimingResistantEqual(const T& a, const T& b) */ [[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); +namespace { +/** Helper class for the default infn argument to ConvertBits (just returns the input). */ +struct IntIdentity +{ + [[maybe_unused]] int operator()(int x) const { return x; } +}; + +} // namespace + /** Convert from one power-of-2 number base to another. */ -template<int frombits, int tobits, bool pad, typename O, typename I> -bool ConvertBits(const O& outfn, I it, I end) { +template<int frombits, int tobits, bool pad, typename O, typename It, typename I = IntIdentity> +bool ConvertBits(O outfn, It it, It end, I infn = {}) { 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; + int v = infn(*it); + if (v < 0) return false; + acc = ((acc << frombits) | v) & max_acc; bits += frombits; while (bits >= tobits) { bits -= tobits; |