Skip to content

Commit

Permalink
Minor hexdump optimization, use more cmdCall and minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
radare authored and trufae committed Jan 5, 2023
1 parent fff7d4b commit 634219b
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 43 deletions.
1 change: 0 additions & 1 deletion libr/anal/meta.c
Expand Up @@ -140,7 +140,6 @@ R_API const char *r_meta_get_string(RAnal *a, RAnalMetaType type, ut64 addr) {
return item->str;
}


static void del(RAnal *a, RAnalMetaType type, const RSpace *space, ut64 addr, ut64 size) {
RPVector *victims = NULL;
if (size == UT64_MAX) {
Expand Down
1 change: 0 additions & 1 deletion libr/cons/hud.c
Expand Up @@ -147,7 +147,6 @@ static bool __matchString(char *entry, char *filter, char *mask, const int mask_
return true;
}


static RList *hud_filter(RList *list, char *user_input, int top_entry_n, int *current_entry_n, char **selected_entry, bool simple) {
RListIter *iter;
char *current_entry;
Expand Down
17 changes: 17 additions & 0 deletions libr/core/canal.c
Expand Up @@ -6327,3 +6327,20 @@ R_API void r_core_anal_propagate_noreturn(RCore *core, ut64 addr) {
r_list_free (todo);
ht_uu_free (done);
}

R_API char *r_core_anal_get_comments(RCore *core, ut64 addr) {
if (core) {
const char *type = r_meta_get_string (core->anal, R_META_TYPE_VARTYPE, addr);
const char *cmt = r_meta_get_string (core->anal, R_META_TYPE_COMMENT, addr);
if (type && cmt) {
return r_str_newf ("%s %s", type, cmt);
}
if (type) {
return strdup (type);
}
if (cmt) {
return strdup (cmt);
}
}
return NULL;
}
2 changes: 1 addition & 1 deletion libr/core/cconfig.c
Expand Up @@ -3843,7 +3843,7 @@ R_API int r_core_config_init(RCore *core) {
free (zigndir);
SETPREF ("stack.reg", "SP", "which register to use as stack pointer in the visual debug");
SETBPREF ("stack.bytes", "true", "show bytes instead of words in stack");
SETBPREF ("stack.anotated", "false", "show anotated hexdump in visual debug");
SETBPREF ("stack.annotated", "false", "show annotated hexdump in visual debug");
SETI ("stack.size", 64, "size in bytes of stack hexdump in visual debug");
SETI ("stack.delta", 0, "delta for the stack dump");

Expand Down
14 changes: 7 additions & 7 deletions libr/core/cmd_print.c
Expand Up @@ -2071,14 +2071,15 @@ static void cmd_print_format(RCore *core, const char *_input, const ut8* block,
* that are 4 chars long. */
#define append(x, y) if (x && y) { strcat (x, y); x += strlen (y); }
static void annotated_hexdump(RCore *core, const char *str, int len) {
if (!core || !str || len < 1) {
r_return_if_fail (core);
if (!str || len < 1) {
return;
}
const int usecolor = r_config_get_i (core->config, "scr.color");
int nb_cols = r_config_get_i (core->config, "hex.cols");
core->print->use_comments = r_config_get_i (core->config, "hex.comments");
int flagsz = r_config_get_i (core->config, "hex.flagsz");
bool showSection = r_config_get_i (core->config, "hex.section");
bool showSection = r_config_get_b (core->config, "hex.section");
const ut8 *buf = core->block;
ut64 addr = core->offset;
int color_idx = 0;
Expand All @@ -2088,7 +2089,7 @@ static void annotated_hexdump(RCore *core, const char *str, int len) {
int i, j, low, max, here, rows;
bool marks = false, setcolor = true, hascolor = false;
ut8 ch = 0;
char *colors[10] = {NULL};
char *colors[10] = { NULL };
for (i = 0; i < 10; i++) {
colors[i] = r_cons_rainbow_get (i, 10, false);
}
Expand Down Expand Up @@ -2179,7 +2180,6 @@ static void annotated_hexdump(RCore *core, const char *str, int len) {
ea = va;
}
}

if (usecolor) {
append (ebytes, core->cons->context->pal.offset);
}
Expand All @@ -2204,7 +2204,7 @@ static void annotated_hexdump(RCore *core, const char *str, int len) {
RAnalMetaItem *meta = meta_node ? meta_node->data : NULL;
if (meta && meta->type == R_META_TYPE_FORMAT && meta_node->start == addr + j) {
r_cons_printf (".format %s ; size=", meta->str);
r_core_cmdf (core, "pfs %s", meta->str);
r_core_cmd_callf (core, "pfs %s", meta->str);
r_core_cmdf (core, "pf %s @ 0x%08"PFMT64x, meta->str, meta_node->start);
if (usecolor) {
append (ebytes, Color_INVERT);
Expand Down Expand Up @@ -2375,8 +2375,8 @@ static void annotated_hexdump(RCore *core, const char *str, int len) {
}
}
}
sprintf (ebytes, "%02x", ch);
// r_print_byte (core->print, "%02x ", j, ch);
// R2_590 - r_hex_from_byte (ebytes, ch);
snprintf (ebytes, 3, "%02x", (ch & 0xff));
ebytes += strlen (ebytes);
if (hadflag) {
if (usecolor) {
Expand Down
17 changes: 0 additions & 17 deletions libr/core/core.c
Expand Up @@ -2580,23 +2580,6 @@ R_API char *r_core_anal_hasrefs_to_depth(RCore *core, ut64 value, PJ *pj, int de
return res;
}

R_API char *r_core_anal_get_comments(RCore *core, ut64 addr) {
if (core) {
const char *type = r_meta_get_string (core->anal, R_META_TYPE_VARTYPE, addr);
const char *cmt = r_meta_get_string (core->anal, R_META_TYPE_COMMENT, addr);
if (type && cmt) {
return r_str_newf ("%s %s", type, cmt);
}
if (type) {
return strdup (type);
}
if (cmt) {
return strdup (cmt);
}
}
return NULL;
}

static R_TH_LOCAL char *const_color = NULL;

R_API const char *colorforop(RCore *core, ut64 addr) {
Expand Down
4 changes: 2 additions & 2 deletions libr/core/disasm.c
Expand Up @@ -7133,9 +7133,9 @@ R_API int r_core_disasm_pde(RCore *core, int nb_opcodes, int mode) {
pj_a (pj);
}
if (!core->anal->esil) {
r_core_cmd0 (core, "aei");
r_core_cmd_call (core, "aei");
if (!r_config_get_b (core->config, "cfg.debug")) {
r_core_cmd0 (core, "aeim");
r_core_cmd_call (core, "aeim");
}
}
REsil *esil = core->anal->esil;
Expand Down
2 changes: 1 addition & 1 deletion libr/core/visual.c
Expand Up @@ -4622,7 +4622,7 @@ R_API int r_core_visual(RCore *core, const char *input) {
#if 1
// This is why multiple debug views dont work
if (core->printidx == R_CORE_VISUAL_MODE_DB) {
const int pxa = r_config_get_i (core->config, "stack.anotated"); // stack.anotated
const bool pxa = r_config_get_b (core->config, "stack.annotated");
const char *reg = r_config_get (core->config, "stack.reg");
const int size = r_config_get_i (core->config, "stack.size");
const int delta = r_config_get_i (core->config, "stack.delta");
Expand Down
28 changes: 19 additions & 9 deletions libr/util/hex.c
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2020 - pancake */
/* radare - LGPL - Copyright 2007-2023 - pancake */

#include <r_util.h>

Expand All @@ -16,6 +16,15 @@ R_API bool r_hex_to_byte(ut8 *val, ut8 c) {
return false;
}

// R2_590 make r_hex_from_byte public via R_API

// takes 'c' byte and fills 2 bytes in the val string
static void r_hex_from_byte(char *val, ut8 c) {
const char abc[] = "0123456789abcdef";
val[0] = abc[(c >> 4) & 0xf];
val[1] = abc[c & 0xf];
}

R_API char *r_hex_from_py_str(char *out, const char *code) {
if (!strncmp (code, "'''", 3)) {
const char *s = code + 2;
Expand Down Expand Up @@ -112,16 +121,17 @@ R_API char *r_hex_from_c_str(char *out, const char **code) {
if (*iter == '\\') {
iter++;
switch (iter[0]) {
case 'e': *out++='1';*out++='b';break;
case 'r': *out++='0';*out++='d';break;
case 'n': *out++='0';*out++='a';break;
case 'e': *out++ = '1'; *out++ = 'b'; break;
case 'r': *out++ = '0'; *out++ = 'd'; break;
case 'n': *out++ = '0'; *out++ = 'a'; break;
case 'x': {
ut8 c1 = iter[1];
ut8 c2 = iter[2];
iter += 2;
if (c1 == '\0' || c2 == '\0') {
return NULL;
} else if (strchr (abc, c1) && strchr (abc, c2)) {
}
if (strchr (abc, c1) && strchr (abc, c2)) {
*out++ = tolower (c1);
*out++ = tolower (c2);
} else {
Expand Down Expand Up @@ -164,7 +174,7 @@ const char *skip_comment_c(const char *code) {

R_API char *r_hex_from_c_array(char *out, const char *code) {
const char abc[] = "0123456789abcdef";
if (*code != '{' || !strchr(code, '}')) {
if (*code != '{' || !strchr (code, '}')) {
return NULL;
}
code++;
Expand Down Expand Up @@ -339,7 +349,7 @@ R_API int r_hex_pair2bin(const char *arg) {
ut32 j = 0;

for (ptr = (ut8*)arg; ;ptr = ptr + 1) {
if (!*ptr || *ptr==' ' || j==2) {
if (!*ptr || *ptr==' ' || j == 2) {
break;
}
d = c;
Expand All @@ -362,7 +372,7 @@ R_API int r_hex_bin2str(const ut8 *in, int len, char *out) {
return 0;
}
for (idx = i = 0; i < len; i++, idx += 2) {
snprintf (tmp, sizeof (tmp), "%02x", in[i]);
r_hex_from_byte (tmp, in[i]);
memcpy (out + idx, tmp, 2);
}
out[idx] = 0;
Expand All @@ -381,7 +391,7 @@ R_API char *r_hex_bin2strdup(const ut8 *in, int len) {
return NULL;
}
for (i = idx = 0; i < len; i++, idx += 2) {
snprintf (tmp, sizeof (tmp), "%02x", in[i]);
r_hex_from_byte (tmp, in[i]);
memcpy (out+idx, tmp, 2);
}
out[idx] = 0;
Expand Down
5 changes: 3 additions & 2 deletions libr/util/print.c
Expand Up @@ -1382,16 +1382,17 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
}
if (p && p->offname) {
a = p->offname (p->user, addr + j);
if (p->colorfor && a && *a) {
if (p->colorfor && R_STR_ISNOTEMPTY (a)) {
const char *color = p->colorfor (p->user, addr + j, addr + j, true);
printfmt ("%s ; %s%s", r_str_get (color), a,
color ? Color_RESET : "");
}
}
char *comment = p->get_comments (p->user, addr + j);
if (comment) {
// r_str_ansi_strip (comment);
if (p && p->colorfor) {
a = p->colorfor (p->user, addr + j, addr +j, true);
a = p->colorfor (p->user, addr + j, addr + j, true);
if (R_STR_ISEMPTY (a)) {
a = "";
}
Expand Down
4 changes: 2 additions & 2 deletions libr/util/str.c
Expand Up @@ -2006,8 +2006,8 @@ R_API size_t r_str_nlen_w(const char *str, int n) {
len++;
str++;
if (!*str) {
//handle wide strings
//xx00yy00bb00
// handle wide strings
// xx00yy00bb00
if (n - 2 > 0) {
if (str[2]) {
break;
Expand Down

0 comments on commit 634219b

Please sign in to comment.