diff options
Diffstat (limited to 'src/lib/mhd_byteorder.h')
-rw-r--r-- | src/lib/mhd_byteorder.h | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/lib/mhd_byteorder.h b/src/lib/mhd_byteorder.h new file mode 100644 index 00000000..66689804 --- /dev/null +++ b/src/lib/mhd_byteorder.h | |||
@@ -0,0 +1,160 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 2015 Karlson2k (Evgeny Grin) | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library. | ||
17 | If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file microhttpd/mhd_byteorder.h | ||
22 | * @brief macro definitions for host byte order | ||
23 | * @author Karlson2k (Evgeny Grin) | ||
24 | */ | ||
25 | |||
26 | #ifndef MHD_BYTEORDER_H | ||
27 | #define MHD_BYTEORDER_H | ||
28 | |||
29 | #include "platform.h" | ||
30 | |||
31 | #if HAVE_ENDIAN_H | ||
32 | #include <endian.h> | ||
33 | #endif | ||
34 | |||
35 | #if HAVE_SYS_PARAM_H | ||
36 | #include <sys/param.h> | ||
37 | #endif | ||
38 | |||
39 | #if HAVE_MACHINE_ENDIAN_H | ||
40 | #include <machine/endian.h> | ||
41 | #endif | ||
42 | |||
43 | #if HAVE_SYS_ENDIAN_H | ||
44 | #include <sys/endian.h> | ||
45 | #endif | ||
46 | |||
47 | #if HAVE_SYS_TYPES_H | ||
48 | #include <sys/types.h> | ||
49 | #endif | ||
50 | |||
51 | #if HAVE_SYS_BYTEORDER_H | ||
52 | #include <sys/byteorder.h> | ||
53 | #endif | ||
54 | |||
55 | #if HAVE_SYS_MACHINE_H | ||
56 | #include <sys/machine.h> | ||
57 | #endif | ||
58 | |||
59 | #if HAVE_MACHINE_PARAM_H | ||
60 | #include <machine/param.h> | ||
61 | #endif | ||
62 | |||
63 | #if HAVE_SYS_ISA_DEFS_H | ||
64 | #include <sys/isa_defs.h> | ||
65 | #endif | ||
66 | |||
67 | #define _MHD_BIG_ENDIAN 1234 | ||
68 | #define _MHD_LITTLE_ENDIAN 4321 | ||
69 | #define _MHD_PDP_ENDIAN 2143 | ||
70 | |||
71 | #if defined(__BYTE_ORDER__) | ||
72 | #if defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||
73 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
74 | #elif defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | ||
75 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
76 | #elif defined(__ORDER_PDP_ENDIAN__) && __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__ | ||
77 | #define _MHD_BYTE_ORDER _MHD_PDP_ENDIAN | ||
78 | #endif /* __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__ */ | ||
79 | #elif defined(__BYTE_ORDER) | ||
80 | #if defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN | ||
81 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
82 | #elif defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN | ||
83 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
84 | #elif defined(__PDP_ENDIAN) && __BYTE_ORDER == __PDP_ENDIAN | ||
85 | #define _MHD_BYTE_ORDER _MHD_PDP_ENDIAN | ||
86 | #endif /* __BYTE_ORDER == __PDP_ENDIAN */ | ||
87 | #elif defined (BYTE_ORDER) | ||
88 | #if defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN | ||
89 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
90 | #elif defined(LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN | ||
91 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
92 | #elif defined(PDP_ENDIAN) && BYTE_ORDER == PDP_ENDIAN | ||
93 | #define _MHD_BYTE_ORDER _MHD_PDP_ENDIAN | ||
94 | #endif /* __BYTE_ORDER == _PDP_ENDIAN */ | ||
95 | #elif defined (_BYTE_ORDER) | ||
96 | #if defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN | ||
97 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
98 | #elif defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN | ||
99 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
100 | #elif defined(_PDP_ENDIAN) && _BYTE_ORDER == _PDP_ENDIAN | ||
101 | #define _MHD_BYTE_ORDER _MHD_PDP_ENDIAN | ||
102 | #endif /* _BYTE_ORDER == _PDP_ENDIAN */ | ||
103 | #endif /* _BYTE_ORDER */ | ||
104 | |||
105 | #ifndef _MHD_BYTE_ORDER | ||
106 | /* Byte order specification didn't detected in system headers */ | ||
107 | /* Try some guessing */ | ||
108 | |||
109 | #if (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) || \ | ||
110 | (defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) | ||
111 | /* Seems that we are on big endian platform */ | ||
112 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
113 | #elif (defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ | ||
114 | (defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) | ||
115 | /* Seems that we are on little endian platform */ | ||
116 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
117 | #elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || \ | ||
118 | defined(_M_X64) || defined(_M_AMD64) || defined(i386) || defined(__i386) || \ | ||
119 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ | ||
120 | defined(_M_IX86) || defined(_X86_) || defined (__THW_INTEL__) | ||
121 | /* x86 family is little endian */ | ||
122 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
123 | #elif defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \ | ||
124 | defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) | ||
125 | /* Looks like we are on ARM/MIPS in big endian mode */ | ||
126 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
127 | #elif defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \ | ||
128 | defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) | ||
129 | /* Looks like we are on ARM/MIPS in little endian mode */ | ||
130 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
131 | #elif defined(__m68k__) || defined(M68000) || defined(__hppa__) || defined(__hppa) || \ | ||
132 | defined(__HPPA__) || defined(__370__) || defined(__THW_370__) || \ | ||
133 | defined(__s390__) || defined(__s390x__) || defined(__SYSC_ZARCH__) | ||
134 | /* Looks like we are on big endian platform */ | ||
135 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
136 | #elif defined(__ia64__) || defined(_IA64) || defined(__IA64__) || defined(__ia64) || \ | ||
137 | defined(_M_IA64) || defined(__itanium__) || defined(__bfin__) || \ | ||
138 | defined(__BFIN__) || defined(bfin) || defined(BFIN) | ||
139 | /* Looks like we are on little endian platform */ | ||
140 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
141 | #elif defined(_WIN32) | ||
142 | /* W32 is always little endian on all platforms */ | ||
143 | #define _MHD_BYTE_ORDER _MHD_LITTLE_ENDIAN | ||
144 | #elif defined(WORDS_BIGENDIAN) | ||
145 | /* Use byte order detected by configure */ | ||
146 | #define _MHD_BYTE_ORDER _MHD_BIG_ENDIAN | ||
147 | #endif /* _WIN32 */ | ||
148 | |||
149 | #endif /* !_MHD_BYTE_ORDER */ | ||
150 | |||
151 | #ifdef _MHD_BYTE_ORDER | ||
152 | /* Some safety checks */ | ||
153 | #if defined(WORDS_BIGENDIAN) && _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN | ||
154 | #error Configure detected big endian byte order but headers specify different byte order | ||
155 | #elif !defined(WORDS_BIGENDIAN) && _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN | ||
156 | #error Configure did not detect big endian byte order but headers specify big endian byte order | ||
157 | #endif /* !WORDS_BIGENDIAN && _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN */ | ||
158 | #endif /* _MHD_BYTE_ORDER */ | ||
159 | |||
160 | #endif /* !MHD_BYTEORDER_H */ | ||