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