Skip to content

Commit

Permalink
Transition of strings in RzBinPlugin (from list to pvector)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu committed Jan 3, 2024
1 parent 63d3369 commit 56de55e
Show file tree
Hide file tree
Showing 42 changed files with 172 additions and 137 deletions.
35 changes: 21 additions & 14 deletions librz/bin/bfile_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct shared_data_t {

typedef struct search_thread_data_t {
RzThreadQueue *intervals;
RzList /*<RzBinString *>*/ *results;
RzPVector /*<RzBinString *>*/ *results;
size_t min_length;
RzStrEnc encoding;
bool check_ascii_freq;
Expand Down Expand Up @@ -132,7 +132,7 @@ static void *search_string_thread_runner(SearchThreadData *std) {
}

RzBinString *bstr = to_bin_string(detected);
if (!bstr || !rz_list_append(std->results, bstr)) {
if (!bstr || !rz_pvector_push(std->results, bstr)) {
rz_bin_string_free(bstr);
loop = false;
break;
Expand All @@ -158,7 +158,7 @@ static void bin_file_string_search_free(SearchThreadData *std) {
if (!std) {
return;
}
rz_list_free(std->results);
rz_pvector_free(std->results);
rz_atomic_bool_free(std->loop);
free(std);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ static bool create_string_search_thread(RzThreadPool *pool, size_t min_length, R
return false;
}

std->results = rz_list_newf(rz_bin_string_free);
std->results = rz_pvector_new((RzPVectorFree)rz_bin_string_free);
if (!std->results) {
bin_file_string_search_free(std);
return false;
Expand Down Expand Up @@ -247,7 +247,7 @@ static int string_compare_sort(const RzBinString *a, const RzBinString *b) {
return 0;
}

static void string_scan_range_cfstring(RzBinFile *bf, HtUP *strings_db, RzList /*<RzBinString *>*/ *results, const RzBinSection *section) {
static void string_scan_range_cfstring(RzBinFile *bf, HtUP *strings_db, RzPVector /*<RzBinString *>*/ *results, const RzBinSection *section) {
// load objc/swift strings from CFstring table section

RzBinObject *o = bf->o;
Expand Down Expand Up @@ -293,13 +293,13 @@ static void string_scan_range_cfstring(RzBinFile *bf, HtUP *strings_db, RzList /
bs->vaddr = cfstr_vaddr;
bs->paddr = rz_bin_object_v2p(o, bs->vaddr);
bs->string = rz_str_newf("cstr.%s", s->string);
rz_list_append(results, bs);
rz_pvector_push(results, bs);
ht_up_insert(strings_db, bs->vaddr, bs);
}
free(sbuf);
}

static void scan_cfstring_table(RzBinFile *bf, HtUP *strings_db, RzList /*<RzBinString *>*/ *results, ut64 max_interval) {
static void scan_cfstring_table(RzBinFile *bf, HtUP *strings_db, RzPVector /*<RzBinString *>*/ *results, ut64 max_interval) {
RzListIter *iter = NULL;
RzBinSection *section = NULL;
RzBinObject *o = bf->o;
Expand Down Expand Up @@ -331,11 +331,11 @@ static void scan_cfstring_table(RzBinFile *bf, HtUP *strings_db, RzList /*<RzBin
*
* \return On success returns RzList pointer, otherwise NULL
*/
RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_file_strings(RZ_NONNULL RzBinFile *bf, size_t min_length, bool raw_strings) {
RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_file_strings(RZ_NONNULL RzBinFile *bf, size_t min_length, bool raw_strings) {
rz_return_val_if_fail(bf, NULL);

HtUP *strings_db = NULL;
RzList *results = NULL;
RzPVector *results = NULL;
RzThreadQueue *intervals = NULL;
RzThreadPool *pool = NULL;
RzThreadLock *lock = NULL;
Expand Down Expand Up @@ -467,7 +467,7 @@ RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_file_strings(RZ_NONNULL RzBinFi

rz_th_pool_wait(pool);

results = rz_list_newf(rz_bin_string_free);
results = rz_pvector_new((RzPVectorFree)rz_bin_string_free);
if (!results) {
RZ_LOG_ERROR("bin_file_strings: cannot allocate results list.\n");
goto fail;
Expand All @@ -480,20 +480,27 @@ RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_file_strings(RZ_NONNULL RzBinFi
}
SearchThreadData *std = (SearchThreadData *)rz_th_get_user(th);
if (std) {
rz_list_join(results, std->results);
void **iter;
RzBinString *bstr;
rz_pvector_foreach (std->results, iter) {
bstr = *iter;
rz_pvector_push(results, bstr);
}
std->results->v.len = 0;
}
}

if (!raw_strings) {
scan_cfstring_table(bf, strings_db, results, max_interval);
}
rz_list_sort(results, (RzListComparator)string_compare_sort);
rz_pvector_sort(results, (RzPVectorComparator)string_compare_sort);

{
RzListIter *it;
void **it;
RzBinString *bstr;
ut32 ordinal = 0;
rz_list_foreach (results, it, bstr) {
rz_pvector_foreach (results, it) {
bstr = *it;
bstr->ordinal = ordinal;
ordinal++;
}
Expand Down
6 changes: 0 additions & 6 deletions librz/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,12 +733,6 @@ RZ_API RZ_OWN RzPVector /*<RzBinMap *>*/ *rz_bin_object_get_maps_at(RzBinObject
return res;
}

RZ_DEPRECATE RZ_API RZ_BORROW RzList /*<RzBinString *>*/ *rz_bin_get_strings(RZ_NONNULL RzBin *bin) {
rz_return_val_if_fail(bin, NULL);
RzBinObject *o = rz_bin_cur_object(bin);
return o ? (RzList *)rz_bin_object_get_strings(o) : NULL;
}

RZ_DEPRECATE RZ_API RZ_BORROW RzList /*<RzBinSymbol *>*/ *rz_bin_get_symbols(RZ_NONNULL RzBin *bin) {
rz_return_val_if_fail(bin, NULL);
RzBinObject *o = rz_bin_cur_object(bin);
Expand Down
35 changes: 21 additions & 14 deletions librz/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,14 +744,14 @@ RZ_API const RzPVector /*<RzBinClass *>*/ *rz_bin_object_get_classes(RZ_NONNULL
}

/**
* \brief Get list of \p RzBinString representing the strings identified in the binary object.
* \brief Get RzPVector of \p RzBinString representing the strings identified in the binary object.
*/
RZ_API const RzList /*<RzBinString *>*/ *rz_bin_object_get_strings(RZ_NONNULL RzBinObject *obj) {
RZ_API const RzPVector /*<RzBinString *>*/ *rz_bin_object_get_strings(RZ_NONNULL RzBinObject *obj) {
rz_return_val_if_fail(obj, NULL);
if (!obj->strings) {
return NULL;
}
return obj->strings->list;
return obj->strings->pvec;
}

/**
Expand Down Expand Up @@ -952,29 +952,36 @@ RZ_API ut64 rz_bin_object_v2p(RZ_NONNULL RzBinObject *obj, ut64 vaddr) {
/**
* \brief Allocates and initializes the RzBinStrDb structure with the given list of strings
*
* \param list The list of strings to initialize the database with
* \param pvector The pvector of strings to initialize the database with
*
* \return On success returns a valid pointer, otherwise NULL
*/
RZ_API RZ_OWN RzBinStrDb *rz_bin_string_database_new(RZ_NULLABLE RZ_OWN RzList /*<RzBinString *>*/ *list) {
RZ_API RZ_OWN RzBinStrDb *rz_bin_string_database_new(RZ_NULLABLE RZ_OWN RzPVector /*<RzBinString *>*/ *pvector) {
RzBinStrDb *db = RZ_NEW0(RzBinStrDb);
if (!db) {
RZ_LOG_ERROR("rz_bin: Cannot allocate RzBinStrDb\n");
rz_list_free(list);
rz_pvector_free(pvector);
return NULL;
}

db->list = list ? list : rz_list_newf((RzListFree)rz_bin_string_free);
void **it;
RzBinString *bstr;
db->pvec = rz_pvector_new((RzPVectorFree)rz_bin_string_free);
if (pvector) {
rz_pvector_foreach (pvector, it) {
bstr = *it;
rz_pvector_push(db->pvec, bstr);
}
}
db->phys = ht_up_new0();
db->virt = ht_up_new0();
if (!db->list || !db->phys || !db->virt) {
if (!db->pvec || !db->phys || !db->virt) {
RZ_LOG_ERROR("rz_bin: Cannot allocate RzBinStrDb internal data structure.\n");
goto fail;
}

RzListIter *it;
RzBinString *bstr;
rz_list_foreach (list, it, bstr) {
rz_pvector_foreach (pvector, it) {
bstr = *it;
if (!ht_up_update(db->phys, bstr->paddr, bstr)) {
RZ_LOG_ERROR("rz_bin: Cannot insert/update RzBinString in RzBinStrDb (phys)\n");
goto fail;
Expand All @@ -1000,7 +1007,7 @@ RZ_API void rz_bin_string_database_free(RZ_NULLABLE RzBinStrDb *db) {
if (!db) {
return;
}
rz_list_free(db->list);
rz_pvector_free(db->pvec);
ht_up_free(db->phys);
ht_up_free(db->virt);
free(db);
Expand All @@ -1017,7 +1024,7 @@ RZ_API void rz_bin_string_database_free(RZ_NULLABLE RzBinStrDb *db) {
RZ_API bool rz_bin_string_database_add(RZ_NONNULL RzBinStrDb *db, RZ_NONNULL RzBinString *bstr) {
rz_return_val_if_fail(db && bstr, false);

if (!rz_list_append(db->list, bstr)) {
if (!rz_pvector_push(db->pvec, bstr)) {
RZ_LOG_ERROR("rz_bin: Cannot add RzBinString in RzBinStrDb (list)\n");
return false;
}
Expand Down Expand Up @@ -1047,6 +1054,6 @@ RZ_API bool rz_bin_string_database_remove(RZ_NONNULL RzBinStrDb *db, ut64 addres

ht_up_delete(db->virt, bstr->vaddr);
ht_up_delete(db->phys, bstr->paddr);
rz_list_delete_data(db->list, bstr);
rz_pvector_remove_data(db->pvec, bstr);
return true;
}
7 changes: 4 additions & 3 deletions librz/bin/bobj_process_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ RZ_IPI void rz_bin_set_and_process_strings(RzBinFile *bf, RzBinObject *o) {
return;
}

RzList *strings = NULL;
RzPVector *strings = NULL;
RzBinPlugin *plugin = o->plugin;

if (!plugin->strings || !(strings = plugin->strings(bf))) {
int minlen = (bin->minstrlen > 0) ? bin->minstrlen : plugin->minstrlen;
strings = rz_bin_file_strings(bf, minlen, true);
}

RzListIter *it;
void **it;
RzBinString *string;
rz_list_foreach (strings, it, string) {
rz_pvector_foreach (strings, it) {
string = *it;
// rebase physical address
string->paddr += o->opts.loadaddr;

Expand Down
8 changes: 4 additions & 4 deletions librz/bin/format/dex/dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,14 +736,14 @@ RZ_API RZ_OWN char *rz_bin_dex_access_flags_readable(ut32 access_flags) {
}

/**
* \brief Returns a RzList<RzBinString*> containing the dex strings
* \brief Returns a RzPVector<RzBinString*> containing the dex strings
*/
RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_dex_strings(RZ_NONNULL RzBinDex *dex) {
RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_dex_strings(RZ_NONNULL RzBinDex *dex) {
rz_return_val_if_fail(dex, NULL);

DexString *string;
void **it;
RzList *strings = rz_list_newf(rz_bin_string_free);
RzPVector *strings = rz_pvector_new((RzPVectorFree)rz_bin_string_free);
if (!strings) {
return NULL;
}
Expand All @@ -762,7 +762,7 @@ RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_dex_strings(RZ_NONNULL RzBinDex
bstr->size = string->size;
bstr->string = rz_str_ndup(string->data, string->size);
bstr->type = RZ_STRING_ENC_UTF8;
if (!rz_list_append(strings, bstr)) {
if (!rz_pvector_push(strings, bstr)) {
free(bstr);
}
ordinal++;
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/dex/dex.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ RZ_API void rz_bin_dex_free(RZ_NULLABLE RzBinDex *dex);
RZ_API RZ_OWN char *rz_bin_dex_version(RZ_NONNULL RzBinDex *dex);
RZ_API ut64 rz_bin_dex_debug_info(RZ_NONNULL RzBinDex *dex);

RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_dex_strings(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_dex_strings(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinClass *>*/ *rz_bin_dex_classes(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzList /*<RzBinClassField *>*/ *rz_bin_dex_fields(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_dex_sections(RZ_NONNULL RzBinDex *dex);
Expand Down
12 changes: 6 additions & 6 deletions librz/bin/format/java/class_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -993,11 +993,11 @@ RZ_API RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_java_class_entrypoints(RZ_NONNULL
/**
* \brief Returns a RzList<RzBinString*> containing the strings
*/
RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_java_class_strings(RZ_NONNULL RzBinJavaClass *bin) {
RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_java_class_strings(RZ_NONNULL RzBinJavaClass *bin) {
rz_return_val_if_fail(bin, NULL);

RzList *list = rz_list_newf(rz_bin_string_free);
if (!list) {
RzPVector *vec = rz_pvector_new((RzPVectorFree)rz_bin_string_free);
if (!vec) {
return NULL;
}

Expand Down Expand Up @@ -1025,7 +1025,7 @@ RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_java_class_strings(RZ_NONNULL R
bstr->size = cpool->size;
bstr->string = string;
bstr->type = RZ_STRING_ENC_MUTF8;
rz_list_append(list, bstr);
rz_pvector_push(vec, bstr);
}
}

Expand All @@ -1043,10 +1043,10 @@ RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_java_class_strings(RZ_NONNULL R
bstr->size = attr->attribute_length;
bstr->string = strdup(attr->info);
bstr->type = RZ_STRING_ENC_UTF8;
rz_list_append(list, bstr);
rz_pvector_push(vec, bstr);
}
}
return list;
return vec;
}

static char *add_class_name_to_name(char *name, char *classname) {
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/java/class_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ RZ_API RZ_OWN char *rz_bin_java_class_const_pool_resolve_index(RZ_NONNULL RzBinJ
/* used in bin_java.c and core_java.c */
RZ_API void rz_bin_java_class_as_source_code(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb);
RZ_API RZ_OWN RzBinAddr *rz_bin_java_class_resolve_symbol(RZ_NONNULL RzBinJavaClass *bin, RzBinSpecialSymbol resolve);
RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_java_class_strings(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_java_class_strings(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_java_class_entrypoints(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzList /*<RzBinSymbol *>*/ *rz_bin_java_class_methods_as_symbols(RZ_NONNULL RzBinJavaClass *bin);
RZ_API void rz_bin_java_class_methods_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb);
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/pyc/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ static bool add_string_to_cache(RzBinPycObj *pyc, ut64 addr, const char *data, u
string->ordinal = 0;
string->type = type;
string->string = rz_str_new(data);
if (!rz_list_append(pyc->strings_cache, string)) {
if (!rz_pvector_push(pyc->strings_cache, string)) {
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/pyc/marshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ typedef struct pyc_context {
/* used from marshall.c */
RzList /*<char *>*/ *interned_table;
RzList /*<RzBinSection *>*/ *sections_cache;
RzList /*<RzBinString *>*/ *strings_cache;
RzPVector /*<RzBinString *>*/ *strings_cache;
RzList /*<RzBinSymbol *>*/ *symbols_cache;
RzList /*<RzList<void *> *>*/ *shared;
RzList /*<pyc_object *>*/ *refs; // If you don't have a good reason, do not change this. And also checkout !refs in get_code_object()
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/i/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ RZ_IPI RzBinProcessLanguage rz_bin_process_language_symbol(RzBinObject *o);
RZ_IPI RzBinProcessLanguage rz_bin_process_language_import(RzBinObject *o);

struct rz_bin_string_database_t {
RzList /*<RzBinString *>*/ *list; ///< Contains all the strings in list form
RzPVector /*<RzBinString *>*/ *pvec; ///< Contains all the strings in list form
HtUP /*<ut64, RzBinString*>*/ *phys; ///< Contains all the strings but mapped by physical address
HtUP /*<ut64, RzBinString*>*/ *virt; ///< Contains all the strings but mapped by virtual address
};
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/p/bin_art.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static ut64 baddr(RzBinFile *bf) {
return ao ? ao->art.image_base : 0;
}

static RzList /*<RzBinString *>*/ *strings(RzBinFile *bf) {
static RzPVector /*<RzBinString *>*/ *strings(RzBinFile *bf) {
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions librz/bin/p/bin_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ static RzList /*<RzBinSymbol *>*/ *symbols(RzBinFile *bf) {
return ret;
}

static RzList /*<RzBinString *>*/ *strings(RzBinFile *bf) {
static RzPVector /*<RzBinString *>*/ *strings(RzBinFile *bf) {
// we dont want to find strings in avr bins because there are lot of false positives
return rz_list_newf((RzListFree)rz_bin_string_free);
return rz_pvector_new((RzPVectorFree)rz_bin_string_free);
}

RzBinPlugin rz_bin_plugin_avr = {
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/p/bin_bf.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ static ut64 baddr(RzBinFile *bf) {
return 0;
}

static RzList /*<RzBinString *>*/ *strings(RzBinFile *bf) {
return rz_list_newf((RzListFree)rz_bin_string_free);
static RzPVector /*<RzBinString *>*/ *strings(RzBinFile *bf) {
return rz_pvector_new((RzPVectorFree)rz_bin_string_free);
}

static RzBinInfo *info(RzBinFile *bf) {
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/p/bin_bflt.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ static void destroy(RzBinFile *bf) {
rz_bflt_free(bf->o->bin_obj);
}

static RzList /*<RzBinString *>*/ *strings(RzBinFile *bf) {
static RzPVector /*<RzBinString *>*/ *strings(RzBinFile *bf) {
return rz_bin_file_strings(bf, bf->minstrlen, false);
}

Expand Down
2 changes: 1 addition & 1 deletion librz/bin/p/bin_bios.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static ut64 baddr(RzBinFile *bf) {
}

/* accelerate binary load */
static RzList /*<RzBinString *>*/ *strings(RzBinFile *bf) {
static RzPVector /*<RzBinString *>*/ *strings(RzBinFile *bf) {
return NULL;
}

Expand Down
Loading

0 comments on commit 56de55e

Please sign in to comment.