File: boops.h

package info (click to toggle)
dateutils 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,924 kB
  • sloc: ansic: 22,097; makefile: 1,666; yacc: 198; sh: 168; lex: 108
file content (304 lines) | stat: -rw-r--r-- 8,488 bytes parent folder | download | duplicates (2)
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
/*** boops.h -- byte-order operations
 *
 * Copyright (C) 2012-2018 Sebastian Freundt
 * Copyright (C) 2012 Ruediger Meier
 *
 * Author:  Sebastian Freundt <freundt@ga-group.nl>
 *
 * This file is part of uterus, dateutils and atem.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the author nor the names of any contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***/

#if !defined INCLUDED_boops_h_
#define INCLUDED_boops_h_

#if defined HAVE_CONFIG_H
# include "config.h"
#endif	/* HAVE_CONFIG_H */
#if defined HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif	/* HAVE_SYS_PARAM_H */
#if defined HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif	/* HAVE_SYS_TYPES_H */
/* *bsd except for openbsd */
#if defined HAVE_SYS_ENDIAN_H
# include <sys/endian.h>
#elif defined HAVE_MACHINE_ENDIAN_H
# include <machine/endian.h>
#elif defined HAVE_ENDIAN_H
# include <endian.h>
#elif defined HAVE_BYTEORDER_H
# include <byteorder.h>
#endif	/* SYS/ENDIAN_H || MACHINE/ENDIAN_H || ENDIAN_H || BYTEORDER_H */

/* check for byteswap to do the swapping ourselves if need be */
#if defined HAVE_BYTESWAP_H
# include <byteswap.h>
#endif	/* BYTESWAP_H */

#if !defined BYTE_ORDER
# if defined __BYTE_ORDER
#  define BYTE_ORDER	__BYTE_ORDER
# elif defined __BYTE_ORDER__
#  define BYTE_ORDER	__BYTE_ORDER__
# else
#  define BYTE_ORDER	-1
# endif
#endif	/* !BYTE_ORDER */

#if !defined LITTLE_ENDIAN
# if defined __ORDER_LITTLE_ENDIAN__
#  define LITTLE_ENDIAN	__ORDER_LITTLE_ENDIAN__
# elif defined __LITTLE_ENDIAN
#  define LITTLE_ENDIAN	__LITTLE_ENDIAN
# elif defined __LITTLE_ENDIAN__
#  define LITTLE_ENDIAN	__LITTLE_ENDIAN__
# else
#  define LITTLE_ENDIAN	0
# endif
#endif	/* !LITTLE_ENDIAN */

#if !defined BIG_ENDIAN
# if defined __ORDER_BIG_ENDIAN__
#  define BIG_ENDIAN	__ORDER_BIG_ENDIAN__
# elif defined __BIG_ENDIAN
#  define BIG_ENDIAN	__BIG_ENDIAN
# elif defined __BIG_ENDIAN__
#  define BIG_ENDIAN	__BIG_ENDIAN__
# else
#  define BIG_ENDIAN	0
# endif
#endif	/* !BIG_ENDIAN */

#if BYTE_ORDER == LITTLE_ENDIAN
/* do nothing */
#elif BYTE_ORDER == BIG_ENDIAN
/* still nothing */
#elif LITTLE_ENDIAN && !BIG_ENDIAN
# undef BYTE_ORDER
# define BYTE_ORDER	LITTLE_ENDIAN
#elif BIG_ENDIAN && !LITTLE_ENDIAN
# undef BYTE_ORDER
# define BYTE_ORDER	BIG_ENDIAN
#endif


/* start off with opposite-endianness converters */
#if defined htooe16
/* yay, nothing to do really */
#elif defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ >= 7
# define htooe16(x)	__builtin_bswap16(x)
#elif defined __bswap_16
# define htooe16(x)	__bswap_16(x)
#elif defined __swap16
# define htooe16(x)	__swap16(x)
#elif BYTE_ORDER == BIG_ENDIAN && defined le16toh
# define htooe16(x)	le16toh(x)
#elif BYTE_ORDER == LITTLE_ENDIAN && defined be16toh
# define htooe16(x)	be16toh(x)
#else
/* do it agnostically */
static inline __attribute__((pure, const)) uint16_t
htooe16(uint16_t x)
{
	return ((x >> 8U) & 0xffU) | ((x << 8U) & 0xff00U);
}
#endif	/* htooe16 */

#if !defined be16toh
# if defined betoh16
#  define be16toh	betoh16
# elif BYTE_ORDER == BIG_ENDIAN
#  define be16toh(x)	(x)
# else	/* means we need swapping */
#  define be16toh(x)	htooe16(x)
# endif	 /* betoh16 */
#endif	/* !be16toh */

#if !defined le16toh
# if defined letoh16
#  define le16toh	letoh16
# elif BYTE_ORDER == BIG_ENDIAN
#  define le16toh(x)	htooe16(x)
# else	/* no swapping needed */
#  define le16toh(x)	(x)
# endif	 /* letoh16 */
#endif	/* !le16toh */

