Rizin
unix-like reverse engineering framework and cli tools
rust.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 #include "demangler_util.h"
4 #include <rz_libdemangle.h>
5 
6 #define RS(from, to) \
7  if (replace_seq((const char **)&in, &out, (const char *)(from), to)) \
8  continue
9 
10 static bool replace_seq(const char **in, char **out, const char *seq, char value) {
11  size_t len = strlen(seq);
12 
13  if (strncmp(*in, seq, len)) {
14  return false;
15  }
16 
17  **out = value;
18 
19  *in += len;
20  *out += 1;
21 
22  return true;
23 }
24 
25 char *libdemangle_handler_rust(const char *sym) {
26  int len;
27  char *str = NULL, *out, *in;
28 
29  str = libdemangle_handler_cxx(sym);
30 
31  if (!str) {
32  return str;
33  }
34 
35  out = in = str;
36  len = strlen(str);
37 
38  if (*in == '_') {
39  in++;
40  len--;
41  }
42 
43  while ((len = strlen(in)) > 0) {
44  if (*in == '$') {
45  RS("$SP$", '@');
46  RS("$BP$", '*');
47  RS("$RF$", '&');
48  RS("$LT$", '<');
49  RS("$GT$", '>');
50  RS("$LP$", '(');
51  RS("$RP$", ')');
52  RS("$C$", ',');
53  // maybe a good idea to replace all utf-sequences by regexp \$u[0-9a-f]{2}\$ or so
54  RS("$u20$", ' ');
55  RS("$u22$", '\"');
56  RS("$u27$", '\'');
57  RS("$u2b$", '+');
58  RS("$u3b$", ';');
59  RS("$u5b$", '[');
60  RS("$u5d$", ']');
61  RS("$u7e$", '~');
62  }
63  if (*in == '.') {
64  if (len > 0 && in[1] == '.') {
65  in += 2;
66  *out++ = ':';
67  *out++ = ':';
68  len--;
69  } else {
70  in += 1;
71  *out = '-';
72  }
73  } else {
74  *out++ = *in++;
75  }
76  }
77  *out = '\0';
78 
79  return str;
80 }
size_t len
Definition: 6502dis.c:15
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static int value
Definition: cmd_api.c:93
#define NULL
Definition: cris-opc.c:27
static bool replace_seq(const char **in, char **out, const char *seq, char value)
Definition: rust.c:10
#define RS(from, to)
Definition: rust.c:6
char * libdemangle_handler_rust(const char *sym)
Definition: rust.c:25