pancake wrote:
> Hi
>
> On Jan 15, 2011, at 6:17 PM, Glyn Kennington <[email protected]> wrote:
[...]
> > Another feature that I think might be useful here is a matching
> > r +#
> > syntax to insert bytes, shifting the rest of the file up.
>
> Isnt this already implemented in r1 or ired? In r1 theres also thr eval var
> write.insert that shifts bytes when using the 'w' command. I agree that r+
> must be implemented too.
>
> > I suppose this isn't a commonly-used function on files containing
> > executable code, but it's something I occasionally need when editing
> > data streams. Is there another obvious way of doing this that I've
> > missed? And if not, is it worth me hacking about further and submitting
> > some patches, for either version?
>
> Feel free to submit the patches. I'll ve happy to review them and commit. :)
Attached is support for r+ in radare1. Feel free to flame my coding
style. :-)
Glyn
diff -r 84107891680f src/cmds.c
--- a/src/cmds.c Wed Jan 12 08:28:12 2011 +0100
+++ b/src/cmds.c Sun Jan 16 22:49:08 2011 +0000
@@ -110,7 +110,7 @@
COMMAND('p', "[fmt] [len]", "print print data block", print),
COMMAND('q', "[!]", "quit close radare shell", quit),
COMMAND('P', "[so][i [file]]", "Project project Open, Save, Info",
project),
- COMMAND('r', " [size|-strip]", "resize resize or query the file
size", resize),
+ COMMAND('r', " [[+,-]size]", "resize resize or query the file
size", resize),
COMMAND('g', "[act] ([arg])", "graph graph analysis operations",
graph),
COMMAND('s', " [[+,-]pos]", "seek seek to absolute/relative
expression", seek),
COMMAND('S', "[len] [vaddr]", "Section manage io.vaddr sections",
sections),
diff -r 84107891680f src/radare.c
--- a/src/radare.c Wed Jan 12 08:28:12 2011 +0100
+++ b/src/radare.c Sun Jan 16 22:49:08 2011 +0000
@@ -1702,9 +1702,10 @@
// the readline layer.
if ( arg[0] == '\0' || arg[0] == '?') {
- D cons_printf("Usage: r[?] [#|-#]\n");
- D cons_printf(" positive value means resize\n");
+ D cons_printf("Usage: r[?] [#|-#|+#]\n");
+ D cons_printf(" no sign means resize\n");
D cons_printf(" negative value is used to remove N bytes from
the current seek\n");
+ D cons_printf(" positive value is used to insert N bytes at
the current seek\n");
D cons_printf("size: %lld\n", config.size);
D cons_printf("limit: %lld\n", config.limit);
return;
@@ -1720,22 +1721,29 @@
return;
}
- if (arg[0]=='-') {
+ if (arg[0]=='-' || arg[0]=='+') {
ut64 rest;
- size = -size; // be positive
- D eprintf("stripping %lld bytes\n", size);
- rest = config.size - (config.seek -size);
+ int sign=(arg[0]=='-'); /* 0 when inserting, 1 when removing */
+ size = get_math(arg+1);
+ if (sign && size > config.size - config.seek) {
+ eprintf("Only %lld bytes between seek and end of
file\n",
+ config.size - config.seek);
+ return;
+ }
+ D eprintf("%sing %lld bytes\n", sign?"stripp":"insert", size);
+ rest = config.size - config.seek - (sign?size:0);
if (rest > 0) {
char *str = malloc(rest);
- io_lseek(config.fd, config.seek+size, SEEK_SET);
+ io_lseek(config.fd, config.seek + (sign?size:0),
SEEK_SET);
io_read(config.fd, str, rest);
- io_lseek(config.fd, config.seek, SEEK_SET);
+ io_lseek(config.fd, config.seek + (sign?0:size),
SEEK_SET);
io_write(config.fd, str, rest);
free(str);
io_lseek(config.fd, config.seek, SEEK_SET);
- config.size -= size;
- ftruncate(config.fd, (off_t)config.size);
}
+ config.size += sign?-size:size;
+ ftruncate(config.fd, (off_t)config.size);
+ config.limit=config.size;
return;
}
if (arg[1]=='x') sscanf(arg, OFF_FMTx, &size);
_______________________________________________
radare mailing list
[email protected]
http://lists.nopcode.org/listinfo.cgi/radare-nopcode.org