Skip to content

Commit

Permalink
Initial implementation of r_io.zip plugin
Browse files Browse the repository at this point in the history
Allows to: r2 zip://foo.apk//classes.dex
Honor LDFLAGS in r_bin
Fix filename issue in r_io_redirect
Add testing on-activate event in r2w2
  • Loading branch information
radare committed Sep 7, 2012
1 parent 61fc7e5 commit 3861772
Show file tree
Hide file tree
Showing 27 changed files with 134 additions and 37 deletions.
2 changes: 1 addition & 1 deletion libr/bin/p/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ../../config.mk

CFLAGS=-I$(LTOP)/include -I$(LTOP)/bin/format/ -Wall -fPIC
CFLAGS+=-L$(LTOP)/util -lr_util
LDFLAGS+=-L$(LTOP)/util -lr_util

foo: all

Expand Down
2 changes: 1 addition & 1 deletion libr/bin/p/any.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_ANY=bin_any.${EXT_SO}
ALL_TARGETS+=${TARGET_ANY}

${TARGET_ANY}: ${OBJ_ANY}
${CC} $(call libname,bin_any) ${CFLAGS} ${OBJ_ANY}
${CC} $(call libname,bin_any) ${CFLAGS} $(LDFLAGS) ${OBJ_ANY}
1 change: 1 addition & 0 deletions libr/bin/p/bin_meta_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <r_types.h>
#include <r_bin.h>

