Raven Core  3.0.0
P2P Digital Currency
common.h
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_CRYPTO_COMMON_H
7 #define RAVEN_CRYPTO_COMMON_H
8 
9 #if defined(HAVE_CONFIG_H)
10 #include "config/raven-config.h"
11 #endif
12 
13 #include <stdint.h>
14 #include <string.h>
15 
16 #include "compat/endian.h"
17 
18 uint16_t static inline ReadLE16(const unsigned char* ptr)
19 {
20  uint16_t x;
21  memcpy((char*)&x, ptr, 2);
22  return le16toh(x);
23 }
24 
25 uint32_t static inline ReadLE32(const unsigned char* ptr)
26 {
27  uint32_t x;
28  memcpy((char*)&x, ptr, 4);
29  return le32toh(x);
30 }
31 
32 uint64_t static inline ReadLE64(const unsigned char* ptr)
33 {
34  uint64_t x;
35  memcpy((char*)&x, ptr, 8);
36  return le64toh(x);
37 }
38 
39 void static inline WriteLE16(unsigned char* ptr, uint16_t x)
40 {
41  uint16_t v = htole16(x);
42  memcpy(ptr, (char*)&v, 2);
43 }
44 
45 void static inline WriteLE32(unsigned char* ptr, uint32_t x)
46 {
47  uint32_t v = htole32(x);
48  memcpy(ptr, (char*)&v, 4);
49 }
50 
51 void static inline WriteLE64(unsigned char* ptr, uint64_t x)
52 {
53  uint64_t v = htole64(x);
54  memcpy(ptr, (char*)&v, 8);
55 }
56 
57 uint32_t static inline ReadBE32(const unsigned char* ptr)
58 {
59  uint32_t x;
60  memcpy((char*)&x, ptr, 4);
61  return be32toh(x);
62 }
63 
64 uint64_t static inline ReadBE64(const unsigned char* ptr)
65 {
66  uint64_t x;
67  memcpy((char*)&x, ptr, 8);
68  return be64toh(x);
69 }
70 
71 void static inline WriteBE32(unsigned char* ptr, uint32_t x)
72 {
73  uint32_t v = htobe32(x);
74  memcpy(ptr, (char*)&v, 4);
75 }
76 
77 void static inline WriteBE64(unsigned char* ptr, uint64_t x)
78 {
79  uint64_t v = htobe64(x);
80  memcpy(ptr, (char*)&v, 8);
81 }
82 
84 uint64_t static inline CountBits(uint64_t x)
85 {
86 #ifdef HAVE_DECL___BUILTIN_CLZL
87  if (sizeof(unsigned long) >= sizeof(uint64_t)) {
88  return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
89  }
90 #endif
91 #ifdef HAVE_DECL___BUILTIN_CLZLL
92  if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
93  return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
94  }
95 #endif
96  int ret = 0;
97  while (x) {
98  x >>= 1;
99  ++ret;
100  }
101  return ret;
102 }
103 
104 #endif // RAVEN_CRYPTO_COMMON_H
uint64_t htobe64(uint64_t host_64bits)
Definition: endian.h:168
uint32_t be32toh(uint32_t big_endian_32bits)
Definition: endian.h:154
uint32_t htole32(uint32_t host_32bits)
Definition: endian.h:147
uint32_t htobe32(uint32_t host_32bits)
Definition: endian.h:140
uint64_t be64toh(uint64_t big_endian_64bits)
Definition: endian.h:182
uint16_t le16toh(uint16_t little_endian_16bits)
Definition: endian.h:133
uint16_t htole16(uint16_t host_16bits)
Definition: endian.h:119
uint64_t le64toh(uint64_t little_endian_64bits)
Definition: endian.h:189
void * memcpy(void *a, const void *b, size_t c)
uint64_t htole64(uint64_t host_64bits)
Definition: endian.h:175
uint32_t le32toh(uint32_t little_endian_32bits)
Definition: endian.h:161