textual
[RRG-proxmark3.git] / client / src / proxendian.h
blob8501858e04f087fabfb7e9a4486f0e74b65f9f65
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 Hector Martin "marcan" <marcan@marcansoft.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Endianness convenience functions
9 //-----------------------------------------------------------------------------
11 #ifndef PROXENDIAN_H__
12 #define PROXENDIAN_H__
14 #include "common.h"
16 #ifdef _WIN32
17 # define HOST_LITTLE_ENDIAN
18 #else
19 // Only some OSes include endian.h from sys/types.h, not Termux, so let's include endian.h directly
20 # if defined(__APPLE__)
21 # include <machine/endian.h>
22 # else
23 # include <endian.h>
24 # endif
25 # if !defined(BYTE_ORDER)
26 # if !defined(__BYTE_ORDER) || (__BYTE_ORDER != __LITTLE_ENDIAN && __BYTE_ORDER != __BIG_ENDIAN)
27 # error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
28 # endif
29 # if __BYTE_ORDER == __LITTLE_ENDIAN
30 # define HOST_LITTLE_ENDIAN
31 # endif
32 # else
33 # if BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN
34 # error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
35 # endif
36 # if BYTE_ORDER == LITTLE_ENDIAN
37 # define HOST_LITTLE_ENDIAN
38 # endif
39 # endif
40 #endif
42 #ifdef HOST_LITTLE_ENDIAN
43 # define le16(x) (x)
44 # define le32(x) (x)
45 #else
47 static inline uint16_t le16(uint16_t v) {
48 return (v >> 8) | (v << 8);
51 static inline uint32_t le32(uint32_t v) {
52 return (le16(v) << 16) | (le16(v >> 16));
54 #endif // HOST_LITTLE_ENDIAN
56 #endif // PROXENDIAN_H__