Skip to content

Commit

Permalink
core/disasm: remove static variables (#4214)
Browse files Browse the repository at this point in the history
XVilka authored Feb 12, 2024
1 parent c5e787d commit 3da3184
Showing 7 changed files with 51 additions and 48 deletions.
37 changes: 19 additions & 18 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
@@ -557,6 +557,20 @@ static const ut32 colormap[256] = {
0xffffff,
};

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 rz_str_dup("unknown");
}
rz_str_trim(csection);
if (RZ_STR_ISEMPTY(csection)) {
free(csection);
return rz_str_dup("unknown");
}
return csection;
}

static void colordump(RzCore *core, const ut8 *block, int len) {
const char *chars = " .,:;!O@#";
bool square = rz_config_get_i(core->config, "scr.square");
@@ -574,8 +588,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);
@@ -1403,10 +1418,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) {
@@ -2305,23 +2321,8 @@ 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);
if (RZ_STR_ISEMPTY(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);
return strdup("unknown");
}
return section_name;
}

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;
}
21 changes: 8 additions & 13 deletions librz/core/disasm.c
Original file line number Diff line number Diff line change
@@ -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;
}

@@ -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)

2 changes: 1 addition & 1 deletion librz/include/rz_core.h
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 3 additions & 3 deletions librz/include/rz_util/rz_str.h
Original file line number Diff line number Diff line change
@@ -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);
21 changes: 14 additions & 7 deletions librz/util/str_trim.c
Original file line number Diff line number Diff line change
@@ -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++) {
;
@@ -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);
@@ -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])) {
@@ -188,6 +194,7 @@ RZ_API void rz_str_trim_char(RZ_NONNULL RZ_INOUT char *str, const char c) {
* \param str The string to trim.
*/
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str) {
rz_return_if_fail(str);
rz_str_trim_head(str);
rz_str_trim_tail(str);
}
6 changes: 3 additions & 3 deletions test/db/cmd/cmd_px
Original file line number Diff line number Diff line change
@@ -141,9 +141,9 @@ EXPECT=<<EOF
0x00000708 0x1f0f obj.__FRAME_END+5799
0x0000070a 0x0084 segment.PHDR+68
0x0000070c 0x0000 segment.LOAD0
.rodata 0x00000720 0x0001 segment.LOAD0+1
.rodata 0x00000722 0x0002 segment.LOAD0+2
.eh_frame_hdr 0x00000724 0x1b01 obj.__FRAME_END+4761
.rodata 0x00000720 0x0001 segment.LOAD0+1
.rodata 0x00000722 0x0002 segment.LOAD0+2
.eh_frame_hdr 0x00000724 0x1b01 obj.__FRAME_END+4761
EOF
RUN

6 changes: 3 additions & 3 deletions test/db/cmd/cmd_pxw
Original file line number Diff line number Diff line change
@@ -132,9 +132,9 @@ EXPECT=<<EOF
0x00000760 0x00000014 segment.LOAD0+20
0x00000764 0x00000000 segment.LOAD0
0x00000768 0x00527a01
.eh_frame_hdr 0x00000724 0x3b031b01
.eh_frame_hdr 0x00000728 0x00000038 segment.LOAD0+56
.eh_frame_hdr 0x0000072c 0x00000006 segment.LOAD0+6
.eh_frame_hdr 0x00000724 0x3b031b01
.eh_frame_hdr 0x00000728 0x00000038 segment.LOAD0+56
.eh_frame_hdr 0x0000072c 0x00000006 segment.LOAD0+6
EOF
RUN

0 comments on commit 3da3184

Please sign in to comment.