[go: up one dir, main page]

Menu

[d6a55f]: / q_endian.h  Maximize  Restore  History

Download this file

257 lines (198 with data), 7.8 kB

  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
/* q_endian.h -- endianness detection / handling
*
* Copyright (C) 1996-1997 Id Software, Inc.
* Copyright (C) 2007-2016 O.Sezer <sezero@users.sourceforge.net>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef QENDIAN_H
#define QENDIAN_H
/* to detect byte order at runtime instead of compile time, define as 1 */
#undef ENDIAN_RUNTIME_DETECT
#define ENDIAN_RUNTIME_DETECT 0
/* try the system headers first: with BSD and derivatives,
* sys/types.h can pull in the correct header and defs. */
#include <sys/types.h>
#if !defined(BYTE_ORDER) /* include more if it didn't work */
# if defined(__linux__) || defined(__linux)
# include <endian.h>
# elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__FreeBSD_kernel__) /* Debian GNU/kFreeBSD */ || \
(defined(__APPLE__) && defined(__MACH__)) /* Mac OS X */ || \
defined(__DragonFly__)
# include <machine/endian.h>
# elif defined(__sun) || defined(__svr4__)
# include <sys/byteorder.h>
# elif defined(_AIX)
# include <sys/machine.h>
# elif defined(sgi)
# include <sys/endian.h>
# elif defined(__MINGW32__)
# include <sys/param.h>
# elif defined(__DJGPP__)
# include <machine/endian.h>
# elif defined(__EMX__)
# include <machine/endian.h>
# elif defined(__OS2__) && defined(__WATCOMC__)
# include <machine/endian.h>
# elif defined(__MORPHOS__)
# include <machine/endian.h>
# elif defined(__amigaos__) && defined(__NEWLIB__)
# include <machine/endian.h>
# elif defined(__amigaos__) && defined(__CLIB2__)
# if !defined(__amigaos4__)
# define __NO_NET_API /* have netinclude/ instead */
# endif
# include <unistd.h>
# endif
#endif /* endian includes */
/* alternative macro names: */
#if defined(__BYTE_ORDER) && !defined(BYTE_ORDER)
#define BYTE_ORDER __BYTE_ORDER
#endif
#if defined(__LITTLE_ENDIAN) && !defined(LITTLE_ENDIAN)
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#endif
#if defined(__BIG_ENDIAN) && !defined(BIG_ENDIAN)
#define BIG_ENDIAN __BIG_ENDIAN
#endif
/* new gcc and clang versions pre-define the following: */
#if defined(__BYTE_ORDER__) && !defined(BYTE_ORDER)
#define BYTE_ORDER __BYTE_ORDER__
#endif
#if defined(__ORDER_LITTLE_ENDIAN__) && !defined(LITTLE_ENDIAN)
#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
#endif
#if defined(__ORDER_BIG_ENDIAN__) && !defined(BIG_ENDIAN)
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
#endif
#if !defined(PDP_ENDIAN)
#if defined(__ORDER_PDP_ENDIAN__)
#define PDP_ENDIAN __ORDER_PDP_ENDIAN__
#elif defined(__PDP_ENDIAN)
#define PDP_ENDIAN __PDP_ENDIAN
#else
#define PDP_ENDIAN 3412
#endif
#endif /* NUXI endian -- unsupported */
#if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# if (BYTE_ORDER != LITTLE_ENDIAN) && (BYTE_ORDER != BIG_ENDIAN)
# error "Unsupported endianness."
# endif
#else /* one of the definitions is mising. */
# undef BYTE_ORDER
# undef LITTLE_ENDIAN
# undef BIG_ENDIAN
# define LITTLE_ENDIAN 1234
# define BIG_ENDIAN 4321
#endif /* byte order defs */
#if !defined(BYTE_ORDER) /* good assumptions: */
/* try the compiler-predefined endianness macros.
* Ref.: https://sf.net/p/predef/wiki/Endianness/
*/
# if defined(__BIG_ENDIAN__) || defined(_BIG_ENDIAN)
# define BYTE_ORDER BIG_ENDIAN
# elif defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
# define BYTE_ORDER BIG_ENDIAN
# elif defined(__LITTLE_ENDIAN__) || defined(_LITTLE_ENDIAN)
# define BYTE_ORDER LITTLE_ENDIAN
# elif defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
# define BYTE_ORDER LITTLE_ENDIAN
/* assumptions based on OS and/or architecture macros. Some refs:
* predef/other/endian.h from the boost library
* http://labs.hoffmanlabs.com/node/544
* https://blogs.msdn.com/larryosterman/archive/2005/06/07/426334.aspx
*/
# elif defined(__i386) || defined(__i386__) || defined(__386__) || defined(_M_IX86) || defined(__I386__)
# define BYTE_ORDER LITTLE_ENDIAN /* any x86 */
# elif defined(__amd64) || defined(__x86_64__) || defined(_M_X64)
# define BYTE_ORDER LITTLE_ENDIAN /* any x64 */
# elif defined(__mc68000__) || defined(__M68K__) || defined(__m68k__) || defined(__MC68K__)
# define BYTE_ORDER BIG_ENDIAN /* Motorola 68k */
# elif defined(__sun) || defined(__svr4__) /* solaris */
# if defined(_LITTLE_ENDIAN) /* x86 */
# define BYTE_ORDER LITTLE_ENDIAN
# elif defined(_BIG_ENDIAN) /* sparc */
# define BYTE_ORDER BIG_ENDIAN
# endif
# elif defined(__hppa) || defined(__hppa__)
# define BYTE_ORDER BIG_ENDIAN /* PARISC */
# elif defined(__sparc) || defined(__sparc__)
# define BYTE_ORDER BIG_ENDIAN /* SPARC -- cf. boost/predef/other/endian.h */
# elif defined(__DJGPP__) || defined(__MSDOS__) || defined(__DOS__) || defined(_DOS)
# define BYTE_ORDER LITTLE_ENDIAN /* DOS */
# elif defined(__OS2__) || defined(__EMX__)
# define BYTE_ORDER LITTLE_ENDIAN /* OS2 */
# elif defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(__NT__) || defined(_Windows)
# define BYTE_ORDER LITTLE_ENDIAN /* Windows */
# endif
#endif /* good assumptions */
#if !defined(BYTE_ORDER) /* [un]safe assumptions: */
/* assumptions based on OS and/or architecture macros.
* proceed carefully: many of these are bi-endian CPUs.
*/
# if defined(__ppc__) || defined(__powerpc__) || defined(__POWERPC__) || defined(__PPC__)
# define BYTE_ORDER BIG_ENDIAN
# elif defined(__alpha__) || defined(__alpha)
# define BYTE_ORDER LITTLE_ENDIAN
# elif defined(__mips__) || defined(__MIPS__)
# define BYTE_ORDER BIG_ENDIAN
# endif
# if defined(BYTE_ORDER) && 0 /* change to 1 to for warnings */
# if (BYTE_ORDER == LITTLE_ENDIAN)
# warning "Using LIL endian as a SAFE default."
# else /*(BYTE_ORDER == BIG_ENDIAN)*/
# warning "Using BIG endian as a SAFE default."
# endif
# endif
#endif /* [un]safe assumptions */
#if !ENDIAN_RUNTIME_DETECT
#if !defined(BYTE_ORDER)
# error Could not determine BYTE_ORDER
#endif
/* for autotools compatibility */
#if (BYTE_ORDER == BIG_ENDIAN) && !defined(WORDS_BIGENDIAN)
# define WORDS_BIGENDIAN 1
#endif
#if (BYTE_ORDER != BIG_ENDIAN) && defined(WORDS_BIGENDIAN)
#error WORDS_BIGENDIAN defined for non-BIG_ENDIAN
#endif
#endif /* ! ENDIAN_RUNTIME_DETECT */
extern int host_byteorder;
extern int host_bigendian; /* bool */
extern int DetectByteorder (void);
extern void ByteOrder_Init (void);
/* byte swapping. most times we want to convert to
* little endian: our data files are written in LE
* format. sometimes, such as when writing to net,
* we also convert to big endian.
*/
#if ENDIAN_RUNTIME_DETECT
extern int (*BigLong) (int);
extern int (*LittleLong) (int);
#else /* ! ENDIAN_RUNTIME_DETECT */
extern int LongSwap (int);
#if (BYTE_ORDER == BIG_ENDIAN)
#define BigLong(l) (l)
#define LittleLong(l) LongSwap((l))
#else /* BYTE_ORDER == LITTLE_ENDIAN */
#define BigLong(l) LongSwap((l))
#define LittleLong(l) (l)
#endif /* swap macros */
#endif /* runtime det */
#endif /* QENDIAN_H */