diff --git a/dist/plugins-cfg/plugins.def.cfg b/dist/plugins-cfg/plugins.def.cfg index e2184190fb..f8a4df564b 100644 --- a/dist/plugins-cfg/plugins.def.cfg +++ b/dist/plugins-cfg/plugins.def.cfg @@ -25,6 +25,7 @@ anal.java anal.kvx anal.lh5801 anal.m68k_cs +anal.mcs96 anal.m680x_cs anal.xtensa anal.malbolge @@ -95,7 +96,6 @@ asm.x86_cs asm.x86_nz asm.x86_nasm asm.vasm -asm.mcs96 bin.any bin.tic bin.wasm diff --git a/dist/plugins-cfg/plugins.mingw.cfg b/dist/plugins-cfg/plugins.mingw.cfg index 7cec5c8542..7db90fa33a 100644 --- a/dist/plugins-cfg/plugins.mingw.cfg +++ b/dist/plugins-cfg/plugins.mingw.cfg @@ -23,6 +23,7 @@ anal.m680x_cs anal.malbolge anal.mcore anal.mips_cs +anal.mcs96 anal.mips_gnu anal.msp430 anal.nios2 @@ -87,7 +88,6 @@ asm.x86_cs asm.x86_nz asm.x86_nasm asm.vasm -asm.mcs96 bin.any bin.tic bin.wasm diff --git a/dist/plugins-cfg/plugins.nocs.cfg b/dist/plugins-cfg/plugins.nocs.cfg index 172deb1673..60ebcc2538 100644 --- a/dist/plugins-cfg/plugins.nocs.cfg +++ b/dist/plugins-cfg/plugins.nocs.cfg @@ -26,6 +26,7 @@ anal.nios2 anal.null anal.or1k anal.ppc_gnu +anal.mcs96 anal.s390_gnu anal.lm32 anal.sh @@ -70,7 +71,6 @@ asm.arm_as asm.x86_as asm.x86_nz asm.x86_nasm -asm.mcs96 bin.any bin.wasm bin.nro diff --git a/dist/plugins-cfg/plugins.static.cfg b/dist/plugins-cfg/plugins.static.cfg index 464a102473..7861402ec7 100644 --- a/dist/plugins-cfg/plugins.static.cfg +++ b/dist/plugins-cfg/plugins.static.cfg @@ -10,6 +10,7 @@ anal.chip8 anal.cris anal.dalvik anal.gb +anal.mcs96 anal.i8080 anal.java anal.kvx @@ -61,7 +62,6 @@ asm.x86_as asm.x86_cs asm.x86_nz asm.x86_nasm -asm.mcs96 bin.any bin.nro bin.nso diff --git a/dist/plugins-cfg/plugins.static.nogpl.cfg b/dist/plugins-cfg/plugins.static.nogpl.cfg index 34098accf2..892b4a9172 100644 --- a/dist/plugins-cfg/plugins.static.nogpl.cfg +++ b/dist/plugins-cfg/plugins.static.nogpl.cfg @@ -13,6 +13,7 @@ anal.java anal.m68k_cs anal.malbolge anal.mips_cs +anal.mcs96 anal.nios2 anal.null anal.sh @@ -42,7 +43,6 @@ asm.x86_as asm.x86_cs asm.x86_nz asm.x86_nasm -asm.mcs96 bin.any bin.nro bin.nso diff --git a/dist/plugins-cfg/plugins.termux.cfg b/dist/plugins-cfg/plugins.termux.cfg index 7d6b4c620c..27548758a4 100644 --- a/dist/plugins-cfg/plugins.termux.cfg +++ b/dist/plugins-cfg/plugins.termux.cfg @@ -12,6 +12,7 @@ anal.mips_gnu anal.null anal.pdp11_gnu anal.ppc_cs +anal.mcs96 anal.ppc_gnu anal.s390_gnu anal.s390_cs @@ -59,7 +60,6 @@ asm.x86_cs asm.x86_nz asm.x86_nasm asm.vasm -asm.mcs96 bin.any bin.tic bin.wasm diff --git a/libr/asm/arch/mcs96/mcs96.h b/libr/anal/arch/mcs96/mcs96.h similarity index 98% rename from libr/asm/arch/mcs96/mcs96.h rename to libr/anal/arch/mcs96/mcs96.h index b7f21ca05d..3a1914e146 100644 --- a/libr/asm/arch/mcs96/mcs96.h +++ b/libr/anal/arch/mcs96/mcs96.h @@ -1,5 +1,4 @@ #include -#include typedef struct mcs96_op_t { const char *ins; @@ -26,7 +25,7 @@ typedef struct mcs96_op_t { #define MCS96_FE 0x2000 //0xfe extension -static Mcs96Op mcs96_op[] = { +static const Mcs96Op mcs96_op[] = { {"skip", MCS96_1B}, {"invalid", MCS96_1B}, {"invalid", MCS96_1B}, @@ -286,5 +285,5 @@ static Mcs96Op mcs96_op[] = { {"rst", MCS96_1B} }; -static const char *mcs96_fe_op[] = { "mul", "mulb", "mul", "mulb", "div", "divb", "invalid", "invalid" }; +static const char * const mcs96_fe_op[] = { "mul", "mulb", "mul", "mulb", "div", "divb", "invalid", "invalid" }; // in theory these invalids can never happen diff --git a/libr/anal/meson.build b/libr/anal/meson.build index 3638a92550..c300beab67 100644 --- a/libr/anal/meson.build +++ b/libr/anal/meson.build @@ -54,6 +54,7 @@ r_anal_sources = [ join_paths('p','anal_arm_gnu.c'), join_paths('p','anal_avr.c'), join_paths('p','anal_bf.c'), + join_paths('p','anal_mcs96.c'), join_paths('p','anal_bpf.c'), join_paths('p','anal_bpf_cs.c'), join_paths('p','anal_chip8.c'), diff --git a/libr/asm/p/asm_mcs96.c b/libr/anal/p/anal_mcs96.c similarity index 63% rename from libr/asm/p/asm_mcs96.c rename to libr/anal/p/anal_mcs96.c index 370c0c216a..34bdfc3ad0 100644 --- a/libr/asm/p/asm_mcs96.c +++ b/libr/anal/p/anal_mcs96.c @@ -1,12 +1,10 @@ -/* radare - LGPL - Copyright - 2015-2019 - condret */ +/* radare - LGPL - Copyright - 2015-2022 - condret */ -#include -#include #include #include #include "../arch/mcs96/mcs96.h" -static int mcs96_len(const ut8 *buf, int len, RStrBuf *asm_buf) { +static int mcs96_len(const ut8 *buf, int len, RAnalOp *op) { int ret = 1; if (buf[0] == 0xfe) { if (len < 2) { @@ -45,10 +43,11 @@ static int mcs96_len(const ut8 *buf, int len, RStrBuf *asm_buf) { } if (ret <= len) { const ut32 fe_idx = ((buf[1] & 0x70) >> 4) ^ 0x4; - r_strbuf_set (asm_buf, mcs96_fe_op[fe_idx]); - if ((mcs96_op[buf[1]].type & (MCS96_2OP | MCS96_REG_8)) == (MCS96_2OP | MCS96_REG_8) && - buf[2] > 0x19 && buf[3] > 0x19) { - r_strbuf_appendf(asm_buf, " rb%02x, rb%02x", buf[2] - 0x1a, buf[3] - 0x1a); + if ((mcs96_op[buf[1]].type & (MCS96_2OP | MCS96_REG_8)) == (MCS96_2OP | MCS96_REG_8) && buf[2] > 0x19 && buf[3] > 0x19) { + op->mnemonic = r_str_newf ("%s rb%02x, rb%02x", + mcs96_fe_op[fe_idx], buf[2] - 0x1a, buf[3] - 0x1a); + } else { + op->mnemonic = strdup (mcs96_fe_op[fe_idx]); } } else { ret = 0; @@ -87,10 +86,11 @@ static int mcs96_len(const ut8 *buf, int len, RStrBuf *asm_buf) { ret = 2; } if (ret <= len) { - r_strbuf_set (asm_buf, mcs96_op[buf[0]].ins); - if ((mcs96_op[buf[0]].type & (MCS96_2OP | MCS96_REG_8)) == (MCS96_2OP | MCS96_REG_8) && - buf[1] > 0x19 && buf[2] > 0x19) { - r_strbuf_appendf(asm_buf, " rb%02x, rb%02x", buf[1] - 0x1a, buf[2] - 0x1a); + const char *opstr = mcs96_op[buf[0]].ins; + if ((mcs96_op[buf[0]].type & (MCS96_2OP | MCS96_REG_8)) == (MCS96_2OP | MCS96_REG_8) && buf[1] > 0x19 && buf[2] > 0x19) { + op->mnemonic = r_str_newf ("%s rb%02x, rb%02x", opstr, buf[1] - 0x1a, buf[2] - 0x1a); + } else { + op->mnemonic = strdup (opstr); } } else { ret = 0; @@ -98,28 +98,38 @@ static int mcs96_len(const ut8 *buf, int len, RStrBuf *asm_buf) { return ret; } -static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { +static int disassemble(RAnal *a, RAnalOp *op, const ut8 *buf, int len) { if (len > 1 && !memcmp (buf, "\xff\xff", 2)) { return -1; } - op->size = mcs96_len (buf, len, &op->buf_asm); + op->size = mcs96_len (buf, len, op); return op->size; } -RAsmPlugin r_asm_plugin_mcs96 = { +static int _op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len, RAnalOpMask mask) { + int ilen = disassemble (anal, op, data, len); + if (mask & R_ANAL_OP_MASK_DISASM) { + // do nothing + } + return ilen; +} + +RAnalPlugin r_anal_plugin_mcs96 = { .name = "mcs96", - .desc = "condrets car", + .desc = "Intel MCS96 microcontroller, also known as 8xC196 or 80196", .arch = "mcs96", .license = "LGPL3", + .op = &_op, + .author = "condret", .bits = 16, .endian = R_SYS_ENDIAN_NONE, - .disassemble = &disassemble + // .disassemble = &disassemble }; #ifndef R2_PLUGIN_INCORE R_API RLibStruct radare_plugin = { - .type = R_LIB_TYPE_ASM, - .data = &r_asm_plugin_mcs96, + .type = R_LIB_TYPE_ANAL, + .data = &r_anal_plugin_mcs96, .version = R2_VERSION }; #endif diff --git a/libr/anal/p/mcs96.mk b/libr/anal/p/mcs96.mk new file mode 100644 index 0000000000..35bb486c7a --- /dev/null +++ b/libr/anal/p/mcs96.mk @@ -0,0 +1,11 @@ +OBJ_MCS96=anal_mcs96.o + +STATIC_OBJ+=$(OBJ_MCS96) +TARGET_MCS96=anal_mcs96.$(EXT_SO) + +ifeq ($(WITHPIC),1) +ALL_TARGETS+=$(TARGET_MCS96) + +$(TARGET_MCS96): $(OBJ_MCS96) + $(CC) $(call libname,anal_mcs96) $(CFLAGS) -o $(TARGET_MCS96) $(OBJ_MCS96) +endif diff --git a/libr/asm/meson.build b/libr/asm/meson.build index ea2a4d67c4..267fea0757 100644 --- a/libr/asm/meson.build +++ b/libr/asm/meson.build @@ -21,7 +21,6 @@ r_asm_sources = [ join_paths('p','asm_lanai_gnu.c'), join_paths('p','asm_m68k_gnu.c'), join_paths('p','asm_m680x_cs.c'), - join_paths('p','asm_mcs96.c'), join_paths('p','asm_mips_cs.c'), join_paths('p','asm_mips_gnu.c'), join_paths('p','asm_or1k.c'), diff --git a/libr/asm/p/mcs96.mk b/libr/asm/p/mcs96.mk deleted file mode 100644 index 62083119f4..0000000000 --- a/libr/asm/p/mcs96.mk +++ /dev/null @@ -1,11 +0,0 @@ -OBJ_MCS96=asm_mcs96.o - -STATIC_OBJ+=${OBJ_MCS96} -TARGET_MCS96=asm_mcs96.${EXT_SO} - -ifeq ($(WITHPIC),1) -ALL_TARGETS+=${TARGET_MCS96} - -${TARGET_MCS96}: ${OBJ_MCS96} - ${CC} ${call libname,asm_mcs96} ${CFLAGS} -o ${TARGET_MCS96} ${OBJ_MCS96} -endif diff --git a/libr/cons/cpipe.c b/libr/cons/cpipe.c index a7049fd48b..169f197936 100644 --- a/libr/cons/cpipe.c +++ b/libr/cons/cpipe.c @@ -35,7 +35,7 @@ R_API int r_cons_pipe_open(const char *file, int fdn, int append) { if (fdn < 1) { return -1; } - char *targetFile = (!strncmp (file, "~/", 2) || !strncmp (file, "~\\", 2)) + char *targetFile = (r_str_startswith (file, "~/") || r_str_startswith (file, "~\\")) ? r_str_home (file + 2): strdup (file); const int fd_flags = O_BINARY | O_RDWR | O_CREAT | (append? O_APPEND: O_TRUNC); int fd = r_sandbox_open (targetFile, fd_flags, 0644); diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 63725adb4e..7d39e72ac3 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -224,11 +224,6 @@ static int r_line_readchar_utf8(ut8 *s, int slen) { return -1; } *s = ch; -#if 0 - if ((t = read (0, s, 1)) != 1) { - return t; - } -#endif *s = r_cons_controlz (*s); if (*s < 0x80) { len = 1; @@ -2072,9 +2067,8 @@ _end: R_FREE (I.sel_widget); - // should be here or not? - if (!memcmp (I.buffer.data, "!history", 8)) { - // if (I.buffer.data[0]=='!' && I.buffer.data[1]=='\0') { + // shouldnt be here + if (r_str_startswith (I.buffer.data, "!history")) { r_line_hist_list (); return ""; } diff --git a/libr/cons/grep.c b/libr/cons/grep.c index cec8fa95ce..91b4501f7c 100644 --- a/libr/cons/grep.c +++ b/libr/cons/grep.c @@ -150,17 +150,17 @@ static void parse_grep_expression(const char *str) { if (ptr[1] == ':') { grep->human = true; // human friendly indentation ij~{: grep->json = 1; - if (!strncmp (ptr, "{:...", 5)) { + if (r_str_startswith (ptr, "{:...")) { grep->hud = true; - } else if (!strncmp (ptr, "{:..", 4)) { + } else if (r_str_startswith (ptr, "{:..")) { grep->less = 1; } } else if (ptr[1] == '}') { // standard json indentation grep->json = 1; - if (!strncmp (ptr, "{}...", 5)) { + if (r_str_startswith (ptr, "{}...")) { grep->hud = true; - } else if (!strncmp (ptr, "{}..", 4)) { + } else if (r_str_startswith (ptr, "{}..")) { grep->less = 1; } } else { diff --git a/libr/cons/help.c b/libr/cons/help.c index e664bffcda..e7f55f17d5 100644 --- a/libr/cons/help.c +++ b/libr/cons/help.c @@ -79,7 +79,7 @@ R_API void r_cons_cmd_help(const char *help[], bool use_color) { help_args = help[i + 1]; help_desc = help[i + 2]; - if (!strncmp (help_cmd, usage_str, strlen (usage_str))) { + if (r_str_startswith (help_cmd, usage_str)) { /* Usage header */ r_cons_printf ("%s%s",pal_args_color, help_cmd); if (help_args[0]) { diff --git a/libr/cons/html.c b/libr/cons/html.c index f8f0a8d090..fd978d93ff 100644 --- a/libr/cons/html.c +++ b/libr/cons/html.c @@ -162,14 +162,14 @@ R_API char *r_cons_html_filter(const char *ptr, int *newlen) { str = ptr + 1; esc = 0; } - } else if (!strncmp (ptr, "48;5;", 5) || !strncmp (ptr, "48;2;", 5)) { + } else if (r_str_startswith (ptr, "48;5;") || r_str_startswith (ptr, "48;2;")) { char *end = strchr (ptr, 'm'); gethtmlrgb (ptr, background_color); need_to_set = true; ptr = end; str = ptr + 1; esc = 0; - } else if (!strncmp (ptr, "38;5;", 5) || !strncmp (ptr, "38;2;", 5)) { + } else if (r_str_startswith (ptr, "38;5;") || r_str_startswith (ptr, "38;2;")) { char *end = strchr (ptr, 'm'); gethtmlrgb (ptr, text_color); need_to_set = true; @@ -201,7 +201,7 @@ R_API char *r_cons_html_filter(const char *ptr, int *newlen) { need_to_set = need_to_clear = true; continue; // reset color - } else if (!strncmp (ptr, "27m", 3)) { + } else if (r_str_startswith (ptr, "27m")) { inv = false; need_to_set = true; ptr = ptr + 2; diff --git a/libr/cons/pal.c b/libr/cons/pal.c index 7ddbdd4925..0ad6855bf9 100644 --- a/libr/cons/pal.c +++ b/libr/cons/pal.c @@ -317,7 +317,7 @@ R_API char *r_cons_pal_parse(const char *str, R_NULLABLE RColor *outcol) { if (!outcol) { r_cons_rgb_str (out, sizeof (out), &rcolor); } - } else if (!strncmp (fgcolor, "#", 1)) { // "#00ff00" HTML format + } else if (fgcolor[0] == '#') { // "#00ff00" HTML format if (strlen (fgcolor + 1) == 6) { const char *kule = fgcolor + 1; rcolor.r = rgbnum (kule[0], kule[1]); @@ -329,7 +329,7 @@ R_API char *r_cons_pal_parse(const char *str, R_NULLABLE RColor *outcol) { } else { R_LOG_WARN ("Invalid html color code"); } - } else if (!strncmp (fgcolor, "rgb:", 4)) { // "rgb:123" rgb format + } else if (r_str_startswith (fgcolor, "rgb:")) { // "rgb:123" rgb format if (strlen (fgcolor + 4) == 3) { // "rgb:RGB" rcolor.r = rgbnum (fgcolor[4], '0'); rcolor.g = rgbnum (fgcolor[5], '0'); @@ -347,7 +347,7 @@ R_API char *r_cons_pal_parse(const char *str, R_NULLABLE RColor *outcol) { } } // Handle second color (bgcolor) - if (bgcolor && !strncmp (bgcolor, "rgb:", 4)) { // "rgb:123" rgb format + if (bgcolor && r_str_startswith (bgcolor, "rgb:")) { // "rgb:123" rgb format rcolor.a |= ALPHA_BG; if (strlen (bgcolor + 4) == 3) { rcolor.r2 = rgbnum (bgcolor[4], '0'); @@ -392,15 +392,15 @@ R_API char *r_cons_pal_parse(const char *str, R_NULLABLE RColor *outcol) { // Parse extra attributes. const char *p = attr; while (p) { - if (!strncmp(p, "bold", 4)) { + if (r_str_startswith (p, "bold")) { rcolor.attr |= R_CONS_ATTR_BOLD; - } else if (!strncmp(p, "dim", 3)) { + } else if (r_str_startswith (p, "dim")) { rcolor.attr |= R_CONS_ATTR_DIM; - } else if (!strncmp(p, "italic", 6)) { + } else if (r_str_startswith (p, "italic")) { rcolor.attr |= R_CONS_ATTR_ITALIC; - } else if (!strncmp(p, "underline", 9)) { + } else if (r_str_startswith (p, "underline")) { rcolor.attr |= R_CONS_ATTR_UNDERLINE; - } else if (!strncmp(p, "blink", 5)) { + } else if (r_str_startswith (p, "blink")) { rcolor.attr |= R_CONS_ATTR_BLINK; } else { R_LOG_ERROR ("Failed to parse terminal attributes: %s", p); diff --git a/libr/include/r_anal.h b/libr/include/r_anal.h index df4f1a6b9b..951c1b1702 100644 --- a/libr/include/r_anal.h +++ b/libr/include/r_anal.h @@ -2285,6 +2285,7 @@ extern RAnalPlugin r_anal_plugin_xcore_cs; extern RAnalPlugin r_anal_plugin_xtensa; extern RAnalPlugin r_anal_plugin_arm_v35; extern RAnalPlugin r_anal_plugin_z80; +extern RAnalPlugin r_anal_plugin_mcs96; extern RAnalPlugin r_anal_plugin_pyc; extern RAnalPlugin r_anal_plugin_evm_cs; extern RAnalPlugin r_anal_plugin_bpf; diff --git a/libr/include/r_asm.h b/libr/include/r_asm.h index c06bdc150b..884ea25d21 100644 --- a/libr/include/r_asm.h +++ b/libr/include/r_asm.h @@ -200,7 +200,6 @@ extern RAsmPlugin r_asm_plugin_hppa_gnu; extern RAsmPlugin r_asm_plugin_java; extern RAsmPlugin r_asm_plugin_lanai_gnu; extern RAsmPlugin r_asm_plugin_m680x_cs; -extern RAsmPlugin r_asm_plugin_mcs96; extern RAsmPlugin r_asm_plugin_mips_cs; extern RAsmPlugin r_asm_plugin_mips_gnu; extern RAsmPlugin r_asm_plugin_nios2; diff --git a/libr/meson.build b/libr/meson.build index 99faf8101b..e65099199f 100644 --- a/libr/meson.build +++ b/libr/meson.build @@ -134,7 +134,6 @@ asm_plugins += [ 'gb', 'java', 'm680x_cs', - 'mcs96', 'mips_cs', 'or1k', 'ppc_as', @@ -154,6 +153,7 @@ asm_plugins += [ anal_plugins += [ '6502', '6502_cs', + 'mcs96', '8051', 'alpha', 'amd29k', diff --git a/libr/util/str.c b/libr/util/str.c index 5fb6e17636..5cb53bcf67 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -1,20 +1,11 @@ /* radare - LGPL - Copyright 2007-2022 - pancake */ -#include "r_types.h" -#include "r_util.h" -#include "r_cons.h" -#include "r_bin.h" -#include -#include -#include -#include -#include -#include +#include /* stable code */ -static const char *nullstr = ""; -static const char *nullstr_c = "(null)"; -static const char *rwxstr[] = { +static const char * const nullstr = ""; +static const char * const nullstr_c = "(null)"; +static const char * const rwxstr[] = { [0] = "---", [1] = "--x", [2] = "-w-", @@ -34,7 +25,6 @@ static const char *rwxstr[] = { [15] = "rwx", }; - R_API int r_str_casecmp(const char *s1, const char *s2) { #ifdef _MSC_VER return stricmp (s1, s2); diff --git a/sys/lint.sh b/sys/lint.sh index 1b337c5dda..e3b697656c 100755 --- a/sys/lint.sh +++ b/sys/lint.sh @@ -6,12 +6,14 @@ (git grep 'for (size_t' | grep -v sys/) && exit 1 (git grep 'R_LOG_' | grep '\\n' | grep -v sys/) && exit 1 (git grep 'eprintf' libr | grep 'Error:') && exit 1 - -# pending cleanups -# (git grep 'strncmp' libr) # && exit 1 # use r_str_startswith() (git grep 'x ""' libr) && exit 1 (git grep 'x""' libr) && exit 1 (git grep '4d""' libr) && exit 1 + +# pending cleanups +# (git grep 'TODO' libr) # && exit 1 # use r_str_startswith() +# (git grep 'XXX' libr) # && exit 1 # use r_str_startswith() +# (git grep 'strncmp' libr) # && exit 1 # use r_str_startswith() # (git grep 'eprintf' libr | grep 'Warning:') # && exit 1 # (git grep 'eprintf' | grep 'Usage:' | grep -v sys/) # && exit 1