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