forked from michaelforney/oscmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intpack.h
211 lines (177 loc) · 3.44 KB
/
intpack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* SPDX-License-Identifier: Unlicense */
#ifndef INTPACK_H
#define INTPACK_H
#include <stdint.h>
static inline void *
putle16(void *p, uint_least16_t v)
{
unsigned char *b = p;
b[0] = v & 0xff;
b[1] = v >> 8 & 0xff;
return b + 2;
}
static inline void *
putbe16(void *p, uint_least16_t v)
{
unsigned char *b = p;
b[0] = v >> 8 & 0xff;
b[1] = v & 0xff;
return b + 2;
}
static inline void *
putle24(void *p, uint_least32_t v)
{
unsigned char *b = p;
b[0] = v & 0xff;
b[1] = v >> 8 & 0xff;
b[2] = v >> 16 & 0xff;
return b + 3;
}
static inline void *
putbe24(void *p, uint_least32_t v)
{
unsigned char *b = p;
b[0] = v >> 16 & 0xff;
b[1] = v >> 8 & 0xff;
b[2] = v & 0xff;
return b + 4;
}
static inline void *
putle32(void *p, uint_least32_t v)
{
unsigned char *b = p;
b[0] = v & 0xff;
b[1] = v >> 8 & 0xff;
b[2] = v >> 16 & 0xff;
b[3] = v >> 24 & 0xff;
return b + 4;
}
static inline void *
putbe32(void *p, uint_least32_t v)
{
unsigned char *b = p;
b[0] = v >> 24 & 0xff;
b[1] = v >> 16 & 0xff;
b[2] = v >> 8 & 0xff;
b[3] = v & 0xff;
return b + 4;
}
static inline void *
putle64(void *p, uint_least64_t v)
{
unsigned char *b = p;
b[0] = v & 0xff;
b[1] = v >> 8 & 0xff;
b[2] = v >> 16 & 0xff;
b[3] = v >> 24 & 0xff;
b[4] = v >> 32 & 0xff;
b[5] = v >> 40 & 0xff;
b[6] = v >> 48 & 0xff;
b[7] = v >> 56 & 0xff;
return b + 8;
}
static inline void *
putbe64(void *p, uint_least64_t v)
{
unsigned char *b = p;
b[0] = v >> 56 & 0xff;
b[1] = v >> 48 & 0xff;
b[2] = v >> 40 & 0xff;
b[3] = v >> 32 & 0xff;
b[4] = v >> 24 & 0xff;
b[5] = v >> 16 & 0xff;
b[6] = v >> 8 & 0xff;
b[7] = v & 0xff;
return b + 8;
}
static inline uint_least16_t
getle16(const void *p)
{
const unsigned char *b = p;
uint_least16_t v;
v = b[0] & 0xffu;
v |= (b[1] & 0xffu) << 8;
return v;
}
static inline uint_least16_t
getbe16(const void *p)
{
const unsigned char *b = p;
uint_least16_t v;
v = (b[0] & 0xffu) << 8;
v |= b[1] & 0xffu;
return v;
}
static inline uint_least32_t
getle24(const void *p)
{
const unsigned char *b = p;
uint_least32_t v;
v = b[0] & 0xfful;
v |= (b[1] & 0xfful) << 8;
v |= (b[2] & 0xfful) << 16;
return v;
}
static inline uint_least32_t
getbe24(const void *p)
{
const unsigned char *b = p;
uint_least32_t v;
v = (b[0] & 0xfful) << 16;
v |= (b[1] & 0xfful) << 8;
v |= b[2] & 0xfful;
return v;
}
static inline uint_least32_t
getle32(const void *p)
{
const unsigned char *b = p;
uint_least32_t v;
v = b[0] & 0xfful;
v |= (b[1] & 0xfful) << 8;
v |= (b[2] & 0xfful) << 16;
v |= (b[3] & 0xfful) << 24;
return v;
}
static inline uint_least32_t
getbe32(const void *p)
{
const unsigned char *b = p;
uint_least32_t v;
v = (b[0] & 0xfful) << 24;
v |= (b[1] & 0xfful) << 16;
v |= (b[2] & 0xfful) << 8;
v |= b[3] & 0xfful;
return v;
}
static inline uint_least64_t
getle64(const void *p)
{
const unsigned char *b = p;
uint_least64_t v;
v = b[0] & 0xffull;
v |= (b[1] & 0xffull) << 8;
v |= (b[2] & 0xffull) << 16;
v |= (b[3] & 0xffull) << 24;
v |= (b[4] & 0xffull) << 32;
v |= (b[5] & 0xffull) << 40;
v |= (b[6] & 0xffull) << 48;
v |= (b[7] & 0xffull) << 56;
return v;
}
static inline uint_least64_t
getbe64(const void *p)
{
const unsigned char *b = p;
uint_least64_t v;
v = (b[0] & 0xffull) << 56;
v |= (b[1] & 0xffull) << 48;
v |= (b[2] & 0xffull) << 40;
v |= (b[3] & 0xffull) << 32;
v |= (b[4] & 0xffull) << 24;
v |= (b[5] & 0xffull) << 16;
v |= (b[6] & 0xffull) << 8;
v |= b[7] & 0xffull;
return v;
}
#endif