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