indent corrupted the file
[oweals/gnunet.git] / src / transport / wlan / byteorder.h
1 /*
2  *  Compatibility header
3  *
4  *  Copyright (C) 2009 Thomas d'Otreppe
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifndef _AIRCRACK_NG_BYTEORDER_H_
22 #define _AIRCRACK_NG_BYTEORDER_H_
23
24 #define ___my_swab16(x) \
25 ((u_int16_t)( \
26   (((u_int16_t)(x) & (u_int16_t)0x00ffU) << 8) | \
27   (((u_int16_t)(x) & (u_int16_t)0xff00U) >> 8) ))
28
29 #define ___my_swab32(x) \
30 ((u_int32_t)( \
31   (((u_int32_t)(x) & (u_int32_t)0x000000ffUL) << 24) | \
32   (((u_int32_t)(x) & (u_int32_t)0x0000ff00UL) <<  8) | \
33   (((u_int32_t)(x) & (u_int32_t)0x00ff0000UL) >>  8) | \
34   (((u_int32_t)(x) & (u_int32_t)0xff000000UL) >> 24) ))
35 #define ___my_swab64(x) \
36 ((u_int64_t)( \
37   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x00000000000000ffULL) << 56) | \
38   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x000000000000ff00ULL) << 40) | \
39   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x0000000000ff0000ULL) << 24) | \
40   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x00000000ff000000ULL) <<  8) | \
41   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x000000ff00000000ULL) >>  8) | \
42   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x0000ff0000000000ULL) >> 24) | \
43   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x00ff000000000000ULL) >> 40) | \
44   (u_int64_t)(((u_int64_t)(x) & (u_int64_t)0xff00000000000000ULL) >> 56) ))
45
46     /*
47      * Linux
48      */
49 #if defined(linux) || defined(Linux) || defined(__linux__) || defined(__linux) || defined(__gnu_linux__)
50 #include <endian.h>
51 #include <unistd.h>
52 #include <stdint.h>
53
54 #ifndef __int8_t_defined
55 typedef uint64_t u_int64_t;
56 \rtypedef uint32_t u_int32_t;
57 \rtypedef uint16_t u_int16_t;
58 \rtypedef uint8_t u_int8_t;
59
60
61 #endif /* \r */
62
63 #ifndef htole16
64 #if __BYTE_ORDER == __LITTLE_ENDIAN
65 #define htobe16(x) ___my_swab16 (x)
66 #define htole16(x) (x)
67 #define be16toh(x) ___my_swab16 (x)
68 #define le16toh(x) (x)
69
70 #define htobe32(x) ___my_swab32 (x)
71 #define htole32(x) (x)
72 #define be32toh(x) ___my_swab32 (x)
73 #define le32toh(x) (x)
74
75 #define htobe64(x) ___my_swab64 (x)
76 #define htole64(x) (x)
77 #define be64toh(x) ___my_swab64 (x)
78 #define le64toh(x) (x)
79 #else /* \r */
80 #define htobe16(x) (x)
81 #define htole16(x) ___my_swab16 (x)
82 #define be16toh(x) (x)
83 #define le16toh(x) ___my_swab16 (x)
84
85 #define htobe32(x) (x)
86 #define htole32(x) ___my_swab32 (x)
87 #define be32toh(x) (x)
88 #define le32toh(x) ___my_swab32 (x)
89
90 #define htobe64(x) (x)
91 #define htole64(x) ___my_swab64 (x)
92 #define be64toh(x) (x)
93 #define le64toh(x) ___my_swab64 (x)
94 #endif /* \r */
95 #endif /* \r */
96
97 #endif /* \r */
98     /*
99      * Cygwin
100      */
101 #if defined(__CYGWIN32__)
102 #include <asm/byteorder.h>
103 #include <unistd.h>
104 #define __be64_to_cpu(x) ___my_swab64(x)
105 #define __be32_to_cpu(x) ___my_swab32(x)
106 #define __be16_to_cpu(x) ___my_swab16(x)
107 #define __cpu_to_be64(x) ___my_swab64(x)
108 #define __cpu_to_be32(x) ___my_swab32(x)
109 #define __cpu_to_be16(x) ___my_swab16(x)
110 #define __le64_to_cpu(x) (x)
111 #define __le32_to_cpu(x) (x)
112 #define __le16_to_cpu(x) (x)
113 #define __cpu_to_le64(x) (x)
114 #define __cpu_to_le32(x) (x)
115 #define __cpu_to_le16(x) (x)
116 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
117 #endif /* \r */
118     /*
119      * Windows (DDK)
120      */
121 #if defined(__WIN__)
122 #include <io.h>
123 #define __be64_to_cpu(x) ___my_swab64(x)
124 #define __be32_to_cpu(x) ___my_swab32(x)
125 #define __be16_to_cpu(x) ___my_swab16(x)
126 #define __cpu_to_be64(x) ___my_swab64(x)
127 #define __cpu_to_be32(x) ___my_swab32(x)
128 #define __cpu_to_be16(x) ___my_swab16(x)
129 #define __le64_to_cpu(x) (x)
130 #define __le32_to_cpu(x) (x)
131 #define __le16_to_cpu(x) (x)
132 #define __cpu_to_le64(x) (x)
133 #define __cpu_to_le32(x) (x)
134 #define __cpu_to_le16(x) (x)
135 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
136 #endif /* \r */
137     /*
138      * MAC (Darwin)
139      */
140 #if defined(__APPLE_CC__)
141 #if defined(__x86_64__) && defined(__APPLE__)
142 #include <libkern/OSByteOrder.h>
143 #define __swab64(x)      (unsigned long long) OSSwapInt64((uint64_t)x)
144 #define __swab32(x)      (unsigned long) OSSwapInt32((uint32_t)x)
145 #define __swab16(x)      (unsigned short) OSSwapInt16((uint16_t)x)
146 #define __be64_to_cpu(x) (unsigned long long) OSSwapBigToHostInt64((uint64_t)x)
147 #define __be32_to_cpu(x) (unsigned long) OSSwapBigToHostInt32((uint32_t)x)
148 #define __be16_to_cpu(x) (unsigned short) OSSwapBigToHostInt16((uint16_t)x)
149 #define __le64_to_cpu(x) (unsigned long long) OSSwapLittleToHostInt64((uint64_t)x)
150 #define __le32_to_cpu(x) (unsigned long) OSSwapLittleToHostInt32((uint32_t)x)
151 #define __le16_to_cpu(x) (unsigned short) OSSwapLittleToHostInt16((uint16_t)x)
152 #define __cpu_to_be64(x) (unsigned long long) OSSwapHostToBigInt64((uint64_t)x)
153 #define __cpu_to_be32(x) (unsigned long) OSSwapHostToBigInt32((uint32_t)x)
154 #define __cpu_to_be16(x) (unsigned short) OSSwapHostToBigInt16((uint16_t)x)
155 #define __cpu_to_le64(x) (unsigned long long) OSSwapHostToLittleInt64((uint64_t)x)
156 #define __cpu_to_le32(x) (unsigned long) OSSwapHostToLittleInt32((uint32_t)x)
157 #define __cpu_to_le16(x) (unsigned short) OSSwapHostToLittleInt16((uint16_t)x)
158 #else /* \r */
159 #include <architecture/byte_order.h>
160 #define __swab64(x)      NXSwapLongLong(x)
161 #define __swab32(x)      NXSwapLong(x)
162 #define __swab16(x)      NXSwapShort(x)
163 #define __be64_to_cpu(x) NXSwapBigLongLongToHost(x)
164 #define __be32_to_cpu(x) NXSwapBigLongToHost(x)
165 #define __be16_to_cpu(x) NXSwapBigShortToHost(x)
166 #define __le64_to_cpu(x) NXSwapLittleLongLongToHost(x)
167 #define __le32_to_cpu(x) NXSwapLittleLongToHost(x)
168 #define __le16_to_cpu(x) NXSwapLittleShortToHost(x)
169 #define __cpu_to_be64(x) NXSwapHostLongLongToBig(x)
170 #define __cpu_to_be32(x) NXSwapHostLongToBig(x)
171 #define __cpu_to_be16(x) NXSwapHostShortToBig(x)
172 #define __cpu_to_le64(x) NXSwapHostLongLongToLittle(x)
173 #define __cpu_to_le32(x) NXSwapHostLongToLittle(x)
174 #define __cpu_to_le16(x) NXSwapHostShortToLittle(x)
175 #endif /* \r */
176 #define __LITTLE_ENDIAN 1234
177 #define __BIG_ENDIAN    4321
178 #define __PDP_ENDIAN    3412
179 #define __BYTE_ORDER    __BIG_ENDIAN
180 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
181 #endif /* \r */
182     /*
183      * Solaris
184      * -------
185      */
186 #if defined(__sparc__) && defined(__sun__)
187 #include <sys/byteorder.h>
188 #include <sys/types.h>
189 #include <unistd.h>
190 #define __be64_to_cpu(x) (x)
191 #define __be32_to_cpu(x) (x)
192 #define __be16_to_cpu(x) (x)
193 #define __cpu_to_be64(x) (x)
194 #define __cpu_to_be32(x) (x)
195 #define __cpu_to_be16(x) (x)
196 #define __le64_to_cpu(x) ___my_swab64(x)
197 #define __le32_to_cpu(x) ___my_swab32(x)
198 #define __le16_to_cpu(x) ___my_swab16(x)
199 #define __cpu_to_le64(x) ___my_swab64(x)
200 #define __cpu_to_le32(x) ___my_swab32(x)
201 #define __cpu_to_le16(x) ___my_swab16(x)
202 \rtypedef uint64_t u_int64_t;
203 \rtypedef uint32_t u_int32_t;
204 \rtypedef uint16_t u_int16_t;
205 \rtypedef uint8_t u_int8_t;
206
207
208 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
209 #endif /* \r */
210     /*
211      * Custom stuff
212      */
213 #if  defined(__MACH__) && !defined(__APPLE_CC__)
214 #include <libkern/OSByteOrder.h>
215 #define __cpu_to_be64(x) = OSSwapHostToBigInt64(x)
216 #define __cpu_to_be32(x) = OSSwapHostToBigInt32(x)
217 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
218 #endif /* \r */
219
220     // FreeBSD
221 #ifdef __FreeBSD__
222 #include <machine/endian.h>
223 #endif /* \r */
224     // XXX: Is there anything to include on OpenBSD/NetBSD/DragonFlyBSD/...?
225
226     // XXX: Mac: Check http://www.opensource.apple.com/source/CF/CF-476.18/CFByteOrder.h
227     //           http://developer.apple.com/DOCUMENTATION/CoreFoundation/Reference/CFByteOrderUtils/Reference/reference.html
228     //           Write to apple to ask what should be used.
229 #if defined(LITTLE_ENDIAN)
230 #define AIRCRACK_NG_LITTLE_ENDIAN LITTLE_ENDIAN
231 #elif defined(__LITTLE_ENDIAN)
232 #define AIRCRACK_NG_LITTLE_ENDIAN __LITTLE_ENDIAN
233 #elif defined(_LITTLE_ENDIAN)
234 #define AIRCRACK_NG_LITTLE_ENDIAN _LITTLE_ENDIAN
235 #endif /* \r */
236 #if defined(BIG_ENDIAN)
237 #define AIRCRACK_NG_BIG_ENDIAN BIG_ENDIAN
238 #elif defined(__BIG_ENDIAN)
239 #define AIRCRACK_NG_BIG_ENDIAN __BIG_ENDIAN
240 #elif defined(_BIG_ENDIAN)
241 #define AIRCRACK_NG_BIG_ENDIAN _BIG_ENDIAN
242 #endif /* \r */
243 #if !defined(AIRCRACK_NG_LITTLE_ENDIAN) && !defined(AIRCRACK_NG_BIG_ENDIAN)
244 #error Impossible to determine endianness (Little or Big endian), please contact the author.
245 #endif /* \r */
246 #if defined(BYTE_ORDER)
247 #if (BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
248 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
249 #elif (BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
250 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
251 #endif /* \r */
252 #elif defined(__BYTE_ORDER)
253 #if (__BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
254 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
255 #elif (__BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
256 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
257 #endif /* \r */
258 #elif defined(_BYTE_ORDER)
259 #if (_BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
260 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
261 #elif (_BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
262 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
263 #endif /* \r */
264 #endif /* \r */
265 #ifndef AIRCRACK_NG_BYTE_ORDER
266 #error Impossible to determine endianness (Little or Big endian), please contact the author.
267 #endif /* \r */
268 #if (AIRCRACK_NG_BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
269 #ifndef AIRCRACK_NG_BYTE_ORDER_DEFINED
270 #define __be64_to_cpu(x) ___my_swab64(x)
271 #define __be32_to_cpu(x) ___my_swab32(x)
272 #define __be16_to_cpu(x) ___my_swab16(x)
273 #define __cpu_to_be64(x) ___my_swab64(x)
274 #define __cpu_to_be32(x) ___my_swab32(x)
275 #define __cpu_to_be16(x) ___my_swab16(x)
276 #define __le64_to_cpu(x) (x)
277 #define __le32_to_cpu(x) (x)
278 #define __le16_to_cpu(x) (x)
279 #define __cpu_to_le64(x) (x)
280 #define __cpu_to_le32(x) (x)
281 #define __cpu_to_le16(x) (x)
282 #endif /* \r */
283 #ifndef htobe16
284 #define htobe16 ___my_swab16
285 #endif /* \r */
286 #ifndef htobe32
287 #define htobe32 ___my_swab32
288 #endif /* \r */
289 #ifndef betoh16
290 #define betoh16 ___my_swab16
291 #endif /* \r */
292 #ifndef betoh32
293 #define betoh32 ___my_swab32
294 #endif /* \r */
295 #ifndef htole16
296 #define htole16(x) (x)
297 #endif /* \r */
298 #ifndef htole32
299 #define htole32(x) (x)
300 #endif /* \r */
301 #ifndef letoh16
302 #define letoh16(x) (x)
303 #endif /* \r */
304 #ifndef letoh32
305 #define letoh32(x) (x)
306 #endif /* \r */
307 #endif /* \r */
308 #if (AIRCRACK_NG_BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
309 #ifndef AIRCRACK_NG_BYTE_ORDER_DEFINED
310 #define __be64_to_cpu(x) (x)
311 #define __be32_to_cpu(x) (x)
312 #define __be16_to_cpu(x) (x)
313 #define __cpu_to_be64(x) (x)
314 #define __cpu_to_be32(x) (x)
315 #define __cpu_to_be16(x) (x)
316 #define __le64_to_cpu(x) ___my_swab64(x)
317 #define __le32_to_cpu(x) ___my_swab32(x)
318 #define __le16_to_cpu(x) ___my_swab16(x)
319 #define __cpu_to_le64(x) ___my_swab64(x)
320 #define __cpu_to_le32(x) ___my_swab32(x)
321 #define __cpu_to_le16(x) ___my_swab16(x)
322 #endif /* \r */
323 #ifndef htobe16
324 #define htobe16(x) (x)
325 #endif /* \r */
326 #ifndef htobe32
327 #define htobe32(x) (x)
328 #endif /* \r */
329 #ifndef betoh16
330 #define betoh16(x) (x)
331 #endif /* \r */
332 #ifndef betoh32
333 #define betoh32(x) (x)
334 #endif /* \r */
335 #ifndef htole16
336 #define htole16 ___my_swab16
337 #endif /* \r */
338 #ifndef htole32
339 #define htole32 ___my_swab32
340 #endif /* \r */
341 #ifndef letoh16
342 #define letoh16 ___my_swab16
343 #endif /* \r */
344 #ifndef letoh32
345 #define letoh32 ___my_swab32
346 #endif /* \r */
347 #endif /* \r */
348     // Common defines
349 #define cpu_to_le64 __cpu_to_le64
350 #define le64_to_cpu __le64_to_cpu
351 #define cpu_to_le32 __cpu_to_le32
352 #define le32_to_cpu __le32_to_cpu
353 #define cpu_to_le16 __cpu_to_le16
354 #define le16_to_cpu __le16_to_cpu
355 #define cpu_to_be64 __cpu_to_be64
356 #define be64_to_cpu __be64_to_cpu
357 #define cpu_to_be32 __cpu_to_be32
358 #define be32_to_cpu __be32_to_cpu
359 #define cpu_to_be16 __cpu_to_be16
360 #define be16_to_cpu __be16_to_cpu
361 #ifndef le16toh
362 #define le16toh le16_to_cpu
363 #endif /* \r */
364 #ifndef be16toh
365 #define be16toh be16_to_cpu
366 #endif /* \r */
367 #ifndef le32toh
368 #define le32toh le32_to_cpu
369 #endif /* \r */
370 #ifndef be32toh
371 #define be32toh be32_to_cpu
372 #endif /* \r */
373
374 #ifndef htons
375 #define htons be16_to_cpu
376 #endif /* \r */
377 #ifndef htonl
378 #define htonl cpu_to_be16
379 #endif /* \r */
380 #ifndef ntohs
381 #define ntohs cpu_to_be16
382 #endif /* \r */
383 #ifndef ntohl
384 #define ntohl cpu_to_be32
385 #endif /* \r */
386 #endif /* \r */