// - name should be allocated on the heap R_API char *r_bin_filter_name(RBinFile *bf, Sdb *db, ut64 vaddr, char *name) { r_return_val_if_fail (db && name, NULL); char *resname = name; const char *uname = sdb_fmt ("%" PFMT64x ".%s", vaddr, resname); ut32 vhash = sdb_hash (uname); // vaddr hash - unique ut32 hash = sdb_hash (resname); // name hash - if dupped and not in unique hash must insert int count = sdb_num_inc (db, sdb_fmt ("%x", hash), 1, 0); if (sdb_exists (db, sdb_fmt ("%x", vhash))) { // TODO: symbol is dupped, so symbol can be removed! return resname; } sdb_num_set (db, sdb_fmt ("%x", vhash), 1, 0); if (vaddr) { char *p = hashify (resname, vaddr); if (p) { resname = p; } } if (count > 1) { char *p = r_str_appendf (resname, "_%d", count - 1); if (p) { resname = p; } // two symbols at different addresses and same name wtf // eprintf ("Symbol '%s' dupped!\n", sym->name); } return resname; }
R_API void r_bin_filter_sym(RBinFile *bf, Sdb *db, ut64 vaddr, RBinSymbol *sym) { if (!db || !sym || !sym->name) { return; } char *name = sym->name; // if (!strncmp (sym->name, "imp.", 4)) { // demangle symbol name depending on the language specs if any if (bf && bf->o && bf->o->lang) { const char *lang = r_bin_lang_tostring (bf->o->lang); char *dn = r_bin_demangle (bf, lang, sym->name, sym->vaddr); if (dn && *dn) { sym->dname = dn; // XXX this is wrong but is required for this test to pass // pmb:new pancake$ bin/r2r.js db/formats/mangling/swift sym->name = dn; // extract class information from demangled symbol name char *p = strchr (dn, '.'); if (p) { if (IS_UPPER (*dn)) { sym->classname = strdup (dn); sym->classname[p - dn] = 0; } else if (IS_UPPER (p[1])) { sym->classname = strdup (p + 1); p = strchr (sym->classname, '.'); if (p) { *p = 0; } } } } } // XXX this is very slow, must be optimized const char *uname = sdb_fmt ("%" PFMT64x ".%s", vaddr, name); ut32 vhash = sdb_hash (uname); // vaddr hash - unique ut32 hash = sdb_hash (name); // name hash - if dupped and not in unique hash must insert int count = sdb_num_inc (db, sdb_fmt ("%x", hash), 1, 0); if (sdb_exists (db, sdb_fmt ("%x", vhash))) { // TODO: symbol is dupped, so symbol can be removed! return; } sdb_num_set (db, sdb_fmt ("%x", vhash), 1, 0); sym->dup_count = count - 1; }
static int meta_add(RAnal *a, int type, int subtype, ut64 from, ut64 to, const char *str) { int space_idx = a->meta_spaces.space_idx; char *e_str, key[100], val[2048]; int exists; if (from > to) { return false; } if (from == to) { to = from + 1; } if (type == 100 && (to - from) < 1) { return false; } /* set entry */ e_str = sdb_encode ((const void*)str, -1); RAnalMetaItem mi = {from, to, (int)(to - from), type, subtype, e_str, space_idx}; meta_serialize (&mi, key, sizeof (key), val, sizeof (val)); exists = sdb_exists (DB, key); sdb_set (DB, key, val, 0); free (e_str); // XXX: This is totally inefficient, using array_add withuot // checking return value is wrong practice, also it may lead // to inconsistent DB, and pretty bad performance. We should // store this list in a different storage that doesnt have // those limits and it's O(1) instead of O(n) snprintf (key, sizeof (key) - 1, "meta.0x%"PFMT64x, from); if (exists) { const char *value = sdb_const_get (DB, key, 0); int idx = sdb_array_indexof (DB, key, value, 0); sdb_array_delete (DB, key, idx, 0); } snprintf (val, sizeof (val)-1, "%c", type); sdb_array_add (DB, key, val, 0); meta_inrange_add (a, from, to - from); return true; }
R_API int r_meta_add(RAnal *a, int type, ut64 from, ut64 to, const char *str) { int exists; char *e_str, key[100], val[2048]; if (from>to) return R_FALSE; if (from == to) to = from+1; if (type == 100) { if ((to-from)<3) { return R_FALSE; } } /* set entry */ e_str = sdb_encode ((const void*)str, -1); snprintf (key, sizeof (key)-1, "meta.%c.0x%"PFMT64x, type, from); snprintf (val, sizeof (val)-1, "%d,%s", (int)(to-from), e_str); exists = sdb_exists (DB, key); sdb_set (DB, key, val, 0); free (e_str); // XXX: This is totally inefficient, using array_add withuot // checking return value is wrong practice, also it may lead // to inconsistent DB, and pretty bad performance. We should // store this list in a different storage that doesnt have // those limits and it's O(1) instead of O(n) if (!exists) { /* set type index */ snprintf (key, sizeof (key)-1, "meta.0x%"PFMT64x, from); snprintf (val, sizeof (val)-1, "%c", type); sdb_array_add (DB, key, val, 0); /* set type index */ snprintf (key, sizeof (key)-1, "meta.%c", type); sdb_array_add_num (DB, key, from, 0); } return R_TRUE; }
// set if not defined SDB_VISIBLE int sdb_add (Sdb *s, const char *key, const char *val, ut32 cas) { if (sdb_exists (s, key)) return 0; return sdb_set (s, key, val, cas); }