summaryrefslogtreecommitdiffstats
path: root/src/blob.h
blob: 1fcaec0b555f30103197cb1aa9e59f8c9a2fb4d1 (plain)
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
#ifndef BLOB_H
#define BLOB_H

#include <string.h>

#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_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);
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);

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);

#endif