1 // Copyright (c) 2014-2018 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_COMPAT_ENDIAN_H 6 #define BITCOIN_COMPAT_ENDIAN_H 7 8 #if defined(HAVE_CONFIG_H) 9 #include <config/bitcoin-config.h> 10 #endif 11 12 #include <compat/byteswap.h> 13 14 #include <stdint.h> 15 16 #if defined(HAVE_ENDIAN_H) 17 #include <endian.h> 18 #elif defined(HAVE_SYS_ENDIAN_H) 19 #include <sys/endian.h> 20 #endif 21 22 #ifndef HAVE_CONFIG_H 23 // While not technically a supported configuration, defaulting to defining these 24 // DECLs when we were compiled without autotools makes it easier for other build 25 // systems to build things like libbitcoinconsensus for strange targets. 26 #ifdef htobe16 27 #define HAVE_DECL_HTOBE16 1 28 #endif 29 #ifdef htole16 30 #define HAVE_DECL_HTOLE16 1 31 #endif 32 #ifdef be16toh 33 #define HAVE_DECL_BE16TOH 1 34 #endif 35 #ifdef le16toh 36 #define HAVE_DECL_LE16TOH 1 37 #endif 38 39 #ifdef htobe32 40 #define HAVE_DECL_HTOBE32 1 41 #endif 42 #ifdef htole32 43 #define HAVE_DECL_HTOLE32 1 44 #endif 45 #ifdef be32toh 46 #define HAVE_DECL_BE32TOH 1 47 #endif 48 #ifdef le32toh 49 #define HAVE_DECL_LE32TOH 1 50 #endif 51 52 #ifdef htobe64 53 #define HAVE_DECL_HTOBE64 1 54 #endif 55 #ifdef htole64 56 #define HAVE_DECL_HTOLE64 1 57 #endif 58 #ifdef be64toh 59 #define HAVE_DECL_BE64TOH 1 60 #endif 61 #ifdef le64toh 62 #define HAVE_DECL_LE64TOH 1 63 #endif 64 65 #endif // HAVE_CONFIG_H 66 67 #if defined(WORDS_BIGENDIAN) 68 69 #if HAVE_DECL_HTOBE16 == 0 htobe16(uint16_t host_16bits)70inline uint16_t htobe16(uint16_t host_16bits) 71 { 72 return host_16bits; 73 } 74 #endif // HAVE_DECL_HTOBE16 75 76 #if HAVE_DECL_HTOLE16 == 0 htole16(uint16_t host_16bits)77inline uint16_t htole16(uint16_t host_16bits) 78 { 79 return bswap_16(host_16bits); 80 } 81 #endif // HAVE_DECL_HTOLE16 82 83 #if HAVE_DECL_BE16TOH == 0 be16toh(uint16_t big_endian_16bits)84inline uint16_t be16toh(uint16_t big_endian_16bits) 85 { 86 return big_endian_16bits; 87 } 88 #endif // HAVE_DECL_BE16TOH 89 90 #if HAVE_DECL_LE16TOH == 0 le16toh(uint16_t little_endian_16bits)91inline uint16_t le16toh(uint16_t little_endian_16bits) 92 { 93 return bswap_16(little_endian_16bits); 94 } 95 #endif // HAVE_DECL_LE16TOH 96 97 #if HAVE_DECL_HTOBE32 == 0 htobe32(uint32_t host_32bits)98inline uint32_t htobe32(uint32_t host_32bits) 99 { 100 return host_32bits; 101 } 102 #endif // HAVE_DECL_HTOBE32 103 104 #if HAVE_DECL_HTOLE32 == 0 htole32(uint32_t host_32bits)105inline uint32_t htole32(uint32_t host_32bits) 106 { 107 return bswap_32(host_32bits); 108 } 109 #endif // HAVE_DECL_HTOLE32 110 111 #if HAVE_DECL_BE32TOH == 0 be32toh(uint32_t big_endian_32bits)112inline uint32_t be32toh(uint32_t big_endian_32bits) 113 { 114 return big_endian_32bits; 115 } 116 #endif // HAVE_DECL_BE32TOH 117 118 #if HAVE_DECL_LE32TOH == 0 le32toh(uint32_t little_endian_32bits)119inline uint32_t le32toh(uint32_t little_endian_32bits) 120 { 121 return bswap_32(little_endian_32bits); 122 } 123 #endif // HAVE_DECL_LE32TOH 124 125 #if HAVE_DECL_HTOBE64 == 0 htobe64(uint64_t host_64bits)126inline uint64_t htobe64(uint64_t host_64bits) 127 { 128 return host_64bits; 129 } 130 #endif // HAVE_DECL_HTOBE64 131 132 #if HAVE_DECL_HTOLE64 == 0 htole64(uint64_t host_64bits)133inline uint64_t htole64(uint64_t host_64bits) 134 { 135 return bswap_64(host_64bits); 136 } 137 #endif // HAVE_DECL_HTOLE64 138 139 #if HAVE_DECL_BE64TOH == 0 be64toh(uint64_t big_endian_64bits)140inline uint64_t be64toh(uint64_t big_endian_64bits) 141 { 142 return big_endian_64bits; 143 } 144 #endif // HAVE_DECL_BE64TOH 145 146 #if HAVE_DECL_LE64TOH == 0 le64toh(uint64_t little_endian_64bits)147inline uint64_t le64toh(uint64_t little_endian_64bits) 148 { 149 return bswap_64(little_endian_64bits); 150 } 151 #endif // HAVE_DECL_LE64TOH 152 153 #else // WORDS_BIGENDIAN 154 155 #if HAVE_DECL_HTOBE16 == 0 htobe16(uint16_t host_16bits)156inline uint16_t htobe16(uint16_t host_16bits) 157 { 158 return bswap_16(host_16bits); 159 } 160 #endif // HAVE_DECL_HTOBE16 161 162 #if HAVE_DECL_HTOLE16 == 0 htole16(uint16_t host_16bits)163inline uint16_t htole16(uint16_t host_16bits) 164 { 165 return host_16bits; 166 } 167 #endif // HAVE_DECL_HTOLE16 168 169 #if HAVE_DECL_BE16TOH == 0 be16toh(uint16_t big_endian_16bits)170inline uint16_t be16toh(uint16_t big_endian_16bits) 171 { 172 return bswap_16(big_endian_16bits); 173 } 174 #endif // HAVE_DECL_BE16TOH 175 176 #if HAVE_DECL_LE16TOH == 0 le16toh(uint16_t little_endian_16bits)177inline uint16_t le16toh(uint16_t little_endian_16bits) 178 { 179 return little_endian_16bits; 180 } 181 #endif // HAVE_DECL_LE16TOH 182 183 #if HAVE_DECL_HTOBE32 == 0 htobe32(uint32_t host_32bits)184inline uint32_t htobe32(uint32_t host_32bits) 185 { 186 return bswap_32(host_32bits); 187 } 188 #endif // HAVE_DECL_HTOBE32 189 190 #if HAVE_DECL_HTOLE32 == 0 htole32(uint32_t host_32bits)191inline uint32_t htole32(uint32_t host_32bits) 192 { 193 return host_32bits; 194 } 195 #endif // HAVE_DECL_HTOLE32 196 197 #if HAVE_DECL_BE32TOH == 0 be32toh(uint32_t big_endian_32bits)198inline uint32_t be32toh(uint32_t big_endian_32bits) 199 { 200 return bswap_32(big_endian_32bits); 201 } 202 #endif // HAVE_DECL_BE32TOH 203 204 #if HAVE_DECL_LE32TOH == 0 le32toh(uint32_t little_endian_32bits)205inline uint32_t le32toh(uint32_t little_endian_32bits) 206 { 207 return little_endian_32bits; 208 } 209 #endif // HAVE_DECL_LE32TOH 210 211 #if HAVE_DECL_HTOBE64 == 0 htobe64(uint64_t host_64bits)212inline uint64_t htobe64(uint64_t host_64bits) 213 { 214 return bswap_64(host_64bits); 215 } 216 #endif // HAVE_DECL_HTOBE64 217 218 #if HAVE_DECL_HTOLE64 == 0 htole64(uint64_t host_64bits)219inline uint64_t htole64(uint64_t host_64bits) 220 { 221 return host_64bits; 222 } 223 #endif // HAVE_DECL_HTOLE64 224 225 #if HAVE_DECL_BE64TOH == 0 be64toh(uint64_t big_endian_64bits)226inline uint64_t be64toh(uint64_t big_endian_64bits) 227 { 228 return bswap_64(big_endian_64bits); 229 } 230 #endif // HAVE_DECL_BE64TOH 231 232 #if HAVE_DECL_LE64TOH == 0 le64toh(uint64_t little_endian_64bits)233inline uint64_t le64toh(uint64_t little_endian_64bits) 234 { 235 return little_endian_64bits; 236 } 237 #endif // HAVE_DECL_LE64TOH 238 239 #endif // WORDS_BIGENDIAN 240 241 #endif // BITCOIN_COMPAT_ENDIAN_H 242