From 3f5675d0d583d7d44ef194a5d95aa1b7a50782d7 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Sat, 19 Oct 2024 06:01:31 -0500 Subject: [PATCH] Refactor: Use HtSP for storing plugins. This saves iterations when searching for a plugin of a given name. It also allows to save plugin configurations and data in hash table, all addressed by their name. Hence, usage is more streamlined. --- librz/arch/analysis.c | 13 +++-- librz/arch/asm.c | 49 +++++++++------- librz/bin/bin.c | 110 +++++++++++++++++------------------- librz/bp/bp.c | 4 +- librz/bp/bp_plugin.c | 38 +++++++------ librz/core/canalysis.c | 6 +- librz/core/casm.c | 13 +++-- librz/core/cautocmpl.c | 11 ++-- librz/core/cbin.c | 10 +++- librz/core/cconfig.c | 15 +++-- librz/core/ccrypto.c | 6 +- librz/core/cdebug.c | 5 +- librz/core/chash.c | 6 +- librz/core/cio.c | 5 +- librz/core/cmd/cmd_egg.c | 5 +- librz/core/core.c | 5 +- librz/core/cplugin.c | 15 ++--- librz/crypto/crypto.c | 23 ++++---- librz/debug/debug.c | 3 +- librz/debug/plugin.c | 22 +++----- librz/egg/egg.c | 18 +++--- librz/hash/hash.c | 40 ++++--------- librz/include/rz_analysis.h | 2 +- librz/include/rz_asm.h | 4 +- librz/include/rz_bin.h | 4 +- librz/include/rz_bp.h | 2 +- librz/include/rz_core.h | 2 +- librz/include/rz_crypto.h | 8 +-- librz/include/rz_debug.h | 2 +- librz/include/rz_egg.h | 2 +- librz/include/rz_hash.h | 4 +- librz/include/rz_io.h | 2 +- librz/include/rz_lib.h | 17 ------ librz/io/io.c | 2 +- librz/io/io_plugin.c | 18 +++--- librz/main/rz-gg.c | 10 +++- librz/main/rz-hash.c | 8 ++- librz/util/iterator.c | 2 +- 38 files changed, 254 insertions(+), 257 deletions(-) diff --git a/librz/arch/analysis.c b/librz/arch/analysis.c index 890d524fdc6..6f83e4f59a2 100644 --- a/librz/arch/analysis.c +++ b/librz/arch/analysis.c @@ -117,7 +117,7 @@ RZ_API RzAnalysis *rz_analysis_new(void) { analysis->leaddrs = NULL; analysis->imports = rz_list_newf(free); rz_analysis_set_bits(analysis, 32); - analysis->plugins = rz_list_new(); + analysis->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); if (analysis->plugins) { const size_t n_plugins = rz_arch_get_n_plugins(); for (size_t i = 0; i < n_plugins; i++) { @@ -189,7 +189,7 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) { rz_str_constpool_fini(&a->constpool); ht_sp_free(a->ht_global_var); ht_up_free(a->ht_rop_semantics); - rz_list_free(a->plugins); + ht_sp_free(a->plugins); rz_analysis_debug_info_free(a->debug_info); free(a); return NULL; @@ -197,7 +197,7 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) { RZ_API bool rz_analysis_plugin_add(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPlugin *p) { rz_return_val_if_fail(analysis && p, false); - RZ_PLUGIN_CHECK_AND_ADD(analysis->plugins, p, RzAnalysisPlugin); + ht_sp_insert(analysis->plugins, p->name, p); return true; } @@ -207,18 +207,18 @@ RZ_API bool rz_analysis_plugin_del(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPl plugin_fini(analysis); analysis->cur = NULL; } - return rz_list_delete_data(analysis->plugins, p); + return ht_sp_delete(analysis->plugins, p->name); } RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) { - RzListIter *it; + RzIterator *it = ht_sp_as_iter(analysis->plugins); RzAnalysisPlugin *h; if (analysis) { if (analysis->cur && !strcmp(analysis->cur->name, name)) { return true; } - rz_list_foreach (analysis->plugins, it, h) { + rz_iterator_foreach(it, h) { if (!h || !h->name || strcmp(h->name, name)) { continue; } @@ -234,6 +234,7 @@ RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) { } return true; } + rz_iterator_free(it); } return false; } diff --git a/librz/arch/asm.c b/librz/arch/asm.c index 33d480491b8..8deb3944bcb 100644 --- a/librz/arch/asm.c +++ b/librz/arch/asm.c @@ -283,7 +283,7 @@ RZ_API RzAsm *rz_asm_new(void) { a->bits = RZ_SYS_BITS; a->bitshift = 0; a->syntax = RZ_ASM_SYNTAX_INTEL; - a->plugins = rz_list_new(); + a->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); if (!a->plugins) { free(a); return NULL; @@ -338,7 +338,7 @@ RZ_API void rz_asm_free(RzAsm *a) { } plugin_fini(a); if (a->plugins) { - rz_list_free(a->plugins); + ht_sp_free(a->plugins); a->plugins = NULL; } rz_syscall_free(a->syscall); @@ -358,7 +358,7 @@ RZ_API bool rz_asm_plugin_add(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) { if (rz_asm_is_valid(a, p->name)) { return false; } - RZ_PLUGIN_CHECK_AND_ADD(a->plugins, p, RzAsmPlugin); + ht_sp_insert(a->plugins, p->name, p); return true; } @@ -371,37 +371,41 @@ RZ_API bool rz_asm_plugin_del(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) { if (a->acur == p) { a->acur = NULL; } - return rz_list_delete_data(a->plugins, p); + return ht_sp_delete(a->plugins, p->name); } RZ_API bool rz_asm_is_valid(RzAsm *a, const char *name) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); if (!name || !*name) { return false; } - rz_list_foreach (a->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (!strcmp(h->name, name)) { return true; } } + rz_iterator_free(iter); return false; } RZ_API bool rz_asm_use_assembler(RzAsm *a, const char *name) { RzAsmPlugin *h; - RzListIter *iter; - if (a) { - if (name && *name) { - rz_list_foreach (a->plugins, iter, h) { - if (h->assemble && !strcmp(h->name, name)) { - a->acur = h; - return true; - } - } - } + RzIterator *iter = ht_sp_as_iter(a->plugins); + if (!a) { + return false; + } + if (!(name && *name)) { a->acur = NULL; } + rz_iterator_foreach(iter, h) { + if (h->assemble && !strcmp(h->name, name)) { + a->acur = h; + return true; + } + } + rz_iterator_free(iter); + a->acur = NULL; return false; } @@ -439,7 +443,7 @@ static void remove_plugin_config(RZ_BORROW RzCore *core, const char *plugin_name */ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); if (!a || !name) { return false; } @@ -447,7 +451,7 @@ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { if (a->cur && !strcmp(a->cur->arch, name)) { return true; } - rz_list_foreach (a->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (h->arch && h->name && !strcmp(h->name, name)) { if (!a->cur || (a->cur && strcmp(a->cur->arch, h->arch))) { plugin_fini(a); @@ -476,6 +480,7 @@ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { return true; } } + rz_iterator_free(iter); sdb_free(a->pair); a->pair = NULL; return false; @@ -628,11 +633,11 @@ static bool assemblerMatches(RzAsm *a, RzAsmPlugin *h) { static Ase findAssembler(RzAsm *a, const char *kw) { Ase ase = NULL; RzAsmPlugin *h; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); if (a->acur && a->acur->assemble) { return a->acur->assemble; } - rz_list_foreach (a->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (assemblerMatches(a, h)) { if (kw) { if (strstr(h->name, kw)) { @@ -643,6 +648,7 @@ static Ase findAssembler(RzAsm *a, const char *kw) { } } } + rz_iterator_free(iter); return ase; } @@ -1193,7 +1199,8 @@ RZ_API char *rz_asm_describe(RzAsm *a, const char *str) { return (a && a->pair) ? sdb_get(a->pair, str) : NULL; } -RZ_API RzList /**/ *rz_asm_get_plugins(RzAsm *a) { +RZ_API RZ_BORROW HtSP /**/ *rz_asm_get_plugins(RZ_BORROW RZ_NONNULL RzAsm *a) { + rz_return_val_if_fail(a, NULL); return a->plugins; } diff --git a/librz/bin/bin.c b/librz/bin/bin.c index 174a2914a90..476bb547ff6 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -11,8 +11,9 @@ #include "i/private.h" // include both generated plugin lists. -#include "rz_bin_plugins.h" -#include "rz_bin_xtr_plugins.h" +#include +#include +#include RZ_LIB_VERSION(rz_bin); @@ -257,7 +258,7 @@ RZ_API RzBinFile *rz_bin_reload(RzBin *bin, RzBinFile *bf, ut64 baseaddr) { RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) { rz_return_val_if_fail(bin && opt, NULL); - RzListIter *it; + RzIterator *it = ht_sp_as_iter(bin->binxtrs); RzBinXtrPlugin *xtr; bin->file = opt->filename; @@ -270,7 +271,7 @@ RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) // XXX - for the time being this is fine, but we may want to // change the name to something like // : - rz_list_foreach (bin->binxtrs, it, xtr) { + rz_iterator_foreach(it, xtr) { if (!xtr->check_buffer) { RZ_LOG_ERROR("Missing check_buffer callback for '%s'\n", xtr->name); continue; @@ -285,6 +286,7 @@ RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) } } } + rz_iterator_free(it); if (!bf) { // Uncomment for this speedup: 20s vs 22s // RzBuffer *buf = rz_buf_new_slurp (bin->file); @@ -352,73 +354,66 @@ RZ_API RzBinFile *rz_bin_open_io(RzBin *bin, RzBinOptions *opt) { } RZ_IPI RzBinPlugin *rz_bin_get_binplugin_by_name(RzBin *bin, const char *name) { - RzBinPlugin *plugin; - RzListIter *it; - rz_return_val_if_fail(bin && name, NULL); - rz_list_foreach (bin->plugins, it, plugin) { - if (!strcmp(plugin->name, name)) { - return plugin; - } + bool found = false; + RzBinPlugin *plugin = ht_sp_find(bin->plugins, name, &found); + if (found) { + return plugin; } return NULL; } RZ_API RzBinPlugin *rz_bin_get_binplugin_by_buffer(RzBin *bin, RzBuffer *buf) { RzBinPlugin *plugin; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(bin->plugins); rz_return_val_if_fail(bin && buf, NULL); - rz_list_foreach (bin->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (plugin->check_buffer) { if (plugin->check_buffer(buf)) { return plugin; } } } + rz_iterator_free(it); return NULL; } RZ_IPI RzBinPlugin *rz_bin_get_binplugin_by_filename(RzBin *bin) { RzBinPlugin *plugin; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(bin->plugins); rz_return_val_if_fail(bin, NULL); const char *filename = strrchr(bin->file, RZ_SYS_DIR[0]); filename = filename ? filename + 1 : bin->file; - rz_list_foreach (bin->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (plugin->check_filename) { if (plugin->check_filename(filename)) { return plugin; } } } + rz_iterator_free(it); return NULL; } RZ_IPI RzBinXtrPlugin *rz_bin_get_xtrplugin_by_name(RzBin *bin, const char *name) { - RzBinXtrPlugin *xtr; - RzListIter *it; - rz_return_val_if_fail(bin && name, NULL); - // TODO: use a hashtable here - rz_list_foreach (bin->binxtrs, it, xtr) { - if (!strcmp(xtr->name, name)) { - return xtr; - } - // must be set to null - xtr = NULL; + bool found = false; + RzBinXtrPlugin *xtr = ht_sp_find(bin->binxtrs, name, &found); + if (found) { + return xtr; } return NULL; } RZ_API bool rz_bin_plugin_add(RzBin *bin, RZ_NONNULL RzBinPlugin *plugin) { rz_return_val_if_fail(bin && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(bin->plugins, plugin, RzBinPlugin); + ht_sp_insert(bin->plugins, plugin->name, plugin); return true; } @@ -433,13 +428,13 @@ RZ_API bool rz_bin_plugin_del(RzBin *bin, RZ_NONNULL RzBinPlugin *plugin) { rz_bin_file_delete(bin, bf); } } - return rz_list_delete_data(bin->plugins, plugin); + return ht_sp_delete(bin->plugins, plugin->name); } RZ_API bool rz_bin_xtr_plugin_add(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin) { rz_return_val_if_fail(bin && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(bin->binxtrs, plugin, RzBinXtrPlugin); + ht_sp_insert(bin->binxtrs, plugin->name, plugin); if (plugin->init) { plugin->init(bin->user); } @@ -464,7 +459,7 @@ RZ_API bool rz_bin_xtr_plugin_del(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin) } } } - return rz_list_delete_data(bin->binxtrs, plugin); + return ht_sp_delete(bin->binxtrs, plugin->name); } RZ_API void rz_bin_free(RZ_NULLABLE RzBin *bin) { @@ -477,13 +472,14 @@ RZ_API void rz_bin_free(RZ_NULLABLE RzBin *bin) { // rz_bin_free_bin_files (bin); rz_list_free(bin->binfiles); - RzListIter *it, *tmp; + RzIterator *it = ht_sp_as_iter(bin->binxtrs); RzBinXtrPlugin *p; - rz_list_foreach_safe (bin->binxtrs, it, tmp, p) { + rz_iterator_foreach(it, p) { plugin_fini(bin, p); } - rz_list_free(bin->binxtrs); - rz_list_free(bin->plugins); + rz_iterator_free(it); + ht_sp_free(bin->binxtrs); + ht_sp_free(bin->plugins); rz_list_free(bin->default_hashes); sdb_free(bin->sdb); rz_id_storage_free(bin->ids); @@ -544,22 +540,18 @@ static void __printXtrPluginDetails(RzBin *bin, RzBinXtrPlugin *bx, int json) { } RZ_API bool rz_bin_list_plugin(RzBin *bin, const char *name, PJ *pj, int json) { - RzListIter *it; RzBinPlugin *bp; RzBinXtrPlugin *bx; rz_return_val_if_fail(bin && name, false); - rz_list_foreach (bin->plugins, it, bp) { - if (rz_str_cmp(name, bp->name, strlen(name))) { - continue; - } + bool found = false; + bp = ht_sp_find(bin->plugins, name, &found); + if (found) { return rz_bin_print_plugin_details(bin, bp, pj, json); } - rz_list_foreach (bin->binxtrs, it, bx) { - if (rz_str_cmp(name, bx->name, strlen(name))) { - continue; - } + bx = ht_sp_find(bin->binxtrs, name, &found); + if (found) { __printXtrPluginDetails(bin, bx, json); return true; } @@ -763,9 +755,16 @@ RZ_API RzBin *rz_bin_new(void) { /* bin parsers */ bin->binfiles = rz_list_newf((RzListFree)rz_bin_file_free); - bin->plugins = rz_list_new_from_array((const void **)bin_static_plugins, RZ_ARRAY_SIZE(bin_static_plugins)); + + bin->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); + for (size_t i = 0; i < RZ_ARRAY_SIZE(bin_static_plugins); ++i) { + ht_sp_insert(bin->plugins, bin_static_plugins[i]->name, bin_static_plugins[i]); + } /* extractors */ - bin->binxtrs = rz_list_new_from_array((const void **)bin_xtr_static_plugins, RZ_ARRAY_SIZE(bin_xtr_static_plugins)); + bin->binxtrs = ht_sp_new(HT_STR_DUP, NULL, NULL); + for (size_t i = 0; i < RZ_ARRAY_SIZE(bin_xtr_static_plugins); ++i) { + ht_sp_insert(bin->plugins, bin_xtr_static_plugins[i]->name, bin_xtr_static_plugins[i]); + } return bin; @@ -1266,13 +1265,10 @@ RZ_API void rz_bin_trycatch_free(RzBinTrycatch *tc) { RZ_API const RzBinPlugin *rz_bin_plugin_get(RZ_NONNULL RzBin *bin, RZ_NONNULL const char *name) { rz_return_val_if_fail(bin && name, NULL); - RzListIter *iter; - RzBinPlugin *bp; - - rz_list_foreach (bin->plugins, iter, bp) { - if (!strcmp(bp->name, name)) { - return bp; - } + bool found = false; + RzBinPlugin *bp = ht_sp_find(bin->plugins, name, &found); + if (found) { + return bp; } return NULL; } @@ -1282,14 +1278,10 @@ RZ_API const RzBinPlugin *rz_bin_plugin_get(RZ_NONNULL RzBin *bin, RZ_NONNULL co */ RZ_API const RzBinXtrPlugin *rz_bin_xtrplugin_get(RZ_NONNULL RzBin *bin, RZ_NONNULL const char *name) { rz_return_val_if_fail(bin && name, NULL); - - RzListIter *iter; - RzBinXtrPlugin *bp; - - rz_list_foreach (bin->binxtrs, iter, bp) { - if (!strcmp(bp->name, name)) { - return bp; - } + bool found = false; + RzBinXtrPlugin *bp = ht_sp_find(bin->plugins, name, &found); + if (found) { + return bp; } return NULL; } diff --git a/librz/bp/bp.c b/librz/bp/bp.c index c3fad4139fd..d50c265ffa8 100644 --- a/librz/bp/bp.c +++ b/librz/bp/bp.c @@ -36,7 +36,7 @@ RZ_API RzBreakpoint *rz_bp_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *ctx) { bp->traces = rz_bp_traptrace_new(); bp->cb_printf = (PrintfCallback)printf; bp->bps = rz_list_newf((RzListFree)rz_bp_item_free); - bp->plugins = rz_list_new(); + bp->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); bp->nhwbps = 0; for (i = 0; i < RZ_ARRAY_SIZE(bp_static_plugins); i++) { rz_bp_plugin_add(bp, bp_static_plugins[i]); @@ -47,7 +47,7 @@ RZ_API RzBreakpoint *rz_bp_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *ctx) { RZ_API RzBreakpoint *rz_bp_free(RzBreakpoint *bp) { rz_list_free(bp->bps); - rz_list_free(bp->plugins); + ht_sp_free(bp->plugins); rz_list_free(bp->traces); free(bp->bps_idx); free(bp); diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index c84f6e8e1eb..c7419ef61f3 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -3,34 +3,34 @@ #include #include +#include +#include RZ_API int rz_bp_plugin_del_byname(RzBreakpoint *bp, RZ_NONNULL const char *name) { rz_return_val_if_fail(bp && name, false); - RzListIter *iter; - RzBreakpointPlugin *h; - rz_list_foreach (bp->plugins, iter, h) { - if (!strcmp(h->name, name)) { - if (bp->cur == h) { - bp->cur = NULL; - } - rz_list_delete(bp->plugins, iter); - bp->nbps--; - return true; - } + bool found = false; + RzBreakpointPlugin *bp_plugin = ht_sp_find(bp->plugins, name, &found); + if (!found) { + return false; } - return false; + if (bp_plugin == bp->cur) { + bp->cur = NULL; + } + ht_sp_delete(bp->plugins, name); + bp->nbps--; + return true; } RZ_API bool rz_bp_plugin_add(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpointPlugin *plugin) { rz_return_val_if_fail(bp && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(bp->plugins, plugin, RzBreakpointPlugin); + ht_sp_insert(bp->plugins, plugin->name, plugin); return true; } RZ_API bool rz_bp_plugin_del(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpointPlugin *plugin) { rz_return_val_if_fail(bp && plugin, false); - bool res = rz_list_delete_data(bp->plugins, plugin); + bool res = ht_sp_delete(bp->plugins, plugin->name); if (res) { bp->nbps--; if (bp->cur == plugin) { @@ -45,24 +45,26 @@ RZ_API bool rz_bp_plugin_del(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpoint */ RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { rz_return_val_if_fail(bp && name, false); - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(bp->plugins); RzBreakpointPlugin *h; - rz_list_foreach (bp->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (!strcmp(h->name, name)) { bp->cur = h; return true; } } + rz_iterator_free(iter); return false; } // TODO: deprecate RZ_API void rz_bp_plugin_list(RzBreakpoint *bp) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(bp->plugins); RzBreakpointPlugin *b; - rz_list_foreach (bp->plugins, iter, b) { + rz_iterator_foreach(iter, b) { bp->cb_printf("bp %c %s\n", (bp->cur && !strcmp(bp->cur->name, b->name)) ? '*' : '-', b->name); } + rz_iterator_free(iter); } diff --git a/librz/core/canalysis.c b/librz/core/canalysis.c index a865c18fe68..411e5b80723 100644 --- a/librz/core/canalysis.c +++ b/librz/core/canalysis.c @@ -14,6 +14,7 @@ #include #include "core_private.h" +#include "rz_util/rz_iterator.h" HEAPTYPE(ut64); @@ -3938,13 +3939,14 @@ static bool is_apple_target(RzCore *core) { } static void core_analysis_using_plugins(RzCore *core) { - RzListIter *it; + RzIterator *it = ht_sp_as_iter(core->plugins); const RzCorePlugin *plugin; - rz_list_foreach (core->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (plugin->analysis) { plugin->analysis(core); } } + rz_iterator_free(it); } /** diff --git a/librz/core/casm.c b/librz/core/casm.c index c3734cd7745..f250558b777 100644 --- a/librz/core/casm.c +++ b/librz/core/casm.c @@ -65,10 +65,10 @@ RZ_API char *rz_core_asm_search(RzCore *core, const char *input) { } static const char *has_esil(RzCore *core, const char *name) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(core->analysis->plugins); RzAnalysisPlugin *h; rz_return_val_if_fail(core && core->analysis && name, NULL); - rz_list_foreach (core->analysis->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (!h->name || strcmp(name, h->name)) { continue; } @@ -85,6 +85,7 @@ static const char *has_esil(RzCore *core, const char *name) { // Only the analysis plugin. return "A__"; } + rz_iterator_free(iter); return "___"; } @@ -163,10 +164,10 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC int i; RzAsm *a = core->rasm; RzAsmPlugin *ap; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); RzCmdStatus status; if (arch) { - rz_list_foreach (a->plugins, iter, ap) { + rz_iterator_foreach(iter, ap) { if (ap->cpus && !strcmp(arch, ap->name)) { char *c = rz_str_dup(ap->cpus); int n = rz_str_split(c, ','); @@ -179,17 +180,19 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC } } else { rz_cmd_state_output_array_start(state); - rz_list_foreach (a->plugins, iter, ap) { + rz_iterator_foreach(iter, ap) { const char *license = ap->license ? ap->license : "unknown"; status = rz_core_asm_plugin_print(core, ap, arch, state, license); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(iter); return status; } } rz_cmd_state_output_array_end(state); } + rz_iterator_free(iter); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cautocmpl.c b/librz/core/cautocmpl.c index c35e0c79c3e..2e096db719f 100644 --- a/librz/core/cautocmpl.c +++ b/librz/core/cautocmpl.c @@ -6,8 +6,6 @@ #include #include -#include "core_private.h" - /** * Describe what needs to be autocompleted. */ @@ -193,8 +191,8 @@ static void autocmplt_bits_plugin(RzAsmPlugin *plugin, RzLineNSCompletionResult static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const char *s, size_t len) { rz_return_if_fail(core->rasm); - RzList *asm_plugins = rz_asm_get_plugins(core->rasm); - RzListIter *it; + HtSP *asm_plugins = rz_asm_get_plugins(core->rasm); + RzIterator *it = ht_sp_as_iter(asm_plugins); RzAsmPlugin *plugin; // @a: can either be used with @a:arch or @a:arch:bits @@ -202,7 +200,7 @@ static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const ch const char *delim = rz_sub_str_rchr(s, 0, len, ':'); if (!delim) { // We autocomplete just the architecture part - rz_list_foreach (asm_plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (!strncmp(plugin->name, s, len)) { rz_line_ns_completion_result_add(res, plugin->name); } @@ -211,13 +209,14 @@ static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const ch } else { // We autocomplete the bits part res->start += delim + 1 - s; - rz_list_foreach (asm_plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (!strncmp(plugin->name, s, delim - s)) { autocmplt_bits_plugin(plugin, res, delim + 1, len - (delim + 1 - s)); break; } } } + rz_iterator_free(it); } static void autocmplt_bits(RzCore *core, RzLineNSCompletionResult *res, const char *s, size_t len) { diff --git a/librz/core/cbin.c b/librz/core/cbin.c index 7679ede502c..dada7992cbf 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "../bin/dwarf/dwarf_private.h" #include "core_private.h" @@ -4904,22 +4905,25 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state RzBinPlugin *bp; RzBinXtrPlugin *bx; - RzListIter *iter; RzCmdStatus status; + RzIterator *iter = ht_sp_as_iter(bin->plugins); rz_cmd_state_output_array_start(state); - rz_list_foreach (bin->plugins, iter, bp) { + rz_iterator_foreach(iter, bp) { status = rz_core_bin_plugin_print(bp, state); if (status != RZ_CMD_STATUS_OK) { return status; } } - rz_list_foreach (bin->binxtrs, iter, bx) { + rz_iterator_free(iter); + iter = ht_sp_as_iter(bin->binxtrs); + rz_iterator_foreach(iter, bx) { status = rz_core_binxtr_plugin_print(bx, state); if (status != RZ_CMD_STATUS_OK) { return status; } } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cconfig.c b/librz/core/cconfig.c index 9665c4679a6..dbccfd4e1d0 100644 --- a/librz/core/cconfig.c +++ b/librz/core/cconfig.c @@ -249,13 +249,14 @@ static bool cb_analysis_hpskip(void *user, void *data) { static void update_analysis_arch_options(RzCore *core, RzConfigNode *node) { RzAnalysisPlugin *h; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(core->analysis->plugins); if (core && core->analysis && node) { rz_list_purge(node->options); - rz_list_foreach (core->analysis->plugins, it, h) { + rz_iterator_foreach(it, h) { SETOPTIONS(node, h->name, NULL); } } + rz_iterator_free(it); } static bool cb_analysis_arch(void *user, void *data) { @@ -373,14 +374,14 @@ static bool cb_asmassembler(void *user, void *data) { static void update_asmcpu_options(RzCore *core, RzConfigNode *node) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *it = ht_sp_as_iter(core->rasm->plugins); rz_return_if_fail(core && core->rasm); const char *arch = rz_config_get(core->config, "asm.arch"); if (!arch || !*arch) { return; } rz_list_purge(node->options); - rz_list_foreach (core->rasm->plugins, iter, h) { + rz_iterator_foreach(it, h) { if (h->cpus && !strcmp(arch, h->name)) { char *c = rz_str_dup(h->cpus); int i, n = rz_str_split(c, ','); @@ -394,6 +395,7 @@ static void update_asmcpu_options(RzCore *core, RzConfigNode *node) { free(c); } } + rz_iterator_free(it); } static bool cb_asmcpu(void *user, void *data) { @@ -425,13 +427,14 @@ static bool cb_asmcpu(void *user, void *data) { static void update_asmarch_options(RzCore *core, RzConfigNode *node) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *it = ht_sp_as_iter(core->rasm->plugins); if (core && node && core->rasm) { rz_list_purge(node->options); - rz_list_foreach (core->rasm->plugins, iter, h) { + rz_iterator_foreach(it, h) { SETOPTIONS(node, h->name, NULL); } } + rz_iterator_free(it); } static void update_asmbits_options(RzCore *core, RzConfigNode *node) { diff --git a/librz/core/ccrypto.c b/librz/core/ccrypto.c index d31ff60515b..5593afdb367 100644 --- a/librz/core/ccrypto.c +++ b/librz/core/ccrypto.c @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only #include +#include static RzCmdStatus core_crypto_plugin_print(RzCmdStateOutput *state, const RzCryptoPlugin *plugin) { PJ *pj = state->d.pj; @@ -36,17 +37,18 @@ RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput const RzCryptoPlugin *plugin = NULL; RzCmdStatus status; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(cry->plugins); rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_list_foreach (cry->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { status = core_crypto_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { return status; } } + rz_iterator_free(it); if (state->mode == RZ_OUTPUT_MODE_QUIET) { rz_cons_newline(); } diff --git a/librz/core/cdebug.c b/librz/core/cdebug.c index 8bf9f4c6042..f787602f462 100644 --- a/librz/core/cdebug.c +++ b/librz/core/cdebug.c @@ -325,14 +325,14 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s memset(spaces, ' ', 15); spaces[15] = 0; RzDebug *dbg = core->dbg; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(dbg->plugins); RzDebugPlugin *plugin; RzCmdStatus status; if (!dbg) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_list_foreach (dbg->plugins, iter, plugin) { + rz_iterator_foreach(iter, plugin) { int sp = 8 - strlen(plugin->name); spaces[sp] = 0; status = rz_core_debug_plugin_print(dbg, plugin, state, count, spaces); @@ -342,6 +342,7 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s spaces[sp] = ' '; count++; } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/chash.c b/librz/core/chash.c index 85f9bfb8c51..afe3e70d1a6 100644 --- a/librz/core/chash.c +++ b/librz/core/chash.c @@ -36,17 +36,19 @@ RZ_API RzCmdStatus rz_core_hash_plugins_print(RzHash *hash, RzCmdStateOutput *st const RzHashPlugin *plugin = NULL; RzCmdStatus status; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(hash->plugins); rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_list_foreach (hash->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { status = rz_core_hash_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(it); return status; } } + rz_iterator_free(it); if (state->mode == RZ_OUTPUT_MODE_QUIET) { rz_cons_newline(); } diff --git a/librz/core/cio.c b/librz/core/cio.c index bd12350beee..03f810f345a 100644 --- a/librz/core/cio.c +++ b/librz/core/cio.c @@ -480,15 +480,16 @@ RZ_API RzCmdStatus rz_core_io_plugin_print(RzIOPlugin *plugin, RzCmdStateOutput */ RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state) { RzIOPlugin *plugin; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(io->plugins); if (!io) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); rz_cmd_state_output_set_columnsf(state, "sssss", "perm", "license", "name", "uri", "description"); - rz_list_foreach (io->plugins, iter, plugin) { + rz_iterator_foreach(iter, plugin) { rz_core_io_plugin_print(plugin, state); } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cmd/cmd_egg.c b/librz/core/cmd/cmd_egg.c index 3b2851a6213..1cb8da78578 100644 --- a/librz/core/cmd/cmd_egg.c +++ b/librz/core/cmd/cmd_egg.c @@ -205,12 +205,13 @@ RZ_IPI RzCmdStatus rz_egg_list_plugins_handler(RzCore *core, int argc, const cha if (!egg) { return RZ_CMD_STATUS_ERROR; } - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzEggPlugin *p; - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { rz_cons_printf("%s %6s : %s\n", (p->type == RZ_EGG_PLUGIN_SHELLCODE) ? "shc" : "enc", p->name, p->desc); } + rz_iterator_free(iter); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/core.c b/librz/core/core.c index 4b2da914e78..987a9a47cf7 100644 --- a/librz/core/core.c +++ b/librz/core/core.c @@ -2460,14 +2460,14 @@ RZ_API RzCmdStatus rz_core_core_plugin_print(RzCorePlugin *cp, RzCmdStateOutput } RZ_API RzCmdStatus rz_core_core_plugins_print(RzCore *core, RzCmdStateOutput *state) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(core->plugins); RzCorePlugin *cp; RzCmdStatus status; if (!core) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_list_foreach (core->plugins, iter, cp) { + rz_iterator_foreach(iter, cp) { const char *license = cp->license ? cp->license : "???"; @@ -2476,6 +2476,7 @@ RZ_API RzCmdStatus rz_core_core_plugins_print(RzCore *core, RzCmdStateOutput *st return status; } } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cplugin.c b/librz/core/cplugin.c index 45c2e292398..779f0d87d5f 100644 --- a/librz/core/cplugin.c +++ b/librz/core/cplugin.c @@ -13,14 +13,15 @@ static RzCorePlugin *core_static_plugins[] = { RZ_CORE_STATIC_PLUGINS }; RZ_API bool rz_core_plugin_fini(RzCore *core) { rz_return_val_if_fail(core->plugins, false); - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(core->plugins); RzCorePlugin *plugin; - rz_list_foreach (core->plugins, iter, plugin) { + rz_iterator_foreach(iter, plugin) { if (plugin->fini) { plugin->fini(core); } } - rz_list_free(core->plugins); + rz_iterator_free(iter); + ht_sp_free(core->plugins); ht_sp_free(core->plugin_configs); core->plugins = NULL; return true; @@ -30,9 +31,9 @@ RZ_API bool rz_core_plugin_add(RzCore *core, RZ_NONNULL RzCorePlugin *plugin) { rz_return_val_if_fail(core, false); rz_return_val_if_fail(plugin && plugin->init && plugin->name && plugin->author && plugin->license, false); // TODO: Add config from core plugin. - RZ_PLUGIN_CHECK_AND_ADD(core->plugins, plugin, RzCorePlugin); + ht_sp_insert(core->plugins, plugin->name, plugin); if (!plugin->init(core)) { - RZ_PLUGIN_REMOVE(core->plugins, plugin); + ht_sp_delete(core->plugins, plugin->name); return false; } return true; @@ -44,13 +45,13 @@ RZ_API bool rz_core_plugin_del(RzCore *core, RZ_NONNULL RzCorePlugin *plugin) { if (plugin->fini && !plugin->fini(core)) { return false; } - return rz_list_delete_data(core->plugins, plugin); + return ht_sp_delete(core->plugins, plugin->name); } RZ_API bool rz_core_plugin_init(RzCore *core) { int i; bool res = true; - core->plugins = rz_list_new(); + core->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(core_static_plugins); i++) { if (!rz_core_plugin_add(core, core_static_plugins[i])) { RZ_LOG_ERROR("core: error loading core plugin '%s'\n", core_static_plugins[i]->name); diff --git a/librz/crypto/crypto.c b/librz/crypto/crypto.c index 1e42e9646ce..84d412467ca 100644 --- a/librz/crypto/crypto.c +++ b/librz/crypto/crypto.c @@ -10,8 +10,6 @@ RZ_LIB_VERSION(rz_crypto); -static RzCryptoPlugin *crypto_static_plugins[] = { RZ_CRYPTO_STATIC_PLUGINS }; - static const struct { const char *name; RzCryptoSelector bit; @@ -66,22 +64,23 @@ RZ_API RZ_BORROW const char *rz_crypto_codec_name(const RzCryptoSelector bit) { RZ_API RZ_BORROW const RzCryptoPlugin *rz_crypto_plugin_by_index(RZ_NONNULL RzCrypto *cry, size_t index) { rz_return_val_if_fail(cry, NULL); - RzListIter *it; + RzIterator *it = ht_sp_as_iter(cry->plugins); const RzCryptoPlugin *plugin; size_t i = 0; - rz_list_foreach (cry->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (i == index) { return plugin; } i++; } + rz_iterator_free(it); return NULL; } RZ_API bool rz_crypto_plugin_add(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugin) { rz_return_val_if_fail(cry && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(cry->plugins, plugin, RzCryptoPlugin); + ht_sp_insert(cry->plugins, plugin->name, plugin); return true; } @@ -91,7 +90,7 @@ RZ_API bool rz_crypto_plugin_del(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPl cry->h->fini(cry); cry->h = NULL; } - rz_list_delete_data(cry->plugins, plugin); + ht_sp_delete(cry->plugins, plugin->name); return true; } @@ -107,7 +106,10 @@ RZ_API RZ_OWN RzCrypto *rz_crypto_new(void) { goto rz_crypto_new_bad; } - cry->plugins = rz_list_new_from_array((const void **)crypto_static_plugins, RZ_ARRAY_SIZE(crypto_static_plugins)); + cry->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); + for (size_t i = 0; i < RZ_ARRAY_SIZE(crypto_static_plugins); ++i) { + ht_sp_insert(cry->plugins, crypto_static_plugins[i]->name, crypto_static_plugins[i]); + } if (!cry->plugins) { goto rz_crypto_new_bad; } @@ -126,7 +128,7 @@ RZ_API void rz_crypto_free(RZ_NULLABLE RzCrypto *cry) { if (cry->h && cry->h->fini && !cry->h->fini(cry)) { RZ_LOG_ERROR("[!] crypto: error terminating '%s' plugin\n", cry->h->name); } - rz_list_free(cry->plugins); + ht_sp_free(cry->plugins); free(cry->output); free(cry->key); free(cry->iv); @@ -155,12 +157,12 @@ RZ_API void rz_crypto_reset(RZ_NONNULL RzCrypto *cry) { RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo) { rz_return_val_if_fail(cry && algo, false); - RzListIter *iter; + RzIterator *it = ht_sp_as_iter(cry->plugins); RzCryptoPlugin *h; if (cry->h && cry->h->fini && !cry->h->fini(cry)) { RZ_LOG_ERROR("[!] crypto: error terminating '%s' plugin\n", cry->h->name); } - rz_list_foreach (cry->plugins, iter, h) { + rz_iterator_foreach(it, h) { rz_warn_if_fail(h && h->use); if (h && h->use(algo)) { if (h->init && !h->init(cry)) { @@ -172,6 +174,7 @@ RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo) return true; } } + rz_iterator_free(it); return false; } diff --git a/librz/debug/debug.c b/librz/debug/debug.c index 01f791802cb..d1fc5dca85b 100644 --- a/librz/debug/debug.c +++ b/librz/debug/debug.c @@ -8,7 +8,6 @@ #include #include #include -#include RZ_LIB_VERSION(rz_debug); @@ -435,7 +434,7 @@ RZ_API RzDebug *rz_debug_free(RzDebug *dbg) { sdb_free(dbg->sgnls); rz_tree_free(dbg->tree); ht_up_free(dbg->tracenodes); - rz_list_free(dbg->plugins); + ht_sp_free(dbg->plugins); rz_list_free(dbg->call_frames); free(dbg->btalgo); rz_debug_trace_free(dbg->trace); diff --git a/librz/debug/plugin.c b/librz/debug/plugin.c index 4bb8428e2f5..15b19a3c1ee 100644 --- a/librz/debug/plugin.c +++ b/librz/debug/plugin.c @@ -9,25 +9,19 @@ static RzDebugPlugin *debug_static_plugins[] = { RZ_DEBUG_STATIC_PLUGINS }; RZ_API void rz_debug_plugin_init(RzDebug *dbg) { int i; - dbg->plugins = rz_list_new(); + dbg->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(debug_static_plugins); i++) { rz_debug_plugin_add(dbg, debug_static_plugins[i]); } } -RZ_API bool rz_debug_use(RzDebug *dbg, const char *str) { +RZ_API bool rz_debug_use(RzDebug *dbg, const char *name) { rz_return_val_if_fail(dbg, false); RzDebugPlugin *new_plugin = NULL; - if (str) { - RzDebugPlugin *h; - RzListIter *iter; - rz_list_foreach (dbg->plugins, iter, h) { - if (h->name && !strcmp(str, h->name)) { - new_plugin = h; - break; - } - } - if (!new_plugin) { + if (name) { + bool found = false; + new_plugin = ht_sp_find(dbg->plugins, name, &found); + if (!found) { return false; } } @@ -58,7 +52,7 @@ RZ_API bool rz_debug_use(RzDebug *dbg, const char *str) { RZ_API bool rz_debug_plugin_add(RzDebug *dbg, RZ_NONNULL RzDebugPlugin *plugin) { rz_return_val_if_fail(dbg && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(dbg->plugins, plugin, RzDebugPlugin); + ht_sp_insert(dbg->plugins, plugin->name, plugin); return true; } @@ -69,7 +63,7 @@ RZ_API bool rz_debug_plugin_del(RzDebug *dbg, RZ_NONNULL RzDebugPlugin *plugin) dbg->cur = NULL; dbg->plugin_data = NULL; } - return rz_list_delete_data(dbg->plugins, plugin); + return ht_sp_delete(dbg->plugins, plugin->name); } RZ_API bool rz_debug_plugin_set_reg_profile(RzDebug *dbg, const char *profile) { diff --git a/librz/egg/egg.c b/librz/egg/egg.c index 65520aa944c..64e90107269 100644 --- a/librz/egg/egg.c +++ b/librz/egg/egg.c @@ -79,7 +79,7 @@ RZ_API RzEgg *rz_egg_new(void) { if (!egg->patches) { goto beach; } - egg->plugins = rz_list_new(); + egg->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(egg_static_plugins); i++) { rz_egg_plugin_add(egg, egg_static_plugins[i]); } @@ -92,13 +92,13 @@ RZ_API RzEgg *rz_egg_new(void) { RZ_API bool rz_egg_plugin_add(RzEgg *a, RZ_NONNULL RzEggPlugin *plugin) { rz_return_val_if_fail(a && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(a->plugins, plugin, RzEggPlugin); + ht_sp_insert(a->plugins, plugin->name, plugin); return true; } RZ_API bool rz_egg_plugin_del(RzEgg *a, RZ_NONNULL RzEggPlugin *plugin) { rz_return_val_if_fail(a && plugin, false); - return rz_list_delete_data(a->plugins, plugin); + return ht_sp_delete(a->plugins, plugin->name); } RZ_API char *rz_egg_to_string(RzEgg *egg) { @@ -115,7 +115,7 @@ RZ_API void rz_egg_free(RzEgg *egg) { rz_asm_free(egg->rasm); rz_syscall_free(egg->syscall); sdb_free(egg->db); - rz_list_free(egg->plugins); + ht_sp_free(egg->plugins); rz_list_free(egg->patches); rz_egg_lang_free(egg); free(egg); @@ -533,9 +533,9 @@ RZ_API char *rz_egg_option_get(RzEgg *egg, const char *key) { RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { RzEggPlugin *p; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzBuffer *b; - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_SHELLCODE && !strcmp(name, p->name)) { b = p->build(egg); if (!b) { @@ -548,14 +548,15 @@ RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { return true; } } + rz_iterator_free(iter); return false; } RZ_API int rz_egg_encode(RzEgg *egg, const char *name) { RzEggPlugin *p; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzBuffer *b; - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_ENCODER && !strcmp(name, p->name)) { b = p->build(egg); if (!b) { @@ -566,6 +567,7 @@ RZ_API int rz_egg_encode(RzEgg *egg, const char *name) { return true; } } + rz_iterator_free(iter); return false; } diff --git a/librz/hash/hash.c b/librz/hash/hash.c index 4fb30662cd7..36cdeff8c79 100644 --- a/librz/hash/hash.c +++ b/librz/hash/hash.c @@ -215,32 +215,13 @@ static HashCfgConfig *hash_cfg_config_new(const RzHashPlugin *plugin) { return mdc; } -RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_index(RZ_NONNULL RzHash *rh, size_t index) { - rz_return_val_if_fail(rh, NULL); - - RzListIter *it; - const RzHashPlugin *rhp; - size_t i = 0; - - rz_list_foreach (rh->plugins, it, rhp) { - if (i == index) { - return rhp; - } - i++; - } - return NULL; -} - RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_name(RZ_NONNULL RzHash *rh, RZ_NONNULL const char *name) { rz_return_val_if_fail(name && rh, NULL); - RzListIter *it; - const RzHashPlugin *rhp; - - rz_list_foreach (rh->plugins, it, rhp) { - if (!strcmp(rhp->name, name)) { - return rhp; - } + bool found = false; + const RzHashPlugin *rhp = ht_sp_find(rh->plugins, name, &found); + if (found) { + return rhp; } return NULL; } @@ -326,10 +307,10 @@ RZ_API bool rz_hash_cfg_configure(RZ_NONNULL RzHashCfg *md, RZ_NONNULL const cha } HashCfgConfig *mdc = NULL; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(md->hash->plugins); const RzHashPlugin *plugin; - rz_list_foreach (md->hash->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (is_all || !strcmp(plugin->name, name)) { mdc = hash_cfg_config_new(plugin); if (!mdc) { @@ -347,6 +328,7 @@ RZ_API bool rz_hash_cfg_configure(RZ_NONNULL RzHashCfg *md, RZ_NONNULL const cha } } } + rz_iterator_free(it); if (is_all) { return true; @@ -708,7 +690,7 @@ RZ_API RzHash *rz_hash_new(void) { #if REQUIRE_OPENSSL_PROVIDER rz_hash_init_openssl_lib(); #endif /* REQUIRE_OPENSSL_PROVIDER */ - rh->plugins = rz_list_new(); + rh->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (int i = 0; i < RZ_ARRAY_SIZE(hash_static_plugins); i++) { rz_hash_plugin_add(rh, hash_static_plugins[i]); } @@ -719,7 +701,7 @@ RZ_API void rz_hash_free(RZ_NULLABLE RzHash *rh) { if (!rh) { return; } - rz_list_free(rh->plugins); + ht_sp_free(rh->plugins); free(rh); #if REQUIRE_OPENSSL_PROVIDER rz_hash_fini_openssl_lib(); @@ -732,11 +714,11 @@ RZ_API void rz_hash_free(RZ_NULLABLE RzHash *rh) { */ RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN RzHashPlugin *plugin) { rz_return_val_if_fail(rh && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(rh->plugins, plugin, RzHashPlugin); + ht_sp_insert(rh->plugins, plugin->name, plugin); return true; } RZ_API bool rz_hash_plugin_del(RZ_NONNULL RzHash *rh, RZ_NONNULL RzHashPlugin *plugin) { rz_return_val_if_fail(rh && plugin, false); - return rz_list_delete_data(rh->plugins, plugin); + return ht_sp_delete(rh->plugins, plugin->name); } diff --git a/librz/include/rz_analysis.h b/librz/include/rz_analysis.h index 8a847e05b2e..6a35627cfab 100644 --- a/librz/include/rz_analysis.h +++ b/librz/include/rz_analysis.h @@ -497,7 +497,7 @@ typedef struct rz_analysis_t { RzAnalysisILVM *il_vm; ///< user-faced VM, NEVER use this for any analysis passes! struct rz_analysis_plugin_t *cur; RzAnalysisRange *limit; // analysis.from, analysis.to - RzList /**/ *plugins; + HtSP /**/ *plugins; Sdb *sdb_noret; Sdb *sdb_fmts; HtUP *ht_xrefs_from; diff --git a/librz/include/rz_asm.h b/librz/include/rz_asm.h index d086fa36101..d0fab182895 100644 --- a/librz/include/rz_asm.h +++ b/librz/include/rz_asm.h @@ -108,7 +108,7 @@ typedef struct rz_asm_t { ut64 pc; _RzAsmPlugin *cur; _RzAsmPlugin *acur; - RzList /**/ *plugins; + HtSP /**/ *plugins; RzBinBind binb; RzParse *ifilter; RzParse *ofilter; @@ -180,7 +180,7 @@ RZ_API ut8 *rz_asm_from_string(RzAsm *a, ut64 addr, const char *b, int *l); RZ_API int rz_asm_sub_names_input(RzAsm *a, const char *f); RZ_API int rz_asm_sub_names_output(RzAsm *a, const char *f); RZ_API char *rz_asm_describe(RzAsm *a, const char *str); -RZ_API RzList /**/ *rz_asm_get_plugins(RzAsm *a); +RZ_API RZ_BORROW HtSP /**/ *rz_asm_get_plugins(RZ_BORROW RZ_NONNULL RzAsm *a); RZ_API void rz_asm_list_directives(void); /* code.c */ diff --git a/librz/include/rz_bin.h b/librz/include/rz_bin.h index 592981a30fa..829dcce4a2f 100644 --- a/librz/include/rz_bin.h +++ b/librz/include/rz_bin.h @@ -367,8 +367,8 @@ struct rz_bin_t { int rawstr; RZ_DEPRECATE Sdb *sdb; RzIDStorage *ids; - RzList /**/ *plugins; - RzList /**/ *binxtrs; + HtSP /**/ *plugins; + HtSP /**/ *binxtrs; RzList /**/ *binfiles; PrintfCallback cb_printf; int loadany; diff --git a/librz/include/rz_bp.h b/librz/include/rz_bp.h index 912102c1eba..3d2fff72c21 100644 --- a/librz/include/rz_bp.h +++ b/librz/include/rz_bp.h @@ -84,7 +84,7 @@ typedef struct rz_bp_t { RzIOBind iob; // compile time dependency RzBreakpointPlugin *cur; RzList /**/ *traces; // XXX - RzList /**/ *plugins; + HtSP /**/ *plugins; PrintfCallback cb_printf; RzBreakpointCallback breakpoint; /* storage of breakpoints */ diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 3469f19dd8a..20796b8131a 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -252,7 +252,7 @@ struct rz_core_t { // NOTE: Do not change the order of fields above! // They are used in pointer passing hacks in rz_types.h. RzIO *io; - RzList /**/ *plugins; ///< List of registered core plugins + HtSP /**/ *plugins; ///< List of registered core plugins RzConfig *config; HtSP /*: RzConfig>*/ *plugin_configs; ///< Pointers to plugin configurations. Indexed by "plugins." ut64 offset; // current seek diff --git a/librz/include/rz_crypto.h b/librz/include/rz_crypto.h index 6c08a57e8d8..ee33e68cbc1 100644 --- a/librz/include/rz_crypto.h +++ b/librz/include/rz_crypto.h @@ -1,9 +1,9 @@ #ifndef RZ_CRYPTO_H #define RZ_CRYPTO_H -#include "rz_types.h" -#include "rz_list.h" -#include "rz_crypto/rz_des.h" +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -34,7 +34,7 @@ typedef struct rz_crypto_t { int output_size; int dir; void *user; - RzList /**/ *plugins; + HtSP /**/ *plugins; } RzCrypto; typedef struct rz_crypto_plugin_t { diff --git a/librz/include/rz_debug.h b/librz/include/rz_debug.h index b0df51a6353..ebc464aaca3 100644 --- a/librz/include/rz_debug.h +++ b/librz/include/rz_debug.h @@ -292,7 +292,7 @@ typedef struct rz_debug_t { struct rz_debug_plugin_t *cur; void *plugin_data; - RzList /**/ *plugins; + HtSP /**/ *plugins; bool pc_at_bp; /* after a breakpoint, is the pc at the bp? */ bool pc_at_bp_set; /* is the pc_at_bp variable set already? */ diff --git a/librz/include/rz_egg.h b/librz/include/rz_egg.h index 8a9a0098c64..bfd5a89e915 100644 --- a/librz/include/rz_egg.h +++ b/librz/include/rz_egg.h @@ -99,7 +99,7 @@ typedef struct rz_egg_t { RzSyscall *syscall; RzEggLang lang; Sdb *db; - RzList /**/ *plugins; + HtSP /**/ *plugins; RzList /**/ *patches; // struct rz_egg_emit_t *remit; int arch; diff --git a/librz/include/rz_hash.h b/librz/include/rz_hash.h index 83f280a7c70..bab1b12fd50 100644 --- a/librz/include/rz_hash.h +++ b/librz/include/rz_hash.h @@ -6,6 +6,7 @@ #include #include +#include #include #ifdef __cplusplus @@ -39,7 +40,7 @@ typedef struct rz_hash_plugin_t { } RzHashPlugin; typedef struct rz_hash_t { - RzList /**/ *plugins; + HtSP /**/ *plugins; } RzHash; typedef struct rz_hash_cfg_t { @@ -54,7 +55,6 @@ RZ_API RzHash *rz_hash_new(void); RZ_API void rz_hash_free(RZ_NULLABLE RzHash *rh); RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN RzHashPlugin *plugin); RZ_API bool rz_hash_plugin_del(RZ_NONNULL RzHash *rh, RZ_NONNULL RzHashPlugin *plugin); -RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_index(RZ_NONNULL RzHash *rh, size_t index); RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_name(RZ_NONNULL RzHash *rh, RZ_NONNULL const char *name); RZ_API RZ_OWN RzHashCfg *rz_hash_cfg_new(RZ_NONNULL RzHash *rh); diff --git a/librz/include/rz_io.h b/librz/include/rz_io.h index 51c3de97295..f27d570d39c 100644 --- a/librz/include/rz_io.h +++ b/librz/include/rz_io.h @@ -77,7 +77,7 @@ typedef struct rz_io_t { RzSkyline cache_skyline; ut8 *write_mask; int write_mask_len; - RzList /**/ *plugins; + HtSP /**/ *plugins; char *runprofile; char *envprofile; #if USE_PTRACE_WRAP diff --git a/librz/include/rz_lib.h b/librz/include/rz_lib.h index 295edf4923f..f652ba80a4a 100644 --- a/librz/include/rz_lib.h +++ b/librz/include/rz_lib.h @@ -106,23 +106,6 @@ typedef struct rz_lib_t { HtSU *opened_dirs; ///< Hashtable to keep track of already opened directories } RzLib; -#define RZ_PLUGIN_CHECK_AND_ADD(plugins, plugin, py_type) \ - do { \ - RzListIter *_it; \ - py_type *_p; \ - rz_list_foreach ((plugins), _it, _p) { \ - if (!strcmp(_p->name, (plugin)->name)) { \ - return false; \ - } \ - } \ - rz_list_append(plugins, plugin); \ - } while (0) - -#define RZ_PLUGIN_REMOVE(plugins, plugin) \ - do { \ - rz_list_delete_data(plugins, plugin); \ - } while (0) - #ifdef RZ_API RZ_API RzLib *rz_lib_new(RZ_NULLABLE const char *symname, RZ_NULLABLE const char *symnamefunc); RZ_API void rz_lib_free(RzLib *lib); diff --git a/librz/io/io.c b/librz/io/io.c index 13ee108f2f6..bfde065ebf2 100644 --- a/librz/io/io.c +++ b/librz/io/io.c @@ -733,7 +733,7 @@ RZ_API int rz_io_fini(RzIO *io) { rz_io_desc_cache_fini_all(io); rz_io_desc_fini(io); rz_io_map_fini(io); - rz_list_free(io->plugins); + ht_sp_free(io->plugins); rz_io_cache_fini(io); if (io->runprofile) { RZ_FREE(io->runprofile); diff --git a/librz/io/io_plugin.c b/librz/io/io_plugin.c index ab45d270828..9ac77cf4056 100644 --- a/librz/io/io_plugin.c +++ b/librz/io/io_plugin.c @@ -6,6 +6,8 @@ #include #include "rz_io_plugins.h" +#include "rz_util/ht_sp.h" +#include "rz_util/rz_iterator.h" static volatile RzIOPlugin *default_plugin = NULL; @@ -13,7 +15,7 @@ static RzIOPlugin *io_static_plugins[] = { RZ_IO_STATIC_PLUGINS }; RZ_API bool rz_io_plugin_add(RzIO *io, RZ_NONNULL RZ_BORROW RzIOPlugin *plugin) { rz_return_val_if_fail(io && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(io->plugins, plugin, RzIOPlugin); + ht_sp_insert(io->plugins, plugin->name, plugin); return true; } @@ -29,7 +31,7 @@ static bool close_if_plugin(void *user, void *data, ut32 id) { RZ_API bool rz_io_plugin_del(RzIO *io, RZ_NONNULL RZ_BORROW RzIOPlugin *plugin) { rz_return_val_if_fail(io && plugin, false); rz_id_storage_foreach(io->files, close_if_plugin, plugin); - return rz_list_delete_data(io->plugins, plugin); + return ht_sp_delete(io->plugins, plugin->name); } RZ_API bool rz_io_plugin_init(RzIO *io) { @@ -37,7 +39,7 @@ RZ_API bool rz_io_plugin_init(RzIO *io) { if (!io) { return false; } - io->plugins = rz_list_new(); + io->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(io_static_plugins); i++) { if (!io_static_plugins[i]->name) { continue; @@ -55,9 +57,9 @@ RZ_API RzIOPlugin *rz_io_plugin_get_default(RzIO *io, const char *filename, bool } RZ_API RzIOPlugin *rz_io_plugin_resolve(RzIO *io, const char *filename, bool many) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(io->plugins); RzIOPlugin *ret; - rz_list_foreach (io->plugins, iter, ret) { + rz_iterator_foreach(iter, ret) { if (!ret || !ret->check) { continue; } @@ -65,17 +67,19 @@ RZ_API RzIOPlugin *rz_io_plugin_resolve(RzIO *io, const char *filename, bool man return ret; } } + rz_iterator_free(iter); return rz_io_plugin_get_default(io, filename, many); } RZ_API RzIOPlugin *rz_io_plugin_byname(RzIO *io, const char *name) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(io->plugins); RzIOPlugin *iop; - rz_list_foreach (io->plugins, iter, iop) { + rz_iterator_foreach(iter, iop) { if (!strcmp(name, iop->name)) { return iop; } } + rz_iterator_free(iter); return rz_io_plugin_get_default(io, name, false); } diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index b2181655134..156a630b12b 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2011-2020 pancake // SPDX-License-Identifier: LGPL-3.0-only +#include "rz_util/rz_iterator.h" #include #include #include @@ -68,20 +69,23 @@ static int usage(int v) { } static void list(RzEgg *egg) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzEggPlugin *p; printf("shellcodes:\n"); - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_SHELLCODE) { printf("%10s : %s\n", p->name, p->desc); } } + rz_iterator_free(iter); + iter = ht_sp_as_iter(egg->plugins); printf("encoders:\n"); - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_ENCODER) { printf("%10s : %s\n", p->name, p->desc); } } + rz_iterator_free(iter); } static bool create(const char *format, const char *arch, int bits, const ut8 *code, int codelen) { diff --git a/librz/main/rz-hash.c b/librz/main/rz-hash.c index 8d9908fa56a..d8a6282a156 100644 --- a/librz/main/rz-hash.c +++ b/librz/main/rz-hash.c @@ -131,10 +131,12 @@ static void rz_hash_show_algorithms(RzHashContext *ctx) { printf("flags algorithm license author\n"); const RzHashPlugin *rmdp; - for (size_t i = 0; (rmdp = rz_hash_plugin_by_index(ctx->rh, i)); ++i) { + RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); + rz_iterator_foreach(it, rmdp) { snprintf(flags, sizeof(flags), "____h%c", rmdp->support_hmac ? 'm' : '_'); printf("%6s %-14s %-10s %s\n", flags, rmdp->name, rmdp->license, rmdp->author); } + rz_iterator_free(it); const RzCryptoPlugin *rcp; for (size_t i = 0; (rcp = rz_crypto_plugin_by_index(ctx->rc, i)); i++) { @@ -809,9 +811,11 @@ static RzList /**/ *parse_hash_algorithms(RzHashContext *ctx) { if (!list) { return NULL; } - for (ut64 i = 0; (plugin = rz_hash_plugin_by_index(ctx->rh, i)); ++i) { + RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); + rz_iterator_foreach(it, plugin) { rz_list_append(list, (void *)plugin->name); } + rz_iterator_free(it); return list; } return rz_str_split_list(ctx->algorithm, ",", 0); diff --git a/librz/util/iterator.c b/librz/util/iterator.c index 88e78e6357b..d562b48a416 100644 --- a/librz/util/iterator.c +++ b/librz/util/iterator.c @@ -75,4 +75,4 @@ RZ_API void rz_iterator_free(RzIterator *it) { it->free_u(it->u); } free(it); -} \ No newline at end of file +}