Fix #5425 - Code cleanup and refactor

This commit is contained in:
pancake 2016-08-01 13:37:25 +02:00
parent 61518c2646
commit 8e45f6248c
8 changed files with 81 additions and 77 deletions

View File

@ -9,7 +9,7 @@ DEPS+=r_reg r_search r_syscall r_socket r_fs r_magic r_crypto
OBJS=core.o cmd.o file.o config.o visual.o io.o yank.o libs.o graph.o
OBJS+=hack.o vasm.o patch.o bin.o log.o syscmd.o rtr.o cmd_api.o
OBJS+=anal.o project.o gdiff.o asm.o vmenus.o disasm.o plugin.o
OBJS+=help.o task.o panels.o pseudo.o vmarks.o xrefs.o
OBJS+=help.o task.o panels.o pseudo.o vmarks.o
CFLAGS+=-DCORELIB
LDFLAGS+=${DL_LIBS}

View File

@ -2106,9 +2106,9 @@ R_API RBuffer *r_core_syscallf (RCore *core, const char *name, const char *fmt,
}
R_API RBuffer *r_core_syscall (RCore *core, const char *name, const char *args) {
int i, num;
RBuffer *b = NULL;
char code[1024];
int i, num;
num = r_syscall_get_num (core->anal->syscall, name);
if (!num) {
@ -2123,23 +2123,20 @@ R_API RBuffer *r_core_syscall (RCore *core, const char *name, const char *args)
// TODO: setup arch/bits/os?
r_egg_load (core->egg, code, 0);
if (!r_egg_compile (core->egg))
if (!r_egg_compile (core->egg)) {
eprintf ("Cannot compile.\n");
if (!r_egg_assemble (core->egg))
}
if (!r_egg_assemble (core->egg)) {
eprintf ("r_egg_assemble: invalid assembly\n");
}
if ((b = r_egg_get_bin (core->egg))) {
if (b->length>0) {
for (i=0; i<b->length; i++)
if (b->length > 0) {
for (i = 0; i < b->length; i++) {
r_cons_printf ("%02x", b->buf[i]);
}
r_cons_printf ("\n");
}
}
return b;
}
R_API bool r_core_project_load(RCore *core, const char *prjfile) {
if (prjfile && *prjfile) {
return r_core_xrefs_load (core, prjfile);
}
return false;
}

View File

@ -211,6 +211,11 @@ static bool r_core_rop_load(RCore *core, const char *prjfile) {
return true;
}
R_API void r_core_project_load(RCore *core, const char *prjfile) {
(void)r_core_rop_load (core, prjfile);
(void)r_core_project_load_xrefs (core, prjfile);
}
R_API int r_core_project_open(RCore *core, const char *prjfile) {
int askuser = 1;
int ret, close_current_session = 1;
@ -275,9 +280,8 @@ R_API int r_core_project_open(RCore *core, const char *prjfile) {
// TODO: handle base address
r_core_bin_load (core, filepath, UT64_MAX);
}
// FIXME: If r_anal_project_load is not called before r_core_cmd_file, xrefs are not loaded correctly
/* load sdb stuff in here */
r_core_project_load (core, prjfile);
r_core_rop_load (core, prjfile);
ret = r_core_cmd_file (core, prj);
r_config_bump (core->config, "asm.arch");
free (filepath);
@ -471,3 +475,44 @@ R_API char *r_core_project_notes_file (RCore *core, const char *file) {
free (prjpath);
return notes_txt;
}
#define DB core->anal->sdb_xrefs
R_API bool r_core_project_load_xrefs(RCore *core, const char *prjfile) {
char *path, *db;
const char *prjdir = r_config_get (core->config, "dir.projects");
if (!prjfile || !*prjfile) {
return false;
}
if (prjfile[0] == '/') {
db = r_str_newf ("%s.d", prjfile);
if (!db) return false;
path = strdup (db);
} else {
db = r_str_newf ("%s/%s.d", prjdir, prjfile);
if (!db) return false;
path = r_file_abspath (db);
}
if (!path) {
free (db);
return false;
}
if (!sdb_ns_unset (core->anal->sdb, NULL, DB)) {
sdb_free (DB);
}
DB = sdb_new (path, "xrefs", 0);
if (!DB) {
free (db);
free (path);
return false;
}
sdb_ns_set (core->anal->sdb, "xrefs", DB);
free (path);
free (db);
return true;
}

View File

@ -1,53 +0,0 @@
/* radare - LGPL - Copyright 2009-2014 - pancake, nibble, Darredevil */
#include <r_core.h>
#include <sdb.h>
#define _DB core->anal->sdb_xrefs
R_API bool r_core_xrefs_load(RCore *core, const char *prjfile) {
char *path, *db;
ut8 found = 0;
SdbListIter *it;
SdbNs *ns;
const char *prjdir = r_config_get (core->config, "dir.projects");
if (!prjfile || !*prjfile) {
return false;
}
if (prjfile[0] == '/') {
db = r_str_newf ("%s.d", prjfile);
if (!db) return false;
path = strdup (db);
} else {
db = r_str_newf ("%s/%s.d", prjdir, prjfile);
if (!db) return false;
path = r_file_abspath (db);
}
if (!path) {
free (db);
return false;
}
ls_foreach (core->anal->sdb->ns, it, ns){
if (ns->sdb == _DB){
ls_delete (core->anal->sdb->ns, it);
found = 1;
break;
}
}
if (!found) sdb_free (_DB);
_DB = sdb_new (path, "xrefs", 0);
if (!_DB) {
free (db);
free (path);
return false;
}
sdb_ns_set (core->anal->sdb, "xrefs", _DB);
free (path);
free (db);
return true;
}

View File

@ -288,8 +288,8 @@ R_API ut32 r_core_file_cur_fd (RCore *core);
R_API void r_core_debug_rr (RCore *core, RReg *reg);
/* project */
R_API bool r_core_project_load(RCore *core, const char *prjfile);
R_API bool r_core_xrefs_load(RCore *core, const char *prjfile);
R_API void r_core_project_load(RCore *core, const char *prjfile);
R_API bool r_core_project_load_xrefs(RCore *core, const char *prjfile);
#define R_CORE_FOREIGN_ADDR -1
R_API int r_core_yank(RCore *core, ut64 addr, int len);

View File

@ -223,7 +223,7 @@ void sdb_ns_free (Sdb* s);
void sdb_ns_lock(Sdb *s, int lock, int depth);
void sdb_ns_sync (Sdb* s);
int sdb_ns_set (Sdb *s, const char *name, Sdb *r);
int sdb_ns_unset (Sdb *s, const char *name);
bool sdb_ns_unset (Sdb *s, const char *name, Sdb *r);
// array
int sdb_array_contains (Sdb* s, const char *key, const char *val, ut32 *cas);

View File

@ -1,4 +1,4 @@
/* sdb - MIT - Copyright 2011-2014 - pancake */
/* sdb - MIT - Copyright 2011-2016 - pancake */
#include "sdb.h"
@ -113,17 +113,31 @@ static SdbNs *sdb_ns_new (Sdb *s, const char *name, ut32 hash) {
return ns;
}
/*
static void sdb_ns_free (SdbNs *) {
SDB_API bool sdb_ns_unset (Sdb *s, const char *name, Sdb *r) {
SdbNs *ns;
SdbListIter *it;
if (s && (name || r)) {
ls_foreach (s->ns, it, ns) {
if (name && (!strcmp (name, ns->name))) {
ls_delete (s->ns, it);
return true;
}
if (r && ns->sdb == r) {
ls_delete (s->ns, it);
return true;
}
}
}
return false;
}
*/
SDB_API int sdb_ns_set (Sdb *s, const char *name, Sdb *r) {
SdbNs *ns;
SdbListIter *it;
ut32 hash = sdb_hash (name);
if (!s || !r || !name)
if (!s || !r || !name) {
return 0;
}
ls_foreach (s->ns, it, ns) {
if (ns->hash == hash) {
if (ns->sdb == r)
@ -134,8 +148,9 @@ SDB_API int sdb_ns_set (Sdb *s, const char *name, Sdb *r) {
return 1;
}
}
if (s->ns_lock)
if (s->ns_lock) {
return 0;
}
ns = R_NEW (SdbNs);
ns->name = strdup (name);
ns->hash = hash;

View File

@ -223,7 +223,7 @@ void sdb_ns_free (Sdb* s);
void sdb_ns_lock(Sdb *s, int lock, int depth);
void sdb_ns_sync (Sdb* s);
int sdb_ns_set (Sdb *s, const char *name, Sdb *r);
int sdb_ns_unset (Sdb *s, const char *name);
bool sdb_ns_unset (Sdb *s, const char *name, Sdb *r);
// array
int sdb_array_contains (Sdb* s, const char *key, const char *val, ut32 *cas);