-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawint.h
288 lines (257 loc) · 6.27 KB
/
rawint.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#ifndef __SWFHAX_RAWINT_H
#define __SWFHAX_RAWINT_H
#ifndef _BSD_SOURCE
#pragma warning You need to define _BSD_SOURCE
#define _BSD_SOURCE
#endif
#include <stdint.h>
#include <endian.h>
/**
* Reads a little endian 8 bit value in the buffer, to an unsigned 8 bit int
*
* @param p the buffer
* @return the int
**/
static inline uint8_t __le_u8(const void *p) {
return *((uint8_t*)(p));
}
/**
* Reads a little endian 16 bit value in the buffer, to an unsigned 16 bit int
*
* @param p the buffer
* @return the int
**/
static inline uint16_t __le_u16(const void *p) {
return *((uint16_t*)(p));
}
/**
* Reads a little endian 8 bit value in the buffer, to a signed 8 bit int
*
* @param p the buffer
* @return the int
**/
static inline int8_t __le_s8(const void *p) {
return *((int8_t*)(p));
}
/**
* Reads a little endian 8 bit value in the buffer, to a signed 16 bit int
*
* @param p the buffer
* @return the int
**/
static inline int16_t __le_s16(const void *p) {
return *((int16_t*)(p));
}
/**
* Write a little endian 16 bit unsigned int into the buffer
*
* @param p the buffer
* @data 24 bit data to write
**/
static inline void __w_le_u16(void *p,const uint16_t val) {
*((uint16_t*)(p)) = val;
}
/**
* Write a big endian 16 bit unsigned int into the buffer
*
* @param p the buffer
* @val 24 bit data to write
**/
static inline void __w_be_u16(void *p,const uint16_t val) {
((uint8_t*)(p))[0] = val >> 8UL;
((uint8_t*)(p))[1] = val;
}
/**
* Reads a little endian 32 bit value in the buffer to a unsigned 24 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint32_t __le_u32(const void *p) {
return *((uint32_t*)(p));
}
/**
* Reads a little endian 32 bit value in the buffer to a unsigned 32 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline int32_t __le_s32(const void *p) {
return *((int32_t*)(p));
}
/**
* Write a little endian 32 bit unsigned int into the buffer
*
* @param p the buffer
* @val 24 bit data to write
**/
static inline void __w_le_u32(void *p,const uint32_t val) {
*((uint32_t*)(p)) = val;
}
/**
* Write a big endian 32 bit unsigned int into the buffer
*
* @param p the buffer
* @val 24 bit data to write
**/
static inline void __w_be_u32(void *p,const uint32_t val) {
((uint8_t*)(p))[0] = val >> 24UL;
((uint8_t*)(p))[1] = val >> 16UL;
((uint8_t*)(p))[2] = val >> 8UL;
((uint8_t*)(p))[3] = val;
}
/**
* Write a little endian 64 bit unsigned int into the buffer
*
* @param p the buffer
* @data 24 bit data to write
**/
static inline uint64_t __le_u64(const void *p) {
return *((const uint64_t*)(p));
}
/**
* Reads a little endian 64 bit value in the buffer to a signed bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline int64_t __le_s64(const void *p) {
return *((const int64_t*)(p));
}
/**
* Write a little endian 64 bit unsigned int into the buffer
*
* @param p the buffer
* @val 64 bit data to write
**/
static inline void __w_le_u64(void *p,const uint64_t val) {
*((uint64_t*)(p)) = val;
}
/**
* Reads a big endian 16 bit value in the buffer to a unsigned 16 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint16_t __be_u16(const void *p) {
const unsigned char *c = (const unsigned char *)p;
return
(((uint16_t)c[0]) << 8U) |
(((uint16_t)c[1]) << 0U);
}
/**
* Reads a big endian 16 bit value in the buffer to a signed 16 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline int16_t __be_s16(const void *p) {
return (int16_t)__be_u16(p);
}
/**
* Reads a big endian 32 bit value in the buffer to a unsigned 32 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint32_t __be_u32(const void *p) {
const unsigned char *c = (const unsigned char *)p;
return
(((uint32_t)c[0]) << 24U) |
(((uint32_t)c[1]) << 16U) |
(((uint32_t)c[2]) << 8U) |
(((uint32_t)c[3]) << 0U);
}
/**
* Reads a big endian 32 bit value in the buffer to a signed 32 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline int32_t __be_s32(const void *p) {
return (int32_t)__be_u32(p);
}
/**
* Reads a big endian 64 bit value in the buffer to an unsigned 64 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint64_t __be_u64(const void *p) {
const unsigned char *c = (const unsigned char *)p;
return
(((uint64_t)c[0]) << 56ULL) |
(((uint64_t)c[1]) << 48ULL) |
(((uint64_t)c[2]) << 40ULL) |
(((uint64_t)c[3]) << 32ULL) |
(((uint64_t)c[4]) << 24ULL) |
(((uint64_t)c[5]) << 16ULL) |
(((uint64_t)c[6]) << 8ULL) |
(((uint64_t)c[7]) << 0ULL);
}
/**
* Reads a big endian 64 bit value in the buffer to a signed 64 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline int64_t __be_s64(const void *p) {
return (int64_t)__be_u64(p);
}
/**
* Write a little endian 64 bit unsigned int into the buffer
*
* @param p the buffer
* @val 64 bit data to write
**/
static inline void __w_be_u64(void *p,const uint64_t val) {
((uint8_t*)(p))[0] = val >> 56ULL;
((uint8_t*)(p))[1] = val >> 48ULL;
((uint8_t*)(p))[2] = val >> 40ULL;
((uint8_t*)(p))[3] = val >> 32ULL;
((uint8_t*)(p))[4] = val >> 24UL;
((uint8_t*)(p))[5] = val >> 16UL;
((uint8_t*)(p))[6] = val >> 8UL;
((uint8_t*)(p))[7] = val;
}
/**
* Reads a little endian value in the buffer to a signed 32 bit int
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint16_t __be_to_he_16(uint16_t val) {
return be16toh(val);
}
/**
* alias for htobe16 (host endian to big endian 16)
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint16_t __he_to_be_16(uint16_t val) {
return htobe16(val);
}
/**
* alias for be32toh (big endian to host endian 32)
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint32_t __be_to_he_32(uint32_t val) {
return be32toh(val);
}
/**
* alias for htobe32 (host endian to big endian 32)
*
* @param p the buffer
* @return the unsigned int
**/
static inline uint32_t __he_to_be_32(uint32_t val) {
return htobe32(val);
}
#endif /* __SWFHAX_RAWINT_H */
/* vim: set tabstop=4 */
/* vim: set softtabstop=4 */
/* vim: set shiftwidth=4 */
/* vim: set expandtab */