Rizin
unix-like reverse engineering framework and cli tools
demangler_util.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

Go to the source code of this file.

Classes

struct  DemString
 
struct  dem_list_iter_t
 
struct  dem_list_t
 

Macros

#define true   1
 
#define false   0
 
#define RZ_NEW0(x)   (x *)calloc(1, sizeof(x))
 
#define RZ_NEW(x)   (x *)malloc(sizeof(x))
 
#define RZ_FREE(x)
 
#define RZ_API
 
#define IS_UPPER(c)   ((c) >= 'A' && (c) <= 'Z')
 
#define IS_DIGIT(x)   ((x) >= '0' && (x) <= '9')
 
#define RZ_MIN(x, y)   (((x) > (y)) ? (y) : (x))
 
#define RZ_STR_ISEMPTY(x)   (!(x) || !*(x))
 
#define dem_list_foreach_prev(list, it, pos)
 
#define dem_list_head(x)   ((x) ? (x)->head : NULL)
 

Typedefs

typedef int bool
 
typedef uint8_t ut8
 
typedef uint16_t ut16
 
typedef uint32_t ut32
 
typedef void(* DemListFree) (void *ptr)
 
typedef struct dem_list_iter_t DemListIter
 
typedef struct dem_list_t DemList
 

Functions

char * dem_str_ndup (const char *ptr, int len)
 
char * dem_str_newf (const char *fmt,...)
 
char * dem_str_append (char *ptr, const char *string)
 
void dem_str_replace_char (char *string, size_t size, char ch, char rp)
 
char * dem_str_replace (char *str, const char *key, const char *val, int g)
 
void dem_string_free (DemString *ds)
 
DemStringdem_string_new ()
 
char * dem_string_drain (DemString *ds)
 
bool dem_string_append (DemString *ds, const char *string)
 
bool dem_string_append_n (DemString *ds, const char *string, size_t size)
 
bool dem_string_appendf (DemString *ds, const char *fmt,...)
 
void dem_string_replace_char (DemString *ds, char ch, char rp)
 
DemListdem_list_newf (DemListFree f)
 
DemListIterdem_list_append (DemList *list, void *data)
 
void dem_list_free (DemList *list)
 
void * dem_list_get_n (const DemList *list, ut32 n)
 
ut32 dem_list_length (const DemList *list)
 

Macro Definition Documentation

◆ dem_list_foreach_prev

#define dem_list_foreach_prev (   list,
  it,
  pos 
)
Value:
if (list) \
for (it = list->tail; it && (pos = it->data, 1); it = it->p)
static void list(RzEgg *egg)
Definition: rz-gg.c:52
int pos
Definition: main.c:11

Definition at line 81 of file demangler_util.h.

◆ dem_list_head

#define dem_list_head (   x)    ((x) ? (x)->head : NULL)

Definition at line 85 of file demangler_util.h.

◆ false

#define false   0

Definition at line 14 of file demangler_util.h.

◆ IS_DIGIT

#define IS_DIGIT (   x)    ((x) >= '0' && (x) <= '9')

Definition at line 42 of file demangler_util.h.

◆ IS_UPPER

#define IS_UPPER (   c)    ((c) >= 'A' && (c) <= 'Z')

Definition at line 41 of file demangler_util.h.

◆ RZ_API

#define RZ_API

Definition at line 38 of file demangler_util.h.

◆ RZ_FREE

#define RZ_FREE (   x)
Value:
{ \
free((void *)x); \
x = NULL; \
}
#define NULL
Definition: cris-opc.c:27
int x
Definition: mipsasm.c:20

Definition at line 23 of file demangler_util.h.

◆ RZ_MIN

#define RZ_MIN (   x,
 
)    (((x) > (y)) ? (y) : (x))

Definition at line 43 of file demangler_util.h.

◆ RZ_NEW

#define RZ_NEW (   x)    (x *)malloc(sizeof(x))

Definition at line 22 of file demangler_util.h.

◆ RZ_NEW0

#define RZ_NEW0 (   x)    (x *)calloc(1, sizeof(x))

Definition at line 21 of file demangler_util.h.

◆ RZ_STR_ISEMPTY

#define RZ_STR_ISEMPTY (   x)    (!(x) || !*(x))

Definition at line 44 of file demangler_util.h.

