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