Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;253/** Helper function for ParseFixedPoint */254static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)255{256if(ch == '0')257++mantissa_tzeros;258else {259for (int i=0; i<=mantissa_tzeros; ++i) {260if (mantissa > (UPPER_BOUND / 10LL))261return false; /* overflow */262mantissa *= 10;263}264mantissa += ch - '0';265mantissa_tzeros = 0;266}267return true;268}270bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)271{272int64_t mantissa = 0;273int64_t exponent = 0;274int mantissa_tzeros = 0;275bool mantissa_sign = false;276bool exponent_sign = false;277int ptr = 0;278int end = val.size();279int point_ofs = 0;281if (ptr < end && val[ptr] == '-') {282mantissa_sign = true;283++ptr;284}285if (ptr < end)286{287if (val[ptr] == '0') {288/* pass single 0 */289++ptr;290} else if (val[ptr] >= '1' && val[ptr] <= '9') {291while (ptr < end && IsDigit(val[ptr])) {292if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))293return false; /* overflow */294++ptr;295}296} else return false; /* missing expected digit */297} else return false; /* empty string or loose '-' */298if (ptr < end && val[ptr] == '.')299{300++ptr;301if (ptr < end && IsDigit(val[ptr]))302{303while (ptr < end && IsDigit(val[ptr])) {304if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))305return false; /* overflow */306++ptr;307++point_ofs;308}309} else return false; /* missing expected digit */310}311if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))312{313++ptr;314if (ptr < end && val[ptr] == '+')315++ptr;316else if (ptr < end && val[ptr] == '-') {317exponent_sign = true;318++ptr;319}320if (ptr < end && IsDigit(val[ptr])) {321while (ptr < end && IsDigit(val[ptr])) {322if (exponent > (UPPER_BOUND / 10LL))323return false; /* overflow */324exponent = exponent * 10 + val[ptr] - '0';325++ptr;326}327} else return false; /* missing expected digit */328}329if (ptr != end)330return false; /* trailing garbage */332/* finalize exponent */333if (exponent_sign)334exponent = -exponent;335exponent = exponent - point_ofs + mantissa_tzeros;337/* finalize mantissa */338if (mantissa_sign)339mantissa = -mantissa;341/* convert to one 64-bit fixed-point value */342exponent += decimals;343if (exponent < 0)344return false; /* cannot represent values smaller than 10^-decimals */345if (exponent >= 18)346return false; /* cannot represent values larger than or equal to 10^(18-decimals) */348for (int i=0; i < exponent; ++i) {349if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))350return false; /* overflow */351mantissa *= 10;352}353if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)354return false; /* overflow */356if (amount_out)357*amount_out = mantissa;359return true;360}362std::string ToLower(std::string_view str)363{364std::string r;365r.reserve(str.size());366for (auto ch : str) r += ToLower(ch);367return r;368}370std::string ToUpper(std::string_view str)371{372std::string r;373r.reserve(str.size());374for (auto ch : str) r += ToUpper(ch);375return r;376}378std::string Capitalize(std::string str)379{380if (str.empty()) return str;381str[0] = ToUpper(str.front());382return str;383}385std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)386{387if (str.empty()) {388return std::nullopt;389}390auto multiplier = default_multiplier;391char unit = str.back();392switch (unit) {393case 'k':394multiplier = ByteUnit::k;395break;396case 'K':397multiplier = ByteUnit::K;398break;399case 'm':400multiplier = ByteUnit::m;401break;402case 'M':403multiplier = ByteUnit::M;404break;405case 'g':406multiplier = ByteUnit::g;407break;408case 'G':409multiplier = ByteUnit::G;410break;411case 't':412multiplier = ByteUnit::t;413break;414case 'T':415multiplier = ByteUnit::T;416break;417default:418unit = 0;419break;420}422uint64_t unit_amount = static_cast<uint64_t>(multiplier);423auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);424if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) { // check overflow425return std::nullopt;426}427return *parsed_num * unit_amount;428}430bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2)431{432if (s1.size() != s2.size()) return false;433for (size_t i = 0; i < s1.size(); ++i) {434char c1 = s1[i];435if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');436char c2 = s2[i];437if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');438if (c1 != c2) return false;439}440return true;441}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.