◆ true

#define true   1

Definition at line 13 of file demangler_util.h.

Typedef Documentation

◆ bool

typedef int bool

Definition at line 12 of file demangler_util.h.

◆ DemList

typedef struct dem_list_t DemList

◆ DemListFree

typedef void(* DemListFree) (void *ptr)

Definition at line 66 of file demangler_util.h.

◆ DemListIter

typedef struct dem_list_iter_t DemListIter

◆ ut16

typedef uint16_t ut16

Definition at line 30 of file demangler_util.h.

◆ ut32

typedef uint32_t ut32

Definition at line 31 of file demangler_util.h.

◆ ut8

typedef uint8_t ut8

Definition at line 29 of file demangler_util.h.

Function Documentation

◆ dem_list_append()

DemListIter* dem_list_append ( DemList list,
void *  data 
)

Definition at line 229 of file demangler_util.c.

229  {
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 }
#define dem_return_val_if_fail(expr, val)
Definition: demangler_util.c:8
#define RZ_NEW(x)
Definition: rz_types.h:285
struct dem_list_iter_t * n
struct dem_list_iter_t * p

References dem_list_iter_t::data, dem_return_val_if_fail, list(), dem_list_iter_t::n, NULL, dem_list_iter_t::p, and RZ_NEW.

Referenced by get_namespace_and_name(), get_operator_code(), get_template(), and parse_function_args().

◆ dem_list_free()

void dem_list_free ( DemList list)

Definition at line 299 of file demangler_util.c.

299  {
300  if (list) {
302  free(list);
303  }
304 }
void dem_list_purge(DemList *list)
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References dem_list_purge(), free(), and list().

Referenced by get_namespace_and_name(), get_operator_code(), get_template(), and microsoft_demangle().

◆ dem_list_get_n()

void* dem_list_get_n ( const DemList list,
ut32  n 
)

Definition at line 306 of file demangler_util.c.

306  {
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 }
lzma_index ** i
Definition: index.h:629
uint32_t ut32
int n
Definition: mipsasm.c:19

References dem_list_iter_t::data, dem_return_val_if_fail, i, list(), n, dem_list_iter_t::n, and NULL.

Referenced by get_namespace_and_name(), and parse_function_args().

◆ dem_list_length()

ut32 dem_list_length ( const DemList list)

Definition at line 253 of file demangler_util.c.

253  {
255  return list->length;
256 }

References dem_return_val_if_fail, and list().

Referenced by get_namespace_and_name().

◆ dem_list_newf()

DemList* dem_list_newf ( DemListFree  f)

Definition at line 221 of file demangler_util.c.

221  {
222  DemList *l = RZ_NEW0(DemList);
223  if (l) {
224  l->free = f;
225  }
226  return l;
227 }
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define f(i)
Definition: sha256.c:46
DemListFree free

References f, dem_list_t::free, and RZ_NEW0.

Referenced by get_namespace_and_name(), get_template(), and microsoft_demangle().

◆ dem_str_append()

char* dem_str_append ( char *  ptr,
const char *  string 
)

Definition at line 112 of file demangler_util.c.

