From 58dfc4051d462b90cde602cf2fa227caa2cf44bb Mon Sep 17 00:00:00 2001 From: pancake Date: Sat, 21 Apr 2012 14:28:53 +0200 Subject: [PATCH] * Remove max blocksize hard limit - Now configurable thru the io.maxblk var * Add support for huge dumping with 'wt' - As requested by nics --- libr/core/cmd_print.c | 2 +- libr/core/cmd_write.c | 16 ++++++++++++---- libr/core/config.c | 19 +++++++++++++++++-- libr/core/core.c | 8 +++++--- libr/core/io.c | 32 ++++++++++++++++++++++++++++++++ libr/include/r_core.h | 6 ++++-- 6 files changed, 71 insertions(+), 12 deletions(-) diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index f557e56944..dc036e0ae7 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -65,7 +65,7 @@ static int cmd_print(void *data, const char *input) { }// else l = 0; } else l = len; - i = r_config_get_i (core->config, "cfg.maxbsize"); + i = r_config_get_i (core->config, "io.maxblk"); if (i && l > i) { eprintf ("This block size is too big. Did you mean 'p%c @ %s' instead?\n", *input, input+2); diff --git a/libr/core/cmd_write.c b/libr/core/cmd_write.c index 568bf3c820..855ed66d43 100644 --- a/libr/core/cmd_write.c +++ b/libr/core/cmd_write.c @@ -132,12 +132,20 @@ static int cmd_write(void *data, const char *input) { r_core_block_read (core, 0); break; case 't': - /* TODO: support userdefined size? */ - arg = (const char *)(input+((input[1]==' ')?2:1)); - r_file_dump (arg, core->block, core->blocksize); + if (*str != ' ') { + eprintf ("Usage: wt file [off]\n"); + } else { + tmp = strchr (str+1, ' '); + if (tmp) { + st64 sz = (st64) r_num_math (core->num, tmp+1); + *tmp = 0; + if (sz<1) eprintf ("Invalid length\n"); + else r_core_dump (core, str+1, core->offset, (ut64)sz); + } else r_file_dump (str+1, core->block, core->blocksize); + } break; case 'T': - eprintf ("TODO\n"); + eprintf ("TODO: wT // why?\n"); break; case 'f': arg = (const char *)(input+((input[1]==' ')?2:1)); diff --git a/libr/core/config.c b/libr/core/config.c index d0aa8014e6..8ae412cd7d 100644 --- a/libr/core/config.c +++ b/libr/core/config.c @@ -32,6 +32,15 @@ static int config_searchalign_callback(void *user, void *data) { return R_TRUE; } +static int config_iomaxblk_callback(void *user, void *data) { + RCore *core = (RCore *) user; + RConfigNode *node = (RConfigNode *) data; + if (node->i_value>1) { + core->blocksize_max = node->i_value; + return R_TRUE; + } + return R_FALSE; +} static int config_ioffio_callback(void *user, void *data) { RCore *core = (RCore *) user; RConfigNode *node = (RConfigNode *) data; @@ -497,8 +506,6 @@ R_API int r_core_config_init(RCore *core) { r_config_desc (cfg, "cfg.datefmt", "Date format (%d:%m:%Y %H:%M:%S %z)"); r_config_set (cfg, "cfg.fortunes", "true"); r_config_desc (cfg, "cfg.fortunes", "If enabled show tips at start"); - r_config_set_i (cfg, "cfg.maxbsize", 524288); - r_config_desc (cfg, "cfg.maxbsize", "Max block size in print command"); r_config_set (cfg, "cfg.wseek", "false"); r_config_desc (cfg, "cfg.wseek", "Seek after write"); r_config_set_i (cfg, "cfg.hashlimit", SLURP_LIMIT); @@ -579,6 +586,14 @@ R_API int r_core_config_init(RCore *core) { r_config_desc (cfg, "search.align", "Only catch aligned search hits"); r_config_set_cb (cfg, "scr.html", "false", &config_scrhtml_callback); r_config_desc (cfg, "scr.html", "If enabled disassembly use HTML syntax"); + +{ + char buf[128]; + sprintf (buf, "%d", R_CORE_BLOCKSIZE_MAX); + r_config_set_cb (cfg, "io.maxblk", buf, &config_iomaxblk_callback); + r_config_desc (cfg, "io.maxblk", "set max block size (soft limit)"); +} + r_config_set_cb (cfg, "io.ffio", "true", &config_ioffio_callback); r_config_desc (cfg, "io.ffio", "fill invalid buffers with 0xff instead of returning error"); r_config_set_cb (cfg, "io.va", "true", &config_iova_callback); diff --git a/libr/core/core.c b/libr/core/core.c index 9b4d41b59c..2820fcb4ab 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -482,9 +482,11 @@ R_API int r_core_block_size(RCore *core, int bsize) { return R_FALSE; if (bsize<1) bsize = 1; - else if (bsize> R_CORE_BLOCKSIZE_MAX) - bsize = R_CORE_BLOCKSIZE_MAX; - else ret = R_TRUE; + else if (bsize>core->blocksize_max) { + eprintf ("blocksize is bigger than io.maxblk. dimmed to 0x%x\n", + core->blocksize_max); + bsize = core->blocksize_max; + } else ret = R_TRUE; core->block = realloc (core->block, bsize+1); if (core->block == NULL) { eprintf ("Oops. cannot allocate that much (%u)\n", bsize); diff --git a/libr/core/io.c b/libr/core/io.c index 40afa6fc3c..71f7efb691 100644 --- a/libr/core/io.c +++ b/libr/core/io.c @@ -2,6 +2,38 @@ #include "r_core.h" +R_API int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size) { + ut64 i; + ut8 *buf; + int bs = core->blocksize; + FILE *fd; + truncate (file, 0); + fd = fopen (file, "wb"); + if (!fd) { + eprintf ("Cannot open '%s' for writing\n", file); + return R_FALSE; + } + buf = malloc (bs); + r_cons_break (NULL, NULL); + for (i=0; ibreaked) + break; + if ((i+bs)>size) + bs = size-i; + r_io_read_at (core->io, addr+i, buf, bs); + if (fwrite (buf, bs, 1, fd) <1) { + eprintf ("write error\n"); + break; + } + i += bs; + } + eprintf ("dumped 0x%"PFMT64x" bytes\n", i); + r_cons_break_end (); + fclose (fd); + free (buf); + return R_TRUE; +} + R_API int r_core_write_op(RCore *core, const char *arg, char op) { char *str; ut8 *buf; diff --git a/libr/include/r_core.h b/libr/include/r_core.h index fb0882989b..e4a89acd3b 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2011 pancake */ +/* radare - LGPL - Copyright 2009-2012 pancake */ #ifndef _INCLUDE_R_CORE_H_ #define _INCLUDE_R_CORE_H_ @@ -28,7 +28,7 @@ #define R_CORE_CMD_EXIT -2 #define R_CORE_BLOCKSIZE 64 -#define R_CORE_BLOCKSIZE_MAX 0x40000 /* 4 MB */ +#define R_CORE_BLOCKSIZE_MAX 0x40000 /* 256KB */ #define R_CORE_ANAL_GRAPHLINES 0x1 #define R_CORE_ANAL_GRAPHBODY 0x2 @@ -75,6 +75,7 @@ typedef struct r_core_asmsteps_t { typedef struct r_core_t { ut64 offset; ut32 blocksize; + ut32 blocksize_max; ut8 *block; ut8 *oobi; /* out of band input ; used to get input from file or multiline */ int ffio; @@ -274,6 +275,7 @@ R_API int r_core_patch (RCore *core, const char *patch); R_API void r_core_hack_help(RCore *core); R_API int r_core_hack(RCore *core, const char *op); +R_API int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size); R_API void r_core_diff_show(RCore *c, RCore *c2); #endif