From 81a1a20e534d4a06c2cd520fea64a738ea762b75 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Wed, 23 Oct 2024 12:46:51 -0500 Subject: [PATCH] Sort plugins alphabetically when listed. --- librz/bp/bp_plugin.c | 9 +- librz/core/casm.c | 13 +- librz/core/cbin.c | 23 +- librz/core/ccrypto.c | 16 +- librz/core/cdebug.c | 13 +- librz/core/cio.c | 12 +- librz/include/rz_asm.h | 14 + librz/include/rz_bin.h | 28 ++ librz/include/rz_bp.h | 14 + librz/include/rz_crypto.h | 15 ++ librz/include/rz_debug.h | 17 ++ librz/include/rz_egg.h | 14 + librz/include/rz_hash.h | 15 ++ librz/include/rz_io.h | 14 + librz/main/rz-gg.c | 17 +- librz/main/rz-hash.c | 45 ++-- test/db/archos/linux-x64/cmd_list | 30 +-- test/db/cmd/cmd_list | 428 +++++++++++++++--------------- test/db/tools/rz_hash | 236 ++++++++-------- 19 files changed, 571 insertions(+), 402 deletions(-) diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index 62e3109ebe6..95141b8cea4 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -64,12 +64,15 @@ RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { // TODO: deprecate RZ_API void rz_bp_plugin_list(RzBreakpoint *bp) { RzIterator *iter = ht_sp_as_iter(bp->plugins); - RzBreakpointPlugin **val; - rz_iterator_foreach(iter, val) { - RzBreakpointPlugin *b = *val; + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_breakpoint_plugin_cmp, NULL); + RzListIter *it; + RzBreakpointPlugin *b; + rz_list_foreach (plugin_list, it, b) { bp->cb_printf("bp %c %s\n", (bp->cur && !strcmp(bp->cur->name, b->name)) ? '*' : '-', b->name); } + rz_list_free(plugin_list); rz_iterator_free(iter); } diff --git a/librz/core/casm.c b/librz/core/casm.c index 864913f3c1f..a108576342d 100644 --- a/librz/core/casm.c +++ b/librz/core/casm.c @@ -169,11 +169,13 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC int i; RzAsm *a = core->rasm; RzIterator *iter = ht_sp_as_iter(a->plugins); - RzAsmPlugin **val; + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_asm_plugin_cmp, NULL); + RzListIter *it; + RzAsmPlugin *ap; RzCmdStatus status; if (arch) { - rz_iterator_foreach(iter, val) { - RzAsmPlugin *ap = *val; + rz_list_foreach (plugin_list, it, ap) { if (ap->cpus && !strcmp(arch, ap->name)) { char *c = rz_str_dup(ap->cpus); int n = rz_str_split(c, ','); @@ -186,19 +188,20 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC } } else { rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, val) { - RzAsmPlugin *ap = *val; + rz_list_foreach (plugin_list, it, 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); + rz_list_free(plugin_list); return status; } } rz_cmd_state_output_array_end(state); } + rz_list_free(plugin_list); rz_iterator_free(iter); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cbin.c b/librz/core/cbin.c index f1a9e74f5b0..98465ba4910 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -4904,29 +4904,38 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state rz_return_val_if_fail(bin && state, RZ_CMD_STATUS_ERROR); RzCmdStatus status; + rz_cmd_state_output_array_start(state); + RzIterator *iter = ht_sp_as_iter(bin->plugins); - RzBinPlugin **bp_val; - RzBinXtrPlugin **bx_val; + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_bin_plugin_cmp, NULL); + RzListIter *it; + RzBinPlugin *bp; + RzBinXtrPlugin *bx; - rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, bp_val) { - RzBinPlugin *bp = *bp_val; + rz_list_foreach (plugin_list, it, bp) { status = rz_core_bin_plugin_print(bp, state); if (status != RZ_CMD_STATUS_OK) { rz_iterator_free(iter); + rz_list_free(plugin_list); return status; } } + rz_list_free(plugin_list); rz_iterator_free(iter); + iter = ht_sp_as_iter(bin->binxtrs); - rz_iterator_foreach(iter, bx_val) { - RzBinXtrPlugin *bx = *bx_val; + plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_bin_xtr_plugin_cmp, NULL); + rz_list_foreach (plugin_list, it, bx) { status = rz_core_binxtr_plugin_print(bx, state); if (status != RZ_CMD_STATUS_OK) { rz_iterator_free(iter); + rz_list_free(plugin_list); return status; } } + rz_list_free(plugin_list); rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; diff --git a/librz/core/ccrypto.c b/librz/core/ccrypto.c index 80b7358016c..8fad02e20ff 100644 --- a/librz/core/ccrypto.c +++ b/librz/core/ccrypto.c @@ -36,20 +36,26 @@ RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput rz_return_val_if_fail(cry, RZ_CMD_STATUS_ERROR); RzCmdStatus status; - RzIterator *it = ht_sp_as_iter(cry->plugins); - RzCryptoPlugin **val; rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_iterator_foreach(it, val) { - const RzCryptoPlugin *plugin = *val; + RzIterator *iter = ht_sp_as_iter(cry->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_crypto_plugin_cmp, NULL); + RzListIter *it; + RzCryptoPlugin *plugin; + rz_list_foreach (plugin_list, it, plugin) { status = core_crypto_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { + rz_list_free(plugin_list); + rz_iterator_free(iter); return status; } } - rz_iterator_free(it); + rz_list_free(plugin_list); + rz_iterator_free(iter); + if (state->mode == RZ_OUTPUT_MODE_QUIET) { rz_cons_newline(); } diff --git a/librz/core/cdebug.c b/librz/core/cdebug.c index 9f02183e9f1..b159b3d2ada 100644 --- a/librz/core/cdebug.c +++ b/librz/core/cdebug.c @@ -325,25 +325,30 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s memset(spaces, ' ', 15); spaces[15] = 0; RzDebug *dbg = core->dbg; - RzIterator *iter = ht_sp_as_iter(dbg->plugins); - RzDebugPlugin **val; RzCmdStatus status; if (!dbg) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, val) { - RzDebugPlugin *plugin = *val; + RzIterator *iter = ht_sp_as_iter(dbg->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_debug_plugin_cmp, NULL); + RzListIter *it; + RzDebugPlugin *plugin; + rz_list_foreach (plugin_list, it, plugin) { int sp = 8 - strlen(plugin->name); spaces[sp] = 0; status = rz_core_debug_plugin_print(dbg, plugin, state, count, spaces); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(iter); + rz_list_free(plugin_list); return status; } spaces[sp] = ' '; count++; } rz_iterator_free(iter); + rz_list_free(plugin_list); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cio.c b/librz/core/cio.c index 4fc7cf85c48..e92b8dd3171 100644 --- a/librz/core/cio.c +++ b/librz/core/cio.c @@ -480,19 +480,23 @@ RZ_API RzCmdStatus rz_core_io_plugin_print(RzIOPlugin *plugin, RzCmdStateOutput */ RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state) { rz_return_val_if_fail(io && state, RZ_CMD_STATUS_ERROR); - RzIterator *iter = ht_sp_as_iter(io->plugins); - RzIOPlugin **val; 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_iterator_foreach(iter, val) { - RzIOPlugin *plugin = *val; + + RzIterator *iter = ht_sp_as_iter(io->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_io_plugin_cmp, NULL); + RzListIter *it; + RzIOPlugin *plugin; + rz_list_foreach (plugin_list, it, plugin) { rz_core_io_plugin_print(plugin, state); } rz_iterator_free(iter); + rz_list_free(plugin_list); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/include/rz_asm.h b/librz/include/rz_asm.h index b29860b4147..821c9d2c71b 100644 --- a/librz/include/rz_asm.h +++ b/librz/include/rz_asm.h @@ -149,6 +149,20 @@ typedef struct rz_asm_plugin_t { const char *platforms; } RzAsmPlugin; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_asm_plugin_cmp(RZ_NULLABLE const RzAsmPlugin *a, RZ_NULLABLE const RzAsmPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API /* asm.c */ RZ_API RzAsm *rz_asm_new(void); diff --git a/librz/include/rz_bin.h b/librz/include/rz_bin.h index 829dcce4a2f..da9b9da0aa7 100644 --- a/librz/include/rz_bin.h +++ b/librz/include/rz_bin.h @@ -770,6 +770,34 @@ typedef struct rz_bin_bind_t { ut32 visibility; } RzBinBind; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_bin_plugin_cmp(RZ_NULLABLE const RzBinPlugin *a, RZ_NULLABLE const RzBinPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_bin_xtr_plugin_cmp(RZ_NULLABLE const RzBinXtrPlugin *a, RZ_NULLABLE const RzBinXtrPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + RZ_API RzBinField *rz_bin_field_new(ut64 paddr, ut64 vaddr, int size, const char *name, const char *comment, const char *format, bool format_named); RZ_API void rz_bin_field_free(RZ_NULLABLE RzBinField *field); RZ_API RzBinClassField *rz_bin_class_field_new(ut64 vaddr, ut64 paddr, const char *name, const char *classname, const char *libname, const char *type); diff --git a/librz/include/rz_bp.h b/librz/include/rz_bp.h index 3d2fff72c21..25ce4fff2c3 100644 --- a/librz/include/rz_bp.h +++ b/librz/include/rz_bp.h @@ -106,6 +106,20 @@ typedef struct rz_bp_trace_t { int bitlen; } RzBreakpointTrace; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_breakpoint_plugin_cmp(RZ_NULLABLE const RzBreakpointPlugin *a, RZ_NULLABLE const RzBreakpointPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API RzBreakpoint *rz_bp_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *ctx); RZ_API RzBreakpoint *rz_bp_free(RzBreakpoint *bp); diff --git a/librz/include/rz_crypto.h b/librz/include/rz_crypto.h index ee33e68cbc1..d174c121145 100644 --- a/librz/include/rz_crypto.h +++ b/librz/include/rz_crypto.h @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -53,6 +54,20 @@ typedef struct rz_crypto_plugin_t { typedef ut64 RzCryptoSelector; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_crypto_plugin_cmp(RZ_NULLABLE const RzCryptoPlugin *a, RZ_NULLABLE const RzCryptoPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API bool rz_crypto_plugin_add(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h); RZ_API bool rz_crypto_plugin_del(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h); diff --git a/librz/include/rz_debug.h b/librz/include/rz_debug.h index ebc464aaca3..dc888ceb8ac 100644 --- a/librz/include/rz_debug.h +++ b/librz/include/rz_debug.h @@ -15,6 +15,8 @@ #include #include "rz_bind.h" +#include "rz_util/rz_assert.h" +#include "rz_util/rz_str.h" #ifdef __cplusplus extern "C" { #endif @@ -430,6 +432,21 @@ typedef struct rz_debug_esil_watchpoint_t { } RzDebugEsilWatchpoint; #ifdef RZ_API + +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_debug_plugin_cmp(RZ_NULLABLE const RzDebugPlugin *a, RZ_NULLABLE const RzDebugPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + RZ_API RZ_OWN RzDebug *rz_debug_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *bp_ctx); RZ_API RzDebug *rz_debug_free(RzDebug *dbg); diff --git a/librz/include/rz_egg.h b/librz/include/rz_egg.h index bfd5a89e915..e452040ed51 100644 --- a/librz/include/rz_egg.h +++ b/librz/include/rz_egg.h @@ -165,6 +165,20 @@ typedef struct rz_egg_emit_t { void (*get_while_end)(RzEgg *egg, char *out, const char *ctxpush, const char *label); } RzEggEmit; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_egg_plugin_cmp(RZ_NULLABLE const RzEggPlugin *a, RZ_NULLABLE const RzEggPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API RzEgg *rz_egg_new(void); RZ_API void rz_egg_lang_init(RzEgg *egg); diff --git a/librz/include/rz_hash.h b/librz/include/rz_hash.h index bab1b12fd50..eecb9ac8cd7 100644 --- a/librz/include/rz_hash.h +++ b/librz/include/rz_hash.h @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -49,6 +50,20 @@ typedef struct rz_hash_cfg_t { RzHash *hash; } RzHashCfg; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_hash_plugin_cmp(RZ_NULLABLE const RzHashPlugin *a, RZ_NULLABLE const RzHashPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API RzHash *rz_hash_new(void); diff --git a/librz/include/rz_io.h b/librz/include/rz_io.h index f27d570d39c..1f485ccaccd 100644 --- a/librz/include/rz_io.h +++ b/librz/include/rz_io.h @@ -270,6 +270,20 @@ typedef struct rz_io_bind_t { #endif } RzIOBind; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_io_plugin_cmp(RZ_NULLABLE const RzIOPlugin *a, RZ_NULLABLE const RzIOPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + // map.c RZ_API RzIOMap *rz_io_map_new(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size); RZ_API void rz_io_map_init(RzIO *io); diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index 759b51014c6..5db9beb04a9 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -69,24 +69,25 @@ static int usage(int v) { } static void list(RzEgg *egg) { - RzIterator *iter = ht_sp_as_iter(egg->plugins); - RzEggPlugin **val; printf("shellcodes:\n"); - rz_iterator_foreach(iter, val) { - RzEggPlugin *p = *val; + RzIterator *iter = ht_sp_as_iter(egg->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_egg_plugin_cmp, NULL); + RzListIter *it; + RzEggPlugin *p; + rz_list_foreach (plugin_list, it, 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_iterator_foreach(iter, val) { - RzEggPlugin *p = *val; + rz_list_foreach (plugin_list, it, p) { if (p->type == RZ_EGG_PLUGIN_ENCODER) { printf("%10s : %s\n", p->name, p->desc); } } + rz_list_free(plugin_list); rz_iterator_free(iter); } diff --git a/librz/main/rz-hash.c b/librz/main/rz-hash.c index 25c541f8fb1..1a5afd6529b 100644 --- a/librz/main/rz-hash.c +++ b/librz/main/rz-hash.c @@ -130,14 +130,17 @@ static void rz_hash_show_algorithms(RzHashContext *ctx) { printf("flags algorithm license author\n"); - RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); - const RzHashPlugin **val; - rz_iterator_foreach(it, val) { - const RzHashPlugin *rmdp = *val; + RzIterator *iter = ht_sp_as_iter(ctx->rh->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_hash_plugin_cmp, NULL); + RzListIter *it; + const RzHashPlugin *rmdp; + rz_list_foreach (plugin_list, 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); + rz_list_free(plugin_list); + rz_iterator_free(iter); const RzCryptoPlugin *rcp; for (size_t i = 0; (rcp = rz_crypto_plugin_by_index(ctx->rc, i)); i++) { @@ -807,21 +810,25 @@ static void hash_context_compare_hashes(RzHashContext *ctx, size_t filesize, boo } static RzList /**/ *parse_hash_algorithms(RzHashContext *ctx) { - if (!strcmp(ctx->algorithm, "all")) { - RzList *list = rz_list_newf(NULL); - if (!list) { - return NULL; - } - RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); - RzHashPlugin **val; - rz_iterator_foreach(it, val) { - const RzHashPlugin *plugin = *val; - rz_list_append(list, (void *)plugin->name); - } - rz_iterator_free(it); - return list; + if (strcmp(ctx->algorithm, "all")) { + return rz_str_split_list(ctx->algorithm, ",", 0); + } + + RzList *list = rz_list_newf(NULL); + if (!list) { + return NULL; + } + RzIterator *iter = ht_sp_as_iter(ctx->rh->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_hash_plugin_cmp, NULL); + RzListIter *it; + const RzHashPlugin *rmdp; + rz_list_foreach (plugin_list, it, rmdp) { + rz_list_append(list, (void *)rmdp->name); } - return rz_str_split_list(ctx->algorithm, ",", 0); + rz_list_free(plugin_list); + rz_iterator_free(iter); + return list; } static bool calculate_hash(RzHashContext *ctx, RzIO *io, const char *filename) { diff --git a/test/db/archos/linux-x64/cmd_list b/test/db/archos/linux-x64/cmd_list index c6e47457f8e..e61eae98817 100644 --- a/test/db/archos/linux-x64/cmd_list +++ b/test/db/archos/linux-x64/cmd_list @@ -4,16 +4,16 @@ NAME=Print the debug plugins FILE== CMDS=Ld EXPECT=<