// TODO: use proper dwarf api here.. or deprecate
static int get_line(RBinArch *arch, ut64 addr, char *file, int len, int *line) {
char *p, *out = r_sys_cmd_strf ("addr2line -e '%s' 0x%08"PFMT64x"", arch->file, addr);
if (out == NULL || *out=='?')
Expand Down
2 changes: 1 addition & 1 deletion libr/bin/p/dex.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_DEX=bin_dex.${EXT_SO}
ALL_TARGETS+=${TARGET_DEX}

${TARGET_DEX}: ${OBJ_DEX}
${CC} $(call libname,bin_dex) -shared ${CFLAGS} -o ${TARGET_DEX} ${OBJ_DEX}
${CC} $(call libname,bin_dex) -shared ${CFLAGS} -o ${TARGET_DEX} ${OBJ_DEX} $(LDFLAGS)
2 changes: 1 addition & 1 deletion libr/bin/p/dyldcache.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_DYLDCACHE=bin_xtr_dyldcache.${EXT_SO}
ALL_TARGETS+=${TARGET_DYLDCACHE}

${TARGET_DYLDCACHE}: ${OBJ_DYLDCACHE}
${CC} $(call libname,bin_xtr_dyldcache) -shared ${CFLAGS} -o ${TARGET_DYLDCACHE} ${OBJ_DYLDCACHE}
${CC} $(call libname,bin_xtr_dyldcache) -shared ${CFLAGS} -o ${TARGET_DYLDCACHE} ${OBJ_DYLDCACHE} $(LDFLAGS)
2 changes: 1 addition & 1 deletion libr/bin/p/elf.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ TARGET_ELF=bin_elf.${EXT_SO}
ALL_TARGETS+=${TARGET_ELF}

${TARGET_ELF}: ${OBJ_ELF}
${CC} $(call libname,bin_elf) ${CFLAGS} ${OBJ_ELF}
${CC} $(call libname,bin_elf) ${CFLAGS} $(LDFLAGS) ${OBJ_ELF}
2 changes: 1 addition & 1 deletion libr/bin/p/elf64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ TARGET_ELF64=bin_elf64.${EXT_SO}
ALL_TARGETS+=${TARGET_ELF64}

${TARGET_ELF64}: ${OBJ_ELF64}
${CC} $(call libname,bin_elf64) -shared ${CFLAGS} ${OBJ_ELF64}
${CC} $(call libname,bin_elf64) -shared ${CFLAGS} $(LDFLAGS) ${OBJ_ELF64}
2 changes: 1 addition & 1 deletion libr/bin/p/fatmach0.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_FATMACH0=bin_xtr_fatmach0.${EXT_SO}
ALL_TARGETS+=${TARGET_FATMACH0}

${TARGET_FATMACH0}: ${OBJ_FATMACH0}
${CC} $(call libname,bin_xtr_fatmach0) -shared ${CFLAGS} -o ${TARGET_FATMACH0} ${OBJ_FATMACH0}
${CC} $(call libname,bin_xtr_fatmach0) -shared ${CFLAGS} -o ${TARGET_FATMACH0} ${OBJ_FATMACH0} $(LDFLAGS)
2 changes: 1 addition & 1 deletion libr/bin/p/fs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_FS=bin_fs.${EXT_SO}
ALL_TARGETS+=${TARGET_FS}

${TARGET_FS}: ${OBJ_FS}
${CC} $(call libname,bin_fs) -shared ${CFLAGS} -o ${TARGET_FS} ${OBJ_FS}
${CC} $(call libname,bin_fs) -shared ${CFLAGS} -o ${TARGET_FS} ${OBJ_FS} $(LDFLAGS)
2 changes: 1 addition & 1 deletion libr/bin/p/java.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_JAVA=bin_java.${EXT_SO}
ALL_TARGETS+=${TARGET_JAVA}

${TARGET_JAVA}: ${OBJ_JAVA}
${CC} $(call libname,bin_java) ${CFLAGS} ${OBJ_JAVA}
${CC} $(call libname,bin_java) ${CFLAGS} ${OBJ_JAVA} $(LDFLAGS)
2 changes: 1 addition & 1 deletion libr/bin/p/mach0.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_MACH0=bin_mach0.${EXT_SO}
ALL_TARGETS+=${TARGET_MACH0}

${TARGET_MACH0}: ${OBJ_MACH0}
${CC} $(call libname,bin_mach0) ${CFLAGS} ${OBJ_MACH0}
${CC} $(call libname,bin_mach0) ${CFLAGS} $(LDFLAGS) ${OBJ_MACH0}
2 changes: 1 addition & 1 deletion libr/bin/p/mach064.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_MACH064=bin_mach064.${EXT_SO}
ALL_TARGETS+=${TARGET_MACH064}

${TARGET_MACH064}: ${OBJ_MACH064}
${CC} $(call libname,bin_mach064) -shared ${CFLAGS} -o ${TARGET_MACH064} ${OBJ_MACH064}
${CC} $(call libname,bin_mach064) -shared ${CFLAGS} -o ${TARGET_MACH064} ${OBJ_MACH064} $(LDFLAGS)
2 changes: 1 addition & 1 deletion libr/bin/p/pe.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_PE=bin_pe.${EXT_SO}
ALL_TARGETS+=${TARGET_PE}

${TARGET_PE}: ${OBJ_PE}
${CC} $(call libname,bin_pe) ${CFLAGS} ${OBJ_PE}
${CC} $(call libname,bin_pe) ${CFLAGS} $(LDFLAGS) ${OBJ_PE}
2 changes: 1 addition & 1 deletion libr/bin/p/pe64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TARGET_PE64=bin_pe64.${EXT_SO}
ALL_TARGETS+=${TARGET_PE64}

${TARGET_PE64}: ${OBJ_PE64}
${CC} $(call libname,bin_pe64) ${CFLAGS} ${OBJ_PE64}
${CC} $(call libname,bin_pe64) ${CFLAGS} $(LDFLAGS) ${OBJ_PE64}
13 changes: 12 additions & 1 deletion libr/core/cmd_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,21 @@ static int cmd_open(void *data, const char *input) {
case 'o':
r_core_file_reopen (core, input+2);
break;
case 'c':
// memleak? lose all settings wtf
// if load fails does not fallbacks to previous file
r_core_fini (core);
r_core_init (core);
if (!r_core_file_open (core, input+2, R_IO_READ, 0))
eprintf ("Cannot open file\n");
if (!r_core_bin_load (core, NULL))
r_config_set (core->config, "io.va", "false");
break;
case '?':
default:
eprintf ("Usage: o[o-] [file] ([offset])\n"
eprintf ("Usage: o[com- ] [file] ([offset])\n"
" o list opened files\n"
" oc [ƒile] open core file, like relaunching r2\n"
" oo reopen current file (kill+fork in debugger)\n"
" o 4 priorize io on fd 4 (bring to front)\n"
" o-1 close file index 1\n"
Expand Down
4 changes: 2 additions & 2 deletions libr/core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,8 @@ R_API int r_core_config_init(RCore *core) {
r_config_desc (cfg, "cmd.vprompt", "Visual prompt commands");
r_config_set (cfg, "cmd.bp", "");
r_config_desc (cfg, "cmd.bp", "Command to executed every breakpoint hitted");
r_config_set (cfg, "http.local", "true");
r_config_desc (cfg, "http.local", "set to false to listen on 0.0.0.0");
r_config_set (cfg, "http.public", "false");
r_config_desc (cfg, "http.public", "set to true to listen on 0.0.0.0");
#if __WINDOWS__
r_config_set (cfg, "http.browser", "start");
#else
Expand Down
4 changes: 2 additions & 2 deletions libr/core/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int mode, ut64 loa
fh = R_NEW (RCoreFile);
fh->fd = fd;
fh->map = NULL;
fh->uri = strdup (file);
fh->size = r_file_size (file);
fh->uri = strdup (fd->name);
fh->size = r_file_size (fh->uri);
if (!fh->size)
fh->size = r_io_size (r->io);
fh->filename = strdup (fh->uri);
Expand Down
2 changes: 1 addition & 1 deletion libr/core/rtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ R_API int r_core_rtr_http(RCore *core, int launch) {
int z = r_config_get_i (core->config, "asm.bytes");
const char *port = r_config_get (core->config, "http.port");
s = r_socket_new (R_FALSE);
s->local = r_config_get_i (core->config, "http.local");
s->local = !r_config_get_i (core->config, "http.public");
if (!r_socket_listen (s, port, NULL)) {
eprintf ("Cannot listen on http.port\n");
return 1;
Expand Down
1 change: 1 addition & 0 deletions libr/include/r_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ extern RBinPlugin r_bin_plugin_mach064;
extern RBinPlugin r_bin_plugin_java;
extern RBinPlugin r_bin_plugin_dex;
extern RBinPlugin r_bin_plugin_dummy;
extern RBinXtrPlugin r_bin_xtr_plugin_zip;
extern RBinXtrPlugin r_bin_xtr_plugin_fatmach0;
extern RBinXtrPlugin r_bin_xtr_plugin_dyldcache;
#endif
Expand Down
1 change: 1 addition & 0 deletions libr/include/r_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ extern RIOPlugin r_io_plugin_haret;
extern RIOPlugin r_io_plugin_bfdbg;
extern RIOPlugin r_io_plugin_w32;
extern RIOPlugin r_io_plugin_ewf;
extern RIOPlugin r_io_plugin_zip;
#endif

#if 0
Expand Down
17 changes: 9 additions & 8 deletions libr/io/io.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2012 pancake */
/* radare - LGPL - Copyright 2008-2012 - pancake */

#include "r_io.h"
#include "r_util.h"
Expand Down Expand Up @@ -83,7 +83,7 @@ R_API RIODesc *r_io_open_as(struct r_io_t *io, const char *urihandler, const cha
return ret;
}

R_API RIODesc *r_io_open(struct r_io_t *io, const char *file, int flags, int mode) {
R_API RIODesc *r_io_open(RIO *io, const char *file, int flags, int mode) {
RIODesc *desc = NULL;
int fd = -2;
char *uri = strdup (file);
Expand Down Expand Up @@ -113,18 +113,19 @@ R_API RIODesc *r_io_open(struct r_io_t *io, const char *file, int flags, int mod
if (fd == -2) {
#if __WINDOWS__
if (flags & R_IO_WRITE) {
fd = open (file, O_BINARY | 1);
fd = open (uri, O_BINARY | 1);
if (fd == -1)
creat (file, O_BINARY);
fd = open (file, O_BINARY | 1);
} else fd = open (file, O_BINARY);
creat (uri, O_BINARY);
fd = open (uri, O_BINARY | 1);
} else fd = open (uri, O_BINARY);
#else
fd = open (file, (flags&R_IO_WRITE)?O_RDWR:O_RDONLY, mode);
fd = open (uri, (flags&R_IO_WRITE)?O_RDWR:O_RDONLY, mode);
#endif
}
if (fd >= 0) {
if (desc == NULL)
desc = r_io_desc_new (io->plugin, fd, file, flags, mode, NULL);
desc = r_io_desc_new (io->plugin,
fd, uri, flags, mode, NULL);
r_io_desc_add (io, desc);
r_io_set_fd (io, desc);
}
Expand Down
52 changes: 52 additions & 0 deletions libr/io/p/io_zip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* radare - LGPL - Copyright 2012 - pancake */

#include <r_io.h>
#include <r_lib.h>
#include <r_util.h>

static int __plugin_open(RIO *io, const char *file) {
if (!memcmp (file, "zip://", 6) && file[6])
return R_TRUE;
return R_FALSE;
}

static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
char *p, *str, cmd[1024];
if (__plugin_open (io, file)) {
str = strdup (file+6);
p = (char *)r_str_casestr (str, "//");
if (p) {
*p++ = 0;
// TODO: escape quotes
// TODO: add support for directories
snprintf (cmd, sizeof (cmd), "unzip -o '%s' '%s'", str, p+1);
if (system (cmd) == 0)
r_io_redirect (io, p+1);
} else {
snprintf (cmd, sizeof (cmd), "unzip -l '%s' |grep -e '[0-9][0-9]-[0-9][0-9]'| awk '{print $4}'", str);
if (system (cmd) != 0)
eprintf ("Use zip://<path-to-zip>//<path-inside-zip>\n");
}
free (str);
return NULL;
}
r_io_redirect (io, NULL);
return NULL;
}

struct r_io_plugin_t r_io_plugin_zip = {
.name = "zip",
.desc = "Open files inside zip archives zip://whatsapp.apk/classes.dex",
.open = __open,
.plugin_open = __plugin_open,
.lseek = NULL,
.system = NULL,
.debug = (void *)1,
};

#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_IO,
.data = &r_io_plugin_zip
};
#endif
18 changes: 18 additions & 0 deletions libr/io/p/zip.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
OBJ_ZIP=io_zip.o

STATIC_OBJ+=${OBJ_ZIP}
TARGET_ZIP=io_zip.${EXT_SO}
ALL_TARGETS+=${TARGET_ZIP}

ifeq (${WITHPIC},0)
LINKFLAGS+=../../util/libr_util.a
LINKFLAGS+=../../lib/libr_lib.a
LINKFLAGS+=../../io/libr_io.a
else
LINKFLAGS+=-L../../lib -lr_lib
LINKFLAGS+=-L../../util -lr_util
LINKFLAGS+=-L.. -L../../lib -lr_lib -lr_io
endif

${TARGET_ZIP}: ${OBJ_ZIP}
${CC_LIB} $(call libname,io_zip) ${CFLAGS} -o ${TARGET_ZIP} ${OBJ_ZIP} ${LINKFLAGS}
2 changes: 1 addition & 1 deletion libr/io/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ R_API RIOPlugin *r_io_plugin_resolve(RIO *io, const char *filename) {
continue;
if (il->plugin->plugin_open == NULL)
continue;
if (il->plugin->plugin_open(io, filename))
if (il->plugin->plugin_open (io, filename))
return il->plugin;
}
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion libr/util/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ R_API char *r_file_tmpdir() {
char *path = r_sys_getenv ("TEMP");
if (!path) path = strdup ("C:\\WINDOWS\\Temp\\");
#elif __ANDROID__
char *path = strdup ("/data/local/tmp");
char *path = strdup ("/data/data/org.radare.installer/radare2/tmp");
#else
char *path = r_sys_getenv ("TMPDIR");
if (!path) path = strdup ("/tmp");
Expand Down
1 change: 1 addition & 0 deletions plugins.def.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fs.ufs
fs.posix
fs.jfs
fs.minix
io.zip
fs.fb
fs.sfs
io.debug
Expand Down
27 changes: 19 additions & 8 deletions shlr/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@
function handleKeyPress(e, form) {
var key = e.keyCode || e.which;
if (key==13) {
var txt = document.getElementById('input').value;
if (txt.length == 0) {
show_popup_commands ();
} else Ajax ('GET', '/cmd/'+txt, '', function (x) {
document.getElementById('output').innerHTML = x;
document.getElementById('input').value = '';
});
input_activate ();
}
}

function input_activate () {
var txt = document.getElementById('input').value;
if (txt.length == 0) show_popup_commands (); else
Ajax ('GET', '/cmd/'+txt, '', function (x) {
document.getElementById('output').innerHTML = x;
document.getElementById('input').value = '';
});
}

function css(selector, property, value) {
for (var i=0; i<document.styleSheets.length;i++) {
try {
Expand Down Expand Up @@ -149,9 +153,16 @@
}
}

function init() {
var input = document.getElementById ('input');
input.addEventListener ("activate", function (x) {
input_activate();
}, false);
}

</script>
</head>
<body>
<body onload="init()">
<div class=popup_background id="popup_background"></div>
<div class=popup id="popup">
<table>
Expand Down

0 comments on commit 3861772

Please sign in to comment.