Skip to content

Commit

Permalink
Refactor: Use HtSP for storing plugins.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Rot127 committed Oct 19, 2024
1 parent f2a6d2a commit 3f5675d
Show file tree
Hide file tree
Showing 38 changed files with 254 additions and 257 deletions.
13 changes: 7 additions & 6 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -189,15 +189,15 @@ 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;
}

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;
}

Expand All @@ -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;
}
Expand All @@ -234,6 +234,7 @@ RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) {
}
return true;
}
rz_iterator_free(it);
}
return false;
}
Expand Down
49 changes: 28 additions & 21 deletions librz/arch/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -439,15 +443,15 @@ 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;
}
RzCore *core = a->core;
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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -643,6 +648,7 @@ static Ase findAssembler(RzAsm *a, const char *kw) {
}
}
}
rz_iterator_free(iter);
return ase;
}

Expand Down Expand Up @@ -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 /*<RzAsmPlugin *>*/ *rz_asm_get_plugins(RzAsm *a) {
RZ_API RZ_BORROW HtSP /*<RzAsmPlugin *>*/ *rz_asm_get_plugins(RZ_BORROW RZ_NONNULL RzAsm *a) {
rz_return_val_if_fail(a, NULL);
return a->plugins;
}

Expand Down
Loading

0 comments on commit 3f5675d

Please sign in to comment.