Skip to content

Commit 36f5f79

Browse files
clasonamaanq
authored andcommitted
fix(endian): rely on system headers where possible
Problem: Macros (re)defined in `endian.h` conflict with system headers on FreeBSD (at least). Solution: Rely on system `endian.h` on OpenBSD, FreeBSD, NetBSD, and DragonFly Ref. mikepb/endian.h#4
1 parent c01bf6e commit 36f5f79

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

lib/src/portable/endian.h

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,33 @@
44
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
55
// an example on how to get the endian conversion functions on different platforms.
66

7-
#ifndef PORTABLE_ENDIAN_H__
8-
#define PORTABLE_ENDIAN_H__
7+
// updates from https://github.com/mikepb/endian.h/issues/4
8+
9+
#ifndef ENDIAN_H
10+
#define ENDIAN_H
911

1012
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
1113

1214
# define __WINDOWS__
1315

1416
#endif
1517

16-
#if defined(__linux__) || defined(__CYGWIN__) || defined(__GNU__) || defined(__EMSCRIPTEN__)
18+
#if defined(HAVE_ENDIAN_H) || \
19+
defined(__linux__) || \
20+
defined(__GNU__) || \
21+
defined(__OpenBSD__) || \
22+
defined(__CYGWIN__) || \
23+
defined(__MSYS__) || \
24+
defined(__EMSCRIPTEN__)
25+
26+
# include <endian.h>
1727

18-
# include <endian.h>
28+
#elif defined(HAVE_SYS_ENDIAN_H) || \
29+
defined(__FreeBSD__) || \
30+
defined(__NetBSD__) || \
31+
defined(__DragonFly__)
32+
33+
# include <sys/endian.h>
1934

2035
#elif defined(__APPLE__)
2136
# define __BYTE_ORDER BYTE_ORDER
@@ -75,31 +90,9 @@
7590
# error byte order not supported
7691
# endif
7792
# endif
78-
#elif defined(__OpenBSD__)
79-
80-
# include <endian.h>
81-
82-
# define __BYTE_ORDER BYTE_ORDER
83-
# define __BIG_ENDIAN BIG_ENDIAN
84-
# define __LITTLE_ENDIAN LITTLE_ENDIAN
85-
# define __PDP_ENDIAN PDP_ENDIAN
86-
87-
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
88-
89-
# include <sys/endian.h>
90-
91-
# define be16toh(x) betoh16(x)
92-
# define le16toh(x) letoh16(x)
93-
94-
# define be32toh(x) betoh32(x)
95-
# define le32toh(x) letoh32(x)
96-
97-
# define be64toh(x) betoh64(x)
98-
# define le64toh(x) letoh64(x)
9993

10094
#elif defined(__WINDOWS__)
10195

102-
10396
# if defined(_MSC_VER) && !defined(__clang__)
10497
# include <stdlib.h>
10598
# define B_SWAP_16(x) _byteswap_ushort(x)

0 commit comments

Comments
 (0)