2020-01-16 21:15:42 +01:00
|
|
|
/* radare2 - LGPL - Copyright 2009-2020 - nibble, pancake, maijin */
|
2009-02-24 15:58:21 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_parse.h>
|
2017-05-26 02:43:26 +02:00
|
|
|
#include <config.h>
|
2009-08-14 01:44:12 +00:00
|
|
|
|
2013-06-20 09:54:42 +02:00
|
|
|
R_LIB_VERSION (r_parse);
|
2013-06-14 02:51:33 +02:00
|
|
|
|
2012-07-22 12:00:35 +04:00
|
|
|
static RParsePlugin *parse_static_plugins[] =
|
2009-08-14 01:44:12 +00:00
|
|
|
{ R_PARSE_STATIC_PLUGINS };
|
2009-02-24 15:58:21 +01:00
|
|
|
|
2019-06-17 02:23:58 +02:00
|
|
|
R_API RParse *r_parse_new(void) {
|
2010-05-20 17:40:58 +02:00
|
|
|
int i;
|
2015-09-28 11:21:23 +02:00
|
|
|
RParse *p = R_NEW0 (RParse);
|
2016-11-02 22:49:36 +01:00
|
|
|
if (!p) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-06-17 02:23:58 +02:00
|
|
|
p->parsers = r_list_newf (NULL); // memleak
|
2016-05-24 21:22:15 +01:00
|
|
|
if (!p->parsers) {
|
|
|
|
r_parse_free (p);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-02-03 16:06:45 +01:00
|
|
|
p->notin_flagspace = NULL;
|
|
|
|
p->flagspace = NULL;
|
2018-05-11 15:40:25 +08:00
|
|
|
p->pseudo = false;
|
2020-07-06 14:34:50 +05:30
|
|
|
p->subrel = false;
|
|
|
|
p->subtail = false;
|
2017-06-28 01:29:04 +02:00
|
|
|
p->minval = 0x100;
|
2017-03-17 11:54:34 +01:00
|
|
|
p->localvar_only = false;
|
2016-11-02 22:49:36 +01:00
|
|
|
for (i = 0; parse_static_plugins[i]; i++) {
|
2016-04-13 17:24:23 +05:30
|
|
|
r_parse_add (p, parse_static_plugins[i]);
|
2009-09-24 12:29:05 +02:00
|
|
|
}
|
|
|
|
return p;
|
2009-02-24 15:58:21 +01:00
|
|
|
}
|
|
|
|
|
2011-10-05 02:38:37 +02:00
|
|
|
R_API void r_parse_free(RParse *p) {
|
2012-11-13 00:53:52 +01:00
|
|
|
r_list_free (p->parsers);
|
2011-10-05 02:38:37 +02:00
|
|
|
free (p);
|
2010-05-20 17:40:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 02:23:58 +02:00
|
|
|
R_API bool r_parse_add(RParse *p, RParsePlugin *foo) {
|
|
|
|
bool itsFine = true;
|
2016-10-18 01:49:32 +02:00
|
|
|
if (foo->init) {
|
2019-06-17 02:23:58 +02:00
|
|
|
itsFine = foo->init (p, p->user);
|
|
|
|
}
|
|
|
|
if (itsFine) {
|
|
|
|
r_list_append (p->parsers, foo);
|
2016-10-18 01:49:32 +02:00
|
|
|
}
|
2015-09-14 02:08:31 +02:00
|
|
|
return true;
|
2009-02-24 15:58:21 +01:00
|
|
|
}
|
|
|
|
|
2019-06-17 02:23:58 +02:00
|
|
|
R_API bool r_parse_use(RParse *p, const char *name) {
|
2012-11-13 00:53:52 +01:00
|
|
|
RListIter *iter;
|
|
|
|
RParsePlugin *h;
|
2019-06-17 02:23:58 +02:00
|
|
|
r_return_val_if_fail (p && name, false);
|
2012-11-13 00:53:52 +01:00
|
|
|
r_list_foreach (p->parsers, iter, h) {
|
2011-10-05 02:38:37 +02:00
|
|
|
if (!strcmp (h->name, name)) {
|
2009-02-24 15:58:21 +01:00
|
|
|
p->cur = h;
|
2015-09-14 02:08:31 +02:00
|
|
|
return true;
|
2009-02-24 15:58:21 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-14 02:08:31 +02:00
|
|
|
return false;
|
2009-02-24 15:58:21 +01:00
|
|
|
}
|
|
|
|
|
2019-06-17 02:23:58 +02:00
|
|
|
// this function is a bit confussing, assembles C code into wat?, whehres theh input and wheres the output
|
|
|
|
// and its unused. so imho it sshould be DEPRECATED this conflicts with rasm.assemble imhoh
|
|
|
|
R_API bool r_parse_assemble(RParse *p, char *data, char *str) {
|
2011-10-05 02:38:37 +02:00
|
|
|
char *in = strdup (str);
|
2019-06-17 02:23:58 +02:00
|
|
|
bool ret = false;
|
2009-09-24 12:29:05 +02:00
|
|
|
char *s, *o;
|
2009-08-14 02:19:54 +00:00
|
|
|
|
|
|
|
data[0]='\0';
|
|
|
|
if (p->cur && p->cur->assemble) {
|
2016-11-02 22:49:36 +01:00
|
|
|
o = data + strlen (data);
|
2009-08-14 02:19:54 +00:00
|
|
|
do {
|
2011-10-05 02:38:37 +02:00
|
|
|
s = strchr (str, ';');
|
2016-11-02 22:49:36 +01:00
|
|
|
if (s) {
|
|
|
|
*s = '\0';
|
|
|
|
}
|
2011-10-05 02:38:37 +02:00
|
|
|
ret = p->cur->assemble (p, o, str);
|
2016-11-02 22:49:36 +01:00
|
|
|
if (!ret) {
|
|
|
|
break;
|
|
|
|
}
|
2009-08-14 02:19:54 +00:00
|
|
|
if (s) {
|
|
|
|
str = s + 1;
|
2019-04-25 02:22:38 +02:00
|
|
|
o += strlen (data);
|
2016-10-18 01:49:32 +02:00
|
|
|
o[0] = '\n';
|
|
|
|
o[1] = '\0';
|
2011-10-05 02:38:37 +02:00
|
|
|
o++;
|
2009-08-14 02:19:54 +00:00
|
|
|
}
|
2011-10-05 02:38:37 +02:00
|
|
|
} while (s);
|
2009-08-14 02:19:54 +00:00
|
|
|
}
|
2011-10-05 02:38:37 +02:00
|
|
|
free (in);
|
2009-08-14 02:19:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-15 13:24:00 +02:00
|
|
|
// data is input disasm, str is output pseudo
|
2019-04-25 02:22:38 +02:00
|
|
|
// TODO: refactooring, this should return char * instead
|
2019-06-15 13:24:00 +02:00
|
|
|
R_API bool r_parse_parse(RParse *p, const char *data, char *str) {
|
|
|
|
r_return_val_if_fail (p && data && str, false);
|
2020-01-16 21:15:42 +01:00
|
|
|
return (p && data && *data && p->cur && p->cur->parse)
|
|
|
|
? p->cur->parse (p, data, str) : false;
|
2011-02-23 15:17:06 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 00:01:02 +08:00
|
|
|
R_API char *r_parse_immtrim(char *opstr) {
|
2017-11-29 18:56:12 +01:00
|
|
|
if (!opstr || !*opstr) {
|
2018-08-13 16:59:08 +02:00
|
|
|
return NULL;
|
2017-11-29 18:56:12 +01:00
|
|
|
}
|
|
|
|
char *n = strstr (opstr, "0x");
|
|
|
|
if (n) {
|
|
|
|
char *p = n + 2;
|
2018-05-21 18:05:46 +02:00
|
|
|
while (IS_HEXCHAR (*p)) {
|
2017-11-29 18:56:12 +01:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
memmove (n, p, strlen (p) + 1);
|
|
|
|
}
|
2017-11-30 00:14:08 +01:00
|
|
|
if (strstr (opstr, " - ]")) {
|
2018-08-13 16:59:08 +02:00
|
|
|
opstr = r_str_replace (opstr, " - ]", "]", 1);
|
2017-11-30 00:14:08 +01:00
|
|
|
}
|
|
|
|
if (strstr (opstr, " + ]")) {
|
2018-08-13 16:59:08 +02:00
|
|
|
opstr = r_str_replace (opstr, " + ]", "]", 1);
|
2017-11-30 00:14:08 +01:00
|
|
|
}
|
|
|
|
if (strstr (opstr, ", ]")) {
|
2018-08-13 16:59:08 +02:00
|
|
|
opstr = r_str_replace (opstr, ", ]", "]", 1);
|
2017-11-30 00:14:08 +01:00
|
|
|
}
|
2017-11-29 18:56:12 +01:00
|
|
|
if (strstr (opstr, " - ")) {
|
2018-08-13 16:59:08 +02:00
|
|
|
opstr = r_str_replace (opstr, " - ", "-", 1);
|
2017-11-29 18:56:12 +01:00
|
|
|
}
|
|
|
|
if (strstr (opstr, " + ")) {
|
2018-08-13 16:59:08 +02:00
|
|
|
opstr = r_str_replace (opstr, " + ", "+", 1);
|
2017-11-29 18:56:12 +01:00
|
|
|
}
|
2018-08-13 16:59:08 +02:00
|
|
|
return opstr;
|
2017-11-29 18:56:12 +01:00
|
|
|
}
|
|
|
|
|
2020-09-28 15:21:38 +05:30
|
|
|
R_API bool r_parse_subvar(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
|
|
|
|
if (p->cur && p->cur->subvar) {
|
|
|
|
return p->cur->subvar (p, f, addr, oplen, data, str, len);
|
2015-11-27 18:27:44 +01:00
|
|
|
}
|
2015-09-14 02:08:31 +02:00
|
|
|
return false;
|
2009-02-24 15:58:21 +01:00
|
|
|
}
|
2012-11-13 00:53:52 +01:00
|
|
|
|
|
|
|
/* setters */
|
|
|
|
R_API void r_parse_set_user_ptr(RParse *p, void *user) {
|
|
|
|
p->user = user;
|
|
|
|
}
|