File: strpool.c

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (123 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* radare - LGPL - Copyright 2012 - pancake */

#include <r_util.h>

R_API RStrpool* r_strpool_new (int sz) {
	RStrpool *p = R_NEW (RStrpool);
	if (sz<1) sz = 1024;
	p->size = sz;
	p->len = 0;
	p->str = malloc (sz);
	if (!p->str) {
		free (p);
		return NULL;
	}
	p->str[0] = 0;
	return p;
}

R_API char *r_strpool_empty (RStrpool *p) {
	p->len = 0;
	p->str[0] = 0;
	p->str[1] = 0;
	return p->str;
}

R_API char *r_strpool_alloc (RStrpool *p, int l) {
	char *ret = p->str+p->len;
	if ((p->len+l)>=p->size) {
		p->size += R_STRPOOL_INC;
		ret = realloc (p->str, p->size);
		if (!ret) return NULL;
		p->str = ret;
		ret += p->len;
	}
	p->len += l;
	return ret;
}

R_API int r_strpool_append(RStrpool *p, const char *s) {
	int l = strlen (s)+1;
	char *ptr = r_strpool_alloc (p, l);
	if (!ptr) return -1;
	memcpy (ptr, s, l);
	return (size_t)(ptr-p->str);
}

R_API void r_strpool_free (RStrpool *p) {
	free (p->str);
	free (p);
}

R_API int r_strpool_fit(RStrpool *p) {
	char *s;
	if (p->len == p->size)
		return R_FALSE;
	s = realloc (p->str, p->len);
	if (!s) return R_FALSE;
	p->str = s;
	p->size = p->len;
	return R_TRUE;
}

R_API char *r_strpool_get(RStrpool *p, int index) {
	if (!p || !p->str || index<0 || index>=p->len)
		return NULL;
	return p->str+index;
}

R_API char *r_strpool_get_i(RStrpool *p, int index) {
	int i, n = 0;
	if (index<0 || index>=p->len)
		return NULL;
	for (i=0; i<index; i++) {
		char *s = r_strpool_next (p, n);
		n = r_strpool_get_index (p, s);
	}
	return p->str+n;
}

R_API int r_strpool_get_index(RStrpool *p, const char *s) {
	int ret = (size_t)(s-p->str);
	return ret>0? ret: 0;
}

R_API char *r_strpool_next(RStrpool *p, int index) {
	char *ptr = r_strpool_get (p, index);
	if (ptr) {
		char *q = ptr + strlen (ptr)+1;
		if (q>=(p->str+p->len))
			return NULL;
		ptr = q;
		if (!*ptr) ptr = NULL;
	}
	return ptr;
}

R_API char *r_strpool_slice (RStrpool *p, int index) {
	int idx, len;
	char *o, *x = r_strpool_get_i (p, index+1);
	if (!x) return NULL;
	idx = (size_t)(x-p->str);
	len = p->len - idx;
	o = malloc (len+128);
	if (!o) return NULL;
	memcpy (o, x, len);
	free (p->str);
	p->str = o;
	p->size = len + 128;
	p->len = len;
	return o;
}

#if TEST
int main() {
	RStrpool *p = r_strpool_new (1024);
	printf ("%d\n", r_strpool_append (p, "Hello World"));
	printf ("%d\n", r_strpool_append (p, "Patata Barata"));
	printf ("%s\n", r_strpool_get (p, 12));
	r_strpool_fit (p);
	r_strpool_free (p);
	return 0;
}
#endif