From 47601c22a5a0b338ca676184d3041d7f8bd14bd1 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 26 Sep 2024 17:44:52 +0200 Subject: [PATCH] Initial import of the rapatch2 tool ##shell --- binr/Makefile | 2 +- binr/meson.build | 1 + binr/rapatch2/Makefile | 22 ++++++++++++++ binr/rapatch2/meson.build | 19 +++++++++++++ binr/rapatch2/rapatch2.c | 7 +++++ libr/core/cmd.c | 3 ++ libr/include/r_main.h | 1 + libr/main/Makefile | 1 + libr/main/main.c | 1 + libr/main/meson.build | 1 + libr/main/rapatch2.c | 60 +++++++++++++++++++++++++++++++++++++++ 11 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 binr/rapatch2/Makefile create mode 100644 binr/rapatch2/meson.build create mode 100644 binr/rapatch2/rapatch2.c create mode 100644 libr/main/rapatch2.c diff --git a/binr/Makefile b/binr/Makefile index 2c26a7e6e04dbe..c64ec1b6b185a5 100644 --- a/binr/Makefile +++ b/binr/Makefile @@ -6,7 +6,7 @@ BTOP=$(shell pwd) .PHONY: all clean install install-symlink deinstall uninstall mrproper preload -BINS=r2r r2pm ravc2 rax2 rasm2 rabin2 rahash2 radiff2 radare2 rafind2 rarun2 ragg2 r2agent rasign2 +BINS=r2r r2pm ravc2 rax2 rasm2 rabin2 rahash2 radiff2 rapatch2 radare2 rafind2 rarun2 ragg2 r2agent rasign2 LIBR2=$(call libname-version,libr2.$(EXT_SO),${LIBVERSION}) diff --git a/binr/meson.build b/binr/meson.build index cf59d7c3db4d0b..4643fe3baa8ec4 100644 --- a/binr/meson.build +++ b/binr/meson.build @@ -17,6 +17,7 @@ if cli_enabled subdir('ragg2') subdir('r2agent') subdir('radiff2') + subdir('rapatch2') subdir('rafind2') subdir('rasign2') subdir('ravc2') diff --git a/binr/rapatch2/Makefile b/binr/rapatch2/Makefile new file mode 100644 index 00000000000000..a1b136448cdf62 --- /dev/null +++ b/binr/rapatch2/Makefile @@ -0,0 +1,22 @@ +BIN=rapatch2 + +BINDEPS=r_core r_search r_cons r_config +BINDEPS+=r_bin r_debug r_anal r_reg r_bp r_io r_fs +BINDEPS+=r_lang r_asm r_syscall r_main r_util r_esil +BINDEPS+=r_magic r_socket r_flag r_egg r_crypto + +include ../rules.mk + +ifeq ($(OSTYPE),android) +LDFLAGS+=${DL_LIBS} -lm +endif + +include ../../libr/socket/deps.mk +include ../../libr/main/deps.mk +include ../../shlr/zip/deps.mk +include ../../shlr/gdb/deps.mk +include ../../shlr/bochs/deps.mk +include ../../shlr/qnx/deps.mk +include ../../shlr/ar/deps.mk + +LDFLAGS+=$(LINK) diff --git a/binr/rapatch2/meson.build b/binr/rapatch2/meson.build new file mode 100644 index 00000000000000..f5bd1a30fde3bd --- /dev/null +++ b/binr/rapatch2/meson.build @@ -0,0 +1,19 @@ +executable('rapatch2', 'rapatch2.c', + include_directories: [platform_inc], + dependencies: [ + r_util_dep, + r_main_dep, + r_io_dep, + r_search_dep, + r_cons_dep, + r_core_dep, + r_bin_dep, + r_anal_dep, + r_asm_dep, + r_crypto_dep, + r_config_dep + ], + install: true, + install_rpath: rpath_exe, + implicit_include_directories: false +) diff --git a/binr/rapatch2/rapatch2.c b/binr/rapatch2/rapatch2.c new file mode 100644 index 00000000000000..b0986874b07a1d --- /dev/null +++ b/binr/rapatch2/rapatch2.c @@ -0,0 +1,7 @@ +/* radare - LGPL - Copyright 2024 - pancake */ + +#include + +int main (int argc, const char *argv[]) { + return r_main_rapatch2 (argc, argv); +} diff --git a/libr/core/cmd.c b/libr/core/cmd.c index e6e78c68e10b95..eb8fe2289658be 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -2862,6 +2862,9 @@ static bool cmd_r2cmd(RCore *core, const char *_input) { int rc = 0; if (r_str_startswith (input, "rax2")) { rc = __runMain (core->r_main_rax2, input); + } else if (r_str_startswith (input, "rapatch2")) { + r_sys_cmdf ("%s", input); + // rc = __runMain (r_main_rapatch2, input); } else if (r_str_startswith (input, "radare2")) { r_sys_cmdf ("%s", input); // rc = __runMain (core->r_main_radare2, input); diff --git a/libr/include/r_main.h b/libr/include/r_main.h index e823c2e5ec2978..cf46f8cb84f74d 100644 --- a/libr/include/r_main.h +++ b/libr/include/r_main.h @@ -39,6 +39,7 @@ R_API int r_main_radiff2(int argc, const char **argv); R_API int r_main_ragg2(int argc, const char **argv); R_API int r_main_rasign2(int argc, const char **argv); R_API int r_main_r2pm(int argc, const char **argv); +R_API int r_main_rapatch2(int argc, const char **argv); #ifdef __cplusplus } diff --git a/libr/main/Makefile b/libr/main/Makefile index 2cad3afbe82f1f..672ecec60bf443 100644 --- a/libr/main/Makefile +++ b/libr/main/Makefile @@ -16,6 +16,7 @@ OBJS+=rasign2.o OBJS+=rafind2.o OBJS+=r2agent.o OBJS+=radiff2.o +OBJS+=rapatch2.o OBJS+=radare2.o OBJS+=rahash2.o diff --git a/libr/main/main.c b/libr/main/main.c index 3d0a2fab1fb6eb..c615654f61ec41 100644 --- a/libr/main/main.c +++ b/libr/main/main.c @@ -13,6 +13,7 @@ static RMain foo[] = { { "rarun2", r_main_rarun2 }, { "rasm2", r_main_rasm2 }, { "ragg2", r_main_ragg2 }, + { "rapatch2", r_main_rapatch2 }, { "rabin2", r_main_rabin2 }, { "radare2", r_main_radare2 }, { "r2", r_main_radare2 }, diff --git a/libr/main/meson.build b/libr/main/meson.build index d571bded5499f3..c4bee0a3b1c8aa 100644 --- a/libr/main/meson.build +++ b/libr/main/meson.build @@ -8,6 +8,7 @@ r_main_sources = [ 'rafind2.c', 'ragg2.c', 'rahash2.c', + 'rapatch2.c', 'rarun2.c', 'rasign2.c', 'rasm2.c', diff --git a/libr/main/rapatch2.c b/libr/main/rapatch2.c new file mode 100644 index 00000000000000..01f17bd90a15d2 --- /dev/null +++ b/libr/main/rapatch2.c @@ -0,0 +1,60 @@ +/* radare - LGPL - Copyright 2024 - pancake */ + +#define R_LOG_ORIGIN "rapatch2" + +#include +#include + +static int show_help(int v) { + printf ("Usage: rapatch2 [-R] [origfile] ([patchfile])\n"); + if (v) { + printf ( + " -R reverse patch\n" + ); + } + return 1; +} + +R_API int r_main_rapatch2(int argc, const char **argv) { + RGetopt opt; + int o; + + bool reverse = false; + + r_getopt_init (&opt, argc, argv, "hR"); + while ((o = r_getopt_next (&opt)) != -1) { + switch (o) { + case 'h': + return show_help (1); + case 'R': + reverse = true; + break; + default: + return show_help (0); + } + } + + if (argc < 3 || opt.ind + 2 > argc) { + return show_help (0); + } + const char *file = (opt.ind < argc)? argv[opt.ind]: NULL; + const char *patchfile = (opt.ind + 1 < argc)? argv[opt.ind + 1]: NULL; + + if (R_STR_ISEMPTY (file) || R_STR_ISEMPTY (patchfile)) { + R_LOG_ERROR ("Cannot open empty path"); + return 1; + } + if (reverse) { + R_LOG_TODO ("reverse patch not yet supported"); + } + const char *r2argv[5] = { + "radare2", + "-qwP", + patchfile, + file, + NULL + }; + r_main_radare2 (5, r2argv); + + return 0; +}