aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrent Cook <busterb@gmail.com>2023-07-06 11:06:25 +0300
committerBrent Cook <busterb@gmail.com>2023-07-06 11:07:42 +0300
commit4aa76421303aaddbae7da8cf76549d5c0f1419be (patch)
tree6522e3c86ac630d194c9e23f34ba08f1f3ded681
parent09418fe29ed7d5829e0b251aa543b6c1b6f2090f (diff)
downloadportable-4aa76421303aaddbae7da8cf76549d5c0f1419be.tar.gz
portable-4aa76421303aaddbae7da8cf76549d5c0f1419be.tar.bz2
portable-4aa76421303aaddbae7da8cf76549d5c0f1419be.zip
update endian compatibility shim
Prefer function-like macros where possible, some style tweaks, and add Solaris support.
-rw-r--r--include/compat/endian.h54
1 files changed, 36 insertions, 18 deletions
diff --git a/include/compat/endian.h b/include/compat/endian.h
index d9f7eb2..928affe 100644
--- a/include/compat/endian.h
+++ b/include/compat/endian.h
@@ -8,9 +8,9 @@
8 8
9#if defined(_WIN32) 9#if defined(_WIN32)
10 10
11#define LITTLE_ENDIAN 1234 11#define LITTLE_ENDIAN 1234
12#define BIG_ENDIAN 4321 12#define BIG_ENDIAN 4321
13#define PDP_ENDIAN 3412 13#define PDP_ENDIAN 3412
14 14
15/* 15/*
16 * Use GCC and Visual Studio compiler defines to determine endian. 16 * Use GCC and Visual Studio compiler defines to determine endian.
@@ -28,8 +28,8 @@
28#include_next <machine/endian.h> 28#include_next <machine/endian.h>
29 29
30#elif defined(__sun) || defined(_AIX) || defined(__hpux) 30#elif defined(__sun) || defined(_AIX) || defined(__hpux)
31#include <sys/types.h>
32#include <arpa/nameser_compat.h> 31#include <arpa/nameser_compat.h>
32#include <sys/types.h>
33 33
34#elif defined(__sgi) 34#elif defined(__sgi)
35#include <standards.h> 35#include <standards.h>
@@ -39,10 +39,9 @@
39 39
40#ifndef __STRICT_ALIGNMENT 40#ifndef __STRICT_ALIGNMENT
41#define __STRICT_ALIGNMENT 41#define __STRICT_ALIGNMENT
42#if defined(__i386) || defined(__i386__) || \ 42#if defined(__i386) || defined(__i386__) || defined(__x86_64) || \
43 defined(__x86_64) || defined(__x86_64__) || \ 43 defined(__x86_64__) || defined(__s390__) || defined(__s390x__) || \
44 defined(__s390__) || defined(__s390x__) || \ 44 defined(__aarch64__) || \
45 defined(__aarch64__) || \
46 ((defined(__arm__) || defined(__arm)) && __ARM_ARCH >= 6) 45 ((defined(__arm__) || defined(__arm)) && __ARM_ARCH >= 6)
47#undef __STRICT_ALIGNMENT 46#undef __STRICT_ALIGNMENT
48#endif 47#endif
@@ -70,10 +69,16 @@
70#define be64toh(x) ntohll((x)) 69#define be64toh(x) ntohll((x))
71 70
72#if !defined(ntohll) 71#if !defined(ntohll)
73#define ntohll(x) ((1==htonl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) 72#define ntohll(x) \
73 ((1 == htonl(1)) \
74 ? (x) \
75 : ((uint64_t)ntohl((x)&0xFFFFFFFF) << 32) | ntohl((x) >> 32))
74#endif 76#endif
75#if !defined(htonll) 77#if !defined(htonll)
76#define htonll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) 78#define htonll(x) \
79 ((1 == ntohl(1)) \
80 ? (x) \
81 : ((uint64_t)htonl((x)&0xFFFFFFFF) << 32) | htonl((x) >> 32))
77#endif 82#endif
78 83
79#define htobe64(x) ntohll((x)) 84#define htobe64(x) ntohll((x))
@@ -81,13 +86,13 @@
81 86
82#ifdef __linux__ 87#ifdef __linux__
83#if !defined(betoh16) 88#if !defined(betoh16)
84#define betoh16 be16toh 89#define betoh16(x) be16toh(x)
85#endif 90#endif
86#if !defined(betoh32) 91#if !defined(betoh32)
87#define betoh32 be32toh 92#define betoh32(x) be32toh(x)
88#endif 93#endif
89#if !defined(betoh64) 94#if !defined(betoh64)
90#define betoh64 be64toh 95#define betoh64(x) be64toh(x)
91#endif 96#endif
92#endif /* __linux__ */ 97#endif /* __linux__ */
93 98
@@ -96,26 +101,39 @@
96#include <sys/endian.h> 101#include <sys/endian.h>
97#endif 102#endif
98#if !defined(betoh16) 103#if !defined(betoh16)
99#define betoh16 be16toh 104#define betoh16(x) be16toh(x)
100#endif 105#endif
101#if !defined(betoh32) 106#if !defined(betoh32)
102#define betoh32 be32toh 107#define betoh32(x) be32toh(x)
103#endif 108#endif
104#if !defined(betoh64) 109#if !defined(betoh64)
105#define betoh64 be64toh 110#define betoh64(x) be64toh(x)
106#endif 111#endif
107#endif 112#endif
108 113
109#if defined(__NetBSD__) 114#if defined(__NetBSD__)
110#if !defined(betoh16) 115#if !defined(betoh16)
111#define betoh16 be16toh 116#define betoh16(x) be16toh(x)
112#endif 117#endif
113#if !defined(betoh32) 118#if !defined(betoh32)
114#define betoh32 be32toh 119#define betoh32(x) be32toh(x)
115#endif 120#endif
116#if !defined(betoh64) 121#if !defined(betoh64)
117#define betoh64 be64toh 122#define betoh64(x) be64toh(x)
118#endif 123#endif
119#endif 124#endif
120 125
126#if defined(__sun)
127#include <sys/byteorder.h>
128#define be16toh(x) BE_IN16(x)
129#define betoh16(x) BE_IN16(x)
130#define htobe16(x) BE_16(x)
131#define be32toh(x) BE_IN32(x)
132#define betoh32(x) BE_IN32(x)
133#define htobe32(x) BE_32(x)
134#define be64toh(x) BE_IN64(x)
135#define betoh64(x) BE_IN64(x)
136#define htobe64(x) BE_64(x)
137#endif
138
121#endif 139#endif