radare2/libr/core/asm.c
Nibble ddffdff8d2 * Rename r_asm_aop_* to r_asm_op_*
* Rename remaining r_anal_aop_*
2011-02-24 16:50:29 +01:00

190 lines
4.1 KiB
C

/* radare - LGPL - Copyright 2009-2010 */
/* nibble<.ds@gmail.com> */
#include <r_types.h>
#include <r_core.h>
#include <r_asm.h>
R_API RCoreAsmHit *r_core_asm_hit_new() {
RCoreAsmHit *hit = R_NEW (RCoreAsmHit);
if (hit) {
hit->code = NULL;
hit->len = 0;
hit->addr = -1;
}
return hit;
}
R_API RList *r_core_asm_hit_list_new() {
RList *list = r_list_new ();
list->free = &r_core_asm_hit_free;
return list;
}
R_API void r_core_asm_hit_free(void *_hit) {
RCoreAsmHit *hit = _hit;
if (hit) {
if (hit->code)
free (hit->code);
free (hit);
}
}
R_API char* r_core_asm_search(RCore *core, const char *input, ut64 from, ut64 to) {
RAsmCode *acode;
char *ret;
if (!(acode = r_asm_massemble (core->assembler, input)))
return NULL;
ret = strdup (acode->buf_hex);
r_asm_code_free (acode);
return ret;
}
#define OPSZ 8
R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut64 to) {
RCoreAsmHit *hit;
RAsmOp op;
RList *hits;
ut64 at, toff = core->offset;
ut8 *buf;
char *tok, *tokens[1024], *code = NULL, *ptr;
int idx, tidx, ret, len;
int tokcount, matchcount;
if (core->blocksize<=OPSZ) {
eprintf ("error: block size too small\n");
return NULL;
}
if (!(buf = (ut8 *)malloc (core->blocksize))){
return NULL;
}
if (!(ptr = strdup (input))) {
free (buf);
return NULL;
}
if (!(hits = r_core_asm_hit_list_new ())) {
free (buf);
free (ptr);
return NULL;
}
for (tokcount=0;;tokcount++) {
if (tokcount==0) tok = (char*)strtok (ptr, ";");
else tok = (char*)strtok (NULL, ";");
if (tok == NULL)
break;
tokens[tokcount] = r_str_trim_head_tail (tok);
}
for (at = from, matchcount = 0; at < to; at += core->blocksize-OPSZ) {
if (r_cons_singleton ()->breaked)
break;
ret = r_io_read_at (core->io, at, buf, core->blocksize);
if (ret != core->blocksize)
break;
idx = 0, matchcount = 0;
while (idx<core->blocksize) {
r_asm_set_pc (core->assembler, at+idx);
if (!(len = r_asm_disassemble (core->assembler, &op, buf+idx, core->blocksize-idx))) {
if (matchcount != 0)
idx = tidx+1;
else idx++;
matchcount = 0;
continue;
}
if (strstr (op.buf_asm, tokens[matchcount])) {
code = r_str_concatf (code, "%s; ", op.buf_asm);
if (matchcount == tokcount-1) {
if (tokcount == 1)
tidx = idx;
if (!(hit = r_core_asm_hit_new ())) {
r_list_destroy (hits);
free (buf);
free (ptr);
free (code);
return NULL;
}
hit->addr = at+tidx;
hit->len = idx+len-tidx;
hit->code = strdup (code);
r_list_append (hits, hit);
R_FREE (code);
matchcount = 0;
idx = tidx+1;
} else if (matchcount == 0) {
tidx = idx;
matchcount++;
idx += len;
} else {
matchcount++;
idx += len;
}
} else {
if (matchcount != 0)
idx = tidx+1;
else idx++;
R_FREE (code);
matchcount = 0;
}
}
}
r_asm_set_pc (core->assembler, toff);
free (buf);
free (ptr);
free (code);
return hits;
}
R_API RList *r_core_asm_bwdisassemble (RCore *core, ut64 addr, int n, int len) {
RCoreAsmHit *hit;
RAsmOp op;
RList *hits = NULL;
ut8 *buf;
ut64 at;
int instrlen, ni, idx;
if (!(hits = r_core_asm_hit_list_new ()))
return NULL;
buf = (ut8 *)malloc (len);
if (!buf) {
r_list_destroy (hits);
return NULL;
}
if (r_io_read_at (core->io, addr-len, buf, len) != len) {
r_list_destroy (hits);
free (buf);
return NULL;
}
for (idx = 1; idx < len; idx++) {
if (r_cons_singleton ()->breaked)
break;
at = addr - idx; ni = 1;
while (at < addr) {
r_asm_set_pc (core->assembler, at);
//XXX HACK We need another way to detect invalid disasm!!
if (!(instrlen = r_asm_disassemble (core->assembler, &op, buf+(len-(addr-at)), addr-at)) || strstr (op.buf_asm, "invalid")) {
break;
} else {
at += instrlen;
if (at == addr) {
if (ni == n) {
if (!(hit = r_core_asm_hit_new ())) {
r_list_destroy (hits);
free (buf);
return NULL;
}
hit->addr = addr-idx;
hit->len = idx;
hit->code = NULL;
r_list_append (hits, hit);
}
} else {
ni++;
}
}
}
}
r_asm_set_pc (core->assembler, addr);
free (buf);
return hits;
}