Rizin
unix-like reverse engineering framework and cli tools
parse.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 nibble <nibble.ds@gmail.com>
2 // SPDX-FileCopyrightText: 2009-2020 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2009-2020 maijin <maijin21@gmail.com>
4 // SPDX-License-Identifier: LGPL-3.0-only
5 
6 #include <stdio.h>
7 
8 #include <rz_types.h>
9 #include <rz_parse.h>
10 #include <config.h>
11 
12 RZ_LIB_VERSION(rz_parse);
13 
15 
17  int i;
19  if (!p) {
20  return NULL;
21  }
22  p->parsers = rz_list_newf(NULL); // memleak
23  if (!p->parsers) {
25  return NULL;
26  }
27  p->notin_flagspace = NULL;
28  p->flagspace = NULL;
29  p->pseudo = false;
30  p->subrel = false;
31  p->subtail = false;
32  p->minval = 0x100;
33  p->localvar_only = false;
34  for (i = 0; i < RZ_ARRAY_SIZE(parse_static_plugins); i++) {
36  }
37  return p;
38 }
39 
41  rz_list_free(p->parsers);
42  free(p);
43 }
44 
46  bool itsFine = true;
47  if (foo->init) {
48  itsFine = foo->init(p, p->user);
49  }
50  if (itsFine) {
51  rz_list_append(p->parsers, foo);
52  }
53  return true;
54 }
55 
56 RZ_API bool rz_parse_use(RzParse *p, const char *name) {
59  rz_return_val_if_fail(p && name, false);
60  rz_list_foreach (p->parsers, iter, h) {
61  if (!strcmp(h->name, name)) {
62  p->cur = h;
63  return true;
64  }
65  }
66  p->cur = NULL;
67  return false;
68 }
69 
70 // this function is a bit confussing, assembles C code into wat?, whehres theh input and wheres the output
71 // and its unused. so imho it sshould be DEPRECATED this conflicts with rasm.assemble imhoh
72 RZ_API bool rz_parse_assemble(RzParse *p, char *data, char *str) {
73  char *in = strdup(str);
74  bool ret = false;
75  char *s, *o;
76 
77  data[0] = '\0';
78  if (p->cur && p->cur->assemble) {
79  o = data + strlen(data);
80  do {
81  s = strchr(str, ';');
82  if (s) {
83  *s = '\0';
84  }
85  ret = p->cur->assemble(p, o, str);
86  if (!ret) {
87  break;
88  }
89  if (s) {
90  str = s + 1;
91  o += strlen(data);
92  o[0] = '\n';
93  o[1] = '\0';
94  o++;
95  }
96  } while (s);
97  }
98  free(in);
99  return ret;
100 }
101 
107 RZ_API char *rz_parse_pseudocode(RzParse *p, const char *assembly) {
109  if (RZ_STR_ISEMPTY(assembly)) {
110  return NULL;
111  }
112 
113  RzStrBuf *sb = rz_strbuf_new("");
114  if (!sb) {
115  return NULL;
116  }
117 
118  rz_strbuf_reserve(sb, 128);
119  if (!p->cur || !p->cur->parse || !p->cur->parse(p, assembly, sb)) {
121  return NULL;
122  }
123 
124  return rz_strbuf_drain(sb);
125 }
126 
128  if (!opstr || !*opstr) {
129  return NULL;
130  }
131  char *n = strstr(opstr, "0x");
132  if (n) {
133  char *p = n + 2;
134  while (IS_HEXCHAR(*p)) {
135  p++;
136  }
137  memmove(n, p, strlen(p) + 1);
138  }
139  if (strstr(opstr, " - ]")) {
140  opstr = rz_str_replace(opstr, " - ]", "]", 1);
141  }
142  if (strstr(opstr, " + ]")) {
143  opstr = rz_str_replace(opstr, " + ]", "]", 1);
144  }
145  if (strstr(opstr, ", ]")) {
146  opstr = rz_str_replace(opstr, ", ]", "]", 1);
147  }
148  if (strstr(opstr, " - ")) {
149  opstr = rz_str_replace(opstr, " - ", "-", 1);
150  }
151  if (strstr(opstr, " + ")) {
152  opstr = rz_str_replace(opstr, " + ", "+", 1);
153  }
154  return opstr;
155 }
156 
157 /*
158  * \brief Substitutes register relative accesses with function variable names
159  * \param p The parser
160  * \param f The function
161  * \param op The analysis op of the current instruction
162  * \param data The disassembly of the current instruction
163  * \param str The string buffer to write the output to
164  * \param len The length of the string buffer
165  */
167  rz_return_val_if_fail(op && data && str, false);
168  if (p->cur && p->cur->subvar) {
169  return p->cur->subvar(p, f, op, data, str, len);
170  }
171  return false;
172 }
173 
174 /* setters */
175 RZ_API void rz_parse_set_user_ptr(RzParse *p, void *user) {
176  p->user = user;
177 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static int opstr(RzAsm *a, ut8 *data, const Opcode *op)
Definition: asm_x86_nz.c:4054
static SblHeader sb
Definition: bin_mbn.c:26
const lzma_allocator const uint8_t * in
Definition: block.h:527
#define RZ_PARSE_STATIC_PLUGINS
Definition: config.h:26
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
static RzMain foo[]
Definition: main.c:11
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
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
RZ_API char * rz_parse_immtrim(char *opstr)
Definition: parse.c:127
RZ_API void rz_parse_set_user_ptr(RzParse *p, void *user)
Definition: parse.c:175
RZ_API void rz_parse_free(RzParse *p)
Definition: parse.c:40
RZ_API char * rz_parse_pseudocode(RzParse *p, const char *assembly)
Converts the assembly line into pseudocode.
Definition: parse.c:107
RZ_LIB_VERSION(rz_parse)
RZ_API bool rz_parse_add(RzParse *p, RzParsePlugin *foo)
Definition: parse.c:45
RZ_API RzParse * rz_parse_new(void)
Definition: parse.c:16
static RzParsePlugin * parse_static_plugins[]
Definition: parse.c:14
RZ_API bool rz_parse_use(RzParse *p, const char *name)
Definition: parse.c:56
RZ_API bool rz_parse_subvar(RzParse *p, RZ_NULLABLE RzAnalysisFunction *f, RZ_NONNULL RzAnalysisOp *op, RZ_NONNULL RZ_IN char *data, RZ_BORROW RZ_NONNULL RZ_OUT char *str, int len)
Definition: parse.c:166
RZ_API bool rz_parse_assemble(RzParse *p, char *data, char *str)
Definition: parse.c:72
static RzSocket * s
Definition: rtr.c:28
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110
#define IS_HEXCHAR(x)
Definition: rz_str_util.h:9
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API bool rz_strbuf_reserve(RzStrBuf *sb, size_t len)
Definition: strbuf.c:67
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
#define RZ_IN
Definition: rz_types.h:50
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
#define RZ_BORROW
Definition: rz_types.h:63
#define f(i)
Definition: sha256.c:46
#define h(i)
Definition: sha256.c:48
Definition: z80asm.h:102
Definition: dis.c:32