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