#ifndef BLOB_H #define BLOB_H #include #include struct in_addr; #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #if defined __GNUC__ && __GNUC__ == 2 && __GNUC_MINOR__ < 96 #define __builtin_expect(x, expected_value) (x) #endif #ifndef likely #define likely(x) __builtin_expect((!!(x)),1) #endif #ifndef unlikely #define unlikely(x) __builtin_expect((!!(x)),0) #endif typedef struct blob { char *ptr; unsigned int len; } blob_t; #define BLOB_PTR_LEN(ptr,len) (blob_t){(void*)(ptr), (len)} #define BLOB_PTR_PTR(beg,end) BLOB_PTR_LEN((beg),(end)-(beg)+1) #define BLOB_BUF(buf) (blob_t){(void*)(buf), sizeof(buf)} #define BLOB_STRLEN(str) (blob_t){(str), strlen(str)} #define BLOB_STR_INIT(str) {(str), sizeof(str)-1} #define BLOB_STR(str) (blob_t) BLOB_STR_INIT(str) #define BLOB_CHAR_ARRAY(a) (blob_t){(a), strnlen(a, sizeof(a))} #define BLOB_NULL (blob_t){NULL, 0} static inline int blob_is_null(blob_t b) { return b.len == 0; } char *blob_cstr_dup(blob_t b); blob_t blob_dup(blob_t b); int blob_cmp(blob_t a, blob_t b); int blob_icmp(blob_t a, blob_t b); void blob_lowercase(blob_t blob); unsigned long blob_inet_addr(blob_t buf); blob_t blob_pushed(blob_t buffer, blob_t left); void blob_push(blob_t *b, blob_t d); void blob_push_lower(blob_t *b, blob_t d); void blob_push_byte(blob_t *b, unsigned char byte); void blob_push_uint(blob_t *to, unsigned int value, int radix); void blob_push_ctime(blob_t *to, time_t t); void blob_push_hexdump(blob_t *to, blob_t binary); void blob_push_urldecode(blob_t *to, blob_t url); void blob_push_urlencode(blob_t *to, blob_t url); blob_t blob_pull(blob_t *b, int len); void blob_pull_skip(blob_t *b, int len); int blob_pull_matching(blob_t *b, blob_t e); unsigned int blob_pull_uint(blob_t *b, int radix); blob_t blob_pull_spn(blob_t *b, const blob_t spn); blob_t blob_pull_cspn(blob_t *b, const blob_t cspn); int blob_pull_inet_addr(blob_t *b, struct in_addr *saddr); blob_t blob_expand_head(blob_t *b, blob_t limits, unsigned char sep); blob_t blob_expand_tail(blob_t *b, blob_t limits, unsigned char sep); blob_t blob_shrink_tail(blob_t *b, blob_t limits, unsigned char sep); void blob_expand_head_bytes(blob_t *b, int bytes); #endif