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. */
39 #include <sys/types.h>
42 static inline int8_t bswap(const int8_t& x
)
47 static inline u_int8_t
bswap(const u_int8_t
& x
)
52 static inline int16_t bswap(const int16_t& x
)
57 static inline u_int16_t
bswap(const u_int16_t
& x
)
62 static inline int32_t bswap(const int32_t& x
)
67 static inline u_int32_t
bswap(const u_int32_t
& x
)
72 static inline int64_t bswap(const int64_t& x
)
77 static inline u_int64_t
bswap(const u_int64_t
& x
)
82 #define le_to_cpu cpu_to_le
83 #define be_to_cpu cpu_to_be
85 template <class T
> static inline T
cpu_to_le(const T
& x
)
87 #if BYTE_ORDER == LITTLE_ENDIAN
94 template <class T
> static inline T
cpu_to_be(const T
& x
)
96 #if BYTE_ORDER == LITTLE_ENDIAN
103 template <class T
> class le_t
{
108 void write(const T
& n
) {
121 le_t
<T
> operator++() {
125 le_t
<T
> operator++(int) {
129 le_t
<T
> operator--() {
133 le_t
<T
> operator--(int) {
137 le_t
<T
>& operator+=(const T
& t
) {
141 le_t
<T
>& operator-=(const T
& t
) {
145 le_t
<T
>& operator&=(const le_t
<T
>& t
) {
149 le_t
<T
>& operator|=(const le_t
<T
>& t
) {
153 } __attribute__((packed
));
155 /* Just copy-and-pasted from le_t. Too lazy to do it right. */
157 template <class T
> class be_t
{
162 void write(const T
& n
) {
175 be_t
<T
> operator++() {
179 be_t
<T
> operator++(int) {
183 be_t
<T
> operator--() {
187 be_t
<T
> operator--(int) {
191 be_t
<T
>& operator+=(const T
& t
) {
195 be_t
<T
>& operator-=(const T
& t
) {
199 be_t
<T
>& operator&=(const be_t
<T
>& t
) {
203 be_t
<T
>& operator|=(const be_t
<T
>& t
) {
207 } __attribute__((packed
));
209 /* Define types of native endianness similar to the little and big endian
210 * versions below. Not really necessary but useful occasionally to emphasize
211 * endianness of data.
214 typedef int8_t int8_ne_t
;
215 typedef int16_t int16_ne_t
;
216 typedef int32_t int32_ne_t
;
217 typedef int64_t int64_ne_t
;
218 typedef u_int8_t u_int8_ne_t
;
219 typedef u_int16_t u_int16_ne_t
;
220 typedef u_int32_t u_int32_ne_t
;
221 typedef u_int64_t u_int64_ne_t
;
224 /* The classes work on their native endianness as well, but obviously
225 * introduce some overhead. Use the faster typedefs to native types
226 * therefore, unless you're debugging.
229 #if BYTE_ORDER == LITTLE_ENDIAN
230 typedef int8_ne_t int8_le_t
;
231 typedef int16_ne_t int16_le_t
;
232 typedef int32_ne_t int32_le_t
;
233 typedef int64_ne_t int64_le_t
;
234 typedef u_int8_ne_t u_int8_le_t
;
235 typedef u_int16_ne_t u_int16_le_t
;
236 typedef u_int32_ne_t u_int32_le_t
;
237 typedef u_int64_ne_t u_int64_le_t
;
238 typedef int8_t int8_be_t
;
239 typedef be_t
<int16_t> int16_be_t
;
240 typedef be_t
<int32_t> int32_be_t
;
241 typedef be_t
<int64_t> int64_be_t
;
242 typedef u_int8_t u_int8_be_t
;
243 typedef be_t
<u_int16_t
> u_int16_be_t
;
244 typedef be_t
<u_int32_t
> u_int32_be_t
;
245 typedef be_t
<u_int64_t
> u_int64_be_t
;
247 typedef int8_ne_t int8_be_t
;
248 typedef int16_ne_t int16_be_t
;
249 typedef int32_ne_t int32_be_t
;
250 typedef int64_ne_t int64_be_t
;
251 typedef u_int8_ne_t u_int8_be_t
;
252 typedef u_int16_ne_t u_int16_be_t
;
253 typedef u_int32_ne_t u_int32_be_t
;
254 typedef u_int64_ne_t u_int64_be_t
;
255 typedef int8_t int8_le_t
;
256 typedef le_t
<int16_t> int16_le_t
;
257 typedef le_t
<int32_t> int32_le_t
;
258 typedef le_t
<int64_t> int64_le_t
;
259 typedef u_int8_t u_int8_le_t
;
260 typedef le_t
<u_int16_t
> u_int16_le_t
;
261 typedef le_t
<u_int32_t
> u_int32_le_t
;
262 typedef le_t
<u_int64_t
> u_int64_le_t
;