even nicer indentation, thanks to LRN's indent patch
[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      (\\r(((u_int16_t) (x) & (u_int16_t) 0x00ffU) << 8) |
27       \\r(((u_int16_t) (x) & (u_int16_t) 0xff00U) >> 8))) 
28 #define ___my_swab32(x) \
29     ((u_int32_t)
30      (\\r(((u_int32_t) (x) & (u_int32_t) 0x000000ffUL) << 24) |
31       \\r(((u_int32_t) (x) & (u_int32_t) 0x0000ff00UL) << 8) |
32       \\r(((u_int32_t) (x) & (u_int32_t) 0x00ff0000UL) >> 8) |
33       \\r(((u_int32_t) (x) & (u_int32_t) 0xff000000UL) >> 24))) 
34 #define ___my_swab64(x) \
35     ((u_int64_t)
36      (\\r(u_int64_t)
37       (((u_int64_t) (x) & (u_int64_t) 0x00000000000000ffULL) << 56) |
38       \\r(u_int64_t) (((u_int64_t) (x) & (u_int64_t) 0x000000000000ff00ULL) <<
39                      40) | \\r(u_int64_t) (((u_int64_t) (x) & (u_int64_t)
40                                            0x0000000000ff0000ULL) << 24) |
41       \\r(u_int64_t) (((u_int64_t) (x) & (u_int64_t) 0x00000000ff000000ULL) << 8)
42       | \\r(u_int64_t) (((u_int64_t) (x) & (u_int64_t) 0x000000ff00000000ULL) >>
43                        8) | \\r(u_int64_t) (((u_int64_t) (x) & (u_int64_t)
44                                             0x0000ff0000000000ULL) >> 24) |
45       \\r(u_int64_t) (((u_int64_t) (x) & (u_int64_t) 0x00ff000000000000ULL) >>
46                      40) | \\r(u_int64_t) (((u_int64_t) (x) & (u_int64_t)
47                                            0xff00000000000000ULL) >> 56))) \r \r 
48     /*
49      * Linux
50      */ 
51 #if defined(linux) || defined(Linux) || defined(__linux__) || defined(__linux) || defined(__gnu_linux__)
52 #include <endian.h>
53 #include <unistd.h>
54 #include <stdint.h>
55     
56 #ifndef __int8_t_defined
57 typedef uint64_t u_int64_t;
58 \rtypedef uint32_t u_int32_t;
59 \rtypedef uint16_t u_int16_t;
60 \rtypedef uint8_t u_int8_t;
61
62
63 #endif /* \r */
64     
65 #ifndef htole16
66 #if __BYTE_ORDER == __LITTLE_ENDIAN
67 #define htobe16(x) ___my_swab16 (x)
68 #define htole16(x) (x)
69 #define be16toh(x) ___my_swab16 (x)
70 #define le16toh(x) (x)
71     
72 #define htobe32(x) ___my_swab32 (x)
73 #define htole32(x) (x)
74 #define be32toh(x) ___my_swab32 (x)
75 #define le32toh(x) (x)
76     
77 #define htobe64(x) ___my_swab64 (x)
78 #define htole64(x) (x)
79 #define be64toh(x) ___my_swab64 (x)
80 #define le64toh(x) (x)
81 #else /* \r */
82 #define htobe16(x) (x)
83 #define htole16(x) ___my_swab16 (x)
84 #define be16toh(x) (x)
85 #define le16toh(x) ___my_swab16 (x)
86     
87 #define htobe32(x) (x)
88 #define htole32(x) ___my_swab32 (x)
89 #define be32toh(x) (x)
90 #define le32toh(x) ___my_swab32 (x)
91     
92 #define htobe64(x) (x)
93 #define htole64(x) ___my_swab64 (x)
94 #define be64toh(x) (x)
95 #define le64toh(x) ___my_swab64 (x)
96 #endif /* \r */
97 #endif /* \r */
98     \r \r 
99 #endif /* \r */
100     
101     /*
102      * Cygwin
103      */ 
104 #if defined(__CYGWIN32__)
105 #include <asm/byteorder.h>
106 #include <unistd.h>
107     
108 #define __be64_to_cpu(x) ___my_swab64(x)
109 #define __be32_to_cpu(x) ___my_swab32(x)
110 #define __be16_to_cpu(x) ___my_swab16(x)
111 #define __cpu_to_be64(x) ___my_swab64(x)
112 #define __cpu_to_be32(x) ___my_swab32(x)
113 #define __cpu_to_be16(x) ___my_swab16(x)
114 #define __le64_to_cpu(x) (x)
115 #define __le32_to_cpu(x) (x)
116 #define __le16_to_cpu(x) (x)
117 #define __cpu_to_le64(x) (x)
118 #define __cpu_to_le32(x) (x)
119 #define __cpu_to_le16(x) (x)
120     
121 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
122     
123 #endif /* \r */
124     
125     /*
126      * Windows (DDK)
127      */ 
128 #if defined(__WIN__)
129     
130 #include <io.h>
131     
132 #define __be64_to_cpu(x) ___my_swab64(x)
133 #define __be32_to_cpu(x) ___my_swab32(x)
134 #define __be16_to_cpu(x) ___my_swab16(x)
135 #define __cpu_to_be64(x) ___my_swab64(x)
136 #define __cpu_to_be32(x) ___my_swab32(x)
137 #define __cpu_to_be16(x) ___my_swab16(x)
138 #define __le64_to_cpu(x) (x)
139 #define __le32_to_cpu(x) (x)
140 #define __le16_to_cpu(x) (x)
141 #define __cpu_to_le64(x) (x)
142 #define __cpu_to_le32(x) (x)
143 #define __cpu_to_le16(x) (x)
144     
145 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
146     
147 #endif /* \r */
148     
149     /*
150      * MAC (Darwin)
151      */ 
152 #if defined(__APPLE_CC__)
153 #if defined(__x86_64__) && defined(__APPLE__)
154     
155 #include <libkern/OSByteOrder.h>
156     
157 #define __swab64(x)      (unsigned long long) OSSwapInt64((uint64_t)x)
158 #define __swab32(x)      (unsigned long) OSSwapInt32((uint32_t)x)
159 #define __swab16(x)      (unsigned short) OSSwapInt16((uint16_t)x)
160 #define __be64_to_cpu(x) (unsigned long long) OSSwapBigToHostInt64((uint64_t)x)
161 #define __be32_to_cpu(x) (unsigned long) OSSwapBigToHostInt32((uint32_t)x)
162 #define __be16_to_cpu(x) (unsigned short) OSSwapBigToHostInt16((uint16_t)x)
163 #define __le64_to_cpu(x) (unsigned long long) OSSwapLittleToHostInt64((uint64_t)x)
164 #define __le32_to_cpu(x) (unsigned long) OSSwapLittleToHostInt32((uint32_t)x)
165 #define __le16_to_cpu(x) (unsigned short) OSSwapLittleToHostInt16((uint16_t)x)
166 #define __cpu_to_be64(x) (unsigned long long) OSSwapHostToBigInt64((uint64_t)x)
167 #define __cpu_to_be32(x) (unsigned long) OSSwapHostToBigInt32((uint32_t)x)
168 #define __cpu_to_be16(x) (unsigned short) OSSwapHostToBigInt16((uint16_t)x)
169 #define __cpu_to_le64(x) (unsigned long long) OSSwapHostToLittleInt64((uint64_t)x)
170 #define __cpu_to_le32(x) (unsigned long) OSSwapHostToLittleInt32((uint32_t)x)
171 #define __cpu_to_le16(x) (unsigned short) OSSwapHostToLittleInt16((uint16_t)x)
172     
173 #else /* \r */
174     
175 #include <architecture/byte_order.h>
176     
177 #define __swab64(x)      NXSwapLongLong(x)
178 #define __swab32(x)      NXSwapLong(x)
179 #define __swab16(x)      NXSwapShort(x)
180 #define __be64_to_cpu(x) NXSwapBigLongLongToHost(x)
181 #define __be32_to_cpu(x) NXSwapBigLongToHost(x)
182 #define __be16_to_cpu(x) NXSwapBigShortToHost(x)
183 #define __le64_to_cpu(x) NXSwapLittleLongLongToHost(x)
184 #define __le32_to_cpu(x) NXSwapLittleLongToHost(x)
185 #define __le16_to_cpu(x) NXSwapLittleShortToHost(x)
186 #define __cpu_to_be64(x) NXSwapHostLongLongToBig(x)
187 #define __cpu_to_be32(x) NXSwapHostLongToBig(x)
188 #define __cpu_to_be16(x) NXSwapHostShortToBig(x)
189 #define __cpu_to_le64(x) NXSwapHostLongLongToLittle(x)
190 #define __cpu_to_le32(x) NXSwapHostLongToLittle(x)
191 #define __cpu_to_le16(x) NXSwapHostShortToLittle(x)
192     
193 #endif /* \r */
194     
195 #define __LITTLE_ENDIAN 1234
196 #define __BIG_ENDIAN    4321
197 #define __PDP_ENDIAN    3412
198 #define __BYTE_ORDER    __BIG_ENDIAN
199     
200 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
201     
202 #endif /* \r */
203     
204     /*
205      * Solaris
206      * -------
207      */ 
208 #if defined(__sparc__) && defined(__sun__)
209 #include <sys/byteorder.h>
210 #include <sys/types.h>
211 #include <unistd.h>
212     
213 #define __be64_to_cpu(x) (x)
214 #define __be32_to_cpu(x) (x)
215 #define __be16_to_cpu(x) (x)
216 #define __cpu_to_be64(x) (x)
217 #define __cpu_to_be32(x) (x)
218 #define __cpu_to_be16(x) (x)
219 #define __le64_to_cpu(x) ___my_swab64(x)
220 #define __le32_to_cpu(x) ___my_swab32(x)
221 #define __le16_to_cpu(x) ___my_swab16(x)
222 #define __cpu_to_le64(x) ___my_swab64(x)
223 #define __cpu_to_le32(x) ___my_swab32(x)
224 #define __cpu_to_le16(x) ___my_swab16(x)
225 \rtypedef uint64_t u_int64_t;
226 \rtypedef uint32_t u_int32_t;
227 \rtypedef uint16_t u_int16_t;
228 \rtypedef uint8_t u_int8_t;
229
230 \r 
231 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
232     
233 #endif /* \r */
234     
235     /*
236      * Custom stuff
237      */ 
238 #if  defined(__MACH__) && !defined(__APPLE_CC__)
239 #include <libkern/OSByteOrder.h>
240 #define __cpu_to_be64(x) = OSSwapHostToBigInt64(x)
241 #define __cpu_to_be32(x) = OSSwapHostToBigInt32(x)
242     
243 #define AIRCRACK_NG_BYTE_ORDER_DEFINED
244     
245 #endif /* \r */
246     \r 
247     // FreeBSD
248 #ifdef __FreeBSD__
249 #include <machine/endian.h>
250 #endif /* \r */
251     
252     // XXX: Is there anything to include on OpenBSD/NetBSD/DragonFlyBSD/...?
253     \r 
254     // XXX: Mac: Check http://www.opensource.apple.com/source/CF/CF-476.18/CFByteOrder.h
255     //           http://developer.apple.com/DOCUMENTATION/CoreFoundation/Reference/CFByteOrderUtils/Reference/reference.html
256     //           Write to apple to ask what should be used.
257     
258 #if defined(LITTLE_ENDIAN)
259 #define AIRCRACK_NG_LITTLE_ENDIAN LITTLE_ENDIAN
260 #elif defined(__LITTLE_ENDIAN)
261 #define AIRCRACK_NG_LITTLE_ENDIAN __LITTLE_ENDIAN
262 #elif defined(_LITTLE_ENDIAN)
263 #define AIRCRACK_NG_LITTLE_ENDIAN _LITTLE_ENDIAN
264 #endif /* \r */
265     
266 #if defined(BIG_ENDIAN)
267 #define AIRCRACK_NG_BIG_ENDIAN BIG_ENDIAN
268 #elif defined(__BIG_ENDIAN)
269 #define AIRCRACK_NG_BIG_ENDIAN __BIG_ENDIAN
270 #elif defined(_BIG_ENDIAN)
271 #define AIRCRACK_NG_BIG_ENDIAN _BIG_ENDIAN
272 #endif /* \r */
273     
274 #if !defined(AIRCRACK_NG_LITTLE_ENDIAN) && !defined(AIRCRACK_NG_BIG_ENDIAN)
275 #error Impossible to determine endianness (Little or Big endian), please contact the author.
276 #endif /* \r */
277     
278 #if defined(BYTE_ORDER)
279 #if (BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
280 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
281 #elif (BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
282 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
283 #endif /* \r */
284 #elif defined(__BYTE_ORDER)
285 #if (__BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
286 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
287 #elif (__BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
288 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
289 #endif /* \r */
290 #elif defined(_BYTE_ORDER)
291 #if (_BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
292 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
293 #elif (_BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
294 #define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
295 #endif /* \r */
296 #endif /* \r */
297     
298 #ifndef AIRCRACK_NG_BYTE_ORDER
299 #error Impossible to determine endianness (Little or Big endian), please contact the author.
300 #endif /* \r */
301     
302 #if (AIRCRACK_NG_BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
303     
304 #ifndef AIRCRACK_NG_BYTE_ORDER_DEFINED
305 #define __be64_to_cpu(x) ___my_swab64(x)
306 #define __be32_to_cpu(x) ___my_swab32(x)
307 #define __be16_to_cpu(x) ___my_swab16(x)
308 #define __cpu_to_be64(x) ___my_swab64(x)
309 #define __cpu_to_be32(x) ___my_swab32(x)
310 #define __cpu_to_be16(x) ___my_swab16(x)
311 #define __le64_to_cpu(x) (x)
312 #define __le32_to_cpu(x) (x)
313 #define __le16_to_cpu(x) (x)
314 #define __cpu_to_le64(x) (x)
315 #define __cpu_to_le32(x) (x)
316 #define __cpu_to_le16(x) (x)
317 #endif /* \r */
318     
319 #ifndef htobe16
320 #define htobe16 ___my_swab16
321 #endif /* \r */
322 #ifndef htobe32
323 #define htobe32 ___my_swab32
324 #endif /* \r */
325 #ifndef betoh16
326 #define betoh16 ___my_swab16
327 #endif /* \r */
328 #ifndef betoh32
329 #define betoh32 ___my_swab32
330 #endif /* \r */
331     
332 #ifndef htole16
333 #define htole16(x) (x)
334 #endif /* \r */
335 #ifndef htole32
336 #define htole32(x) (x)
337 #endif /* \r */
338 #ifndef letoh16
339 #define letoh16(x) (x)
340 #endif /* \r */
341 #ifndef letoh32
342 #define letoh32(x) (x)
343 #endif /* \r */
344     
345 #endif /* \r */
346     
347 #if (AIRCRACK_NG_BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
348     
349 #ifndef AIRCRACK_NG_BYTE_ORDER_DEFINED
350 #define __be64_to_cpu(x) (x)
351 #define __be32_to_cpu(x) (x)
352 #define __be16_to_cpu(x) (x)
353 #define __cpu_to_be64(x) (x)
354 #define __cpu_to_be32(x) (x)
355 #define __cpu_to_be16(x) (x)
356 #define __le64_to_cpu(x) ___my_swab64(x)
357 #define __le32_to_cpu(x) ___my_swab32(x)
358 #define __le16_to_cpu(x) ___my_swab16(x)
359 #define __cpu_to_le64(x) ___my_swab64(x)
360 #define __cpu_to_le32(x) ___my_swab32(x)
361 #define __cpu_to_le16(x) ___my_swab16(x)
362 #endif /* \r */
363     
364 #ifndef htobe16
365 #define htobe16(x) (x)
366 #endif /* \r */
367 #ifndef htobe32
368 #define htobe32(x) (x)
369 #endif /* \r */
370 #ifndef betoh16
371 #define betoh16(x) (x)
372 #endif /* \r */
373 #ifndef betoh32
374 #define betoh32(x) (x)
375 #endif /* \r */
376     
377 #ifndef htole16
378 #define htole16 ___my_swab16
379 #endif /* \r */
380 #ifndef htole32
381 #define htole32 ___my_swab32
382 #endif /* \r */
383 #ifndef letoh16
384 #define letoh16 ___my_swab16
385 #endif /* \r */
386 #ifndef letoh32
387 #define letoh32 ___my_swab32
388 #endif /* \r */
389     
390 #endif /* \r */
391     
392     // Common defines
393 #define cpu_to_le64 __cpu_to_le64
394 #define le64_to_cpu __le64_to_cpu
395 #define cpu_to_le32 __cpu_to_le32
396 #define le32_to_cpu __le32_to_cpu
397 #define cpu_to_le16 __cpu_to_le16
398 #define le16_to_cpu __le16_to_cpu
399 #define cpu_to_be64 __cpu_to_be64
400 #define be64_to_cpu __be64_to_cpu
401 #define cpu_to_be32 __cpu_to_be32
402 #define be32_to_cpu __be32_to_cpu
403 #define cpu_to_be16 __cpu_to_be16
404 #define be16_to_cpu __be16_to_cpu
405     
406 #ifndef le16toh
407 #define le16toh le16_to_cpu
408 #endif /* \r */
409 #ifndef be16toh
410 #define be16toh be16_to_cpu
411 #endif /* \r */
412 #ifndef le32toh
413 #define le32toh le32_to_cpu
414 #endif /* \r */
415 #ifndef be32toh
416 #define be32toh be32_to_cpu
417 #endif /* \r */
418     \r 
419 #ifndef htons
420 #define htons be16_to_cpu
421 #endif /* \r */
422 #ifndef htonl
423 #define htonl cpu_to_be16
424 #endif /* \r */
425 #ifndef ntohs
426 #define ntohs cpu_to_be16
427 #endif /* \r */
428 #ifndef ntohl
429 #define ntohl cpu_to_be32
430 #endif /* \r */
431     
432 #endif /* \r */