Merge ../mlt
[melted] / src / modules / kino / endian_types.h
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #ifndef _ENDIAN_TYPES_H
28 #define _ENDIAN_TYPES_H
29
30 /* Needed for BYTE_ORDER and BIG/LITTLE_ENDIAN macros. */
31 #ifndef _BSD_SOURCE
32 # define _BSD_SOURCE
33 #ifndef __FreeBSD__
34 # include <endian.h>
35 #else
36 # include <sys/endian.h>
37 #endif /* __FreeBSD__ */
38 # undef _BSD_SOURCE
39 #else
40 #ifndef __FreeBSD__
41 # include <endian.h>
42 #else
43 # include <sys/endian.h>
44 #endif /* __FreeBSD__ */
45 #endif
46
47 #include <sys/types.h>
48 #ifndef __FreeBSD__
49 #include <byteswap.h>
50 #else
51 #define bswap_16(x) bswap16(x)
52 #define bswap_32(x) bswap32(x)
53 #define bswap_64(x) bswap64(x)
54 #endif /* __FreeBSD__ */
55
56 static inline int8_t bswap(const int8_t& x)
57 {
58 return x;
59 }
60
61 static inline u_int8_t bswap(const u_int8_t& x)
62 {
63 return x;
64 }
65
66 static inline int16_t bswap(const int16_t& x)
67 {
68 return bswap_16(x);
69 }
70
71 static inline u_int16_t bswap(const u_int16_t& x)
72 {
73 return bswap_16(x);
74 }
75
76 static inline int32_t bswap(const int32_t& x)
77 {
78 return bswap_32(x);
79 }
80
81 static inline u_int32_t bswap(const u_int32_t& x)
82 {
83 return bswap_32(x);
84 }
85
86 static inline int64_t bswap(const int64_t& x)
87 {
88 return bswap_64(x);
89 }
90
91 static inline u_int64_t bswap(const u_int64_t& x)
92 {
93 return bswap_64(x);
94 }
95
96 #define le_to_cpu cpu_to_le
97 #define be_to_cpu cpu_to_be
98
99 template <class T> static inline T cpu_to_le(const T& x)
100 {
101 #if BYTE_ORDER == LITTLE_ENDIAN
102 return x;
103 #else
104 return bswap(x);
105 #endif
106 }
107
108 template <class T> static inline T cpu_to_be(const T& x)
109 {
110 #if BYTE_ORDER == LITTLE_ENDIAN
111 return bswap(x);
112 #else
113 return x;
114 #endif
115 }
116
117 template <class T> class le_t {
118 T m;
119 T read() const {
120 return le_to_cpu(m);
121 };
122 void write(const T& n) {
123 m = cpu_to_le(n);
124 };
125 public:
126 le_t(void) {
127 m = 0;
128 };
129 le_t(const T& o) {
130 write(o);
131 };
132 operator T() const {
133 return read();
134 };
135 le_t<T> operator++() {
136 write(read() + 1);
137 return *this;
138 };
139 le_t<T> operator++(int) {
140 write(read() + 1);
141 return *this;
142 };
143 le_t<T> operator--() {
144 write(read() - 1);
145 return *this;
146 };
147 le_t<T> operator--(int) {
148 write(read() - 1);
149 return *this;
150 };
151 le_t<T>& operator+=(const T& t) {
152 write(read() + t);
153 return *this;
154 };
155 le_t<T>& operator-=(const T& t) {
156 write(read() - t);
157 return *this;
158 };
159 le_t<T>& operator&=(const le_t<T>& t) {
160 m &= t.m;
161 return *this;
162 };
163 le_t<T>& operator|=(const le_t<T>& t) {
164 m |= t.m;
165 return *this;
166 };
167 } __attribute__((packed));
168
169 /* Just copy-and-pasted from le_t. Too lazy to do it right. */
170
171 template <class T> class be_t {
172 T m;
173 T read() const {
174 return be_to_cpu(m);
175 };
176 void write(const T& n) {
177 m = cpu_to_be(n);
178 };
179 public:
180 be_t(void) {
181 m = 0;
182 };
183 be_t(const T& o) {
184 write(o);
185 };
186 operator T() const {
187 return read();
188 };
189 be_t<T> operator++() {
190 write(read() + 1);
191 return *this;
192 };
193 be_t<T> operator++(int) {
194 write(read() + 1);
195 return *this;
196 };
197 be_t<T> operator--() {
198 write(read() - 1);
199 return *this;
200 };
201 be_t<T> operator--(int) {
202 write(read() - 1);
203 return *this;
204 };
205 be_t<T>& operator+=(const T& t) {
206 write(read() + t);
207 return *this;
208 };
209 be_t<T>& operator-=(const T& t) {
210 write(read() - t);
211 return *this;
212 };
213 be_t<T>& operator&=(const be_t<T>& t) {
214 m &= t.m;
215 return *this;
216 };
217 be_t<T>& operator|=(const be_t<T>& t) {
218 m |= t.m;
219 return *this;
220 };
221 } __attribute__((packed));
222
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.
226 */
227
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;
236
237
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.
241 */
242
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;
260 #else
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;
277 #endif
278
279 #endif