Skip to content

Commit 20c2783

Browse files
committed
fix(lib): fix compilation issue on windows with endian.h
1 parent b7a0052 commit 20c2783

File tree

1 file changed

+61
-25
lines changed

1 file changed

+61
-25
lines changed

lib/src/portable/endian.h

+61-25
Original file line numberDiff line numberDiff line change
@@ -65,57 +65,93 @@
6565

6666
#elif defined(__WINDOWS__)
6767

68-
# include <winsock2.h>
69-
# ifdef __GNUC__
70-
# include <sys/param.h>
68+
69+
# if defined(_MSC_VER) && !defined(__clang__)
70+
# include <stdlib.h>
71+
# define B_SWAP_16(x) _byteswap_ushort(x)
72+
# define B_SWAP_32(x) _byteswap_ulong(x)
73+
# define B_SWAP_64(x) _byteswap_uint64(x)
74+
# else
75+
# define B_SWAP_16(x) __builtin_bswap16(x)
76+
# define B_SWAP_32(x) __builtin_bswap32(x)
77+
# define B_SWAP_64(x) __builtin_bswap64(x)
78+
# endif
79+
80+
# if defined(__MINGW32__) || defined(HAVE_SYS_PARAM_H)
81+
# include <sys/param.h>
82+
# endif
83+
84+
# ifndef BIG_ENDIAN
85+
# ifdef __BIG_ENDIAN
86+
# define BIG_ENDIAN __BIG_ENDIAN
87+
# elif defined(__ORDER_BIG_ENDIAN__)
88+
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
89+
# else
90+
# define BIG_ENDIAN 4321
91+
# endif
92+
# endif
93+
94+
# ifndef LITTLE_ENDIAN
95+
# ifdef __LITTLE_ENDIAN
96+
# define LITTLE_ENDIAN __LITTLE_ENDIAN
97+
# elif defined(__ORDER_LITTLE_ENDIAN__)
98+
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
99+
# else
100+
# define LITTLE_ENDIAN 1234
101+
# endif
102+
# endif
103+
104+
# ifndef BYTE_ORDER
105+
# ifdef __BYTE_ORDER
106+
# define BYTE_ORDER __BYTE_ORDER
107+
# elif defined(__BYTE_ORDER__)
108+
# define BYTE_ORDER __BYTE_ORDER__
109+
# else
110+
/* assume LE on Windows if nothing was defined */
111+
# define BYTE_ORDER LITTLE_ENDIAN
112+
# endif
71113
# endif
72114

73115
# if BYTE_ORDER == LITTLE_ENDIAN
74116

75-
# define htobe16(x) htons(x)
117+
# define htobe16(x) B_SWAP_16(x)
76118
# define htole16(x) (x)
77-
# define be16toh(x) ntohs(x)
119+
# define be16toh(x) B_SWAP_16(x)
78120
# define le16toh(x) (x)
79-
80-
# define htobe32(x) htonl(x)
121+
122+
# define htobe32(x) B_SWAP_32(x)
81123
# define htole32(x) (x)
82-
# define be32toh(x) ntohl(x)
124+
# define be32toh(x) B_SWAP_32(x)
83125
# define le32toh(x) (x)
84-
85-
# define htobe64(x) htonll(x)
126+
127+
# define htobe64(x) B_SWAP_64(x)
86128
# define htole64(x) (x)
87-
# define be64toh(x) ntohll(x)
129+
# define be64toh(x) B_SWAP_64(x)
88130
# define le64toh(x) (x)
89131

90132
# elif BYTE_ORDER == BIG_ENDIAN
91133

92-
/* that would be xbox 360 */
93134
# define htobe16(x) (x)
94-
# define htole16(x) __builtin_bswap16(x)
135+
# define htole16(x) B_SWAP_16(x)
95136
# define be16toh(x) (x)
96-
# define le16toh(x) __builtin_bswap16(x)
97-
137+
# define le16toh(x) B_SWAP_16(x)
138+
98139
# define htobe32(x) (x)
99-
# define htole32(x) __builtin_bswap32(x)
140+
# define htole32(x) B_SWAP_32(x)
100141
# define be32toh(x) (x)
101-
# define le32toh(x) __builtin_bswap32(x)
102-
142+
# define le32toh(x) B_SWAP_32(x)
143+
103144
# define htobe64(x) (x)
104-
# define htole64(x) __builtin_bswap64(x)
145+
# define htole64(x) B_SWAP_64(x)
105146
# define be64toh(x) (x)
106-
# define le64toh(x) __builtin_bswap64(x)
147+
# define le64toh(x) B_SWAP_64(x)
107148

108149
# else
109150

110151
# error byte order not supported
111152

112153
# endif
113154

114-
# define __BYTE_ORDER BYTE_ORDER
115-
# define __BIG_ENDIAN BIG_ENDIAN
116-
# define __LITTLE_ENDIAN LITTLE_ENDIAN
117-
# define __PDP_ENDIAN PDP_ENDIAN
118-
119155
#elif defined(__QNXNTO__)
120156

121157
# include <gulliver.h>

0 commit comments

Comments
 (0)