5 #ifndef BITCOIN_CRYPTO_COMMON_H 6 #define BITCOIN_CRYPTO_COMMON_H 8 #if defined(HAVE_CONFIG_H) 9 #include "config/bitcoin-config.h" 14 #if defined(HAVE_ENDIAN_H) 18 uint32_t
static inline ReadLE32(
const unsigned char* ptr)
20 #if HAVE_DECL_LE32TOH == 1 21 return le32toh(*((uint32_t*)ptr));
22 #elif !defined(WORDS_BIGENDIAN) 23 return *((uint32_t*)ptr);
25 return ((uint32_t)ptr[3] << 24 | (uint32_t)ptr[2] << 16 | (uint32_t)ptr[1] << 8 | (uint32_t)ptr[0]);
29 uint64_t
static inline ReadLE64(
const unsigned char* ptr)
31 #if HAVE_DECL_LE64TOH == 1 32 return le64toh(*((uint64_t*)ptr));
33 #elif !defined(WORDS_BIGENDIAN) 34 return *((uint64_t*)ptr);
36 return ((uint64_t)ptr[7] << 56 | (uint64_t)ptr[6] << 48 | (uint64_t)ptr[5] << 40 | (uint64_t)ptr[4] << 32 |
37 (uint64_t)ptr[3] << 24 | (uint64_t)ptr[2] << 16 | (uint64_t)ptr[1] << 8 | (uint64_t)ptr[0]);
41 void static inline WriteLE32(
unsigned char* ptr, uint32_t x)
43 #if HAVE_DECL_HTOLE32 == 1 44 *((uint32_t*)ptr) = htole32(x);
45 #elif !defined(WORDS_BIGENDIAN) 46 *((uint32_t*)ptr) = x;
55 void static inline WriteLE64(
unsigned char* ptr, uint64_t x)
57 #if HAVE_DECL_HTOLE64 == 1 58 *((uint64_t*)ptr) = htole64(x);
59 #elif !defined(WORDS_BIGENDIAN) 60 *((uint64_t*)ptr) = x;
73 uint32_t
static inline ReadBE32(
const unsigned char* ptr)
75 #if HAVE_DECL_BE32TOH == 1 76 return be32toh(*((uint32_t*)ptr));
78 return ((uint32_t)ptr[0] << 24 | (uint32_t)ptr[1] << 16 | (uint32_t)ptr[2] << 8 | (uint32_t)ptr[3]);
82 uint64_t
static inline ReadBE64(
const unsigned char* ptr)
84 #if HAVE_DECL_BE64TOH == 1 85 return be64toh(*((uint64_t*)ptr));
87 return ((uint64_t)ptr[0] << 56 | (uint64_t)ptr[1] << 48 | (uint64_t)ptr[2] << 40 | (uint64_t)ptr[3] << 32 |
88 (uint64_t)ptr[4] << 24 | (uint64_t)ptr[5] << 16 | (uint64_t)ptr[6] << 8 | (uint64_t)ptr[7]);
92 void static inline WriteBE32(
unsigned char* ptr, uint32_t x)
94 #if HAVE_DECL_HTOBE32 == 1 95 *((uint32_t*)ptr) = htobe32(x);
104 void static inline WriteBE64(
unsigned char* ptr, uint64_t x)
106 #if HAVE_DECL_HTOBE64 == 1 107 *((uint64_t*)ptr) = htobe64(x);
110 ptr[1] = (
unsigned char)(x >> 48);
111 ptr[2] = (
unsigned char)(x >> 40);
112 ptr[3] = (
unsigned char)(x >> 32);
113 ptr[4] = (
unsigned char)(x >> 24);
114 ptr[5] = (
unsigned char)(x >> 16);
115 ptr[6] = (
unsigned char)(x >> 8);
116 ptr[7] = (
unsigned char)x;
120 #endif // BITCOIN_CRYPTO_COMMON_H