Rizin
unix-like reverse engineering framework and cli tools
strpool.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2012-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 
8  if (!p) {
9  eprintf("Malloc failed!\n");
10  return NULL;
11  }
12  if (sz < 1) {
13  sz = 1024;
14  }
15  p->str = malloc(sz);
16  if (!p->str) {
17  eprintf("Malloc failed!\n");
18  free(p);
19  return NULL;
20  }
21  p->size = sz;
22  p->len = 0;
23  p->str[0] = 0;
24  return p;
25 }
26 
28  p->len = 0;
29  p->str[0] = 0;
30  p->str[1] = 0;
31  return p->str;
32 }
33 
35  char *ret = p->str + p->len;
36  if ((p->len + l) >= p->size) {
37  ut64 osize = p->size;
38  if (l >= RZ_STRPOOL_INC) {
39  p->size += l + RZ_STRPOOL_INC;
40  } else {
41  p->size += RZ_STRPOOL_INC;
42  }
43  if (p->size < osize) {
44  eprintf("Underflow!\n");
45  p->size = osize;
46  return NULL;
47  }
48  ret = realloc(p->str, p->size);
49  if (!ret) {
50  eprintf("Realloc failed!\n");
51  RZ_FREE(p->str);
52  return NULL;
53  }
54  p->str = ret;
55  ret += p->len;
56  }
57  p->len += l;
58  return ret;
59 }
60 
61 RZ_API int rz_strpool_memcat(RzStrpool *p, const char *s, int len) {
62  char *ptr = rz_strpool_alloc(p, len);
63  if (!ptr) {
64  return -1;
65  }
66  memcpy(ptr, s, len);
67  return (size_t)(ptr - p->str);
68 }
69 
70 RZ_API int rz_strpool_append(RzStrpool *p, const char *s) {
71  int l = strlen(s) + 1;
72  return rz_strpool_memcat(p, s, l);
73 }
74 
76  /* p->str need not be a c-string */
77  int i = rz_str_ansi_trim(p->str, p->len, n);
78  p->len = i;
79  return i;
80 }
81 
83  free(p->str);
84  free(p);
85 }
86 
88  char *s;
89  if (p->len == p->size) {
90  return false;
91  }
92  s = realloc(p->str, p->len);
93  if (!s) {
94  eprintf("Realloc failed!\n");
95  RZ_FREE(p->str);
96  return false;
97  }
98  p->str = s;
99  p->size = p->len;
100  return true;
101 }
102 
103 RZ_API char *rz_strpool_get(RzStrpool *p, int index) {
104  if (!p || !p->str || index < 0 || index >= p->len) {
105  return NULL;
106  }
107  return p->str + index;
108 }
109 
110 RZ_API char *rz_strpool_get_i(RzStrpool *p, int index) {
111  int i, n = 0;
112  if (index < 0 || index >= p->len) {
113  return NULL;
114  }
115  for (i = 0; i < index; i++) {
116  char *s = rz_strpool_next(p, n);
118  }
119  return p->str + n;
120 }
121 
123  int ret = (size_t)(s - p->str);
124  return (ret > 0) ? ret : 0;
125 }
126 
127 RZ_API char *rz_strpool_next(RzStrpool *p, int index) {
128  char *ptr = rz_strpool_get(p, index);
129  if (ptr) {
130  char *q = ptr + strlen(ptr) + 1;
131  if (q >= (p->str + p->len)) {
132  return NULL;
133  }
134  ptr = q;
135  if (!*ptr) {
136  ptr = NULL;
137  }
138  }
139  return ptr;
140 }
141 
142 RZ_API char *rz_strpool_slice(RzStrpool *p, int index) {
143  int idx, len;
144  char *o, *x = rz_strpool_get_i(p, index + 1);
145  if (!x || !*x) {
146  return NULL;
147  }
148  idx = (size_t)(x - p->str);
149  len = p->len - idx;
150  o = malloc(len + 128);
151  if (!o) {
152  return NULL;
153  }
154  memcpy(o, x, len);
155  free(p->str);
156  p->str = o;
157  p->size = len + 128;
158  p->len = len;
159  return o;
160 }
161 
162 #if TEST
163 int main() {
164  RzStrpool *p = rz_strpool_new(1024);
165  printf("%d\n", rz_strpool_append(p, "Hello World"));
166  printf("%d\n", rz_strpool_append(p, "Patata Barata"));
167  printf("%s\n", rz_strpool_get(p, 12));
168  rz_strpool_fit(p);
170  return 0;
171 }
172 #endif
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define RZ_API
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
int x
Definition: mipsasm.c:20
int n
Definition: mipsasm.c:19
int idx
Definition: setup.py:197
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
int main(int argc, char **argv)
Definition: rz-bb.c:29
RZ_API int rz_str_ansi_trim(char *str, int str_len, int n)
Definition: str_trim.c:204
#define RZ_STRPOOL_INC
Definition: rz_strpool.h:6
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_FREE(x)
Definition: rz_types.h:369
int size_t
Definition: sftypes.h:40
RZ_API int rz_strpool_get_index(RzStrpool *p, const char *s)
Definition: strpool.c:122
RZ_API int rz_strpool_append(RzStrpool *p, const char *s)
Definition: strpool.c:70
RZ_API int rz_strpool_ansi_chop(RzStrpool *p, int n)
Definition: strpool.c:75
RZ_API char * rz_strpool_get(RzStrpool *p, int index)
Definition: strpool.c:103
RZ_API int rz_strpool_fit(RzStrpool *p)
Definition: strpool.c:87
RZ_API char * rz_strpool_get_i(RzStrpool *p, int index)
Definition: strpool.c:110
RZ_API char * rz_strpool_slice(RzStrpool *p, int index)
Definition: strpool.c:142
RZ_API char * rz_strpool_empty(RzStrpool *p)
Definition: strpool.c:27
RZ_API void rz_strpool_free(RzStrpool *p)
Definition: strpool.c:82
RZ_API char * rz_strpool_next(RzStrpool *p, int index)
Definition: strpool.c:127
RZ_API char * rz_strpool_alloc(RzStrpool *p, int l)
Definition: strpool.c:34
RZ_API RzStrpool * rz_strpool_new(int sz)
Definition: strpool.c:6
RZ_API int rz_strpool_memcat(RzStrpool *p, const char *s, int len)
Definition: strpool.c:61
ut64(WINAPI *w32_GetEnabledXStateFeatures)()