Refactor analysis mips to remove static vars

This commit is contained in:
wargio 2023-06-25 17:01:39 +08:00 committed by Anton Kochkov
parent d6779c42d7
commit 2c9a7008e9

View file

@ -720,11 +720,9 @@ static void set_opdir(RzAnalysisOp *op) {
}
static int analop(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *buf, int len, RzAnalysisOpMask mask) {
int n, ret, opsize = -1;
static csh hndl = 0;
static int omode = -1;
static int obits = 32;
cs_insn *insn;
int n = 0, opsize = -1;
csh hndl = 0;
cs_insn *insn = NULL;
int mode = analysis->big_endian ? CS_MODE_BIG_ENDIAN : CS_MODE_LITTLE_ENDIAN;
if (analysis->cpu && *analysis->cpu) {
@ -740,26 +738,29 @@ static int analop(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *
#endif
}
}
mode |= (analysis->bits == 64) ? CS_MODE_MIPS64 : CS_MODE_MIPS32;
if (mode != omode || analysis->bits != obits) {
cs_close(&hndl);
hndl = 0;
omode = mode;
obits = analysis->bits;
switch (analysis->bits) {
case 64:
mode |= CS_MODE_MIPS64;
break;
case 32:
mode |= CS_MODE_MIPS32;
break;
default:
return -1;
}
// XXX no arch->cpu ?!?! CS_MODE_MICRO, N64
op->addr = addr;
if (len < 4) {
return -1;
}
op->size = 4;
if (hndl == 0) {
ret = cs_open(CS_ARCH_MIPS, mode, &hndl);
if (ret != CS_ERR_OK) {
goto fin;
}
cs_option(hndl, CS_OPT_DETAIL, CS_OPT_ON);
if (cs_open(CS_ARCH_MIPS, mode, &hndl) != CS_ERR_OK) {
return -1;
}
cs_option(hndl, CS_OPT_DETAIL, CS_OPT_ON);
n = cs_disasm(hndl, (ut8 *)buf, len, addr, 1, &insn);
if (n < 1 || insn->size < 1) {
if (mask & RZ_ANALYSIS_OP_MASK_DISASM) {
@ -1090,9 +1091,9 @@ beach:
if (mask & RZ_ANALYSIS_OP_MASK_VAL) {
op_fillval(analysis, op, &hndl, insn);
}
cs_free(insn, n);
// cs_close (&handle);
fin:
cs_close(&hndl);
return opsize;
}