Skip to content

Commit

Permalink
core/disasm: remove static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed Feb 12, 2024
1 parent 722de8b commit 1a63bc0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
24 changes: 13 additions & 11 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,9 @@ static void colordump(RzCore *core, const ut8 *block, int len) {
}
for (i = 0; i < len; i += cols) {
if (show_section) {
const char *name = rz_core_get_section_name(core, core->offset + i);
char *name = get_section_name(core, core->offset + i);
rz_cons_printf("%20s ", name ? name : "");
free(name);
}
if (show_offset) {
rz_print_addr(core->print, core->offset + i);
Expand Down Expand Up @@ -1403,10 +1404,11 @@ static void annotated_hexdump(RzCore *core, int len) {
append(ebytes, core->cons->context->pal.offset);
}
if (showSection) {
const char *name = rz_core_get_section_name(core, ea);
char *name = get_section_name(core, ea);
char *s = rz_str_newf("%20s ", name);
append(ebytes, s);
free(s);
free(name);
}
ebytes += sprintf(ebytes, "0x%08" PFMT64x, ea);
if (usecolor) {
Expand Down Expand Up @@ -2305,23 +2307,23 @@ static inline int cmd_pxb_k(const ut8 *buffer, int x) {
return buffer[3 - x] << (8 * x);
}

static inline char *get_section_name(RzCore *core) {
const char *csection = rz_core_get_section_name(core, core->offset);
static inline char *get_section_name(RzCore *core, ut64 offset) {
char *csection = rz_core_get_section_name(core, offset);
if (RZ_STR_ISEMPTY(csection)) {
free(csection);
return strdup("unknown");
}
csection = rz_str_trim_head_ro(csection);
char *section_name = strdup(csection);
rz_str_trim_tail(section_name);
if (RZ_STR_ISEMPTY(section_name)) {
free(section_name);
rz_str_trim_head(csection);
rz_str_trim_tail(csection);
if (RZ_STR_ISEMPTY(csection)) {
free(csection);
return strdup("unknown");
}
return section_name;
return csection;
}

static void print_json_string(RzCore *core, const ut8 *block, ut32 len, RzStrEnc encoding, bool stop_at_nil) {
char *section = get_section_name(core);
char *section = get_section_name(core, core->offset);
if (!section) {
return;
}
Expand Down
21 changes: 8 additions & 13 deletions librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,29 +425,23 @@ static void get_bits_comment(RzCore *core, RzAnalysisFunction *f, char *cmt, int
}
}

RZ_API const char *rz_core_get_section_name(RzCore *core, ut64 addr) {
static char section[128] = "";
static ut64 oaddr = UT64_MAX;
if (oaddr == addr) {
return section;
}
RZ_API RZ_OWN char *rz_core_get_section_name(RzCore *core, ut64 addr) {
char *section = NULL;
RzBinObject *bo = rz_bin_cur_object(core->bin);
RzBinSection *s = bo ? rz_bin_get_section_at(bo, addr, core->io->va) : NULL;
if (s && s->name && *s->name) {
snprintf(section, sizeof(section) - 1, "%10s ", s->name);
if (s && RZ_STR_ISNOTEMPTY(s->name)) {
return rz_str_dup(s->name);
} else {
RzListIter *iter;
RzDebugMap *map;
*section = 0;
rz_list_foreach (core->dbg->maps, iter, map) {
if (addr >= map->addr && addr < map->addr_end) {
const char *mn = rz_str_lchr(map->name, '/');
rz_str_ncpy(section, mn ? mn + 1 : map->name, sizeof(section));
section = rz_str_dup(mn ? mn + 1 : map->name);
break;
}
}
}
oaddr = addr;
return section;
}

Expand All @@ -458,14 +452,15 @@ static void _ds_comment_align_(RzDisasmState *ds, bool up, bool nl) {
theme_print_color(comment);
return;
}
const char *sn = ds->show_section ? rz_core_get_section_name(ds->core, ds->at) : "";
char *sn = ds->show_section ? rz_core_get_section_name(ds->core, ds->at) : NULL;
ds_align_comment(ds);
ds_align_comment(ds);
rz_cons_print(COLOR_RESET(ds));
ds_print_pre(ds, true);
rz_cons_printf("%s%s", nl ? "\n" : "", sn);
rz_cons_printf("%s%s", nl ? "\n" : "", rz_str_get(sn));
ds_print_ref_lines(ds->refline, ds->line_col, ds);
rz_cons_printf(" %s %s", up ? "" : ".-", COLOR(ds, comment));
free(sn);
}
#define CMT_ALIGN _ds_comment_align_(ds, true, false)

Expand Down
2 changes: 1 addition & 1 deletion librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ RZ_API RZ_OWN RzList /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core);
RZ_API char *rz_core_theme_get(RzCore *core);
RZ_API bool rz_core_theme_load(RzCore *core, const char *name);
RZ_API void rz_core_theme_nextpal(RzCore *core, RzConsPalSeekMode mode);
RZ_API const char *rz_core_get_section_name(RzCore *core, ut64 addr);
RZ_API RZ_OWN char *rz_core_get_section_name(RzCore *core, ut64 addr);
RZ_API RzCons *rz_core_get_cons(RzCore *core);
RZ_API RzBin *rz_core_get_bin(RzCore *core);
RZ_API RzConfig *rz_core_get_config(RzCore *core);
Expand Down
6 changes: 3 additions & 3 deletions librz/include/rz_util/rz_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str);
RZ_API void rz_str_trim_char(RZ_NONNULL RZ_INOUT char *str, const char c);
RZ_API char *rz_str_trim_dup(const char *str);
RZ_API char *rz_str_trim_lines(char *str);
RZ_API void rz_str_trim_head(RZ_NONNULL char *str);
RZ_API void rz_str_trim_head(RZ_NONNULL RZ_INOUT char *str);
RZ_API void rz_str_trim_head_char(RZ_NONNULL RZ_INOUT char *str, const char c);
RZ_API const char *rz_str_trim_head_ro(const char *str);
RZ_API const char *rz_str_trim_head_wp(const char *str);
RZ_API const char *rz_str_trim_head_ro(RZ_NONNULL const char *str);
RZ_API const char *rz_str_trim_head_wp(RZ_NONNULL const char *str);
RZ_API RZ_BORROW char *rz_str_trim_tail(RZ_NONNULL char *str);
RZ_API void rz_str_trim_tail_char(RZ_NONNULL RZ_INOUT char *str, const char c);
RZ_API ut64 rz_str_djb2_hash(const char *str);
Expand Down
20 changes: 13 additions & 7 deletions librz/util/str_trim.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,23 @@ RZ_API char *rz_str_trim_dup(const char *str) {
return a;
}

// Returns a pointer to the first non-whitespace character of str.
// TODO: Find a better name: to rz_str_trim_head_ro(), rz_str_skip_head or so
RZ_API const char *rz_str_trim_head_ro(const char *str) {
/* \brief Returns a pointer to the first non-whitespace character of \p str
*
* It considers space, TAB, and newline characters as the whitespace
*/
RZ_API const char *rz_str_trim_head_ro(RZ_NONNULL const char *str) {
rz_return_val_if_fail(str, NULL);
for (; *str && IS_WHITECHAR(*str); str++) {
;
}
return str;
}

// TODO: find better name
RZ_API const char *rz_str_trim_head_wp(const char *str) {
/* \brief Returns a pointer to the first non-whitespace character of \p str
*
* It considers only space and TAB as the whitespace
*/
RZ_API const char *rz_str_trim_head_wp(RZ_NONNULL const char *str) {
rz_return_val_if_fail(str, NULL);
for (; *str && !IS_WHITESPACE(*str); str++) {
;
Expand All @@ -107,7 +112,8 @@ RZ_API const char *rz_str_trim_head_wp(const char *str) {
*
* \param str The string to trim.
*/
RZ_API void rz_str_trim_head(RZ_NONNULL char *str) {
RZ_API void rz_str_trim_head(RZ_NONNULL RZ_INOUT char *str) {
rz_return_if_fail(str);
char *p = (char *)rz_str_trim_head_ro(str);
if (p) {
memmove(str, p, strlen(p) + 1);
Expand All @@ -123,7 +129,7 @@ RZ_API void rz_str_trim_head(RZ_NONNULL char *str) {
* \return The edited string.
*/
RZ_API RZ_BORROW char *rz_str_trim_tail(RZ_NONNULL char *str) {
rz_return_val_if_fail(str, str);
rz_return_val_if_fail(str, NULL);
size_t length = strlen(str);
while (length-- > 0) {
if (IS_WHITECHAR(str[length])) {
Expand Down

0 comments on commit 1a63bc0

Please sign in to comment.