|
| 1 | +// |
| 2 | +// endian.h |
| 3 | +// |
| 4 | +// https://gist.github.com/panzi/6856583 |
| 5 | +// |
| 6 | +// I, Mathias Panzenböck, place this file hereby into the public domain. Use |
| 7 | +// it at your own risk for whatever you like. In case there are |
| 8 | +// jurisdictions that don't support putting things in the public domain you |
| 9 | +// can also consider it to be "dual licensed" under the BSD, MIT and Apache |
| 10 | +// licenses, if you want to. This code is trivial anyway. Consider it an |
| 11 | +// example on how to get the endian conversion functions on different |
| 12 | +// platforms. |
| 13 | + |
| 14 | +#ifndef PORTABLE_ENDIAN_H__ |
| 15 | +#define PORTABLE_ENDIAN_H__ |
| 16 | + |
| 17 | +// Byte order |
| 18 | +#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__) |
| 19 | +# include <endian.h> |
| 20 | +#elif defined(__APPLE__) |
| 21 | +# include <libkern/OSByteOrder.h> |
| 22 | + |
| 23 | +# define htobe16(x) OSSwapHostToBigInt16(x) |
| 24 | +# define htole16(x) OSSwapHostToLittleInt16(x) |
| 25 | +# define be16toh(x) OSSwapBigToHostInt16(x) |
| 26 | +# define le16toh(x) OSSwapLittleToHostInt16(x) |
| 27 | + |
| 28 | +# define htobe32(x) OSSwapHostToBigInt32(x) |
| 29 | +# define htole32(x) OSSwapHostToLittleInt32(x) |
| 30 | +# define be32toh(x) OSSwapBigToHostInt32(x) |
| 31 | +# define le32toh(x) OSSwapLittleToHostInt32(x) |
| 32 | + |
| 33 | +# define htobe64(x) OSSwapHostToBigInt64(x) |
| 34 | +# define htole64(x) OSSwapHostToLittleInt64(x) |
| 35 | +# define be64toh(x) OSSwapBigToHostInt64(x) |
| 36 | +# define le64toh(x) OSSwapLittleToHostInt64(x) |
| 37 | + |
| 38 | +# define __BYTE_ORDER BYTE_ORDER |
| 39 | +# define __BIG_ENDIAN BIG_ENDIAN |
| 40 | +# define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 41 | +# define __PDP_ENDIAN PDP_ENDIAN |
| 42 | +#elif defined(__OpenBSD__) |
| 43 | +# include <sys/endian.h> |
| 44 | +#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) |
| 45 | +# include <sys/endian.h> |
| 46 | + |
| 47 | +# define be16toh(x) betoh16(x) |
| 48 | +# define le16toh(x) letoh16(x) |
| 49 | + |
| 50 | +# define be32toh(x) betoh32(x) |
| 51 | +# define le32toh(x) letoh32(x) |
| 52 | + |
| 53 | +# define be64toh(x) betoh64(x) |
| 54 | +# define le64toh(x) letoh64(x) |
| 55 | +#elif defined(_WIN32) |
| 56 | +# include <stdlib.h> |
| 57 | +# if BYTE_ORDER == LITTLE_ENDIAN |
| 58 | +# if defined(_MSC_VER) |
| 59 | +# define htobe16(x) _byteswap_ushort(x) |
| 60 | +# define htole16(x) (x) |
| 61 | +# define be16toh(x) _byteswap_ushort(x) |
| 62 | +# define le16toh(x) (x) |
| 63 | + |
| 64 | +# define htobe32(x) _byteswap_ulong(x) |
| 65 | +# define htole32(x) (x) |
| 66 | +# define be32toh(x) _byteswap_ulong(x) |
| 67 | +# define le32toh(x) (x) |
| 68 | + |
| 69 | +# define htobe64(x) _byteswap_uint64(x) |
| 70 | +# define htole64(x) (x) |
| 71 | +# define be64toh(x) _byteswap_uint64(x) |
| 72 | +# define le64toh(x) (x) |
| 73 | +# elif defined(__GNUC__) || defined(__clang__) |
| 74 | +# define htobe16(x) __builtin_bswap16(x) |
| 75 | +# define htole16(x) (x) |
| 76 | +# define be16toh(x) __builtin_bswap16(x) |
| 77 | +# define le16toh(x) (x) |
| 78 | + |
| 79 | +# define htobe32(x) __builtin_bswap32(x) |
| 80 | +# define htole32(x) (x) |
| 81 | +# define be32toh(x) __builtin_bswap32(x) |
| 82 | +# define le32toh(x) (x) |
| 83 | + |
| 84 | +# define htobe64(x) __builtin_bswap64(x) |
| 85 | +# define htole64(x) (x) |
| 86 | +# define be64toh(x) __builtin_bswap64(x) |
| 87 | +# define le64toh(x) (x) |
| 88 | +# else |
| 89 | +# error Compiler is not supported |
| 90 | +# endif |
| 91 | +# else |
| 92 | +# error Byte order is not supported |
| 93 | +# endif |
| 94 | + |
| 95 | +# define __BYTE_ORDER BYTE_ORDER |
| 96 | +# define __BIG_ENDIAN BIG_ENDIAN |
| 97 | +# define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 98 | +# define __PDP_ENDIAN PDP_ENDIAN |
| 99 | +#else |
| 100 | +# error Platform is not supported |
| 101 | +#endif |
| 102 | + |
| 103 | +#endif // PORTABLE_ENDIAN_H__ |
0 commit comments