Bitcoin Core 28.99.0
P2P Digital Currency
common.h
Go to the documentation of this file.
1// Copyright (c) 2014-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_CRYPTO_COMMON_H
6#define BITCOIN_CRYPTO_COMMON_H
7
8#include <compat/endian.h>
9
10#include <concepts>
11#include <cstddef>
12#include <cstdint>
13#include <cstring>
14
15template <typename B>
16concept ByteType = std::same_as<B, unsigned char> || std::same_as<B, std::byte>;
17
18template <ByteType B>
19inline uint16_t ReadLE16(const B* ptr)
20{
21 uint16_t x;
22 memcpy(&x, ptr, 2);
23 return le16toh_internal(x);
24}
25
26template <ByteType B>
27inline uint32_t ReadLE32(const B* ptr)
28{
29 uint32_t x;
30 memcpy(&x, ptr, 4);
31 return le32toh_internal(x);
32}
33
34template <ByteType B>
35inline uint64_t ReadLE64(const B* ptr)
36{
37 uint64_t x;
38 memcpy(&x, ptr, 8);
39 return le64toh_internal(x);
40}
41
42template <ByteType B>
43inline void WriteLE16(B* ptr, uint16_t x)
44{
45 uint16_t v = htole16_internal(x);
46 memcpy(ptr, &v, 2);
47}
48
49template <ByteType B>
50inline void WriteLE32(B* ptr, uint32_t x)
51{
52 uint32_t v = htole32_internal(x);
53 memcpy(ptr, &v, 4);
54}
55
56template <ByteType B>
57inline void WriteLE64(B* ptr, uint64_t x)
58{
59 uint64_t v = htole64_internal(x);
60 memcpy(ptr, &v, 8);
61}
62
63template <ByteType B>
64inline uint16_t ReadBE16(const B* ptr)
65{
66 uint16_t x;
67 memcpy(&x, ptr, 2);
68 return be16toh_internal(x);
69}
70
71template <ByteType B>
72inline uint32_t ReadBE32(const B* ptr)
73{
74 uint32_t x;
75 memcpy(&x, ptr, 4);
76 return be32toh_internal(x);
77}
78
79template <ByteType B>
80inline uint64_t ReadBE64(const B* ptr)
81{
82 uint64_t x;
83 memcpy(&x, ptr, 8);
84 return be64toh_internal(x);
85}
86
87template <ByteType B>
88inline void WriteBE16(B* ptr, uint16_t x)
89{
90 uint16_t v = htobe16_internal(x);
91 memcpy(ptr, &v, 2);
92}
93
94template <ByteType B>
95inline void WriteBE32(B* ptr, uint32_t x)
96{
97 uint32_t v = htobe32_internal(x);
98 memcpy(ptr, &v, 4);
99}
100
101template <ByteType B>
102inline void WriteBE64(B* ptr, uint64_t x)
103{
104 uint64_t v = htobe64_internal(x);
105 memcpy(ptr, &v, 8);
106}
107
108#endif // BITCOIN_CRYPTO_COMMON_H
void WriteLE32(B *ptr, uint32_t x)
Definition: common.h:50
uint16_t ReadLE16(const B *ptr)
Definition: common.h:19
uint64_t ReadLE64(const B *ptr)
Definition: common.h:35
uint64_t ReadBE64(const B *ptr)
Definition: common.h:80
void WriteLE64(B *ptr, uint64_t x)
Definition: common.h:57
uint32_t ReadLE32(const B *ptr)
Definition: common.h:27
void WriteBE32(B *ptr, uint32_t x)
Definition: common.h:95
void WriteBE16(B *ptr, uint16_t x)
Definition: common.h:88
uint16_t ReadBE16(const B *ptr)
Definition: common.h:64
void WriteLE16(B *ptr, uint16_t x)
Definition: common.h:43
void WriteBE64(B *ptr, uint64_t x)
Definition: common.h:102
uint32_t ReadBE32(const B *ptr)
Definition: common.h:72
BSWAP_CONSTEXPR uint32_t be32toh_internal(uint32_t big_endian_32bits)
Definition: endian.h:43
BSWAP_CONSTEXPR uint16_t be16toh_internal(uint16_t big_endian_16bits)
Definition: endian.h:23
BSWAP_CONSTEXPR uint64_t htobe64_internal(uint64_t host_64bits)
Definition: endian.h:53
BSWAP_CONSTEXPR uint16_t htobe16_internal(uint16_t host_16bits)
Definition: endian.h:13
BSWAP_CONSTEXPR uint32_t htole32_internal(uint32_t host_32bits)
Definition: endian.h:38
BSWAP_CONSTEXPR uint16_t htole16_internal(uint16_t host_16bits)
Definition: endian.h:18
BSWAP_CONSTEXPR uint64_t be64toh_internal(uint64_t big_endian_64bits)
Definition: endian.h:63
BSWAP_CONSTEXPR uint16_t le16toh_internal(uint16_t little_endian_16bits)
Definition: endian.h:28
BSWAP_CONSTEXPR uint64_t htole64_internal(uint64_t host_64bits)
Definition: endian.h:58
BSWAP_CONSTEXPR uint64_t le64toh_internal(uint64_t little_endian_64bits)
Definition: endian.h:68
BSWAP_CONSTEXPR uint32_t le32toh_internal(uint32_t little_endian_32bits)
Definition: endian.h:48
BSWAP_CONSTEXPR uint32_t htobe32_internal(uint32_t host_32bits)
Definition: endian.h:33
#define B
Definition: util_tests.cpp:545