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/coding.h"7namespace leveldb {9void PutFixed32(std::string* dst, uint32_t value) {10char buf[sizeof(value)];11EncodeFixed32(buf, value);12dst->append(buf, sizeof(buf));13}15void PutFixed64(std::string* dst, uint64_t value) {16char buf[sizeof(value)];17EncodeFixed64(buf, value);18dst->append(buf, sizeof(buf));19}21char* EncodeVarint32(char* dst, uint32_t v) {22// Operate on characters as unsigneds23uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);24static const int B = 128;25if (v < (1 << 7)) {26*(ptr++) = v;27} else if (v < (1 << 14)) {28*(ptr++) = v | B;29*(ptr++) = v >> 7;30} else if (v < (1 << 21)) {31*(ptr++) = v | B;32*(ptr++) = (v >> 7) | B;33*(ptr++) = v >> 14;34} else if (v < (1 << 28)) {35*(ptr++) = v | B;36*(ptr++) = (v >> 7) | B;37*(ptr++) = (v >> 14) | B;38*(ptr++) = v >> 21;39} else {40*(ptr++) = v | B;41*(ptr++) = (v >> 7) | B;42*(ptr++) = (v >> 14) | B;43*(ptr++) = (v >> 21) | B;44*(ptr++) = v >> 28;45}46return reinterpret_cast<char*>(ptr);47}49void PutVarint32(std::string* dst, uint32_t v) {50char buf[5];51char* ptr = EncodeVarint32(buf, v);52dst->append(buf, ptr - buf);53}55char* EncodeVarint64(char* dst, uint64_t v) {56static const int B = 128;57uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);58while (v >= B) {59*(ptr++) = v | B;60v >>= 7;61}62*(ptr++) = static_cast<uint8_t>(v);63return reinterpret_cast<char*>(ptr);64}66void PutVarint64(std::string* dst, uint64_t v) {67char buf[10];68char* ptr = EncodeVarint64(buf, v);69dst->append(buf, ptr - buf);70}72void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {73PutVarint32(dst, value.size());74dst->append(value.data(), value.size());75}77int VarintLength(uint64_t v) {78int len = 1;79while (v >= 128) {80v >>= 7;81len++;82}83return len;84}86const char* GetVarint32PtrFallback(const char* p, const char* limit,87uint32_t* value) {88uint32_t result = 0;89for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {90uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));91p++;92if (byte & 128) {93// More bytes are present94result |= ((byte & 127) << shift);95} else {96result |= (byte << shift);97*value = result;98return reinterpret_cast<const char*>(p);99}100}101return nullptr;102}104bool GetVarint32(Slice* input, uint32_t* value) {105const char* p = input->data();106const char* limit = p + input->size();107const char* q = GetVarint32Ptr(p, limit, value);108if (q == nullptr) {109return false;110} else {111*input = Slice(q, limit - q);112return true;113}114}116const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {117uint64_t result = 0;118for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {119uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));120p++;121if (byte & 128) {122// More bytes are present123result |= ((byte & 127) << shift);124} else {125result |= (byte << shift);126*value = result;127return reinterpret_cast<const char*>(p);128}129}130return nullptr;131}133bool GetVarint64(Slice* input, uint64_t* value) {134const char* p = input->data();135const char* limit = p + input->size();136const char* q = GetVarint64Ptr(p, limit, value);137if (q == nullptr) {138return false;139} else {140*input = Slice(q, limit - q);141return true;142}143}145const char* GetLengthPrefixedSlice(const char* p, const char* limit,146Slice* result) {147uint32_t len;148p = GetVarint32Ptr(p, limit, &len);149if (p == nullptr) return nullptr;150if (p + len > limit) return nullptr;151*result = Slice(p, len);152return p + len;153}155bool GetLengthPrefixedSlice(Slice* input, Slice* result) {156uint32_t len;157if (GetVarint32(input, &len) && input->size() >= len) {158*result = Slice(input->data(), len);159input->remove_prefix(len);160return true;161} else {162return false;163}164}166} // 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.