2022-06-26 01:04:52 +00:00
|
|
|
/* radare - LGPL - Copyright 2012-2022 - pancake */
|
2012-11-06 08:47:52 +00:00
|
|
|
|
|
|
|
#include <r_util.h>
|
|
|
|
|
2020-12-21 00:10:22 +00:00
|
|
|
R_API RStrpool* r_strpool_new(int sz) {
|
2012-11-06 08:47:52 +00:00
|
|
|
RStrpool *p = R_NEW (RStrpool);
|
2017-07-08 17:21:08 +00:00
|
|
|
if (!p) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
if (sz < 1) {
|
|
|
|
sz = 1024;
|
|
|
|
}
|
2012-11-06 08:47:52 +00:00
|
|
|
p->str = malloc (sz);
|
|
|
|
if (!p->str) {
|
|
|
|
free (p);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-08 17:21:08 +00:00
|
|
|
p->size = sz;
|
|
|
|
p->len = 0;
|
2012-12-23 12:52:57 +00:00
|
|
|
p->str[0] = 0;
|
2012-11-06 08:47:52 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:10:22 +00:00
|
|
|
R_API char *r_strpool_empty(RStrpool *p) {
|
2012-12-23 12:52:57 +00:00
|
|
|
p->len = 0;
|
|
|
|
p->str[0] = 0;
|
|
|
|
p->str[1] = 0;
|
|
|
|
return p->str;
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:10:22 +00:00
|
|
|
R_API char *r_strpool_alloc(RStrpool *p, int l) {
|
2017-07-08 17:21:08 +00:00
|
|
|
char *ret = p->str + p->len;
|
|
|
|
if ((p->len + l) >= p->size) {
|
2015-04-11 23:11:57 +00:00
|
|
|
ut64 osize = p->size;
|
2017-07-08 17:21:08 +00:00
|
|
|
if (l >= R_STRPOOL_INC) {
|
2015-04-11 23:11:57 +00:00
|
|
|
p->size += l + R_STRPOOL_INC;
|
|
|
|
} else {
|
|
|
|
p->size += R_STRPOOL_INC;
|
|
|
|
}
|
|
|
|
if (p->size < osize) {
|
2015-04-11 23:12:54 +00:00
|
|
|
p->size = osize;
|
2015-04-11 23:11:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-06 08:47:52 +00:00
|
|
|
ret = realloc (p->str, p->size);
|
2015-04-11 23:12:54 +00:00
|
|
|
if (!ret) {
|
2017-07-08 17:21:08 +00:00
|
|
|
free (p->str);
|
2020-12-21 00:10:22 +00:00
|
|
|
p->str = NULL;
|
2015-04-11 23:12:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-12-23 12:52:57 +00:00
|
|
|
p->str = ret;
|
2012-11-06 08:47:52 +00:00
|
|
|
ret += p->len;
|
|
|
|
}
|
|
|
|
p->len += l;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-04-12 12:59:13 +00:00
|
|
|
R_API int r_strpool_memcat(RStrpool *p, const char *s, int len) {
|
|
|
|
char *ptr = r_strpool_alloc (p, len);
|
2017-07-08 17:21:08 +00:00
|
|
|
if (!ptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-12 12:59:13 +00:00
|
|
|
memcpy (ptr, s, len);
|
2017-07-08 17:21:08 +00:00
|
|
|
return (size_t)(ptr - p->str);
|
2012-11-06 08:47:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 12:59:13 +00:00
|
|
|
R_API int r_strpool_append(RStrpool *p, const char *s) {
|
2017-07-08 17:21:08 +00:00
|
|
|
int l = strlen (s) + 1;
|
2017-07-12 15:07:47 +00:00
|
|
|
return r_strpool_memcat (p, s, l);
|
2015-04-12 12:59:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 12:37:29 +00:00
|
|
|
R_API int r_strpool_ansi_chop(RStrpool *p, int n) {
|
2015-04-12 12:59:13 +00:00
|
|
|
/* p->str need not be a c-string */
|
2018-01-08 02:33:01 +00:00
|
|
|
int i = r_str_ansi_trim (p->str, p->len, n);
|
2015-04-12 12:59:13 +00:00
|
|
|
p->len = i;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:10:22 +00:00
|
|
|
R_API void r_strpool_free(RStrpool *p) {
|
2022-06-26 01:04:52 +00:00
|
|
|
if (p) {
|
|
|
|
free (p->str);
|
|
|
|
free (p);
|
|
|
|
}
|
2012-11-06 08:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_strpool_fit(RStrpool *p) {
|
2017-07-08 17:21:08 +00:00
|
|
|
if (p->len == p->size) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2017-07-08 17:21:08 +00:00
|
|
|
}
|
2022-06-26 01:04:52 +00:00
|
|
|
char *s = realloc (p->str, p->len);
|
|
|
|
if (!s) {
|
2017-07-08 17:21:08 +00:00
|
|
|
free (p->str);
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-06 08:47:52 +00:00
|
|
|
p->str = s;
|
|
|
|
p->size = p->len;
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2012-11-06 08:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API char *r_strpool_get(RStrpool *p, int index) {
|
2017-07-08 17:21:08 +00:00
|
|
|
if (!p || !p->str || index < 0 || index >= p->len) {
|
2012-11-06 08:47:52 +00:00
|
|
|
return NULL;
|
2017-07-08 17:21:08 +00:00
|
|
|
}
|
|
|
|
return p->str + index;
|
2012-11-06 08:47:52 +00:00
|
|
|
}
|
|
|
|
|
2012-12-23 12:52:57 +00:00
|
|
|
R_API char *r_strpool_get_i(RStrpool *p, int index) {
|
|
|
|
int i, n = 0;
|
2016-11-15 21:00:52 +00:00
|
|
|
if (index < 0 || index >= p->len) {
|
2012-12-23 12:52:57 +00:00
|
|
|
return NULL;
|
2016-11-15 21:00:52 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < index; i++) {
|
2012-12-23 12:52:57 +00:00
|
|
|
char *s = r_strpool_next (p, n);
|
|
|
|
n = r_strpool_get_index (p, s);
|
|
|
|
}
|
2016-11-15 21:00:52 +00:00
|
|
|
return p->str + n;
|
2012-12-23 12:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_strpool_get_index(RStrpool *p, const char *s) {
|
2017-07-08 17:21:08 +00:00
|
|
|
int ret = (size_t)(s - p->str);
|
|
|
|
return (ret > 0) ? ret : 0;
|
2012-12-23 12:52:57 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 09:41:12 +00:00
|
|
|
R_API char *r_strpool_next(RStrpool *p, int index) {
|
|
|
|
char *ptr = r_strpool_get (p, index);
|
|
|
|
if (ptr) {
|
2017-07-08 17:21:08 +00:00
|
|
|
char *q = ptr + strlen (ptr) + 1;
|
|
|
|
if (q >= (p->str + p->len)) {
|
2012-11-07 09:41:12 +00:00
|
|
|
return NULL;
|
2017-07-08 17:21:08 +00:00
|
|
|
}
|
2012-11-07 09:41:12 +00:00
|
|
|
ptr = q;
|
2017-07-08 17:21:08 +00:00
|
|
|
if (!*ptr) {
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
2012-11-07 09:41:12 +00:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:10:22 +00:00
|
|
|
R_API char *r_strpool_slice(RStrpool *p, int index) {
|
2022-06-26 01:04:52 +00:00
|
|
|
char *x = r_strpool_get_i (p, index + 1);
|
|
|
|
if (!x) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(*x)) {
|
|
|
|
free (x);
|
2017-05-09 00:10:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-26 01:04:52 +00:00
|
|
|
int idx = (size_t)(x - p->str);
|
|
|
|
int len = p->len - idx;
|
|
|
|
char *o = malloc (len + 128);
|
2017-05-09 00:10:11 +00:00
|
|
|
if (!o) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-12-23 12:52:57 +00:00
|
|
|
memcpy (o, x, len);
|
|
|
|
free (p->str);
|
|
|
|
p->str = o;
|
|
|
|
p->size = len + 128;
|
|
|
|
p->len = len;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2012-11-06 08:47:52 +00:00
|
|
|
#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
|