Added RConfigHold (#6250)

This commit is contained in:
Álvaro Felipe Melchor 2016-11-23 18:36:11 +01:00 committed by radare
parent 85a02e0f29
commit a11477769c
7 changed files with 147 additions and 81 deletions

View File

@ -119,9 +119,10 @@ R_API void r_config_list(RConfig *cfg, const char *str, int rad) {
}
}
R_API RConfigNode *r_config_node_get(RConfig *cfg, const char *name) {
if (strnull (name))
R_API RConfigNode *r_config_node_get(RConfig *cfg, const char *name) {
if (strnull (name)) {
return NULL;
}
return r_hashtable_lookup (cfg->ht, r_str_hash (name));
}
@ -154,7 +155,9 @@ static bool is_bool(const char *s) {
R_API const char *r_config_get(RConfig *cfg, const char *name) {
RConfigNode *node = r_config_node_get (cfg, name);
if (node) {
if (node->getter) node->getter (cfg->user, node);
if (node->getter) {
node->getter (cfg->user, node);
}
cfg->last_notfound = 0;
if (node->flags & CN_BOOL) {
return r_str_bool (is_true (node->value));
@ -179,10 +182,12 @@ R_API int r_config_toggle(RConfig *cfg, const char *name) {
R_API ut64 r_config_get_i(RConfig *cfg, const char *name) {
RConfigNode *node = r_config_node_get (cfg, name);
if (node) {
if (node->getter)
if (node->getter) {
node->getter (cfg->user, node);
if (node->i_value != 0 || !strcmp (node->value, "false"))
}
if (node->i_value || !strcmp (node->value, "false")) {
return node->i_value;
}
return (ut64)r_num_math (cfg->num, node->value);
}
return (ut64)0LL;
@ -500,3 +505,77 @@ R_API void r_config_bump(RConfig *cfg, const char *key) {
r_config_set (cfg, key, orig);
free (orig);
}
R_API bool r_config_save_char(RConfigHold *h, ...) {
va_list ap;
char *key;
if (!h->list_char) {
h->list_char = r_list_newf ((RListFree)free);
if (!h->list_char) {
return false;
}
}
va_start (ap, h);
while ((key = va_arg (ap, char*))) {
RConfigHoldChar *hc = R_NEW0 (RConfigHoldChar);
if (!hc) {
continue;
}
hc->key = key;
hc->value = r_config_get (h->cfg, key);
r_list_append (h->list_char, hc);
}
va_end (ap);
return true;
}
R_API bool r_config_save_num(RConfigHold *h, ...) {
va_list ap;
char *key;
if (!h->list_num) {
h->list_num = r_list_newf ((RListFree)free);
if (!h->list_num) {
return false;
}
}
va_start (ap, h);
while ((key = va_arg (ap, char*))) {
RConfigHoldNum *hc = R_NEW0 (RConfigHoldNum);
if (!hc) {
continue;
}
hc->key = key;
hc->value = r_config_get_i (h->cfg, key);
r_list_append (h->list_num, hc);
}
va_end (ap);
return true;
}
R_API RConfigHold* r_config_hold_new(RConfig *cfg) {
RConfigHold* hold = R_NEW0 (RConfigHold);
if (hold) {
hold->cfg = cfg;
return hold;
}
return NULL;
}
R_API void r_config_restore(RConfigHold *h) {
RListIter *iter;
RConfigHoldChar *hchar;
RConfigHoldNum *hnum;
r_list_foreach (h->list_num, iter, hnum) {
r_config_set_i (h->cfg, hnum->key, hnum->value);
}
r_list_foreach (h->list_char, iter, hchar) {
r_config_set (h->cfg, hchar->key, hchar->value);
}
}
R_API void r_config_hold_free(RConfigHold* h) {
r_list_free (h->list_num);
r_list_free (h->list_char);
R_FREE (h);
}

View File

@ -2236,7 +2236,7 @@ R_API int r_core_anal_graph(RCore *core, ut64 addr, int opts) {
int is_html = r_cons_singleton ()->is_html;
int is_json = opts & R_CORE_ANAL_JSON;
int is_keva = opts & R_CORE_ANAL_KEYVALUE;
int reflines, bytes, dwarf;
RConfigHold *hc;
RAnalFunction *fcni;
RListIter *iter;
int nodes = 0;
@ -2249,11 +2249,12 @@ R_API int r_core_anal_graph(RCore *core, ut64 addr, int opts) {
eprintf ("No functions to diff\n");
return false;
}
hc = r_config_hold_new (core->config);
if (!hc) {
return false;
}
r_config_save_num (hc, "asm.lines", "asm.bytes", "asm.dwarf", NULL);
//opts |= R_CORE_ANAL_GRAPHBODY;
reflines = r_config_get_i (core->config, "asm.lines");
bytes = r_config_get_i (core->config, "asm.bytes");
dwarf = r_config_get_i (core->config, "asm.dwarf");
r_config_set_i (core->config, "asm.lines", 0);
r_config_set_i (core->config, "asm.bytes", 0);
r_config_set_i (core->config, "asm.dwarf", 0);
@ -2303,9 +2304,8 @@ R_API int r_core_anal_graph(RCore *core, ut64 addr, int opts) {
if (is_json) {
r_cons_printf ("]\n");
}
r_config_set_i (core->config, "asm.lines", reflines);
r_config_set_i (core->config, "asm.bytes", bytes);
r_config_set_i (core->config, "asm.dwarf", dwarf);
r_config_restore (hc);
r_config_hold_free (hc);
return true;
}

View File

@ -15,16 +15,13 @@ enum {
STATES_SIZE
};
static bool r_anal_emul_init(RCore *core, bool *state) {
state[ROMEM] = r_config_get_i (core->config, "esil.romem");
static bool r_anal_emul_init(RCore *core, RConfigHold *hc) {
r_config_save_num (hc, "esil.romem", "asm.trace", "anal.trace",
"dbg.trace", "esil.nonull", NULL);
r_config_set (core->config, "esil.romem", "true");
state[ASM_TRACE] = r_config_get_i (core->config, "asm.trace");
r_config_set (core->config, "asm.trace", "true");
state[ANAL_TRACE] = r_config_get_i (core->config, "anal.trace");
r_config_set (core->config, "anal.trace", "true");
state[DBG_TRACE] = r_config_get_i (core->config, "dbg.trace");
r_config_set (core->config, "dbg.trace", "true");
state[NONULL] = r_config_get_i (core->config, "esil.nonull");
r_config_set (core->config, "esil.nonull", "true");
const char *bp = r_reg_get_name (core->anal->reg, R_REG_NAME_BP);
const char *sp = r_reg_get_name (core->anal->reg, R_REG_NAME_SP);
@ -36,13 +33,10 @@ static bool r_anal_emul_init(RCore *core, bool *state) {
return (core->anal->esil != NULL);
}
static void r_anal_emul_restore(RCore *core, bool *state) {
static void r_anal_emul_restore(RCore *core, RConfigHold *hc) {
sdb_reset (core->anal->esil->db_trace);
r_config_set_i (core->config, "esil.romem", state[ROMEM]);
r_config_set_i (core->config, "asm.trace", state[ASM_TRACE]);
r_config_set_i (core->config, "anal.trace", state[ANAL_TRACE]);
r_config_set_i (core->config, "dbg.trace", state[DBG_TRACE]);
r_config_set_i (core->config, "esil.nonull", state[NONULL]);
r_config_restore (hc);
r_config_hold_free (hc);
}
static void type_match(RCore *core, ut64 addr, char *name) {
@ -228,12 +222,16 @@ static int stack_clean (RCore *core, ut64 addr, RAnalFunction *fcn) {
}
R_API void r_core_anal_type_match(RCore *core, RAnalFunction *fcn) {
bool esil_var[STATES_SIZE] = {false};
RConfigHold *hc = NULL;
if (!core) {
return;
}
if (!r_anal_emul_init (core, esil_var) || !fcn) {
r_anal_emul_restore (core, esil_var);
hc = r_config_hold_new (core->config);
if (!hc) {
return;
}
if (!r_anal_emul_init (core, hc) || !fcn) {
r_anal_emul_restore (core, hc);
return;
}
const char *pc = r_reg_get_name (core->anal->reg, R_REG_NAME_PC);
@ -279,5 +277,5 @@ R_API void r_core_anal_type_match(RCore *core, RAnalFunction *fcn) {
}
out:
r_cons_break_pop ();
r_anal_emul_restore (core, esil_var);
r_anal_emul_restore (core, hc);
}

View File

@ -1551,12 +1551,12 @@ static void set_layout(RAGraph *g) {
static char *get_body(RCore *core, ut64 addr, int size, int opts) {
char *body;
const bool o_fcnlines = r_config_get_i (core->config, "asm.fcnlines");
const bool o_lines = r_config_get_i (core->config, "asm.lines");
const bool o_bytes = r_config_get_i (core->config, "asm.bytes");
const int o_cmtcol = r_config_get_i (core->config, "asm.cmtcol");
const bool o_marks = r_config_get_i (core->config, "asm.marks");
const bool o_offset = r_config_get_i (core->config, "asm.offset");
RConfigHold *hc = r_config_hold_new (core->config);
if (!hc) {
return NULL;
}
r_config_save_num (hc, "asm.fcnlines", "asm.lines", "asm.bytes",
"asm.cmtcol", "asm.marks", "asm.marks", "asm.offset", "asm.comments", NULL);
const bool o_comments = r_config_get_i (core->config, "asm.comments");
int o_cursor = core->print->cur_enabled;
@ -1582,13 +1582,8 @@ static char *get_body(RCore *core, ut64 addr, int size, int opts) {
// restore original options
core->print->cur_enabled = o_cursor;
r_config_set_i (core->config, "asm.fcnlines", o_fcnlines);
r_config_set_i (core->config, "asm.lines", o_lines);
r_config_set_i (core->config, "asm.bytes", o_bytes);
r_config_set_i (core->config, "asm.cmtcol", o_cmtcol);
r_config_set_i (core->config, "asm.marks", o_marks);
r_config_set_i (core->config, "asm.offset", o_offset);
r_config_set_i (core->config, "asm.comments", o_comments);
r_config_restore (hc);
r_config_hold_free (hc);
return body;
}

View File

@ -3,28 +3,17 @@
#include <r_core.h>
R_API int r_core_pseudo_code (RCore *core, const char *input) {
R_API int r_core_pseudo_code(RCore *core, const char *input) {
Sdb *db;
ut64 queuegoto = 0LL;
const char *blocktype = "else";
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal,
core->offset, R_ANAL_FCN_TYPE_NULL);
int asmpseudo = r_config_get_i (core->config, "asm.pseudo");
int asmdecode = r_config_get_i (core->config, "asm.decode");
int asmlines = r_config_get_i (core->config, "asm.lines");
int asmbytes = r_config_get_i (core->config, "asm.bytes");
int asmoffset = r_config_get_i (core->config, "asm.offset");
int asmflags = r_config_get_i (core->config, "asm.flags");
int asmfcnlines = r_config_get_i (core->config, "asm.fcnlines");
int asmcomments = r_config_get_i (core->config, "asm.comments");
int asmfunctions = r_config_get_i (core->config, "asm.functions");
int asmsection = r_config_get_i (core->config, "asm.section");
int asmcmtcol = r_config_get_i (core->config, "asm.cmtcol");
//int asmtabs = r_config_get_i (core->config, "asm.tabs");
int asmfilter = r_config_get_i (core->config, "asm.filter");
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, core->offset, R_ANAL_FCN_TYPE_NULL);
RConfigHold *hc = r_config_hold_new (core->config);
r_config_save_num (hc, "asm.pseudo", "asm.decode", "asm.lines", "asm.bytes", NULL);
r_config_save_num (hc, "asm.offset", "asm.flags", "asm.fcnlines", "asm.comments", NULL);
r_config_save_num (hc, "asm.functions", "asm.section", "asm.cmtcol", "asm.filter", NULL);
if (!fcn) {
eprintf ("Cannot find function in 0x%08"PFMT64x"\n",
core->offset);
eprintf ("Cannot find function in 0x%08"PFMT64x"\n", core->offset);
return false;
}
r_config_set_i (core->config, "asm.pseudo", 1);
@ -194,7 +183,7 @@ R_API int r_core_pseudo_code (RCore *core, const char *input) {
nindent = sdb_num_get (db, K_INDENT(addr), NULL);
if (indent>nindent) {
int i;
for (i=indent; i!=nindent; i--) {
for (i = indent; i != nindent; i--) {
SET_INDENT (i);
r_cons_printf ("\n%s}", indentstr);
}
@ -206,20 +195,10 @@ R_API int r_core_pseudo_code (RCore *core, const char *input) {
}
}
//n_bb --;
} while (n_bb>0);
} while (n_bb > 0);
r_cons_printf ("}\n");
r_config_set_i (core->config, "asm.pseudo", asmpseudo);
r_config_set_i (core->config, "asm.decode", asmdecode);
r_config_set_i (core->config, "asm.lines", asmlines);
r_config_set_i (core->config, "asm.cmtcol", asmcmtcol);
r_config_set_i (core->config, "asm.bytes", asmbytes);
r_config_set_i (core->config, "asm.offset", asmoffset);
r_config_set_i (core->config, "asm.flags", asmflags);
r_config_set_i (core->config, "asm.fcnlines", asmfcnlines);
r_config_set_i (core->config, "asm.comments", asmcomments);
r_config_set_i (core->config, "asm.functions", asmfunctions);
r_config_set_i (core->config, "asm.section", asmsection);
r_config_set_i (core->config, "asm.filter", asmfilter);
r_config_restore (hc);
r_config_hold_free (hc);
sdb_free (db);
return true;
}

View File

@ -46,7 +46,29 @@ typedef struct r_config_t {
RHashTable *ht;
} RConfig;
typedef struct r_config_hold_num_t {
char *key;
ut64 value;
} RConfigHoldNum;
typedef struct r_config_hold_char_t {
char *key;
const char *value;
} RConfigHoldChar;
typedef struct r_config_hold_t {
RConfig *cfg;
RList *list_num; //list of RConfigHoldNum to hold numeric values
RList *list_char; //list of RConfigHoldChar to hold char values
} RConfigHold;
#ifdef R_API
R_API bool r_config_save_num(RConfigHold *h, ...);
R_API bool r_config_save_char(RConfigHold *h, ...);
R_API RConfigHold* r_config_hold_new(RConfig *cfg);
R_API void r_config_hold_free(RConfigHold *h);
R_API void r_config_restore(RConfigHold *h);
R_API RConfig *r_config_new(void *user);
R_API RConfig *r_config_clone (RConfig *cfg);
R_API int r_config_free(RConfig *cfg);

View File

@ -57,13 +57,6 @@ typedef struct r_db_table_t {
int *offset;
} RDatabaseTable;
#if 0
it = r_db_iterator (db);
while (r_db_iter_next(it)) {
f = (RAnalFcn*) r_db_iter_get (it);
/* ... */
}
#endif
typedef struct r_pair_item_t {
char *k, *v;