1+ #ifndef STARKWARE_CRYPTO_FFI_PORTABLE_ENDIAN_H_
2+ #define STARKWARE_CRYPTO_FFI_PORTABLE_ENDIAN_H_
3+
4+ #if (defined(_WIN16 ) || defined(_WIN32 ) || defined(_WIN64 )) && !defined(__WINDOWS__ )
5+
6+ # define __WINDOWS__
7+
8+ #endif
9+
10+ #if defined(__linux__ ) || defined(__CYGWIN__ )
11+
12+ # include <endian.h>
13+
14+ #elif defined(__APPLE__ )
15+
16+ # include <libkern/OSByteOrder.h>
17+
18+ # define htobe16 (x ) OSSwapHostToBigInt16(x)
19+ # define htole16 (x ) OSSwapHostToLittleInt16(x)
20+ # define be16toh (x ) OSSwapBigToHostInt16(x)
21+ # define le16toh (x ) OSSwapLittleToHostInt16(x)
22+
23+ # define htobe32 (x ) OSSwapHostToBigInt32(x)
24+ # define htole32 (x ) OSSwapHostToLittleInt32(x)
25+ # define be32toh (x ) OSSwapBigToHostInt32(x)
26+ # define le32toh (x ) OSSwapLittleToHostInt32(x)
27+
28+ # define htobe64 (x ) OSSwapHostToBigInt64(x)
29+ # define htole64 (x ) OSSwapHostToLittleInt64(x)
30+ # define be64toh (x ) OSSwapBigToHostInt64(x)
31+ # define le64toh (x ) OSSwapLittleToHostInt64(x)
32+
33+ # define __BYTE_ORDER BYTE_ORDER
34+ # define __BIG_ENDIAN BIG_ENDIAN
35+ # define __LITTLE_ENDIAN LITTLE_ENDIAN
36+ # define __PDP_ENDIAN PDP_ENDIAN
37+
38+ #elif defined(__OpenBSD__ )
39+
40+ # include <sys/endian.h>
41+
42+ #elif defined(__NetBSD__ ) || defined(__FreeBSD__ ) || defined(__DragonFly__ )
43+
44+ # include <sys/endian.h>
45+
46+ # define be16toh (x ) betoh16(x)
47+ # define le16toh (x ) letoh16(x)
48+
49+ # define be32toh (x ) betoh32(x)
50+ # define le32toh (x ) letoh32(x)
51+
52+ # define be64toh (x ) betoh64(x)
53+ # define le64toh (x ) letoh64(x)
54+
55+ #elif defined(__WINDOWS__ )
56+
57+ # include <winsock2.h>
58+ # include <sys/param.h>
59+
60+ # if BYTE_ORDER == LITTLE_ENDIAN
61+
62+ # define htobe16 (x ) htons(x)
63+ # define htole16 (x ) (x)
64+ # define be16toh (x ) ntohs(x)
65+ # define le16toh (x ) (x)
66+
67+ # define htobe32 (x ) htonl(x)
68+ # define htole32 (x ) (x)
69+ # define be32toh (x ) ntohl(x)
70+ # define le32toh (x ) (x)
71+
72+ # define htobe64 (x ) htonll(x)
73+ # define htole64 (x ) (x)
74+ # define be64toh (x ) ntohll(x)
75+ # define le64toh (x ) (x)
76+
77+ # elif BYTE_ORDER == BIG_ENDIAN
78+
79+ /* that would be xbox 360 */
80+ # define htobe16 (x ) (x)
81+ # define htole16 (x ) __builtin_bswap16(x)
82+ # define be16toh (x ) (x)
83+ # define le16toh (x ) __builtin_bswap16(x)
84+
85+ # define htobe32 (x ) (x)
86+ # define htole32 (x ) __builtin_bswap32(x)
87+ # define be32toh (x ) (x)
88+ # define le32toh (x ) __builtin_bswap32(x)
89+
90+ # define htobe64 (x ) (x)
91+ # define htole64 (x ) __builtin_bswap64(x)
92+ # define be64toh (x ) (x)
93+ # define le64toh (x ) __builtin_bswap64(x)
94+
95+ # else
96+
97+ # error byte order not supported
98+
99+ # endif
100+
101+ # define __BYTE_ORDER BYTE_ORDER
102+ # define __BIG_ENDIAN BIG_ENDIAN
103+ # define __LITTLE_ENDIAN LITTLE_ENDIAN
104+ # define __PDP_ENDIAN PDP_ENDIAN
105+
106+ #else
107+
108+ # error platform not supported
109+
110+ #endif
111+
112+ #endif // STARKWARE_CRYPTO_FFI_PORTABLE_ENDIAN_H_
0 commit comments