* Add memoization in r_print_zoom to speedup the work on big disks

* Added R_NEW0 macro to fill with zeroes
* Add documentation for [] keys in Visual mode
This commit is contained in:
pancake 2011-03-23 19:19:23 +01:00
parent e6fd0a9245
commit 083d2faebd
7 changed files with 62 additions and 25 deletions

View File

@ -2080,6 +2080,7 @@ static int cmd_print(void *data, const char *input) {
"Usage: pZ [len]\n"
" print N bytes where each byte represents a block of filesize/N\n"
"Configuration:\n"
" zoom.maxsz : max size of block\n"
" zoom.from : start address\n"
" zoom.to : end address\n"
" zoom.byte : specify how to calculate each byte\n"
@ -2094,10 +2095,11 @@ static int cmd_print(void *data, const char *input) {
);
} else {
const char *mode = r_config_get (core->config, "zoom.byte");
ut64 maxsize = r_config_get_i (core->config, "zoom.maxsz");
ut64 from = r_config_get_i (core->config, "zoom.from");
ut64 to = r_config_get_i (core->config, "zoom.to");
if (mode) r_print_zoom (core->print, core, printzoomcallback,
from, to, *mode, core->blocksize);
from, to, *mode, core->blocksize, (int)maxsize);
else eprintf ("No zoom.byte defined\n");
}
break;

View File

@ -388,6 +388,7 @@ R_API int r_core_config_init(RCore *core) {
r_config_set_i (cfg, "magic.depth", 100);
r_config_set (cfg, "rap.loop", "true");
/* zoom */
r_config_set_i (cfg, "zoom.maxsz", 512);
r_config_set_i (cfg, "zoom.from", 0);
r_config_set_i (cfg, "zoom.to", 0);
r_config_set (cfg, "zoom.byte", "h");

View File

@ -1113,7 +1113,7 @@ R_API int r_core_visual_cmd(RCore *core, int ch) {
" hjkl - move around\n"
" HJKL - move around faster\n"
" pP - rotate print modes\n"
" /*+- - change block size\n"
" /*+-[] - change block size, [] = resize scr.cols\n"
" cC - toggle cursor and colors\n"
" d[f?] - define function, data, code, ..\n"
" x - find xrefs for current offset\n"

View File

@ -12,6 +12,13 @@
typedef int (*RPrintZoomCallback)(void *user, int mode, ut64 addr, ut8 *bufz, ut64 size);
typedef struct r_print_zoom_t {
ut8 *buf;
ut64 from;
ut64 to;
int size;
} RPrintZoom;
typedef struct r_print_t {
void *user;
RIOBind iob;
@ -28,6 +35,7 @@ typedef struct r_print_t {
int ocur;
int flags;
int addrmod;
RPrintZoom *zoom;
} RPrint;
#ifdef R_API
@ -52,7 +60,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, int mode, int len);
R_API void r_print_zoom (RPrint *p, void *user, RPrintZoomCallback cb, ut64 from, ut64 to, int mode, int len, int maxlen);
R_API void r_print_progressbar(RPrint *pr, int pc, int _cols);
#endif

View File

@ -57,6 +57,7 @@ typedef void (*PrintfCallback)(const char *str, ...);
#define ZERO_FILL(x) memset (x, 0, sizeof (x))
#define R_NEWS(x,y) (x*)malloc(sizeof(x)*y)
#define R_NEW(x) (x*)malloc(sizeof(x))
#define R_NEW0(x) (x*)calloc(1,sizeof(x))
// TODO: Make R_NEW_COPY be 1 arg, not two
#define R_NEW_COPY(x,y) x=(y*)malloc(sizeof(y));memcpy(x,y,sizeof(y))
#define IS_PRINTABLE(x) (x>=' '&&x<='~')

View File

@ -21,10 +21,21 @@ R_API RPrint *r_print_new() {
R_PRINT_FLAGS_COLOR |
R_PRINT_FLAGS_HEADER |
R_PRINT_FLAGS_ADDRMOD;
p->zoom = R_NEW0 (RPrintZoom);
}
return p;
}
R_API RPrint *r_print_free(RPrint *p) {
if (p->zoom) {
free (p->zoom->buf);
free (p->zoom);
p->zoom = NULL;
}
free (p);
return NULL;
}
// dummy setter can be removed
R_API void r_print_set_flags(RPrint *p, int _flags) {
p->flags = _flags;
@ -34,11 +45,6 @@ R_API void r_print_unset_flags(RPrint *p, int flags) {
p->flags = p->flags & (p->flags^flags);
}
R_API RPrint *r_print_free(RPrint *p) {
free(p);
return NULL;
}
R_API void r_print_set_cursor(RPrint *p, int enable, int ocursor, int cursor) {
p->cur_enabled = enable;
p->ocur = ocursor;
@ -282,33 +288,52 @@ R_API void r_print_progressbar(RPrint *p, int pc, int _cols) {
(pc<0)?pc=0:(pc>100)?pc=100:0;
p->printf ("%4d%% [", pc);
cols -= 15;
for(tmp=cols*pc/100;tmp;tmp--) p->printf ("#");
for(tmp=cols-(cols*pc/100);tmp;tmp--) p->printf ("-");
for (tmp=cols*pc/100;tmp;tmp--) p->printf ("#");
for (tmp=cols-(cols*pc/100);tmp;tmp--) p->printf ("-");
p->printf ("]");
}
R_API void r_print_zoom (RPrint *p, void *user, RPrintZoomCallback cb, ut64 from, ut64 to, int mode, int len) {
ut64 size;
R_API void r_print_zoom (RPrint *p, void *user, RPrintZoomCallback cb, ut64 from, ut64 to, int mode, int len, int maxlen) {
ut8 *bufz, *bufz2;
int i, j = 0;
ut64 size = (to-from)/len;
size = (to-from)/len;
if (size < 1)
size = 1;
bufz = (ut8 *) malloc (len);
bufz2 = (ut8 *) malloc (size);
memset (bufz, 0, len);
bufz = bufz2 = NULL;
if (maxlen<2) maxlen = 1024*1024;
if (size>maxlen) size = maxlen;
if (size<1) size = 1;
if (from == p->zoom->from && to == p->zoom->to && size==p->zoom->size) {
// get from cache
bufz = p->zoom->buf;
size = p->zoom->size;
} else {
bufz = (ut8 *) malloc (len);
if (bufz == NULL) return;
bufz2 = (ut8 *) malloc (size);
if (bufz2 == NULL) {
free (bufz);
return;
}
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;
// TODO: memoize blocks or gtfo
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;
}
free (bufz2);
// memoize
free (p->zoom->buf);
p->zoom->buf = bufz;
p->zoom->from = from;
p->zoom->to = to;
p->zoom->size = 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

View File

@ -8,7 +8,7 @@ int main()
r_cons_new();
p = r_print_new();
r_print_hexdump(p, (ut64)(main), buf, 128, 16, 1);
r_print_hexdump(p, (ut64)(size_t)(main), buf, 128, 16, 1);
r_cons_flush();
return 0;