From 3296a3bb7fc0a6c47b60c79e968dbf8175d6b716 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 22 Nov 2017 17:04:48 -0800 Subject: [PATCH] Generalize ConvertBits --- src/key_io.cpp | 8 ++++---- src/utilstrencodings.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/key_io.cpp b/src/key_io.cpp index 20df945d8d..0cc4927679 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -43,14 +43,14 @@ public: std::string operator()(const WitnessV0KeyHash& id) const { std::vector data = {0}; - ConvertBits<8, 5, true>(data, id.begin(), id.end()); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end()); return bech32::Encode(m_params.Bech32HRP(), data); } std::string operator()(const WitnessV0ScriptHash& id) const { std::vector data = {0}; - ConvertBits<8, 5, true>(data, id.begin(), id.end()); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end()); return bech32::Encode(m_params.Bech32HRP(), data); } @@ -60,7 +60,7 @@ public: return {}; } std::vector data = {(unsigned char)id.version}; - ConvertBits<8, 5, true>(data, id.program, id.program + id.length); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length); return bech32::Encode(m_params.Bech32HRP(), data); } @@ -94,7 +94,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par // Bech32 decoding int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16) // The rest of the symbols are converted witness program bytes. - if (ConvertBits<5, 8, false>(data, bech.second.begin() + 1, bech.second.end())) { + if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, bech.second.begin() + 1, bech.second.end())) { if (version == 0) { { WitnessV0KeyHash keyid; diff --git a/src/utilstrencodings.h b/src/utilstrencodings.h index 994e6abbad..1c9cca90b2 100644 --- a/src/utilstrencodings.h +++ b/src/utilstrencodings.h @@ -151,7 +151,7 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); /** Convert from one power-of-2 number base to another. */ template -bool ConvertBits(O& out, I it, I end) { +bool ConvertBits(const O& outfn, I it, I end) { size_t acc = 0; size_t bits = 0; constexpr size_t maxv = (1 << tobits) - 1; @@ -161,12 +161,12 @@ bool ConvertBits(O& out, I it, I end) { bits += frombits; while (bits >= tobits) { bits -= tobits; - out.push_back((acc >> bits) & maxv); + outfn((acc >> bits) & maxv); } ++it; } if (pad) { - if (bits) out.push_back((acc << (tobits - bits)) & maxv); + if (bits) outfn((acc << (tobits - bits)) & maxv); } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { return false; }