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