112  {
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 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
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")
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67

References free(), memcpy(), NULL, realloc(), RZ_STR_ISEMPTY, and strdup().

Referenced by get_operator_code(), get_ptr_modifier(), and parse_microsoft_mangled_name().

◆ dem_str_ndup()

char* dem_str_ndup ( const char *  ptr,
int  len 
)

Definition at line 78 of file demangler_util.c.

78  {
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 }
size_t len
Definition: 6502dis.c:15
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
void * malloc(size_t size)
Definition: malloc.c:123

References len, malloc(), NULL, and out.

Referenced by get_template().

◆ dem_str_newf()

char* dem_str_newf ( const char *  fmt,
  ... 
)

Definition at line 91 of file demangler_util.c.

91  {
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 }
vsnprintf
Definition: kernel.h:366
void * p
Definition: libc.cpp:67
void * calloc(size_t number, size_t size)
Definition: malloc.c:102

References calloc(), dem_return_val_if_fail, NULL, p, strdup(), and vsnprintf.

Referenced by get_namespace_and_name(), get_num(), get_operator_code(), get_template_params(), libdemangle_handler_objc(), parse_data_type(), parse_microsoft_mangled_name(), and parse_microsoft_rtti_mangled_name().

◆ dem_str_replace()

char* dem_str_replace ( char *  str,
const char *  key,
const char *  val,
int  g 
)

Definition at line 32 of file demangler_util.c.

32  {
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 }
ut16 val
Definition: armass64_const.h:6
static RASN1String * newstr(const char *string)
Definition: astr.c:23
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_str_replace_char(char *string, size_t size, char ch, char rp)
struct @667 g
int off
Definition: pal.c:13
#define RZ_FREE(x)
Definition: rz_types.h:369
static int
Definition: sfsocketcall.h:114

References dem_return_val_if_fail, dem_str_replace_char(), g, i, int, key, memcpy(), newstr(), NULL, off, p, realloc(), RZ_FREE, cmd_descs_generate::str, and val.

Referenced by libdemangle_handler_java(), and parse_function().

◆ dem_str_replace_char()

void dem_str_replace_char ( char *  string,
size_t  size,
char  ch,
char  rp 
)

Definition at line 24 of file demangler_util.c.

24  {
25  for (size_t i = 0; i < size; ++i) {
26  if (string[i] == ch) {
27  string[i] = rp;
28  }
29  }
30 }
static char * rp[]
Definition: i8080dis.c:36
voidpf void uLong size
Definition: ioapi.h:138

References i, and rp.

Referenced by dem_str_replace(), and dem_string_replace_char().

◆ dem_string_append()

bool dem_string_append ( DemString ds,
const char *  string 
)

Definition at line 167 of file demangler_util.c.

167  {
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 }
bool dem_string_append_n(DemString *ds, const char *string, size_t size)

References dem_return_val_if_fail, and dem_string_append_n().

Referenced by demangle_method(), demangle_type(), and get_operator_code().

◆ dem_string_append_n()

bool dem_string_append_n ( DemString ds,
const char *  string,
size_t  size 
)

Definition at line 173 of file demangler_util.c.

173  {
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 }
static bool dem_string_realloc_no_len_update(DemString *ds, ssize_t size)
size_t len
char * buf

References DemString::buf, dem_return_val_if_fail, dem_string_realloc_no_len_update(), DemString::len, and memcpy().

Referenced by dem_string_append(), demangle_type(), and get_operator_code().

◆ dem_string_appendf()

bool dem_string_appendf ( DemString ds,
const char *  fmt,
  ... 
)

Definition at line 187 of file demangler_util.c.

187  {
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 }

References DemString::buf, dem_return_val_if_fail, dem_string_realloc_no_len_update(), DemString::len, NULL, and vsnprintf.

Referenced by demangle_class_object(), demangle_object_with_type(), and get_operator_code().

◆ dem_string_drain()

char* dem_string_drain ( DemString ds)

Definition at line 160 of file demangler_util.c.

160  {
162  char *ret = ds->buf;
163  free(ds);
164  return ret;
165 }

References DemString::buf, dem_return_val_if_fail, free(), and NULL.

Referenced by demangle_any(), demangle_class_object(), demangle_method(), demangle_object_with_type(), and get_operator_code().

◆ dem_string_free()

void dem_string_free ( DemString ds)

Definition at line 131 of file demangler_util.c.

131  {
132  if (!ds) {
133  return;
134  }
135  free(ds->buf);
136  free(ds);
137 }

References DemString::buf, and free().

Referenced by demangle_any(), demangle_class_object(), demangle_method(), demangle_object_with_type(), and get_operator_code().

◆ dem_string_new()

DemString* dem_string_new ( )

Definition at line 139 of file demangler_util.c.

139  {
140  return RZ_NEW0(DemString);
141 }

References RZ_NEW0.

Referenced by demangle_any(), demangle_class_object(), demangle_method(), demangle_object_with_type(), and get_operator_code().

◆ dem_string_replace_char()

void dem_string_replace_char ( DemString ds,
char  ch,
char  rp 
)

Definition at line 214 of file demangler_util.c.

214  {
215  if (!ds->buf) {
216  return;
217  }
218  dem_str_replace_char(ds->buf, ds->len, ch, rp);
219 }

References DemString::buf, dem_str_replace_char(), DemString::len, and rp.

Referenced by demangle_any(), demangle_class_object(), demangle_method(), and demangle_object_with_type().