|
26 | 26 | #define CUTILS_H
|
27 | 27 |
|
28 | 28 | #include <stdlib.h>
|
| 29 | +#include <string.h> |
29 | 30 | #include <inttypes.h>
|
30 | 31 |
|
31 | 32 | /* set if CPU is big endian */
|
@@ -82,14 +83,6 @@ static void *__builtin_frame_address(unsigned int level) {
|
82 | 83 | # define FORMAT_STRING(p) p
|
83 | 84 | #endif /* _MSC_VER */
|
84 | 85 |
|
85 |
| -// https://stackoverflow.com/a/3312896 |
86 |
| -// https://stackoverflow.com/a/3312896 |
87 |
| -#if defined(__GNUC__) || defined(__clang__) // GCC, clang, clang-cl, and so on but not MSVC |
88 |
| -# define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__)) |
89 |
| -#elif defined(_MSC_VER) && !defined(__clang__) // MSVC |
90 |
| -# define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop)) |
91 |
| -#endif |
92 |
| - |
93 | 86 | #if defined(_MSC_VER) && !defined(__clang__)
|
94 | 87 | #include <math.h>
|
95 | 88 | #define INF INFINITY
|
@@ -228,67 +221,61 @@ static inline int ctz64(uint64_t a)
|
228 | 221 | #endif
|
229 | 222 | }
|
230 | 223 |
|
231 |
| -PACK( |
232 |
| - struct packed_u64 { |
233 |
| - uint64_t v; |
234 |
| - } |
235 |
| -); |
236 |
| - |
237 |
| -PACK( |
238 |
| - struct packed_u32 { |
239 |
| - uint32_t v; |
240 |
| - } |
241 |
| -); |
242 |
| - |
243 |
| -PACK( |
244 |
| - struct packed_u16 { |
245 |
| - uint16_t v; |
246 |
| - } |
247 |
| -); |
248 |
| - |
249 | 224 | static inline uint64_t get_u64(const uint8_t *tab)
|
250 | 225 | {
|
251 |
| - return ((const struct packed_u64 *)tab)->v; |
| 226 | + uint64_t v; |
| 227 | + memcpy(&v, tab, sizeof(v)); |
| 228 | + return v; |
252 | 229 | }
|
253 | 230 |
|
254 | 231 | static inline int64_t get_i64(const uint8_t *tab)
|
255 | 232 | {
|
256 |
| - return (int64_t)((const struct packed_u64 *)tab)->v; |
| 233 | + int64_t v; |
| 234 | + memcpy(&v, tab, sizeof(v)); |
| 235 | + return v; |
257 | 236 | }
|
258 | 237 |
|
259 | 238 | static inline void put_u64(uint8_t *tab, uint64_t val)
|
260 | 239 | {
|
261 |
| - ((struct packed_u64 *)tab)->v = val; |
| 240 | + memcpy(tab, &val, sizeof(val)); |
262 | 241 | }
|
263 | 242 |
|
264 | 243 | static inline uint32_t get_u32(const uint8_t *tab)
|
265 | 244 | {
|
266 |
| - return ((const struct packed_u32 *)tab)->v; |
| 245 | + uint32_t v; |
| 246 | + memcpy(&v, tab, sizeof(v)); |
| 247 | + return v; |
267 | 248 | }
|
268 | 249 |
|
269 | 250 | static inline int32_t get_i32(const uint8_t *tab)
|
270 | 251 | {
|
271 |
| - return (int32_t)((const struct packed_u32 *)tab)->v; |
| 252 | + int32_t v; |
| 253 | + memcpy(&v, tab, sizeof(v)); |
| 254 | + return v; |
272 | 255 | }
|
273 | 256 |
|
274 | 257 | static inline void put_u32(uint8_t *tab, uint32_t val)
|
275 | 258 | {
|
276 |
| - ((struct packed_u32 *)tab)->v = val; |
| 259 | + memcpy(tab, &val, sizeof(val)); |
277 | 260 | }
|
278 | 261 |
|
279 | 262 | static inline uint32_t get_u16(const uint8_t *tab)
|
280 | 263 | {
|
281 |
| - return ((const struct packed_u16 *)tab)->v; |
| 264 | + uint16_t v; |
| 265 | + memcpy(&v, tab, sizeof(v)); |
| 266 | + return v; |
282 | 267 | }
|
283 | 268 |
|
284 | 269 | static inline int32_t get_i16(const uint8_t *tab)
|
285 | 270 | {
|
286 |
| - return (int16_t)((const struct packed_u16 *)tab)->v; |
| 271 | + int16_t v; |
| 272 | + memcpy(&v, tab, sizeof(v)); |
| 273 | + return v; |
287 | 274 | }
|
288 | 275 |
|
289 | 276 | static inline void put_u16(uint8_t *tab, uint16_t val)
|
290 | 277 | {
|
291 |
| - ((struct packed_u16 *)tab)->v = val; |
| 278 | + memcpy(tab, &val, sizeof(val)); |
292 | 279 | }
|
293 | 280 |
|
294 | 281 | static inline uint32_t get_u8(const uint8_t *tab)
|
|
0 commit comments