Rizin
unix-like reverse engineering framework and cli tools
demangler_util.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
2 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 #include "demangler_util.h"
5 #include <stdarg.h>
6 /* These are mostly copied from rz_util till the util itself is not a side lib */
7 
8 #define dem_return_val_if_fail(expr, val) \
9  do { \
10  if (!(expr)) { \
11  fprintf(stderr, "%s: assertion '%s' failed (line %d)\n", __FILE__, #expr, __LINE__); \
12  return (val); \
13  } \
14  } while (0)
15 
16 #define dem_return_if_fail(expr) \
17  do { \
18  if (!(expr)) { \
19  fprintf(stderr, "%s: assertion '%s' failed (line %d)\n", __FILE__, #expr, __LINE__); \
20  return; \
21  } \
22  } while (0)
23 
24 void dem_str_replace_char(char *string, size_t size, char ch, char rp) {
25  for (size_t i = 0; i < size; ++i) {
26  if (string[i] == ch) {
27  string[i] = rp;
28  }
29  }
30 }
31 
32 char *dem_str_replace(char *str, const char *key, const char *val, int g) {
34 
35  int off, i, slen;
36  char *newstr, *p = str;
37  int klen = strlen(key);
38  int vlen = strlen(val);
39  slen = strlen(str);
40  if (klen == 1 && vlen < 2) {
41  dem_str_replace_char(str, slen, *key, *val);
42  return str;
43  }
44  if (klen == vlen && !strcmp(key, val)) {
45  return str;
46  }
47  char *q = str;
48  for (;;) {
49  p = strstr(q, key);
50  if (!p) {
51  break;
52  }
53  off = (int)(size_t)(p - str);
54  if (vlen != klen) {
55  int tlen = slen - (off + klen);
56  slen += vlen - klen;
57  if (vlen > klen) {
58  newstr = realloc(str, slen + 1);
59  if (!newstr) {
60  RZ_FREE(str);
61  break;
62  }
63  str = newstr;
64  }
65  p = str + off;
66  memmove(p + vlen, p + klen, tlen + 1);
67  }
68  memcpy(p, val, vlen);
69  i = off + vlen;
70  q = str + i;
71  if (!g) {
72  break;
73  }
74  }
75  return str;
76 }
77 
78 char *dem_str_ndup(const char *ptr, int len) {
79  if (len < 0) {
80  return NULL;
81  }
82  char *out = malloc(len + 1);
83  if (!out) {
84  return NULL;
85  }
86  strncpy(out, ptr, len);
87  out[len] = 0;
88  return out;
89 }
90 
91 char *dem_str_newf(const char *fmt, ...) {
93  va_list ap, ap2;
94 
95  va_start(ap, fmt);
96  if (!strchr(fmt, '%')) {
97  va_end(ap);
98  return strdup(fmt);
99  }
100  va_copy(ap2, ap);
101  int ret = vsnprintf(NULL, 0, fmt, ap2);
102  ret++;
103  char *p = calloc(1, ret);
104  if (p) {
105  (void)vsnprintf(p, ret, fmt, ap);
106  }
107  va_end(ap2);
108  va_end(ap);
109  return p;
110 }
111 
112 char *dem_str_append(char *ptr, const char *string) {
113  if (string && !ptr) {
114  return strdup(string);
115  }
116  if (RZ_STR_ISEMPTY(string)) {
117  return ptr;
118  }
119  int plen = strlen(ptr);
120  int slen = strlen(string);
121  char *newptr = realloc(ptr, slen + plen + 1);
122  if (!newptr) {
123  free(ptr);
124  return NULL;
125  }
126  ptr = newptr;
127  memcpy(ptr + plen, string, slen + 1);
128  return ptr;
129 }
130 
132  if (!ds) {
133  return;
134  }
135  free(ds->buf);
136  free(ds);
137 }
138 
140  return RZ_NEW0(DemString);
141 }
142 
144  if (size < 0) {
145  return false;
146  }
147  char *tmp = NULL;
148  if (!ds->len) {
149  tmp = malloc(size + 1);
150  } else {
151  tmp = realloc(ds->buf, ds->len + size + 1);
152  }
153  if (!tmp) {
154  return false;
155  }
156  ds->buf = tmp;
157  return true;
158 }
159 
162  char *ret = ds->buf;
163  free(ds);
164  return ret;
165 }
166 
167 bool dem_string_append(DemString *ds, const char *string) {
168  dem_return_val_if_fail(ds && string, false);
169  size_t size = strlen(string);
170  return dem_string_append_n(ds, string, size);
171 }
172 
173 bool dem_string_append_n(DemString *ds, const char *string, size_t size) {
174  dem_return_val_if_fail(ds && string, false);
175  if (!size) {
176  return true;
177  } else if (!dem_string_realloc_no_len_update(ds, size)) {
178  return false;
179  }
180 
181  memcpy(ds->buf + ds->len, string, size);
182  ds->len += size;
183  ds->buf[ds->len] = 0;
184  return true;
185 }
186 
187 bool dem_string_appendf(DemString *ds, const char *fmt, ...) {
188  va_list ap1;
189  va_list ap2;
190  int size;
191  bool res = true;
192  dem_return_val_if_fail(ds && fmt, false);
193 
194  va_start(ap1, fmt);
195  va_copy(ap2, ap1);
196  size = vsnprintf(NULL, 0, fmt, ap1);
197  if (size < 1) {
198  // always success on empty strings
199  goto dem_string_appendf_end;
200  } else if (!dem_string_realloc_no_len_update(ds, size)) {
201  res = false;
202  goto dem_string_appendf_end;
203  }
204 
205  vsnprintf(ds->buf + ds->len, size + 1, fmt, ap2);
206  ds->len += size;
207 
208 dem_string_appendf_end:
209  va_end(ap2);
210  va_end(ap1);
211  return res;
212 }
213 
214 void dem_string_replace_char(DemString *ds, char ch, char rp) {
215  if (!ds->buf) {
216  return;
217  }
218  dem_str_replace_char(ds->buf, ds->len, ch, rp);
219 }
220 
222  DemList *l = RZ_NEW0(DemList);
223  if (l) {
224  l->free = f;
225  }
226  return l;
227 }
228 
230  DemListIter *item = NULL;
231 
233 
234  item = RZ_NEW(DemListIter);
235  if (!item) {
236  return item;
237  }
238  if (list->tail) {
239  list->tail->n = item;
240  }
241  item->data = data;
242  item->p = list->tail;
243  item->n = NULL;
244  list->tail = item;
245  if (!list->head) {
246  list->head = item;
247  }
248  list->length++;
249  list->sorted = false;
250  return item;
251 }
252 
255  return list->length;
256 }
257 
260 
261  if (list->head == iter) {
262  list->head = iter->n;
263  }
264  if (list->tail == iter) {
265  list->tail = iter->p;
266  }
267  if (iter->p) {
268  iter->p->n = iter->n;
269  }
270  if (iter->n) {
271  iter->n->p = iter->p;
272  }
273  list->length--;
274 }
275 
279  if (list->free && iter->data) {
280  list->free(iter->data);
281  }
282  iter->data = NULL;
283  free(iter);
284 }
285 
288 
289  DemListIter *it = list->head;
290  while (it) {
291  DemListIter *next = it->n;
292  dem_list_delete(list, it);
293  it = next;
294  }
295  list->length = 0;
296  list->head = list->tail = NULL;
297 }
298 
300  if (list) {
302  free(list);
303  }
304 }
305 
306 void *dem_list_get_n(const DemList *list, ut32 n) {
307  DemListIter *it;
308  ut32 i;
309 
311 
312  for (it = list->head, i = 0; it && it->data; it = it->n, i++) {
313  if (i == n) {
314  return it->data;
315  }
316  }
317  return NULL;
318 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
ut16 val
Definition: armass64_const.h:6
static RASN1String * newstr(const char *string)
Definition: astr.c:23
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
void dem_list_free(DemList *list)
char * dem_str_ndup(const char *ptr, int len)
DemListIter * dem_list_append(DemList *list, void *data)
void dem_list_delete(DemList *list, DemListIter *iter)
char * dem_string_drain(DemString *ds)
char * dem_str_replace(char *str, const char *key, const char *val, int g)
void dem_list_purge(DemList *list)
ut32 dem_list_length(const DemList *list)
void * dem_list_get_n(const DemList *list, ut32 n)
void dem_string_free(DemString *ds)
DemString * dem_string_new()
char * dem_str_newf(const char *fmt,...)
void dem_list_split_iter(DemList *list, DemListIter *iter)
static bool dem_string_realloc_no_len_update(DemString *ds, ssize_t size)
char * dem_str_append(char *ptr, const char *string)
bool dem_string_append(DemString *ds, const char *string)
#define dem_return_if_fail(expr)
bool dem_string_appendf(DemString *ds, const char *fmt,...)
bool dem_string_append_n(DemString *ds, const char *string, size_t size)
DemList * dem_list_newf(DemListFree f)
void dem_string_replace_char(DemString *ds, char ch, char rp)
#define dem_return_val_if_fail(expr, val)
Definition: demangler_util.c:8
void dem_str_replace_char(char *string, size_t size, char ch, char rp)
void(* DemListFree)(void *ptr)
uint32_t ut32
struct @667 g
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static char * rp[]
Definition: i8080dis.c:36
voidpf void uLong size
Definition: ioapi.h:138
vsnprintf
Definition: kernel.h:366
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void list(RzEgg *egg)
Definition: rz-gg.c:52
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int n
Definition: mipsasm.c:19
int off
Definition: pal.c:13
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_FREE(x)
Definition: rz_types.h:369
static int
Definition: sfsocketcall.h:114
int ssize_t
Definition: sftypes.h:39
#define f(i)
Definition: sha256.c:46
size_t len
char * buf
struct dem_list_iter_t * n
struct dem_list_iter_t * p
DemListFree free