Skip to content

Commit

Permalink
Refactor <libs> from RzList to RzPVector in RzBinPlugin (#3961)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu authored Nov 8, 2023
1 parent ec4cdf0 commit c0f62c5
Show file tree
Hide file tree
Showing 31 changed files with 139 additions and 101 deletions.
6 changes: 0 additions & 6 deletions librz/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 /*<char *>*/ *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 /*<RzBinSection *>*/ *rz_bin_get_sections(RZ_NONNULL RzBin *bin) {
rz_return_val_if_fail(bin, NULL);
RzBinObject *o = rz_bin_cur_object(bin);
Expand Down
4 changes: 3 additions & 1 deletion librz/bin/bin_language.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions librz/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 /*<char *>*/ *rz_bin_object_get_libs(RZ_NONNULL RzBinObject *obj) {
RZ_API const RzPVector /*<char *>*/ *rz_bin_object_get_libs(RZ_NONNULL RzBinObject *obj) {
rz_return_val_if_fail(obj, NULL);
return obj->libs;
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/bobj_process_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions librz/bin/format/dex/dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1503,20 +1503,20 @@ static int compare_strings(const void *a, const void *b) {
}

/**
* \brief Returns a RzList<char*> containing the dex libraries
* \brief Returns a RzPVector<char*> containing the dex libraries
*/
RZ_API RZ_OWN RzList /*<char *>*/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex) {
RZ_API RZ_OWN RzPVector /*<char *>*/ *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);
Expand All @@ -1531,7 +1531,7 @@ RZ_API RZ_OWN RzList /*<char *>*/ *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;
Expand Down Expand Up @@ -1564,12 +1564,12 @@ RZ_API RZ_OWN RzList /*<char *>*/ *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;
}
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 @@ -213,7 +213,7 @@ RZ_API RZ_OWN RzList /*<RzBinClassField *>*/ *rz_bin_dex_fields(RZ_NONNULL RzBin
RZ_API RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_dex_sections(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzList /*<RzBinSymbol *>*/ *rz_bin_dex_symbols(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinImport *>*/ *rz_bin_dex_imports(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzList /*<char *>*/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<char *>*/ *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 /*<RzBinAddr *>*/ *rz_bin_dex_entrypoints(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_BORROW RzBuffer *rz_bin_dex_relocations(RZ_NONNULL RzBinDex *dex);
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/elf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 /*<char *>*/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin);
RZ_OWN RzPVector /*<char *>*/ *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);
Expand Down
12 changes: 6 additions & 6 deletions librz/bin/format/elf/elf_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 /*<char *>*/ *Elf_(rz_bin_elf_get_libs)(RZ_NONNULL ELFOBJ *bin) {
RZ_OWN RzPVector /*<char *>*/ *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) {
Expand All @@ -886,7 +886,7 @@ RZ_OWN RzList /*<char *>*/ *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;
}
Expand All @@ -895,12 +895,12 @@ RZ_OWN RzList /*<char *>*/ *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;
}
}
Expand Down
25 changes: 13 additions & 12 deletions librz/bin/format/java/class_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -1888,13 +1889,13 @@ static int compare_strings(const void *a, const void *b) {
}

/**
* \brief Returns a RzList<char*> containing the class libraries
* \brief Returns a RzPVector<char*> containing the class libraries
*/
RZ_API RZ_OWN RzList /*<char *>*/ *rz_bin_java_class_as_libraries(RZ_NONNULL RzBinJavaClass *bin) {
RZ_API RZ_OWN RzPVector /*<char *>*/ *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;
Expand Down Expand Up @@ -1929,14 +1930,14 @@ RZ_API RZ_OWN RzList /*<char *>*/ *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;
}

/**
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 @@ -96,7 +96,7 @@ RZ_API RZ_OWN RzPVector /*<RzBinImport *>*/ *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 /*<RzBinSection *>*/ *rz_bin_java_class_as_sections(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzList /*<char *>*/ *rz_bin_java_class_as_libraries(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzPVector /*<char *>*/ *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 /*<RzBinClass *>*/ *rz_bin_java_class_as_classes(RZ_NONNULL RzBinJavaClass *bin);
Expand Down
8 changes: 4 additions & 4 deletions librz/bin/format/le/le.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,20 +1680,20 @@ RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf) {
return entries;
}

RZ_OWN RzList /*<char *>*/ *rz_bin_le_get_libs(RzBinFile *bf) {
RZ_OWN RzPVector /*<char *>*/ *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;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/le/le.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf);
RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_le_get_sections(RzBinFile *bf);
RZ_OWN RzList /*<RzBinSymbol *>*/ *rz_bin_le_get_symbols(RzBinFile *bf);
RZ_OWN RzPVector /*<RzBinImport *>*/ *rz_bin_le_get_imports(RzBinFile *bf);
RZ_OWN RzList /*<char *>*/ *rz_bin_le_get_libs(RzBinFile *bf);
RZ_OWN RzPVector /*<char *>*/ *rz_bin_le_get_libs(RzBinFile *bf);
RZ_OWN RzPVector /*<RzBinReloc *>*/ *rz_bin_le_get_relocs(RzBinFile *bf);
RZ_OWN RzList /*<RzBinVirtualFile *>*/ *rz_bin_le_get_virtual_files(RzBinFile *bf);

Expand Down
2 changes: 1 addition & 1 deletion librz/bin/p/bin_coff.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ static RzPVector /*<RzBinImport *>*/ *imports(RzBinFile *bf) {
return ret;
}

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

Expand Down
2 changes: 1 addition & 1 deletion librz/bin/p/bin_dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static RzList /*<RzBinSymbol *>*/ *symbols(RzBinFile *bf) {
return rz_bin_dex_symbols(dex);
}

static RzList /*<char *>*/ *libraries(RzBinFile *bf) {
static RzPVector /*<char *>*/ *libraries(RzBinFile *bf) {
RzBinDex *dex = rz_bin_file_get_dex(bf);
if (!dex) {
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions librz/bin/p/bin_dmp64.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,20 @@ static RzList /*<RzBinMap *>*/ *maps(RzBinFile *bf) {
return ret;
}

static RzList /*<char *>*/ *libs(RzBinFile *bf) {
static RzPVector /*<char *>*/ *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) {
char *file = strdup(driver->file);
if (!file) {
break;
}
rz_list_append(ret, file);
rz_pvector_push(ret, file);
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/p/bin_elf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ static RzPVector /*<RzBinImport *>*/ *imports(RzBinFile *bf) {
return result;
}

static RzList /*<char *>*/ *libs(RzBinFile *bf) {
static RzPVector /*<char *>*/ *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);
}
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/p/bin_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static RzList /*<RzBinSymbol *>*/ *symbols(RzBinFile *bf) {
return list;
}

static RzList /*<char *>*/ *libs(RzBinFile *bf) {
static RzPVector /*<char *>*/ *libs(RzBinFile *bf) {
RzBinJavaClass *jclass = rz_bin_file_get_java_class(bf);
if (!jclass) {
return NULL;
Expand Down
8 changes: 4 additions & 4 deletions librz/bin/p/bin_mach0.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,20 @@ static RzPVector /*<RzBinReloc *>*/ *relocs(RzBinFile *bf) {
return ret;
}

static RzList /*<char *>*/ *libs(RzBinFile *bf) {
static RzPVector /*<char *>*/ *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);
}
Expand Down
Loading

0 comments on commit c0f62c5

Please sign in to comment.