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