| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifndef __ENDIAN_SHIM_H |
| 9 |
#define __ENDIAN_SHIM_H |
| 10 |
|
| 11 |
#if defined(_WIN32) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600 |
| 18 |
# undef _WIN32_WINNT |
| 19 |
# define _WIN32_WINNT 0x0600 |
| 20 |
#endif |
| 21 |
#include <winsock2.h> |
| 22 |
|
| 23 |
#define be16toh(x) ntohs(x) |
| 24 |
#define htobe16(x) htons(x) |
| 25 |
|
| 26 |
#define be32toh(x) ntohl(x) |
| 27 |
#define htobe32(x) htonl(x) |
| 28 |
|
| 29 |
|
| 30 |
#if BYTE_ORDER == LITTLE_ENDIAN |
| 31 |
# define htobe64(x) htonll(x) |
| 32 |
# define be64toh(x) ntohll(x) |
| 33 |
#else |
| 34 |
#error byte order not supported |
| 35 |
#endif |
| 36 |
|
| 37 |
#elif defined(__APPLE__) |
| 38 |
# include <libkern/OSByteOrder.h> |
| 39 |
|
| 40 |
|
| 41 |
#define htobe16(x) OSSwapHostToBigInt16(x) |
| 42 |
#define htole16(x) OSSwapHostToLittleInt16(x) |
| 43 |
#define be16toh(x) OSSwapBigToHostInt16(x) |
| 44 |
#define le16toh(x) OSSwapLittleToHostInt16(x) |
| 45 |
|
| 46 |
#define htobe32(x) OSSwapHostToBigInt32(x) |
| 47 |
#define htole32(x) OSSwapHostToLittleInt32(x) |
| 48 |
#define be32toh(x) OSSwapBigToHostInt32(x) |
| 49 |
#define le32toh(x) OSSwapLittleToHostInt32(x) |
| 50 |
|
| 51 |
#define htobe64(x) OSSwapHostToBigInt64(x) |
| 52 |
#define htole64(x) OSSwapHostToLittleInt64(x) |
| 53 |
#define be64toh(x) OSSwapBigToHostInt64(x) |
| 54 |
#define le64toh(x) OSSwapLittleToHostInt64(x) |
| 55 |
|
| 56 |
#elif defined(__linux__) || defined(__GNU__) || defined(__EMSCRIPTEN__) |
| 57 |
# include <endian.h> |
| 58 |
# if !defined(htobe64) |
| 59 |
# if __BYTE_ORDER == __LITTLE_ENDIAN |
| 60 |
# define htobe64(x) __bswap_64(x) |
| 61 |
# else |
| 62 |
# define htobe64(x) (x) |
| 63 |
# endif |
| 64 |
# endif |
| 65 |
# if !defined(be64toh) |
| 66 |
# if __BYTE_ORDER == __LITTLE_ENDIAN |
| 67 |
# define be64toh(x) __bswap_64(x) |
| 68 |
# else |
| 69 |
# define be64toh(x) (x) |
| 70 |
# endif |
| 71 |
# endif |
| 72 |
#else |
| 73 |
#include <sys/endian.h> |
| 74 |
#endif |
| 75 |
|
| 76 |
#endif |