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