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. */
31 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
40 # include <sys/endian.h>
41 #endif /* !defined(__FreeBSD__) && !defined(__NetBSD__) */
43 #include <sys/types.h>
44 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
47 #define bswap_16(x) bswap16(x)
48 #define bswap_32(x) bswap32(x)
49 #define bswap_64(x) bswap64(x)
50 #endif /* !defined(__FreeBSD__) && !defined(__NetBSD__) */
52 static inline int8_t bswap(const int8_t& x)
57 static inline u_int8_t bswap(const u_int8_t& x)
62 static inline int16_t bswap(const int16_t& x)
67 static inline u_int16_t bswap(const u_int16_t& x)
72 static inline int32_t bswap(const int32_t& x)
77 static inline u_int32_t bswap(const u_int32_t& x)
82 static inline int64_t bswap(const int64_t& x)
87 static inline u_int64_t bswap(const u_int64_t& x)
92 #define le_to_cpu cpu_to_le
93 #define be_to_cpu cpu_to_be
95 template <class T> static inline T cpu_to_le(const T& x)
97 #if BYTE_ORDER == LITTLE_ENDIAN
104 template <class T> static inline T cpu_to_be(const T& x)
106 #if BYTE_ORDER == LITTLE_ENDIAN
113 template <class T> class le_t {
118 void write(const T& n) {
131 le_t<T> operator++() {
135 le_t<T> operator++(int) {
139 le_t<T> operator--() {
143 le_t<T> operator--(int) {
147 le_t<T>& operator+=(const T& t) {
151 le_t<T>& operator-=(const T& t) {
155 le_t<T>& operator&=(const le_t<T>& t) {
159 le_t<T>& operator|=(const le_t<T>& t) {
163 } __attribute__((packed));
165 /* Just copy-and-pasted from le_t. Too lazy to do it right. */
167 template <class T> class be_t {
172 void write(const T& n) {
185 be_t<T> operator++() {
189 be_t<T> operator++(int) {
193 be_t<T> operator--() {
197 be_t<T> operator--(int) {
201 be_t<T>& operator+=(const T& t) {
205 be_t<T>& operator-=(const T& t) {
209 be_t<T>& operator&=(const be_t<T>& t) {
213 be_t<T>& operator|=(const be_t<T>& t) {
217 } __attribute__((packed));
219 /* Define types of native endianness similar to the little and big endian
220 * versions below. Not really necessary but useful occasionally to emphasize
221 * endianness of data.
224 typedef int8_t int8_ne_t;
225 typedef int16_t int16_ne_t;
226 typedef int32_t int32_ne_t;
227 typedef int64_t int64_ne_t;
228 typedef u_int8_t u_int8_ne_t;
229 typedef u_int16_t u_int16_ne_t;
230 typedef u_int32_t u_int32_ne_t;
231 typedef u_int64_t u_int64_ne_t;
234 /* The classes work on their native endianness as well, but obviously
235 * introduce some overhead. Use the faster typedefs to native types
236 * therefore, unless you're debugging.
239 #if BYTE_ORDER == LITTLE_ENDIAN
240 typedef int8_ne_t int8_le_t;
241 typedef int16_ne_t int16_le_t;
242 typedef int32_ne_t int32_le_t;
243 typedef int64_ne_t int64_le_t;
244 typedef u_int8_ne_t u_int8_le_t;
245 typedef u_int16_ne_t u_int16_le_t;
246 typedef u_int32_ne_t u_int32_le_t;
247 typedef u_int64_ne_t u_int64_le_t;
248 typedef int8_t int8_be_t;
249 typedef be_t<int16_t> int16_be_t;
250 typedef be_t<int32_t> int32_be_t;
251 typedef be_t<int64_t> int64_be_t;
252 typedef u_int8_t u_int8_be_t;
253 typedef be_t<u_int16_t> u_int16_be_t;
254 typedef be_t<u_int32_t> u_int32_be_t;
255 typedef be_t<u_int64_t> u_int64_be_t;
257 typedef int8_ne_t int8_be_t;
258 typedef int16_ne_t int16_be_t;
259 typedef int32_ne_t int32_be_t;
260 typedef int64_ne_t int64_be_t;
261 typedef u_int8_ne_t u_int8_be_t;
262 typedef u_int16_ne_t u_int16_be_t;
263 typedef u_int32_ne_t u_int32_be_t;
264 typedef u_int64_ne_t u_int64_be_t;
265 typedef int8_t int8_le_t;
266 typedef le_t<int16_t> int16_le_t;
267 typedef le_t<int32_t> int32_le_t;
268 typedef le_t<int64_t> int64_le_t;
269 typedef u_int8_t u_int8_le_t;
270 typedef le_t<u_int16_t> u_int16_le_t;
271 typedef le_t<u_int32_t> u_int32_le_t;
272 typedef le_t<u_int64_t> u_int64_le_t;