mp4sys: Handle extra bytes within descriptors.
[L-SMASH.git] / common / memint.h
blob3f0b6c08af83027cfeedd350e6b115c664871116
1 /*****************************************************************************
2 * memint.h
3 *****************************************************************************
4 * Copyright (C) 2014-2015 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 /*---- memory writers ----*/
24 #define LSMASH_SET_BYTE( p, x ) \
25 do \
26 { \
27 ((uint8_t *)(p))[0] = (x); \
28 } while( 0 )
29 #define LSMASH_SET_BE16( p, x ) \
30 do \
31 { \
32 ((uint8_t *)(p))[0] = (x) >> 8; \
33 ((uint8_t *)(p))[1] = (x); \
34 } while( 0 )
35 #define LSMASH_SET_BE24( p, x ) \
36 do \
37 { \
38 ((uint8_t *)(p))[0] = (x) >> 16; \
39 ((uint8_t *)(p))[1] = (x) >> 8; \
40 ((uint8_t *)(p))[2] = (x); \
41 } while( 0 )
42 #define LSMASH_SET_BE32( p, x ) \
43 do \
44 { \
45 ((uint8_t *)(p))[0] = (x) >> 24; \
46 ((uint8_t *)(p))[1] = (x) >> 16; \
47 ((uint8_t *)(p))[2] = (x) >> 8; \
48 ((uint8_t *)(p))[3] = (x); \
49 } while( 0 )
50 #define LSMASH_SET_BE64( p, x ) \
51 do \
52 { \
53 ((uint8_t *)(p))[0] = (x) >> 56; \
54 ((uint8_t *)(p))[1] = (x) >> 48; \
55 ((uint8_t *)(p))[2] = (x) >> 40; \
56 ((uint8_t *)(p))[3] = (x) >> 32; \
57 ((uint8_t *)(p))[4] = (x) >> 24; \
58 ((uint8_t *)(p))[5] = (x) >> 16; \
59 ((uint8_t *)(p))[6] = (x) >> 8; \
60 ((uint8_t *)(p))[7] = (x); \
61 } while( 0 )
62 #define LSMASH_SET_LE16( p, x ) \
63 do \
64 { \
65 ((uint8_t *)(p))[0] = (x); \
66 ((uint8_t *)(p))[1] = (x) >> 8; \
67 } while( 0 )
68 #define LSMASH_SET_LE32( p, x ) \
69 do \
70 { \
71 ((uint8_t *)(p))[0] = (x); \
72 ((uint8_t *)(p))[1] = (x) >> 8; \
73 ((uint8_t *)(p))[2] = (x) >> 16; \
74 ((uint8_t *)(p))[3] = (x) >> 24; \
75 } while( 0 )
77 /*---- memory readers ----*/
78 #define LSMASH_GET_BYTE( p ) \
79 (((const uint8_t *)(p))[0])
80 #define LSMASH_GET_BE16( p ) \
81 (((uint16_t)((const uint8_t *)(p))[0] << 8) \
82 | ((uint16_t)((const uint8_t *)(p))[1]))
83 #define LSMASH_GET_BE24( p ) \
84 (((uint32_t)((const uint8_t *)(p))[0] << 16) \
85 | ((uint32_t)((const uint8_t *)(p))[1] << 8) \
86 | ((uint32_t)((const uint8_t *)(p))[2]))
87 #define LSMASH_GET_BE32( p ) \
88 (((uint32_t)((const uint8_t *)(p))[0] << 24) \
89 | ((uint32_t)((const uint8_t *)(p))[1] << 16) \
90 | ((uint32_t)((const uint8_t *)(p))[2] << 8) \
91 | ((uint32_t)((const uint8_t *)(p))[3]))
92 #define LSMASH_GET_BE64( p ) \
93 (((uint64_t)((const uint8_t *)(p))[0] << 56) \
94 | ((uint64_t)((const uint8_t *)(p))[1] << 48) \
95 | ((uint64_t)((const uint8_t *)(p))[2] << 40) \
96 | ((uint64_t)((const uint8_t *)(p))[3] << 32) \
97 | ((uint64_t)((const uint8_t *)(p))[4] << 24) \
98 | ((uint64_t)((const uint8_t *)(p))[5] << 16) \
99 | ((uint64_t)((const uint8_t *)(p))[6] << 8) \
100 | ((uint64_t)((const uint8_t *)(p))[7]))
101 #define LSMASH_GET_LE16( p ) \
102 (((uint16_t)((const uint8_t *)(p))[0]) \
103 | ((uint16_t)((const uint8_t *)(p))[1] << 8))
104 #define LSMASH_GET_LE32( p ) \
105 (((uint32_t)((const uint8_t *)(p))[0]) \
106 | ((uint32_t)((const uint8_t *)(p))[1] << 8) \
107 | ((uint32_t)((const uint8_t *)(p))[2] << 16) \
108 | ((uint32_t)((const uint8_t *)(p))[3] << 24))