Bitcoin ABC  0.24.7
P2P Digital Currency
common.h
Go to the documentation of this file.
1 // Copyright (c) 2014 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 #if defined(HAVE_CONFIG_H)
9 #include <config/bitcoin-config.h>
10 #endif
11 
12 #include <cstdint>
13 #include <cstring>
14 
15 #include <compat/endian.h>
16 
17 static inline uint16_t ReadLE16(const uint8_t *ptr) {
18  uint16_t x;
19  memcpy((char *)&x, ptr, 2);
20  return le16toh(x);
21 }
22 
23 static inline uint32_t ReadLE32(const uint8_t *ptr) {
24  uint32_t x;
25  memcpy((char *)&x, ptr, 4);
26  return le32toh(x);
27 }
28 
29 static inline uint64_t ReadLE64(const uint8_t *ptr) {
30  uint64_t x;
31  memcpy((char *)&x, ptr, 8);
32  return le64toh(x);
33 }
34 
35 static inline void WriteLE16(uint8_t *ptr, uint16_t x) {
36  uint16_t v = htole16(x);
37  memcpy(ptr, (char *)&v, 2);
38 }
39 
40 static inline void WriteLE32(uint8_t *ptr, uint32_t x) {
41  uint32_t v = htole32(x);
42  memcpy(ptr, (char *)&v, 4);
43 }
44 
45 static inline void WriteLE64(uint8_t *ptr, uint64_t x) {
46  uint64_t v = htole64(x);
47  memcpy(ptr, (char *)&v, 8);
48 }
49 
50 uint16_t static inline ReadBE16(const uint8_t *ptr) {
51  uint16_t x;
52  memcpy((char *)&x, ptr, 2);
53  return be16toh(x);
54 }
55 
56 static inline uint32_t ReadBE32(const uint8_t *ptr) {
57  uint32_t x;
58  memcpy((char *)&x, ptr, 4);
59  return be32toh(x);
60 }
61 
62 static inline uint64_t ReadBE64(const uint8_t *ptr) {
63  uint64_t x;
64  memcpy((char *)&x, ptr, 8);
65  return be64toh(x);
66 }
67 
68 static inline void WriteBE32(uint8_t *ptr, uint32_t x) {
69  uint32_t v = htobe32(x);
70  memcpy(ptr, (char *)&v, 4);
71 }
72 
73 static inline void WriteBE64(uint8_t *ptr, uint64_t x) {
74  uint64_t v = htobe64(x);
75  memcpy(ptr, (char *)&v, 8);
76 }
77 
82 uint64_t static inline CountBits(uint64_t x) {
83 #if HAVE_DECL___BUILTIN_CLZL
84  if (sizeof(unsigned long) >= sizeof(uint64_t)) {
85  return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
86  }
87 #endif
88 #if HAVE_DECL___BUILTIN_CLZLL
89  if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
90  return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
91  }
92 #endif
93  int ret = 0;
94  while (x) {
95  x >>= 1;
96  ++ret;
97  }
98  return ret;
99 }
100 
101 #endif // BITCOIN_CRYPTO_COMMON_H
htole16
uint16_t htole16(uint16_t host_16bits)
Definition: endian.h:150
htole64
uint64_t htole64(uint64_t host_64bits)
Definition: endian.h:198
WriteLE32
static void WriteLE32(uint8_t *ptr, uint32_t x)
Definition: common.h:40
ReadLE64
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
le32toh
uint32_t le32toh(uint32_t little_endian_32bits)
Definition: endian.h:186
ReadLE32
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:23
be64toh
uint64_t be64toh(uint64_t big_endian_64bits)
Definition: endian.h:204
be32toh
uint32_t be32toh(uint32_t big_endian_32bits)
Definition: endian.h:180
ReadLE16
static uint16_t ReadLE16(const uint8_t *ptr)
Definition: common.h:17
ReadBE64
static uint64_t ReadBE64(const uint8_t *ptr)
Definition: common.h:62
WriteLE64
static void WriteLE64(uint8_t *ptr, uint64_t x)
Definition: common.h:45
le16toh
uint16_t le16toh(uint16_t little_endian_16bits)
Definition: endian.h:162
htobe32
uint32_t htobe32(uint32_t host_32bits)
Definition: endian.h:168
be16toh
uint16_t be16toh(uint16_t big_endian_16bits)
Definition: endian.h:156
WriteBE64
static void WriteBE64(uint8_t *ptr, uint64_t x)
Definition: common.h:73
WriteBE32
static void WriteBE32(uint8_t *ptr, uint32_t x)
Definition: common.h:68
htole32
uint32_t htole32(uint32_t host_32bits)
Definition: endian.h:174
CountBits
static uint64_t CountBits(uint64_t x)
Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set.
Definition: common.h:82
le64toh
uint64_t le64toh(uint64_t little_endian_64bits)
Definition: endian.h:210
htobe64
uint64_t htobe64(uint64_t host_64bits)
Definition: endian.h:192
ReadBE16
static uint16_t ReadBE16(const uint8_t *ptr)
Definition: common.h:50
WriteLE16
static void WriteLE16(uint8_t *ptr, uint16_t x)
Definition: common.h:35
ReadBE32
static uint32_t ReadBE32(const uint8_t *ptr)
Definition: common.h:56
endian.h