/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 | 346k | { |
29 | 346k | uint32_t x; |
30 | 346k | memcpy(&x, ptr, 4); |
31 | 346k | return le32toh_internal(x); |
32 | 346k | } unsigned int ReadLE32<unsigned char>(unsigned char const*) Line | Count | Source | 28 | 17.0k | { | 29 | 17.0k | uint32_t x; | 30 | 17.0k | memcpy(&x, ptr, 4); | 31 | 17.0k | return le32toh_internal(x); | 32 | 17.0k | } |
unsigned int ReadLE32<std::byte>(std::byte const*) Line | Count | Source | 28 | 329k | { | 29 | 329k | uint32_t x; | 30 | 329k | memcpy(&x, ptr, 4); | 31 | 329k | return le32toh_internal(x); | 32 | 329k | } |
|
33 | | |
34 | | template <ByteType B> |
35 | | inline uint64_t ReadLE64(const B* ptr) |
36 | 34.0k | { |
37 | 34.0k | uint64_t x; |
38 | 34.0k | memcpy(&x, ptr, 8); |
39 | 34.0k | return le64toh_internal(x); |
40 | 34.0k | } unsigned long long ReadLE64<unsigned char>(unsigned char const*) Line | Count | Source | 36 | 6.04k | { | 37 | 6.04k | uint64_t x; | 38 | 6.04k | memcpy(&x, ptr, 8); | 39 | 6.04k | return le64toh_internal(x); | 40 | 6.04k | } |
unsigned long long ReadLE64<std::byte>(std::byte const*) Line | Count | Source | 36 | 27.9k | { | 37 | 27.9k | uint64_t x; | 38 | 27.9k | memcpy(&x, ptr, 8); | 39 | 27.9k | return le64toh_internal(x); | 40 | 27.9k | } |
|
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 | 428k | { |
52 | 428k | uint32_t v = htole32_internal(x); |
53 | 428k | memcpy(ptr, &v, 4); |
54 | 428k | } Unexecuted instantiation: void WriteLE32<unsigned char>(unsigned char*, unsigned int) void WriteLE32<std::byte>(std::byte*, unsigned int) Line | Count | Source | 51 | 428k | { | 52 | 428k | uint32_t v = htole32_internal(x); | 53 | 428k | memcpy(ptr, &v, 4); | 54 | 428k | } |
|
55 | | |
56 | | template <ByteType B> |
57 | | inline void WriteLE64(B* ptr, uint64_t x) |
58 | 292 | { |
59 | 292 | uint64_t v = htole64_internal(x); |
60 | 292 | memcpy(ptr, &v, 8); |
61 | 292 | } Unexecuted instantiation: void WriteLE64<std::byte>(std::byte*, unsigned long long) void WriteLE64<unsigned char>(unsigned char*, unsigned long long) Line | Count | Source | 58 | 292 | { | 59 | 292 | uint64_t v = htole64_internal(x); | 60 | 292 | memcpy(ptr, &v, 8); | 61 | 292 | } |
|
62 | | |
63 | | template <ByteType B> |
64 | | inline uint16_t ReadBE16(const B* ptr) |
65 | 3.15k | { |
66 | 3.15k | uint16_t x; |
67 | 3.15k | memcpy(&x, ptr, 2); |
68 | 3.15k | return be16toh_internal(x); |
69 | 3.15k | } |
70 | | |
71 | | template <ByteType B> |
72 | | inline uint32_t ReadBE32(const B* ptr) |
73 | 1.00k | { |
74 | 1.00k | uint32_t x; |
75 | 1.00k | memcpy(&x, ptr, 4); |
76 | 1.00k | return be32toh_internal(x); |
77 | 1.00k | } unsigned int ReadBE32<unsigned char>(unsigned char const*) Line | Count | Source | 73 | 1.00k | { | 74 | 1.00k | uint32_t x; | 75 | 1.00k | memcpy(&x, ptr, 4); | 76 | 1.00k | return be32toh_internal(x); | 77 | 1.00k | } |
Unexecuted instantiation: unsigned int ReadBE32<std::byte>(std::byte const*) |
78 | | |
79 | | template <ByteType B> |
80 | | inline uint64_t ReadBE64(const B* ptr) |
81 | 266k | { |
82 | 266k | uint64_t x; |
83 | 266k | memcpy(&x, ptr, 8); |
84 | 266k | return be64toh_internal(x); |
85 | 266k | } |
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 | 794k | { |
97 | 794k | uint32_t v = htobe32_internal(x); |
98 | 794k | memcpy(ptr, &v, 4); |
99 | 794k | } |
100 | | |
101 | | template <ByteType B> |
102 | | inline void WriteBE64(B* ptr, uint64_t x) |
103 | 249k | { |
104 | 249k | uint64_t v = htobe64_internal(x); |
105 | 249k | memcpy(ptr, &v, 8); |
106 | 249k | } |
107 | | |
108 | | #endif // BITCOIN_CRYPTO_COMMON_H |