misc.h (1110B)
1 2 #ifndef BCS_MISC_H 3 #define BCS_MISC_H 4 5 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 6 7 /* #include <endian.h> */ 8 #include <string.h> 9 #include <stdint.h> 10 #include <stdio.h> 11 12 #define DEBUG 0 13 14 enum compiler_options { 15 CO_WARNINGS_ARE_ERRORS = 1 << 1, 16 CO_WARN_MINIMAL = 1 << 2 17 }; 18 19 20 /* #define debug(fmt, ...) \ */ 21 /* do { if (DEBUG) fprintf(stderr, fmt, __VA_ARGS__); } while (0) */ 22 23 typedef unsigned char u8; 24 typedef unsigned short u16; 25 typedef unsigned int u32; 26 typedef int64_t s64; 27 typedef uint64_t u64; 28 29 #define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(!!(COND))*2-1] 30 31 // TODO: host endianness 32 #define le16toh(x) (x) 33 #define le32toh(x) (x) 34 35 inline static u16 36 readle16(const u8* ptr) { 37 u16 x; 38 memcpy((char*)&x, ptr, 2); 39 return le16toh(x); 40 } 41 42 inline static u32 43 readle32(const u8* ptr) { 44 u32 x; 45 memcpy((char*)&x, ptr, 4); 46 return le32toh(x); 47 } 48 49 inline static void 50 print_bytes(u8 *bytes, size_t size, int with_space) { 51 size_t i; 52 for (i = 0; i < size; i++) { 53 printf("%02x", bytes[i]); 54 if (with_space) printf(" "); 55 } 56 } 57 58 59 #endif /* BCS_MISC_H */