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.
1// Copyright (c) 2011 The LevelDB Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file. See the AUTHORS file for names of contributors.5#include "util/logging.h"7#include <errno.h>8#include <stdarg.h>9#include <stdio.h>10#include <stdlib.h>12#include <limits>14#include "leveldb/env.h"15#include "leveldb/slice.h"17namespace leveldb {19void AppendNumberTo(std::string* str, uint64_t num) {20char buf[30];21snprintf(buf, sizeof(buf), "%llu", (unsigned long long)num);22str->append(buf);23}25void AppendEscapedStringTo(std::string* str, const Slice& value) {26for (size_t i = 0; i < value.size(); i++) {27char c = value[i];28if (c >= ' ' && c <= '~') {29str->push_back(c);30} else {31char buf[10];32snprintf(buf, sizeof(buf), "\\x%02x",33static_cast<unsigned int>(c) & 0xff);34str->append(buf);35}36}37}39std::string NumberToString(uint64_t num) {40std::string r;41AppendNumberTo(&r, num);42return r;43}45std::string EscapeString(const Slice& value) {46std::string r;47AppendEscapedStringTo(&r, value);48return r;49}51bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {52// Constants that will be optimized away.53constexpr const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();54constexpr const char kLastDigitOfMaxUint64 =55'0' + static_cast<char>(kMaxUint64 % 10);57uint64_t value = 0;59// reinterpret_cast-ing from char* to uint8_t* to avoid signedness.60const uint8_t* start = reinterpret_cast<const uint8_t*>(in->data());62const uint8_t* end = start + in->size();63const uint8_t* current = start;64for (; current != end; ++current) {65const uint8_t ch = *current;66if (ch < '0' || ch > '9') break;68// Overflow check.69// kMaxUint64 / 10 is also constant and will be optimized away.70if (value > kMaxUint64 / 10 ||71(value == kMaxUint64 / 10 && ch > kLastDigitOfMaxUint64)) {72return false;73}75value = (value * 10) + (ch - '0');76}78*val = value;79const size_t digits_consumed = current - start;80in->remove_prefix(digits_consumed);81return digits_consumed != 0;82}84} // namespace leveldb
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.