3 * Quick hack to handle endianness and word length issues.
4 * Defines _le, _be, and _ne variants to standard ISO types
5 * like int32_t, that are stored in little-endian, big-endian,
6 * and native-endian byteorder in memory, respectively.
7 * Caveat: int32_le_t and friends cannot be used in vararg
8 * functions like printf() without an explicit cast.
10 * Copyright (c) 2003-2005 Daniel Kobras <kobras@debian.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #ifndef _ENDIAN_TYPES_H
28 #define _ENDIAN_TYPES_H
30 /* Needed for BYTE_ORDER and BIG/LITTLE_ENDIAN macros. */
36 # include <sys/endian.h>
37 #endif /* __FreeBSD__ */
43 # include <sys/endian.h>
44 #endif /* __FreeBSD__ */
47 #include <sys/types.h>
51 #define bswap_16(x) bswap16(x)
52 #define bswap_32(x) bswap32(x)
53 #define bswap_64(x) bswap64(x)
54 #endif /* __FreeBSD__ */
56 static inline int8_t bswap(const int8_t& x
)
61 static inline u_int8_t
bswap(const u_int8_t
& x
)
66 static inline int16_t bswap(const int16_t& x
)
71 static inline u_int16_t
bswap(const u_int16_t
& x
)
76 static inline int32_t bswap(const int32_t& x
)
81 static inline u_int32_t
bswap(const u_int32_t
& x
)
86 static inline int64_t bswap(const int64_t& x
)
91 static inline u_int64_t
bswap(const u_int64_t
& x
)
96 #define le_to_cpu cpu_to_le
97 #define be_to_cpu cpu_to_be
99 template <class T
> static inline T
cpu_to_le(const T
& x
)
101 #if BYTE_ORDER == LITTLE_ENDIAN
108 template <class T
> static inline T
cpu_to_be(const T
& x
)
110 #if BYTE_ORDER == LITTLE_ENDIAN
117 template <class T
> class le_t
{
122 void write(const T
& n
) {
135 le_t
<T
> operator++() {
139 le_t
<T
> operator++(int) {
143 le_t
<T
> operator--() {
147 le_t
<T
> operator--(int) {
151 le_t
<T
>& operator+=(const T
& t
) {
155 le_t
<T
>& operator-=(const T
& t
) {
159 le_t
<T
>& operator&=(const le_t
<T
>& t
) {
163 le_t
<T
>& operator|=(const le_t
<T
>& t
) {
167 } __attribute__((packed
));
169 /* Just copy-and-pasted from le_t. Too lazy to do it right. */
171 template <class T
> class be_t
{
176 void write(const T
& n
) {
189 be_t
<T
> operator++() {
193 be_t
<T
> operator++(int) {
197 be_t
<T
> operator--() {
201 be_t
<T
> operator--(int) {
205 be_t
<T
>& operator+=(const T
& t
) {
209 be_t
<T
>& operator-=(const T
& t
) {
213 be_t
<T
>& operator&=(const be_t
<T
>& t
) {
217 be_t
<T
>& operator|=(const be_t
<T
>& t
) {
221 } __attribute__((packed
));
223 /* Define types of native endianness similar to the little and big endian
224 * versions below. Not really necessary but useful occasionally to emphasize
225 * endianness of data.
228 typedef int8_t int8_ne_t
;
229 typedef int16_t int16_ne_t
;
230 typedef int32_t int32_ne_t
;
231 typedef int64_t int64_ne_t
;
232 typedef u_int8_t u_int8_ne_t
;
233 typedef u_int16_t u_int16_ne_t
;
234 typedef u_int32_t u_int32_ne_t
;
235 typedef u_int64_t u_int64_ne_t
;
238 /* The classes work on their native endianness as well, but obviously
239 * introduce some overhead. Use the faster typedefs to native types
240 * therefore, unless you're debugging.
243 #if BYTE_ORDER == LITTLE_ENDIAN
244 typedef int8_ne_t int8_le_t
;
245 typedef int16_ne_t int16_le_t
;
246 typedef int32_ne_t int32_le_t
;
247 typedef int64_ne_t int64_le_t
;
248 typedef u_int8_ne_t u_int8_le_t
;
249 typedef u_int16_ne_t u_int16_le_t
;
250 typedef u_int32_ne_t u_int32_le_t
;
251 typedef u_int64_ne_t u_int64_le_t
;
252 typedef int8_t int8_be_t
;
253 typedef be_t
<int16_t> int16_be_t
;
254 typedef be_t
<int32_t> int32_be_t
;
255 typedef be_t
<int64_t> int64_be_t
;
256 typedef u_int8_t u_int8_be_t
;
257 typedef be_t
<u_int16_t
> u_int16_be_t
;
258 typedef be_t
<u_int32_t
> u_int32_be_t
;
259 typedef be_t
<u_int64_t
> u_int64_be_t
;
261 typedef int8_ne_t int8_be_t
;
262 typedef int16_ne_t int16_be_t
;
263 typedef int32_ne_t int32_be_t
;
264 typedef int64_ne_t int64_be_t
;
265 typedef u_int8_ne_t u_int8_be_t
;
266 typedef u_int16_ne_t u_int16_be_t
;
267 typedef u_int32_ne_t u_int32_be_t
;
268 typedef u_int64_ne_t u_int64_be_t
;
269 typedef int8_t int8_le_t
;
270 typedef le_t
<int16_t> int16_le_t
;
271 typedef le_t
<int32_t> int32_le_t
;
272 typedef le_t
<int64_t> int64_le_t
;
273 typedef u_int8_t u_int8_le_t
;
274 typedef le_t
<u_int16_t
> u_int16_le_t
;
275 typedef le_t
<u_int32_t
> u_int32_le_t
;
276 typedef le_t
<u_int64_t
> u_int64_le_t
;