Bitcoin Core 28.99.0
P2P Digital Currency
common.h
Go to the documentation of this file.
1// Copyright (c) 2014-2020 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 <cstdint>
11#include <cstring>
12
13uint16_t static inline ReadLE16(const unsigned char* ptr)
14{
15 uint16_t x;
16 memcpy(&x, ptr, 2);
17 return le16toh_internal(x);
18}
19
20uint32_t static inline ReadLE32(const unsigned char* ptr)
21{
22 uint32_t x;
23 memcpy(&x, ptr, 4);
24 return le32toh_internal(x);
25}
26
27uint64_t static inline ReadLE64(const unsigned char* ptr)
28{
29 uint64_t x;
30 memcpy(&x, ptr, 8);
31 return le64toh_internal(x);
32}
33
34void static inline WriteLE16(unsigned char* ptr, uint16_t x)
35{
36 uint16_t v = htole16_internal(x);
37 memcpy(ptr, &v, 2);
38}
39
40void static inline WriteLE32(unsigned char* ptr, uint32_t x)
41{
42 uint32_t v = htole32_internal(x);
43 memcpy(ptr, &v, 4);
44}
45
46void static inline WriteLE64(unsigned char* ptr, uint64_t x)
47{
48 uint64_t v = htole64_internal(x);
49 memcpy(ptr, &v, 8);
50}
51
52uint16_t static inline ReadBE16(const unsigned char* ptr)
53{
54 uint16_t x;
55 memcpy(&x, ptr, 2);
56 return be16toh_internal(x);
57}
58
59uint32_t static inline ReadBE32(const unsigned char* ptr)
60{
61 uint32_t x;
62 memcpy(&x, ptr, 4);
63 return be32toh_internal(x);
64}
65
66uint64_t static inline ReadBE64(const unsigned char* ptr)
67{
68 uint64_t x;
69 memcpy(&x, ptr, 8);
70 return be64toh_internal(x);
71}
72
73void static inline WriteBE16(unsigned char* ptr, uint16_t x)
74{
75 uint16_t v = htobe16_internal(x);
76 memcpy(ptr, &v, 2);
77}
78
79void static inline WriteBE32(unsigned char* ptr, uint32_t x)
80{
81 uint32_t v = htobe32_internal(x);
82 memcpy(ptr, &v, 4);
83}
84
85void static inline WriteBE64(unsigned char* ptr, uint64_t x)
86{
87 uint64_t v = htobe64_internal(x);
88 memcpy(ptr, &v, 8);
89}
90
91#endif // BITCOIN_CRYPTO_COMMON_H
static void WriteBE16(unsigned char *ptr, uint16_t x)
Definition: common.h:73
static uint16_t ReadLE16(const unsigned char *ptr)
Definition: common.h:13
static void WriteLE16(unsigned char *ptr, uint16_t x)
Definition: common.h:34
static uint64_t ReadBE64(const unsigned char *ptr)
Definition: common.h:66
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:27
static uint16_t ReadBE16(const unsigned char *ptr)
Definition: common.h:52
static uint32_t ReadLE32(const unsigned char *ptr)
Definition: common.h:20
static void WriteBE32(unsigned char *ptr, uint32_t x)
Definition: common.h:79
static void WriteLE32(unsigned char *ptr, uint32_t x)
Definition: common.h:40
static void WriteBE64(unsigned char *ptr, uint64_t x)
Definition: common.h:85
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:59
static void WriteLE64(unsigned char *ptr, uint64_t x)
Definition: common.h:46
BSWAP_CONSTEXPR uint32_t be32toh_internal(uint32_t big_endian_32bits)
Definition: endian.h:43
BSWAP_CONSTEXPR uint16_t be16toh_internal(uint16_t big_endian_16bits)
Definition: endian.h:23
BSWAP_CONSTEXPR uint64_t htobe64_internal(uint64_t host_64bits)
Definition: endian.h:53
BSWAP_CONSTEXPR uint16_t htobe16_internal(uint16_t host_16bits)
Definition: endian.h:13
BSWAP_CONSTEXPR uint32_t htole32_internal(uint32_t host_32bits)
Definition: endian.h:38
BSWAP_CONSTEXPR uint16_t htole16_internal(uint16_t host_16bits)
Definition: endian.h:18
BSWAP_CONSTEXPR uint64_t be64toh_internal(uint64_t big_endian_64bits)
Definition: endian.h:63
BSWAP_CONSTEXPR uint16_t le16toh_internal(uint16_t little_endian_16bits)
Definition: endian.h:28
BSWAP_CONSTEXPR uint64_t htole64_internal(uint64_t host_64bits)
Definition: endian.h:58
BSWAP_CONSTEXPR uint64_t le64toh_internal(uint64_t little_endian_64bits)
Definition: endian.h:68
BSWAP_CONSTEXPR uint32_t le32toh_internal(uint32_t little_endian_32bits)
Definition: endian.h:48
BSWAP_CONSTEXPR uint32_t htobe32_internal(uint32_t host_32bits)
Definition: endian.h:33