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.4//5// Endian-neutral encoding:6// * Fixed-length numbers are encoded with least-significant byte first7// * In addition we support variable length "varint" encoding8// * Strings are encoded prefixed by their length in varint format10#ifndef STORAGE_LEVELDB_UTIL_CODING_H_11#define STORAGE_LEVELDB_UTIL_CODING_H_13#include <cstdint>14#include <cstring>15#include <string>17#include "leveldb/slice.h"18#include "port/port.h"20namespace leveldb {22// Standard Put... routines append to a string23void PutFixed32(std::string* dst, uint32_t value);24void PutFixed64(std::string* dst, uint64_t value);25void PutVarint32(std::string* dst, uint32_t value);26void PutVarint64(std::string* dst, uint64_t value);27void PutLengthPrefixedSlice(std::string* dst, const Slice& value);29// Standard Get... routines parse a value from the beginning of a Slice30// and advance the slice past the parsed value.31bool GetVarint32(Slice* input, uint32_t* value);32bool GetVarint64(Slice* input, uint64_t* value);33bool GetLengthPrefixedSlice(Slice* input, Slice* result);35// Pointer-based variants of GetVarint... These either store a value36// in *v and return a pointer just past the parsed value, or return37// nullptr on error. These routines only look at bytes in the range38// [p..limit-1]39const char* GetVarint32Ptr(const char* p, const char* limit, uint32_t* v);40const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* v);42// Returns the length of the varint32 or varint64 encoding of "v"43int VarintLength(uint64_t v);45// Lower-level versions of Put... that write directly into a character buffer46// and return a pointer just past the last byte written.47// REQUIRES: dst has enough space for the value being written48char* EncodeVarint32(char* dst, uint32_t value);49char* EncodeVarint64(char* dst, uint64_t value);51// Lower-level versions of Put... that write directly into a character buffer52// REQUIRES: dst has enough space for the value being written54inline void EncodeFixed32(char* dst, uint32_t value) {55uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);57// Recent clang and gcc optimize this to a single mov / str instruction.58buffer[0] = static_cast<uint8_t>(value);59buffer[1] = static_cast<uint8_t>(value >> 8);60buffer[2] = static_cast<uint8_t>(value >> 16);61buffer[3] = static_cast<uint8_t>(value >> 24);62}64inline void EncodeFixed64(char* dst, uint64_t value) {65uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);67// Recent clang and gcc optimize this to a single mov / str instruction.68buffer[0] = static_cast<uint8_t>(value);69buffer[1] = static_cast<uint8_t>(value >> 8);70buffer[2] = static_cast<uint8_t>(value >> 16);71buffer[3] = static_cast<uint8_t>(value >> 24);72buffer[4] = static_cast<uint8_t>(value >> 32);73buffer[5] = static_cast<uint8_t>(value >> 40);74buffer[6] = static_cast<uint8_t>(value >> 48);75buffer[7] = static_cast<uint8_t>(value >> 56);76}78// Lower-level versions of Get... that read directly from a character buffer79// without any bounds checking.81inline uint32_t DecodeFixed32(const char* ptr) {82const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);84// Recent clang and gcc optimize this to a single mov / ldr instruction.85return (static_cast<uint32_t>(buffer[0])) |86(static_cast<uint32_t>(buffer[1]) << 8) |87(static_cast<uint32_t>(buffer[2]) << 16) |88(static_cast<uint32_t>(buffer[3]) << 24);89}91inline uint64_t DecodeFixed64(const char* ptr) {92const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);94// Recent clang and gcc optimize this to a single mov / ldr instruction.95return (static_cast<uint64_t>(buffer[0])) |96(static_cast<uint64_t>(buffer[1]) << 8) |97(static_cast<uint64_t>(buffer[2]) << 16) |98(static_cast<uint64_t>(buffer[3]) << 24) |99(static_cast<uint64_t>(buffer[4]) << 32) |100(static_cast<uint64_t>(buffer[5]) << 40) |101(static_cast<uint64_t>(buffer[6]) << 48) |102(static_cast<uint64_t>(buffer[7]) << 56);103}105// Internal routine for use by fallback path of GetVarint32Ptr106const char* GetVarint32PtrFallback(const char* p, const char* limit,107uint32_t* value);108inline const char* GetVarint32Ptr(const char* p, const char* limit,109uint32_t* value) {110if (p < limit) {111uint32_t result = *(reinterpret_cast<const uint8_t*>(p));112if ((result & 128) == 0) {113*value = result;114return p + 1;115}116}117return GetVarint32PtrFallback(p, limit, value);118}120} // namespace leveldb122#endif // STORAGE_LEVELDB_UTIL_CODING_H_
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.