/tmp/bitcoin/src/crypto/common.h
Line | Count | Source |
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 | | |
15 | | template <typename B> |
16 | | concept ByteType = std::same_as<B, unsigned char> || std::same_as<B, std::byte>; |
17 | | |
18 | | template <ByteType B> |
19 | | inline uint16_t ReadLE16(const B* ptr) |
20 | 22.6k | { |
21 | 22.6k | uint16_t x; |
22 | 22.6k | memcpy(&x, ptr, 2); |
23 | 22.6k | return le16toh_internal(x); |
24 | 22.6k | } |
25 | | |
26 | | template <ByteType B> |
27 | | inline uint32_t ReadLE32(const B* ptr) |
28 | 774M | { |
29 | 774M | uint32_t x; |
30 | 774M | memcpy(&x, ptr, 4); |
31 | 774M | return le32toh_internal(x); |
32 | 774M | } unsigned int ReadLE32<unsigned char>(unsigned char const*) Line | Count | Source | 28 | 648M | { | 29 | 648M | uint32_t x; | 30 | 648M | memcpy(&x, ptr, 4); | 31 | 648M | return le32toh_internal(x); | 32 | 648M | } |
unsigned int ReadLE32<std::byte>(std::byte const*) Line | Count | Source | 28 | 126M | { | 29 | 126M | uint32_t x; | 30 | 126M | memcpy(&x, ptr, 4); | 31 | 126M | return le32toh_internal(x); | 32 | 126M | } |
|
33 | | |
34 | | template <ByteType B> |
35 | | inline uint64_t ReadLE64(const B* ptr) |
36 | 1.15G | { |
37 | 1.15G | uint64_t x; |
38 | 1.15G | memcpy(&x, ptr, 8); |
39 | 1.15G | return le64toh_internal(x); |
40 | 1.15G | } unsigned long ReadLE64<unsigned char>(unsigned char const*) Line | Count | Source | 36 | 1.12G | { | 37 | 1.12G | uint64_t x; | 38 | 1.12G | memcpy(&x, ptr, 8); | 39 | 1.12G | return le64toh_internal(x); | 40 | 1.12G | } |
unsigned long ReadLE64<std::byte>(std::byte const*) Line | Count | Source | 36 | 33.8M | { | 37 | 33.8M | uint64_t x; | 38 | 33.8M | memcpy(&x, ptr, 8); | 39 | 33.8M | return le64toh_internal(x); | 40 | 33.8M | } |
|
41 | | |
42 | | template <ByteType B> |
43 | | inline void WriteLE16(B* ptr, uint16_t x) |
44 | 377 | { |
45 | 377 | uint16_t v = htole16_internal(x); |
46 | 377 | memcpy(ptr, &v, 2); |
47 | 377 | } |
48 | | |
49 | | template <ByteType B> |
50 | | inline void WriteLE32(B* ptr, uint32_t x) |
51 | 309M | { |
52 | 309M | uint32_t v = htole32_internal(x); |
53 | 309M | memcpy(ptr, &v, 4); |
54 | 309M | } void WriteLE32<unsigned char>(unsigned char*, unsigned int) Line | Count | Source | 51 | 38.6M | { | 52 | 38.6M | uint32_t v = htole32_internal(x); | 53 | 38.6M | memcpy(ptr, &v, 4); | 54 | 38.6M | } |
void WriteLE32<std::byte>(std::byte*, unsigned int) Line | Count | Source | 51 | 270M | { | 52 | 270M | uint32_t v = htole32_internal(x); | 53 | 270M | memcpy(ptr, &v, 4); | 54 | 270M | } |
|
55 | | |
56 | | template <ByteType B> |
57 | | inline void WriteLE64(B* ptr, uint64_t x) |
58 | 15.6M | { |
59 | 15.6M | uint64_t v = htole64_internal(x); |
60 | 15.6M | memcpy(ptr, &v, 8); |
61 | 15.6M | } void WriteLE64<unsigned char>(unsigned char*, unsigned long) Line | Count | Source | 58 | 12.8M | { | 59 | 12.8M | uint64_t v = htole64_internal(x); | 60 | 12.8M | memcpy(ptr, &v, 8); | 61 | 12.8M | } |
void WriteLE64<std::byte>(std::byte*, unsigned long) Line | Count | Source | 58 | 2.77M | { | 59 | 2.77M | uint64_t v = htole64_internal(x); | 60 | 2.77M | memcpy(ptr, &v, 8); | 61 | 2.77M | } |
|
62 | | |
63 | | template <ByteType B> |
64 | | inline uint16_t ReadBE16(const B* ptr) |
65 | 18.7k | { |
66 | 18.7k | uint16_t x; |
67 | 18.7k | memcpy(&x, ptr, 2); |
68 | 18.7k | return be16toh_internal(x); |
69 | 18.7k | } |
70 | | |
71 | | template <ByteType B> |
72 | | inline uint32_t ReadBE32(const B* ptr) |
73 | 1.01G | { |
74 | 1.01G | uint32_t x; |
75 | 1.01G | memcpy(&x, ptr, 4); |
76 | 1.01G | return be32toh_internal(x); |
77 | 1.01G | } unsigned int ReadBE32<unsigned char>(unsigned char const*) Line | Count | Source | 73 | 1.01G | { | 74 | 1.01G | uint32_t x; | 75 | 1.01G | memcpy(&x, ptr, 4); | 76 | 1.01G | return be32toh_internal(x); | 77 | 1.01G | } |
unsigned int ReadBE32<std::byte>(std::byte const*) Line | Count | Source | 73 | 43 | { | 74 | 43 | uint32_t x; | 75 | 43 | memcpy(&x, ptr, 4); | 76 | 43 | return be32toh_internal(x); | 77 | 43 | } |
|
78 | | |
79 | | template <ByteType B> |
80 | | inline uint64_t ReadBE64(const B* ptr) |
81 | 607M | { |
82 | 607M | uint64_t x; |
83 | 607M | memcpy(&x, ptr, 8); |
84 | 607M | return be64toh_internal(x); |
85 | 607M | } |
86 | | |
87 | | template <ByteType B> |
88 | | inline void WriteBE16(B* ptr, uint16_t x) |
89 | 22 | { |
90 | 22 | uint16_t v = htobe16_internal(x); |
91 | 22 | memcpy(ptr, &v, 2); |
92 | 22 | } |
93 | | |
94 | | template <ByteType B> |
95 | | inline void WriteBE32(B* ptr, uint32_t x) |
96 | 626M | { |
97 | 626M | uint32_t v = htobe32_internal(x); |
98 | 626M | memcpy(ptr, &v, 4); |
99 | 626M | } |
100 | | |
101 | | template <ByteType B> |
102 | | inline void WriteBE64(B* ptr, uint64_t x) |
103 | 399M | { |
104 | 399M | uint64_t v = htobe64_internal(x); |
105 | 399M | memcpy(ptr, &v, 8); |
106 | 399M | } |
107 | | |
108 | | #endif // BITCOIN_CRYPTO_COMMON_H |