2011-01-26 20:54:39 +00:00
|
|
|
/* radare - LGPL - Copyright 2010-2011 - pancake<nopcode.org> */
|
2010-06-16 07:42:46 +00:00
|
|
|
|
|
|
|
#include <r_anal.h>
|
|
|
|
|
2014-03-26 13:47:30 +00:00
|
|
|
R_API RAnalValue *r_anal_value_new() { //makro for this ?
|
|
|
|
return R_NEW0 (RAnalValue);
|
2010-06-16 07:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API RAnalValue *r_anal_value_new_from_string(const char *str) {
|
|
|
|
/* TODO */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:26:13 +00:00
|
|
|
R_API RAnalValue *r_anal_value_copy(RAnalValue *ov) {
|
2016-05-19 15:20:35 +00:00
|
|
|
RAnalValue *v = R_NEW0 (RAnalValue);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!v) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-28 08:24:01 +00:00
|
|
|
memcpy (v, ov, sizeof (RAnalValue));
|
|
|
|
// reference to reg and regdelta should be kept
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-06-16 07:42:46 +00:00
|
|
|
// TODO: move into .h as #define free
|
|
|
|
R_API void r_anal_value_free(RAnalValue *value) {
|
2016-06-27 21:26:13 +00:00
|
|
|
free (value);
|
|
|
|
#if 0
|
2015-03-24 02:23:56 +00:00
|
|
|
ut64 pval = (ut64)(size_t)value;
|
|
|
|
if (pval && pval != UT64_MAX) {
|
|
|
|
/* TODO: free RRegItem objects? */
|
|
|
|
free (value);
|
|
|
|
}
|
2016-06-27 21:26:13 +00:00
|
|
|
#endif
|
2010-06-16 07:42:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 23:20:39 +00:00
|
|
|
// mul*value+regbase+regidx+delta
|
2010-06-17 22:53:47 +00:00
|
|
|
R_API ut64 r_anal_value_to_ut64(RAnal *anal, RAnalValue *val) {
|
|
|
|
ut64 num;
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!val) {
|
2010-06-17 22:53:47 +00:00
|
|
|
return 0LL;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2010-06-17 22:53:47 +00:00
|
|
|
num = val->base + (val->delta*(val->mul?val->mul:1));
|
2018-09-13 08:17:26 +00:00
|
|
|
if (val->reg) {
|
2010-06-17 22:53:47 +00:00
|
|
|
num += r_reg_get_value (anal->reg, val->reg);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
|
|
|
if (val->regdelta) {
|
2010-06-17 22:53:47 +00:00
|
|
|
num += r_reg_get_value (anal->reg, val->regdelta);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2010-06-17 22:53:47 +00:00
|
|
|
switch (val->memref) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
case 8:
|
|
|
|
//anal->bio ...
|
|
|
|
eprintf ("TODO: memref for to_ut64 not supported\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return num;
|
2010-06-16 07:42:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 23:20:39 +00:00
|
|
|
R_API int r_anal_value_set_ut64(RAnal *anal, RAnalValue *val, ut64 num) {
|
|
|
|
if (val->memref) {
|
|
|
|
if (anal->iob.io) {
|
|
|
|
ut8 data[8];
|
|
|
|
ut64 addr = r_anal_value_to_ut64 (anal, val);
|
2016-04-26 09:09:15 +00:00
|
|
|
r_mem_set_num (data, val->memref, num);
|
2011-02-02 23:20:39 +00:00
|
|
|
anal->iob.write_at (anal->iob.io, addr, data, val->memref);
|
2018-09-13 08:17:26 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("No IO binded to r_anal\n");
|
|
|
|
}
|
2011-02-02 23:20:39 +00:00
|
|
|
} else {
|
2018-09-13 08:17:26 +00:00
|
|
|
if (val->reg) {
|
2014-03-26 13:47:30 +00:00
|
|
|
r_reg_set_value (anal->reg, val->reg, num);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2011-02-02 23:20:39 +00:00
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
return false; //is this necessary
|
2011-02-02 23:20:39 +00:00
|
|
|
}
|
|
|
|
|
2010-06-16 07:42:46 +00:00
|
|
|
R_API char *r_anal_value_to_string (RAnalValue *value) {
|
2011-02-27 15:17:05 +00:00
|
|
|
char *out = NULL;
|
2010-06-16 19:44:19 +00:00
|
|
|
if (value) {
|
2011-02-27 15:17:05 +00:00
|
|
|
out = r_str_new ("");
|
2011-02-28 12:07:41 +00:00
|
|
|
if (!value->base && !value->reg) {
|
2018-09-13 08:17:26 +00:00
|
|
|
if (value->imm != -1LL) {
|
2017-03-16 21:29:49 +00:00
|
|
|
out = r_str_appendf (out, "0x%"PFMT64x, value->imm);
|
2018-09-13 08:17:26 +00:00
|
|
|
} else {
|
|
|
|
out = r_str_append (out, "-1");
|
|
|
|
}
|
2011-02-27 15:17:05 +00:00
|
|
|
} else {
|
|
|
|
if (value->memref) {
|
|
|
|
switch (value->memref) {
|
2017-03-16 21:29:49 +00:00
|
|
|
case 1: out = r_str_append (out, "(char)"); break;
|
|
|
|
case 2: out = r_str_append (out, "(short)"); break;
|
|
|
|
case 4: out = r_str_append (out, "(word)"); break;
|
|
|
|
case 8: out = r_str_append (out, "(dword)"); break;
|
2011-02-27 15:17:05 +00:00
|
|
|
}
|
2017-03-16 21:29:49 +00:00
|
|
|
out = r_str_append (out, "[");
|
2010-06-16 23:48:51 +00:00
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
if (value->mul) {
|
|
|
|
out = r_str_appendf (out, "%d*", value->mul);
|
|
|
|
}
|
|
|
|
if (value->reg) {
|
|
|
|
out = r_str_appendf (out, "%s", value->reg->name);
|
|
|
|
}
|
|
|
|
if (value->regdelta) {
|
|
|
|
out = r_str_appendf (out, "+%s", value->regdelta->name);
|
|
|
|
}
|
|
|
|
if (value->base != 0) {
|
|
|
|
out = r_str_appendf (out, "0x%" PFMT64x, value->base);
|
|
|
|
}
|
|
|
|
if (value->delta > 0) {
|
|
|
|
out = r_str_appendf (out, "+0x%" PFMT64x, value->delta);
|
|
|
|
} else if (value->delta < 0) {
|
|
|
|
out = r_str_appendf (out, "-0x%" PFMT64x, -value->delta);
|
|
|
|
}
|
|
|
|
if (value->memref) {
|
|
|
|
out = r_str_append (out, "]");
|
|
|
|
}
|
2010-06-16 23:48:51 +00:00
|
|
|
}
|
2010-06-16 19:44:19 +00:00
|
|
|
}
|
2010-06-16 07:42:46 +00:00
|
|
|
return out;
|
|
|
|
}
|