aboutsummaryrefslogtreecommitdiff
path: root/src/transport/wlan/byteorder.h
blob: 9e16fd70ccb5335a8e060f5395969fe17330f04f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 *  Compatibility header
 *
 *  Copyright (C) 2009 Thomas d'Otreppe
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef _AIRCRACK_NG_BYTEORDER_H_
#define _AIRCRACK_NG_BYTEORDER_H_

	#define ___my_swab16(x) \
	((u_int16_t)( \
			(((u_int16_t)(x) & (u_int16_t)0x00ffU) << 8) | \
			(((u_int16_t)(x) & (u_int16_t)0xff00U) >> 8) ))
	#define ___my_swab32(x) \
	((u_int32_t)( \
			(((u_int32_t)(x) & (u_int32_t)0x000000ffUL) << 24) | \
			(((u_int32_t)(x) & (u_int32_t)0x0000ff00UL) <<  8) | \
			(((u_int32_t)(x) & (u_int32_t)0x00ff0000UL) >>  8) | \
			(((u_int32_t)(x) & (u_int32_t)0xff000000UL) >> 24) ))
	#define ___my_swab64(x) \
	((u_int64_t)( \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x00000000000000ffULL) << 56) | \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x000000000000ff00ULL) << 40) | \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x0000000000ff0000ULL) << 24) | \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x00000000ff000000ULL) <<  8) | \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x000000ff00000000ULL) >>  8) | \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x0000ff0000000000ULL) >> 24) | \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0x00ff000000000000ULL) >> 40) | \
			(u_int64_t)(((u_int64_t)(x) & (u_int64_t)0xff00000000000000ULL) >> 56) ))


	/*
	 * Linux
	 */
	#if defined(linux) || defined(Linux) || defined(__linux__) || defined(__linux) || defined(__gnu_linux__)
		#include <endian.h>
		#include <unistd.h>
		#include <stdint.h>

		#ifndef __int8_t_defined
			typedef uint64_t u_int64_t;
			typedef uint32_t u_int32_t;
			typedef uint16_t u_int16_t;
			typedef uint8_t  u_int8_t;
		#endif

	#endif

	/*
	 * Cygwin
	 */
	#if defined(__CYGWIN32__)
		#include <asm/byteorder.h>
		#include <unistd.h>

		#define __be64_to_cpu(x) ___my_swab64(x)
		#define __be32_to_cpu(x) ___my_swab32(x)
		#define __be16_to_cpu(x) ___my_swab16(x)
		#define __cpu_to_be64(x) ___my_swab64(x)
		#define __cpu_to_be32(x) ___my_swab32(x)
		#define __cpu_to_be16(x) ___my_swab16(x)
		#define __le64_to_cpu(x) (x)
		#define __le32_to_cpu(x) (x)
		#define __le16_to_cpu(x) (x)
		#define __cpu_to_le64(x) (x)
		#define __cpu_to_le32(x) (x)
		#define __cpu_to_le16(x) (x)

		#define AIRCRACK_NG_BYTE_ORDER_DEFINED

	#endif

	/*
	 * Windows (DDK)
	 */
	#if defined(__WIN__)

		#include <io.h>

		#define __be64_to_cpu(x) ___my_swab64(x)
		#define __be32_to_cpu(x) ___my_swab32(x)
		#define __be16_to_cpu(x) ___my_swab16(x)
		#define __cpu_to_be64(x) ___my_swab64(x)
		#define __cpu_to_be32(x) ___my_swab32(x)
		#define __cpu_to_be16(x) ___my_swab16(x)
		#define __le64_to_cpu(x) (x)
		#define __le32_to_cpu(x) (x)
		#define __le16_to_cpu(x) (x)
		#define __cpu_to_le64(x) (x)
		#define __cpu_to_le32(x) (x)
		#define __cpu_to_le16(x) (x)

		#define AIRCRACK_NG_BYTE_ORDER_DEFINED

	#endif

	/*
	 * MAC (Darwin)
	 */
	#if defined(__APPLE_CC__)
		#if defined(__x86_64__) && defined(__APPLE__)

			#include <libkern/OSByteOrder.h>

			#define __swab64(x)      (unsigned long long) OSSwapInt64((uint64_t)x)
			#define __swab32(x)      (unsigned long) OSSwapInt32((uint32_t)x)
			#define __swab16(x)      (unsigned short) OSSwapInt16((uint16_t)x)
			#define __be64_to_cpu(x) (unsigned long long) OSSwapBigToHostInt64((uint64_t)x)
			#define __be32_to_cpu(x) (unsigned long) OSSwapBigToHostInt32((uint32_t)x)
			#define __be16_to_cpu(x) (unsigned short) OSSwapBigToHostInt16((uint16_t)x)
			#define __le64_to_cpu(x) (unsigned long long) OSSwapLittleToHostInt64((uint64_t)x)
			#define __le32_to_cpu(x) (unsigned long) OSSwapLittleToHostInt32((uint32_t)x)
			#define __le16_to_cpu(x) (unsigned short) OSSwapLittleToHostInt16((uint16_t)x)
			#define __cpu_to_be64(x) (unsigned long long) OSSwapHostToBigInt64((uint64_t)x)
			#define __cpu_to_be32(x) (unsigned long) OSSwapHostToBigInt32((uint32_t)x)
			#define __cpu_to_be16(x) (unsigned short) OSSwapHostToBigInt16((uint16_t)x)
			#define __cpu_to_le64(x) (unsigned long long) OSSwapHostToLittleInt64((uint64_t)x)
			#define __cpu_to_le32(x) (unsigned long) OSSwapHostToLittleInt32((uint32_t)x)
			#define __cpu_to_le16(x) (unsigned short) OSSwapHostToLittleInt16((uint16_t)x)

		#else

			#include <architecture/byte_order.h>

			#define __swab64(x)      NXSwapLongLong(x)
			#define __swab32(x)      NXSwapLong(x)
			#define __swab16(x)      NXSwapShort(x)
			#define __be64_to_cpu(x) NXSwapBigLongLongToHost(x)
			#define __be32_to_cpu(x) NXSwapBigLongToHost(x)
			#define __be16_to_cpu(x) NXSwapBigShortToHost(x)
			#define __le64_to_cpu(x) NXSwapLittleLongLongToHost(x)
			#define __le32_to_cpu(x) NXSwapLittleLongToHost(x)
			#define __le16_to_cpu(x) NXSwapLittleShortToHost(x)
			#define __cpu_to_be64(x) NXSwapHostLongLongToBig(x)
			#define __cpu_to_be32(x) NXSwapHostLongToBig(x)
			#define __cpu_to_be16(x) NXSwapHostShortToBig(x)
			#define __cpu_to_le64(x) NXSwapHostLongLongToLittle(x)
			#define __cpu_to_le32(x) NXSwapHostLongToLittle(x)
			#define __cpu_to_le16(x) NXSwapHostShortToLittle(x)

		#endif

		#define __LITTLE_ENDIAN 1234
		#define __BIG_ENDIAN    4321
		#define __PDP_ENDIAN    3412
		#define __BYTE_ORDER    __BIG_ENDIAN

		#define AIRCRACK_NG_BYTE_ORDER_DEFINED

	#endif

	/*
	 * Solaris
	 * -------
	 */
	#if defined(__sparc__) && defined(__sun__)
	#include <sys/byteorder.h>
	#include <sys/types.h>
	#include <unistd.h>

		#define __be64_to_cpu(x) (x)
		#define __be32_to_cpu(x) (x)
		#define __be16_to_cpu(x) (x)
		#define __cpu_to_be64(x) (x)
		#define __cpu_to_be32(x) (x)
		#define __cpu_to_be16(x) (x)
		#define __le64_to_cpu(x) ___my_swab64(x)
		#define __le32_to_cpu(x) ___my_swab32(x)
		#define __le16_to_cpu(x) ___my_swab16(x)
		#define __cpu_to_le64(x) ___my_swab64(x)
		#define __cpu_to_le32(x) ___my_swab32(x)
		#define __cpu_to_le16(x) ___my_swab16(x)

		typedef uint64_t u_int64_t;
		typedef uint32_t u_int32_t;
		typedef uint16_t u_int16_t;
		typedef uint8_t  u_int8_t;

		#define AIRCRACK_NG_BYTE_ORDER_DEFINED

	#endif

	/*
	 * Custom stuff
	 */
	#if  defined(__MACH__) && !defined(__APPLE_CC__)
		#include <libkern/OSByteOrder.h>
		#define __cpu_to_be64(x) = OSSwapHostToBigInt64(x)
		#define __cpu_to_be32(x) = OSSwapHostToBigInt32(x)

		#define AIRCRACK_NG_BYTE_ORDER_DEFINED

	#endif


	// FreeBSD
	#ifdef __FreeBSD__
		#include <machine/endian.h>
	#endif

	// XXX: Is there anything to include on OpenBSD/NetBSD/DragonFlyBSD/...?


	// XXX: Mac: Check http://www.opensource.apple.com/source/CF/CF-476.18/CFByteOrder.h
	//           http://developer.apple.com/DOCUMENTATION/CoreFoundation/Reference/CFByteOrderUtils/Reference/reference.html
	//           Write to apple to ask what should be used.

	#if defined(LITTLE_ENDIAN)
		#define AIRCRACK_NG_LITTLE_ENDIAN LITTLE_ENDIAN
	#elif defined(__LITTLE_ENDIAN)
		#define AIRCRACK_NG_LITTLE_ENDIAN __LITTLE_ENDIAN
	#elif defined(_LITTLE_ENDIAN)
		#define AIRCRACK_NG_LITTLE_ENDIAN _LITTLE_ENDIAN
	#endif

	#if defined(BIG_ENDIAN)
		#define AIRCRACK_NG_BIG_ENDIAN BIG_ENDIAN
	#elif defined(__BIG_ENDIAN)
		#define AIRCRACK_NG_BIG_ENDIAN __BIG_ENDIAN
	#elif defined(_BIG_ENDIAN)
		#define AIRCRACK_NG_BIG_ENDIAN _BIG_ENDIAN
	#endif

	#if !defined(AIRCRACK_NG_LITTLE_ENDIAN) && !defined(AIRCRACK_NG_BIG_ENDIAN)
		#error Impossible to determine endianness (Little or Big endian), please contact the author.
	#endif

	#if defined(BYTE_ORDER)
		#if (BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
			#define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
		#elif (BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
			#define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
		#endif
	#elif defined(__BYTE_ORDER)
		#if (__BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
			#define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
		#elif (__BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
			#define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
		#endif
	#elif defined(_BYTE_ORDER)
		#if (_BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)
			#define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_LITTLE_ENDIAN
		#elif (_BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)
			#define AIRCRACK_NG_BYTE_ORDER AIRCRACK_NG_BIG_ENDIAN
		#endif
	#endif

	#ifndef AIRCRACK_NG_BYTE_ORDER
		#error Impossible to determine endianness (Little or Big endian), please contact the author.
	#endif

	#if (AIRCRACK_NG_BYTE_ORDER == AIRCRACK_NG_LITTLE_ENDIAN)

		#ifndef AIRCRACK_NG_BYTE_ORDER_DEFINED
			#define __be64_to_cpu(x) ___my_swab64(x)
			#define __be32_to_cpu(x) ___my_swab32(x)
			#define __be16_to_cpu(x) ___my_swab16(x)
			#define __cpu_to_be64(x) ___my_swab64(x)
			#define __cpu_to_be32(x) ___my_swab32(x)
			#define __cpu_to_be16(x) ___my_swab16(x)
			#define __le64_to_cpu(x) (x)
			#define __le32_to_cpu(x) (x)
			#define __le16_to_cpu(x) (x)
			#define __cpu_to_le64(x) (x)
			#define __cpu_to_le32(x) (x)
			#define __cpu_to_le16(x) (x)
		#endif

		#ifndef htobe16
			#define htobe16 ___my_swab16
		#endif
		#ifndef htobe32
			#define htobe32 ___my_swab32
		#endif
		#ifndef betoh16
			#define betoh16 ___my_swab16
		#endif
		#ifndef betoh32
			#define betoh32 ___my_swab32
		#endif

		#ifndef htole16
			#define htole16(x) (x)
		#endif
		#ifndef htole32
			#define htole32(x) (x)
		#endif
		#ifndef letoh16
			#define letoh16(x) (x)
		#endif
		#ifndef letoh32
			#define letoh32(x) (x)
		#endif

	#endif

	#if (AIRCRACK_NG_BYTE_ORDER == AIRCRACK_NG_BIG_ENDIAN)

		#ifndef AIRCRACK_NG_BYTE_ORDER_DEFINED
			#define __be64_to_cpu(x) (x)
			#define __be32_to_cpu(x) (x)
			#define __be16_to_cpu(x) (x)
			#define __cpu_to_be64(x) (x)
			#define __cpu_to_be32(x) (x)
			#define __cpu_to_be16(x) (x)
			#define __le64_to_cpu(x) ___my_swab64(x)
			#define __le32_to_cpu(x) ___my_swab32(x)
			#define __le16_to_cpu(x) ___my_swab16(x)
			#define __cpu_to_le64(x) ___my_swab64(x)
			#define __cpu_to_le32(x) ___my_swab32(x)
			#define __cpu_to_le16(x) ___my_swab16(x)
		#endif

		#ifndef htobe16
			#define htobe16(x) (x)
		#endif
		#ifndef htobe32
			#define htobe32(x) (x)
		#endif
		#ifndef betoh16
			#define betoh16(x) (x)
		#endif
		#ifndef betoh32
			#define betoh32(x) (x)
		#endif

		#ifndef htole16
			#define htole16 ___my_swab16
		#endif
		#ifndef htole32
			#define htole32 ___my_swab32
		#endif
		#ifndef letoh16
			#define letoh16 ___my_swab16
		#endif
		#ifndef letoh32
			#define letoh32 ___my_swab32
		#endif

	#endif

	// Common defines
	#define cpu_to_le64 __cpu_to_le64
	#define le64_to_cpu __le64_to_cpu
	#define cpu_to_le32 __cpu_to_le32
	#define le32_to_cpu __le32_to_cpu
	#define cpu_to_le16 __cpu_to_le16
	#define le16_to_cpu __le16_to_cpu
	#define cpu_to_be64 __cpu_to_be64
	#define be64_to_cpu __be64_to_cpu
	#define cpu_to_be32 __cpu_to_be32
	#define be32_to_cpu __be32_to_cpu
	#define cpu_to_be16 __cpu_to_be16
	#define be16_to_cpu __be16_to_cpu

	#ifndef le16toh
		#define le16toh le16_to_cpu
	#endif
	#ifndef be16toh
		#define be16toh be16_to_cpu
	#endif
	#ifndef le32toh
		#define le32toh le32_to_cpu
	#endif
	#ifndef be32toh
		#define be32toh be32_to_cpu
	#endif


	#ifndef htons
		#define htons be16_to_cpu
	#endif
	#ifndef htonl
		#define htonl cpu_to_be16
	#endif
	#ifndef ntohs
		#define ntohs cpu_to_be16
	#endif
	#ifndef ntohl
		#define ntohl cpu_to_be32
	#endif

#endif