Commit | Line | Data |
---|---|---|
eb71a0aa | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <dgoulet@efficios.com> |
eb71a0aa | 3 | * |
c922647d | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
eb71a0aa | 5 | * |
eb71a0aa DG |
6 | */ |
7 | ||
f263b7fd | 8 | #ifndef _COMPAT_ENDIAN_H |
eb71a0aa DG |
9 | #define _COMPAT_ENDIAN_H |
10 | ||
07bb6d71 | 11 | #if defined(__linux__) || defined(__CYGWIN__) |
eb71a0aa | 12 | #include <endian.h> |
4ff128af | 13 | #include <byteswap.h> |
f263b7fd JD |
14 | |
15 | /* | |
16 | * htobe/betoh are not defined for glibc <2.9, so add them | |
17 | * explicitly if they are missing. | |
18 | */ | |
19 | #ifdef __USE_BSD | |
20 | /* Conversion interfaces. */ | |
21 | # include <byteswap.h> | |
22 | ||
23 | # if __BYTE_ORDER == __LITTLE_ENDIAN | |
24 | # ifndef htobe16 | |
25 | # define htobe16(x) __bswap_16(x) | |
26 | # endif | |
27 | # ifndef htole16 | |
28 | # define htole16(x) (x) | |
29 | # endif | |
30 | # ifndef be16toh | |
31 | # define be16toh(x) __bswap_16(x) | |
32 | # endif | |
33 | # ifndef le16toh | |
34 | # define le16toh(x) (x) | |
35 | # endif | |
36 | ||
37 | # ifndef htobe32 | |
38 | # define htobe32(x) __bswap_32(x) | |
39 | # endif | |
40 | # ifndef htole32 | |
41 | # define htole32(x) (x) | |
42 | # endif | |
43 | # ifndef be32toh | |
44 | # define be32toh(x) __bswap_32(x) | |
45 | # endif | |
46 | # ifndef le32toh | |
47 | # define le32toh(x) (x) | |
48 | # endif | |
49 | ||
50 | # ifndef htobe64 | |
51 | # define htobe64(x) __bswap_64(x) | |
52 | # endif | |
53 | # ifndef htole64 | |
54 | # define htole64(x) (x) | |
55 | # endif | |
56 | # ifndef be64toh | |
57 | # define be64toh(x) __bswap_64(x) | |
58 | # endif | |
59 | # ifndef le64toh | |
60 | # define le64toh(x) (x) | |
61 | # endif | |
62 | ||
63 | # else /* __BYTE_ORDER == __LITTLE_ENDIAN */ | |
64 | # ifndef htobe16 | |
65 | # define htobe16(x) (x) | |
66 | # endif | |
67 | # ifndef htole16 | |
68 | # define htole16(x) __bswap_16(x) | |
69 | # endif | |
70 | # ifndef be16toh | |
71 | # define be16toh(x) (x) | |
72 | # endif | |
73 | # ifndef le16toh | |
74 | # define le16toh(x) __bswap_16(x) | |
75 | # endif | |
76 | ||
77 | # ifndef htobe32 | |
78 | # define htobe32(x) (x) | |
79 | # endif | |
80 | # ifndef htole32 | |
81 | # define htole32(x) __bswap_32(x) | |
82 | # endif | |
83 | # ifndef be32toh | |
84 | # define be32toh(x) (x) | |
85 | # endif | |
86 | # ifndef le32toh | |
87 | # define le32toh(x) __bswap_32(x) | |
88 | # endif | |
89 | ||
90 | # ifndef htobe64 | |
91 | # define htobe64(x) (x) | |
92 | # endif | |
93 | # ifndef htole64 | |
94 | # define htole64(x) __bswap_64(x) | |
95 | # endif | |
96 | # ifndef be64toh | |
97 | # define be64toh(x) (x) | |
98 | # endif | |
99 | # ifndef le64toh | |
100 | # define le64toh(x) __bswap_64(x) | |
101 | # endif | |
102 | ||
103 | # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */ | |
104 | #endif /* __USE_BSD */ | |
105 | ||
b2c3836f | 106 | #elif defined(__FreeBSD__) |
4ff128af MJ |
107 | #include <sys/endian.h> |
108 | ||
109 | #define bswap_16(x) bswap16(x) | |
110 | #define bswap_32(x) bswap32(x) | |
111 | #define bswap_64(x) bswap64(x) | |
0c97bd7a MJ |
112 | |
113 | #elif defined(__sun__) | |
114 | #include <sys/byteorder.h> | |
115 | #ifndef __BIG_ENDIAN | |
116 | #define __BIG_ENDIAN 4321 | |
117 | #endif /* __BIG_ENDIAN */ | |
118 | #ifndef __LITTLE_ENDIAN | |
119 | #define __LITTLE_ENDIAN 1234 | |
120 | #endif /* __LITTLE_ENDIAN */ | |
121 | ||
122 | #ifdef _LITTLE_ENDIAN | |
123 | #define __BYTE_ORDER __LITTLE_ENDIAN | |
124 | #endif /* _LITTLE_ENDIAN */ | |
125 | #ifdef _BIG_ENDIAN | |
126 | #define __BYTE_ORDER __BIG_ENDIAN | |
127 | #endif /* _BIG_ENDIAN */ | |
128 | ||
129 | #define LITTLE_ENDIAN __LITTLE_ENDIAN | |
130 | #define BIG_ENDIAN __BIG_ENDIAN | |
131 | #define PDP_ENDIAN __PDP_ENDIAN | |
132 | #define BYTE_ORDER __BYTE_ORDER | |
133 | ||
134 | #define betoh16(x) BE_16(x) | |
135 | #define letoh16(x) LE_16(x) | |
136 | #define betoh32(x) BE_32(x) | |
137 | #define letoh32(x) LE_32(x) | |
138 | #define betoh64(x) BE_64(x) | |
139 | #define letoh64(x) LE_64(x) | |
140 | #define htobe16(x) BE_16(x) | |
141 | #define be16toh(x) BE_16(x) | |
142 | #define htobe32(x) BE_32(x) | |
143 | #define be32toh(x) BE_32(x) | |
144 | #define htobe64(x) BE_64(x) | |
145 | #define be64toh(x) BE_64(x) | |
146 | ||
50ec4d1a MJ |
147 | #elif defined(__APPLE__) |
148 | # include <machine/endian.h> | |
149 | # include <libkern/OSByteOrder.h> | |
150 | ||
151 | # if BYTE_ORDER == LITTLE_ENDIAN | |
152 | # define htobe16(x) OSSwapConstInt16(x) | |
153 | # define htole16(x) (x) | |
154 | # define be16toh(x) OSSwapConstInt16(x) | |
155 | # define le16toh(x) (x) | |
156 | ||
157 | # define htobe32(x) OSSwapConstInt32(x) | |
158 | # define htole32(x) (x) | |
159 | # define be32toh(x) OSSwapConstInt32(x) | |
160 | # define le32toh(x) (x) | |
161 | ||
162 | # define htobe64(x) OSSwapConstInt64(x) | |
163 | # define htole64(x) (x) | |
164 | # define be64toh(x) OSSwapConstInt64(x) | |
165 | # define le64toh(x) (x) | |
166 | ||
167 | # else /* BYTE_ORDER == LITTLE_ENDIAN */ | |
168 | # define htobe16(x) (x) | |
169 | # define htole16(x) OSSwapConstInt16(x) | |
170 | # define be16toh(x) (x) | |
171 | # define le16toh(x) OSSwapConstInt16(x) | |
172 | ||
173 | # define htobe32(x) (x) | |
174 | # define htole32(x) OSSwapConstInt32(x) | |
175 | # define be32toh(x) (x) | |
176 | # define le32toh(x) OSSwapConstInt32(x) | |
177 | ||
178 | # define htobe64(x) (x) | |
179 | # define htole64(x) OSSwapConstInt64(x) | |
180 | # define be64toh(x) (x) | |
181 | # define le64toh(x) OSSwapConstInt64(x) | |
182 | # endif | |
183 | ||
eb71a0aa | 184 | #else |
b2c3836f | 185 | #error "Please add support for your OS." |
eb71a0aa DG |
186 | #endif |
187 | ||
188 | #endif /* _COMPAT_ENDIAN_H */ |