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) 2014-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_CRYPTO_COMMON_H6#define BITCOIN_CRYPTO_COMMON_H8#include <compat/endian.h>10#include <concepts>11#include <cstddef>12#include <cstdint>13#include <cstring>15template <typename B>16concept ByteType = std::same_as<B, unsigned char> || std::same_as<B, std::byte>;18template <ByteType B>19inline uint16_t ReadLE16(const B* ptr)20{21uint16_t x;22memcpy(&x, ptr, 2);23return le16toh_internal(x);24}26template <ByteType B>27inline uint32_t ReadLE32(const B* ptr)28{29uint32_t x;30memcpy(&x, ptr, 4);31return le32toh_internal(x);32}34template <ByteType B>35inline uint64_t ReadLE64(const B* ptr)36{37uint64_t x;38memcpy(&x, ptr, 8);39return le64toh_internal(x);40}42template <ByteType B>43inline void WriteLE16(B* ptr, uint16_t x)44{45uint16_t v = htole16_internal(x);46memcpy(ptr, &v, 2);47}49template <ByteType B>50inline void WriteLE32(B* ptr, uint32_t x)51{52uint32_t v = htole32_internal(x);53memcpy(ptr, &v, 4);54}56template <ByteType B>57inline void WriteLE64(B* ptr, uint64_t x)58{59uint64_t v = htole64_internal(x);60memcpy(ptr, &v, 8);61}63template <ByteType B>64inline uint16_t ReadBE16(const B* ptr)65{66uint16_t x;67memcpy(&x, ptr, 2);68return be16toh_internal(x);69}71template <ByteType B>72inline uint32_t ReadBE32(const B* ptr)73{74uint32_t x;75memcpy(&x, ptr, 4);76return be32toh_internal(x);77}79template <ByteType B>80inline uint64_t ReadBE64(const B* ptr)81{82uint64_t x;83memcpy(&x, ptr, 8);84return be64toh_internal(x);85}87template <ByteType B>88inline void WriteBE16(B* ptr, uint16_t x)89{90uint16_t v = htobe16_internal(x);91memcpy(ptr, &v, 2);92}94template <ByteType B>95inline void WriteBE32(B* ptr, uint32_t x)96{97uint32_t v = htobe32_internal(x);98memcpy(ptr, &v, 4);99}101template <ByteType B>102inline void WriteBE64(B* ptr, uint64_t x)103{104uint64_t v = htobe64_internal(x);105memcpy(ptr, &v, 8);106}108#endif // BITCOIN_CRYPTO_COMMON_H