bin.filter class names and hashify raw bytes in name

This commit is contained in:
pancake 2015-07-06 00:58:51 +02:00
parent e20a1c0ce4
commit c10bcdfbea
4 changed files with 70 additions and 12 deletions

View File

@ -439,7 +439,11 @@ static int r_bin_object_set_items(RBinFile *binfile, RBinObject *o) {
}
if (cp->strings) o->strings = cp->strings (binfile);
else o->strings = get_strings (binfile, minlen, 0);
if (cp->classes) o->classes = cp->classes (binfile);
if (cp->classes) {
o->classes = cp->classes (binfile);
if (bin->filter)
r_bin_filter_classes (o->classes);
}
if (cp->lines) o->lines = cp->lines (binfile);
if (cp->get_sdb) o->kv = cp->get_sdb (o);
if (cp->mem) o->mem = cp->mem (binfile);

View File

@ -5,13 +5,31 @@
// TODO: optimize this api:
// - bin plugins should call r_bin_filter_name() before appending
R_API void r_bin_filter_name(Sdb *db, char *name, int maxlen) {
static void hashify(char *s, ut64 vaddr) {
while (*s) {
if (!IS_PRINTABLE(*s)) {
if (vaddr && vaddr != UT64_MAX) {
sprintf (s, "%"PFMT64d, vaddr);
} else {
ut32 hash = sdb_hash (s);
sprintf (s, "%x", hash);
}
break;
}
s++;
}
}
R_API void r_bin_filter_name(Sdb *db, ut64 vaddr, char *name, int maxlen) {
ut32 hash = sdb_hash (name);
int count = sdb_num_inc (db, sdb_fmt (0, "%x", hash), 1, 0);
if (vaddr) {
hashify (name, vaddr);
}
if (count>1) {
int namelen = strlen (name);
if (namelen>maxlen) name[maxlen] = 0;
strcat (name, sdb_fmt(0,"_%d", count-1));
strcat (name, sdb_fmt (0,"_%d", count-1));
// eprintf ("Symbol '%s' dupped!\n", sym->name);
}
}
@ -23,10 +41,10 @@ R_API void r_bin_filter_symbols (RList *list) {
RListIter *iter;
if (maxlen>0) {
r_list_foreach (list, iter, sym) {
r_bin_filter_name (db, sym->name, maxlen);
r_bin_filter_name (db, sym->vaddr, sym->name, maxlen);
}
sdb_free (db);
} else eprintf ("SymbolName is not dynamic\n");
sdb_free (db);
}
R_API void r_bin_filter_sections (RList *list) {
@ -36,8 +54,26 @@ R_API void r_bin_filter_sections (RList *list) {
RListIter *iter;
if (maxlen>0) {
r_list_foreach (list, iter, sec) {
r_bin_filter_name (db, sec->name, maxlen);
r_bin_filter_name (db, sec->vaddr, sec->name, maxlen);
}
sdb_free (db);
} else eprintf ("SectionName is not dynamic\n");
sdb_free (db);
}
R_API void r_bin_filter_classes (RList *list) {
Sdb *db = sdb_new0 ();
RListIter *iter;
RBinClass *cls;
r_list_foreach (list, iter, cls) {
int namepad_len = strlen (cls->name)+10;
char *namepad = malloc (namepad_len);
if (namepad) {
strcpy (namepad, cls->name);
r_bin_filter_name (db, cls->index, namepad, namepad_len);
//hashify (namepad, (ut64)cls->index);
free (cls->name);
cls->name = namepad;
} else eprintf ("Cannot alloc %d bytes\n", namepad_len);
}
sdb_free (db);
}

View File

@ -696,6 +696,16 @@ static RList* classes (RBinFile *arch) {
return ret;
}
static int already_entry (RList *entries, ut64 vaddr) {
RBinAddr *e;
RListIter *iter;
r_list_foreach (entries, iter, e) {
if (e->vaddr == vaddr)
return 1;
}
return 0;
}
static RList* entries(RBinFile *arch) {
RListIter *iter;
RBinDexObj *bin = (RBinDexObj*) arch->o->bin_obj;
@ -707,19 +717,26 @@ static RList* entries(RBinFile *arch) {
free (ptr);
return NULL;
}
if (!bin->methods_list)
if (!bin->methods_list) {
dex_loadcode (arch, bin);
}
// XXX: entry + main???
r_list_foreach (bin->methods_list, iter, m) {
if (strlen (m->name)>=4 && !strcmp (m->name+strlen (m->name)-4, "main")) {
dprintf ("ENTRY -> %s\n", m->name);
ptr->paddr = ptr->vaddr = m->paddr;
r_list_append (ret, ptr);
if (!already_entry (ret, ptr->vaddr)) {
r_list_append (ret, ptr);
ptr = R_NEW0 (RBinAddr);
if (!ptr) break;
}
}
}
if (r_list_empty (ret)) {
if (ptr && r_list_empty (ret)) {
ptr->paddr = ptr->vaddr = bin->code_from;
r_list_append (ret, ptr);
if (!already_entry (ret, ptr->vaddr)) {
r_list_append (ret, ptr);
}
}
return ret;
}

View File

@ -494,9 +494,10 @@ R_API char *r_bin_demangle_plugin(RBin *bin, const char *name, const char *str);
R_API RList *r_bin_get_mem (RBin *bin);
/* filter.c */
R_API void r_bin_filter_name(Sdb *db, char *name, int maxlen);
R_API void r_bin_filter_name(Sdb *db, ut64 addr, char *name, int maxlen);
R_API void r_bin_filter_symbols (RList *list);
R_API void r_bin_filter_sections (RList *list);
R_API void r_bin_filter_classes (RList *list);
/* plugin pointers */
extern RBinPlugin r_bin_plugin_any;