More code cleanup in RBin api for file and obj

This commit is contained in:
pancake 2018-03-05 00:05:17 +01:00
parent 0e692944a4
commit 03ff82839d
4 changed files with 93 additions and 106 deletions

View File

@ -190,76 +190,6 @@ R_API void r_bin_string_free(void *_str) {
free (str);
}
static char *swiftField(const char *dn, const char *cn) {
char *p = strstr (dn, ".getter_");
if (!p) {
p = strstr (dn, ".setter_");
if (!p) {
p = strstr (dn, ".method_");
}
}
if (p) {
char *q = strstr (dn, cn);
if (q && q[strlen (cn)] == '.') {
q = strdup (q + strlen (cn) + 1);
char *r = strchr (q, '.');
if (r) {
*r = 0;
}
return q;
}
}
return NULL;
}
R_API RList *r_bin_classes_from_symbols (RBinFile *bf, RBinObject *o) {
RBinSymbol *sym;
RListIter *iter;
RList *symbols = o->symbols;
RList *classes = o->classes;
if (!classes) {
classes = r_list_newf ((RListFree)r_bin_class_free);
}
r_list_foreach (symbols, iter, sym) {
if (sym->name[0] != '_') {
continue;
}
const char *cn = sym->classname;
if (cn) {
RBinClass *c = r_bin_class_new (bf, sym->classname, NULL, 0);
if (!c) {
continue;
}
// swift specific
char *dn = sym->dname;
char *fn = swiftField (dn, cn);
if (fn) {
// eprintf ("FIELD %s %s\n", cn, fn);
RBinField *f = r_bin_field_new (sym->paddr, sym->vaddr, sym->size, fn, NULL, NULL);
r_list_append (c->fields, f);
free (fn);
} else {
char *mn = strstr (dn, "..");
if (mn) {
// eprintf ("META %s %s\n", sym->classname, mn);
} else {
char *mn = strstr (dn, cn);
if (mn && mn[strlen(cn)] == '.') {
mn += strlen (cn) + 1;
// eprintf ("METHOD %s %s\n", sym->classname, mn);
r_list_append (c->methods, sym);
}
}
}
}
}
if (r_list_empty (classes)) {
r_list_free (classes);
return NULL;
}
return classes;
}
// XXX - change this to RBinObject instead of RBinFile
// makes no sense to pass in a binfile and set the RBinObject
// kinda a clunky functions
@ -800,18 +730,10 @@ R_API int r_bin_list_plugin(RBin *bin, const char* name, int json) {
return false;
}
static ut64 binobj_get_baddr(RBinObject *o) {
return o? o->baddr + o->baddr_shift: UT64_MAX;
}
R_API ut64 r_binfile_get_baddr(RBinFile *binfile) {
return binfile? binobj_get_baddr (binfile->o): UT64_MAX;
}
/* returns the base address of bin or UT64_MAX in case of errors */
R_API ut64 r_bin_get_baddr(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return binobj_get_baddr (o);
return r_bin_object_get_baddr (o);
}
/* returns the load address of bin or UT64_MAX in case of errors */
@ -1124,7 +1046,7 @@ R_API int r_bin_use_arch(RBin *bin, const char *arch, int bits, const char *name
obj->info->bits = bits;
}
}
return (binfile && r_bin_file_set_cur_binfile_obj (bin, binfile, obj));
return r_bin_file_set_cur_binfile_obj (bin, binfile, obj);
}
R_API int r_bin_select(RBin *bin, const char *arch, int bits, const char *name) {
@ -1138,8 +1060,7 @@ R_API int r_bin_select(RBin *bin, const char *arch, int bits, const char *name)
return r_bin_file_set_cur_binfile_obj (bin, binfile, obj);
}
R_API int r_bin_select_object(RBinFile *binfile, const char *arch, int bits,
const char *name) {
R_API int r_bin_select_object(RBinFile *binfile, const char *arch, int bits, const char *name) {
RBinObject *obj = r_bin_object_find_by_arch_bits (binfile, arch, bits, name);
return r_bin_file_set_cur_binfile_obj (binfile->rbin, binfile, obj);
}
@ -1161,10 +1082,7 @@ R_API int r_bin_select_by_ids(RBin *bin, ut32 binfile_id, ut32 binobj_id) {
binfile = r_bin_file_find_by_id (bin, binfile_id);
obj = binfile? r_bin_file_object_find_by_id (binfile, binobj_id): NULL;
}
if (!binfile || !obj) {
return false;
}
return obj && binfile && r_bin_file_set_cur_binfile_obj (bin, binfile, obj);
return r_bin_file_set_cur_binfile_obj (bin, binfile, obj);
}
R_API int r_bin_select_idx(RBin *bin, const char *name, int idx) {
@ -1177,8 +1095,7 @@ R_API int r_bin_select_idx(RBin *bin, const char *name, int idx) {
}
nbinfile = r_bin_file_find_by_name_n (bin, tname, idx);
obj = nbinfile? r_list_get_n (nbinfile->objs, idx): NULL;
return obj && nbinfile &&
r_bin_file_set_cur_binfile_obj (bin, nbinfile, obj);
return r_bin_file_set_cur_binfile_obj (bin, nbinfile, obj);
}
static void list_xtr_archs(RBin *bin, int mode) {
@ -1571,7 +1488,7 @@ R_API void r_bin_class_add_field(RBinFile *binfile, const char *classname, const
/* returns vaddr, rebased with the baseaddr of binfile, if va is enabled for
* bin, paddr otherwise */
R_API ut64 r_binfile_get_vaddr(RBinFile *binfile, ut64 paddr, ut64 vaddr) {
R_API ut64 r_bin_file_get_vaddr(RBinFile *binfile, ut64 paddr, ut64 vaddr) {
int use_va = 0;
if (binfile && binfile->o && binfile->o->info) {
use_va = binfile->o->info->has_va;
@ -1603,7 +1520,7 @@ R_API ut64 r_bin_get_vaddr(RBin *bin, ut64 paddr, ut64 vaddr) {
}
}
}
return r_binfile_get_vaddr (bin->cur, paddr, vaddr);
return r_bin_file_get_vaddr (bin->cur, paddr, vaddr);
}
R_API ut64 r_bin_a2b(RBin *bin, ut64 addr) {

View File

@ -62,7 +62,6 @@ static void print_string(RBinString *string, RBinFile *bf) {
}
}
static int string_scan_range(RList *list, RBinFile *bf, int min,
const ut64 from, const ut64 to, int type) {
ut8 tmp[R_STRING_SCAN_BUFFER_SIZE];
@ -100,8 +99,6 @@ static int string_scan_range(RList *list, RBinFile *bf, int min,
} else {
str_type = type;
}
runes = 0;
str_start = needle;
@ -217,7 +214,75 @@ static int string_scan_range(RList *list, RBinFile *bf, int min,
return count;
}
static char *swiftField(const char *dn, const char *cn) {
char *p = strstr (dn, ".getter_");
if (!p) {
p = strstr (dn, ".setter_");
if (!p) {
p = strstr (dn, ".method_");
}
}
if (p) {
char *q = strstr (dn, cn);
if (q && q[strlen (cn)] == '.') {
q = strdup (q + strlen (cn) + 1);
char *r = strchr (q, '.');
if (r) {
*r = 0;
}
return q;
}
}
return NULL;
}
R_API RList *r_bin_classes_from_symbols (RBinFile *bf, RBinObject *o) {
RBinSymbol *sym;
RListIter *iter;
RList *symbols = o->symbols;
RList *classes = o->classes;
if (!classes) {
classes = r_list_newf ((RListFree)r_bin_class_free);
}
r_list_foreach (symbols, iter, sym) {
if (sym->name[0] != '_') {
continue;
}
const char *cn = sym->classname;
if (cn) {
RBinClass *c = r_bin_class_new (bf, sym->classname, NULL, 0);
if (!c) {
continue;
}
// swift specific
char *dn = sym->dname;
char *fn = swiftField (dn, cn);
if (fn) {
// eprintf ("FIELD %s %s\n", cn, fn);
RBinField *f = r_bin_field_new (sym->paddr, sym->vaddr, sym->size, fn, NULL, NULL);
r_list_append (c->fields, f);
free (fn);
} else {
char *mn = strstr (dn, "..");
if (mn) {
// eprintf ("META %s %s\n", sym->classname, mn);
} else {
char *mn = strstr (dn, cn);
if (mn && mn[strlen(cn)] == '.') {
mn += strlen (cn) + 1;
// eprintf ("METHOD %s %s\n", sym->classname, mn);
r_list_append (c->methods, sym);
}
}
}
}
}
if (r_list_empty (classes)) {
r_list_free (classes);
return NULL;
}
return classes;
}
R_API RBinFile *r_bin_file_new(RBin *bin, const char *file, const ut8 *bytes, ut64 sz, ut64 file_sz, int rawstr, int fd, const char *xtrname, Sdb *sdb, bool steal_ptr) {
RBinFile *binfile = R_NEW0 (RBinFile);
@ -551,7 +616,7 @@ R_API int r_bin_file_set_cur_by_fd(RBin *bin, ut32 bin_fd) {
return r_bin_file_set_cur_binfile (bin, bf);
}
R_API int r_bin_file_set_cur_binfile_obj(RBin *bin, RBinFile *bf, RBinObject *obj) {
R_API bool r_bin_file_set_cur_binfile_obj(RBin *bin, RBinFile *bf, RBinObject *obj) {
RBinPlugin *plugin = NULL;
if (!bin || !bf || !obj) {
return false;
@ -569,7 +634,7 @@ R_API int r_bin_file_set_cur_binfile_obj(RBin *bin, RBinFile *bf, RBinObject *ob
R_API int r_bin_file_set_cur_binfile(RBin *bin, RBinFile *bf) {
RBinObject *obj = bf? bf->o: NULL;
return obj? r_bin_file_set_cur_binfile_obj (bin, bf, obj): false;
return r_bin_file_set_cur_binfile_obj (bin, bf, obj);
}
R_API int r_bin_file_set_cur_by_name(RBin *bin, const char *name) {
@ -611,7 +676,9 @@ R_API int r_bin_file_deref(RBin *bin, RBinFile *a) {
// it is possible for a file not
// to be bound to RBin and RBinFiles
// XXX - is this an ok assumption?
if (bin) bin->cur = NULL;
if (bin) {
bin->cur = NULL;
}
return res;
}
@ -734,7 +801,6 @@ R_API RBinPlugin *r_bin_file_cur_plugin(RBinFile *binfile) {
return binfile && binfile->o? binfile->o->plugin: NULL;
}
static int is_data_section(RBinFile *a, RBinSection *s) {
if (s->has_strings || s->is_data) {
return true;
@ -746,7 +812,6 @@ static int is_data_section(RBinFile *a, RBinSection *s) {
return (strstr (s->name, "_const") != NULL);
}
R_API RList *r_bin_file_get_strings(RBinFile *a, int min, int dump) {
RListIter *iter;
RBinSection *section;
@ -865,3 +930,8 @@ R_API void r_bin_file_get_strings_range(RBinFile *bf, RList *list, int min, ut64
}
}
}
R_API ut64 r_bin_file_get_baddr(RBinFile *binfile) {
return binfile? r_bin_object_get_baddr (binfile->o): UT64_MAX;
}

View File

@ -286,6 +286,10 @@ R_API RBinObject *r_bin_object_find_by_arch_bits(RBinFile *binfile, const char *
return obj;
}
R_API ut64 r_bin_object_get_baddr(RBinObject *o) {
return o? o->baddr + o->baddr_shift: UT64_MAX;
}
R_API int r_bin_object_delete(RBin *bin, ut32 binfile_id, ut32 binobj_id) {
RBinFile *binfile = NULL;
RBinObject *obj = NULL;

View File

@ -581,15 +581,11 @@ R_API int r_bin_file_delete(RBin *bin, ut32 bin_fd);
R_API RBinFile *r_bin_file_find_by_fd(RBin *bin, ut32 bin_fd);
R_API RBinFile *r_bin_file_find_by_name(RBin *bin, const char *name);
R_API RBinFile *r_bin_file_find_by_name_n(RBin *bin, const char *name, int idx);
R_API int r_bin_file_set_cur_by_fd(RBin *bin, ut32 bin_fd);
R_API int r_bin_file_set_cur_binfile_obj(RBin *bin, RBinFile *bf, RBinObject *obj);
R_API bool r_bin_file_set_cur_binfile_obj(RBin *bin, RBinFile *bf, RBinObject *obj);
R_API int r_bin_file_set_cur_binfile(RBin *bin, RBinFile *bf);
R_API int r_bin_file_set_cur_by_name(RBin *bin, const char *name);
R_API RBinObject *r_bin_file_object_get_cur(RBinFile *binfile);
R_API int r_bin_file_cur_set_plugin(RBinFile *binfile, RBinPlugin *plugin);
R_API int r_bin_file_deref_by_bind(RBinBind *binb);
R_API int r_bin_file_deref(RBin *bin, RBinFile *a);
R_API int r_bin_file_ref_by_bind(RBinBind *binb);
R_API int r_bin_file_ref(RBin *bin, RBinFile *a);
R_API void r_bin_file_free(void /*RBinFile*/ *bf_);
R_API RBinFile *r_bin_file_create_append(RBin *bin, const char *file, const ut8 *bytes, ut64 sz, ut64 file_sz, int rawstr, int fd, const char *xtrname, bool steal_ptr);
@ -599,6 +595,7 @@ R_API RBinPlugin *r_bin_file_cur_plugin(RBinFile *binfile);
/* obj.c */
R_API void r_bin_object_free(void /*RBinObject*/ *o_);
R_API ut64 r_bin_object_get_baddr(RBinObject *o);
R_API void r_bin_object_filter_strings (RBinObject *bo);
R_API void r_bin_object_set_baddr(RBinObject *o, ut64 baddr);
R_API RBinObject *r_bin_object_new(RBinFile *binfile, RBinPlugin *plugin, ut64 baseaddr, ut64 loadaddr, ut64 offset, ut64 sz);
@ -664,11 +661,13 @@ R_API RList* r_bin_get_relocs(RBin *bin);
R_API RList* r_bin_get_sections(RBin *bin);
R_API RList* /*<RBinClass>*/r_bin_get_classes(RBin *bin);
// TODO: rename to r_bin_file_get_class() etc
R_API RBinClass *r_bin_class_get (RBinFile *binfile, const char *name);
R_API RBinClass *r_bin_class_new (RBinFile *binfile, const char *name, const char *super, int view);
R_API void r_bin_class_free(RBinClass *c);
R_API RBinSymbol *r_bin_class_add_method (RBinFile *binfile, const char *classname, const char *name, int nargs);
R_API void r_bin_class_add_field (RBinFile *binfile, const char *classname, const char *name);
R_API RList *r_bin_classes_from_symbols (RBinFile *bf, RBinObject *o);
R_API RBinSection* r_bin_get_section_at(RBinObject *o, ut64 off, int va);
R_API RList* r_bin_get_strings(RBin *bin);
@ -687,7 +686,6 @@ R_API RBin* r_bin_new(void);
R_API void r_bin_iobind(RBin *bin, RIO *io);
R_API RBinFile * r_bin_cur(RBin *bin);
R_API RBinObject * r_bin_cur_object(RBin *bin);
R_API int r_bin_file_set_cur_binfile_obj(RBin * bin, RBinFile *bf, RBinObject *obj);
R_API int r_bin_io_load(RBin *bin, RIO *io, int fd, ut64 baseaddr, ut64 loadaddr, int dummy);
R_API int r_bin_select(RBin *bin, const char *arch, int bits, const char *name);
@ -704,11 +702,9 @@ R_API ut64 r_bin_a2b (RBin *bin, ut64 addr);
R_API int r_bin_file_delete(RBin *bin, ut32 bin_fd);
R_API int r_bin_file_delete_all(RBin *bin);
R_API int r_bin_file_set_cur_by_fd (RBin *bin, ut32 bin_fd);
R_API int r_bin_file_set_cur_by_name (RBin * bin, const char * name);
R_API RBinFile * r_bin_file_find_by_fd (RBin *bin, ut32 bin_fd);
R_API RBinFile * r_bin_file_find_by_name (RBin * bin, const char * name);
R_API RBinFile * r_bin_file_find_by_name_n (RBin * bin, const char * name, int idx);
R_API int r_bin_file_set_cur_binfile (RBin * bin, RBinFile *bf);
R_API RBinPlugin * r_bin_file_cur_plugin (RBinFile *binfile);
R_API void r_bin_force_plugin (RBin *bin, const char *pname);
R_API const char *r_bin_string_type (int type);