/Users/brunogarcia/projects/bitcoin-core-dev/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 | 0 | { |
21 | 0 | uint16_t x; |
22 | 0 | memcpy(&x, ptr, 2); |
23 | 0 | return le16toh_internal(x); |
24 | 0 | } |
25 | | |
26 | | template <ByteType B> |
27 | | inline uint32_t ReadLE32(const B* ptr) |
28 | 20.9M | { |
29 | 20.9M | uint32_t x; |
30 | 20.9M | memcpy(&x, ptr, 4); |
31 | 20.9M | return le32toh_internal(x); |
32 | 20.9M | } unsigned int ReadLE32<unsigned char>(unsigned char const*) Line | Count | Source | 28 | 18.5M | { | 29 | 18.5M | uint32_t x; | 30 | 18.5M | memcpy(&x, ptr, 4); | 31 | 18.5M | return le32toh_internal(x); | 32 | 18.5M | } |
unsigned int ReadLE32<std::byte>(std::byte const*) Line | Count | Source | 28 | 2.38M | { | 29 | 2.38M | uint32_t x; | 30 | 2.38M | memcpy(&x, ptr, 4); | 31 | 2.38M | return le32toh_internal(x); | 32 | 2.38M | } |
|
33 | | |
34 | | template <ByteType B> |
35 | | inline uint64_t ReadLE64(const B* ptr) |
36 | 1.41M | { |
37 | 1.41M | uint64_t x; |
38 | 1.41M | memcpy(&x, ptr, 8); |
39 | 1.41M | return le64toh_internal(x); |
40 | 1.41M | } unsigned long long ReadLE64<unsigned char>(unsigned char const*) Line | Count | Source | 36 | 1.26M | { | 37 | 1.26M | uint64_t x; | 38 | 1.26M | memcpy(&x, ptr, 8); | 39 | 1.26M | return le64toh_internal(x); | 40 | 1.26M | } |
unsigned long long ReadLE64<std::byte>(std::byte const*) Line | Count | Source | 36 | 143k | { | 37 | 143k | uint64_t x; | 38 | 143k | memcpy(&x, ptr, 8); | 39 | 143k | return le64toh_internal(x); | 40 | 143k | } |
|
41 | | |
42 | | template <ByteType B> |
43 | | inline void WriteLE16(B* ptr, uint16_t x) |
44 | 0 | { |
45 | 0 | uint16_t v = htole16_internal(x); |
46 | 0 | memcpy(ptr, &v, 2); |
47 | 0 | } |
48 | | |
49 | | template <ByteType B> |
50 | | inline void WriteLE32(B* ptr, uint32_t x) |
51 | 9.30M | { |
52 | 9.30M | uint32_t v = htole32_internal(x); |
53 | 9.30M | memcpy(ptr, &v, 4); |
54 | 9.30M | } void WriteLE32<unsigned char>(unsigned char*, unsigned int) Line | Count | Source | 51 | 5.77M | { | 52 | 5.77M | uint32_t v = htole32_internal(x); | 53 | 5.77M | memcpy(ptr, &v, 4); | 54 | 5.77M | } |
void WriteLE32<std::byte>(std::byte*, unsigned int) Line | Count | Source | 51 | 3.53M | { | 52 | 3.53M | uint32_t v = htole32_internal(x); | 53 | 3.53M | memcpy(ptr, &v, 4); | 54 | 3.53M | } |
|
55 | | |
56 | | template <ByteType B> |
57 | | inline void WriteLE64(B* ptr, uint64_t x) |
58 | 1.15M | { |
59 | 1.15M | uint64_t v = htole64_internal(x); |
60 | 1.15M | memcpy(ptr, &v, 8); |
61 | 1.15M | } Unexecuted instantiation: void WriteLE64<std::byte>(std::byte*, unsigned long long) void WriteLE64<unsigned char>(unsigned char*, unsigned long long) Line | Count | Source | 58 | 1.15M | { | 59 | 1.15M | uint64_t v = htole64_internal(x); | 60 | 1.15M | memcpy(ptr, &v, 8); | 61 | 1.15M | } |
|
62 | | |
63 | | template <ByteType B> |
64 | | inline uint16_t ReadBE16(const B* ptr) |
65 | 0 | { |
66 | 0 | uint16_t x; |
67 | 0 | memcpy(&x, ptr, 2); |
68 | 0 | return be16toh_internal(x); |
69 | 0 | } |
70 | | |
71 | | template <ByteType B> |
72 | | inline uint32_t ReadBE32(const B* ptr) |
73 | 79.3k | { |
74 | 79.3k | uint32_t x; |
75 | 79.3k | memcpy(&x, ptr, 4); |
76 | 79.3k | return be32toh_internal(x); |
77 | 79.3k | } unsigned int ReadBE32<unsigned char>(unsigned char const*) Line | Count | Source | 73 | 79.3k | { | 74 | 79.3k | uint32_t x; | 75 | 79.3k | memcpy(&x, ptr, 4); | 76 | 79.3k | return be32toh_internal(x); | 77 | 79.3k | } |
Unexecuted instantiation: unsigned int ReadBE32<std::byte>(std::byte const*) |
78 | | |
79 | | template <ByteType B> |
80 | | inline uint64_t ReadBE64(const B* ptr) |
81 | 13.3M | { |
82 | 13.3M | uint64_t x; |
83 | 13.3M | memcpy(&x, ptr, 8); |
84 | 13.3M | return be64toh_internal(x); |
85 | 13.3M | } |
86 | | |
87 | | template <ByteType B> |
88 | | inline void WriteBE16(B* ptr, uint16_t x) |
89 | 0 | { |
90 | 0 | uint16_t v = htobe16_internal(x); |
91 | 0 | memcpy(ptr, &v, 2); |
92 | 0 | } |
93 | | |
94 | | template <ByteType B> |
95 | | inline void WriteBE32(B* ptr, uint32_t x) |
96 | 19.5M | { |
97 | 19.5M | uint32_t v = htobe32_internal(x); |
98 | 19.5M | memcpy(ptr, &v, 4); |
99 | 19.5M | } |
100 | | |
101 | | template <ByteType B> |
102 | | inline void WriteBE64(B* ptr, uint64_t x) |
103 | 6.78M | { |
104 | 6.78M | uint64_t v = htobe64_internal(x); |
105 | 6.78M | memcpy(ptr, &v, 8); |
106 | 6.78M | } |
107 | | |
108 | | #endif // BITCOIN_CRYPTO_COMMON_H |