Initial implementation of the reg:// io plugin ##io

This commit is contained in:
pancake 2022-08-23 23:16:44 +02:00 committed by pancake
parent c89a18067d
commit 9724055874
6 changed files with 143 additions and 1 deletions

View File

@ -226,6 +226,7 @@ io.mmap
io.procpid
io.ptrace
io.rap
io.reg
io.self
io.w32
io.w32dbg

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2017-2021 - condret, pancake */
/* radare2 - LGPL - Copyright 2017-2022 - condret, pancake */
#ifndef R2_IO_H
#define R2_IO_H
@ -588,6 +588,7 @@ extern RIOPlugin r_io_plugin_socket;
extern RIOPlugin r_io_plugin_xattr;
extern RIOPlugin r_io_plugin_isotp;
extern RIOPlugin r_io_plugin_xalz;
extern RIOPlugin r_io_plugin_reg;
#if __cplusplus
}

View File

@ -13,6 +13,7 @@ r_io_sources = [
'undo.c',
'p_cache.c',
'p/io_ar.c',
'p/io_reg.c',
'p/io_fd.c',
'p/io_bfdbg.c',
'p/io_bochs.c',

122
libr/io/p/io_reg.c Normal file
View File

@ -0,0 +1,122 @@
/* radare - LGPL - Copyright 2022 - pancake */
#include <r_userconf.h>
#include <r_io.h>
#include <r_lib.h>
#include <r_reg.h>
#include <r_core.h>
#include <r_cons.h>
static RRegArena *get_arena(RIO *io) {
RCore *core = io->coreb.core;
if (!core) {
return NULL;
}
RRegSet *rs = &core->anal->reg->regset[0];
if (!rs) {
return NULL;
}
RRegArena *arena = rs->arena;
if (!arena) {
return NULL;
}
return arena;
}
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int len) {
RRegArena *arena = get_arena (io);
if (!arena) {
return -1;
}
if (io->off >= arena->size) {
return -1;
}
int left = arena->size - io->off;
memset (buf, 0xff, len);
memcpy (buf, arena->bytes + io->off, R_MIN (left, len));
return len;
}
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int len) {
RRegArena *arena = get_arena (io);
if (!arena) {
return -1;
}
if (io->off >= arena->size) {
return -1;
}
int left = arena->size - io->off;
memcpy (arena->bytes + io->off, buf, R_MIN (left, len));
return len;
}
static bool __plugin_open(RIO *io, const char *file, bool many) {
return r_str_startswith (file, "reg://");
}
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
if (__plugin_open (io, file, 0)) {
RRegArena *arena = get_arena (io);
if (!arena) {
return NULL;
}
return r_io_desc_new (io, &r_io_plugin_reg, file, R_PERM_RW, 0, NULL);
}
return NULL;
}
static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
RRegArena *arena = get_arena (io);
if (!arena) {
return io->off;
}
const int size = arena->size;
switch (whence) {
case SEEK_SET:
if (offset >= size) {
return size;
} else {
return offset;
}
case SEEK_CUR:
return io->off + offset;
case SEEK_END:
return size;
}
return io->off;
}
static bool __close(RIODesc *fd) {
R_FREE (fd->data);
return 0 == 0;
}
static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
if (r_str_startswith (cmd, "pid")) {
io->cb_printf ("%d\n", -1);
}
return NULL;
}
RIOPlugin r_io_plugin_reg = {
.name = "reg",
.desc = "read and write the register arena",
.license = "LGPL3",
.uris = "reg://",
.open = __open,
.close = __close,
.read = __read,
.check = __plugin_open,
.seek = __lseek,
.system = __system,
.write = __write,
};
#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_IO,
.data = &r_io_plugin_reg,
.version = R2_VERSION
};
#endif

16
libr/io/p/reg.mk Normal file
View File

@ -0,0 +1,16 @@
OBJ_REG=io_reg.o
STATIC_OBJ+=${OBJ_REG}
TARGET_REG=io_reg.${EXT_SO}
ALL_TARGETS+=${TARGET_REG}
ifeq (${WITHPIC},0)
LINKFLAGS+=../../util/libr_util.a
LINKFLAGS+=../../io/libr_io.a
else
LINKFLAGS+=-L../../util -lr_util
LINKFLAGS+=-L.. -lr_io
endif
${TARGET_REG}: ${OBJ_REG}
${CC_LIB} $(call libname,io_reg) ${CFLAGS} -o ${TARGET_REG} ${OBJS} ${LINKFLAGS}

View File

@ -359,6 +359,7 @@ fs_plugins += [
io_plugins += [
'ar',
'fd',
'reg',
'bfdbg',
'bochs',
'debug',