hdac SDK
SDK for hdac blockchain development
common.h
1 // Copyright (c) 2014 The Bitcoin 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 <stdint.h>
13 
14 #if defined(HAVE_ENDIAN_H)
15 #include <endian.h>
16 #endif
17 
18 uint32_t static inline ReadLE32(const unsigned char* ptr)
19 {
20 #if HAVE_DECL_LE32TOH == 1
21  return le32toh(*((uint32_t*)ptr));
22 #elif !defined(WORDS_BIGENDIAN)
23  return *((uint32_t*)ptr);
24 #else
25  return ((uint32_t)ptr[3] << 24 | (uint32_t)ptr[2] << 16 | (uint32_t)ptr[1] << 8 | (uint32_t)ptr[0]);
26 #endif
27 }
28 
29 uint64_t static inline ReadLE64(const unsigned char* ptr)
30 {
31 #if HAVE_DECL_LE64TOH == 1
32  return le64toh(*((uint64_t*)ptr));
33 #elif !defined(WORDS_BIGENDIAN)
34  return *((uint64_t*)ptr);
35 #else
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]);
38 #endif
39 }
40 
41 void static inline WriteLE32(unsigned char* ptr, uint32_t x)
42 {
43 #if HAVE_DECL_HTOLE32 == 1
44  *((uint32_t*)ptr) = htole32(x);
45 #elif !defined(WORDS_BIGENDIAN)
46  *((uint32_t*)ptr) = x;
47 #else
48  ptr[3] = x >> 24;
49  ptr[2] = x >> 16;
50  ptr[1] = x >> 8;
51  ptr[0] = x;
52 #endif
53 }
54 
55 void static inline WriteLE64(unsigned char* ptr, uint64_t x)
56 {
57 #if HAVE_DECL_HTOLE64 == 1
58  *((uint64_t*)ptr) = htole64(x);
59 #elif !defined(WORDS_BIGENDIAN)
60  *((uint64_t*)ptr) = x;
61 #else
62  ptr[7] = x >> 56;
63  ptr[6] = x >> 48;
64  ptr[5] = x >> 40;
65  ptr[4] = x >> 32;
66  ptr[3] = x >> 24;
67  ptr[2] = x >> 16;
68  ptr[1] = x >> 8;
69  ptr[0] = x;
70 #endif
71 }
72 
73 uint32_t static inline ReadBE32(const unsigned char* ptr)
74 {
75 #if HAVE_DECL_BE32TOH == 1
76  return be32toh(*((uint32_t*)ptr));
77 #else
78  return ((uint32_t)ptr[0] << 24 | (uint32_t)ptr[1] << 16 | (uint32_t)ptr[2] << 8 | (uint32_t)ptr[3]);
79 #endif
80 }
81 
82 uint64_t static inline ReadBE64(const unsigned char* ptr)
83 {
84 #if HAVE_DECL_BE64TOH == 1
85  return be64toh(*((uint64_t*)ptr));
86 #else
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]);
89 #endif
90 }
91 
92 void static inline WriteBE32(unsigned char* ptr, uint32_t x)
93 {
94 #if HAVE_DECL_HTOBE32 == 1
95  *((uint32_t*)ptr) = htobe32(x);
96 #else
97  ptr[0] = x >> 24;
98  ptr[1] = x >> 16;
99  ptr[2] = x >> 8;
100  ptr[3] = x;
101 #endif
102 }
103 
104 void static inline WriteBE64(unsigned char* ptr, uint64_t x)
105 {
106 #if HAVE_DECL_HTOBE64 == 1
107  *((uint64_t*)ptr) = htobe64(x);
108 #else
109  ptr[0] = x >> 56;
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;
117 #endif
118 }
119 
120 #endif // BITCOIN_CRYPTO_COMMON_H