Merge the asm.mcs96 plugin into anal ##refactor

This commit is contained in:
pancake 2022-06-28 14:32:12 +02:00 committed by GitHub
parent 96f685ea9a
commit 0f8321842f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 79 additions and 84 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,5 +1,4 @@
#include <r_util.h>
#include <r_types.h>
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

View File

@ -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'),

View File

@ -1,12 +1,10 @@
/* radare - LGPL - Copyright - 2015-2019 - condret */
/* radare - LGPL - Copyright - 2015-2022 - condret */
#include <r_types.h>
#include <string.h>
#include <r_asm.h>
#include <r_lib.h>
#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

11
libr/anal/p/mcs96.mk Normal file
View File

@ -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

View File

@ -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'),

View File

@ -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

View File

@ -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);

View File

@ -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 "";
}

View File

@ -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 {

View File

@ -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]) {

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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',

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <r_util/r_base64.h>
#include <r_bin.h>
/* 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);

View File

@ -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