Coverage Report

Created: 2026-09-21 19:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
749M
{
29
749M
    uint32_t x;
30
749M
    memcpy(&x, ptr, 4);
31
749M
    return le32toh_internal(x);
32
749M
}
unsigned int ReadLE32<unsigned char>(unsigned char const*)
Line
Count
Source
28
625M
{
29
625M
    uint32_t x;
30
625M
    memcpy(&x, ptr, 4);
31
625M
    return le32toh_internal(x);
32
625M
}
unsigned int ReadLE32<std::byte>(std::byte const*)
Line
Count
Source
28
123M
{
29
123M
    uint32_t x;
30
123M
    memcpy(&x, ptr, 4);
31
123M
    return le32toh_internal(x);
32
123M
}
33
34
template <ByteType B>
35
inline uint64_t ReadLE64(const B* ptr)
36
1.19G
{
37
1.19G
    uint64_t x;
38
1.19G
    memcpy(&x, ptr, 8);
39
1.19G
    return le64toh_internal(x);
40
1.19G
}
unsigned long ReadLE64<unsigned char>(unsigned char const*)
Line
Count
Source
36
1.16G
{
37
1.16G
    uint64_t x;
38
1.16G
    memcpy(&x, ptr, 8);
39
1.16G
    return le64toh_internal(x);
40
1.16G
}
unsigned long ReadLE64<std::byte>(std::byte const*)
Line
Count
Source
36
34.1M
{
37
34.1M
    uint64_t x;
38
34.1M
    memcpy(&x, ptr, 8);
39
34.1M
    return le64toh_internal(x);
40
34.1M
}
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
303M
{
52
303M
    uint32_t v = htole32_internal(x);
53
303M
    memcpy(ptr, &v, 4);
54
303M
}
void WriteLE32<unsigned char>(unsigned char*, unsigned int)
Line
Count
Source
51
40.7M
{
52
40.7M
    uint32_t v = htole32_internal(x);
53
40.7M
    memcpy(ptr, &v, 4);
54
40.7M
}
void WriteLE32<std::byte>(std::byte*, unsigned int)
Line
Count
Source
51
262M
{
52
262M
    uint32_t v = htole32_internal(x);
53
262M
    memcpy(ptr, &v, 4);
54
262M
}
55
56
template <ByteType B>
57
inline void WriteLE64(B* ptr, uint64_t x)
58
16.0M
{
59
16.0M
    uint64_t v = htole64_internal(x);
60
16.0M
    memcpy(ptr, &v, 8);
61
16.0M
}
void WriteLE64<unsigned char>(unsigned char*, unsigned long)
Line
Count
Source
58
13.2M
{
59
13.2M
    uint64_t v = htole64_internal(x);
60
13.2M
    memcpy(ptr, &v, 8);
61
13.2M
}
void WriteLE64<std::byte>(std::byte*, unsigned long)
Line
Count
Source
58
2.78M
{
59
2.78M
    uint64_t v = htole64_internal(x);
60
2.78M
    memcpy(ptr, &v, 8);
61
2.78M
}
62
63
template <ByteType B>
64
inline uint16_t ReadBE16(const B* ptr)
65
19.1k
{
66
19.1k
    uint16_t x;
67
19.1k
    memcpy(&x, ptr, 2);
68
19.1k
    return be16toh_internal(x);
69
19.1k
}
70
71
template <ByteType B>
72
inline uint32_t ReadBE32(const B* ptr)
73
938M
{
74
938M
    uint32_t x;
75
938M
    memcpy(&x, ptr, 4);
76
938M
    return be32toh_internal(x);
77
938M
}
unsigned int ReadBE32<unsigned char>(unsigned char const*)
Line
Count
Source
73
938M
{
74
938M
    uint32_t x;
75
938M
    memcpy(&x, ptr, 4);
76
938M
    return be32toh_internal(x);
77
938M
}
unsigned int ReadBE32<std::byte>(std::byte const*)
Line
Count
Source
73
52
{
74
52
    uint32_t x;
75
52
    memcpy(&x, ptr, 4);
76
52
    return be32toh_internal(x);
77
52
}
78
79
template <ByteType B>
80
inline uint64_t ReadBE64(const B* ptr)
81
694M
{
82
694M
    uint64_t x;
83
694M
    memcpy(&x, ptr, 8);
84
694M
    return be64toh_internal(x);
85
694M
}
86
87
template <ByteType B>
88
inline void WriteBE16(B* ptr, uint16_t x)
89
20
{
90
20
    uint16_t v = htobe16_internal(x);
91
20
    memcpy(ptr, &v, 2);
92
20
}
93
94
template <ByteType B>
95
inline void WriteBE32(B* ptr, uint32_t x)
96
606M
{
97
606M
    uint32_t v = htobe32_internal(x);
98
606M
    memcpy(ptr, &v, 4);
99
606M
}
100
101
template <ByteType B>
102
inline void WriteBE64(B* ptr, uint64_t x)
103
444M
{
104
444M
    uint64_t v = htobe64_internal(x);
105
444M
    memcpy(ptr, &v, 8);
106
444M
}
107
108
#endif // BITCOIN_CRYPTO_COMMON_H