| 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 |
#ifndef CUTILS_H |
| 26 |
#define CUTILS_H |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
#include <inttypes.h> |
| 30 |
|
| 31 |
|
| 32 |
#undef WORDS_BIGENDIAN |
| 33 |
|
| 34 |
#define likely(x) __builtin_expect(!!(x), 1) |
| 35 |
#define unlikely(x) __builtin_expect(!!(x), 0) |
| 36 |
#define force_inline inline __attribute__((always_inline)) |
| 37 |
#define no_inline __attribute__((noinline)) |
| 38 |
#define __maybe_unused __attribute__((unused)) |
| 39 |
|
| 40 |
#define xglue(x, y) x ## y |
| 41 |
#define glue(x, y) xglue(x, y) |
| 42 |
#define stringify(s) tostring(s) |
| 43 |
#define tostring(s) #s |
| 44 |
|
| 45 |
#ifndef offsetof |
| 46 |
#define offsetof(type, field) ((size_t) &((type *)0)->field) |
| 47 |
#endif |
| 48 |
#ifndef countof |
| 49 |
#define countof(x) (sizeof(x) / sizeof((x)[0])) |
| 50 |
#endif |
| 51 |
|
| 52 |
|
| 53 |
#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member))) |
| 54 |
|
| 55 |
typedef int BOOL; |
| 56 |
|
| 57 |
#ifndef FALSE |
| 58 |
enum { |
| 59 |
FALSE = 0, |
| 60 |
TRUE = 1, |
| 61 |
}; |
| 62 |
#endif |
| 63 |
|
| 64 |
void pstrcpy(char *buf, int buf_size, const char *str); |
| 65 |
char *pstrcat(char *buf, int buf_size, const char *s); |
| 66 |
int strstart(const char *str, const char *val, const char **ptr); |
| 67 |
int has_suffix(const char *str, const char *suffix); |
| 68 |
|
| 69 |
static inline int max_int(int a, int b) |
| 70 |
{ |
| 71 |
if (a > b) |
| 72 |
return a; |
| 73 |
else |
| 74 |
return b; |
| 75 |
} |
| 76 |
|
| 77 |
static inline int min_int(int a, int b) |
| 78 |
{ |
| 79 |
if (a < b) |
| 80 |
return a; |
| 81 |
else |
| 82 |
return b; |
| 83 |
} |
| 84 |
|
| 85 |
static inline uint32_t max_uint32(uint32_t a, uint32_t b) |
| 86 |
{ |
| 87 |
if (a > b) |
| 88 |
return a; |
| 89 |
else |
| 90 |
return b; |
| 91 |
} |
| 92 |
|
| 93 |
static inline uint32_t min_uint32(uint32_t a, uint32_t b) |
| 94 |
{ |
| 95 |
if (a < b) |
| 96 |
return a; |
| 97 |
else |
| 98 |
return b; |
| 99 |
} |
| 100 |
|
| 101 |
static inline int64_t max_int64(int64_t a, int64_t b) |
| 102 |
{ |
| 103 |
if (a > b) |
| 104 |
return a; |
| 105 |
else |
| 106 |
return b; |
| 107 |
} |
| 108 |
|
| 109 |
static inline int64_t min_int64(int64_t a, int64_t b) |
| 110 |
{ |
| 111 |
if (a < b) |
| 112 |
return a; |
| 113 |
else |
| 114 |
return b; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
static inline int clz32(unsigned int a) |
| 119 |
{ |
| 120 |
return __builtin_clz(a); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
static inline int clz64(uint64_t a) |
| 125 |
{ |
| 126 |
return __builtin_clzll(a); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
static inline int ctz32(unsigned int a) |
| 131 |
{ |
| 132 |
return __builtin_ctz(a); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static inline int ctz64(uint64_t a) |
| 137 |
{ |
| 138 |
return __builtin_ctzll(a); |
| 139 |
} |
| 140 |
|
| 141 |
struct __attribute__((packed)) packed_u64 { |
| 142 |
uint64_t v; |
| 143 |
}; |
| 144 |
|
| 145 |
struct __attribute__((packed)) packed_u32 { |
| 146 |
uint32_t v; |
| 147 |
}; |
| 148 |
|
| 149 |
struct __attribute__((packed)) packed_u16 { |
| 150 |
uint16_t v; |
| 151 |
}; |
| 152 |
|
| 153 |
static inline uint64_t get_u64(const uint8_t *tab) |
| 154 |
{ |
| 155 |
return ((const struct packed_u64 *)tab)->v; |
| 156 |
} |
| 157 |
|
| 158 |
static inline int64_t get_i64(const uint8_t *tab) |
| 159 |
{ |
| 160 |
return (int64_t)((const struct packed_u64 *)tab)->v; |
| 161 |
} |
| 162 |
|
| 163 |
static inline void put_u64(uint8_t *tab, uint64_t val) |
| 164 |
{ |
| 165 |
((struct packed_u64 *)tab)->v = val; |
| 166 |
} |
| 167 |
|
| 168 |
static inline uint32_t get_u32(const uint8_t *tab) |
| 169 |
{ |
| 170 |
return ((const struct packed_u32 *)tab)->v; |
| 171 |
} |
| 172 |
|
| 173 |
static inline int32_t get_i32(const uint8_t *tab) |
| 174 |
{ |
| 175 |
return (int32_t)((const struct packed_u32 *)tab)->v; |
| 176 |
} |
| 177 |
|
| 178 |
static inline void put_u32(uint8_t *tab, uint32_t val) |
| 179 |
{ |
| 180 |
((struct packed_u32 *)tab)->v = val; |
| 181 |
} |
| 182 |
|
| 183 |
static inline uint32_t get_u16(const uint8_t *tab) |
| 184 |
{ |
| 185 |
return ((const struct packed_u16 *)tab)->v; |
| 186 |
} |
| 187 |
|
| 188 |
static inline int32_t get_i16(const uint8_t *tab) |
| 189 |
{ |
| 190 |
return (int16_t)((const struct packed_u16 *)tab)->v; |
| 191 |
} |
| 192 |
|
| 193 |
static inline void put_u16(uint8_t *tab, uint16_t val) |
| 194 |
{ |
| 195 |
((struct packed_u16 *)tab)->v = val; |
| 196 |
} |
| 197 |
|
| 198 |
static inline uint32_t get_u8(const uint8_t *tab) |
| 199 |
{ |
| 200 |
return *tab; |
| 201 |
} |
| 202 |
|
| 203 |
static inline int32_t get_i8(const uint8_t *tab) |
| 204 |
{ |
| 205 |
return (int8_t)*tab; |
| 206 |
} |
| 207 |
|
| 208 |
static inline void put_u8(uint8_t *tab, uint8_t val) |
| 209 |
{ |
| 210 |
*tab = val; |
| 211 |
} |
| 212 |
|
| 213 |
static inline uint16_t bswap16(uint16_t x) |
| 214 |
{ |
| 215 |
return (x >> 8) | (x << 8); |
| 216 |
} |
| 217 |
|
| 218 |
static inline uint32_t bswap32(uint32_t v) |
| 219 |
{ |
| 220 |
return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) | |
| 221 |
((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24); |
| 222 |
} |
| 223 |
|
| 224 |
static inline uint64_t bswap64(uint64_t v) |
| 225 |
{ |
| 226 |
return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) | |
| 227 |
((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) | |
| 228 |
((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) | |
| 229 |
((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) | |
| 230 |
((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) | |
| 231 |
((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) | |
| 232 |
((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) | |
| 233 |
((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8)); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size); |
| 238 |
|
| 239 |
typedef struct DynBuf { |
| 240 |
uint8_t *buf; |
| 241 |
size_t size; |
| 242 |
size_t allocated_size; |
| 243 |
BOOL error; |
| 244 |
DynBufReallocFunc *realloc_func; |
| 245 |
void *opaque; |
| 246 |
} DynBuf; |
| 247 |
|
| 248 |
void dbuf_init(DynBuf *s); |
| 249 |
void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func); |
| 250 |
int dbuf_realloc(DynBuf *s, size_t new_size); |
| 251 |
int dbuf_write(DynBuf *s, size_t offset, const uint8_t *data, size_t len); |
| 252 |
int dbuf_put(DynBuf *s, const uint8_t *data, size_t len); |
| 253 |
int dbuf_put_self(DynBuf *s, size_t offset, size_t len); |
| 254 |
int dbuf_putc(DynBuf *s, uint8_t c); |
| 255 |
int dbuf_putstr(DynBuf *s, const char *str); |
| 256 |
static inline int dbuf_put_u16(DynBuf *s, uint16_t val) |
| 257 |
{ |
| 258 |
return dbuf_put(s, (uint8_t *)&val, 2); |
| 259 |
} |
| 260 |
static inline int dbuf_put_u32(DynBuf *s, uint32_t val) |
| 261 |
{ |
| 262 |
return dbuf_put(s, (uint8_t *)&val, 4); |
| 263 |
} |
| 264 |
static inline int dbuf_put_u64(DynBuf *s, uint64_t val) |
| 265 |
{ |
| 266 |
return dbuf_put(s, (uint8_t *)&val, 8); |
| 267 |
} |
| 268 |
int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s, |
| 269 |
const char *fmt, ...); |
| 270 |
void dbuf_free(DynBuf *s); |
| 271 |
static inline BOOL dbuf_error(DynBuf *s) { |
| 272 |
return s->error; |
| 273 |
} |
| 274 |
static inline void dbuf_set_error(DynBuf *s) |
| 275 |
{ |
| 276 |
s->error = TRUE; |
| 277 |
} |
| 278 |
|
| 279 |
#define UTF8_CHAR_LEN_MAX 6 |
| 280 |
|
| 281 |
int unicode_to_utf8(uint8_t *buf, unsigned int c); |
| 282 |
int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp); |
| 283 |
|
| 284 |
static inline int from_hex(int c) |
| 285 |
{ |
| 286 |
if (c >= '0' && c <= '9') |
| 287 |
return c - '0'; |
| 288 |
else if (c >= 'A' && c <= 'F') |
| 289 |
return c - 'A' + 10; |
| 290 |
else if (c >= 'a' && c <= 'f') |
| 291 |
return c - 'a' + 10; |
| 292 |
else |
| 293 |
return -1; |
| 294 |
} |
| 295 |
|
| 296 |
void rqsort(void *base, size_t nmemb, size_t size, |
| 297 |
int (*cmp)(const void *, const void *, void *), |
| 298 |
void *arg); |
| 299 |
|
| 300 |
#endif |