1 /* <endian_types.h>
2 *
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.
9 *
10 * Copyright (c) 2003-2005 Daniel Kobras <kobras@debian.org>
11 *
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.
16 *
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.
21 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26
27 #ifndef _ENDIAN_TYPES_H
28 #define _ENDIAN_TYPES_H
29
30 #include <sys/types.h>
31
32 /* Needed for BYTE_ORDER and BIG/LITTLE_ENDIAN macros. */
33 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
34 #ifndef _BSD_SOURCE
35 # define _BSD_SOURCE
36 # include <endian.h>
37 # undef _BSD_SOURCE
38 #else
39 # include <endian.h>
40 #endif
41 #else
42 # include <sys/endian.h>
43 #endif /* !defined(__FreeBSD__) && !defined(__NetBSD__) */
44
45 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
46 #include <byteswap.h>
47 #elif defined(__OpenBSD__)
48 #define bswap_16(x) swap16(x)
49 #define bswap_32(x) swap32(x)
50 #define bswap_64(x) swap64(x)
51 #else
52 #define bswap_16(x) bswap16(x)
53 #define bswap_32(x) bswap32(x)
54 #define bswap_64(x) bswap64(x)
55 #endif /* !defined(__FreeBSD__) && !defined(__NetBSD__) */
56
bswap(const int8_t & x)57 static inline int8_t bswap(const int8_t& x)
58 {
59 return x;
60 }
61
bswap(const u_int8_t & x)62 static inline u_int8_t bswap(const u_int8_t& x)
63 {
64 return x;
65 }
66
bswap(const int16_t & x)67 static inline int16_t bswap(const int16_t& x)
68 {
69 return bswap_16(x);
70 }
71
bswap(const u_int16_t & x)72 static inline u_int16_t bswap(const u_int16_t& x)
73 {
74 return bswap_16(x);
75 }
76
bswap(const int32_t & x)77 static inline int32_t bswap(const int32_t& x)
78 {
79 return bswap_32(x);
80 }
81
bswap(const u_int32_t & x)82 static inline u_int32_t bswap(const u_int32_t& x)
83 {
84 return bswap_32(x);
85 }
86
bswap(const int64_t & x)87 static inline int64_t bswap(const int64_t& x)
88 {
89 return bswap_64(x);
90 }
91
bswap(const u_int64_t & x)92 static inline u_int64_t bswap(const u_int64_t& x)
93 {
94 return bswap_64(x);
95 }
96
97 #define le_to_cpu cpu_to_le
98 #define be_to_cpu cpu_to_be
99
cpu_to_le(const T & x)100 template <class T> static inline T cpu_to_le(const T& x)
101 {
102 #if BYTE_ORDER == LITTLE_ENDIAN
103 return x;
104 #else
105 return bswap(x);
106 #endif
107 }
108
cpu_to_be(const T & x)109 template <class T> static inline T cpu_to_be(const T& x)
110 {
111 #if BYTE_ORDER == LITTLE_ENDIAN
112 return bswap(x);
113 #else
114 return x;
115 #endif
116 }
117
118 template <class T> class le_t {
119 T m;
read()120 T read() const {
121 return le_to_cpu(m);
122 };
write(const T & n)123 void write(const T& n) {
124 m = cpu_to_le(n);
125 };
126 public:
le_t(void)127 le_t(void) {
128 m = 0;
129 };
le_t(const T & o)130 le_t(const T& o) {
131 write(o);
132 };
T()133 operator T() const {
134 return read();
135 };
136 le_t<T> operator++() {
137 write(read() + 1);
138 return *this;
139 };
140 le_t<T> operator++(int) {
141 write(read() + 1);
142 return *this;
143 };
144 le_t<T> operator--() {
145 write(read() - 1);
146 return *this;
147 };
148 le_t<T> operator--(int) {
149 write(read() - 1);
150 return *this;
151 };
152 le_t<T>& operator+=(const T& t) {
153 write(read() + t);
154 return *this;
155 };
156 le_t<T>& operator-=(const T& t) {
157 write(read() - t);
158 return *this;
159 };
160 le_t<T>& operator&=(const le_t<T>& t) {
161 m &= t.m;
162 return *this;
163 };
164 le_t<T>& operator|=(const le_t<T>& t) {
165 m |= t.m;
166 return *this;
167 };
168 } __attribute__((packed));
169
170 /* Just copy-and-pasted from le_t. Too lazy to do it right. */
171
172 template <class T> class be_t {
173 T m;
read()174 T read() const {
175 return be_to_cpu(m);
176 };
write(const T & n)177 void write(const T& n) {
178 m = cpu_to_be(n);
179 };
180 public:
be_t(void)181 be_t(void) {
182 m = 0;
183 };
be_t(const T & o)184 be_t(const T& o) {
185 write(o);
186 };
T()187 operator T() const {
188 return read();
189 };
190 be_t<T> operator++() {
191 write(read() + 1);
192 return *this;
193 };
194 be_t<T> operator++(int) {
195 write(read() + 1);
196 return *this;
197 };
198 be_t<T> operator--() {
199 write(read() - 1);
200 return *this;
201 };
202 be_t<T> operator--(int) {
203 write(read() - 1);
204 return *this;
205 };
206 be_t<T>& operator+=(const T& t) {
207 write(read() + t);
208 return *this;
209 };
210 be_t<T>& operator-=(const T& t) {
211 write(read() - t);
212 return *this;
213 };
214 be_t<T>& operator&=(const be_t<T>& t) {
215 m &= t.m;
216 return *this;
217 };
218 be_t<T>& operator|=(const be_t<T>& t) {
219 m |= t.m;
220 return *this;
221 };
222 } __attribute__((packed));
223
224 /* Define types of native endianness similar to the little and big endian
225 * versions below. Not really necessary but useful occasionally to emphasize
226 * endianness of data.
227 */
228
229 typedef int8_t int8_ne_t;
230 typedef int16_t int16_ne_t;
231 typedef int32_t int32_ne_t;
232 typedef int64_t int64_ne_t;
233 typedef u_int8_t u_int8_ne_t;
234 typedef u_int16_t u_int16_ne_t;
235 typedef u_int32_t u_int32_ne_t;
236 typedef u_int64_t u_int64_ne_t;
237
238
239 /* The classes work on their native endianness as well, but obviously
240 * introduce some overhead. Use the faster typedefs to native types
241 * therefore, unless you're debugging.
242 */
243
244 #if BYTE_ORDER == LITTLE_ENDIAN
245 typedef int8_ne_t int8_le_t;
246 typedef int16_ne_t int16_le_t;
247 typedef int32_ne_t int32_le_t;
248 typedef int64_ne_t int64_le_t;
249 typedef u_int8_ne_t u_int8_le_t;
250 typedef u_int16_ne_t u_int16_le_t;
251 typedef u_int32_ne_t u_int32_le_t;
252 typedef u_int64_ne_t u_int64_le_t;
253 typedef int8_t int8_be_t;
254 typedef be_t<int16_t> int16_be_t;
255 typedef be_t<int32_t> int32_be_t;
256 typedef be_t<int64_t> int64_be_t;
257 typedef u_int8_t u_int8_be_t;
258 typedef be_t<u_int16_t> u_int16_be_t;
259 typedef be_t<u_int32_t> u_int32_be_t;
260 typedef be_t<u_int64_t> u_int64_be_t;
261 #else
262 typedef int8_ne_t int8_be_t;
263 typedef int16_ne_t int16_be_t;
264 typedef int32_ne_t int32_be_t;
265 typedef int64_ne_t int64_be_t;
266 typedef u_int8_ne_t u_int8_be_t;
267 typedef u_int16_ne_t u_int16_be_t;
268 typedef u_int32_ne_t u_int32_be_t;
269 typedef u_int64_ne_t u_int64_be_t;
270 typedef int8_t int8_le_t;
271 typedef le_t<int16_t> int16_le_t;
272 typedef le_t<int32_t> int32_le_t;
273 typedef le_t<int64_t> int64_le_t;
274 typedef u_int8_t u_int8_le_t;
275 typedef le_t<u_int16_t> u_int16_le_t;
276 typedef le_t<u_int32_t> u_int32_le_t;
277 typedef le_t<u_int64_t> u_int64_le_t;
278 #endif
279
280 #endif
281