Coverage Report

Created: 2026-07-08 14:14

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
740M
{
29
740M
    uint32_t x;
30
740M
    memcpy(&x, ptr, 4);
31
740M
    return le32toh_internal(x);
32
740M
}
unsigned int ReadLE32<unsigned char>(unsigned char const*)
Line
Count
Source
28
614M
{
29
614M
    uint32_t x;
30
614M
    memcpy(&x, ptr, 4);
31
614M
    return le32toh_internal(x);
32
614M
}
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.40G
{
37
1.40G
    uint64_t x;
38
1.40G
    memcpy(&x, ptr, 8);
39
1.40G
    return le64toh_internal(x);
40
1.40G
}
unsigned long ReadLE64<unsigned char>(unsigned char const*)
Line
Count
Source
36
1.36G
{
37
1.36G
    uint64_t x;
38
1.36G
    memcpy(&x, ptr, 8);
39
1.36G
    return le64toh_internal(x);
40
1.36G
}
unsigned long ReadLE64<std::byte>(std::byte const*)
Line
Count
Source
36
34.0M
{
37
34.0M
    uint64_t x;
38
34.0M
    memcpy(&x, ptr, 8);
39
34.0M
    return le64toh_internal(x);
40
34.0M
}
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
306M
{
52
306M
    uint32_t v = htole32_internal(x);
53
306M
    memcpy(ptr, &v, 4);
54
306M
}
void WriteLE32<unsigned char>(unsigned char*, unsigned int)
Line
Count
Source
51
39.2M
{
52
39.2M
    uint32_t v = htole32_internal(x);
53
39.2M
    memcpy(ptr, &v, 4);
54
39.2M
}
void WriteLE32<std::byte>(std::byte*, unsigned int)
Line
Count
Source
51
267M
{
52
267M
    uint32_t v = htole32_internal(x);
53
267M
    memcpy(ptr, &v, 4);
54
267M
}
55
56
template <ByteType B>
57
inline void WriteLE64(B* ptr, uint64_t x)
58
15.7M
{
59
15.7M
    uint64_t v = htole64_internal(x);
60
15.7M
    memcpy(ptr, &v, 8);
61
15.7M
}
void WriteLE64<unsigned char>(unsigned char*, unsigned long)
Line
Count
Source
58
12.9M
{
59
12.9M
    uint64_t v = htole64_internal(x);
60
12.9M
    memcpy(ptr, &v, 8);
61
12.9M
}
void WriteLE64<std::byte>(std::byte*, unsigned long)
Line
Count
Source
58
2.76M
{
59
2.76M
    uint64_t v = htole64_internal(x);
60
2.76M
    memcpy(ptr, &v, 8);
61
2.76M
}
62
63
template <ByteType B>
64
inline uint16_t ReadBE16(const B* ptr)
65
18.8k
{
66
18.8k
    uint16_t x;
67
18.8k
    memcpy(&x, ptr, 2);
68
18.8k
    return be16toh_internal(x);
69
18.8k
}
70
71
template <ByteType B>
72
inline uint32_t ReadBE32(const B* ptr)
73
871M
{
74
871M
    uint32_t x;
75
871M
    memcpy(&x, ptr, 4);
76
871M
    return be32toh_internal(x);
77
871M
}
unsigned int ReadBE32<unsigned char>(unsigned char const*)
Line
Count
Source
73
871M
{
74
871M
    uint32_t x;
75
871M
    memcpy(&x, ptr, 4);
76
871M
    return be32toh_internal(x);
77
871M
}
unsigned int ReadBE32<std::byte>(std::byte const*)
Line
Count
Source
73
48
{
74
48
    uint32_t x;
75
48
    memcpy(&x, ptr, 4);
76
48
    return be32toh_internal(x);
77
48
}
78
79
template <ByteType B>
80
inline uint64_t ReadBE64(const B* ptr)
81
651M
{
82
651M
    uint64_t x;
83
651M
    memcpy(&x, ptr, 8);
84
651M
    return be64toh_internal(x);
85
651M
}
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
565M
{
97
565M
    uint32_t v = htobe32_internal(x);
98
565M
    memcpy(ptr, &v, 4);
99
565M
}
100
101
template <ByteType B>
102
inline void WriteBE64(B* ptr, uint64_t x)
103
416M
{
104
416M
    uint64_t v = htobe64_internal(x);
105
416M
    memcpy(ptr, &v, 8);
106
416M
}
107
108
#endif // BITCOIN_CRYPTO_COMMON_H