From c0f62c529b43d06e6fbfe6c2d06e111a3ec7d696 Mon Sep 17 00:00:00 2001 From: Peiwei Hu Date: Wed, 8 Nov 2023 15:51:41 +0800 Subject: [PATCH] Refactor from RzList to RzPVector in RzBinPlugin (#3961) --- librz/bin/bin.c | 6 ------ librz/bin/bin_language.c | 4 +++- librz/bin/bobj.c | 8 ++++---- librz/bin/bobj_process_file.c | 4 ++-- librz/bin/format/dex/dex.c | 14 +++++++------- librz/bin/format/dex/dex.h | 2 +- librz/bin/format/elf/elf.h | 2 +- librz/bin/format/elf/elf_info.c | 12 ++++++------ librz/bin/format/java/class_bin.c | 25 +++++++++++++------------ librz/bin/format/java/class_bin.h | 2 +- librz/bin/format/le/le.c | 8 ++++---- librz/bin/format/le/le.h | 2 +- librz/bin/p/bin_coff.c | 2 +- librz/bin/p/bin_dex.c | 2 +- librz/bin/p/bin_dmp64.c | 6 +++--- librz/bin/p/bin_elf.inc | 2 +- librz/bin/p/bin_java.c | 2 +- librz/bin/p/bin_mach0.c | 8 ++++---- librz/bin/p/bin_mdmp.c | 10 +++++----- librz/bin/p/bin_nro.c | 2 +- librz/bin/p/bin_p9.c | 2 +- librz/bin/p/bin_pe.inc | 9 ++++----- librz/bin/p/bin_wasm.c | 2 +- librz/bin/p/bin_xbe.c | 6 +++--- librz/core/cbin.c | 7 ++++--- librz/core/cfile.c | 25 +++++++++++++++---------- librz/include/rz_bin.h | 7 +++---- librz/include/rz_vector.h | 3 +++ librz/main/rz-diff.c | 22 +++++++++++----------- librz/util/vector.c | 19 +++++++++++++++++++ test/unit/test_vector.c | 15 +++++++++++++++ 31 files changed, 139 insertions(+), 101 deletions(-) diff --git a/librz/bin/bin.c b/librz/bin/bin.c index 6f74b51b2db..acee3a1b28f 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -616,12 +616,6 @@ RZ_DEPRECATE RZ_API RZ_BORROW RzBinInfo *rz_bin_get_info(RzBin *bin) { return o ? (RzBinInfo *)rz_bin_object_get_info(o) : NULL; } -RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_libs(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_libs(o) : NULL; -} - RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_sections(RZ_NONNULL RzBin *bin) { rz_return_val_if_fail(bin, NULL); RzBinObject *o = rz_bin_cur_object(bin); diff --git a/librz/bin/bin_language.c b/librz/bin/bin_language.c index e87f5837df9..f12818d02e9 100644 --- a/librz/bin/bin_language.c +++ b/librz/bin/bin_language.c @@ -127,7 +127,9 @@ RZ_API RzBinLanguage rz_bin_language_detect(RzBinFile *binfile) { } } } - rz_list_foreach (o->libs, iter, lib) { + void **vec_it = NULL; + rz_pvector_foreach (o->libs, vec_it) { + lib = *vec_it; if (is_macho && strstr(lib, "swift")) { info->lang = "swift"; return language_apply_blocks_mask(RZ_BIN_LANGUAGE_SWIFT, is_blocks); diff --git a/librz/bin/bobj.c b/librz/bin/bobj.c index 0418d9a3db5..175a713d48e 100644 --- a/librz/bin/bobj.c +++ b/librz/bin/bobj.c @@ -197,7 +197,7 @@ RZ_IPI void rz_bin_object_free(RzBinObject *o) { rz_list_free(o->entries); rz_list_free(o->fields); rz_pvector_free(o->imports); - rz_list_free(o->libs); + rz_pvector_free(o->libs); rz_list_free(o->maps); rz_pvector_free(o->mem); rz_list_free(o->sections); @@ -679,9 +679,9 @@ RZ_API const RzBinInfo *rz_bin_object_get_info(RZ_NONNULL RzBinObject *obj) { } /** - * \brief Get list of \p char* representing the libraries used by the binary object. + * \brief Get pvector of \p char* representing the libraries used by the binary object. */ -RZ_API const RzList /**/ *rz_bin_object_get_libs(RZ_NONNULL RzBinObject *obj) { +RZ_API const RzPVector /**/ *rz_bin_object_get_libs(RZ_NONNULL RzBinObject *obj) { rz_return_val_if_fail(obj, NULL); return obj->libs; } @@ -804,7 +804,7 @@ RZ_API bool rz_bin_object_is_big_endian(RZ_NONNULL RzBinObject *obj) { */ RZ_API bool rz_bin_object_is_static(RZ_NONNULL RzBinObject *obj) { rz_return_val_if_fail(obj, false); - if (obj->libs && rz_list_length(obj->libs) > 0) { + if (obj->libs && rz_pvector_len(obj->libs) > 0) { return RZ_BIN_DBG_STATIC & obj->info->dbg_info; } return true; diff --git a/librz/bin/bobj_process_file.c b/librz/bin/bobj_process_file.c index bd39f9cc945..51d7e909b0a 100644 --- a/librz/bin/bobj_process_file.c +++ b/librz/bin/bobj_process_file.c @@ -41,9 +41,9 @@ RZ_IPI void rz_bin_set_and_process_file(RzBinFile *bf, RzBinObject *o) { } } - rz_list_free(o->libs); + rz_pvector_free(o->libs); if (!plugin->libs || !(o->libs = plugin->libs(bf))) { - o->libs = rz_list_newf(free); + o->libs = rz_pvector_new(free); } rz_bin_info_free(o->info); diff --git a/librz/bin/format/dex/dex.c b/librz/bin/format/dex/dex.c index b89072d1b16..245ad28e811 100644 --- a/librz/bin/format/dex/dex.c +++ b/librz/bin/format/dex/dex.c @@ -1503,20 +1503,20 @@ static int compare_strings(const void *a, const void *b) { } /** - * \brief Returns a RzList containing the dex libraries + * \brief Returns a RzPVector containing the dex libraries */ -RZ_API RZ_OWN RzList /**/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex) { +RZ_API RZ_OWN RzPVector /**/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex) { rz_return_val_if_fail(dex, NULL); DexMethodId *method_id; DexClassDef *class_def; - RzList *libraries = NULL; + RzPVector *libraries = NULL; ut32 *class_ids = NULL; void **vit; ut32 n_classes = rz_pvector_len(dex->class_defs); if (n_classes < 1) { - return rz_list_newf((RzListFree)free); + return rz_pvector_new((RzPVectorFree)free); } class_ids = RZ_NEWS0(ut32, n_classes); @@ -1531,7 +1531,7 @@ RZ_API RZ_OWN RzList /**/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex j++; } - libraries = rz_list_newf((RzListFree)free); + libraries = rz_pvector_new((RzPVectorFree)free); if (!libraries) { free(class_ids); return NULL; @@ -1564,12 +1564,12 @@ RZ_API RZ_OWN RzList /**/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex object = p; } - if (rz_list_find(libraries, object, compare_strings)) { + if (rz_pvector_find(libraries, object, compare_strings)) { free(object); continue; } - if (!rz_list_append(libraries, object)) { + if (!rz_pvector_push(libraries, object)) { free(object); break; } diff --git a/librz/bin/format/dex/dex.h b/librz/bin/format/dex/dex.h index ab7eed54841..981bc6116d4 100644 --- a/librz/bin/format/dex/dex.h +++ b/librz/bin/format/dex/dex.h @@ -213,7 +213,7 @@ RZ_API RZ_OWN RzList /**/ *rz_bin_dex_fields(RZ_NONNULL RzBin RZ_API RZ_OWN RzList /**/ *rz_bin_dex_sections(RZ_NONNULL RzBinDex *dex); RZ_API RZ_OWN RzList /**/ *rz_bin_dex_symbols(RZ_NONNULL RzBinDex *dex); RZ_API RZ_OWN RzPVector /**/ *rz_bin_dex_imports(RZ_NONNULL RzBinDex *dex); -RZ_API RZ_OWN RzList /**/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex); +RZ_API RZ_OWN RzPVector /**/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex); RZ_API RZ_OWN RzBinAddr *rz_bin_dex_resolve_symbol(RZ_NONNULL RzBinDex *dex, RzBinSpecialSymbol resolve); RZ_API RZ_OWN RzList /**/ *rz_bin_dex_entrypoints(RZ_NONNULL RzBinDex *dex); RZ_API RZ_BORROW RzBuffer *rz_bin_dex_relocations(RZ_NONNULL RzBinDex *dex); diff --git a/librz/bin/format/elf/elf.h b/librz/bin/format/elf/elf.h index 69302a95030..dc0800f1337 100644 --- a/librz/bin/format/elf/elf.h +++ b/librz/bin/format/elf/elf.h @@ -277,7 +277,7 @@ bool Elf_(rz_bin_elf_has_imports)(RZ_NONNULL ELFOBJ *bin); ut64 Elf_(rz_bin_elf_get_targets_map_base)(ELFOBJ *bin); // elf_info.c -RZ_OWN RzList /**/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin); +RZ_OWN RzPVector /**/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin); RZ_OWN Sdb *Elf_(rz_bin_elf_get_symbols_info)(RZ_NONNULL ELFOBJ *bin); RZ_OWN char *Elf_(rz_bin_elf_get_abi)(RZ_NONNULL ELFOBJ *bin); RZ_OWN char *Elf_(rz_bin_elf_get_arch)(RZ_NONNULL ELFOBJ *bin); diff --git a/librz/bin/format/elf/elf_info.c b/librz/bin/format/elf/elf_info.c index a3b423f0463..90d7ce31103 100644 --- a/librz/bin/format/elf/elf_info.c +++ b/librz/bin/format/elf/elf_info.c @@ -870,11 +870,11 @@ static char *get_abi_mips(ELFOBJ *bin) { /** * \brief List all imported lib * \param elf binary - * \return an allocated list of char* + * \return an allocated pvector of char* * * Use dynamic information (dt_needed) to generate a list of imported lib */ -RZ_OWN RzList /**/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin) { +RZ_OWN RzPVector /**/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin) { rz_return_val_if_fail(bin, NULL); if (!Elf_(rz_bin_elf_has_dt_dynamic)(bin) || !bin->dynstr) { @@ -886,7 +886,7 @@ RZ_OWN RzList /**/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin) { return NULL; } - RzList *result = rz_list_newf(free); + RzPVector *result = rz_pvector_new(free); if (!result) { return NULL; } @@ -895,12 +895,12 @@ RZ_OWN RzList /**/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin) { rz_vector_foreach(dt_needed, iter) { char *tmp = Elf_(rz_bin_elf_strtab_get_dup)(bin->dynstr, *iter); if (!tmp) { - rz_list_free(result); + rz_pvector_free(result); return NULL; } - if (!rz_list_append(result, tmp)) { - rz_list_free(result); + if (!rz_pvector_push(result, tmp)) { + rz_pvector_free(result); return NULL; } } diff --git a/librz/bin/format/java/class_bin.c b/librz/bin/format/java/class_bin.c index 48f04eff47a..4d9e7f03a5e 100644 --- a/librz/bin/format/java/class_bin.c +++ b/librz/bin/format/java/class_bin.c @@ -721,16 +721,17 @@ RZ_API void rz_bin_java_class_as_source_code(RZ_NONNULL RzBinJavaClass *bin, RZ_ char *tmp = NULL; ut16 index; - RzListIter *iter; - RzList *list = rz_bin_java_class_as_libraries(bin); - rz_list_foreach (list, iter, tmp) { + void **iter; + RzPVector *vec = rz_bin_java_class_as_libraries(bin); + rz_pvector_foreach (vec, iter) { + tmp = *iter; rz_str_replace_char(tmp, '/', '.'); rz_strbuf_appendf(sb, "import %s;\n", tmp); } - if (rz_list_length(list) > 0) { + if (rz_pvector_len(vec) > 0) { rz_strbuf_appendf(sb, "\n"); } - rz_list_free(list); + rz_pvector_free(vec); rz_strbuf_append(sb, "class"); @@ -1888,13 +1889,13 @@ static int compare_strings(const void *a, const void *b) { } /** - * \brief Returns a RzList containing the class libraries + * \brief Returns a RzPVector containing the class libraries */ -RZ_API RZ_OWN RzList /**/ *rz_bin_java_class_as_libraries(RZ_NONNULL RzBinJavaClass *bin) { +RZ_API RZ_OWN RzPVector /**/ *rz_bin_java_class_as_libraries(RZ_NONNULL RzBinJavaClass *bin) { rz_return_val_if_fail(bin, NULL); - RzList *list = rz_list_newf(free); - if (!list) { + RzPVector *vec = rz_pvector_new(free); + if (!vec) { return NULL; } ut16 arg0, arg1; @@ -1929,14 +1930,14 @@ RZ_API RZ_OWN RzList /**/ *rz_bin_java_class_as_libraries(RZ_NONNULL RzB // arg0 is name_index tmp = java_class_constant_pool_stringify_at(bin, arg0); } - if (tmp && !rz_list_find(list, tmp, compare_strings)) { - rz_list_append(list, tmp); + if (tmp && !rz_pvector_find(vec, tmp, compare_strings)) { + rz_pvector_push(vec, tmp); } else { free(tmp); } } } - return list; + return vec; } /** diff --git a/librz/bin/format/java/class_bin.h b/librz/bin/format/java/class_bin.h index e2e9931fde5..ac22294c958 100644 --- a/librz/bin/format/java/class_bin.h +++ b/librz/bin/format/java/class_bin.h @@ -96,7 +96,7 @@ RZ_API RZ_OWN RzPVector /**/ *rz_bin_java_class_const_pool_as_imp RZ_API void rz_bin_java_class_const_pool_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb); RZ_API void rz_bin_java_class_const_pool_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j); RZ_API RZ_OWN RzList /**/ *rz_bin_java_class_as_sections(RZ_NONNULL RzBinJavaClass *bin); -RZ_API RZ_OWN RzList /**/ *rz_bin_java_class_as_libraries(RZ_NONNULL RzBinJavaClass *bin); +RZ_API RZ_OWN RzPVector /**/ *rz_bin_java_class_as_libraries(RZ_NONNULL RzBinJavaClass *bin); RZ_API void rz_bin_java_class_interfaces_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb); RZ_API void rz_bin_java_class_interfaces_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j); RZ_API RZ_OWN RzPVector /**/ *rz_bin_java_class_as_classes(RZ_NONNULL RzBinJavaClass *bin); diff --git a/librz/bin/format/le/le.c b/librz/bin/format/le/le.c index aa291885254..579ccc16ab2 100644 --- a/librz/bin/format/le/le.c +++ b/librz/bin/format/le/le.c @@ -1680,20 +1680,20 @@ RZ_OWN RzList /**/ *rz_bin_le_get_entry_points(RzBinFile *bf) { return entries; } -RZ_OWN RzList /**/ *rz_bin_le_get_libs(RzBinFile *bf) { +RZ_OWN RzPVector /**/ *rz_bin_le_get_libs(RzBinFile *bf) { rz_bin_le_obj_t *bin = bf->o->bin_obj; if (rz_pvector_empty(bin->imp_mod_names)) { return NULL; } - RzList *libs = rz_list_new(); + RzPVector *libs = rz_pvector_new(free); if (!libs) { fail_cleanup: - rz_list_free(libs); + rz_pvector_free(libs); return NULL; } void **it; rz_pvector_foreach (bin->imp_mod_names, it) { - CHECK(rz_list_append(libs, *it)); + CHECK(rz_pvector_push(libs, *it)); } return libs; } diff --git a/librz/bin/format/le/le.h b/librz/bin/format/le/le.h index f18a3485c46..68bb84f56ce 100644 --- a/librz/bin/format/le/le.h +++ b/librz/bin/format/le/le.h @@ -108,7 +108,7 @@ RZ_OWN RzList /**/ *rz_bin_le_get_entry_points(RzBinFile *bf); RZ_OWN RzList /**/ *rz_bin_le_get_sections(RzBinFile *bf); RZ_OWN RzList /**/ *rz_bin_le_get_symbols(RzBinFile *bf); RZ_OWN RzPVector /**/ *rz_bin_le_get_imports(RzBinFile *bf); -RZ_OWN RzList /**/ *rz_bin_le_get_libs(RzBinFile *bf); +RZ_OWN RzPVector /**/ *rz_bin_le_get_libs(RzBinFile *bf); RZ_OWN RzPVector /**/ *rz_bin_le_get_relocs(RzBinFile *bf); RZ_OWN RzList /**/ *rz_bin_le_get_virtual_files(RzBinFile *bf); diff --git a/librz/bin/p/bin_coff.c b/librz/bin/p/bin_coff.c index b446d25a9a0..a12f9f02a7a 100644 --- a/librz/bin/p/bin_coff.c +++ b/librz/bin/p/bin_coff.c @@ -375,7 +375,7 @@ static RzPVector /**/ *imports(RzBinFile *bf) { return ret; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { return NULL; } diff --git a/librz/bin/p/bin_dex.c b/librz/bin/p/bin_dex.c index 2e3238c23a3..37c87ab7fd8 100644 --- a/librz/bin/p/bin_dex.c +++ b/librz/bin/p/bin_dex.c @@ -104,7 +104,7 @@ static RzList /**/ *symbols(RzBinFile *bf) { return rz_bin_dex_symbols(dex); } -static RzList /**/ *libraries(RzBinFile *bf) { +static RzPVector /**/ *libraries(RzBinFile *bf) { RzBinDex *dex = rz_bin_file_get_dex(bf); if (!dex) { return NULL; diff --git a/librz/bin/p/bin_dmp64.c b/librz/bin/p/bin_dmp64.c index 8dae093be89..d8476f4abb8 100644 --- a/librz/bin/p/bin_dmp64.c +++ b/librz/bin/p/bin_dmp64.c @@ -227,12 +227,12 @@ static RzList /**/ *maps(RzBinFile *bf) { return ret; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { struct rz_bin_dmp64_obj_t *obj = (struct rz_bin_dmp64_obj_t *)bf->o->bin_obj; if (!obj->drivers) { return NULL; } - RzList *ret = rz_list_newf(free); + RzPVector *ret = rz_pvector_new(free); RzListIter *it; dmp_driver_desc *driver; rz_list_foreach (obj->drivers, it, driver) { @@ -240,7 +240,7 @@ static RzList /**/ *libs(RzBinFile *bf) { if (!file) { break; } - rz_list_append(ret, file); + rz_pvector_push(ret, file); } return ret; } diff --git a/librz/bin/p/bin_elf.inc b/librz/bin/p/bin_elf.inc index 8a4b94b4b5d..a31f7a9fde1 100644 --- a/librz/bin/p/bin_elf.inc +++ b/librz/bin/p/bin_elf.inc @@ -1702,7 +1702,7 @@ static RzPVector /**/ *imports(RzBinFile *bf) { return result; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { rz_return_val_if_fail(bf && bf->o && bf->o->bin_obj, NULL); return Elf_(rz_bin_elf_get_libs)(bf->o->bin_obj); } diff --git a/librz/bin/p/bin_java.c b/librz/bin/p/bin_java.c index 5b22f9f9a59..04d0093f793 100644 --- a/librz/bin/p/bin_java.c +++ b/librz/bin/p/bin_java.c @@ -116,7 +116,7 @@ static RzList /**/ *symbols(RzBinFile *bf) { return list; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { RzBinJavaClass *jclass = rz_bin_file_get_java_class(bf); if (!jclass) { return NULL; diff --git a/librz/bin/p/bin_mach0.c b/librz/bin/p/bin_mach0.c index 490857b8bf7..925cdd67d99 100644 --- a/librz/bin/p/bin_mach0.c +++ b/librz/bin/p/bin_mach0.c @@ -420,20 +420,20 @@ static RzPVector /**/ *relocs(RzBinFile *bf) { return ret; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { int i; char *ptr = NULL; struct lib_t *libs; - RzList *ret = NULL; + RzPVector *ret = NULL; RzBinObject *obj = bf ? bf->o : NULL; - if (!obj || !obj->bin_obj || !(ret = rz_list_newf(free))) { + if (!obj || !obj->bin_obj || !(ret = rz_pvector_new(free))) { return NULL; } if ((libs = MACH0_(get_libs)(obj->bin_obj))) { for (i = 0; !libs[i].last; i++) { ptr = strdup(libs[i].name); - rz_list_append(ret, ptr); + rz_pvector_push(ret, ptr); } free(libs); } diff --git a/librz/bin/p/bin_mdmp.c b/librz/bin/p/bin_mdmp.c index 70b3865842a..7d3f535f1f1 100644 --- a/librz/bin/p/bin_mdmp.c +++ b/librz/bin/p/bin_mdmp.c @@ -121,20 +121,20 @@ static RzBinInfo *mdmp_info(RzBinFile *bf) { return ret; } -static RzList /**/ *mdmp_libs(RzBinFile *bf) { +static RzPVector /**/ *mdmp_libs(RzBinFile *bf) { char *ptr = NULL; int i; MiniDmpObj *obj; struct rz_bin_pe_lib_t *libs = NULL; struct Pe32_rz_bin_mdmp_pe_bin *pe32_bin; struct Pe64_rz_bin_mdmp_pe_bin *pe64_bin; - RzList *ret = NULL; + RzPVector *ret = NULL; RzListIter *it; if (!bf || !bf->o || !bf->o->bin_obj) { return NULL; } - if (!(ret = rz_list_newf(free))) { + if (!(ret = rz_pvector_new(free))) { return NULL; } @@ -148,7 +148,7 @@ static RzList /**/ *mdmp_libs(RzBinFile *bf) { } for (i = 0; !libs[i].last; i++) { ptr = rz_str_newf("[0x%.08" PFMT64x "] - %s", pe32_bin->vaddr, libs[i].name); - rz_list_append(ret, ptr); + rz_pvector_push(ret, ptr); } free(libs); } @@ -158,7 +158,7 @@ static RzList /**/ *mdmp_libs(RzBinFile *bf) { } for (i = 0; !libs[i].last; i++) { ptr = rz_str_newf("[0x%.08" PFMT64x "] - %s", pe64_bin->vaddr, libs[i].name); - rz_list_append(ret, ptr); + rz_pvector_push(ret, ptr); } free(libs); } diff --git a/librz/bin/p/bin_nro.c b/librz/bin/p/bin_nro.c index a8c9546096c..88e0bd0be20 100644 --- a/librz/bin/p/bin_nro.c +++ b/librz/bin/p/bin_nro.c @@ -286,7 +286,7 @@ static RzPVector /**/ *imports(RzBinFile *bf) { return bin->imports_vec; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { return NULL; } diff --git a/librz/bin/p/bin_p9.c b/librz/bin/p/bin_p9.c index 020cfea8d3a..0e3da6cca52 100644 --- a/librz/bin/p/bin_p9.c +++ b/librz/bin/p/bin_p9.c @@ -164,7 +164,7 @@ static RzPVector /**/ *imports(RzBinFile *bf) { return NULL; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { return NULL; } diff --git a/librz/bin/p/bin_pe.inc b/librz/bin/p/bin_pe.inc index b56e0f24747..b1f008dbe5d 100644 --- a/librz/bin/p/bin_pe.inc +++ b/librz/bin/p/bin_pe.inc @@ -490,22 +490,21 @@ static RzPVector /**/ *relocs(RzBinFile *bf) { return NULL; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { struct rz_bin_pe_lib_t *libs = NULL; - RzList *ret = NULL; + RzPVector *ret = NULL; char *ptr = NULL; int i; - if (!(ret = rz_list_new())) { + if (!(ret = rz_pvector_new(free))) { return NULL; } - ret->free = free; if (!(libs = PE_(rz_bin_pe_get_libs)(bf->o->bin_obj))) { return ret; } for (i = 0; !libs[i].last; i++) { ptr = strdup(libs[i].name); - rz_list_append(ret, ptr); + rz_pvector_push(ret, ptr); } free(libs); return ret; diff --git a/librz/bin/p/bin_wasm.c b/librz/bin/p/bin_wasm.c index 5d789500285..1fab4672a38 100644 --- a/librz/bin/p/bin_wasm.c +++ b/librz/bin/p/bin_wasm.c @@ -273,7 +273,7 @@ static RzPVector /**/ *imports(RzBinFile *bf) { return NULL; } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { return NULL; } diff --git a/librz/bin/p/bin_xbe.c b/librz/bin/p/bin_xbe.c index e19b2abd888..95822b604d4 100644 --- a/librz/bin/p/bin_xbe.c +++ b/librz/bin/p/bin_xbe.c @@ -232,13 +232,13 @@ static char *describe_xbe_lib_at(RzBuffer *b, ut64 off, ut64 filesz) { return rz_str_newf("%s %i.%i.%i", name, lib.major, lib.minor, lib.build); } -static RzList /**/ *libs(RzBinFile *bf) { +static RzPVector /**/ *libs(RzBinFile *bf) { if (!bf || !bf->o || !bf->o->bin_obj) { return NULL; } rz_bin_xbe_obj_t *obj = bf->o->bin_obj; xbe_header *h = &obj->header; - RzList *ret = rz_list_newf(free); + RzPVector *ret = rz_pvector_new(free); if (!ret) { return NULL; } @@ -253,7 +253,7 @@ static RzList /**/ *libs(RzBinFile *bf) { if (!lib) { break; } - rz_list_push(ret, lib); + rz_pvector_push(ret, lib); } return ret; diff --git a/librz/core/cbin.c b/librz/core/cbin.c index efeefb5daa2..73cd33f37e0 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -2166,13 +2166,14 @@ RZ_API bool rz_core_bin_imports_print(RZ_NONNULL RzCore *core, RZ_NONNULL RzBinF RZ_API bool rz_core_bin_libs_print(RZ_NONNULL RzCore *core, RZ_NONNULL RzBinFile *bf, RZ_NONNULL RzCmdStateOutput *state) { rz_return_val_if_fail(core && bf && bf->o && state, false); - const RzList *libs = rz_bin_object_get_libs(bf->o); - RzListIter *iter; + const RzPVector *libs = rz_bin_object_get_libs(bf->o); + void **iter; char *lib; rz_cmd_state_output_array_start(state); rz_cmd_state_output_set_columnsf(state, "s", "library"); - rz_list_foreach (libs, iter, lib) { + rz_pvector_foreach (libs, iter) { + lib = *iter; switch (state->mode) { case RZ_OUTPUT_MODE_JSON: pj_s(state->d.pj, lib); diff --git a/librz/core/cfile.c b/librz/core/cfile.c index 5c401422e8a..34f8076c2ad 100644 --- a/librz/core/cfile.c +++ b/librz/core/cfile.c @@ -1056,16 +1056,21 @@ RZ_API bool rz_core_bin_load(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *filen } if (rz_config_get_b(r->config, "bin.libs")) { const char *lib; - RzListIter *iter; - RzList *libs = rz_bin_get_libs(r->bin); - rz_list_foreach (libs, iter, lib) { - if (file_is_loaded(r, lib)) { - continue; - } - RZ_LOG_INFO("Opening library %s\n", lib); - ut64 baddr = rz_io_map_location(r->io, 0x200000); - if (baddr != UT64_MAX) { - rz_core_file_loadlib(r, lib, baddr); + void **iter; + + RzBinObject *o = rz_bin_cur_object(r->bin); + const RzPVector *libs = rz_bin_object_get_libs(o); + if (libs) { + rz_pvector_foreach (libs, iter) { + lib = *iter; + if (file_is_loaded(r, lib)) { + continue; + } + RZ_LOG_INFO("Opening library %s\n", lib); + ut64 baddr = rz_io_map_location(r->io, 0x200000); + if (baddr != UT64_MAX) { + rz_core_file_loadlib(r, lib, baddr); + } } } diff --git a/librz/include/rz_bin.h b/librz/include/rz_bin.h index 141422244fd..5a3a73186e9 100644 --- a/librz/include/rz_bin.h +++ b/librz/include/rz_bin.h @@ -279,7 +279,7 @@ typedef struct rz_bin_object_t { HtPP /**/ *import_name_symbols; // currently only used for imports, but could be extended to all symbols if needed. RzList /**/ *entries; RzList /**/ *fields; - RzList /**/ *libs; + RzPVector /**/ *libs; RzBinRelocStorage *relocs; RzBinStrDb *strings; RzPVector /**/ *classes; @@ -539,7 +539,7 @@ typedef struct rz_bin_plugin_t { RzList /**/ *(*strings)(RzBinFile *bf); RzBinInfo *(*info)(RzBinFile *bf); RzList /**/ *(*fields)(RzBinFile *bf); - RzList /**/ *(*libs)(RzBinFile *bf); + RzPVector /**/ *(*libs)(RzBinFile *bf); RzPVector /**/ *(*relocs)(RzBinFile *bf); RzPVector /**/ *(*trycatch)(RzBinFile *bf); RzPVector /**/ *(*classes)(RzBinFile *bf); @@ -924,7 +924,6 @@ RZ_API RZ_OWN RzList /**/ *rz_bin_file_strings(RZ_NONNULL RzBinFi // use RzBinFile instead RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_entries(RZ_NONNULL RzBin *bin); RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_fields(RZ_NONNULL RzBin *bin); -RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_libs(RZ_NONNULL RzBin *bin); RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_sections(RZ_NONNULL RzBin *bin); RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_strings(RZ_NONNULL RzBin *bin); RZ_DEPRECATE RZ_API RZ_BORROW RzList /**/ *rz_bin_get_symbols(RZ_NONNULL RzBin *bin); @@ -935,7 +934,7 @@ RZ_API const RzList /**/ *rz_bin_object_get_entries(RZ_NONNULL RzBi RZ_API const RzList /**/ *rz_bin_object_get_fields(RZ_NONNULL RzBinObject *obj); RZ_API const RzPVector /**/ *rz_bin_object_get_imports(RZ_NONNULL RzBinObject *obj); RZ_API const RzBinInfo *rz_bin_object_get_info(RZ_NONNULL RzBinObject *obj); -RZ_API const RzList /**/ *rz_bin_object_get_libs(RZ_NONNULL RzBinObject *obj); +RZ_API const RzPVector /**/ *rz_bin_object_get_libs(RZ_NONNULL RzBinObject *obj); RZ_API const RzList /**/ *rz_bin_object_get_sections_all(RZ_NONNULL RzBinObject *obj); RZ_API RZ_OWN RzList /**/ *rz_bin_object_get_sections(RZ_NONNULL RzBinObject *obj); RZ_API RZ_OWN RzList /**/ *rz_bin_object_get_segments(RZ_NONNULL RzBinObject *obj); diff --git a/librz/include/rz_vector.h b/librz/include/rz_vector.h index 6e7384b91aa..dc7689032ba 100644 --- a/librz/include/rz_vector.h +++ b/librz/include/rz_vector.h @@ -290,6 +290,9 @@ static inline void *rz_pvector_tail(RzPVector *vec) { // returns the respective pointer inside the vector if x is found or NULL otherwise. RZ_API void **rz_pvector_contains(RzPVector *vec, const void *x); +// find the element in the vec based on cmparator +RZ_API RZ_BORROW void **rz_pvector_find(RZ_NONNULL const RzPVector *vec, RZ_NONNULL const void *element, RZ_NONNULL RzPVectorComparator cmp); + // removes and returns the pointer at the given index. Does not call free. RZ_API void *rz_pvector_remove_at(RzPVector *vec, size_t index); diff --git a/librz/main/rz-diff.c b/librz/main/rz-diff.c index ba4d8c6413d..1111c9355ce 100644 --- a/librz/main/rz-diff.c +++ b/librz/main/rz-diff.c @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2021 deroad +// SPDX-FileCopyrightText: 2021 deroad // SPDX-License-Identifier: LGPL-3.0-only #include @@ -1075,31 +1075,31 @@ static void libs_stringify(const char *elem, RzStrBuf *sb) { } static RzDiff *rz_diff_libraries_new(DiffFile *dfile_a, DiffFile *dfile_b) { - RzList *list_a = NULL; - RzList *list_b = NULL; + RzPVector *vec_a = NULL; + RzPVector *vec_b = NULL; - list_a = rz_diff_file_get(dfile_a, libs); - if (!list_a) { + vec_a = rz_diff_file_get(dfile_a, libs); + if (!vec_a) { rz_diff_error_ret(NULL, "cannot get libraries from '%s'\n", dfile_a->dio->filename); } - list_b = rz_diff_file_get(dfile_b, libs); - if (!list_b) { + vec_b = rz_diff_file_get(dfile_b, libs); + if (!vec_b) { rz_diff_error_ret(NULL, "cannot get libraries from '%s'\n", dfile_b->dio->filename); } - rz_list_sort(list_a, (RzListComparator)libs_compare); - rz_list_sort(list_b, (RzListComparator)libs_compare); + rz_pvector_sort(vec_a, (RzPVectorComparator)libs_compare); + rz_pvector_sort(vec_b, (RzPVectorComparator)libs_compare); RzDiffMethods methods = { - .elem_at = (RzDiffMethodElemAt)rz_diff_list_elem_at, + .elem_at = (RzDiffMethodElemAt)rz_diff_pvector_elem_at, .elem_hash = (RzDiffMethodElemHash)libs_hash, .compare = (RzDiffMethodCompare)libs_compare, .stringify = (RzDiffMethodStringify)libs_stringify, .ignore = NULL, }; - return rz_diff_generic_new(list_a, rz_list_length(list_a), list_b, rz_list_length(list_b), &methods); + return rz_diff_generic_new(vec_a, rz_pvector_len(vec_a), vec_b, rz_pvector_len(vec_b), &methods); } /**************************************** sections ***************************************/ diff --git a/librz/util/vector.c b/librz/util/vector.c index ea14a647138..ada6a43664f 100644 --- a/librz/util/vector.c +++ b/librz/util/vector.c @@ -382,6 +382,25 @@ RZ_API void **rz_pvector_contains(RzPVector *vec, const void *x) { return NULL; } +/** + * \brief Find the \p element in the \p vec + * \param vec the RzPVector to search in + * \param element the element to search for + * \param cmp the comparator function + * \return the iter of the element if found, NULL otherwise + */ +RZ_API RZ_BORROW void **rz_pvector_find(RZ_NONNULL const RzPVector *vec, RZ_NONNULL const void *element, RZ_NONNULL RzPVectorComparator cmp) { + rz_return_val_if_fail(vec, NULL); + + void **iter; + rz_pvector_foreach (vec, iter) { + if (!cmp(*iter, element)) { + return iter; + } + } + return NULL; +} + RZ_API void *rz_pvector_remove_at(RzPVector *vec, size_t index) { rz_return_val_if_fail(vec, NULL); void *r = rz_pvector_at(vec, index); diff --git a/test/unit/test_vector.c b/test/unit/test_vector.c index 0d27ce5fed0..ad4bf3f5e4f 100644 --- a/test/unit/test_vector.c +++ b/test/unit/test_vector.c @@ -893,6 +893,20 @@ static bool test_pvector_set(void) { mu_end; } +static int compare_int(const void *a, const void *b) { + return *(ut32 *)a - *(ut32 *)b; +} + +static bool test_pvector_find(void) { + RzPVector v; + init_test_pvector(&v, 5, 0); + void *e = ((void **)v.v.a)[3]; + ut32 e_val = 3; + void **p = rz_pvector_find(&v, &e_val, compare_int); + mu_assert_eq(*p, e, "find"); + mu_end; +} + static bool test_pvector_contains(void) { RzPVector v; init_test_pvector(&v, 5, 0); @@ -1393,6 +1407,7 @@ static int all_tests(void) { mu_run_test(test_pvector_free); mu_run_test(test_pvector_at); mu_run_test(test_pvector_set); + mu_run_test(test_pvector_find); mu_run_test(test_pvector_contains); mu_run_test(test_pvector_remove_at); mu_run_test(test_pvector_insert);