* Add print zoom mode 'pZ'

* Visual zoom mode 'z'
* r_print now uses RIOBind
* Add r_flag_space_get
This commit is contained in:
earada 2011-02-17 00:58:54 +01:00
parent 90cf8f0ede
commit c089297bd8
11 changed files with 127 additions and 19 deletions

View File

@ -12,6 +12,49 @@
#include <sys/types.h>
#include <stdarg.h>
static int printzoomcallback(void *user, char *mode, ut64 addr, ut8 *bufz, ut64 size) {
RCore *core = (RCore *) user;
int j, ret = 0;
RListIter *iter;
RFlagItem *flag;
switch (mode[0]) {
case 'p':
for (j=0; j<size; j++)
if (IS_PRINTABLE(bufz[j]))
ret++;
break;
case 'f':
r_list_foreach (core->flags->flags, iter, flag)
if (flag->offset <= addr && addr < flag->offset+flag->size)
ret++;
break;
case 's':
j = r_flag_space_get (core->flags, "strings");
r_list_foreach (core->flags->flags, iter, flag) {
if (flag->space == j && ((addr <= flag->offset
&& flag->offset < addr+size) ||
(addr <= flag->offset+flag->size &&
flag->offset+flag->size < addr+size)))
ret++;
}
break;
case 'F': // 0xFF
for (j=0; j<size; j++)
if (bufz[j] == 0xff)
ret++;
break;
case 'e': // entropy
ret = (unsigned char) r_hash_entropy (bufz, size);
break;
case 'h': //head
default:
ret = bufz[0];
}
return ret;
}
static int checkbpcallback(RCore *core) {
ut64 pc = r_debug_reg_get (core->dbg, "pc");
RBreakpointItem *bpi = r_bp_get (core->dbg->bp, pc);
@ -41,6 +84,8 @@ static void printoffset(ut64 off, int show_color) {
else r_cons_printf ("0x%08"PFMT64x" ", off);
}
/* TODO: move to print/disasm.c */
static void r_print_disasm(RPrint *p, RCore *core, ut64 addr, ut8 *buf, int len, int l) {
RAnalCC cc = {0};
@ -1738,6 +1783,14 @@ static int cmd_print(void *data, const char *input) {
break;
}
break;
case 'Z':
{
char *mode = r_config_get (core->config, "zoom.byte");
ut64 from = r_config_get_i (core->config, "zoom.from");
ut64 to = r_config_get_i (core->config, "zoom.to");
r_print_zoom (core->print, core, printzoomcallback, from, to, mode, core->blocksize);
}
break;
default:
r_cons_printf (
"Usage: p[fmt] [len]\n"
@ -1753,7 +1806,8 @@ static int cmd_print(void *data, const char *input) {
" pD [len] disassemble N bytes\n"
" pr [len] print N raw bytes\n"
" pu [len] print N url encoded bytes\n"
" pU [len] print N wide url encoded bytes\n");
" pU [len] print N wide url encoded bytes\n",
" pZ [len] print zoom view\n");
break;
}
if (tbs != core->blocksize)

View File

@ -358,9 +358,9 @@ R_API int r_core_config_init(RCore *core) {
r_config_set_cb (cfg, "dbg.swstep", "false", &config_swstep_callback);
r_config_set_cb (cfg, "dbg.trace", "true", &config_trace_callback);
r_config_set_cb (cfg, "dbg.trace.tag", "0xff", &config_tracetag_callback);
r_config_set (cfg, "cmd.hit", "");
r_config_set (cfg, "cmd.open", "");
r_config_set (cfg, "cmd.prompt", "");
r_config_set (cfg, "cmd.hit", "");
r_config_set (cfg, "cmd.open", "");
r_config_set (cfg, "cmd.prompt", "");
r_config_set (cfg, "cmd.vprompt", "");
r_config_set (cfg, "cmd.bp", "");
r_config_set_cb (cfg, "scr.prompt", "true", &config_scrprompt_callback);
@ -388,6 +388,10 @@ R_API int r_core_config_init(RCore *core) {
r_config_set (cfg, "rap.loop", "true");
/* vm */
r_config_set_cb (cfg, "vm.arch", "x86", &config_vmarch_callback);
/* zoom */
r_config_set_i (cfg, "zoom.from", 0);
r_config_set_i (cfg, "zoom.to", 0);
r_config_set (cfg, "zoom.byte", "h");
/* TODO cmd */
#if 0
@ -582,10 +586,6 @@ R_API int r_core_config_init(RCore *core) {
config_set("gui.right", "gtk-hello"); // graphviz
config_set("gui.bottom", "gtk-hello"); // graphviz
node = config_set_i("zoom.from", 0);
node = config_set_i("zoom.to", config.size);
node = config_set("zoom.byte", "head");
node->callback = &config_zoombyte_callback;
node = config_set("scr.palette", cons_palette_default);
node->callback = &config_palette_callback;

View File

@ -305,6 +305,7 @@ R_API int r_core_init(RCore *core) {
core->search = r_search_new (R_SEARCH_KEYWORD);
r_io_undo_enable (core->io, 1, 0); // TODO: configurable via eval
core->fs = r_fs_new ();
r_io_bind (core->io, &(core->print->iob));
r_io_bind (core->io, &(core->anal->iob));
r_io_bind (core->io, &(core->fs->iob));
//r_cmd_macro_init (&core->macro);

View File

@ -253,6 +253,7 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int mode, ut64 loa
if (cp && *cp)
r_core_cmd (r, cp, 0);
r_config_set (r->config, "file.path", file);
r_config_set_i (r->config, "zoom.to", loadaddr+fh->size);
fh->map = r_io_map_add (r->io, fh->fd->fd, mode, 0, loadaddr, fh->size);
return fh;
}

View File

@ -13,6 +13,7 @@ static int curset = 0, cursor = 0, ocursor=-1;
static int color = 1;
static int debug = 1;
static int flags = R_PRINT_FLAGS_ADDRMOD;
static int zoom = 0;
static int marks_init = 0;
static ut64 marks[UT8_MAX+1];
@ -1038,6 +1039,9 @@ R_API int r_core_visual_cmd(RCore *core, int ch) {
case 'x':
r_core_cmdf (core, "./a 0x%08llx @ entry0", core->offset);
break;
case 'z':
zoom = !zoom;
break;
case '?':
r_cons_clear00 ();
r_cons_printf (
@ -1059,6 +1063,7 @@ R_API int r_core_visual_cmd(RCore *core, int ch) {
" :cmd - run radare command\n"
" ;[-]cmt - add/remove comment\n"
" . - seek to program counter\n"
" z - toggle zoom mode\n"
" q - back to radare shell\n");
r_cons_flush ();
r_cons_any_key ();
@ -1108,7 +1113,10 @@ static void r_core_visual_refresh (RCore *core) {
r_cons_clear00 ();
r_print_set_cursor (core->print, curset, ocursor, cursor);
r_core_visual_prompt (core, color);
r_core_cmd (core, printfmt[R_ABS (printidx%NPF)], 0);
if (zoom)
r_core_cmd (core, "pZ", 0);
else
r_core_cmd (core, printfmt[R_ABS (printidx%NPF)], 0);
r_cons_visual_flush ();
}

View File

@ -107,7 +107,7 @@ R_API void r_flag_list(RFlag *f, int rad) {
if (rad) {
if (fs == -1 || flag->space != fs) {
fs = flag->space;
r_cons_printf ("fs %s\n", r_flag_space_get (f, fs));
r_cons_printf ("fs %s\n", r_flag_space_get_i (f, fs));
}
r_cons_printf ("f %s %"PFMT64d" 0x%08"PFMT64x"\n",
flag->name, flag->size, flag->offset);

View File

@ -2,7 +2,18 @@
#include <r_flags.h>
R_API const char *r_flag_space_get(struct r_flag_t *f, int idx) {
R_API int r_flag_space_get(struct r_flag_t *f, char *name) {
int i;
for (i=0;i<R_FLAG_SPACES_MAX;i++) {
if (f->spaces[i] != NULL)
if (!strcmp (name, f->spaces[i]))
return i;
}
return -1;
}
R_API const char *r_flag_space_get_i (struct r_flag_t *f, int idx) {
if (idx==-1 || idx>255 || f->spaces[idx]=='\0')
return "";
return f->spaces[idx];

View File

@ -51,8 +51,8 @@ R_API int r_flag_name_filter(char *name);
R_API void r_flag_item_rename(RFlagItem *item, const char *name);
/* spaces */
R_API const char *r_flag_space_get(RFlag *f, int idx);
//R_API RFlagItem *r_flag_space_get_i(RFlag *f, ut64 off);
R_API int r_flag_space_get(RFlag *f, char *name);
R_API const char *r_flag_space_get_i(RFlag *f, int idx);
R_API void r_flag_space_set(RFlag *f, const char *name);
R_API void r_flag_space_list(RFlag *f);
#endif

View File

@ -3,15 +3,18 @@
#include "r_types.h"
#include "r_util.h"
#include "r_io.h"
#define R_PRINT_FLAGS_COLOR 0x00000001
#define R_PRINT_FLAGS_ADDRMOD 0x00000002
#define R_PRINT_FLAGS_CURSOR 0x00000004
#define R_PRINT_FLAGS_HEADER 0x00000008
typedef int (*RPrintZoomCallback)(void *user, char *mode, ut64 addr, ut8 *bufz, ut64 size);
typedef struct r_print_t {
void *user;
int (*read_at)(ut64 addr, ut8 *buf, int len, void *user);
RIOBind iob;
char datefmt[32];
int (*printf)(const char *str, ...);
/* TODO: add printf callback */
@ -49,6 +52,7 @@ R_API int r_print_string(RPrint *p, ut64 seek, const ut8 *str, int len, int wide
R_API int r_print_date_dos(struct r_print_t *p, ut8 *buf, int len);
R_API int r_print_date_w32(struct r_print_t *p, const ut8 *buf, int len);
R_API int r_print_date_unix(struct r_print_t *p, const ut8 *buf, int len);
R_API void r_print_zoom (RPrint *p, void *user, RPrintZoomCallback cb, ut64 from, ut64 to, char *mode, int len);
#endif
#endif

View File

@ -163,10 +163,10 @@ R_API void r_print_format(struct r_print_t *p, ut64 seek, const ut8* buf, int le
break;
case 'B':
memset(buffer, '\0', 255);
if (!p->read_at) {
if (!p->iob.read_at) {
printf ("(cannot read memory)\n");
break;
} else p->read_at ((ut64)addr, buffer, 248, p->user);
} else p->iob.read_at (p->iob.io, (ut64)addr, buffer, 248);
p->printf ("0x%08x = ", seek+i);
for (j=0;j<10;j++) p->printf ("%02x ", buf[j]);
p->printf(" ... (");
@ -223,8 +223,8 @@ R_API void r_print_format(struct r_print_t *p, ut64 seek, const ut8* buf, int le
case 's':
p->printf ("0x%08x = ", seek+i);
memset (buffer, '\0', 255);
if (p->read_at)
p->read_at ((ut64)addr, buffer, 248, p->user);
if (p->iob.read_at)
p->iob.read_at (p->iob.io, (ut64)addr, buffer, 248);
else {
printf ("(cannot read memory)\n");
break;

View File

@ -9,7 +9,7 @@ R_API RPrint *r_print_new() {
if (p) {
strcpy (p->datefmt, "%d:%m:%Y %H:%M:%S %z");
p->user = NULL;
p->read_at = NULL;
r_io_bind_init (p->iob);
p->printf = printf;
p->interrupt = 0;
p->bigendian = 0;
@ -286,6 +286,35 @@ R_API void r_print_progressbar(RPrint *pr, int pc) {
fflush (stderr);
}
R_API void r_print_zoom (RPrint *p, void *user, RPrintZoomCallback cb, ut64 from, ut64 to, char *mode, int len) {
ut64 size;
ut8 *bufz, *bufz2;
int i, j = 0;
if (!mode) {
eprintf("Error: Invalid zoom mode");
return;
}
size = (to-from)/len;
if (size < 1)
size = 1;
bufz = (ut8 *) malloc (len);
bufz2 = (ut8 *) malloc (size);
memset (bufz, 0, len);
for (i=0; i<len; i++) {
p->iob.read_at (p->iob.io, from+j, bufz2, size);
bufz[i] = cb (user, mode, from+j, bufz2, size);
j += size;
}
p->flags &= ~R_PRINT_FLAGS_HEADER;
r_print_hexdump (p, from, bufz, len, 16, size);
p->flags |= R_PRINT_FLAGS_HEADER;
free (bufz);
free (bufz2);
}
#if 0
/* Process source file with given parameters. Output to stdout */
// TODO: use buffer instead of FILE*