Skip to content

Commit

Permalink
Add disasm+decompiler side by side api for the codemeta api ##disasm
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed Nov 6, 2021
1 parent 5a34189 commit 52cb0dd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 49 deletions.
88 changes: 72 additions & 16 deletions libr/anal/codemeta.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,6 @@ R_API void r_codemeta_print_json(RCodeMeta *code) {
* @param width maximum nibbles per address
*/
static void print_offset_in_binary_line_bar(RCodeMeta *code, ut64 offset, size_t width) {
static const char *fmt[9] = {
"0x%08" PFMT64x,
"0x%09" PFMT64x,
"0x%010" PFMT64x,
"0x%011" PFMT64x,
"0x%012" PFMT64x,
"0x%013" PFMT64x,
"0x%014" PFMT64x,
"0x%015" PFMT64x,
"0x%016" PFMT64x
};
if (width < 8) {
width = 8;
}
Expand All @@ -369,13 +358,59 @@ static void print_offset_in_binary_line_bar(RCodeMeta *code, ut64 offset, size_t
} else {
PRINT_COLOR (PALETTE (offset)
: Color_GREEN);
r_cons_printf (fmt[width], offset);
r_cons_printf ("0x%08" PFMT64x, offset);
PRINT_COLOR (Color_RESET);
}
r_cons_printf (" |");
}

R_API void r_codemeta_print(RCodeMeta *code, RVector *line_offsets) {
static void print_disasm_in_binary_line_bar(RCodeMeta *code, ut64 offset, size_t width, RAnal *anal) {
if (width < 8) {
width = 8;
}
if (width > 16) {
width = 16;
}
width -= 8;

width = 40;
RCons *cons = r_cons_singleton ();
r_cons_printf (" ");
if (offset == UT64_MAX) {
const char *pad = r_str_pad (' ', width);
r_cons_print (pad);
} else {
width = 40;
if (anal && anal->coreb.core) {
RCore *core = anal->coreb.core;
char *c = r_str_newf ("pid 1 @ 0x%" PFMT64x " @e:asm.flags=0@e:asm.lines=0@e:asm.bytes=0", offset);
char *res = anal->coreb.cmdstrf (core, c);
free (c);
r_str_trim (res);
int w = r_str_ansi_len (res);
r_cons_print (res);
if (w < width) {
const char *pad = r_str_pad (' ', width - w);
r_cons_print (pad);
} else {
char *p = (char *)r_str_ansi_chrn (res, width);
if (p) {
*p = 0;
}
}
free (res);
} else {
PRINT_COLOR (PALETTE (offset) : Color_GREEN);
r_cons_printf ("0x%08" PFMT64x, offset);
PRINT_COLOR (Color_RESET);
const char *pad = r_str_pad (' ', width - 11);
r_cons_print (pad);
}
}
r_cons_printf (" |");
}

R_API void r_codemeta_print_internal(RCodeMeta *code, RVector *line_offsets, RAnal *anal) {
if (code->annotations.len == 0) {
r_cons_printf ("%s\n", code->code);
return;
Expand Down Expand Up @@ -448,7 +483,11 @@ R_API void r_codemeta_print(RCodeMeta *code, RVector *line_offsets) {
if (line_idx < line_offsets->len) {
offset = *(ut64 *)r_vector_index_ptr (line_offsets, line_idx);
}
print_offset_in_binary_line_bar (code, offset, offset_width);
if (anal) {
print_disasm_in_binary_line_bar (code, offset, offset_width, anal);
} else {
print_offset_in_binary_line_bar (code, offset, offset_width);
}
line_idx++;
}
r_cons_printf ("%c", code->code[cur]);
Expand All @@ -466,7 +505,11 @@ R_API void r_codemeta_print(RCodeMeta *code, RVector *line_offsets) {
offset = *(ut64 *)r_vector_index_ptr (line_offsets, line_idx);
}
PRINT_COLOR (Color_RESET);
print_offset_in_binary_line_bar (code, offset, offset_width);
if (anal) {
print_disasm_in_binary_line_bar (code, offset, offset_width, anal);
} else {
print_offset_in_binary_line_bar (code, offset, offset_width);
}
PRINT_COLOR (color);
line_idx++;
}
Expand All @@ -484,13 +527,26 @@ R_API void r_codemeta_print(RCodeMeta *code, RVector *line_offsets) {
if (line_idx < line_offsets->len) {
offset = *(ut64 *)r_vector_index_ptr (line_offsets, line_idx);
}
print_offset_in_binary_line_bar (code, offset, offset_width);
if (anal) {
print_disasm_in_binary_line_bar (code, offset, offset_width, anal);
} else {
print_offset_in_binary_line_bar (code, offset, offset_width);
}
line_idx++;
}
r_cons_printf ("%c", code->code[cur]);
}
}

R_API void r_codemeta_print_disasm(RCodeMeta *code, RVector *line_offsets, void *anal) {
r_codemeta_print_internal (code, line_offsets, anal);
}

// TODO rename R_API void r_codemeta_print_offsets(RCodeMeta *code, RVector *line_offsets, bool d) {
R_API void r_codemeta_print(RCodeMeta *code, RVector *line_offsets) {
r_codemeta_print_internal (code, line_offsets, NULL);
}

static bool foreach_offset_annotation(void *user, const ut64 offset, const void *val) {
RCodeMeta *code = user;
const RCodeMetaItem *annotation = val;
Expand Down
7 changes: 7 additions & 0 deletions libr/include/r_codemeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ R_API void r_codemeta_item_fini(RCodeMetaItem *e);
R_API bool r_codemeta_item_is_reference(RCodeMetaItem *annotation);
R_API bool r_codemeta_item_is_variable(RCodeMetaItem *annotation);
R_API void r_codemeta_add_item(RCodeMeta *code, RCodeMetaItem *annotation);

/* DECOMPILER PRINTING FUNCTIONS */
R_API void r_codemeta_print_json(RCodeMeta *code);
R_API void r_codemeta_print(RCodeMeta *code, RVector *line_offsets);
R_API void r_codemeta_print_disasm(RCodeMeta *code, RVector *line_offsets, void *anal);
R_API void r_codemeta_print_comment_cmds(RCodeMeta *code);

// compatibility with 5.2.0
#define r_codemeta_add_annotation r_codemeta_add_item
R_API RPVector *r_codemeta_at(RCodeMeta *code, size_t offset);
Expand Down
33 changes: 0 additions & 33 deletions libr/include/r_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -961,39 +961,6 @@ R_API bool r_core_plugin_add(RCmd *cmd, RCorePlugin *plugin);
R_API bool r_core_plugin_check(RCmd *cmd, const char *a0);
R_API void r_core_plugin_fini(RCmd *cmd);


/* DECOMPILER PRINTING FUNCTIONS */
/**
* @brief Prints the data contained in the specified RAnnotatedCode in JSON format.
*
* The function will print the output in console using the function r_cons_printf();
*
* @param code Pointer to a RAnnotatedCode.
*/
R_API void r_codemeta_print_json(RCodeMeta *code);
/**
* @brief Prints the decompiled code from the specified RAnnotatedCode.
*
* This function is used for printing the output of commands pdg and pdgo.
* It can print the decompiled code with or without offsets. If line_offsets is a null pointer,
* the output will be printed without offsets (pdg), otherwise, the output will be
* printed with offsets.
* This function will print the output in console using the function r_cons_printf();
*
* @param code Pointer to a RAnnotatedCode.
* @param line_offsets Pointer to a @ref RVector that contains offsets for the decompiled code.
*/
R_API void r_codemeta_print(RCodeMeta *code, RVector *line_offsets);
/**
* @brief Prints the decompiled code as comments
*
* This function is used for the output of command pdg*
* Output will be printed in console using the function r_cons_printf();
*
* @param code Pointer to a RAnnotatedCode.
*/
R_API void r_codemeta_print_comment_cmds(RCodeMeta *code);

#endif

#ifdef __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions libr/util/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,12 @@ R_API size_t r_wstr_clen(const char *s) {
return len + 1;
}

// TODO: rename to r_str_ansi_at() ? or find better name?
R_API const char *r_str_ansi_chrn(const char *str, size_t n) {
#if 0
size_t pos = r_str_ansi_nlen (str, at);
return str + pos;
#endif
int len, i, li;
for (li = i = len = 0; str[i] && (n != len); i++) {
size_t chlen = __str_ansi_length (str + i);
Expand Down

0 comments on commit 52cb0dd

Please sign in to comment.