From 87162dc8bf6c623b38b50de0d02251d07a1bef50 Mon Sep 17 00:00:00 2001 From: pancake Date: Fri, 3 Apr 2015 04:04:46 +0200 Subject: [PATCH] Add `wta` support for appending dump. --- binr/rabin2/rabin2.c | 6 ++--- doc/fortunes.fun | 1 + libr/bin/bin_write.c | 4 +-- libr/cons/editor.c | 2 +- libr/core/cmd_debug.c | 2 +- libr/core/cmd_project.c | 2 +- libr/core/cmd_section.c | 2 +- libr/core/cmd_write.c | 56 +++++++++++++++++++++++++---------------- libr/core/file.c | 2 +- libr/core/io.c | 10 +++++--- libr/core/rtr.c | 2 +- libr/core/vmenus.c | 2 +- libr/diff/diff.c | 4 +-- libr/fs/fs.c | 4 +-- libr/include/r_core.h | 2 +- libr/include/r_util.h | 2 +- libr/socket/run.c | 4 ++- libr/util/file.c | 15 ++++++++--- 18 files changed, 75 insertions(+), 47 deletions(-) diff --git a/binr/rabin2/rabin2.c b/binr/rabin2/rabin2.c index 13914b4298..0b585c483b 100644 --- a/binr/rabin2/rabin2.c +++ b/binr/rabin2/rabin2.c @@ -167,7 +167,7 @@ static int extract_binobj (const RBinFile *bf, const RBinObject *o, int idx) { eprintf ("Invalid offsets\n"); res = R_FALSE; } else { - if (!outfile || !r_file_dump (outfile, bytes+boffset, bin_size)) { + if (!outfile || !r_file_dump (outfile, bytes+boffset, bin_size, 0)) { eprintf ("Error extracting %s\n", outfile); res = R_FALSE; } else { @@ -253,7 +253,7 @@ static int rabin_dump_sections(char *scnname) { } r_buf_read_at (bin->cur->buf, section->paddr, buf, section->size); if (output) { - r_file_dump (output, buf, section->size); + r_file_dump (output, buf, section->size, 0); } else { r_hex_bin2str (buf, section->size, ret); printf ("%s\n", ret); @@ -592,7 +592,7 @@ int main(int argc, char **argv) { } b = r_bin_create (bin, code, codelen, data, datalen); if (b) { - if (r_file_dump (file, b->buf, b->length)) { + if (r_file_dump (file, b->buf, b->length, 0)) { eprintf ("Dumped %d bytes in '%s'\n", b->length, file); r_file_chmod (file, "+x", 0); } else eprintf ("Error dumping into a.out\n"); diff --git a/doc/fortunes.fun b/doc/fortunes.fun index d059a0d8e7..dc7ffb4ba7 100644 --- a/doc/fortunes.fun +++ b/doc/fortunes.fun @@ -145,3 +145,4 @@ nothing personal, just bitness Too old to crash Finnished a beer ESIL ruined my life +ESIL: The Aftersleep diff --git a/libr/bin/bin_write.c b/libr/bin/bin_write.c index c3106f0ef0..41dd66e7e3 100644 --- a/libr/bin/bin_write.c +++ b/libr/bin/bin_write.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2013 - nibble */ +/* radare - LGPL - Copyright 2009-2015 - pancake, nibble */ #include #include @@ -30,5 +30,5 @@ R_API int r_bin_wr_output(RBin *bin, const char *filename) { if (!binfile || !binfile->buf) return R_FALSE; return r_file_dump (filename, binfile->buf->buf, - binfile->buf->length); + binfile->buf->length, 0); } diff --git a/libr/cons/editor.c b/libr/cons/editor.c index bdca87f5d6..764b5378b7 100644 --- a/libr/cons/editor.c +++ b/libr/cons/editor.c @@ -66,7 +66,7 @@ static void filesave () { lines[i]='\n'; } } - if (r_file_dump (path, (const ut8*)lines, bytes)) + if (r_file_dump (path, (const ut8*)lines, bytes, 0)) eprintf ("File '%s' saved (%d bytes)\n", path, bytes); else eprintf ("Cannot save file\n"); // restore back zeroes diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index a1997676a3..c470a5c200 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -548,7 +548,7 @@ static int cmd_debug_map(RCore *core, const char *input) { } else snprintf (file, sizeof (file), "0x%08"PFMT64x"-0x%08"PFMT64x"-%s.dmp", map->addr, map->addr_end, r_str_rwx_i (map->perm)); - if (!r_file_dump (file, buf, map->size)) { + if (!r_file_dump (file, buf, map->size, 0)) { eprintf ("Cannot write '%s'\n", file); free (buf); return R_FALSE; diff --git a/libr/core/cmd_project.c b/libr/core/cmd_project.c index 51e21dc729..b4e97b9919 100644 --- a/libr/core/cmd_project.c +++ b/libr/core/cmd_project.c @@ -117,7 +117,7 @@ static int cmd_project(void *data, const char *input) { if (data) { char *str = r_core_project_notes_file (core, fileproject); if (str) { - r_file_dump (str, data, strlen ((const char*)data)); + r_file_dump (str, data, strlen ((const char*)data), 0); free (str); } free (data); diff --git a/libr/core/cmd_section.c b/libr/core/cmd_section.c index 7f1d7b5ded..75ca37b3e8 100644 --- a/libr/core/cmd_section.c +++ b/libr/core/cmd_section.c @@ -103,7 +103,7 @@ static int cmd_section(void *data, const char *input) { snprintf (file, sizeof (file), "%s", input+2); } else snprintf (file, sizeof (file), "0x%08"PFMT64x"-0x%08"PFMT64x"-%s.dmp", s->vaddr, s->vaddr+s->size, r_str_rwx_i (s->rwx)); - if (!r_file_dump (file, buf, s->size)) { + if (!r_file_dump (file, buf, s->size, 0)) { eprintf ("Cannot write '%s'\n", file); free (buf); return R_FALSE; diff --git a/libr/core/cmd_write.c b/libr/core/cmd_write.c index 6a5b83f92f..6929ddb256 100644 --- a/libr/core/cmd_write.c +++ b/libr/core/cmd_write.c @@ -669,31 +669,45 @@ static int cmd_write(void *data, const char *input) { WSEEK (core, len); r_core_block_read (core, 0); break; - case 't': { - st64 sz = core->blocksize; + case 't': // "wt" if (*str == '?') { - eprintf ("Usage: wt file [size]\n"); + eprintf ("Usage: wt[a] file [size] write 'size' bytes in current block to file\n"); free (ostr); return 0; - } else - if (*str != ' ') { - const char* prefix = r_config_get (core->config, "cfg.prefixdump"); - snprintf(_fn, sizeof(_fn), "%s.0x%08"PFMT64x, prefix, core->offset); - filename = _fn; - } else filename = str+1; - tmp = strchr (str+1, ' '); - if (tmp) { - sz = (st64) r_num_math (core->num, tmp+1); - *tmp = 0; - if (sz<1) eprintf ("Invalid length\n"); - else r_core_dump (core, filename, core->offset, (ut64)sz); } else { - if (!r_file_dump (filename, core->block, core->blocksize)) { - sz = 0; - } else sz = core->blocksize; - } - eprintf ("Dumped %"PFMT64d" bytes from 0x%08"PFMT64x" into %s\n", - sz, core->offset, filename); + int append = 0; + st64 sz = core->blocksize; + if (*str=='a') { // "wta" + append = 1; + str++; + if (str[0]==' ') { + filename = str+1; + } else { + const char* prefix = r_config_get (core->config, "cfg.prefixdump"); + snprintf (_fn, sizeof (_fn), "%s.0x%08"PFMT64x, prefix, core->offset); + filename = _fn; + } + } else if (*str != ' ') { + const char* prefix = r_config_get (core->config, "cfg.prefixdump"); + snprintf(_fn, sizeof(_fn), "%s.0x%08"PFMT64x, prefix, core->offset); + filename = _fn; + } else filename = str+1; + tmp = strchr (str+1, ' '); + if (tmp) { + sz = (st64) r_num_math (core->num, tmp+1); + if (!sz) { + sz = core->blocksize; + } + *tmp = 0; + if (sz<1) eprintf ("Invalid length\n"); + else r_core_dump (core, filename, core->offset, (ut64)sz, append); + } else { + if (!r_file_dump (filename, core->block, core->blocksize, append)) { + sz = 0; + } else sz = core->blocksize; + } + eprintf ("Dumped %"PFMT64d" bytes from 0x%08"PFMT64x" into %s\n", + sz, core->offset, filename); } break; case 'f': diff --git a/libr/core/file.c b/libr/core/file.c index f9d3b83d56..3940a64b07 100644 --- a/libr/core/file.c +++ b/libr/core/file.c @@ -224,7 +224,7 @@ R_API char *r_core_sysenv_begin(RCore *core, const char *cmd) { if (strstr (cmd, "BLOCK")) { // replace BLOCK in RET string if ((f = r_file_temp ("r2block"))) { - if (r_file_dump (f, core->block, core->blocksize)) + if (r_file_dump (f, core->block, core->blocksize, 0)) r_sys_setenv ("BLOCK", f); free (f); } diff --git a/libr/core/io.c b/libr/core/io.c index c43f77d314..242a15ad4a 100644 --- a/libr/core/io.c +++ b/libr/core/io.c @@ -60,13 +60,17 @@ R_API int r_core_seek_base (RCore *core, const char *hex) { return r_core_seek (core, addr, 1); } -R_API int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size) { +R_API int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int append) { ut64 i; ut8 *buf; int bs = core->blocksize; FILE *fd; - r_sys_truncate (file, 0); - fd = r_sandbox_fopen (file, "wb"); + if (append) { + fd = r_sandbox_fopen (file, "ab"); + } else { + r_sys_truncate (file, 0); + fd = r_sandbox_fopen (file, "wb"); + } if (!fd) { eprintf ("Cannot open '%s' for writing\n", file); return R_FALSE; diff --git a/libr/core/rtr.c b/libr/core/rtr.c index 94595dde67..17f69e59ed 100644 --- a/libr/core/rtr.c +++ b/libr/core/rtr.c @@ -671,7 +671,7 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { r_config_get (core->config, "http.uproot"), rs->path + 4); eprintf ("UPLOADED '%s'\n", filename); - r_file_dump (filename, ret, retlen); + r_file_dump (filename, ret, retlen, 0); free (filename); snprintf (buf, sizeof (buf), "

uploaded %d bytes. Thanks

\n", retlen); diff --git a/libr/core/vmenus.c b/libr/core/vmenus.c index 43096b7f32..e88eb6dad3 100644 --- a/libr/core/vmenus.c +++ b/libr/core/vmenus.c @@ -1120,7 +1120,7 @@ R_API void r_core_visual_mounts (RCore *core) { r_cons_fgets (buf, sizeof (buf)-1, 0, 0); r_cons_set_raw (1); r_cons_show_cursor (R_FALSE); - r_file_dump (buf, file->data, file->size); + r_file_dump (buf, file->data, file->size, 0); r_fs_close (core->fs, file); r_cons_printf ("Done\n"); } else r_cons_printf ("Cannot dump file\n"); diff --git a/libr/diff/diff.c b/libr/diff/diff.c index 39d93f682a..76c9382a33 100644 --- a/libr/diff/diff.c +++ b/libr/diff/diff.c @@ -79,8 +79,8 @@ R_API int r_diff_buffers_radiff(RDiff *d, const ut8 *a, int la, const ut8 *b, in ooa = oob = 0LL; oop = -1; - r_file_dump (".a", a, la); - r_file_dump (".b", b, lb); + r_file_dump (".a", a, la, 0); + r_file_dump (".b", b, lb, 0); r_sys_cmd ("radiff -d .a .b | rsc uncolor > .d"); fd = fopen (".d", "r"); diff --git a/libr/fs/fs.c b/libr/fs/fs.c index d2aa5b9487..97dde4dc42 100644 --- a/libr/fs/fs.c +++ b/libr/fs/fs.c @@ -282,7 +282,7 @@ R_API int r_fs_dir_dump (RFS* fs, const char *path, const char *name) { item = r_fs_open (fs, npath); if (item) { r_fs_read (fs, item, 0, item->size); - r_file_dump (str, item->data, item->size); + r_file_dump (str, item->data, item->size, 0); free (item->data); r_fs_close (fs, item); } @@ -682,7 +682,7 @@ R_API int r_fs_prompt (RFS *fs, const char *root) { file = r_fs_open (fs, s); if (file) { r_fs_read (fs, file, 0, file->size); - r_file_dump (input, file->data, file->size); + r_file_dump (input, file->data, file->size, 0); free (file->data); r_fs_close (fs, file); } else { diff --git a/libr/include/r_core.h b/libr/include/r_core.h index fd27ba7615..b73d960a68 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -424,7 +424,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 int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int append); R_API void r_core_diff_show(RCore *core, RCore *core2); diff --git a/libr/include/r_util.h b/libr/include/r_util.h index d9ae485440..f64cfd5ab7 100644 --- a/libr/include/r_util.h +++ b/libr/include/r_util.h @@ -495,7 +495,7 @@ R_API char *r_file_slurp_range(const char *str, ut64 off, int sz, int *osz); R_API char *r_file_slurp_random_line(const char *file); R_API char *r_file_slurp_random_line_count(const char *file, int *linecount); R_API ut8 *r_file_slurp_hexpairs(const char *str, int *usz); -R_API boolt r_file_dump(const char *file, const ut8 *buf, int len); +R_API boolt r_file_dump(const char *file, const ut8 *buf, int len, int append); R_API boolt r_file_rm(const char *file); R_API boolt r_file_exists(const char *str); R_API boolt r_file_fexists(const char *fmt, ...); diff --git a/libr/socket/run.c b/libr/socket/run.c index a800cd7ddf..eb98d49ca6 100644 --- a/libr/socket/run.c +++ b/libr/socket/run.c @@ -593,7 +593,9 @@ R_API int r_run_start(RRunProfile *p) { if (p->_pidfile) { char pidstr[32]; snprintf (pidstr, sizeof (pidstr), "%d\n", getpid ()); - r_file_dump (p->_pidfile, (const ut8*)pidstr, strlen (pidstr)); + r_file_dump (p->_pidfile, + (const ut8*)pidstr, + strlen (pidstr), 0); } #endif diff --git a/libr/util/file.c b/libr/util/file.c index 0787c98629..783e272cc1 100644 --- a/libr/util/file.c +++ b/libr/util/file.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2007-2014 - pancake */ +/* radare - LGPL - Copyright 2007-2015 - pancake */ #include "r_types.h" #include "r_util.h" @@ -390,12 +390,19 @@ R_API char *r_file_root(const char *root, const char *path) { return ret; } -R_API boolt r_file_dump(const char *file, const ut8 *buf, int len) { +R_API boolt r_file_dump(const char *file, const ut8 *buf, int len, int append) { int ret; FILE *fd; - if (!file || !*file || !buf) + if (!file || !*file || !buf) { + eprintf ("RET %p, buf %p\n", file, buf); return R_FALSE; - fd = r_sandbox_fopen (file, "wb"); + } + if (append) { + fd = r_sandbox_fopen (file, "awb"); + } else { + r_sys_truncate (file, 0); + fd = r_sandbox_fopen (file, "wb"); + } if (fd == NULL) { eprintf ("Cannot open '%s' for writing\n", file); return R_FALSE;