radare2/libr/anal/p/anal_ws.c

148 lines
3.2 KiB
C
Raw Normal View History

2014-02-02 01:42:21 +00:00
/* radare - LGPL - Copyright 2014 Condret */
#include <string.h>
#include <r_types.h>
#include <r_asm.h>
#include <r_anal.h>
#include <r_lib.h>
2014-02-03 01:58:29 +00:00
#include <r_io.h>
2014-02-02 23:19:55 +00:00
#define WS_API static
2014-02-02 01:42:21 +00:00
#include "../../asm/arch/whitespace/wsdis.c"
2015-09-18 18:06:12 +00:00
static ut64 ws_find_label(int l, RIOBind iob) {
RIO *io = iob.io;
2017-05-03 15:19:49 +00:00
ut64 cur = 0, size = iob.desc_size (io->desc);
2014-02-03 01:58:29 +00:00
ut8 buf[128];
2017-08-14 00:55:47 +00:00
RAsmOp aop;;
2017-05-03 15:19:49 +00:00
iob.read_at (iob.io, cur, buf, 128);
2017-08-14 00:55:47 +00:00
while (cur <= size && wsdis(&aop, buf, 128)) {
if( aop.buf_asm[0] == 'm' &&
aop.buf_asm[1] == 'a' &&
l == atoi(&aop.buf_asm[5])) {
2014-02-03 01:58:29 +00:00
return cur;
}
2017-08-14 00:55:47 +00:00
cur = cur + aop.size;
2014-02-03 01:58:29 +00:00
iob.read_at(iob.io, cur, buf, 128);
}
return 0;
}
2015-09-18 18:06:12 +00:00
static int ws_anal(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) {
RAsmOp *aop;
memset (op, '\0', sizeof(RAnalOp));
2014-02-02 01:42:21 +00:00
op->addr = addr;
op->type = R_ANAL_OP_TYPE_UNK;
2015-10-31 00:57:52 +00:00
aop = R_NEW0 (RAsmOp);
op->size = wsdis (aop, data, len);
if (op->size) {
switch (aop->buf_asm[0]) {
case 'n':
op->type = R_ANAL_OP_TYPE_NOP;
break;
case 'e':
op->type = R_ANAL_OP_TYPE_TRAP;
break;
case 'd':
if(aop->buf_asm[1] == 'u')
op->type = R_ANAL_OP_TYPE_UPUSH;
else
op->type = R_ANAL_OP_TYPE_DIV;
break;
case 'i':
op->type = R_ANAL_OP_TYPE_ILL;
break;
case 'a':
op->type = R_ANAL_OP_TYPE_ADD;
break;
case 'm':
if(aop->buf_asm[1] == 'o')
op->type = R_ANAL_OP_TYPE_MOD;
else
op->type = R_ANAL_OP_TYPE_MUL;
break;
case 'r':
op->type = R_ANAL_OP_TYPE_RET;
break;
case 'l':
op->type = R_ANAL_OP_TYPE_LOAD;
break;
case 'c':
if(aop->buf_asm[1] == 'a') {
op->type = R_ANAL_OP_TYPE_CALL;
2014-02-03 01:58:29 +00:00
op->fail = addr + aop->size;
2015-10-31 00:57:52 +00:00
op->jump = ws_find_label(atoi(&aop->buf_asm[5]), anal->iob);
} else {
op->type = R_ANAL_OP_TYPE_UPUSH;
}
break;
case 'j':
if(aop->buf_asm[1] == 'm') {
op->type = R_ANAL_OP_TYPE_JMP;
op->jump = ws_find_label(atoi(&aop->buf_asm[4]), anal->iob);
} else {
op->type = R_ANAL_OP_TYPE_CJMP;
op->jump = ws_find_label(atoi(&aop->buf_asm[3]), anal->iob);
}
op->fail = addr + aop->size;
break;
case 'g':
op->type = R_ANAL_OP_TYPE_IO;
break;
case 'p':
if(aop->buf_asm[1] == 'o') {
op->type = R_ANAL_OP_TYPE_POP;
} else {
if(aop->buf_asm[2] == 's') {
op->type = R_ANAL_OP_TYPE_PUSH;
if(127 > atoi(&aop->buf_asm[5])
&& atoi(&aop->buf_asm[5]) >= 33) {
char c[4];
c[3] = '\0';
c[0] = c[2] = '\'';
c[1] = (char) atoi(&aop->buf_asm[5]);
r_meta_set_string(anal, R_META_TYPE_COMMENT, addr, c);
2014-02-03 01:58:29 +00:00
}
2015-10-31 00:57:52 +00:00
} else {
op->type = R_ANAL_OP_TYPE_IO;
2014-02-02 01:42:21 +00:00
}
2015-10-31 00:57:52 +00:00
}
break;
case 's':
switch (aop->buf_asm[1]) {
case 'u':
op->type = R_ANAL_OP_TYPE_SUB;
break;
case 't':
op->type = R_ANAL_OP_TYPE_STORE;
break;
case 'l':
op->type = R_ANAL_OP_TYPE_LOAD; // XXX
break;
case 'w':
op->type = R_ANAL_OP_TYPE_ROR;
}
break;
2014-02-02 01:42:21 +00:00
}
}
r_asm_op_free(aop);
return op->size;
}
RAnalPlugin r_anal_plugin_ws = {
2014-02-02 01:42:21 +00:00
.name = "ws",
.desc = "Space, tab and linefeed analysis plugin",
.license = "LGPL3",
.arch = "ws",
2014-02-02 01:42:21 +00:00
.bits = 32,
.op = &ws_anal,
};
#ifndef CORELIB
2017-11-10 12:38:05 +00:00
RLibStruct radare_plugin = {
2014-02-02 01:42:21 +00:00
.type = R_LIB_TYPE_ANAL,
2015-07-12 14:04:10 +00:00
.data = &r_anal_plugin_ws,
.version = R2_VERSION
2014-02-02 01:42:21 +00:00
};
#endif