mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
Generalize ConvertBits to permit transforming the input
This commit is contained in:
@@ -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);
|
[[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. */
|
/** Convert from one power-of-2 number base to another. */
|
||||||
template<int frombits, int tobits, bool pad, typename O, typename I>
|
template<int frombits, int tobits, bool pad, typename O, typename It, typename I = IntIdentity>
|
||||||
bool ConvertBits(const O& outfn, I it, I end) {
|
bool ConvertBits(O outfn, It it, It end, I infn = {}) {
|
||||||
size_t acc = 0;
|
size_t acc = 0;
|
||||||
size_t bits = 0;
|
size_t bits = 0;
|
||||||
constexpr size_t maxv = (1 << tobits) - 1;
|
constexpr size_t maxv = (1 << tobits) - 1;
|
||||||
constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
|
constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
|
||||||
while (it != end) {
|
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;
|
bits += frombits;
|
||||||
while (bits >= tobits) {
|
while (bits >= tobits) {
|
||||||
bits -= tobits;
|
bits -= tobits;
|
||||||
|
|||||||
Reference in New Issue
Block a user