1 // "License": Public Domain 2 // I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like. 3 4 #ifndef PORTABLE_ENDIAN_H__ 5 #define PORTABLE_ENDIAN_H__ 6 7 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) 8 9 # define __WINDOWS__ 10 11 #endif 12 13 #if defined(__CYGWIN__) 14 15 # include <endian.h> 16 17 #elif defined(HAVE_ENDIAN_H) 18 # include <endian.h> 19 20 #elif defined(HAVE_SYS_ENDIAN_H) 21 # include <sys/endian.h> 22 23 # if defined(__OpenBSD__) 24 25 # define be16toh(x) betoh16(x) 26 # define le16toh(x) letoh16(x) 27 28 # define be32toh(x) betoh32(x) 29 # define le32toh(x) letoh32(x) 30 31 # define be64toh(x) betoh64(x) 32 # define le64toh(x) letoh64(x) 33 34 # elif defined(__sgi) 35 36 # include <netinet/in.h> 37 # include <inttypes.h> 38 39 # define be64toh(x) (x) 40 # define htobe64(x) (x) 41 42 # endif 43 44 #elif defined(__APPLE__) 45 46 # include <libkern/OSByteOrder.h> 47 48 # define htobe16(x) OSSwapHostToBigInt16(x) 49 # define htole16(x) OSSwapHostToLittleInt16(x) 50 # define be16toh(x) OSSwapBigToHostInt16(x) 51 # define le16toh(x) OSSwapLittleToHostInt16(x) 52 53 # define htobe32(x) OSSwapHostToBigInt32(x) 54 # define htole32(x) OSSwapHostToLittleInt32(x) 55 # define be32toh(x) OSSwapBigToHostInt32(x) 56 # define le32toh(x) OSSwapLittleToHostInt32(x) 57 58 # define htobe64(x) OSSwapHostToBigInt64(x) 59 # define htole64(x) OSSwapHostToLittleInt64(x) 60 # define be64toh(x) OSSwapBigToHostInt64(x) 61 # define le64toh(x) OSSwapLittleToHostInt64(x) 62 63 # define __BYTE_ORDER BYTE_ORDER 64 # define __BIG_ENDIAN BIG_ENDIAN 65 # define __LITTLE_ENDIAN LITTLE_ENDIAN 66 # define __PDP_ENDIAN PDP_ENDIAN 67 68 #elif defined(__sun) && defined(__SVR4) 69 70 # include <sys/types.h> 71 # include <netinet/in.h> 72 # include <inttypes.h> 73 74 # if !defined (ntohll) || !defined(htonll) 75 # ifdef _BIG_ENDIAN 76 # define htonll(x) (x) 77 # define ntohll(x) (x) 78 # else 79 # define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl((uint64_t)(x) >> 32)) 80 # define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl((uint64_t)(x) >> 32)) 81 # endif 82 # endif 83 84 # define be64toh(x) ntohll(x) 85 # define htobe64(x) htonll(x) 86 87 #elif defined(__WINDOWS__) 88 89 # include <winsock2.h> 90 # include <sys/param.h> 91 92 # if BYTE_ORDER == LITTLE_ENDIAN 93 94 # define htobe16(x) htons(x) 95 # define htole16(x) (x) 96 # define be16toh(x) ntohs(x) 97 # define le16toh(x) (x) 98 99 # define htobe32(x) htonl(x) 100 # define htole32(x) (x) 101 # define be32toh(x) ntohl(x) 102 # define le32toh(x) (x) 103 104 # define htobe64(x) htonll(x) 105 # define htole64(x) (x) 106 # define be64toh(x) ntohll(x) 107 # define le64toh(x) (x) 108 109 # elif BYTE_ORDER == BIG_ENDIAN 110 111 /* that would be xbox 360 */ 112 # define htobe16(x) (x) 113 # define htole16(x) __builtin_bswap16(x) 114 # define be16toh(x) (x) 115 # define le16toh(x) __builtin_bswap16(x) 116 117 # define htobe32(x) (x) 118 # define htole32(x) __builtin_bswap32(x) 119 # define be32toh(x) (x) 120 # define le32toh(x) __builtin_bswap32(x) 121 122 # define htobe64(x) (x) 123 # define htole64(x) __builtin_bswap64(x) 124 # define be64toh(x) (x) 125 # define le64toh(x) __builtin_bswap64(x) 126 127 # else 128 129 # error byte order not supported 130 131 # endif 132 133 # define __BYTE_ORDER BYTE_ORDER 134 # define __BIG_ENDIAN BIG_ENDIAN 135 # define __LITTLE_ENDIAN LITTLE_ENDIAN 136 # define __PDP_ENDIAN PDP_ENDIAN 137 138 #else 139 140 // Unsupported platforms. 141 // Intended to support CentOS 5 but hopefully not too far from 142 // the truth because we use the homebrew htonll, et al. implementations 143 // that were originally the sole implementation of this functionality 144 // in iperf 3.0. 145 # warning platform not supported 146 # include <endian.h> 147 #if BYTE_ORDER == BIG_ENDIAN 148 #define HTONLL(n) (n) 149 #define NTOHLL(n) (n) 150 #else 151 #define HTONLL(n) ((((unsigned long long)(n) & 0xFF) << 56) | \ 152 (((unsigned long long)(n) & 0xFF00) << 40) | \ 153 (((unsigned long long)(n) & 0xFF0000) << 24) | \ 154 (((unsigned long long)(n) & 0xFF000000) << 8) | \ 155 (((unsigned long long)(n) & 0xFF00000000) >> 8) | \ 156 (((unsigned long long)(n) & 0xFF0000000000) >> 24) | \ 157 (((unsigned long long)(n) & 0xFF000000000000) >> 40) | \ 158 (((unsigned long long)(n) & 0xFF00000000000000) >> 56)) 159 160 #define NTOHLL(n) ((((unsigned long long)(n) & 0xFF) << 56) | \ 161 (((unsigned long long)(n) & 0xFF00) << 40) | \ 162 (((unsigned long long)(n) & 0xFF0000) << 24) | \ 163 (((unsigned long long)(n) & 0xFF000000) << 8) | \ 164 (((unsigned long long)(n) & 0xFF00000000) >> 8) | \ 165 (((unsigned long long)(n) & 0xFF0000000000) >> 24) | \ 166 (((unsigned long long)(n) & 0xFF000000000000) >> 40) | \ 167 (((unsigned long long)(n) & 0xFF00000000000000) >> 56)) 168 #endif 169 170 #define htobe64(n) HTONLL(n) 171 #define be64toh(n) NTOHLL(n) 172 173 #endif 174 175 #endif 176 177