|
| 1 | +// Copyright (c) 2014-2015 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_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 <stdint.h> |
| 13 | + |
| 14 | +#include "compat/byteswap.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 | +#if defined(WORDS_BIGENDIAN) |
| 23 | + |
| 24 | +#if HAVE_DECL_HTOBE16 == 0 |
| 25 | +inline uint16_t htobe16(uint16_t host_16bits) |
| 26 | +{ |
| 27 | + return host_16bits; |
| 28 | +} |
| 29 | +#endif // HAVE_DECL_HTOBE16 |
| 30 | + |
| 31 | +#if HAVE_DECL_HTOLE16 == 0 |
| 32 | +inline uint16_t htole16(uint16_t host_16bits) |
| 33 | +{ |
| 34 | + return bswap_16(host_16bits); |
| 35 | +} |
| 36 | +#endif // HAVE_DECL_HTOLE16 |
| 37 | + |
| 38 | +#if HAVE_DECL_BE16TOH == 0 |
| 39 | +inline uint16_t be16toh(uint16_t big_endian_16bits) |
| 40 | +{ |
| 41 | + return big_endian_16bits; |
| 42 | +} |
| 43 | +#endif // HAVE_DECL_BE16TOH |
| 44 | + |
| 45 | +#if HAVE_DECL_LE16TOH == 0 |
| 46 | +inline uint16_t le16toh(uint16_t little_endian_16bits) |
| 47 | +{ |
| 48 | + return bswap_16(little_endian_16bits); |
| 49 | +} |
| 50 | +#endif // HAVE_DECL_LE16TOH |
| 51 | + |
| 52 | +#if HAVE_DECL_HTOBE32 == 0 |
| 53 | +inline uint32_t htobe32(uint32_t host_32bits) |
| 54 | +{ |
| 55 | + return host_32bits; |
| 56 | +} |
| 57 | +#endif // HAVE_DECL_HTOBE32 |
| 58 | + |
| 59 | +#if HAVE_DECL_HTOLE32 == 0 |
| 60 | +inline uint32_t htole32(uint32_t host_32bits) |
| 61 | +{ |
| 62 | + return bswap_32(host_32bits); |
| 63 | +} |
| 64 | +#endif // HAVE_DECL_HTOLE32 |
| 65 | + |
| 66 | +#if HAVE_DECL_BE32TOH == 0 |
| 67 | +inline uint32_t be32toh(uint32_t big_endian_32bits) |
| 68 | +{ |
| 69 | + return big_endian_32bits; |
| 70 | +} |
| 71 | +#endif // HAVE_DECL_BE32TOH |
| 72 | + |
| 73 | +#if HAVE_DECL_LE32TOH == 0 |
| 74 | +inline uint32_t le32toh(uint32_t little_endian_32bits) |
| 75 | +{ |
| 76 | + return bswap_32(little_endian_32bits); |
| 77 | +} |
| 78 | +#endif // HAVE_DECL_LE32TOH |
| 79 | + |
| 80 | +#if HAVE_DECL_HTOBE64 == 0 |
| 81 | +inline uint64_t htobe64(uint64_t host_64bits) |
| 82 | +{ |
| 83 | + return host_64bits; |
| 84 | +} |
| 85 | +#endif // HAVE_DECL_HTOBE64 |
| 86 | + |
| 87 | +#if HAVE_DECL_HTOLE64 == 0 |
| 88 | +inline uint64_t htole64(uint64_t host_64bits) |
| 89 | +{ |
| 90 | + return bswap_64(host_64bits); |
| 91 | +} |
| 92 | +#endif // HAVE_DECL_HTOLE64 |
| 93 | + |
| 94 | +#if HAVE_DECL_BE64TOH == 0 |
| 95 | +inline uint64_t be64toh(uint64_t big_endian_64bits) |
| 96 | +{ |
| 97 | + return big_endian_64bits; |
| 98 | +} |
| 99 | +#endif // HAVE_DECL_BE64TOH |
| 100 | + |
| 101 | +#if HAVE_DECL_LE64TOH == 0 |
| 102 | +inline uint64_t le64toh(uint64_t little_endian_64bits) |
| 103 | +{ |
| 104 | + return bswap_64(little_endian_64bits); |
| 105 | +} |
| 106 | +#endif // HAVE_DECL_LE64TOH |
| 107 | + |
| 108 | +#else // WORDS_BIGENDIAN |
| 109 | + |
| 110 | +#if HAVE_DECL_HTOBE16 == 0 |
| 111 | +inline uint16_t htobe16(uint16_t host_16bits) |
| 112 | +{ |
| 113 | + return bswap_16(host_16bits); |
| 114 | +} |
| 115 | +#endif // HAVE_DECL_HTOBE16 |
| 116 | + |
| 117 | +#if HAVE_DECL_HTOLE16 == 0 |
| 118 | +inline uint16_t htole16(uint16_t host_16bits) |
| 119 | +{ |
| 120 | + return host_16bits; |
| 121 | +} |
| 122 | +#endif // HAVE_DECL_HTOLE16 |
| 123 | + |
| 124 | +#if HAVE_DECL_BE16TOH == 0 |
| 125 | +inline uint16_t be16toh(uint16_t big_endian_16bits) |
| 126 | +{ |
| 127 | + return bswap_16(big_endian_16bits); |
| 128 | +} |
| 129 | +#endif // HAVE_DECL_BE16TOH |
| 130 | + |
| 131 | +#if HAVE_DECL_LE16TOH == 0 |
| 132 | +inline uint16_t le16toh(uint16_t little_endian_16bits) |
| 133 | +{ |
| 134 | + return little_endian_16bits; |
| 135 | +} |
| 136 | +#endif // HAVE_DECL_LE16TOH |
| 137 | + |
| 138 | +#if HAVE_DECL_HTOBE32 == 0 |
| 139 | +inline uint32_t htobe32(uint32_t host_32bits) |
| 140 | +{ |
| 141 | + return bswap_32(host_32bits); |
| 142 | +} |
| 143 | +#endif // HAVE_DECL_HTOBE32 |
| 144 | + |
| 145 | +#if HAVE_DECL_HTOLE32 == 0 |
| 146 | +inline uint32_t htole32(uint32_t host_32bits) |
| 147 | +{ |
| 148 | + return host_32bits; |
| 149 | +} |
| 150 | +#endif // HAVE_DECL_HTOLE32 |
| 151 | + |
| 152 | +#if HAVE_DECL_BE32TOH == 0 |
| 153 | +inline uint32_t be32toh(uint32_t big_endian_32bits) |
| 154 | +{ |
| 155 | + return bswap_32(big_endian_32bits); |
| 156 | +} |
| 157 | +#endif // HAVE_DECL_BE32TOH |
| 158 | + |
| 159 | +#if HAVE_DECL_LE32TOH == 0 |
| 160 | +inline uint32_t le32toh(uint32_t little_endian_32bits) |
| 161 | +{ |
| 162 | + return little_endian_32bits; |
| 163 | +} |
| 164 | +#endif // HAVE_DECL_LE32TOH |
| 165 | + |
| 166 | +#if HAVE_DECL_HTOBE64 == 0 |
| 167 | +inline uint64_t htobe64(uint64_t host_64bits) |
| 168 | +{ |
| 169 | + return bswap_64(host_64bits); |
| 170 | +} |
| 171 | +#endif // HAVE_DECL_HTOBE64 |
| 172 | + |
| 173 | +#if HAVE_DECL_HTOLE64 == 0 |
| 174 | +inline uint64_t htole64(uint64_t host_64bits) |
| 175 | +{ |
| 176 | + return host_64bits; |
| 177 | +} |
| 178 | +#endif // HAVE_DECL_HTOLE64 |
| 179 | + |
| 180 | +#if HAVE_DECL_BE64TOH == 0 |
| 181 | +inline uint64_t be64toh(uint64_t big_endian_64bits) |
| 182 | +{ |
| 183 | + return bswap_64(big_endian_64bits); |
| 184 | +} |
| 185 | +#endif // HAVE_DECL_BE64TOH |
| 186 | + |
| 187 | +#if HAVE_DECL_LE64TOH == 0 |
| 188 | +inline uint64_t le64toh(uint64_t little_endian_64bits) |
| 189 | +{ |
| 190 | + return little_endian_64bits; |
| 191 | +} |
| 192 | +#endif // HAVE_DECL_LE64TOH |
| 193 | + |
| 194 | +#endif // WORDS_BIGENDIAN |
| 195 | + |
| 196 | +#endif // BITCOIN_COMPAT_ENDIAN_H |
0 commit comments