Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GStreamer | ||
2 | * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu> | ||
3 | * 2000 Wim Taymans <wtay@chello.be> | ||
4 | * 2002 Thomas Vander Stichele <thomas@apestaart.org> | ||
5 | * | ||
6 | * gstutils.h: Header for various utility functions | ||
7 | * | ||
8 | * This library is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Library General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * This library is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Library General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Library General Public | ||
19 | * License along with this library; if not, write to the | ||
20 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, | ||
21 | * Boston, MA 02110-1301, USA. | ||
22 | */ | ||
23 | |||
24 | #pragma once | ||
25 | |||
26 | #include <glib.h> | ||
27 | |||
28 | G_BEGIN_DECLS | ||
29 | |||
30 | /* Define PUT and GET functions for unaligned memory */ | ||
31 | #define _FP_GET(__data, __idx, __size, __shift) \ | ||
32 | (((guint##__size) (((const guint8 *) (__data))[__idx])) << (__shift)) | ||
33 | |||
34 | #define _FP_PUT(__data, __idx, __size, __shift, __num) \ | ||
35 | (((guint8 *) (__data))[__idx] = (((guint##__size) (__num)) >> (__shift)) & 0xff) | ||
36 | |||
37 | #ifndef __GTK_DOC_IGNORE__ | ||
38 | |||
39 | /** | ||
40 | * FP_READ_UINT64_BE: | ||
41 | * @data: memory location | ||
42 | * | ||
43 | * Read a 64 bit unsigned integer value in big endian format from the memory buffer. | ||
44 | */ | ||
45 | |||
46 | /** | ||
47 | * FP_READ_UINT64_LE: | ||
48 | * @data: memory location | ||
49 | * | ||
50 | * Read a 64 bit unsigned integer value in little endian format from the memory buffer. | ||
51 | */ | ||
52 | #define _FP_READ_UINT64_BE(data) (_FP_GET (data, 0, 64, 56) | \ | ||
53 | _FP_GET (data, 1, 64, 48) | \ | ||
54 | _FP_GET (data, 2, 64, 40) | \ | ||
55 | _FP_GET (data, 3, 64, 32) | \ | ||
56 | _FP_GET (data, 4, 64, 24) | \ | ||
57 | _FP_GET (data, 5, 64, 16) | \ | ||
58 | _FP_GET (data, 6, 64, 8) | \ | ||
59 | _FP_GET (data, 7, 64, 0)) | ||
60 | |||
61 | #define _FP_READ_UINT64_LE(data) (_FP_GET (data, 7, 64, 56) | \ | ||
62 | _FP_GET (data, 6, 64, 48) | \ | ||
63 | _FP_GET (data, 5, 64, 40) | \ | ||
64 | _FP_GET (data, 4, 64, 32) | \ | ||
65 | _FP_GET (data, 3, 64, 24) | \ | ||
66 | _FP_GET (data, 2, 64, 16) | \ | ||
67 | _FP_GET (data, 1, 64, 8) | \ | ||
68 | _FP_GET (data, 0, 64, 0)) | ||
69 | |||
70 | #define FP_READ_UINT64_BE(data) _fpi_slow_read64_be((const guint8 *)(data)) | ||
71 | ✗ | static inline guint64 _fpi_slow_read64_be (const guint8 * data) { | |
72 | ✗ | return _FP_READ_UINT64_BE (data); | |
73 | } | ||
74 | #define FP_READ_UINT64_LE(data) _fpi_slow_read64_le((const guint8 *)(data)) | ||
75 | ✗ | static inline guint64 _fpi_slow_read64_le (const guint8 * data) { | |
76 | ✗ | return _FP_READ_UINT64_LE (data); | |
77 | } | ||
78 | |||
79 | /** | ||
80 | * FP_READ_UINT32_BE: | ||
81 | * @data: memory location | ||
82 | * | ||
83 | * Read a 32 bit unsigned integer value in big endian format from the memory buffer. | ||
84 | */ | ||
85 | |||
86 | /** | ||
87 | * FP_READ_UINT32_LE: | ||
88 | * @data: memory location | ||
89 | * | ||
90 | * Read a 32 bit unsigned integer value in little endian format from the memory buffer. | ||
91 | */ | ||
92 | #define _FP_READ_UINT32_BE(data) (_FP_GET (data, 0, 32, 24) | \ | ||
93 | _FP_GET (data, 1, 32, 16) | \ | ||
94 | _FP_GET (data, 2, 32, 8) | \ | ||
95 | _FP_GET (data, 3, 32, 0)) | ||
96 | |||
97 | #define _FP_READ_UINT32_LE(data) (_FP_GET (data, 3, 32, 24) | \ | ||
98 | _FP_GET (data, 2, 32, 16) | \ | ||
99 | _FP_GET (data, 1, 32, 8) | \ | ||
100 | _FP_GET (data, 0, 32, 0)) | ||
101 | |||
102 | #define FP_READ_UINT32_BE(data) _fpi_slow_read32_be((const guint8 *)(data)) | ||
103 | ✗ | static inline guint32 _fpi_slow_read32_be (const guint8 * data) { | |
104 | ✗ | return _FP_READ_UINT32_BE (data); | |
105 | } | ||
106 | #define FP_READ_UINT32_LE(data) _fpi_slow_read32_le((const guint8 *)(data)) | ||
107 | 2 | static inline guint32 _fpi_slow_read32_le (const guint8 * data) { | |
108 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
2 | return _FP_READ_UINT32_LE (data); |
109 | } | ||
110 | |||
111 | /** | ||
112 | * FP_READ_UINT24_BE: | ||
113 | * @data: memory location | ||
114 | * | ||
115 | * Read a 24 bit unsigned integer value in big endian format from the memory buffer. | ||
116 | */ | ||
117 | #define _FP_READ_UINT24_BE(data) (_FP_GET (data, 0, 32, 16) | \ | ||
118 | _FP_GET (data, 1, 32, 8) | \ | ||
119 | _FP_GET (data, 2, 32, 0)) | ||
120 | |||
121 | #define FP_READ_UINT24_BE(data) _fpi_slow_read24_be((const guint8 *)(data)) | ||
122 | ✗ | static inline guint32 _fpi_slow_read24_be (const guint8 * data) { | |
123 | ✗ | return _FP_READ_UINT24_BE (data); | |
124 | } | ||
125 | |||
126 | /** | ||
127 | * FP_READ_UINT24_LE: | ||
128 | * @data: memory location | ||
129 | * | ||
130 | * Read a 24 bit unsigned integer value in little endian format from the memory buffer. | ||
131 | */ | ||
132 | #define _FP_READ_UINT24_LE(data) (_FP_GET (data, 2, 32, 16) | \ | ||
133 | _FP_GET (data, 1, 32, 8) | \ | ||
134 | _FP_GET (data, 0, 32, 0)) | ||
135 | |||
136 | #define FP_READ_UINT24_LE(data) _fpi_slow_read24_le((const guint8 *)(data)) | ||
137 | ✗ | static inline guint32 _fpi_slow_read24_le (const guint8 * data) { | |
138 | ✗ | return _FP_READ_UINT24_LE (data); | |
139 | } | ||
140 | |||
141 | /** | ||
142 | * FP_READ_UINT16_BE: | ||
143 | * @data: memory location | ||
144 | * | ||
145 | * Read a 16 bit unsigned integer value in big endian format from the memory buffer. | ||
146 | */ | ||
147 | /** | ||
148 | * FP_READ_UINT16_LE: | ||
149 | * @data: memory location | ||
150 | * | ||
151 | * Read a 16 bit unsigned integer value in little endian format from the memory buffer. | ||
152 | */ | ||
153 | #define _FP_READ_UINT16_BE(data) (_FP_GET (data, 0, 16, 8) | \ | ||
154 | _FP_GET (data, 1, 16, 0)) | ||
155 | |||
156 | #define _FP_READ_UINT16_LE(data) (_FP_GET (data, 1, 16, 8) | \ | ||
157 | _FP_GET (data, 0, 16, 0)) | ||
158 | |||
159 | #define FP_READ_UINT16_BE(data) _fpi_slow_read16_be((const guint8 *)(data)) | ||
160 | 7195 | static inline guint16 _fpi_slow_read16_be (const guint8 * data) { | |
161 | 7195 | return _FP_READ_UINT16_BE (data); | |
162 | } | ||
163 | #define FP_READ_UINT16_LE(data) _fpi_slow_read16_le((const guint8 *)(data)) | ||
164 | 2 | static inline guint16 _fpi_slow_read16_le (const guint8 * data) { | |
165 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
2 | return _FP_READ_UINT16_LE (data); |
166 | } | ||
167 | |||
168 | /** | ||
169 | * FP_READ_UINT8: | ||
170 | * @data: memory location | ||
171 | * | ||
172 | * Read an 8 bit unsigned integer value from the memory buffer. | ||
173 | */ | ||
174 | #define FP_READ_UINT8(data) (_FP_GET (data, 0, 8, 0)) | ||
175 | |||
176 | /** | ||
177 | * FP_WRITE_UINT64_BE: | ||
178 | * @data: memory location | ||
179 | * @val: value to store | ||
180 | * | ||
181 | * Store a 64 bit unsigned integer value in big endian format into the memory buffer. | ||
182 | */ | ||
183 | /** | ||
184 | * FP_WRITE_UINT64_LE: | ||
185 | * @data: memory location | ||
186 | * @val: value to store | ||
187 | * | ||
188 | * Store a 64 bit unsigned integer value in little endian format into the memory buffer. | ||
189 | */ | ||
190 | #define FP_WRITE_UINT64_BE(data,val) do { \ | ||
191 | gpointer __put_data = data; \ | ||
192 | guint64 __put_val = val; \ | ||
193 | _FP_PUT (__put_data, 0, 64, 56, __put_val); \ | ||
194 | _FP_PUT (__put_data, 1, 64, 48, __put_val); \ | ||
195 | _FP_PUT (__put_data, 2, 64, 40, __put_val); \ | ||
196 | _FP_PUT (__put_data, 3, 64, 32, __put_val); \ | ||
197 | _FP_PUT (__put_data, 4, 64, 24, __put_val); \ | ||
198 | _FP_PUT (__put_data, 5, 64, 16, __put_val); \ | ||
199 | _FP_PUT (__put_data, 6, 64, 8, __put_val); \ | ||
200 | _FP_PUT (__put_data, 7, 64, 0, __put_val); \ | ||
201 | } while (0) | ||
202 | |||
203 | #define FP_WRITE_UINT64_LE(data,val) do { \ | ||
204 | gpointer __put_data = data; \ | ||
205 | guint64 __put_val = val; \ | ||
206 | _FP_PUT (__put_data, 0, 64, 0, __put_val); \ | ||
207 | _FP_PUT (__put_data, 1, 64, 8, __put_val); \ | ||
208 | _FP_PUT (__put_data, 2, 64, 16, __put_val); \ | ||
209 | _FP_PUT (__put_data, 3, 64, 24, __put_val); \ | ||
210 | _FP_PUT (__put_data, 4, 64, 32, __put_val); \ | ||
211 | _FP_PUT (__put_data, 5, 64, 40, __put_val); \ | ||
212 | _FP_PUT (__put_data, 6, 64, 48, __put_val); \ | ||
213 | _FP_PUT (__put_data, 7, 64, 56, __put_val); \ | ||
214 | } while (0) | ||
215 | |||
216 | /** | ||
217 | * FP_WRITE_UINT32_BE: | ||
218 | * @data: memory location | ||
219 | * @val: value to store | ||
220 | * | ||
221 | * Store a 32 bit unsigned integer value in big endian format into the memory buffer. | ||
222 | */ | ||
223 | /** | ||
224 | * FP_WRITE_UINT32_LE: | ||
225 | * @data: memory location | ||
226 | * @val: value to store | ||
227 | * | ||
228 | * Store a 32 bit unsigned integer value in little endian format into the memory buffer. | ||
229 | */ | ||
230 | #define FP_WRITE_UINT32_BE(data,val) do { \ | ||
231 | gpointer __put_data = data; \ | ||
232 | guint32 __put_val = val; \ | ||
233 | _FP_PUT (__put_data, 0, 32, 24, __put_val); \ | ||
234 | _FP_PUT (__put_data, 1, 32, 16, __put_val); \ | ||
235 | _FP_PUT (__put_data, 2, 32, 8, __put_val); \ | ||
236 | _FP_PUT (__put_data, 3, 32, 0, __put_val); \ | ||
237 | } while (0) | ||
238 | |||
239 | #define FP_WRITE_UINT32_LE(data,val) do { \ | ||
240 | gpointer __put_data = data; \ | ||
241 | guint32 __put_val = val; \ | ||
242 | _FP_PUT (__put_data, 0, 32, 0, __put_val); \ | ||
243 | _FP_PUT (__put_data, 1, 32, 8, __put_val); \ | ||
244 | _FP_PUT (__put_data, 2, 32, 16, __put_val); \ | ||
245 | _FP_PUT (__put_data, 3, 32, 24, __put_val); \ | ||
246 | } while (0) | ||
247 | |||
248 | /** | ||
249 | * FP_WRITE_UINT24_BE: | ||
250 | * @data: memory location | ||
251 | * @num: value to store | ||
252 | * | ||
253 | * Store a 24 bit unsigned integer value in big endian format into the memory buffer. | ||
254 | */ | ||
255 | #define FP_WRITE_UINT24_BE(data, num) do { \ | ||
256 | gpointer __put_data = data; \ | ||
257 | guint32 __put_val = num; \ | ||
258 | _FP_PUT (__put_data, 0, 32, 16, __put_val); \ | ||
259 | _FP_PUT (__put_data, 1, 32, 8, __put_val); \ | ||
260 | _FP_PUT (__put_data, 2, 32, 0, __put_val); \ | ||
261 | } while (0) | ||
262 | |||
263 | /** | ||
264 | * FP_WRITE_UINT24_LE: | ||
265 | * @data: memory location | ||
266 | * @num: value to store | ||
267 | * | ||
268 | * Store a 24 bit unsigned integer value in little endian format into the memory buffer. | ||
269 | */ | ||
270 | #define FP_WRITE_UINT24_LE(data, num) do { \ | ||
271 | gpointer __put_data = data; \ | ||
272 | guint32 __put_val = num; \ | ||
273 | _FP_PUT (__put_data, 0, 32, 0, __put_val); \ | ||
274 | _FP_PUT (__put_data, 1, 32, 8, __put_val); \ | ||
275 | _FP_PUT (__put_data, 2, 32, 16, __put_val); \ | ||
276 | } while (0) | ||
277 | |||
278 | /** | ||
279 | * FP_WRITE_UINT16_BE: | ||
280 | * @data: memory location | ||
281 | * @val: value to store | ||
282 | * | ||
283 | * Store a 16 bit unsigned integer value in big endian format into the memory buffer. | ||
284 | */ | ||
285 | /** | ||
286 | * FP_WRITE_UINT16_LE: | ||
287 | * @data: memory location | ||
288 | * @val: value to store | ||
289 | * | ||
290 | * Store a 16 bit unsigned integer value in little endian format into the memory buffer. | ||
291 | */ | ||
292 | #define FP_WRITE_UINT16_BE(data,val) do { \ | ||
293 | gpointer __put_data = data; \ | ||
294 | guint16 __put_val = val; \ | ||
295 | _FP_PUT (__put_data, 0, 16, 8, __put_val); \ | ||
296 | _FP_PUT (__put_data, 1, 16, 0, __put_val); \ | ||
297 | } while (0) | ||
298 | |||
299 | #define FP_WRITE_UINT16_LE(data,val) do { \ | ||
300 | gpointer __put_data = data; \ | ||
301 | guint16 __put_val = val; \ | ||
302 | _FP_PUT (__put_data, 0, 16, 0, __put_val); \ | ||
303 | _FP_PUT (__put_data, 1, 16, 8, __put_val); \ | ||
304 | } while (0) | ||
305 | |||
306 | /** | ||
307 | * FP_WRITE_UINT8: | ||
308 | * @data: memory location | ||
309 | * @num: value to store | ||
310 | * | ||
311 | * Store an 8 bit unsigned integer value into the memory buffer. | ||
312 | */ | ||
313 | #define FP_WRITE_UINT8(data, num) do { \ | ||
314 | _FP_PUT (data, 0, 8, 0, num); \ | ||
315 | } while (0) | ||
316 | |||
317 | /* Float endianness conversion macros */ | ||
318 | |||
319 | /** | ||
320 | * FP_READ_FLOAT_LE: | ||
321 | * @data: memory location | ||
322 | * | ||
323 | * Read a 32 bit float value in little endian format from the memory buffer. | ||
324 | * | ||
325 | * Returns: The floating point value read from @data | ||
326 | */ | ||
327 | static inline gfloat | ||
328 | ✗ | FP_READ_FLOAT_LE(const guint8 *data) | |
329 | { | ||
330 | ✗ | union | |
331 | { | ||
332 | guint32 i; | ||
333 | gfloat f; | ||
334 | } u; | ||
335 | |||
336 | ✗ | u.i = FP_READ_UINT32_LE (data); | |
337 | ✗ | return u.f; | |
338 | } | ||
339 | |||
340 | /** | ||
341 | * FP_READ_FLOAT_BE: | ||
342 | * @data: memory location | ||
343 | * | ||
344 | * Read a 32 bit float value in big endian format from the memory buffer. | ||
345 | * | ||
346 | * Returns: The floating point value read from @data | ||
347 | */ | ||
348 | static inline gfloat | ||
349 | ✗ | FP_READ_FLOAT_BE(const guint8 *data) | |
350 | { | ||
351 | ✗ | union | |
352 | { | ||
353 | guint32 i; | ||
354 | gfloat f; | ||
355 | } u; | ||
356 | |||
357 | ✗ | u.i = FP_READ_UINT32_BE (data); | |
358 | ✗ | return u.f; | |
359 | } | ||
360 | |||
361 | /** | ||
362 | * FP_READ_DOUBLE_LE: | ||
363 | * @data: memory location | ||
364 | * | ||
365 | * Read a 64 bit double value in little endian format from the memory buffer. | ||
366 | * | ||
367 | * Returns: The double-precision floating point value read from @data | ||
368 | */ | ||
369 | static inline gdouble | ||
370 | ✗ | FP_READ_DOUBLE_LE(const guint8 *data) | |
371 | { | ||
372 | ✗ | union | |
373 | { | ||
374 | guint64 i; | ||
375 | gdouble d; | ||
376 | } u; | ||
377 | |||
378 | ✗ | u.i = FP_READ_UINT64_LE (data); | |
379 | ✗ | return u.d; | |
380 | } | ||
381 | |||
382 | /** | ||
383 | * FP_READ_DOUBLE_BE: | ||
384 | * @data: memory location | ||
385 | * | ||
386 | * Read a 64 bit double value in big endian format from the memory buffer. | ||
387 | * | ||
388 | * Returns: The double-precision floating point value read from @data | ||
389 | */ | ||
390 | static inline gdouble | ||
391 | ✗ | FP_READ_DOUBLE_BE(const guint8 *data) | |
392 | { | ||
393 | ✗ | union | |
394 | { | ||
395 | guint64 i; | ||
396 | gdouble d; | ||
397 | } u; | ||
398 | |||
399 | ✗ | u.i = FP_READ_UINT64_BE (data); | |
400 | ✗ | return u.d; | |
401 | } | ||
402 | |||
403 | /** | ||
404 | * FP_WRITE_FLOAT_LE: | ||
405 | * @data: memory location | ||
406 | * @num: value to store | ||
407 | * | ||
408 | * Store a 32 bit float value in little endian format into the memory buffer. | ||
409 | */ | ||
410 | static inline void | ||
411 | ✗ | FP_WRITE_FLOAT_LE(guint8 *data, gfloat num) | |
412 | { | ||
413 | ✗ | union | |
414 | { | ||
415 | guint32 i; | ||
416 | gfloat f; | ||
417 | } u; | ||
418 | |||
419 | ✗ | u.f = num; | |
420 | ✗ | FP_WRITE_UINT32_LE (data, u.i); | |
421 | } | ||
422 | |||
423 | /** | ||
424 | * FP_WRITE_FLOAT_BE: | ||
425 | * @data: memory location | ||
426 | * @num: value to store | ||
427 | * | ||
428 | * Store a 32 bit float value in big endian format into the memory buffer. | ||
429 | */ | ||
430 | static inline void | ||
431 | ✗ | FP_WRITE_FLOAT_BE(guint8 *data, gfloat num) | |
432 | { | ||
433 | ✗ | union | |
434 | { | ||
435 | guint32 i; | ||
436 | gfloat f; | ||
437 | } u; | ||
438 | |||
439 | ✗ | u.f = num; | |
440 | ✗ | FP_WRITE_UINT32_BE (data, u.i); | |
441 | } | ||
442 | |||
443 | /** | ||
444 | * FP_WRITE_DOUBLE_LE: | ||
445 | * @data: memory location | ||
446 | * @num: value to store | ||
447 | * | ||
448 | * Store a 64 bit double value in little endian format into the memory buffer. | ||
449 | */ | ||
450 | static inline void | ||
451 | ✗ | FP_WRITE_DOUBLE_LE(guint8 *data, gdouble num) | |
452 | { | ||
453 | ✗ | union | |
454 | { | ||
455 | guint64 i; | ||
456 | gdouble d; | ||
457 | } u; | ||
458 | |||
459 | ✗ | u.d = num; | |
460 | ✗ | FP_WRITE_UINT64_LE (data, u.i); | |
461 | ✗ | } | |
462 | |||
463 | /** | ||
464 | * FP_WRITE_DOUBLE_BE: | ||
465 | * @data: memory location | ||
466 | * @num: value to store | ||
467 | * | ||
468 | * Store a 64 bit double value in big endian format into the memory buffer. | ||
469 | */ | ||
470 | static inline void | ||
471 | ✗ | FP_WRITE_DOUBLE_BE(guint8 *data, gdouble num) | |
472 | { | ||
473 | ✗ | union | |
474 | { | ||
475 | guint64 i; | ||
476 | gdouble d; | ||
477 | } u; | ||
478 | |||
479 | ✗ | u.d = num; | |
480 | ✗ | FP_WRITE_UINT64_BE (data, u.i); | |
481 | ✗ | } | |
482 | |||
483 | G_END_DECLS | ||
484 | |||
485 | #endif /* __GTK_DOC_IGNORE__ */ | ||
486 |