#if !defined htobe16
# if BYTE_ORDER == BIG_ENDIAN
#  define htobe16(x)	(x)
# else	/* need swabbing */
#  define htobe16(x)	htooe16(x)
# endif
#endif	/* !htobe16 */

#if !defined htole16
# if BYTE_ORDER == BIG_ENDIAN
#  define htole16(x)	htooe16(x)
# else	/* no byte swapping needed */
#  define htole16(x)	(x)
# endif
#endif	/* !htole16 */


/* just to abstract over pure swapping */
#if defined htooe32
/* yay, nothing to do really */
#elif defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ >= 7
# define htooe32(x)	__builtin_bswap32(x)
#elif defined __bswap_32
# define htooe32(x)	__bswap_32(x)
#elif defined __swap32
# define htooe32(x)	__swap32(x)
#elif BYTE_ORDER == BIG_ENDIAN && defined le32toh
# define htooe32(x)	le32toh(x)
#elif BYTE_ORDER == LITTLE_ENDIAN && defined be32toh
# define htooe32(x)	be32toh(x)
#else
/* do it agnostically */
static inline __attribute__((pure, const)) uint32_t
htooe32(uint32_t x)
{
	/* swap them word-wise first */
	x = ((x >> 16U) & 0xffffU) | ((x << 16U) & 0xffff0000U);
	/* do the byte swap */
	return ((x >> 8U) & 0xff00ffU) | ((x << 8U) & 0xff00ff00U);
}
#endif

/* and even now we may be out of luck */
#if !defined be32toh
# if defined betoh32
#  define be32toh	betoh32
# elif BYTE_ORDER == BIG_ENDIAN
#  define be32toh(x)	(x)
# else	/* need some swaps */
#  define be32toh(x)	htooe32(x)
# endif
#endif	/* !be32toh */

#if !defined le32toh
# if defined letoh32
#  define le32toh	letoh32
# elif BYTE_ORDER == BIG_ENDIAN
#  define le32toh(x)	htooe32(x)
# else	/* no byte swapping here */
#  define le32toh(x)	(x)
# endif	 /* letoh32 */
#endif	/* !le32toh */

#if !defined htobe32
# if BYTE_ORDER == BIG_ENDIAN
#  define htobe32(x)	(x)
# else	/* yep, swap me about */
#  define htobe32(x)	htooe32(x)
# endif
#endif	/* !be32toh */

#if !defined htole32
# if BYTE_ORDER == BIG_ENDIAN
#  define htole32(x)	htooe32(x)
# else	/* nothing to swap */
#  define htole32(x)	(x)
# endif
#endif	/* !htole32 */


#if defined htooe64
/* yay, nothing to do really */
#elif defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ >= 7
# define htooe64(x)	__builtin_bswap64(x)
#elif defined __bswap_64
# define htooe64(x)	__bswap_64(x)
#elif defined __swap64
# define htooe64(x)	__swap64(x)
#elif BYTE_ORDER == BIG_ENDIAN && defined le64toh
# define htooe64(x)	le64toh(x)
#elif BYTE_ORDER == LITTLE_ENDIAN && defined be64toh
# define htooe64(x)	be64toh(x)
#else
/* do it agnostically */
static inline __attribute__((pure, const)) uint64_t
htooe64(uint64_t x)
{
	/* swap them dword-wise first */
	x = ((x >> 32U) & 0xffffffffU) | ((x << 32U) & 0xffffffff00000000U);
	/* word wise swap now */
	x = ((x >> 16U) & 0xffff0000ffffU) |
		((x << 16U) & 0xffff0000ffff0000U);
	/* do the byte swap */
	return ((x >> 8U) & 0xff00ff00ff00ffU) |
		((x << 8U) & 0xff00ff00ff00ff00U);
}
#endif

#if !defined be64toh
# if defined betoh64
#  define be64toh	betoh64
# elif BYTE_ORDER == BIG_ENDIAN
#  define be64toh(x)	(x)
# else	/* swapping */
#  define be64toh(x)	htooe64(x)
# endif
#endif	/* !be64toh */

#if !defined le64toh
# if defined letoh64
#  define le64toh	letoh64
# elif BYTE_ORDER == BIG_ENDIAN
#  define le64toh(x)	htooe64(x)
# else	/* nothing to swap */
#  define le64toh(x)	(x)
# endif
#endif	/* !le64toh */

#if !defined htobe64
# if BYTE_ORDER == BIG_ENDIAN
#  define htobe64(x)	(x)
# else
#  define htobe64(x)	htooe64(x)
# endif
#endif	/* !htobe64 */

#if !defined htole64
# if BYTE_ORDER == BIG_ENDIAN
#  define htole64(x)	htooe64(x)
# else	/* no need swapping */
#  define htole64(x)	(x)
# endif
#endif	/* !htole64 */

/* we could technically include byteswap.h and to the swap ourselves
 * in the missing cases.  Instead we'll just leave it as is and wait
 * for bug reports. */

#endif	/* INCLUDED_boops_h_ */