diff --git a/binr/ragg2/ragg2.c b/binr/ragg2/ragg2.c index 6cd70068b5..94909becb1 100644 --- a/binr/ragg2/ragg2.c +++ b/binr/ragg2/ragg2.c @@ -96,7 +96,9 @@ static int openfile(const char *f, int x) { } } #if __UNIX__ - if (x) fchmod (fd, 0755); + if (x) { + fchmod (fd, 0755); + } #endif #if _MSC_VER _chsize (fd, 0); diff --git a/binr/rasm2/rasm2.c b/binr/rasm2/rasm2.c index 67d9998d75..38670c8a21 100644 --- a/binr/rasm2/rasm2.c +++ b/binr/rasm2/rasm2.c @@ -290,11 +290,12 @@ static int rasm_disasm(char *buf, ut64 offset, int len, int bits, int ascii, int int dr = r_asm_disassemble (a, &op, data + ret, len - ret); if (dr == -1 || op.size < 1) { op.size = 1; - strcpy (op.buf_asm, "invalid"); - sprintf (op.buf_hex, "%02x", data[ret]); + r_asm_op_set_asm (&op, "invalid"); + r_strbuf_set (&op.buf_hex, sdb_fmt ("%02x", data[ret])); } printf ("0x%08" PFMT64x " %2d %24s %s\n", - a->pc, op.size, op.buf_hex, op.buf_asm); + a->pc, op.size, r_asm_op_get_hex (&op), + r_asm_op_get_asm (&op)); ret += op.size; r_asm_set_pc (a, offset + ret); } diff --git a/libr/anal/esil.c b/libr/anal/esil.c index 341bef1d6f..109a827534 100644 --- a/libr/anal/esil.c +++ b/libr/anal/esil.c @@ -181,8 +181,7 @@ R_API int r_anal_esil_fire_interrupt(RAnalEsil *esil, int interrupt) { return false; } icb = (RAnalEsilInterruptCB)sdb_ptr_get (esil->interrupts, i, 0); - if (icb) return icb (esil, interrupt); - return false; + return icb? icb (esil, interrupt): false; } R_API bool r_anal_esil_set_pc(RAnalEsil *esil, ut64 addr) { @@ -2568,17 +2567,20 @@ static int esil_num(RAnalEsil *esil) { /* duplicate the last element in the stack */ static int esil_dup(RAnalEsil *esil) { - if (!esil || !esil->stack || esil->stackptr < 1 || esil->stackptr > (esil->stacksize - 1)) + if (!esil || !esil->stack || esil->stackptr < 1 || esil->stackptr > (esil->stacksize - 1)) { return false; + } return r_anal_esil_push (esil, esil->stack[esil->stackptr-1]); } static int esil_swap(RAnalEsil *esil) { char *tmp; - if (!esil || !esil->stack || esil->stackptr < 2) + if (!esil || !esil->stack || esil->stackptr < 2) { return false; - if (!esil->stack[esil->stackptr-1] || !esil->stack[esil->stackptr-2]) + } + if (!esil->stack[esil->stackptr-1] || !esil->stack[esil->stackptr-2]) { return false; + } tmp = esil->stack[esil->stackptr-1]; esil->stack[esil->stackptr-1] = esil->stack[esil->stackptr-2]; esil->stack[esil->stackptr-2] = tmp; @@ -2914,6 +2916,8 @@ loop: esil->skip = 0; esil->parse_goto = -1; esil->parse_stop = 0; +// memleak or failing aetr test. wat du +// r_anal_esil_stack_free (esil); esil->parse_goto_count = esil->anal? esil->anal->esil_goto_limit: R_ANAL_ESIL_GOTO_LIMIT; str = ostr; repeat: @@ -2938,7 +2942,6 @@ repeat: word[wordi] = 0; dorunword = 2; } - if (dorunword) { if (*word) { if (!runword (esil, word)) { @@ -2947,9 +2950,9 @@ repeat: word[wordi] = ','; wordi = 0; switch (evalWord (esil, ostr, &str)) { - case 0: goto loop; - case 1: return 0; - case 2: continue; + case 0: goto loop; + case 1: return 0; + case 2: continue; } if (dorunword == 1) { return 0; @@ -2960,7 +2963,9 @@ repeat: word[wordi++] = *str; //is *str is '\0' in the next iteration the condition will be true //reading beyond the boundaries - if (*str) str++; + if (*str) { + str++; + } } word[wordi] = 0; if (*word) { diff --git a/libr/anal/esil_trace.c b/libr/anal/esil_trace.c index cb381c3b56..04587d0dee 100644 --- a/libr/anal/esil_trace.c +++ b/libr/anal/esil_trace.c @@ -97,8 +97,9 @@ R_API void r_anal_esil_trace (RAnalEsil *esil, RAnalOp *op) { } ocbs = esil->cb; ocbs_set = true; - if (!DB) DB = sdb_new0 (); - + if (!DB) { + DB = sdb_new0 (); + } sdb_num_set (DB, "idx", esil->trace_idx, 0); sdb_num_set (DB, KEY ("addr"), op->addr, 0); // sdb_set (DB, KEY ("opcode"), op->mnemonic, 0); diff --git a/libr/anal/fcn.c b/libr/anal/fcn.c index 5356106331..211168fe71 100644 --- a/libr/anal/fcn.c +++ b/libr/anal/fcn.c @@ -329,6 +329,7 @@ R_API void r_anal_fcn_free(void *_fcn) { } free (fcn->fingerprint); r_anal_diff_free (fcn->diff); + r_list_free (fcn->fcn_locs); free (fcn->args); free (fcn); } @@ -557,6 +558,7 @@ static bool is_delta_pointer_table (RAnal *anal, RAnalFunction *fcn, ut64 addr, if (aop->type == R_ANAL_OP_TYPE_ADD) { add_aop = *aop; } + r_anal_op_fini (aop); i += len - 1; } if (!isValid) { diff --git a/libr/anal/p/anal_ws.c b/libr/anal/p/anal_ws.c index 5e6a5eedf8..8b6e5b1fcf 100644 --- a/libr/anal/p/anal_ws.c +++ b/libr/anal/p/anal_ws.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2014 Condret */ +/* radare - LGPL - Copyright 2014-2018 condret */ #include #include @@ -9,34 +9,32 @@ #define WS_API static #include "../../asm/arch/whitespace/wsdis.c" - static ut64 ws_find_label(int l, RIOBind iob) { RIO *io = iob.io; ut64 cur = 0, size = iob.desc_size (io->desc); ut8 buf[128]; - RAsmOp aop;; + RAsmOp aop; iob.read_at (iob.io, cur, buf, 128); - while (cur <= size && wsdis(&aop, buf, 128)) { - if( aop.buf_asm[0] == 'm' && - aop.buf_asm[1] == 'a' && - l == atoi(&aop.buf_asm[5])) { - return cur; + while (cur <= size && wsdis (&aop, buf, 128)) { + const char *buf_asm = r_strbuf_get (&aop.buf_asm); // r_asm_op_get_asm (&aop); + if (buf_asm && (strlen (buf_asm) > 4) && buf_asm[0] == 'm' && buf_asm[1] == 'a' && l == atoi (buf_asm + 5)) { + return cur; } cur = cur + aop.size; - iob.read_at(iob.io, cur, buf, 128); + iob.read_at (iob.io, cur, buf, 128); } return 0; } static int ws_anal(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) { - RAsmOp *aop; - memset (op, '\0', sizeof(RAnalOp)); + memset (op, '\0', sizeof (RAnalOp)); op->addr = addr; op->type = R_ANAL_OP_TYPE_UNK; - aop = R_NEW0 (RAsmOp); + RAsmOp *aop = R_NEW0 (RAsmOp); op->size = wsdis (aop, data, len); if (op->size) { - switch (aop->buf_asm[0]) { + const char *buf_asm = r_strbuf_get (&aop->buf_asm); // r_asm_op_get_asm (aop); + switch (*buf_asm) { case 'n': op->type = R_ANAL_OP_TYPE_NOP; break; @@ -44,10 +42,7 @@ static int ws_anal(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len 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; + op->type = (buf_asm[1] == 'u')? R_ANAL_OP_TYPE_UPUSH: R_ANAL_OP_TYPE_DIV; break; case 'i': op->type = R_ANAL_OP_TYPE_ILL; @@ -56,10 +51,7 @@ static int ws_anal(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len 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; + op->type = (buf_asm[1] == 'o') ? R_ANAL_OP_TYPE_MOD : R_ANAL_OP_TYPE_MUL; break; case 'r': op->type = R_ANAL_OP_TYPE_RET; @@ -68,21 +60,21 @@ static int ws_anal(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len op->type = R_ANAL_OP_TYPE_LOAD; break; case 'c': - if(aop->buf_asm[1] == 'a') { + if (buf_asm[1] == 'a') { op->type = R_ANAL_OP_TYPE_CALL; op->fail = addr + aop->size; - op->jump = ws_find_label(atoi(&aop->buf_asm[5]), anal->iob); + op->jump = ws_find_label (atoi (buf_asm + 5), anal->iob); } else { op->type = R_ANAL_OP_TYPE_UPUSH; } break; case 'j': - if(aop->buf_asm[1] == 'm') { + if (buf_asm[1] == 'm') { op->type = R_ANAL_OP_TYPE_JMP; - op->jump = ws_find_label(atoi(&aop->buf_asm[4]), anal->iob); + op->jump = ws_find_label(atoi (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->jump = ws_find_label(atoi(buf_asm + 3), anal->iob); } op->fail = addr + aop->size; break; @@ -90,18 +82,17 @@ static int ws_anal(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len op->type = R_ANAL_OP_TYPE_IO; break; case 'p': - if(aop->buf_asm[1] == 'o') { + if (buf_asm[1] == 'o') { op->type = R_ANAL_OP_TYPE_POP; } else { - if(aop->buf_asm[2] == 's') { + if (buf_asm[2] == 's') { op->type = R_ANAL_OP_TYPE_PUSH; - if(127 > atoi(&aop->buf_asm[5]) - && atoi(&aop->buf_asm[5]) >= 33) { + if (127 > atoi (buf_asm + 5) && atoi (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); + c[1] = (char) atoi (buf_asm + 5); + r_meta_set_string (anal, R_META_TYPE_COMMENT, addr, c); } } else { op->type = R_ANAL_OP_TYPE_IO; @@ -109,23 +100,22 @@ static int ws_anal(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len } 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; + switch (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; } } - r_asm_op_free(aop); return op->size; } diff --git a/libr/asm/Makefile b/libr/asm/Makefile index b1470cc846..2ba1aff86f 100644 --- a/libr/asm/Makefile +++ b/libr/asm/Makefile @@ -29,7 +29,7 @@ plugins: ${LIBSO} ${LIBAR} include ${STATIC_ASM_PLUGINS} STATIC_OBJS=$(subst ..,p/..,$(subst asm_,p/asm_,$(STATIC_OBJ))) -OBJS=${STATIC_OBJS} asm.o code.o +OBJS=${STATIC_OBJS} asm.o code.o op.o # hack to b OBJS+=${SHARED2_OBJ} diff --git a/libr/asm/arch/6502/6502dis.c b/libr/asm/arch/6502/6502dis.c index 8abef20f93..3492095f54 100644 --- a/libr/asm/arch/6502/6502dis.c +++ b/libr/asm/arch/6502/6502dis.c @@ -138,39 +138,41 @@ static int _6502Disass (ut64 pc, RAsmOp *op, const ut8 *buf, ut64 len) { int i; for (i=0; ops[i].name != NULL; i++) { if (ops[i].op == buf[0]) { + const char *buf_asm = "invalid"; + int len = ops[i].len; switch (ops[i].len) { case 1: - sprintf (op->buf_asm, "%s", ops[i].name); + buf_asm = sdb_fmt ("%s", ops[i].name); break; case 2: - if (len>1) { - sprintf (op->buf_asm, ops[i].name, buf[1]); + if (len > 1) { + buf_asm = sdb_fmt (ops[i].name, buf[1]); } else { - strcpy (op->buf_asm, "truncated"); - return -1; + buf_asm = "truncated"; + len = -1; } break; case 3: - if (len>2) { - snprintf (op->buf_asm, sizeof (op->buf_asm), ops[i].name, buf[1]+0x100*buf[2]); + if (len > 2) { + buf_asm = sdb_fmt (ops[i].name, buf[1] + 0x100 * buf[2]); } else { - strcpy (op->buf_asm, "truncated"); - return -1; + buf_asm = "truncated"; + len = -1; } break; case 4: - if (len>3) { - sprintf (op->buf_asm, ops[i].name, - buf[1]+0x100*buf[2]+0x10000*buf[3]); + if (len > 3) { + buf_asm = sdb_fmt (ops[i].name, buf[1]+0x100*buf[2]+0x10000*buf[3]); } else { - strcpy (op->buf_asm, "truncated"); - return -1; + buf_asm = "truncated"; + len = -1; } break; default: goto beach; } - return ops[i].len; + r_strbuf_set (&op->buf_asm, buf_asm); + return len; } } beach: diff --git a/libr/asm/arch/8051/8051_disas.c b/libr/asm/arch/8051/8051_disas.c index 62fa866894..518212d5e8 100644 --- a/libr/asm/arch/8051/8051_disas.c +++ b/libr/asm/arch/8051/8051_disas.c @@ -116,7 +116,7 @@ int _8051_disas (ut64 pc, RAsmOp *op, const ut8 *buf, ut64 len) { val1 = buf[1]; } } else { - strcpy (op->buf_asm, "truncated"); + r_strbuf_set (&op->buf_asm, "truncated"); return -1; } break; @@ -154,7 +154,7 @@ int _8051_disas (ut64 pc, RAsmOp *op, const ut8 *buf, ut64 len) { val1 = buf[1]; } } else { - strcpy (op->buf_asm, "truncated"); + r_strbuf_set (&op->buf_asm, "truncated"); return -1; } break; @@ -167,7 +167,7 @@ int _8051_disas (ut64 pc, RAsmOp *op, const ut8 *buf, ut64 len) { if (disasm) { disasm = _replace_register (disasm, arg1, val1); disasm = _replace_register (disasm, arg2, val2); - r_str_ncpy (op->buf_asm, disasm, sizeof (op->buf_asm)); + r_strbuf_set (&op->buf_asm, disasm); free (disasm); } return oplen; diff --git a/libr/asm/arch/gb/gbasm.c b/libr/asm/arch/gb/gbasm.c index 4ab7e3ae9b..d91988b2df 100644 --- a/libr/asm/arch/gb/gbasm.c +++ b/libr/asm/arch/gb/gbasm.c @@ -1,11 +1,14 @@ +/* radare - LGPL - Copyright 2012-2018 - condret */ + #include #include #include #include static void str_op(char *c) { - if ((c[0] <= 'Z') && (c[0] >= 'A')) + if ((c[0] <= 'Z') && (c[0] >= 'A')) { c[0] += 0x20; + } } static int gb_reg_idx (char r) { @@ -16,39 +19,44 @@ static int gb_reg_idx (char r) { static bool gb_parse_cb1 (ut8 *buf, const int minlen, char *buf_asm, ut8 base) { int i; - if (strlen (buf_asm) < minlen) + if (strlen (buf_asm) < minlen || minlen < 1) { return false; + } buf[0] = base; - i = strlen (&buf_asm[minlen - 1]); - r_str_replace_in (&buf_asm[minlen - 1], (ut32)i, "[ ", "[", true); - r_str_replace_in (&buf_asm[minlen - 1], (ut32)i, " ]", "]", true); + i = strlen (buf_asm + minlen - 1); + r_str_replace_in (buf_asm + minlen - 1, (ut32)i, "[ ", "[", true); + r_str_replace_in (buf_asm + minlen - 1, (ut32)i, " ]", "]", true); r_str_do_until_token (str_op, buf_asm, ' '); i = gb_reg_idx (buf_asm[minlen-1]); if (i != (-1)) { buf[0] |= (ut8)i; return true; - } else if (buf_asm[minlen - 1] == '[' - && buf_asm[minlen] == 'h' - && buf_asm[minlen + 1] == 'l' - && buf_asm[minlen + 2] == ']' ) { + } + if (!strncmp (buf_asm, "[hl]", 4)) { buf[0] |= 6; return true; - } else return false; + } + return false; } static bool gb_parse_cb2 (ut8 *buf, const int minlen, char *buf_asm, ut8 base) { ut64 num; int i; char *p, *q; - if ((i = strlen (buf_asm)) < minlen) + if ((i = strlen (buf_asm)) < minlen) { return false; + } r_str_replace_in (buf_asm, (ut32)i, "[ ", "[", true); r_str_replace_in (buf_asm, (ut32)i, " ]", "]", true); r_str_replace_in (buf_asm, (ut32)i, ", ", ",", true); p = strchr (buf_asm, (int)' '); - if (!p) return false; + if (!p) { + return false; + } q = strchr (p, (int)','); - if (!q) return false; + if (!q) { + return false; + } q[0] = '\0'; if (p[1] == '\0' || q[1] == '\0') { q[0] = ','; @@ -56,19 +64,19 @@ static bool gb_parse_cb2 (ut8 *buf, const int minlen, char *buf_asm, ut8 base) { } num = r_num_get (NULL, &p[1]); q[0] = ','; - if (num > 7) + if (num > 7) { return false; + } buf[0] = base + (ut8)num * 8; i = gb_reg_idx (q[1]); - if (i != (-1)) { + if (i != -1) { buf[0] |= (ut8)i; return true; } - if (strlen(&q[1]) < 4) + if (strlen (&q[1]) < 4) { return false; - if (q[1] == '[' && q[2] == 'h' - && q[3] == 'l' - && q[4] == ']') { + } + if (!strncmp (q + 1, "[hl]", 4)) { buf[0] |= 6; return true; } @@ -78,24 +86,22 @@ static bool gb_parse_cb2 (ut8 *buf, const int minlen, char *buf_asm, ut8 base) { static int gb_parse_arith1 (ut8 *buf, const int minlen, char *buf_asm, ut8 base, ut8 alt) { int i; ut64 num; - if (strlen (buf_asm) < minlen) + if (strlen (buf_asm) < minlen) { return 0; + } buf[0] = base; i = strlen (&buf_asm[minlen - 1]); r_str_replace_in (&buf_asm[minlen - 1], (ut32)i, "[ ", "[", true); r_str_replace_in (&buf_asm[minlen - 1], (ut32)i, " ]", "]", true); r_str_do_until_token (str_op, buf_asm, ' '); - i = gb_reg_idx (buf_asm[minlen-1]); - if (i != (-1)) + i = gb_reg_idx (buf_asm[minlen - 1]); + if (i != (-1)) { buf[0] |= (ut8)i; - else if (buf_asm[minlen - 1] == '[' - && buf_asm[minlen] == 'h' - && buf_asm[minlen + 1] == 'l' - && buf_asm[minlen + 2] == ']' ) + } else if (!strncmp (buf_asm + minlen - 1, "[hl]", 4)) { buf[0] |= 6; - else { + } else { buf[0] = alt; - num = r_num_get (NULL, &buf_asm[minlen - 1]); + num = r_num_get (NULL, buf_asm + minlen - 1); buf[1] = (ut8)(num & 0xff); return 2; } @@ -104,25 +110,25 @@ static int gb_parse_arith1 (ut8 *buf, const int minlen, char *buf_asm, ut8 base, static bool gb_parse_ld1 (ut8 *buf, const int minlen, char *buf_asm) { int i; - r_str_replace_in (buf_asm, strlen(buf_asm), ", ", ",", true); - if ((i = strlen(buf_asm)) < minlen) + r_str_replace_in (buf_asm, strlen (buf_asm), ", ", ",", true); + if ((i = strlen (buf_asm)) < minlen) { return false; + } r_str_do_until_token (str_op, buf_asm, '\0'); if (buf_asm[4] == ',') { i = gb_reg_idx (buf_asm[3]); - if (i == (-1)) + if (i == (-1)) { return false; + } buf[0] = (ut8)(0x40 + (i * 8)); - if ((i = gb_reg_idx (buf_asm[5])) == (-1)) + if ((i = gb_reg_idx (buf_asm[5])) == (-1)) { return false; + } buf[0] |= (ut8)i; - } else if (buf_asm[3] == '[' - && buf_asm[4] == 'h' - && buf_asm[5] == 'l' - && buf_asm[6] == ']' - && buf_asm[7] == ',') { - if ((i = gb_reg_idx (buf_asm[8])) == (-1)) + } else if (!strncmp (buf_asm + 3, "[hl],", 5)) { + if ((i = gb_reg_idx (buf_asm[8])) == (-1)) { return false; + } buf[0] = 0x70 | (ut8)i; } return true; @@ -131,23 +137,20 @@ static bool gb_parse_ld1 (ut8 *buf, const int minlen, char *buf_asm) { static bool gb_parse_ld2 (ut8 *buf, char *buf_asm) { int i; ut64 num; - if (strlen (buf_asm) < 6) + if (strlen (buf_asm) < 6) { return false; + } if (buf_asm[4] == ',') { - if ((i = gb_reg_idx (buf_asm[3])) == (-1)) + if ((i = gb_reg_idx (buf_asm[3])) == -1) { return false; + } buf[0] = 0x6 + (ut8)(i * 8); - num = r_num_get (NULL, &buf_asm[5]); + num = r_num_get (NULL, buf_asm + 5); buf[1] = (ut8)(num & 0xff); return true; - } else if (buf_asm[3] == '[' - && buf_asm[4] == 'h' - && buf_asm[5] == 'l' - && buf_asm[6] == ']' - && buf_asm[7] == ',' - && buf_asm[8] != '\0') { + } else if (!strncmp (buf_asm + 3, "[hl],", 5)) { buf[0] = 0x36; - num = r_num_get (NULL, &buf_asm[8]); + num = r_num_get (NULL, buf_asm + 8); buf[1] = (ut8)(num & 0xff); return true; } @@ -158,429 +161,447 @@ static int gbAsm(RAsm *a, RAsmOp *op, const char *buf) { int mn_len, i, len = 1; ut32 mn = 0; ut64 num; - if (!a || !op || !buf) + if (!a || !op || !buf) { return 0; - strncpy (op->buf_asm, buf, R_ASM_BUFSIZE-1); - op->buf_asm[R_ASM_BUFSIZE-1] = 0; - i = strlen (op->buf_asm); - while (strstr (op->buf_asm, " ")) - r_str_replace_in (op->buf_asm, (ut32)i, " ", " ", true); - r_str_replace_in (op->buf_asm, (ut32)i, " ,", ",", true); - mn_len = r_str_do_until_token (str_op, op->buf_asm, ' '); - if (mn_len < 2 || mn_len > 4) + } + ut8 opbuf[4] = {0}; + r_strbuf_set (&op->buf_asm, buf); + char *buf_asm = r_strbuf_get (&op->buf_asm); + ut32 buf_len = strlen (buf); + while (strstr (buf_asm, " ")) { + r_str_replace_in (buf_asm, buf_len, " ", " ", true); + } + r_str_replace_in (buf_asm, buf_len, " ,", ",", true); + mn_len = r_str_do_until_token (str_op, buf_asm, ' '); + if (mn_len < 2 || mn_len > 4) { return 0; - for (i = 0; i < mn_len; i++) - mn = (mn << 8) | op->buf_asm[i]; + } + for (i = 0; i < mn_len; i++) { + mn = (mn << 8) | buf_asm[i]; + } switch (mn) { - case 0x6e6f70: //nop - op->buf[0] = 0x00; + case 0x6e6f70: //nop + opbuf[0] = 0x00; + break; + case 0x696e63: //inc + if ((i = strlen (buf_asm)) < 5) { + return op->size = 0; + } + r_str_replace_in (buf_asm, (ut32)i, "[ ", "[", true); + r_str_replace_in (buf_asm, (ut32)i, " ]", "]", true); + r_str_do_until_token (str_op, buf_asm + 4, '\0'); + if (buf_asm[4] == 'b') { + opbuf[0] = (buf_asm[5] == 'c')? 3: 4; + } else if (buf_asm[4] == 'c') { + opbuf[0] = 0x0c; + } else if (buf_asm[4] == 'd') { + opbuf[0] = (buf_asm[5] == 'e')? 0x13: 0x14; + } else if (buf_asm[4] == 'e') { + opbuf[0] = 0x1c; + } else if (buf_asm[4] == 'h') { + opbuf[0] = (buf_asm[5] == 'l')? 0x23: 0x24; + } else if (buf_asm[4] == 'l') { + opbuf[0] = 0x2c; + } else if (buf_asm[4] == 'a') { + opbuf[0] = 0x3c; + } else if (buf_asm[4] == 's' && buf_asm[5] == 'p') { + opbuf[0] = 0x33; + } else if (!strncmp (buf_asm + 4, "[hl]", 4)) { + opbuf[0] = 0x34; + } else { + len = 0; + } + break; + case 0x646563: //dec + if ((i = strlen (buf_asm)) < 5) { + return op->size = 0; + } + r_str_replace_in (buf_asm, (ut32)i, "[ ", "[", true); + r_str_replace_in (buf_asm, (ut32)i, " ]", "]", true); + r_str_do_until_token (str_op, &buf_asm[4], '\0'); + switch (buf_asm[4]) { + case 'b': + opbuf[0] = (buf_asm[5] == 'c')? 0x0b: 0x05; break; - case 0x696e63: //inc - if ((i = strlen (op->buf_asm)) < 5) - return op->size = 0; - r_str_replace_in (op->buf_asm, (ut32)i, "[ ", "[", true); - r_str_replace_in (op->buf_asm, (ut32)i, " ]", "]", true); - r_str_do_until_token (str_op, &op->buf_asm[4], '\0'); - if (op->buf_asm[4] == 'b') { - if (op->buf_asm[5] == 'c') - op->buf[0] = 0x03; - else op->buf[0] = 0x04; - } else if (op->buf_asm[4] == 'c') - op->buf[0] = 0x0c; - else if (op->buf_asm[4] == 'd') { - if (op->buf_asm[5] == 'e') - op->buf[0] = 0x13; - else op->buf[0] = 0x14; - } else if (op->buf_asm[4] == 'e') - op->buf[0] = 0x1c; - else if (op->buf_asm[4] == 'h') { - if (op->buf_asm[5] == 'l') - op->buf[0] = 0x23; - else op->buf[0] = 0x24; - } else if (op->buf_asm[4] == 'l') - op->buf[0] = 0x2c; - else if (op->buf_asm[4] == 'a') - op->buf[0] = 0x3c; - else if (op->buf_asm[4] == 's' - && op->buf_asm[5] == 'p') - op->buf[0] = 0x33; - else if (op->buf_asm[4] == '[' - && op->buf_asm[5] == 'h' - && op->buf_asm[6] == 'l' - && op->buf_asm[7] == ']') - op->buf[0] = 0x34; - else len = 0; + case 'c': + opbuf[0] = 0x0d; break; - case 0x646563: //dec - if ((i = strlen (op->buf_asm)) < 5) - return op->size = 0; - r_str_replace_in (op->buf_asm, (ut32)i, "[ ", "[", true); - r_str_replace_in (op->buf_asm, (ut32)i, " ]", "]", true); - r_str_do_until_token (str_op, &op->buf_asm[4], '\0'); - if (op->buf_asm[4] == 'b') { - if (op->buf_asm[5] == 'c') - op->buf[0] = 0x0b; - else op->buf[0] = 0x05; - } else if (op->buf_asm[4] == 'c') - op->buf[0] = 0x0d; - else if (op->buf_asm[4] == 'd') { - if (op->buf_asm[5] == 'e') - op->buf[0] = 0x1b; - else op->buf[0] = 0x15; - } else if (op->buf_asm[4] == 'e') - op->buf[0] = 0x1d; - else if (op->buf_asm[4] == 'h') { - if (op->buf_asm[5] == 'l') - op->buf[0] = 0x2b; - else op->buf[0] = 0x25; - } else if (op->buf_asm[4] == 'l') - op->buf[0] = 0x2d; - else if (op->buf_asm[4] == 'a') - op->buf[0] = 0x3d; - else if (op->buf_asm[4] == 's' - && op->buf_asm[5] == 'p') - op->buf[0] = 0x3b; - else if (op->buf_asm[4] == '[' - && op->buf_asm[5] == 'h' - && op->buf_asm[6] == 'l' - && op->buf_asm[7] == ']') - op->buf[0] = 0x35; - else len = 0; + case 'd': + opbuf[0] = (buf_asm[5] == 'e')? 0x1b: 0x15; break; - case 0x726c6361: //rlca - op->buf[0] = 0x07; + case 'e': + opbuf[0] = 0x1d; break; - case 0x72726361: //rrca - op->buf[0] = 0xf0; + case 'h': + opbuf[0] = (buf_asm[5] == 'l')? 0x2b: 0x25; break; - case 0x73746f70: //stop - op->buf[0] = 0x10; + case 'l': + opbuf[0] = 0x2d; break; - case 0x726c61: //rla - op->buf[0] = 0x17; - break; - case 0x727261: //rra - op->buf[0] = 0x1f; - break; - case 0x646161: //daa - op->buf[0] = 0x27; - break; - case 0x63706c: //cpl - op->buf[0] = 0x2f; - break; - case 0x616464: //add - r_str_replace_in (op->buf_asm, strlen(op->buf_asm), ", ", ",", true); - if (strlen(op->buf_asm) < 5) - return op->size = 0; - if (op->buf_asm[4] == 's' - && op->buf_asm[5] == 'p' - && op->buf_asm[6] == ',' - && op->buf_asm[7] != '\0') { - op->buf[0] = 0xe8; - num = r_num_get (NULL, &op->buf_asm[7]); - op->buf[1] = (ut8)(num & 0xff); - len = 2; - } - else if (!strcmp (op->buf_asm, "hl,bc")) - op->buf[0] = 0x09; - else if (!strcmp (&op->buf_asm[4], "hl,de")) - op->buf[0] = 0x19; - else if (!strcmp (&op->buf_asm[4], "hl,hl")) - op->buf[0] = 0x29; - else if (!strcmp (&op->buf_asm[4], "hl,sp")) - op->buf[0] = 0x39; - else len = gb_parse_arith1 (op->buf, 5, op->buf_asm, 0x80, 0xc6); - break; - case 0x616463: //adc - len = gb_parse_arith1 (op->buf, 5, op->buf_asm, 0x88, 0xce); - break; - case 0x737562: //sub - len = gb_parse_arith1 (op->buf, 5, op->buf_asm, 0x90, 0xd6); - break; - case 0x736263: //sbc - len = gb_parse_arith1 (op->buf, 5, op->buf_asm, 0x98, 0xde); - break; - case 0x616e64: //and - len = gb_parse_arith1 (op->buf, 5, op->buf_asm, 0xa0, 0xe6); - break; - case 0x786f72: //xor - len = gb_parse_arith1 (op->buf, 5, op->buf_asm, 0xa8, 0xee); - break; - case 0x6f72: //or - len = gb_parse_arith1 (op->buf, 4, op->buf_asm, 0xb0, 0xf6); - break; - case 0x6370: //cp - len = gb_parse_arith1 (op->buf, 4, op->buf_asm, 0xb8, 0xfe); - break; - case 0x736366: //scf - op->buf[0] = 0x37; - break; - case 0x636366: //ccf - op->buf[0] = 0x3f; - break; - case 0x68616c74: //halt - op->buf[0] = 0x76; - break; - case 0x726574: //ret - if (strlen(op->buf_asm) < 5) - op->buf[0] = 0xc9; - else if (strlen (op->buf_asm) < 6) { //there is no way that there can be " " - we did r_str_replace_in - str_op(&op->buf_asm[4]); - if (op->buf_asm[4] == 'z') //ret Z - op->buf[0] = 0xc8; - else if (op->buf_asm[4] == 'c') //ret C - op->buf[0] = 0xd8; - else return op->size = 0; - } else { - str_op(&op->buf_asm[4]); - if (op->buf_asm[4] != 'n') - return op->size = 0; - str_op(&op->buf_asm[5]); //if (!(strlen(op->buf_asm) < 6)) => must be 6 or greater - if (op->buf_asm[5] == 'z') //ret nZ - op->buf[0] = 0xc0; - else if (op->buf_asm[5] == 'c') //ret nC - op->buf[0] = 0xd0; - else return op->size = 0; - } - break; - case 0x72657469: //reti - op->buf[0] = 0xd9; - break; - case 0x6469: //di - op->buf[0] = 0xf3; - break; - case 0x6569: //ei - op->buf[0] = 0xfb; - break; - case 0x6c64: //ld - if (!gb_parse_ld1 (op->buf, 6, op->buf_asm)) { - len++; - if (!gb_parse_ld2 (op->buf, op->buf_asm)) - len = 0; - } - break; - case 0x727374: //rst - if (strlen (op->buf_asm) < 5) - return op->size = 0; - num = r_num_get (NULL, &op->buf_asm[4]); - if ((num & 7) || ((num/8) > 7)) - return op->size = 0; - op->buf[0] = (ut8)((num & 0xff) + 0xc7); - break; - case 0x70757368: //push - if (strlen (op->buf_asm) < 7) - return op->size = 0; - str_op (&op->buf_asm[5]); - str_op (&op->buf_asm[6]); - if (op->buf_asm[5] == 'b' && op->buf_asm[6] == 'c') { - op->buf[0] = 0xc5; - } else if (op->buf_asm[5] == 'd' && op->buf_asm[6] == 'e') { - op->buf[0] = 0xd5; - } else if (op->buf_asm[5] == 'h' && op->buf_asm[6] == 'l') { - op->buf[0] = 0xe5; - } else if (op->buf_asm[5] == 'a' && op->buf_asm[6] == 'f') { - op->buf[0] = 0xf5; - } else len = 0; - break; - case 0x706f70: //pop - if (strlen (op->buf_asm) < 6) - return op->size = 0; - str_op (&op->buf_asm[4]); - str_op (&op->buf_asm[5]); - if (op->buf_asm[4] == 'b' && op->buf_asm[5] == 'c') { - op->buf[0] = 0xc1; - } else if (op->buf_asm[4] == 'd' && op->buf_asm[5] == 'e') { - op->buf[0] = 0xd1; - } else if (op->buf_asm[4] == 'h' && op->buf_asm[5] == 'l') { - op->buf[0] = 0xe1; - } else if (op->buf_asm[4] == 'a' && op->buf_asm[5] == 'f') { - op->buf[0] = 0xf1; - } else len = 0; - break; - case 0x6a70: //jp - if (strlen(op->buf_asm) < 4) - return op->size = 0; - { - char *p = strchr (op->buf_asm, (int)','); - if (!p) { - str_op (&op->buf_asm[3]); - str_op (&op->buf_asm[4]); - if (op->buf_asm[3] == 'h' && op->buf_asm[4] == 'l') - op->buf[0] = 0xe9; - else { - num = r_num_get (NULL, &op->buf_asm[3]); - len = 3; - op->buf[0] = 0xc3; - op->buf[1] = (ut8)(num & 0xff); - op->buf[2] = (ut8)((num & 0xff00) >> 8); - } - } else { - str_op (p-2); - str_op (p-1); - if (*(p-2) == 'n') { - if (*(p-1) == 'z') - op->buf[0] = 0xc2; - else if (*(p-1) == 'c') - op->buf[0] = 0xd2; - else return op->size = 0; - } else if (*(p-2) == ' ') { - if (*(p-1) == 'z') - op->buf[0] = 0xca; - else if (*(p-1) == 'c') - op->buf[0] = 0xda; - else return op->size = 0; - } else return op->size = 0; - r_str_replace_in (p, strlen(p), ", ", ",", true); - if (p[1] == '\0') - return op->size = 0; - num = r_num_get (NULL, p + 1); - op->buf[1] = (ut8)(num & 0xff); - op->buf[2] = (ut8)((num & 0xff00) >> 8); - len = 3; - } - } - break; - case 0x6a72: //jr - if (strlen (op->buf_asm) < 4) - return op->size = 0; - { - char *p = strchr (op->buf_asm, (int)','); - if (!p) { - num = r_num_get (NULL, &op->buf_asm[3]); - len = 2; - op->buf[0] = 0x18; - op->buf[1] = (ut8)(num & 0xff); - } else { - str_op (p-2); - str_op (p-1); - if (*(p-2) == 'n') { - if (*(p-1) == 'z') - op->buf[0] = 0x20; - else if (*(p-1) == 'c') - op->buf[0] = 0x30; - else return op->size = 0; - } else if (*(p-2) == ' ') { - if (*(p-1) == 'z') - op->buf[0] = 0x28; - else if (*(p-1) == 'c') - op->buf[0] = 0x38; - else return op->size = 0; - } else return op->size = 0; - r_str_replace_in (p, strlen(p), ", ", ",", true); - if (p[1] == '\0') - return op->size = 0; - num = r_num_get (NULL, p + 1); - op->buf[1] = (ut8)(num & 0xff); - len = 2; - } - } - break; - case 0x63616c6c: //call - if (strlen(op->buf_asm) < 6) - return op->size = 0; - { - char *p = strchr (op->buf_asm, (int)','); - if (!p) { - num = r_num_get (NULL, &op->buf_asm[3]); - len = 3; - op->buf[0] = 0xcd; - op->buf[1] = (ut8)(num & 0xff); - op->buf[2] = (ut8)((num & 0xff00) >> 8); - } else { - str_op (p-2); - str_op (p-1); - if (*(p-2) == 'n') { - if (*(p-1) == 'z') - op->buf[0] = 0xc4; - else if (*(p-1) == 'c') - op->buf[0] = 0xd4; - else return op->size = 0; - } else if (*(p-2) == ' ') { - if (*(p-1) == 'z') - op->buf[0] = 0xcc; - else if (*(p-1) == 'c') - op->buf[0] = 0xdc; - else return op->size = 0; - } else return op->size = 0; - r_str_replace_in (p, strlen(p), ", ", ",", true); - if (p[1] == '\0') - return op->size = 0; - num = r_num_get (NULL, p + 1); - op->buf[1] = (ut8)(num & 0xff); - op->buf[2] = (ut8)((num & 0xff00) >> 8); - len = 3; - } - } - break; - case 0x726c63: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 5, op->buf_asm, 0x00)) - len = 0; - break; - case 0x727263: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 5, op->buf_asm, 0x08)) - len = 0; - break; - case 0x726c: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 4, op->buf_asm, 0x10)) - len = 0; - break; - case 0x7272: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 4, op->buf_asm, 0x18)) - len = 0; - break; - case 0x736c61: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 5, op->buf_asm, 0x20)) - len = 0; - break; - case 0x737261: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 5, op->buf_asm, 0x28)) - len = 0; - break; - case 0x73776170: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 6, op->buf_asm, 0x30)) - len = 0; - break; - case 0x73726c: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb1 (&op->buf[1], 5, op->buf_asm, 0x38)) - len = 0; - break; - case 0x626974: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb2 (&op->buf[1], 6, op->buf_asm, 0x40)) - len = 0; - break; - case 0x726573: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb2 (&op->buf[1], 6, op->buf_asm, 0x80)) - len = 0; - break; - case 0x736574: - op->buf[0] = 0xcb; - len = 2; - if (!gb_parse_cb2 (&op->buf[1], 6, op->buf_asm, 0xc0)) - len = 0; + case 'a': + opbuf[0] = 0x3d; break; default: - len = 0; + if (!strncmp (buf_asm + 4, "sp", 2)) { + opbuf[0] = 0x3b; + } else if (!strncmp (buf_asm, "[hl]", 4)) { + opbuf[0] = 0x35; + } else { + len = 0; + } break; + } + break; + case 0x726c6361: //rlca + opbuf[0] = 0x07; + break; + case 0x72726361: //rrca + opbuf[0] = 0xf0; + break; + case 0x73746f70: //stop + opbuf[0] = 0x10; + break; + case 0x726c61: //rla + opbuf[0] = 0x17; + break; + case 0x727261: //rra + opbuf[0] = 0x1f; + break; + case 0x646161: //daa + opbuf[0] = 0x27; + break; + case 0x63706c: //cpl + opbuf[0] = 0x2f; + break; + case 0x616464: //add + r_str_replace_in (buf_asm, strlen(buf_asm), ", ", ",", true); + if (strlen(buf_asm) < 5) + return op->size = 0; + if (buf_asm[4] == 's' + && buf_asm[5] == 'p' + && buf_asm[6] == ',' + && buf_asm[7] != '\0') { + opbuf[0] = 0xe8; + num = r_num_get (NULL, &buf_asm[7]); + opbuf[1] = (ut8)(num & 0xff); + len = 2; + } else if (!strcmp (buf_asm, "hl,bc")) { + opbuf[0] = 0x09; + } else if (!strcmp (&buf_asm[4], "hl,de")) { + opbuf[0] = 0x19; + } else if (!strcmp (&buf_asm[4], "hl,hl")) { + opbuf[0] = 0x29; + } else if (!strcmp (&buf_asm[4], "hl,sp")) { + opbuf[0] = 0x39; + } else { + len = gb_parse_arith1 (opbuf, 5, buf_asm, 0x80, 0xc6); + } + break; + case 0x616463: //adc + len = gb_parse_arith1 (opbuf, 5, buf_asm, 0x88, 0xce); + break; + case 0x737562: //sub + len = gb_parse_arith1 (opbuf, 5, buf_asm, 0x90, 0xd6); + break; + case 0x736263: //sbc + len = gb_parse_arith1 (opbuf, 5, buf_asm, 0x98, 0xde); + break; + case 0x616e64: //and + len = gb_parse_arith1 (opbuf, 5, buf_asm, 0xa0, 0xe6); + break; + case 0x786f72: //xor + len = gb_parse_arith1 (opbuf, 5, buf_asm, 0xa8, 0xee); + break; + case 0x6f72: //or + len = gb_parse_arith1 (opbuf, 4, buf_asm, 0xb0, 0xf6); + break; + case 0x6370: //cp + len = gb_parse_arith1 (opbuf, 4, buf_asm, 0xb8, 0xfe); + break; + case 0x736366: //scf + opbuf[0] = 0x37; + break; + case 0x636366: //ccf + opbuf[0] = 0x3f; + break; + case 0x68616c74: //halt + opbuf[0] = 0x76; + break; + case 0x726574: //ret + if (strlen (buf_asm) < 5) { + opbuf[0] = 0xc9; + } else if (strlen (buf_asm) < 6) { + // there is no way that there can be " " - we did r_str_replace_in + str_op (buf_asm + 4); + if (buf_asm[4] == 'z') { //ret Z + opbuf[0] = 0xc8; + } else if (buf_asm[4] == 'c') { //ret C + opbuf[0] = 0xd8; + } else { + return op->size = 0; + } + } else { + str_op(&buf_asm[4]); + if (buf_asm[4] != 'n') { + return op->size = 0; + } + str_op (&buf_asm[5]); //if (!(strlen(buf_asm) < 6)) => must be 6 or greater + if (buf_asm[5] == 'z') { //ret nZ + opbuf[0] = 0xc0; + } else if (buf_asm[5] == 'c') { //ret nC + opbuf[0] = 0xd0; + } else { + return op->size = 0; + } + } + break; + case 0x72657469: //reti + opbuf[0] = 0xd9; + break; + case 0x6469: //di + opbuf[0] = 0xf3; + break; + case 0x6569: //ei + opbuf[0] = 0xfb; + break; + case 0x6c64: //ld + if (!gb_parse_ld1 (opbuf, 6, buf_asm)) { + len++; + if (!gb_parse_ld2 (opbuf, buf_asm)) + len = 0; + } + break; + case 0x727374: //rst + if (strlen (buf_asm) < 5) + return op->size = 0; + num = r_num_get (NULL, &buf_asm[4]); + if ((num & 7) || ((num/8) > 7)) + return op->size = 0; + opbuf[0] = (ut8)((num & 0xff) + 0xc7); + break; + case 0x70757368: //push + if (strlen (buf_asm) < 7) + return op->size = 0; + str_op (&buf_asm[5]); + str_op (&buf_asm[6]); + if (buf_asm[5] == 'b' && buf_asm[6] == 'c') { + opbuf[0] = 0xc5; + } else if (buf_asm[5] == 'd' && buf_asm[6] == 'e') { + opbuf[0] = 0xd5; + } else if (buf_asm[5] == 'h' && buf_asm[6] == 'l') { + opbuf[0] = 0xe5; + } else if (buf_asm[5] == 'a' && buf_asm[6] == 'f') { + opbuf[0] = 0xf5; + } else len = 0; + break; + case 0x706f70: //pop + if (strlen (buf_asm) < 6) + return op->size = 0; + str_op (&buf_asm[4]); + str_op (&buf_asm[5]); + if (buf_asm[4] == 'b' && buf_asm[5] == 'c') { + opbuf[0] = 0xc1; + } else if (buf_asm[4] == 'd' && buf_asm[5] == 'e') { + opbuf[0] = 0xd1; + } else if (buf_asm[4] == 'h' && buf_asm[5] == 'l') { + opbuf[0] = 0xe1; + } else if (buf_asm[4] == 'a' && buf_asm[5] == 'f') { + opbuf[0] = 0xf1; + } else { + len = 0; + } + break; + case 0x6a70: //jp + if (strlen (buf_asm) < 4) { + return op->size = 0; + } + { + char *p = strchr (buf_asm, (int)','); + if (!p) { + str_op (&buf_asm[3]); + str_op (&buf_asm[4]); + if (buf_asm[3] == 'h' && buf_asm[4] == 'l') + opbuf[0] = 0xe9; + else { + num = r_num_get (NULL, &buf_asm[3]); + len = 3; + opbuf[0] = 0xc3; + opbuf[1] = (ut8)(num & 0xff); + opbuf[2] = (ut8)((num & 0xff00) >> 8); + } + } else { + str_op (p-2); + str_op (p-1); + if (*(p-2) == 'n') { + if (*(p-1) == 'z') + opbuf[0] = 0xc2; + else if (*(p-1) == 'c') + opbuf[0] = 0xd2; + else return op->size = 0; + } else if (*(p-2) == ' ') { + if (*(p-1) == 'z') + opbuf[0] = 0xca; + else if (*(p-1) == 'c') + opbuf[0] = 0xda; + else return op->size = 0; + } else return op->size = 0; + r_str_replace_in (p, strlen(p), ", ", ",", true); + if (p[1] == '\0') + return op->size = 0; + num = r_num_get (NULL, p + 1); + opbuf[1] = (ut8)(num & 0xff); + opbuf[2] = (ut8)((num & 0xff00) >> 8); + len = 3; + } + } + break; + case 0x6a72: //jr + if (strlen (buf_asm) < 4) + return op->size = 0; + { + char *p = strchr (buf_asm, (int)','); + if (!p) { + num = r_num_get (NULL, &buf_asm[3]); + len = 2; + opbuf[0] = 0x18; + opbuf[1] = (ut8)(num & 0xff); + } else { + str_op (p-2); + str_op (p-1); + if (*(p-2) == 'n') { + if (*(p-1) == 'z') + opbuf[0] = 0x20; + else if (*(p-1) == 'c') + opbuf[0] = 0x30; + else return op->size = 0; + } else if (*(p-2) == ' ') { + if (*(p-1) == 'z') + opbuf[0] = 0x28; + else if (*(p-1) == 'c') + opbuf[0] = 0x38; + else return op->size = 0; + } else { + return op->size = 0; + } + r_str_replace_in (p, strlen(p), ", ", ",", true); + if (!p[1]) { + return op->size = 0; + } + num = r_num_get (NULL, p + 1); + opbuf[1] = (ut8)(num & 0xff); + len = 2; + } + } + break; + case 0x63616c6c: //call + if (strlen(buf_asm) < 6) { + return op->size = 0; + } + { + char *p = strchr (buf_asm, (int)','); + if (!p) { + num = r_num_get (NULL, &buf_asm[3]); + len = 3; + opbuf[0] = 0xcd; + opbuf[1] = (ut8)(num & 0xff); + opbuf[2] = (ut8)((num & 0xff00) >> 8); + } else { + str_op (p-2); + str_op (p-1); + if (*(p-2) == 'n') { + if (*(p-1) == 'z') + opbuf[0] = 0xc4; + else if (*(p-1) == 'c') + opbuf[0] = 0xd4; + else return op->size = 0; + } else if (*(p-2) == ' ') { + if (*(p-1) == 'z') + opbuf[0] = 0xcc; + else if (*(p-1) == 'c') + opbuf[0] = 0xdc; + else return op->size = 0; + } else return op->size = 0; + r_str_replace_in (p, strlen(p), ", ", ",", true); + if (p[1] == '\0') + return op->size = 0; + num = r_num_get (NULL, p + 1); + opbuf[1] = (ut8)(num & 0xff); + opbuf[2] = (ut8)((num & 0xff00) >> 8); + len = 3; + } + } + break; + case 0x726c63: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 5, buf_asm, 0x00)) + len = 0; + break; + case 0x727263: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 5, buf_asm, 0x08)) + len = 0; + break; + case 0x726c: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 4, buf_asm, 0x10)) + len = 0; + break; + case 0x7272: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 4, buf_asm, 0x18)) { + len = 0; + } + break; + case 0x736c61: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 5, buf_asm, 0x20)) + len = 0; + break; + case 0x737261: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 5, buf_asm, 0x28)) + len = 0; + break; + case 0x73776170: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 6, buf_asm, 0x30)) + len = 0; + break; + case 0x73726c: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb1 (&opbuf[1], 5, buf_asm, 0x38)) + len = 0; + break; + case 0x626974: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb2 (&opbuf[1], 6, buf_asm, 0x40)) + len = 0; + break; + case 0x726573: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb2 (&opbuf[1], 6, buf_asm, 0x80)) + len = 0; + break; + case 0x736574: + opbuf[0] = 0xcb; + len = 2; + if (!gb_parse_cb2 (&opbuf[1], 6, buf_asm, 0xc0)) { + len = 0; + } + break; + default: + len = 0; + break; } return op->size = len; } diff --git a/libr/asm/arch/gb/gbdis.c b/libr/asm/arch/gb/gbdis.c index 26a2d74d84..905e0731c7 100644 --- a/libr/asm/arch/gb/gbdis.c +++ b/libr/asm/arch/gb/gbdis.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2013-2014 - condret@runas-racer.com */ +/* radare - LGPL - Copyright 2013-2018 - condret, pancake */ #include #include @@ -9,14 +9,14 @@ #include "gb_op_table.h" static int gbOpLength(int gboptype){ - switch(gboptype) { + switch (gboptype) { case GB_8BIT: return 1; - case GB_8BIT+ARG_8+GB_IO: - case GB_8BIT+ARG_8: + case GB_8BIT + ARG_8 + GB_IO: + case GB_8BIT + ARG_8: case GB_16BIT: return 2; - case GB_8BIT+ARG_16: + case GB_8BIT + ARG_16: return 3; } return 0; @@ -25,25 +25,30 @@ static int gbOpLength(int gboptype){ #ifndef GB_DIS_LEN_ONLY static int gbDisass(RAsmOp *op, const ut8 *buf, int len){ int foo = gbOpLength (gb_op[buf[0]].type); - if (lenbuf_asm, "%s", gb_op[buf[0]].name); + buf_asm = sdb_fmt ("%s", gb_op[buf[0]].name); break; case GB_16BIT: - sprintf (op->buf_asm, "%s %s", cb_ops[buf[1]>>3], cb_regs[buf[1]&7]); + buf_asm = sdb_fmt ("%s %s", cb_ops[buf[1]>>3], cb_regs[buf[1]&7]); break; case GB_8BIT+ARG_8: - sprintf (op->buf_asm, gb_op[buf[0]].name, buf[1]); + buf_asm = sdb_fmt (gb_op[buf[0]].name, buf[1]); break; case GB_8BIT+ARG_16: - sprintf (op->buf_asm, gb_op[buf[0]].name, buf[1]+0x100*buf[2]); + buf_asm = sdb_fmt (gb_op[buf[0]].name, buf[1]+0x100*buf[2]); break; case GB_8BIT+ARG_8+GB_IO: - sprintf (op->buf_asm, gb_op[buf[0]].name, 0xff00+buf[1]); + buf_asm = sdb_fmt (gb_op[buf[0]].name, 0xff00+buf[1]); break; } + if (buf_asm) { + r_strbuf_set (&op->buf_asm, buf_asm); + } return foo; } #endif diff --git a/libr/asm/arch/i4004/i4004dis.c b/libr/asm/arch/i4004/i4004dis.c index 6309e39a47..d51b223041 100644 --- a/libr/asm/arch/i4004/i4004dis.c +++ b/libr/asm/arch/i4004/i4004dis.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2014-2016 - condret@runas-racer.com */ +/* radare - LGPL - Copyright 2014-2018 - condret, pancake */ #include #include @@ -57,40 +57,37 @@ static int i4004_get_ins_len (ut8 hex) { } static int i4004dis (RAsmOp *op, const ut8 *buf, int len) { - const size_t basz = sizeof (op->buf_asm)-1; - char *basm = op->buf_asm; int rlen = i4004_get_ins_len (*buf); - ut8 high = (*buf & 0xf0)>>4; + ut8 high = (*buf & 0xf0) >> 4; ut8 low = (*buf & 0xf); - - if (rlen > len) return op->size = 0; - switch (high) { - case 0: strcpy (basm, low? "invalid": "nop"); break; - case 1: snprintf (basm, basz, "jcn %d 0x%02x", low, buf[1]); break; - case 2: - if (rlen == 1) - snprintf (basm, basz, "src r%d", (low & 0xe)); - else snprintf (basm, basz, "fim r%d, 0x%02x", (low & 0xe), buf[1]); - break; - case 3: snprintf (basm, basz, "fin r%d", (low & 0xe)); break; - case 4: snprintf (basm, basz, "jun 0x%03x", ((ut16)(low<<8) | buf[1])); break; - case 5: snprintf (basm, basz, "jms 0x%03x", ((ut16)(low<<8) | buf[1])); break; - case 6: snprintf (basm, basz, "inc r%d", low); break; - case 7: snprintf (basm, basz, "isz r%d, 0x%02x", low, buf[1]); break; - case 8: snprintf (basm, basz, "add r%d", low); break; - case 9: snprintf (basm, basz, "sub r%d", low); break; - case 10: snprintf (basm, basz, "ld r%d", low); break; - case 11: snprintf (basm, basz, "xch r%d", low); break; - case 12: snprintf (basm, basz, "bbl %d", low); break; - case 13: snprintf (basm, basz, "ldm %d", low); break; - case 14: - strncpy (basm, i4004_e[low], basz); - basm[basz] = '\0'; - break; - case 15: - strncpy (basm, i4004_f[low], basz); - basm[basz] = '\0'; - break; + const char *buf_asm = "invalid"; + if (rlen > len) { + return op->size = 0; } + switch (high) { + case 0: buf_asm = low? "invalid": "nop"; break; + case 1: buf_asm = sdb_fmt ("jcn %d 0x%02x", low, buf[1]); break; + case 2: + if (rlen == 1) { + buf_asm = sdb_fmt ("src r%d", (low & 0xe)); + } else { + buf_asm = sdb_fmt ("fim r%d, 0x%02x", (low & 0xe), buf[1]); + } + break; + case 3: buf_asm = sdb_fmt ("fin r%d", (low & 0xe)); break; + case 4: buf_asm = sdb_fmt ("jun 0x%03x", ((ut16)(low<<8) | buf[1])); break; + case 5: buf_asm = sdb_fmt ("jms 0x%03x", ((ut16)(low<<8) | buf[1])); break; + case 6: buf_asm = sdb_fmt ("inc r%d", low); break; + case 7: buf_asm = sdb_fmt ("isz r%d, 0x%02x", low, buf[1]); break; + case 8: buf_asm = sdb_fmt ("add r%d", low); break; + case 9: buf_asm = sdb_fmt ("sub r%d", low); break; + case 10: buf_asm = sdb_fmt ("ld r%d", low); break; + case 11: buf_asm = sdb_fmt ("xch r%d", low); break; + case 12: buf_asm = sdb_fmt ("bbl %d", low); break; + case 13: buf_asm = sdb_fmt ("ldm %d", low); break; + case 14: buf_asm = i4004_e[low]; break; + case 15: buf_asm = i4004_f[low]; break; + } + r_strbuf_set (&op->buf_asm, buf_asm); return op->size = rlen; } diff --git a/libr/asm/arch/pic/pic_baseline.c b/libr/asm/arch/pic/pic_baseline.c index 6c30271cab..3c825453ef 100644 --- a/libr/asm/arch/pic/pic_baseline.c +++ b/libr/asm/arch/pic/pic_baseline.c @@ -2,7 +2,6 @@ #include "pic_baseline.h" - static const PicBaselineOpInfo pic_baseline_op_info[PIC_BASELINE_OPCODE_INVALID] = { { "nop", PIC_BASELINE_OP_ARGS_NONE }, { "option", PIC_BASELINE_OP_ARGS_NONE }, @@ -42,7 +41,6 @@ static const PicBaselineOpInfo pic_baseline_op_info[PIC_BASELINE_OPCODE_INVALID] { "xorlw", PIC_BASELINE_OP_ARGS_8K } }; - PicBaselineOpcode pic_baseline_get_opcode(ut16 instr) { if (instr & 0xf000) { return PIC_BASELINE_OPCODE_INVALID; @@ -193,10 +191,10 @@ const PicBaselineOpInfo *pic_baseline_get_op_info(PicBaselineOpcode opcode) { } -int pic_baseline_disassemble(RAsm *a, RAsmOp *op, const ut8 *b, int l) { +int pic_baseline_disassemble(RAsmOp *op, char *opbuf, const ut8 *b, int l) { #define EMIT_INVALID { \ op->size = 1; \ - strncpy (op->buf_asm, "invalid", sizeof(op->buf_asm) - 1); \ + strcpy (opbuf, "invalid"); \ return 1; \ } if (!b || l < 2) { @@ -218,53 +216,39 @@ int pic_baseline_disassemble(RAsm *a, RAsmOp *op, const ut8 *b, int l) { op->size = 2; + const char *buf_asm = "invalid"; switch (op_info->args) { case PIC_BASELINE_OP_ARGS_NONE: - strncpy (op->buf_asm, op_info->mnemonic, sizeof(op->buf_asm) - 1); + buf_asm = op_info->mnemonic; break; case PIC_BASELINE_OP_ARGS_2F: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_2F_MASK_F); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_2F_MASK_F); break; case PIC_BASELINE_OP_ARGS_3F: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_3F_MASK_F); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_3F_MASK_F); break; case PIC_BASELINE_OP_ARGS_3K: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_3K_MASK_K); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_3K_MASK_K); break; case PIC_BASELINE_OP_ARGS_1D_5F: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x, %c", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_1D_5F_MASK_F, + buf_asm = sdb_fmt ("%s 0x%x, %c", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_1D_5F_MASK_F, (instr & PIC_BASELINE_OP_ARGS_1D_5F_MASK_D) >> 5 ? 'f' : 'w'); break; case PIC_BASELINE_OP_ARGS_5F: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_5F_MASK_F); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_5F_MASK_F); break; case PIC_BASELINE_OP_ARGS_3B_5F: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x, 0x%x", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_3B_5F_MASK_F, + buf_asm = sdb_fmt ("%s 0x%x, 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_3B_5F_MASK_F, (instr & PIC_BASELINE_OP_ARGS_3B_5F_MASK_B) >> 5); break; case PIC_BASELINE_OP_ARGS_8K: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_8K_MASK_K); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_8K_MASK_K); break; case PIC_BASELINE_OP_ARGS_9K: - snprintf (op->buf_asm, sizeof(op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_BASELINE_OP_ARGS_9K_MASK_K); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_9K_MASK_K); break; } + strcpy (opbuf, buf_asm); return op->size; -} \ No newline at end of file +} diff --git a/libr/asm/arch/pic/pic_baseline.h b/libr/asm/arch/pic/pic_baseline.h index cb90233b5c..da5b88962e 100644 --- a/libr/asm/arch/pic/pic_baseline.h +++ b/libr/asm/arch/pic/pic_baseline.h @@ -76,6 +76,6 @@ typedef enum { PicBaselineOpcode pic_baseline_get_opcode(ut16 instr); const PicBaselineOpInfo *pic_baseline_get_op_info(PicBaselineOpcode opcode); -int pic_baseline_disassemble(RAsm *a, RAsmOp *op, const ut8 *b, int l); +int pic_baseline_disassemble(RAsmOp *op, char *opbuf, const ut8 *b, int l); #endif //PIC_BASELINE_H diff --git a/libr/asm/arch/pic/pic_midrange.c b/libr/asm/arch/pic/pic_midrange.c index 258509efef..50f6bb5cc2 100644 --- a/libr/asm/arch/pic/pic_midrange.c +++ b/libr/asm/arch/pic/pic_midrange.c @@ -154,14 +154,14 @@ const PicMidrangeOpInfo *pic_midrange_get_op_info (PicMidrangeOpcode opcode) { return &pic_midrange_op_info[opcode]; } -int pic_midrange_disassemble (RAsm *a, RAsmOp *op, const ut8 *b, int l) { +int pic_midrange_disassemble (RAsmOp *op, char *opbuf, const ut8 *b, int l) { char fsr_op[6]; st16 branch; #define EMIT_INVALID \ { \ op->size = 2; \ - strncpy (op->buf_asm, "invalid", sizeof (op->buf_asm) - 1); \ + strcpy (opbuf, "invalid"); \ return 1; \ } if (!b || l < 2) { @@ -183,73 +183,52 @@ int pic_midrange_disassemble (RAsm *a, RAsmOp *op, const ut8 *b, int l) { op->size = 2; + const char *buf_asm = NULL; switch (op_info->args) { case PIC_MIDRANGE_OP_ARGS_NONE: - strncpy (op->buf_asm, op_info->mnemonic, - sizeof (op->buf_asm) - 1); + buf_asm = op_info->mnemonic; break; case PIC_MIDRANGE_OP_ARGS_2F: - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_MIDRANGE_OP_ARGS_2F_MASK_F); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_2F_MASK_F); break; case PIC_MIDRANGE_OP_ARGS_7F: - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_MIDRANGE_OP_ARGS_7F_MASK_F); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_7F_MASK_F); break; case PIC_MIDRANGE_OP_ARGS_1D_7F: - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s 0x%x, %c", - op_info->mnemonic, + buf_asm = sdb_fmt ("%s 0x%x, %c", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_1D_7F_MASK_F, - (instr & PIC_MIDRANGE_OP_ARGS_1D_7F_MASK_D) >> 7 ? - 'f' : - 'w'); + (instr & PIC_MIDRANGE_OP_ARGS_1D_7F_MASK_D) >> 7 ? 'f' : 'w'); break; case PIC_MIDRANGE_OP_ARGS_1N_6K: if (opcode == PIC_MIDRANGE_OPCODE_ADDFSR) { - snprintf ( - op->buf_asm, sizeof (op->buf_asm), - "%s FSR%d, 0x%x", op_info->mnemonic, - (instr & PIC_MIDRANGE_OP_ARGS_1N_6K_MASK_N) >> - 6, - instr & PIC_MIDRANGE_OP_ARGS_1N_6K_MASK_K); + buf_asm = sdb_fmt ( "%s FSR%d, 0x%x", op_info->mnemonic, + (instr & PIC_MIDRANGE_OP_ARGS_1N_6K_MASK_N) >> + 6, instr & PIC_MIDRANGE_OP_ARGS_1N_6K_MASK_K); } else { - snprintf ( - op->buf_asm, sizeof (op->buf_asm), - "%s 0x%x[FSR%d]", op_info->mnemonic, + buf_asm = sdb_fmt ("%s 0x%x[FSR%d]", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_1N_6K_MASK_K, - (instr & PIC_MIDRANGE_OP_ARGS_1N_6K_MASK_N) >> - 6); + (instr & PIC_MIDRANGE_OP_ARGS_1N_6K_MASK_N) >> 6); } break; case PIC_MIDRANGE_OP_ARGS_3B_7F: - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s 0x%x, %d", - op_info->mnemonic, - instr & PIC_MIDRANGE_OP_ARGS_3B_7F_MASK_F, + buf_asm = sdb_fmt ("%s 0x%x, %d", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_3B_7F_MASK_F, (instr & PIC_MIDRANGE_OP_ARGS_3B_7F_MASK_B) >> 7); break; case PIC_MIDRANGE_OP_ARGS_4K: - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_MIDRANGE_OP_ARGS_4K_MASK_K); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_4K_MASK_K); break; case PIC_MIDRANGE_OP_ARGS_8K: - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_MIDRANGE_OP_ARGS_8K_MASK_K); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_8K_MASK_K); break; case PIC_MIDRANGE_OP_ARGS_9K: branch = (instr & PIC_MIDRANGE_OP_ARGS_9K_MASK_K); branch |= ((branch & 0x100) ? 0xfe00 : 0); - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s %s0x%x", + buf_asm = sdb_fmt ("%s %s0x%x", op_info->mnemonic, branch < 0 ? "-" : "", branch < 0 ? -branch : branch); break; case PIC_MIDRANGE_OP_ARGS_11K: - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s 0x%x", - op_info->mnemonic, - instr & PIC_MIDRANGE_OP_ARGS_11K_MASK_K); + buf_asm = sdb_fmt ("%s 0x%x", op_info->mnemonic, instr & PIC_MIDRANGE_OP_ARGS_11K_MASK_K); break; case PIC_MIDRANGE_OP_ARGS_1N_2M: snprintf ( @@ -257,10 +236,11 @@ int pic_midrange_disassemble (RAsm *a, RAsmOp *op, const ut8 *b, int l) { PicMidrangeFsrOps[instr & PIC_MIDRANGE_OP_ARGS_1N_2M_MASK_M], (instr & PIC_MIDRANGE_OP_ARGS_1N_2M_MASK_N) >> 2); - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s %s", - op_info->mnemonic, fsr_op); + buf_asm = sdb_fmt ("%s %s", op_info->mnemonic, fsr_op); break; } - + if (buf_asm) { + strcpy (opbuf, buf_asm); + } return op->size; } diff --git a/libr/asm/arch/pic/pic_midrange.h b/libr/asm/arch/pic/pic_midrange.h index 66841ebcbc..0bc498bada 100644 --- a/libr/asm/arch/pic/pic_midrange.h +++ b/libr/asm/arch/pic/pic_midrange.h @@ -98,6 +98,6 @@ typedef enum { PicMidrangeOpcode pic_midrange_get_opcode (ut16 instr); const PicMidrangeOpInfo *pic_midrange_get_op_info (PicMidrangeOpcode opcode); -int pic_midrange_disassemble (RAsm *a, RAsmOp *op, const ut8 *b, int l); +int pic_midrange_disassemble (RAsmOp *op, char *opbuf, const ut8 *b, int l); #endif // PIC_MIDRANGE_H diff --git a/libr/asm/arch/pic/pic_pic18.c b/libr/asm/arch/pic/pic_pic18.c index aff038735e..0039632a3a 100644 --- a/libr/asm/arch/pic/pic_pic18.c +++ b/libr/asm/arch/pic/pic_pic18.c @@ -30,201 +30,188 @@ static struct { ut8 optype; //and some magical hocus pocus ;) } ops[] = { - {0xf000, 0xffff, "nop", NO_ARG}, - {0xef00, 0xefff, "goto", GOTO_T}, - {0xee00, 0xee3f, "lfsr", LFSR_T}, - {0xec00, 0xedff, "call", CALL_T}, - {0xe700, 0xe7ff, "bnn", N_T}, - {0xe600, 0xe6ff, "bn", N_T}, - {0xe500, 0xe5ff, "bnov", N_T}, - {0xe400, 0xe4ff, "bov", N_T}, - {0xe300, 0xe3ff, "bnc", N_T}, - {0xe200, 0xe2ff, "bc", N_T}, - {0xe100, 0xe1ff, "bnz", N_T}, - {0xe000, 0xe0ff, "bz", N_T}, - {0xd800, 0xdfff, "rcall", NEX_T}, - {0xd000, 0xd7ff, "bra", NEX_T}, - {0xc000, 0xcfff, "movff", F32_T}, - {0xb000, 0xbfff, "btfsc", BAF_T}, - {0xa000, 0xafff, "btfss", BAF_T}, - {0x9000, 0x9fff, "bcf", BAF_T}, - {0x8000, 0x8fff, "bsf", BAF_T}, - {0x7000, 0x7fff, "btg", BAF_T}, - {0x6e00, 0x6fff, "movwf", AF_T}, - {0x6c00, 0x6dff, "negf", AF_T}, - {0x6a00, 0x6bff, "clrf", AF_T}, - {0x6800, 0x69ff, "setf", AF_T}, - {0x6600, 0x67ff, "tstfsz", AF_T}, - {0x6400, 0x65ff, "cpfsgt", AF_T}, - {0x6200, 0x63ff, "cpfseq", AF_T}, - {0x6000, 0x61ff, "cpfslt", AF_T}, - {0x5c00, 0x5fff, "subwf", DAF_T}, - {0x5800, 0x5bff, "subwfb", DAF_T}, - {0x5400, 0x57ff, "subfwb", DAF_T}, - {0x5000, 0x53ff, "movf", DAF_T}, - {0x4c00, 0x4fff, "dcfsnz", DAF_T}, - {0x4800, 0x4bff, "infsnz", DAF_T}, - {0x4400, 0x47ff, "rlncf", DAF_T}, - {0x4000, 0x43ff, "rrncf", DAF_T}, - {0x3c00, 0x3fff, "incfsz", DAF_T}, - {0x3800, 0x3bff, "swapf", DAF_T}, - {0x3400, 0x37ff, "rlcf", DAF_T}, - {0x3000, 0x33ff, "rrcf", DAF_T}, - {0x2c00, 0x2fff, "decfsz", DAF_T}, - {0x2800, 0x2bff, "incf", DAF_T}, - {0x2400, 0x27ff, "addwf", DAF_T}, - {0x2000, 0x23ff, "addwfc", DAF_T}, - {0x1c00, 0x1fff, "comf", DAF_T}, - {0x1800, 0x1bff, "xorwf", DAF_T}, - {0x1400, 0x17ff, "andwf", DAF_T}, - {0x1000, 0x13ff, "iorwf", DAF_T}, - {0xf00, 0xfff, "addlw", K_T}, - {0xe00, 0xeff, "movlw", K_T}, - {0xd00, 0xdff, "mullw", K_T}, - {0xc00, 0xcff, "retlw", K_T}, - {0xb00, 0xbff, "andlw", K_T}, - {0xa00, 0xaff, "xorlw", K_T}, - {0x900, 0x9ff, "iorlw", K_T}, - {0x800, 0x8ff, "sublw", K_T}, - {0x400, 0x7ff, "decf", DAF_T}, - {0x200, 0x3ff, "mulwf", AF_T}, - {0x100, 0x10f, "movlb", SHK_T}, - {0xff, 0xff, "reset", NO_ARG}, - {0x12, 0x13, "return", S_T}, - {0x10, 0x11, "retfie", S_T}, - {0xf, 0xf, "tblwt+*", NO_ARG}, - {0xe, 0xe, "tblwt*-", NO_ARG}, - {0xd, 0xd, "tblwt*+", NO_ARG}, - {0xc, 0xc, "tblwt*", NO_ARG}, - {0xb, 0xb, "tblrd+*", NO_ARG}, - {0xa, 0xa, "tblrd*-", NO_ARG}, - {0x9, 0x9, "tblrd*+", NO_ARG}, - {0x8, 0x8, "tblrd*", NO_ARG}, - {0x7, 0x7, "daw", NO_ARG}, - {0x6, 0x6, "pop", NO_ARG}, - {0x5, 0x5, "push", NO_ARG}, - {0x4, 0x4, "clrwdt", NO_ARG}, - {0x3, 0x3, "sleep", NO_ARG}, - {0x0, 0x0, "nop", NO_ARG}, - {0x0, 0xffff, "invalid", NO_ARG}, + {0xf000, 0xffff, "nop", NO_ARG}, + {0xef00, 0xefff, "goto", GOTO_T}, + {0xee00, 0xee3f, "lfsr", LFSR_T}, + {0xec00, 0xedff, "call", CALL_T}, + {0xe700, 0xe7ff, "bnn", N_T}, + {0xe600, 0xe6ff, "bn", N_T}, + {0xe500, 0xe5ff, "bnov", N_T}, + {0xe400, 0xe4ff, "bov", N_T}, + {0xe300, 0xe3ff, "bnc", N_T}, + {0xe200, 0xe2ff, "bc", N_T}, + {0xe100, 0xe1ff, "bnz", N_T}, + {0xe000, 0xe0ff, "bz", N_T}, + {0xd800, 0xdfff, "rcall", NEX_T}, + {0xd000, 0xd7ff, "bra", NEX_T}, + {0xc000, 0xcfff, "movff", F32_T}, + {0xb000, 0xbfff, "btfsc", BAF_T}, + {0xa000, 0xafff, "btfss", BAF_T}, + {0x9000, 0x9fff, "bcf", BAF_T}, + {0x8000, 0x8fff, "bsf", BAF_T}, + {0x7000, 0x7fff, "btg", BAF_T}, + {0x6e00, 0x6fff, "movwf", AF_T}, + {0x6c00, 0x6dff, "negf", AF_T}, + {0x6a00, 0x6bff, "clrf", AF_T}, + {0x6800, 0x69ff, "setf", AF_T}, + {0x6600, 0x67ff, "tstfsz", AF_T}, + {0x6400, 0x65ff, "cpfsgt", AF_T}, + {0x6200, 0x63ff, "cpfseq", AF_T}, + {0x6000, 0x61ff, "cpfslt", AF_T}, + {0x5c00, 0x5fff, "subwf", DAF_T}, + {0x5800, 0x5bff, "subwfb", DAF_T}, + {0x5400, 0x57ff, "subfwb", DAF_T}, + {0x5000, 0x53ff, "movf", DAF_T}, + {0x4c00, 0x4fff, "dcfsnz", DAF_T}, + {0x4800, 0x4bff, "infsnz", DAF_T}, + {0x4400, 0x47ff, "rlncf", DAF_T}, + {0x4000, 0x43ff, "rrncf", DAF_T}, + {0x3c00, 0x3fff, "incfsz", DAF_T}, + {0x3800, 0x3bff, "swapf", DAF_T}, + {0x3400, 0x37ff, "rlcf", DAF_T}, + {0x3000, 0x33ff, "rrcf", DAF_T}, + {0x2c00, 0x2fff, "decfsz", DAF_T}, + {0x2800, 0x2bff, "incf", DAF_T}, + {0x2400, 0x27ff, "addwf", DAF_T}, + {0x2000, 0x23ff, "addwfc", DAF_T}, + {0x1c00, 0x1fff, "comf", DAF_T}, + {0x1800, 0x1bff, "xorwf", DAF_T}, + {0x1400, 0x17ff, "andwf", DAF_T}, + {0x1000, 0x13ff, "iorwf", DAF_T}, + {0xf00, 0xfff, "addlw", K_T}, + {0xe00, 0xeff, "movlw", K_T}, + {0xd00, 0xdff, "mullw", K_T}, + {0xc00, 0xcff, "retlw", K_T}, + {0xb00, 0xbff, "andlw", K_T}, + {0xa00, 0xaff, "xorlw", K_T}, + {0x900, 0x9ff, "iorlw", K_T}, + {0x800, 0x8ff, "sublw", K_T}, + {0x400, 0x7ff, "decf", DAF_T}, + {0x200, 0x3ff, "mulwf", AF_T}, + {0x100, 0x10f, "movlb", SHK_T}, + {0xff, 0xff, "reset", NO_ARG}, + {0x12, 0x13, "return", S_T}, + {0x10, 0x11, "retfie", S_T}, + {0xf, 0xf, "tblwt+*", NO_ARG}, + {0xe, 0xe, "tblwt*-", NO_ARG}, + {0xd, 0xd, "tblwt*+", NO_ARG}, + {0xc, 0xc, "tblwt*", NO_ARG}, + {0xb, 0xb, "tblrd+*", NO_ARG}, + {0xa, 0xa, "tblrd*-", NO_ARG}, + {0x9, 0x9, "tblrd*+", NO_ARG}, + {0x8, 0x8, "tblrd*", NO_ARG}, + {0x7, 0x7, "daw", NO_ARG}, + {0x6, 0x6, "pop", NO_ARG}, + {0x5, 0x5, "push", NO_ARG}, + {0x4, 0x4, "clrwdt", NO_ARG}, + {0x3, 0x3, "sleep", NO_ARG}, + {0x0, 0x0, "nop", NO_ARG}, + {0x0, 0xffff, "invalid", NO_ARG}, }; -int pic_pic18_disassemble(RAsm *a, RAsmOp *op, const ut8 *b, int l) { +int pic_pic18_disassemble(RAsmOp *op, char *opbuf, const ut8 *b, int l) { int i; - if(l<2){//well noone loves reading bitstream of size zero or 1 !! - strncpy (op->buf_asm,"invalid", R_ASM_BUFSIZE); + if (l < 2) { //well noone loves reading bitstream of size zero or 1 !! + strcpy (opbuf, "invalid"); op->size = l; return -1; - } ut16 instr = *(ut16 *)b; //instruction // if still redundan code is reported think of this of instr=0x2 + const char *buf_asm = "invalid"; + strcpy (opbuf, buf_asm); + for (i = 0;ops[i].opmin != (ops[i].opmin & instr) || ops[i].opmax != (ops[i].opmax | instr); i++); - if (ops[i].opmin == 0 && ops[i].opmax==0xffff) { - strncpy (op->buf_asm, ops[i].name, R_ASM_BUFSIZE); + if (ops[i].opmin == 0 && ops[i].opmax == 0xffff) { + strcpy (opbuf, ops[i].name); op->size = 2; return -1; } op->size = 2; switch (ops[i].optype) { case NO_ARG: - strncpy (op->buf_asm, ops[i].name, R_ASM_BUFSIZE); - return 2; + buf_asm = ops[i].name; + break; case N_T: case K_T: - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x", - ops[i].name, instr & 0xff); + buf_asm = sdb_fmt ("%s 0x%x", ops[i].name, instr & 0xff); break; case DAF_T: - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x, %d, %d", - ops[i].name, instr & 0xff, (instr >> 9) & 1, (instr >> 8) & 1); + buf_asm = sdb_fmt ("%s 0x%x, %d, %d", ops[i].name, instr & 0xff, (instr >> 9) & 1, (instr >> 8) & 1); break; case AF_T: - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x, %d", ops[i].name, - instr & 0xff, (instr >> 8) & 1); + buf_asm = sdb_fmt ("%s 0x%x, %d", ops[i].name, instr & 0xff, (instr >> 8) & 1); break; - case BAF_T: - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x, %d, %d", ops[i].name, - instr & 0xff, (instr >> 9) & 0x7, (instr >> 8) & 0x1); + buf_asm = sdb_fmt ("%s 0x%x, %d, %d", ops[i].name, instr & 0xff, (instr >> 9) & 0x7, (instr >> 8) & 0x1); break; case NEX_T: - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x", - ops[i].name, instr & 0x7ff); + buf_asm = sdb_fmt ("%s 0x%x", ops[i].name, instr & 0x7ff); break; - case CALL_T: { + case CALL_T: if (l < 4) { - strcpy (op->buf_asm, "invalid"); return -1; } op->size = 4; + { ut32 dword_instr = *(ut32 *)b; //I dont even know how the bits are arranged but it works !!! //`the wierdness of little endianess` if (dword_instr >> 28 != 0xf) { - strcpy (op->buf_asm, "invalid"); return -1; } - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x, %d", ops[i].name, - (dword_instr & 0xff) | - (dword_instr >> 8 & 0xfff00), - (dword_instr >> 8) & 0x1); + buf_asm = sdb_fmt ("%s 0x%x, %d", ops[i].name, + (dword_instr & 0xff) | (dword_instr >> 8 & 0xfff00), (dword_instr >> 8) & 0x1); + } break; - } - case GOTO_T: { + case GOTO_T: if (l < 4) { - strcpy (op->buf_asm, "invalid"); return -1; } + { op->size = 4; ut32 dword_instr = *(ut32 *)b; if (dword_instr >> 28 != 0xf) { - strcpy (op->buf_asm, "invalid"); return -1; } - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x", ops[i].name, - ((dword_instr & 0xff) | ((dword_instr & 0xfff0000) >>8) )*2); + buf_asm = sdb_fmt ("%s 0x%x", ops[i].name, + ((dword_instr & 0xff) | ((dword_instr & 0xfff0000) >>8) )*2); + } break; - } - case F32_T: { + case F32_T: if (l < 4) { - strcpy (op->buf_asm, "invalid"); return -1; } op->size = 4; + { ut32 dword_instr = *(ut32 *)b; if (dword_instr >> 28 != 0xf) { - strcpy (op->buf_asm, "invalid"); return -1; } - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x, 0x%x", ops[i].name, - dword_instr & 0xfff, - (dword_instr >> 16) & 0xfff); + buf_asm = sdb_fmt ("%s 0x%x, 0x%x", ops[i].name, + dword_instr & 0xfff, (dword_instr >> 16) & 0xfff); + } break; - } case SHK_T: - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s 0x%x", - ops[i].name, instr & 0xf); + buf_asm = sdb_fmt ("%s 0x%x", ops[i].name, instr & 0xf); break; case S_T: - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %d", - ops[i].name, instr & 0x1); + buf_asm = sdb_fmt ("%s %d", ops[i].name, instr & 0x1); break; case LFSR_T: { op->size = 4; ut32 dword_instr = *(ut32 *)b; if (dword_instr >> 28 != 0xf) { - strcpy (op->buf_asm, "invalid"); return -1; } ut8 reg_n = (dword_instr >> 4) & 0x3; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s, %d", ops[i].name, - fsr[reg_n], (dword_instr & 0xf) << 8 | - ((dword_instr >> 16) & 0xff)); + buf_asm = sdb_fmt ("%s %s, %d", ops[i].name, fsr[reg_n], + (dword_instr & 0xf) << 8 | ((dword_instr >> 16) & 0xff)); break; } default: - sprintf (op->buf_asm, "unknown args"); + buf_asm = "unknown args"; }; + strcpy (opbuf, buf_asm); return op->size; -} \ No newline at end of file +} diff --git a/libr/asm/arch/pic/pic_pic18.h b/libr/asm/arch/pic/pic_pic18.h index 91b4b0cf14..88dbb05b13 100644 --- a/libr/asm/arch/pic/pic_pic18.h +++ b/libr/asm/arch/pic/pic_pic18.h @@ -5,6 +5,6 @@ #include -int pic_pic18_disassemble(RAsm *a, RAsmOp *op, const ut8 *b, int l); +int pic_pic18_disassemble(RAsmOp *op, char *opbuf, const ut8 *b, int l); #endif //PIC_PIC18_H diff --git a/libr/asm/arch/riscv/riscv.c b/libr/asm/arch/riscv/riscv.c index dae7f19cd2..7e402da3cd 100644 --- a/libr/asm/arch/riscv/riscv.c +++ b/libr/asm/arch/riscv/riscv.c @@ -285,37 +285,30 @@ static struct riscv_opcode *get_opcode (insn_t word) { return (struct riscv_opcode *)riscv_hash[OP_HASH_IDX (word)]; } -static int -riscv_disassemble(RAsm *a, RAsmOp *rop, insn_t word, int xlen, int len) { +static int riscv_disassemble(RAsm *a, RAsmOp *rop, insn_t word, int xlen, int len) { const bool no_alias = false; const struct riscv_opcode *op = get_opcode (word); if (!op) { return -1; } - - for(; op < &riscv_opcodes[NUMOPCODES]; op++) { + for (; op < &riscv_opcodes[NUMOPCODES]; op++) { if ( !(op->match_func)(op, word) ) { continue; } - if (no_alias && (op->pinfo & INSN_ALIAS)) { continue; } - if (isdigit ((ut8)op->subset[0]) && atoi (op->subset) != xlen ) { continue; } - if (op->name && op->args) { - snprintf (rop->buf_asm, sizeof (rop->buf_asm), "%s", op->name); - get_insn_args (rop->buf_asm, op->args, word, a->pc); + r_asm_op_set_asm (rop, op->name); + get_insn_args (op->name, op->args, word, a->pc); return 0; - } else { - sprintf (rop->buf_asm, "invalid word(%"PFMT64x")", (ut64)word); - return -1; } + r_asm_op_set_asm (rop, sdb_fmt ("invalid word(%"PFMT64x")", (ut64)word)); + return -1; } - return 0; } diff --git a/libr/asm/arch/snes/snesdis.c b/libr/asm/arch/snes/snesdis.c index 0592b19e2c..9134f7ec76 100644 --- a/libr/asm/arch/snes/snesdis.c +++ b/libr/asm/arch/snes/snesdis.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2015-2016 - condret@runas-racer.com */ +/* radare - LGPL - Copyright 2015-2018 - condret, pancake */ #include #include @@ -9,50 +9,49 @@ static int snesDisass(int M_flag, int X_flag, ut64 pc, RAsmOp *op, const ut8 *buf, int len){ snes_op_t *s_op = &snes_op[buf[0]]; - int op_len = snes_op_get_size(M_flag, X_flag, s_op); - if (len < op_len) + int op_len = snes_op_get_size (M_flag, X_flag, s_op); + if (len < op_len) { return 0; + } + const char *buf_asm = "invalid"; switch (s_op->len) { case SNES_OP_8BIT: - strncpy (op->buf_asm, s_op->name, sizeof (op->buf_asm) - 1); + buf_asm = s_op->name; break; case SNES_OP_16BIT: if (*buf % 0x20 == 0x10 || *buf == 0x80) { // relative branch - snprintf (op->buf_asm, sizeof (op->buf_asm), s_op->name, pc + 2 + (st8)buf[1]); + buf_asm = sdb_fmt (s_op->name, pc + 2 + (st8)buf[1]); } else { - snprintf (op->buf_asm, sizeof (op->buf_asm), s_op->name, buf[1]); + buf_asm = sdb_fmt (s_op->name, buf[1]); } break; case SNES_OP_24BIT: if (*buf == 0x44 || *buf == 0x54) { // mvp and mvn - snprintf (op->buf_asm, sizeof (op->buf_asm), s_op->name, buf[1], buf[2]); + buf_asm = sdb_fmt (s_op->name, buf[1], buf[2]); } else if (*buf == 0x82) { // brl - snprintf (op->buf_asm, sizeof (op->buf_asm), s_op->name, pc + 3 + (st16)ut8p_bw(buf+1)); + buf_asm = sdb_fmt (s_op->name, pc + 3 + (st16)ut8p_bw(buf + 1)); } else { - snprintf (op->buf_asm, sizeof (op->buf_asm), s_op->name, ut8p_bw(buf+1)); + buf_asm = sdb_fmt (s_op->name, ut8p_bw (buf + 1)); } break; case SNES_OP_32BIT: - snprintf (op->buf_asm, sizeof (op->buf_asm), s_op->name, buf[1]|buf[2]<<8|buf[3]<<16); + buf_asm = sdb_fmt (s_op->name, buf[1]|buf[2]<<8|buf[3]<<16); break; case SNES_OP_IMM_M: if (M_flag) { - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s #0x%02x", - s_op->name, buf[1]); + buf_asm = sdb_fmt ("%s #0x%02x", s_op->name, buf[1]); } else { - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s #0x%04x", - s_op->name, ut8p_bw (buf+1)); + buf_asm = sdb_fmt ("%s #0x%04x", s_op->name, ut8p_bw (buf + 1)); } break; case SNES_OP_IMM_X: if (X_flag) { - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s #0x%02x", - s_op->name, buf[1]); + buf_asm = sdb_fmt ("%s #0x%02x", s_op->name, buf[1]); } else { - snprintf (op->buf_asm, sizeof (op->buf_asm), "%s #0x%04x", - s_op->name, ut8p_bw (buf+1)); + buf_asm = sdb_fmt ("%s #0x%04x", s_op->name, ut8p_bw (buf + 1)); } break; } + r_strbuf_set (&op->buf_asm, buf_asm); return op_len; } diff --git a/libr/asm/arch/spc700/spc700dis.c b/libr/asm/arch/spc700/spc700dis.c index 3abad73fed..1764142b32 100644 --- a/libr/asm/arch/spc700/spc700dis.c +++ b/libr/asm/arch/spc700/spc700dis.c @@ -21,21 +21,24 @@ static int spc700OpLength(int spcoptype){ static int spc700Disass(RAsmOp *op, const ut8 *buf, int len) { int foo = spc700OpLength (spc_op_table[buf[0]].type); - if (len < foo) + if (len < foo) { return 0; + } + const char *buf_asm = "invalid"; switch (spc_op_table[buf[0]].type) { case SPC_OP: - sprintf (op->buf_asm, "%s", spc_op_table[buf[0]].name); + buf_asm = spc_op_table[buf[0]].name; break; case SPC_ARG8_1: - sprintf (op->buf_asm, spc_op_table[buf[0]].name, buf[1]); + buf_asm = sdb_fmt (spc_op_table[buf[0]].name, buf[1]); break; case SPC_ARG8_2: - sprintf (op->buf_asm, spc_op_table[buf[0]].name, buf[1], buf[2]); + buf_asm = sdb_fmt (spc_op_table[buf[0]].name, buf[1], buf[2]); break; case SPC_ARG16: - sprintf (op->buf_asm, spc_op_table[buf[0]].name, buf[1]+0x100*buf[2]); + buf_asm = sdb_fmt (spc_op_table[buf[0]].name, buf[1]+0x100*buf[2]); break; } + r_asm_op_set_asm (op, buf_asm); return foo; } diff --git a/libr/asm/arch/whitespace/wsdis.c b/libr/asm/arch/whitespace/wsdis.c index 2cd212a7b0..7a3744e715 100644 --- a/libr/asm/arch/whitespace/wsdis.c +++ b/libr/asm/arch/whitespace/wsdis.c @@ -1,4 +1,5 @@ -// dso, condret, pancake 2014-2017 +/* radare - LGPL - Copyright 2014-2018 - dso, condret, pancake */ + #include #include #include @@ -18,45 +19,37 @@ enum { WS_OP_IO }; -static int get_ws_pref_optype(const ut8 *buf, int len) -{ - if (len) { - switch (buf[0]) { - case ' ': - return WS_OP_STACK; - case '\t': - return WS_OP_PREF; - case 10: - return WS_OP_FLOW; - default: - return WS_OP_NOP; - } +static int get_ws_pref_optype(const ut8 *buf, int len) { + if (len < 1) { + return WS_OP_UNK; + } + switch (buf[0]) { + case ' ': return WS_OP_STACK; + case '\t': return WS_OP_PREF; + case 10: return WS_OP_FLOW; + default: return WS_OP_NOP; } - return WS_OP_UNK; } static int get_ws_suf_optype(const ut8 *buf, int len) { - if (len > 0) { - switch (buf[0]) { - case ' ': - return WS_OP_ARITH; - case '\t': - return WS_OP_HEAP; - case 10: - return WS_OP_IO; - default: - return WS_OP_NOP; - } + if (len < 1) { + return WS_OP_UNK; + } + switch (buf[0]) { + case ' ': return WS_OP_ARITH; + case '\t': return WS_OP_HEAP; + case 10: return WS_OP_IO; + default: return WS_OP_NOP; } - return WS_OP_UNK; } WS_API int get_ws_optype(const ut8 *buf, int len) { const ut8 *ptr; if (get_ws_pref_optype (buf, len) == WS_OP_PREF) { ptr = buf + 1; - while (get_ws_suf_optype (ptr, len - (ptr - buf)) == WS_OP_NOP) + while (get_ws_suf_optype (ptr, len - (ptr - buf)) == WS_OP_NOP) { ptr++; + } return get_ws_suf_optype (ptr, len - (ptr - buf)); } return get_ws_pref_optype (buf, len); @@ -79,12 +72,10 @@ WS_API const ut8 *get_ws_next_token(const ut8 *buf, int len) { static st32 get_ws_val(const ut8 *buf, int len) { ut8 sig; - const ut8 *tok; - int i, ret; - ret = 0; - tok = get_ws_next_token (buf, len); + int i, ret = 0; + const ut8 *tok = get_ws_next_token (buf, len); sig = (*tok == '\t'); - len = len - (tok - buf) - 1; + len -= (tok - buf) - 1; for (i = 0; i < 30; i++) { // XXX : conceptually wrong tok++; tok = get_ws_next_token (tok, len); @@ -118,7 +109,7 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { case WS_OP_UNK: return op->size = 0; case WS_OP_NOP: - sprintf (op->buf_asm, "nop"); + r_strbuf_set (&op->buf_asm, "nop"); return op->size = 1; case WS_OP_STACK: ptr++; @@ -127,15 +118,12 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { } switch (*get_ws_next_token (ptr, len - 1)) { case ' ': - if (-1 == test_ws_token_exist (get_ws_next_token (ptr, len - 1), 10, len - 1)) { + if (test_ws_token_exist (get_ws_next_token (ptr, len - 1), 10, len - 1) == -1) { return op->size = 0; } - sprintf (op->buf_asm, "push"); - { - int n = test_ws_token_exist (ptr - 1, 10, len); - sprintf (op->buf_asm + 4, " %d", n); - return op->size = n; - } + int n = test_ws_token_exist (ptr - 1, 10, len); + r_strbuf_set (&op->buf_asm, sdb_fmt ("push %d", n)); + return op->size = n; case 10: ptr = get_ws_next_token (ptr, len - 1) + 1; ptr = get_ws_next_token (ptr, len - (ptr - buf)); @@ -144,13 +132,13 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { } switch (*ptr) { case ' ': - sprintf (op->buf_asm, "dup"); + r_strbuf_set (&op->buf_asm, "dup"); break; case '\t': - sprintf (op->buf_asm, "swap"); + r_strbuf_set (&op->buf_asm, "swap"); break; case 10: - sprintf (op->buf_asm, "pop"); + r_strbuf_set (&op->buf_asm, "pop"); break; } return op->size = ptr - buf + 1; @@ -162,24 +150,24 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { } switch (*ptr) { case ' ': - sprintf (op->buf_asm, "copy"); + r_strbuf_set (&op->buf_asm, "copy"); break; case 10: - sprintf (op->buf_asm, "slide"); + r_strbuf_set (&op->buf_asm, "slide"); break; case '\t': - sprintf (op->buf_asm, "illegal_stack_t"); + r_strbuf_set (&op->buf_asm, "illegal_stack_t"); return op->size = ptr - buf + 1; } ptr++; if (-1 == test_ws_token_exist (ptr, 10, len - (ptr - buf) - 1)) { - op->buf_asm[0] = '\0'; // XXX + r_strbuf_set (&op->buf_asm, ""); return op->size = 0; } - if (strlen (op->buf_asm) < 6) { - sprintf (&op->buf_asm[strlen (op->buf_asm)], " %d", get_ws_val (ptr, len - (ptr - buf) - 1)); + if (r_strbuf_length (&op->buf_asm) < 6) { + r_strbuf_append (&op->buf_asm, sdb_fmt (" %d", get_ws_val (ptr, len - (ptr - buf) - 1))); } - return op->size = test_ws_token_exist (ptr, 10, len - (ptr - buf) - 1) + ptr - buf + 1; // +1? + return op->size = test_ws_token_exist (ptr, 10, len - (ptr - buf) - 1) + ptr - buf + 1; // +1? } case WS_OP_HEAP: ptr = get_ws_next_token (ptr + 1, len - 1) + 1; @@ -189,13 +177,13 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { } switch (*ptr) { case ' ': - sprintf (op->buf_asm, "store"); + r_strbuf_set (&op->buf_asm, "store"); break; case '\t': - sprintf (op->buf_asm, "load"); + r_strbuf_set (&op->buf_asm, "load"); break; case 10: - sprintf (op->buf_asm, "illegal_heap"); + r_strbuf_set (&op->buf_asm, "illegal_heap"); break; } return op->size = ptr - buf + 1; @@ -214,10 +202,10 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { } switch (*ptr) { case ' ': - sprintf (op->buf_asm, "putc"); + r_strbuf_set (&op->buf_asm, "putc"); return op->size = ptr - buf + 1; case '\t': - sprintf (op->buf_asm, "puti"); + r_strbuf_set (&op->buf_asm, "puti"); return op->size = ptr - buf + 1; } break; @@ -229,21 +217,23 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { } switch (*ptr) { case ' ': - sprintf (op->buf_asm, "getc"); + r_strbuf_set (&op->buf_asm, "getc"); return op->size = ptr - buf + 1; case '\t': - sprintf (op->buf_asm, "geti"); + r_strbuf_set (&op->buf_asm, "geti"); return op->size = ptr - buf + 1; } } - sprintf (op->buf_asm, "illegal_io"); + r_strbuf_set (&op->buf_asm, "illegal_io"); return op->size = ptr - buf + 1; + break; case WS_OP_ARITH: ptr = get_ws_next_token (ptr + 1, len - 1) + 1; ptr = get_ws_next_token (ptr, len - (ptr - buf)); if (!ptr) { return op->size = 0; } + const char *buf_asm = NULL; switch (*ptr) { case ' ': ptr++; @@ -252,17 +242,11 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { return op->size = 0; } switch (*ptr) { - case ' ': - sprintf (op->buf_asm, "add"); - break; - case '\t': - sprintf (op->buf_asm, "sub"); - break; - case 10: - sprintf (op->buf_asm, "mul"); - break; + case ' ': buf_asm = "add"; break; + case '\t': buf_asm = "sub"; break; + case 10: buf_asm = "mul"; break; } - return op->size = ptr - buf + 1; + break; case '\t': ptr++; ptr = get_ws_next_token (ptr, len - (ptr - buf)); @@ -270,18 +254,17 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { return op->size = 0; } switch (*ptr) { - case ' ': - sprintf (op->buf_asm, "div"); - break; - case '\t': - sprintf (op->buf_asm, "mod"); - break; - case 10: - sprintf (op->buf_asm, "illegal_ar_t"); + case ' ': buf_asm = "div"; break; + case '\t': buf_asm = "mod"; break; + case 10: buf_asm = "illegal_ar_t"; break; } break; case 10: - sprintf (op->buf_asm, "illegal_ar"); + buf_asm = "illegal_ar"; + break; + } + if (buf_asm) { + r_strbuf_set (&op->buf_asm, buf_asm); } return op->size = ptr - buf + 1; case WS_OP_FLOW: @@ -297,9 +280,9 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { return op->size = 0; } if (*ptr == 10) { - sprintf (op->buf_asm, "exit"); + r_strbuf_set (&op->buf_asm, "exit"); } else { - sprintf (op->buf_asm, "illegal_fl_lf"); + r_strbuf_set (&op->buf_asm, "illegal_fl_lf"); } return op->size = ptr - buf + 1; case '\t': @@ -310,22 +293,22 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { } switch (*ptr) { case 10: - sprintf (op->buf_asm, "ret"); + r_strbuf_set (&op->buf_asm, "ret"); return op->size = ptr - buf + 1; case '\t': - sprintf (op->buf_asm, "jn"); + r_strbuf_set (&op->buf_asm, "jn"); break; case ' ': - sprintf (op->buf_asm, "jz"); + r_strbuf_set (&op->buf_asm, "jz"); break; } ptr++; if (-1 == test_ws_token_exist (ptr, 10, len - (ptr - buf))) { - op->buf_asm[0] = '\0'; + r_strbuf_set (&op->buf_asm, ""); return op->size = 0; } - if (strlen (op->buf_asm) == 2) { - sprintf (&op->buf_asm[2], " %d", get_ws_val (ptr, len - (ptr - buf) - 1)); + if (r_strbuf_length (&op->buf_asm) == 2) { + r_strbuf_append (&op->buf_asm, sdb_fmt (" %d", get_ws_val (ptr, len - (ptr - buf) - 1))); } return op->size = ptr - buf + test_ws_token_exist (ptr, 10, len - (ptr - buf)) + 1; case ' ': @@ -335,25 +318,22 @@ WS_API int wsdis(RAsmOp *op, const ut8 *buf, int len) { return op->size = 0; } switch (*ptr) { - case 10: - sprintf (op->buf_asm, "jmp"); - break; - case '\t': - sprintf (op->buf_asm, "call"); - break; - case ' ': - sprintf (op->buf_asm, "mark"); - break; + case 10: buf_asm = "jmp"; break; + case '\t': buf_asm = "call"; break; + case ' ': buf_asm = "mark"; break; } ptr++; if (-1 == test_ws_token_exist (ptr, 10, len - (ptr - buf))) { - op->buf_asm[0] = '\0'; + buf_asm = NULL; return op->size = 0; } - sprintf (&op->buf_asm[strlen (op->buf_asm)], " %d", get_ws_val (ptr, len - (ptr - buf) - 1)); + if (buf_asm) { + r_strbuf_set (&op->buf_asm, buf_asm); + } + r_strbuf_append (&op->buf_asm, sdb_fmt (" %d", get_ws_val (ptr, len - (ptr - buf) - 1))); return op->size = ptr - buf + test_ws_token_exist (ptr, 10, len - (ptr - buf)) + 1; } } - sprintf (op->buf_asm, "wtf"); + r_strbuf_set (&op->buf_asm, "wtf"); return op->size = 0; } diff --git a/libr/asm/arch/z80/z80.c b/libr/asm/arch/z80/z80.c index e7aeb00fe2..0263407364 100644 --- a/libr/asm/arch/z80/z80.c +++ b/libr/asm/arch/z80/z80.c @@ -9,29 +9,20 @@ #include "z80_tab.h" static ut8 z80_op_24_branch_index_res (ut8 hex) { - if (hex < 0x40) + if (hex < 0x40) { return hex; - switch (hex) { - case 0x46: - return 0x40; - case 0x4e: - return 0x41; - case 0x56: - return 0x42; - case 0x5e: - return 0x43; - case 0x66: - return 0x44; - case 0x6e: - return 0x45; - case 0x76: - return 0x46; - case 0x7e: - return 0x47; } - if (hex > 0x7f) - return hex-0x38; - return 0xc8; + switch (hex) { + case 0x46: return 0x40; + case 0x4e: return 0x41; + case 0x56: return 0x42; + case 0x5e: return 0x43; + case 0x66: return 0x44; + case 0x6e: return 0x45; + case 0x76: return 0x46; + case 0x7e: return 0x47; + } + return (hex > 0x7f)? hex - 0x38: 0xc8; } static int z80OpLength (const ut8 *buf, int len) { @@ -41,8 +32,9 @@ static int z80OpLength (const ut8 *buf, int len) { return 0; op = z80_op; if (op[buf[0]].type & Z80_OP_UNK) { - if (len < 2) + if (len < 2) { return 0; + } if (op[buf[0]].type & Z80_ENC0) { op = (z80_opcode *)op[buf[0]].op_moar; type = op[z80_fddd_branch_index_res(buf[1])].type; @@ -53,19 +45,24 @@ static int z80OpLength (const ut8 *buf, int len) { } else { type = op[buf[0]].type; } - if (type & Z80_OP8) + if (type & Z80_OP8) { ret++; - if ((type & Z80_ARG8) && !(type & Z80_ARG16)) //XXX + } + if ((type & Z80_ARG8) && !(type & Z80_ARG16)) { //XXX ret++; - if (type & Z80_OP16) + } + if (type & Z80_OP16) { ret += 2; - if (type & Z80_ARG16) + } + if (type & Z80_ARG16) { ret += 2; - if (type & Z80_OP24) + } + if (type & Z80_OP24) { ret += 3; - if (ret > len) + } + if (ret > len) { return 0; - + } return ret; } @@ -75,49 +72,59 @@ FUNC_ATTR_USED static int z80Disass (RAsmOp *op, const ut8 *buf, int len) { z80_opcode *z_op; char **cb_tab; ut8 res; - if (!ret) + if (!ret) { return ret; - z_op = z80_op; - - switch (z_op[buf[0]].type) { - case Z80_OP8: - sprintf (op->buf_asm, "%s", z_op[buf[0]].name); - return ret; - case Z80_OP8^Z80_ARG8: - sprintf (op->buf_asm, z_op[buf[0]].name, buf[1]); - return ret; - case Z80_OP8^Z80_ARG16: - sprintf (op->buf_asm, z_op[buf[0]].name, buf[1]+(buf[2]<<8)); - return ret; - case Z80_OP16: - cb_tab = (char **) z_op[buf[0]].op_moar; - sprintf (op->buf_asm, "%s", cb_tab[buf[1]]); - return ret; - case Z80_OP_UNK^Z80_ENC1: - z_op = (z80_opcode *)z_op[buf[0]].op_moar; - res = z80_ed_branch_index_res (buf[1]); - if (z_op[res].type == Z80_OP16) - sprintf (op->buf_asm, "%s", z_op[res].name); - if (z_op[res].type == (Z80_OP16^Z80_ARG16)) - sprintf (op->buf_asm, z_op[res].name, buf[2]+(buf[3]<<8)); - return ret; - case Z80_OP_UNK^Z80_ENC0: - z_op = (z80_opcode *)z_op[buf[0]].op_moar; - res = z80_fddd_branch_index_res (buf[1]); - if (z_op[res].type == Z80_OP16) - sprintf (op->buf_asm, "%s", z_op[res].name); - if (z_op[res].type == (Z80_OP16^Z80_ARG16)) - sprintf (op->buf_asm, z_op[res].name, buf[2]+(buf[3]<<8)); - if (z_op[res].type == (Z80_OP16^Z80_ARG8)) - sprintf (op->buf_asm, z_op[res].name, buf[2], buf[3]); - if (z_op[res].type == (Z80_OP24^Z80_ARG8)) { - cb_tab = (char **) z_op[res].op_moar; - sprintf (op->buf_asm, cb_tab[z80_op_24_branch_index_res (buf[3])], buf[2]); - } - if (z_op[res].type == (Z80_OP16^Z80_ARG8^Z80_ARG16)) - sprintf (op->buf_asm, z_op[res].name, buf[2], buf[3]); } - if (!strcmp (op->buf_asm, "invalid")) + z_op = z80_op; + const char *buf_asm = "invalid"; + switch (z_op[buf[0]].type) { + case Z80_OP8: + buf_asm = sdb_fmt ("%s", z_op[buf[0]].name); + break; + case Z80_OP8^Z80_ARG8: + buf_asm = sdb_fmt (z_op[buf[0]].name, buf[1]); + break; + case Z80_OP8^Z80_ARG16: + buf_asm = sdb_fmt (z_op[buf[0]].name, buf[1]+(buf[2]<<8)); + break; + case Z80_OP16: + cb_tab = (char **) z_op[buf[0]].op_moar; + buf_asm = sdb_fmt ("%s", cb_tab[buf[1]]); + break; + case Z80_OP_UNK^Z80_ENC1: + z_op = (z80_opcode *)z_op[buf[0]].op_moar; + res = z80_ed_branch_index_res (buf[1]); + if (z_op[res].type == Z80_OP16) { + buf_asm = sdb_fmt ("%s", z_op[res].name); + } + if (z_op[res].type == (Z80_OP16^Z80_ARG16)) { + buf_asm = sdb_fmt (z_op[res].name, buf[2]+(buf[3]<<8)); + } + break; + case Z80_OP_UNK^Z80_ENC0: + z_op = (z80_opcode *)z_op[buf[0]].op_moar; + res = z80_fddd_branch_index_res (buf[1]); + if (z_op[res].type == Z80_OP16) { + buf_asm = sdb_fmt ("%s", z_op[res].name); + } + if (z_op[res].type == (Z80_OP16^Z80_ARG16)) { + buf_asm = sdb_fmt (z_op[res].name, buf[2]+(buf[3]<<8)); + } + if (z_op[res].type == (Z80_OP16^Z80_ARG8)) { + buf_asm = sdb_fmt (z_op[res].name, buf[2], buf[3]); + } + if (z_op[res].type == (Z80_OP24^Z80_ARG8)) { + cb_tab = (char **) z_op[res].op_moar; + buf_asm = sdb_fmt (cb_tab[z80_op_24_branch_index_res (buf[3])], buf[2]); + } + if (z_op[res].type == (Z80_OP16^Z80_ARG8^Z80_ARG16)) { + buf_asm = sdb_fmt (z_op[res].name, buf[2], buf[3]); + } + break; + } + if (!strcmp (buf_asm, "invalid")) { ret = 0; + } + r_asm_op_set_asm (op, buf_asm); return ret; } diff --git a/libr/asm/asm.c b/libr/asm/asm.c index f2dbf3fa25..a4815361ba 100644 --- a/libr/asm/asm.c +++ b/libr/asm/asm.c @@ -18,6 +18,7 @@ char *directives[] = { static RAsmPlugin *asm_static_plugins[] = { R_ASM_STATIC_PLUGINS }; +/* pseudo.c - private api */ static int r_asm_pseudo_align(RAsmCode *acode, RAsmOp *op, char *input) { acode->code_align = r_num_math (NULL, input); return 0; @@ -36,8 +37,8 @@ static int r_asm_pseudo_string(RAsmOp *op, char *input, int zero) { input++; } len = r_str_unescape (input) + zero; - r_hex_bin2str ((ut8*)input, len, op->buf_hex); - strncpy ((char*)op->buf, input, R_ASM_BUFSIZE - 1); + r_hex_bin2str ((ut8*)input, len, r_strbuf_get (&op->buf_hex)); + r_strbuf_set (&op->buf, input); // uh? return len; } @@ -62,10 +63,11 @@ static inline int r_asm_pseudo_org(RAsm *a, char *input) { return 0; } +// wtf isnt this the same as r_asm_op_set_hex() ?? static inline int r_asm_pseudo_hex(RAsmOp *op, char *input) { - int len = r_hex_str2bin (input, op->buf); - // non null terminated string - r_str_ncpy (op->buf_hex, r_str_trim_head_tail (input), sizeof op->buf_hex); + // assume input fits inside op->buf + int len = r_hex_str2bin (input, (ut8*)r_strbuf_get (&op->buf)); + r_asm_op_set_hex (op, r_str_trim_head_tail (input)); if (len < 0) { len = -len; len--; @@ -84,19 +86,20 @@ static inline int r_asm_pseudo_intN(RAsm *a, RAsmOp *op, char *input, int n) { return 0; } // XXX honor endian here + ut8 *buf = (ut8*)r_strbuf_get (&op->buf); if (n == 2) { s = (short)s64; - r_write_ble16 (op->buf, s, a->big_endian); + r_write_ble16 (buf, s, a->big_endian); } else if (n == 4) { i = (int)s64; - r_write_ble32 (op->buf, i, a->big_endian); + r_write_ble32 (buf, i, a->big_endian); } else if (n == 8) { l = (long int)s64; - r_write_ble64 (op->buf, l, a->big_endian); + r_write_ble64 (buf, l, a->big_endian); } else { return 0; } - r_hex_bin2str (op->buf, n, op->buf_hex); + r_hex_bin2str (buf, n, r_strbuf_get (&op->buf_hex)); return n; } @@ -117,24 +120,31 @@ static inline int r_asm_pseudo_byte(RAsmOp *op, char *input) { r_str_replace_char (input, ',', ' '); len = r_str_word_count (input); r_str_word_set0 (input); + ut8 *buf = malloc (len); + if (!buf) { + return 0; + } for (i = 0; i < len; i++) { const char *word = r_str_word_get0 (input, i); int num = (int)r_num_math (NULL, word); - op->buf[i] = num; + buf[i] = num; } - r_hex_bin2str (op->buf, len, op->buf_hex); + r_asm_op_set_buf (op, buf, len); + free (buf); return len; } static inline int r_asm_pseudo_fill(RAsmOp *op, char *input) { - int i, repeat=0, size=0, value=0; + int i, repeat = 0, size=0, value=0; sscanf (input, "%d,%d,%d", &repeat, &size, &value); // use r_num? size *= repeat; if (size > 0) { + ut8 *buf = malloc (size); for (i = 0; i < size; i++) { - op->buf[i] = value; + buf[i] = value; } - r_hex_bin2str (op->buf, size, op->buf_hex); + r_asm_op_set_buf (op, buf, size); + free (buf); } else { size = 0; } @@ -142,15 +152,13 @@ static inline int r_asm_pseudo_fill(RAsmOp *op, char *input) { } static inline int r_asm_pseudo_incbin(RAsmOp *op, char *input) { - int skip = 0; - int count = 0; int bytes_read = 0; r_str_replace_char (input, ',', ' '); // int len = r_str_word_count (input); r_str_word_set0 (input); //const char *filename = r_str_word_get0 (input, 0); - skip = (int)r_num_math (NULL, r_str_word_get0 (input, 1)); - count = (int)r_num_math (NULL,r_str_word_get0 (input, 2)); + int skip = (int)r_num_math (NULL, r_str_word_get0 (input, 1)); + int count = (int)r_num_math (NULL,r_str_word_get0 (input, 2)); char *content = r_file_slurp (input, &bytes_read); if (skip > 0) { skip = skip > bytes_read ? bytes_read : skip; @@ -164,7 +172,6 @@ static inline int r_asm_pseudo_incbin(RAsmOp *op, char *input) { r_buf_free (op->buf_inc); op->buf_inc = r_buf_new_with_string (content + skip); // Terminate the original buffer - op->buf_hex[0] = '\0'; free (content); return count; } @@ -401,8 +408,13 @@ R_API int r_asm_set_pc(RAsm *a, ut64 pc) { return true; } +static bool isInvalid (RAsmOp *op) { + const char *buf_asm = r_strbuf_get (&op->buf_asm); + return (buf_asm && *buf_asm && !strcmp (buf_asm, "invalid")); +} + R_API int r_asm_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - int oplen, ret; + int ret; if (!a || !buf || !op) { return -1; } @@ -412,17 +424,15 @@ R_API int r_asm_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (len < 1) { return 0; } - op->buf_asm[0] = '\0'; + r_asm_op_set_asm (op, ""); if (a->pcalign) { const int mod = a->pc % a->pcalign; if (mod) { op->size = a->pcalign - mod; - strcpy (op->buf_asm, "unaligned"); - *op->buf_hex = 0; - if ((op->size * 4) >= sizeof (op->buf_hex)) { - oplen = (sizeof (op->buf_hex) / 4) - 1; - } - r_hex_bin2str (buf, op->size, op->buf_hex); + r_strbuf_set (&op->buf_asm, "unaligned"); + r_strbuf_set (&op->buf_hex, ""); + // assume op->size after pcalign-mod will be small enough to not need checks here + r_hex_bin2str (buf, op->size, r_strbuf_get (&op->buf_hex)); return -1; } } @@ -442,7 +452,7 @@ R_API int r_asm_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (ret < 0) { ret = 0; } - oplen = r_asm_op_get_size (op); + int oplen = r_asm_op_get_size (op); if (op->bitsize > 0) { oplen = op->size = op->bitsize / 8; a->bitshift += op->bitsize % 8; @@ -457,38 +467,40 @@ R_API int r_asm_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (oplen < 1) { oplen = 1; } - if (!op->buf_asm[0] || op->size < 1 || !strcmp (op->buf_asm, "invalid")) { + + if (op->size < 1 || isInvalid (op)) { if (a->invhex) { if (a->bits == 16) { ut16 b = r_read_le16 (buf); - snprintf (op->buf_asm, sizeof (op->buf_asm), ".word 0x%04x", b); + r_strbuf_set (&op->buf_asm, sdb_fmt (".word 0x%04x", b)); } else { ut32 b = r_read_le32 (buf); - snprintf (op->buf_asm, sizeof (op->buf_asm), ".dword 0x%08x", b); + r_strbuf_set (&op->buf_asm, sdb_fmt (".dword 0x%08x", b)); } // TODO: something for 64bits too? } else { - strcpy (op->buf_asm, "invalid"); + r_strbuf_set (&op->buf_asm, "invalid"); } } if (a->ofilter) { - r_parse_parse (a->ofilter, op->buf_asm, op->buf_asm); + char *buf_asm = r_strbuf_get (&op->buf_asm); + r_parse_parse (a->ofilter, buf_asm, buf_asm); } - // XXX this is crap and needs proper fix - int dstlen = R_MIN ((R_ASM_BUFSIZE -1), oplen); - int cpylen = R_MIN (dstlen, len); - if (cpylen > 0) { - memcpy (op->buf, buf, cpylen); - const int addrbytes = a->user ? ((RCore *)a->user)->io->addrbytes : 1; - int len = R_MIN (addrbytes * oplen, (sizeof (op->buf_hex) - 1) / 2); - // len is the length in input buffer, not len of output, so we must / 2 - r_hex_bin2str (op->buf, len, op->buf_hex); + if (op->size > 0) { + r_asm_op_set_buf (op, buf, R_MAX (0, op->size)); } return ret; } typedef int (*Ase)(RAsm *a, RAsmOp *op, const char *buf); +static bool assemblerMatches(RAsm *a, RAsmPlugin *h) { + if (!a || !h->arch || !h->assemble || !has_bits (h, a->bits)) { + return false; + } + return (!strncmp (a->cur->arch, h->arch, strlen (a->cur->arch))); +} + static Ase findAssembler(RAsm *a, const char *kw) { Ase ase = NULL; RAsmPlugin *h; @@ -497,11 +509,7 @@ static Ase findAssembler(RAsm *a, const char *kw) { return a->acur->assemble; } r_list_foreach (a->plugins, iter, h) { - if (h->arch && h->assemble - && has_bits (h, a->bits) - && !strncmp (a->cur->arch, - h->arch, - strlen (a->cur->arch))) { + if (assemblerMatches (a, h)) { if (kw) { if (strstr (h->name, kw)) { return h->assemble; @@ -569,7 +577,6 @@ R_API void r_asm_list_directives() { R_API int r_asm_assemble(RAsm *a, RAsmOp *op, const char *buf) { int ret = 0; char *b = strdup (buf); - if (!b) { return 0; } @@ -596,16 +603,21 @@ R_API int r_asm_assemble(RAsm *a, RAsmOp *op, const char *buf) { ret = ase (a, op, b); } } + // XXX delete this block, the ase thing should be setting asm, buf and hex if (op && ret > 0) { - r_hex_bin2str (op->buf, ret, op->buf_hex); - op->size = ret; - op->buf_hex[ret*2] = 0; - strncpy (op->buf_asm, b, R_ASM_BUFSIZE - 1); + op->size = ret; // XXX shouldnt be necessary + r_asm_op_set_asm (op, b); // XXX ase should be updating this already, isnt? + ut8 *ophex = (ut8*)r_strbuf_get (&op->buf_hex); + if (!*ophex) { + ut8 *opbuf = (ut8*)r_strbuf_get (&op->buf); + r_asm_op_set_buf (op, opbuf, ret); + } } free (b); return ret; } +// TODO: Use RStrBuf api here pls R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) { RStrBuf *buf_asm; RAsmCode *acode; @@ -636,9 +648,10 @@ R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) { ret = 1; } if (a->ofilter) { - r_parse_parse (a->ofilter, op.buf_asm, op.buf_asm); + char *op_buf_asm = r_strbuf_get (&op.buf_asm); + r_parse_parse (a->ofilter, op_buf_asm, op_buf_asm); } - r_strbuf_append (buf_asm, op.buf_asm); + r_strbuf_append (buf_asm, r_strbuf_get (&op.buf_asm)); r_strbuf_append (buf_asm, "\n"); } acode->buf_asm = r_strbuf_drain (buf_asm); @@ -718,7 +731,6 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) { } lbuf = strdup (buf); acode->code_align = 0; - memset (&op, 0, sizeof (op)); /* consider ,, an alias for a newline */ lbuf = r_str_replace (lbuf, ",,", "\n", true); @@ -787,7 +799,6 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) { r_asm_set_pc (a, pc); for (idx = ret = i = j = 0, off = a->pc, acode->buf_hex[0] = '\0'; i <= ctr; i++, idx += ret) { - memset (buf_token, 0, R_ASM_BUFSIZE); strncpy (buf_token, tokens[i], R_ASM_BUFSIZE - 1); if (inComment) { if (!strncmp (ptr_start, "*/", 2)) { @@ -834,13 +845,12 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) { if (is_a_label) { //if (stage != 2) { if (ptr_start[1] != 0 && ptr_start[1] != ' ') { - char food[64]; *ptr = 0; if (acode->code_align) { off += (acode->code_align - (off % acode->code_align)); } + char food[64]; snprintf (food, sizeof (food), "0x%"PFMT64x"", off); - ht_insert (a->flags, ptr_start, food); // TODO: warning when redefined r_asm_code_set_equ (acode, ptr_start, food); @@ -976,26 +986,31 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) { if (stage == STAGES - 1) { if (ret < 1) { eprintf ("Cannot assemble '%s' at line %d\n", ptr_start, linenum); - free(lbuf); + free (lbuf); return r_asm_code_free (acode); } acode->len = idx + ret; char *newbuf = realloc (acode->buf, (idx + ret) * 2); if (!newbuf) { - free(lbuf); + free (lbuf); return r_asm_code_free (acode); } acode->buf = (ut8*)newbuf; - newbuf = realloc (acode->buf_hex, strlen (acode->buf_hex) + strlen (op.buf_hex) + r_buf_size (op.buf_inc) + 1); + + int newlen = strlen (acode->buf_hex) + r_strbuf_length (&op.buf_hex) + + r_buf_size (op.buf_inc) + 128; + newbuf = realloc (acode->buf_hex, newlen); if (!newbuf) { - free(lbuf); + free (lbuf); return r_asm_code_free (acode); } acode->buf_hex = newbuf; - memcpy (acode->buf + idx, op.buf, ret); - strcat (acode->buf_hex, op.buf_hex); + acode->buf = malloc (4095); + memcpy (acode->buf + idx, r_strbuf_get (&op.buf), r_strbuf_length (&op.buf)); // ret); + // XXX slow. use strbuf pls + strcat (acode->buf_hex, r_strbuf_get (&op.buf_hex)); if (r_buf_size (op.buf_inc) > 1) { - if (strlen (acode->buf_hex) > 0) { + if (*acode->buf_hex) { strcat (acode->buf_hex, "\n"); } char *s = r_buf_free_to_string (op.buf_inc); @@ -1013,31 +1028,8 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) { return acode; } -R_API int r_asm_modify(RAsm *a, ut8 *buf, int field, ut64 val) { - if (a->cur && a->cur->modify) { - return a->cur->modify (a, buf, field, val); - } - return false; -} - -R_API char *r_asm_op_get_hex(RAsmOp *op) { - return strdup (op->buf_hex); -} - -R_API char *r_asm_op_get_asm(RAsmOp *op) { - return strdup (op->buf_asm); -} - -R_API int r_asm_op_get_size(RAsmOp *op) { - int len; - if (!op) { - return 1; - } - len = op->size - op->payload; - if (len < 1) { - len = 1; - } - return len; +R_API bool r_asm_modify(RAsm *a, ut8 *buf, int field, ut64 val) { + return (a->cur && a->cur->modify) ? a->cur->modify (a, buf, field, val): false; } R_API int r_asm_get_offset(RAsm *a, int type, int idx) { // link to rbin @@ -1048,30 +1040,21 @@ R_API int r_asm_get_offset(RAsm *a, int type, int idx) { // link to rbin } R_API char *r_asm_describe(RAsm *a, const char* str) { - if (!a->pair) { - return NULL; - } - return sdb_get (a->pair, str, 0); + return (a && a->pair)? sdb_get (a->pair, str, 0): NULL; } R_API RList* r_asm_get_plugins(RAsm *a) { return a->plugins; } -/* new simplified API */ - R_API bool r_asm_set_arch(RAsm *a, const char *name, int bits) { - if (!r_asm_use (a, name)) { - return false; - } - return r_asm_set_bits (a, bits); + return r_asm_use (a, name)? r_asm_set_bits (a, bits): false; } /* to ease the use of the native bindings (not used in r2) */ R_API char *r_asm_to_string(RAsm *a, ut64 addr, const ut8 *b, int l) { - RAsmCode *code; r_asm_set_pc (a, addr); - code = r_asm_mdisassemble (a, b, l); + RAsmCode *code = r_asm_mdisassemble (a, b, l); if (code) { char *buf_asm = code->buf_asm; code->buf_asm = NULL; @@ -1137,6 +1120,9 @@ R_API int r_asm_mnemonics_byname(RAsm *a, const char *name) { R_API RAsmCode* r_asm_rasm_assemble(RAsm *a, const char *buf, bool use_spp) { char *lbuf = strdup (buf); + if (!lbuf) { + return NULL; + } RAsmCode *acode; if (use_spp) { Output out; diff --git a/libr/asm/code.c b/libr/asm/code.c index 5ff9e090b7..5eb420d5d5 100644 --- a/libr/asm/code.c +++ b/libr/asm/code.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2017 - pancake */ +/* radare - LGPL - Copyright 2009-2018 - pancake */ #include #include diff --git a/libr/asm/meson.build b/libr/asm/meson.build index 1338324c8c..0e46796a13 100644 --- a/libr/asm/meson.build +++ b/libr/asm/meson.build @@ -1,5 +1,6 @@ files = [ 'asm.c', + 'op.c', 'code.c', 'p/asm_6502.c', 'p/asm_8051.c', diff --git a/libr/asm/op.c b/libr/asm/op.c new file mode 100644 index 0000000000..1f51959b61 --- /dev/null +++ b/libr/asm/op.c @@ -0,0 +1,73 @@ +/* radare - LGPL - Copyright 2018 - pancake */ + +#include + +R_API RAsmOp *r_asm_op_new() { + return R_NEW0 (RAsmOp); +} + +R_API void r_asm_op_free(RAsmOp *op) { + r_asm_op_fini (op); + free (op); +} + +R_API void r_asm_op_init(RAsmOp *op) { + memset (op, 0, sizeof (*op)); +} + +R_API void r_asm_op_fini(RAsmOp *op) { + r_strbuf_fini (&op->buf); + r_strbuf_fini (&op->buf_asm); + r_strbuf_fini (&op->buf_hex); + r_buf_fini (op->buf_inc); +} + +// accessors +R_API char *r_asm_op_get_hex(RAsmOp *op) { + return r_strbuf_get (&op->buf_hex); +} + +R_API char *r_asm_op_get_asm(RAsmOp *op) { + return r_strbuf_get (&op->buf_asm); +} + +R_API int r_asm_op_get_size(RAsmOp *op) { + if (!op) { + return 1; + } + const int len = op->size - op->payload; + return R_MAX (1, len); +} + +R_API void r_asm_op_set_asm(RAsmOp *op, const char *str) { + r_strbuf_set (&op->buf_asm, str); +} + +R_API void r_asm_op_set_hex(RAsmOp *op, const char *str) { + r_strbuf_set (&op->buf_hex, str); + ut8 *bin = (ut8*)strdup (str); + if (bin) { + int len = r_hex_str2bin (str, bin); + if (len > 0) { + r_strbuf_setbin (&op->buf, bin, len); + } + free (bin); + } +} + +R_API void r_asm_op_set_hexbuf(RAsmOp *op, const ut8 *buf, int len) { + char *hex = malloc (len * 4); + if (hex) { + r_hex_bin2str (buf, len, hex); + r_asm_op_set_hex (op, hex); + free (hex); + } + // TODO: update the op->buf too? +} + +R_API void r_asm_op_set_buf(RAsmOp *op, const ut8 *buf, int len) { + if (op && buf && len >= 0) { + r_strbuf_setbin (&op->buf, buf, len); + r_asm_op_set_hexbuf (op, buf, len); + } +} diff --git a/libr/asm/p/asm_6502.c b/libr/asm/p/asm_6502.c index 18b645bf4c..f38a46a430 100644 --- a/libr/asm/p/asm_6502.c +++ b/libr/asm/p/asm_6502.c @@ -1,7 +1,6 @@ -/* radare - LGPL - Copyright 2012-2015 - pancake, condret */ +/* radare - LGPL - Copyright 2012-2018 - pancake, condret */ // copypasta from asm_gb.c - #include #include #include @@ -10,9 +9,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { int dlen = _6502Disass (a->pc, op, buf, len); - if(dlen<0) dlen=0; - op->size = dlen; - return dlen; + return op->size = R_MAX (dlen, 0); } RAsmPlugin r_asm_plugin_6502 = { diff --git a/libr/asm/p/asm_arc.c b/libr/asm/p/asm_arc.c index 7422c0f5fb..2425d4068e 100644 --- a/libr/asm/p/asm_arc.c +++ b/libr/asm/p/asm_arc.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2012-2016 - pancake */ +/* radare - LGPL - Copyright 2012-2018 - pancake */ #include #include @@ -67,7 +67,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (len < 2) { return -1; } - buf_global = op->buf_asm; + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; if (len > sizeof (bytes)) { len = sizeof (bytes); @@ -86,14 +86,14 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; disasm_obj.mach = 0; - op->buf_asm[0] = '\0'; + r_strbuf_set (&op->buf_asm, ""); if (a->bits == 16) { op->size = ARCompact_decodeInstr ((bfd_vma)Offset, &disasm_obj); } else { op->size = ARCTangent_decodeInstr ((bfd_vma)Offset, &disasm_obj); } if (op->size == -1) { - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); + r_strbuf_set (&op->buf_asm, "(data)"); } return op->size; } diff --git a/libr/asm/p/asm_arm_as.c b/libr/asm/p/asm_arm_as.c index bd514e732a..2e80465fce 100644 --- a/libr/asm/p/asm_arm_as.c +++ b/libr/asm/p/asm_arm_as.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2015 pancake */ +/* radare - LGPL - Copyright 2015-2018 pancake */ #include #include @@ -18,16 +18,16 @@ static int assemble(RAsm *a, RAsmOp *op, const char *buf) { const char *bitconfig = ""; char *ipath, *opath; - int ifd, ofd; char *as = NULL; char asm_buf[R_ASM_BUFSIZE]; int len = 0; - ifd = r_file_mkstemp ("r_as", &ipath); - if (ifd == -1) + int ifd = r_file_mkstemp ("r_as", &ipath); + if (ifd == -1) { return -1; + } - ofd = r_file_mkstemp ("r_as", &opath); + int ofd = r_file_mkstemp ("r_as", &opath); if (ofd == -1) { free (ipath); return -1; @@ -52,29 +52,30 @@ static int assemble(RAsm *a, RAsmOp *op, const char *buf) { "%s\n" ".ascii \"ENDMARK\"\n", bitconfig, buf); // a->pc ?? - write (ifd, asm_buf, len); - close (ifd); + (void)write (ifd, asm_buf, len); + (void)close (ifd); if (!r_sys_cmdf ("%s %s -o %s", as, ipath, opath)) { const ut8 *begin, *end; close (ofd); - ofd = open (opath, O_BINARY|O_RDONLY); + ofd = r_sandbox_open (opath, O_BINARY|O_RDONLY, 0644); if (ofd < 0) { free (as); free (ipath); free (opath); return -1; } - len = read (ofd, op->buf, R_ASM_BUFSIZE); - begin = r_mem_mem (op->buf, len, (const ut8*)"BEGINMARK", 9); - end = r_mem_mem (op->buf, len, (const ut8*)"ENDMARK", 7); + ut8 buf[4096]; + len = read (ofd, buf, sizeof (buf)); + begin = r_mem_mem (buf, len, (const ut8*)"BEGINMARK", 9); + end = r_mem_mem (buf, len, (const ut8*)"ENDMARK", 7); if (!begin || !end) { eprintf ("Cannot find water marks\n"); len = 0; } else { len = (int)(size_t)(end - begin - 9); if (len > 0) { - memcpy (op->buf, begin + 9, len); + r_strbuf_setbin (&op->buf, begin + 9, len); } else { len = 0; } @@ -101,6 +102,7 @@ RAsmPlugin r_asm_plugin_arm_as = { .name = "arm.as", .desc = "as ARM Assembler (use ARM_AS environment)", .arch = "arm", + .author = "pancake", .license = "LGPL3", .bits = 16|32|64, .endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG, diff --git a/libr/asm/p/asm_arm_cs.c b/libr/asm/p/asm_arm_cs.c index efef77d188..088e431d41 100644 --- a/libr/asm/p/asm_arm_cs.c +++ b/libr/asm/p/asm_arm_cs.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2013-2017 - pancake */ +/* radare2 - LGPL - Copyright 2013-2018 - pancake */ #include #include @@ -6,11 +6,39 @@ #include "../arch/arm/asm-arm.h" bool arm64ass(const char *str, ut64 addr, ut32 *op); -static int check_features(RAsm *a, cs_insn *insn); static csh cd = 0; #include "cs_mnemonics.c" +static bool check_features(RAsm *a, cs_insn *insn) { + int i; + if (!insn || !insn->detail) { + return true; + } + for (i = 0; i < insn->detail->groups_count; i++) { + int id = insn->detail->groups[i]; + switch (id) { + case ARM_GRP_ARM: + case ARM_GRP_THUMB: + case ARM_GRP_THUMB1ONLY: + case ARM_GRP_THUMB2: + continue; + default: + if (id < 128) { + continue; + } + } + const char *name = cs_group_name (cd, id); + if (!name) { + return true; + } + if (!strstr (a->features, name)) { + return false; + } + } + return true; +} + static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { static int omode = -1; static int obits = 32; @@ -44,7 +72,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } if (op) { op->size = 4; - op->buf_asm[0] = 0; + r_strbuf_set (&op->buf_asm, ""); } if (!cd || mode != omode) { ret = (a->bits == 64)? @@ -55,53 +83,45 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { goto beach; } } - if (a->syntax == R_ASM_SYNTAX_REGNUM) { - cs_option (cd, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME); - } else { - cs_option (cd, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT); - } - if (a->features && *a->features) { - cs_option (cd, CS_OPT_DETAIL, CS_OPT_ON); - } else { - cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF); - } + cs_option (cd, CS_OPT_SYNTAX, (a->syntax == R_ASM_SYNTAX_REGNUM) + ? CS_OPT_SYNTAX_NOREGNAME + : CS_OPT_SYNTAX_DEFAULT); + cs_option (cd, CS_OPT_DETAIL, (a->features && *a->features) + ? CS_OPT_ON: CS_OPT_OFF); if (!buf) { goto beach; } n = cs_disasm (cd, buf, R_MIN (4, len), a->pc, 1, &insn); - if (n < 1) { + if (n < 1 || insn->size < 1) { ret = -1; goto beach; } if (op) { op->size = 0; } - if (insn->size < 1) { - ret = -1; - goto beach; - } if (a->features && *a->features) { if (!check_features (a, insn) && op) { op->size = insn->size; - strcpy (op->buf_asm, "illegal"); + r_strbuf_set (&op->buf_asm, "illegal"); } } if (op && !op->size) { op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", + char *buf_asm = sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]?" ":"", insn->op_str); if (!disp_hash) { - r_str_rmch (op->buf_asm, '#'); + r_str_rmch (buf_asm, '#'); } + r_strbuf_set (&op->buf_asm, buf_asm); } cs_free (insn, n); beach: cs_close (&cd); if (op) { - if (!op->buf_asm[0]) { - strcpy (op->buf_asm, "invalid"); + if (!*r_strbuf_get (&op->buf_asm)) { + r_strbuf_set (&op->buf_asm, "invalid"); } return op->size; } @@ -126,27 +146,29 @@ static int assemble(RAsm *a, RAsmOp *op, const char *buf) { if (opcode == UT32_MAX) { return -1; } + ut8 opbuf[4]; if (is_thumb) { const int o = opcode >> 16; - opsize = o > 0? 4: 2; //(o&0x80 && ((o&0xe0)==0xe0))? 4: 2; + opsize = o > 0? 4: 2; if (opsize == 4) { if (a->big_endian) { - r_write_le16 (op->buf, opcode >> 16); - r_write_le16 (op->buf + 2, opcode & UT16_MAX); + r_write_le16 (opbuf, opcode >> 16); + r_write_le16 (opbuf + 2, opcode & UT16_MAX); } else { - r_write_be32 (op->buf, opcode); + r_write_be32 (opbuf, opcode); } } else if (opsize == 2) { - r_write_be16 (op->buf, opcode & UT16_MAX); + r_write_be16 (opbuf, opcode & UT16_MAX); } } else { opsize = 4; if (a->big_endian) { - r_write_le32 (op->buf, opcode); + r_write_le32 (opbuf, opcode); } else { - r_write_be32 (op->buf, opcode); + r_write_be32 (opbuf, opcode); } } + r_strbuf_setbin (&op->buf, opbuf, opsize); // XXX. thumb endian assembler needs no swap return opsize; } @@ -172,36 +194,6 @@ RAsmPlugin r_asm_plugin_arm_cs = { #endif }; -static int check_features(RAsm *a, cs_insn *insn) { - const char *name; - int i; - if (!insn || !insn->detail) { - return 1; - } - for (i = 0; i < insn->detail->groups_count; i++) { - int id = insn->detail->groups[i]; - if (id == ARM_GRP_ARM) - continue; - if (id == ARM_GRP_THUMB) - continue; - if (id == ARM_GRP_THUMB1ONLY) - continue; - if (id == ARM_GRP_THUMB2) - continue; - if (id < 128) { - continue; - } - name = cs_group_name (cd, id); - if (!name) { - return 1; - } - if (!strstr (a->features, name)) { - //eprintf ("CANNOT FIND %s\n", name); - return 0; - } - } - return 1; -} #ifndef CORELIB RLibStruct radare_plugin = { diff --git a/libr/asm/p/asm_arm_gnu.c b/libr/asm/p/asm_arm_gnu.c index eecbd06dfd..7d37c01de5 100644 --- a/libr/asm/p/asm_arm_gnu.c +++ b/libr/asm/p/asm_arm_gnu.c @@ -134,7 +134,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (a->bits < 64 && len < (a->bits / 8)) { return -1; } - buf_global = op->buf_asm; + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; /* prepare disassembler */ @@ -180,7 +180,7 @@ cpucode = 66471; obj.bytes_per_chunk = obj.bytes_per_line = (a->bits / 8); - op->buf_asm[0] = '\0'; + r_strbuf_set (&op->buf_asm, ""); if (a->bits == 64) { obj.disassembler_options = NULL; memcpy (bytes, buf, 4); @@ -193,11 +193,10 @@ cpucode = 66471; } opsize = op->size; if (op->size == -1) { - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); + r_strbuf_set (&op->buf_asm, "(data)"); op->size = 4; - } - if (strstr (op->buf_asm, "UNDEF")) { - strcpy (op->buf_asm, "undefined"); + } else if (strstr (buf_global, "UNDEF")) { + r_strbuf_set (&op->buf_asm, "undefined"); op->size = 2; opsize = 2; } diff --git a/libr/asm/p/asm_arm_winedbg.c b/libr/asm/p/asm_arm_winedbg.c index 2def737bdd..b4c8fdd2be 100644 --- a/libr/asm/p/asm_arm_winedbg.c +++ b/libr/asm/p/asm_arm_winedbg.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2017 - nibble, pancake */ +/* radare - LGPL - Copyright 2009-2018 - nibble, pancake */ #include #include @@ -23,11 +23,11 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { op->size = arm_disasm_one_insn (arminsn); const char *asmstr = winedbg_arm_insn_asm (arminsn); if (asmstr) { - r_str_ncpy (op->buf_asm, asmstr, sizeof (op->buf_asm)); - r_str_ncpy (op->buf_hex, winedbg_arm_insn_hex (arminsn), sizeof (op->buf_hex)); + r_strbuf_set (&op->buf_asm, asmstr); + r_strbuf_set (&op->buf_hex, winedbg_arm_insn_hex (arminsn)); } else { - strcpy (op->buf_asm, "invalid"); - op->buf_hex[0] = 0; + r_strbuf_set (&op->buf_asm, "invalid"); + r_strbuf_set (&op->buf_hex, ""); } arm_free (arminsn); return op->size; diff --git a/libr/asm/p/asm_avr.c b/libr/asm/p/asm_avr.c index c74fd9d9f4..08ce2fb7ad 100644 --- a/libr/asm/p/asm_avr.c +++ b/libr/asm/p/asm_avr.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2010-2015 - pancake, dark_k3y */ +/* radare - LGPL - Copyright 2010-2018 - pancake, dark_k3y */ /* AVR assembler realization by Alexander Bolshev aka @dark_k3y, LGPL -- 2015, heavily based (and using!) on disassemble module */ @@ -14,11 +14,13 @@ #include "../arch/avr/disasm.c" static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - int ret = op->size = avrdis (op->buf_asm, a->pc, buf, len); - if (op->buf_asm[0] == '.') { - op->buf_asm[0] = 0; + char buf_asm[32] = {0}; + op->size = avrdis (buf_asm, a->pc, buf, len); + if (*buf_asm == '.') { + *buf_asm = 0; } - return ret; + r_strbuf_set (&op->buf_asm, buf_asm); + return op->size; } extern instructionInfo instructionSet[AVR_TOTAL_INSTRUCTIONS]; @@ -76,8 +78,12 @@ static int parse_specialreg(const char *reg) { /* gets the number from string duplicate from asm_x86_nz.c -- may be create a generic function? */ static int getnum(RAsm *a, const char *s) { - if (!s) return 0; - if (*s=='$') s++; + if (!s) { + return 0; + } + if (*s == '$') { + s++; + } return r_num_math (a->num, s); } @@ -115,9 +121,10 @@ static int search_instruction(RAsm *a, char instr[3][MAX_TOKEN_SIZE], int args) && instructionSet[i].operandTypes[1] == OPERAND_LONG_ABSOLUTE_ADDRESS) { // ineffective, but needed for lds/sts and other cases if (strlen(instr[2]) > 0) { - op2 = getnum(a, instr[2]); - if (op2 > 127) + op2 = getnum (a, instr[2]); + if (op2 > 127) { return i; + } } // handling st & std instruction with 2 args } else if (instructionSet[i].mnemonic[0] == 's' @@ -133,9 +140,10 @@ static int search_instruction(RAsm *a, char instr[3][MAX_TOKEN_SIZE], int args) && instructionSet[i].operandTypes[0] == OPERAND_LONG_ABSOLUTE_ADDRESS) { // same for 1st operand of sts if (strlen(instr[1]) > 0) { - op1 = getnum(a, instr[1]); - if (op1 > 127) + op1 = getnum (a, instr[1]); + if (op1 > 127) { return i; + } } } else { return i; // it's not st/ld/lpm-like instruction with 2 args @@ -404,7 +412,7 @@ static int assemble(RAsm *a, RAsmOp *ao, const char *str) { // copying result to radare struct if (len > 0) { - memcpy (ao->buf, &coded, len); + r_strbuf_setbin (&ao->buf, (const ut8*)&coded, len); } return len; } diff --git a/libr/asm/p/asm_bf.c b/libr/asm/p/asm_bf.c index c9a9616bd3..e719f13f35 100644 --- a/libr/asm/p/asm_bf.c +++ b/libr/asm/p/asm_bf.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2017 - pancake, nibble */ +/* radare - LGPL - Copyright 2009-2018 - pancake, nibble */ #include #include @@ -16,143 +16,126 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { rep++; } } - + const char *buf_asm = "invalid"; switch (*buf) { case '[': - strcpy (op->buf_asm, "while [ptr]"); + buf_asm = "while [ptr]"; break; case ']': - strcpy (op->buf_asm, "loop"); // TODO: detect clause and put label name + buf_asm = "loop"; break; case '>': - if (rep > 1) { - strcpy (op->buf_asm, "add ptr"); - } else { - strcpy (op->buf_asm, "inc ptr"); - } + buf_asm = (rep > 1)? "add ptr": "inc ptr"; break; case '<': - if (rep > 1) { - strcpy (op->buf_asm, "sub ptr"); - } else { - strcpy (op->buf_asm, "dec ptr"); - } + buf_asm = (rep > 1)? "sub ptr": "dec ptr"; break; case '+': - if (rep > 1) { - strcpy (op->buf_asm, "add [ptr]"); - } else { - strcpy (op->buf_asm, "inc [ptr]"); - } + buf_asm = (rep > 1)? "add [ptr]": "inc [ptr]"; break; case '-': - if (rep > 1) { - strcpy (op->buf_asm, "sub [ptr]"); - } else { - strcpy (op->buf_asm, "dec [ptr]"); - } + buf_asm = (rep > 1)? "sub [ptr]": "dec [ptr]"; break; case ',': - strcpy (op->buf_asm, "in [ptr]"); + buf_asm = "in [ptr]"; break; case '.': - strcpy (op->buf_asm, "out [ptr]"); + buf_asm = "out [ptr]"; break; case 0xff: case 0x00: - strcpy (op->buf_asm, "trap"); + buf_asm = "trap"; break; default: - strcpy (op->buf_asm, "nop"); + buf_asm = "nop"; break; } if (rep > 1) { /* Note: snprintf's source and destination buffers may not * overlap. */ - const char *fmt = strchr (op->buf_asm, ' ')? "%s, %d": "%s %d"; - char buf[sizeof (op->buf_asm)]; - snprintf (buf, sizeof (buf), fmt, op->buf_asm, rep); - strcpy (op->buf_asm, buf); + const char *fmt = strchr (buf_asm, ' ')? "%s, %d": "%s %d"; + buf_asm = sdb_fmt (fmt, buf_asm, rep); } - + r_strbuf_set (&op->buf_asm, buf_asm); op->size = rep; return rep; } static int assemble(RAsm *a, RAsmOp *op, const char *buf) { - const char *ref, *arg; int n = 0; if (buf[0] && buf[1] == ' ') { buf += 2; } - arg = strchr (buf, ','); - ref = strchr (buf, '['); + const char *arg = strchr (buf, ','); + const char *ref = strchr (buf, '['); + ut8 opbuf[32]; if (!strncmp (buf, "trap", 4)) { if (arg) { n = atoi (arg + 1); - memset (op->buf, 0xcc, n); + memset (opbuf, 0xcc, n); } else { - op->buf[0] = 0x90; + opbuf[0] = 0x90; n = 1; } } else if (!strncmp (buf, "nop", 3)) { if (arg) { n = atoi (arg + 1); - memset (op->buf, 0x90, n); + memset (opbuf, 0x90, n); } else { - op->buf[0] = 0x90; + opbuf[0] = 0x90; n = 1; } } else if (!strncmp (buf, "inc", 3)) { char ch = ref? '+': '>'; - op->buf[0] = ch; + opbuf[0] = ch; n = 1; } else if (!strncmp (buf, "dec", 3)) { char ch = ref? '-': '<'; - op->buf[0] = ch; + opbuf[0] = ch; n = 1; } else if (!strncmp (buf, "sub", 3)) { char ch = ref? '-': '<'; if (arg) { n = atoi (arg + 1); - memset (op->buf, ch, n); + memset (opbuf, ch, n); } else { - op->buf[0] = '<'; + opbuf[0] = '<'; n = 1; } } else if (!strncmp (buf, "add", 3)) { char ch = ref? '+': '>'; if (arg) { n = atoi (arg + 1); - memset (op->buf, ch, n); + memset (opbuf, ch, n); } else { - op->buf[0] = '<'; + opbuf[0] = '<'; n = 1; } } else if (!strncmp (buf, "while", 5)) { - op->buf[0] = '['; + opbuf[0] = '['; n = 1; } else if (!strncmp (buf, "loop", 4)) { - op->buf[0] = ']'; + opbuf[0] = ']'; n = 1; } else if (!strncmp (buf, "in", 4)) { if (arg) { n = atoi (arg + 1); - memset (op->buf, ',', n); + memset (opbuf, ',', n); } else { - op->buf[0] = ','; + opbuf[0] = ','; n = 1; } } else if (!strncmp (buf, "out", 4)) { if (arg) { n = atoi (arg + 1); - memset (op->buf, '.', n); + memset (opbuf, '.', n); } else { - op->buf[0] = '.'; + opbuf[0] = '.'; n = 1; } } + r_strbuf_setbin (&op->buf, opbuf, n); return n; } diff --git a/libr/asm/p/asm_chip8.c b/libr/asm/p/asm_chip8.c index cf0e06425b..bdf6598cab 100644 --- a/libr/asm/p/asm_chip8.c +++ b/libr/asm/p/asm_chip8.c @@ -1,4 +1,4 @@ -/* radare - LGPL3 - Copyright 2017 - maijin */ +/* radare - LGPL3 - Copyright 2017-2018 - maijin */ #include #include @@ -10,75 +10,77 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *b, int l) { uint8_t nibble = opcode & 0x0F; uint16_t nnn = opcode & 0x0FFF; uint8_t kk = opcode & 0xFF; + const char *buf_asm = "invalid"; switch (opcode & 0xF000) { case 0x0000: if (opcode == 0x00E0) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "cls"); + buf_asm = "cls"; } else if (opcode == 0x00EE) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "ret"); + buf_asm = "ret"; } else if ((opcode & 0xFFF0) == 0x00C0) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "scd 0x%01x", nibble); + buf_asm = sdb_fmt ("scd 0x%01x", nibble); } else if (opcode == 0x00FB) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "scr"); + buf_asm = "scr"; } else if (opcode == 0x00FC) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "scl"); + buf_asm = "scl"; } else if (opcode == 0x00FD) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "exit"); + buf_asm = "exit"; } else if (opcode == 0x00FE) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "low"); + buf_asm = "low"; } else if (opcode == 0x00FF) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "high"); + buf_asm = "high"; } break; - case 0x1000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "jp 0x%03x", nnn); break; - case 0x2000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "call 0x%03x", nnn); break; - case 0x3000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "se v%1x, 0x%02x", x, kk); break; - case 0x4000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "sne v%1x, 0x%02x", x, kk); break; - case 0x5000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "se v%1x, v%1x", x, y); break; - case 0x6000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld v%1x, 0x%02x", x, kk); break; - case 0x7000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "add v%1x, 0x%02x", x, kk); break; + case 0x1000: buf_asm = sdb_fmt ("jp 0x%03x", nnn); break; + case 0x2000: buf_asm = sdb_fmt ("call 0x%03x", nnn); break; + case 0x3000: buf_asm = sdb_fmt ("se v%1x, 0x%02x", x, kk); break; + case 0x4000: buf_asm = sdb_fmt ("sne v%1x, 0x%02x", x, kk); break; + case 0x5000: buf_asm = sdb_fmt ("se v%1x, v%1x", x, y); break; + case 0x6000: buf_asm = sdb_fmt ("ld v%1x, 0x%02x", x, kk); break; + case 0x7000: buf_asm = sdb_fmt ("add v%1x, 0x%02x", x, kk); break; case 0x8000: { switch (nibble) { - case 0x0: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld v%1x, v%1x", x, y); break; - case 0x1: snprintf (op->buf_asm, R_ASM_BUFSIZE, "or v%1x, v%1x", x, y); break; - case 0x2: snprintf (op->buf_asm, R_ASM_BUFSIZE, "and v%1x, v%1x", x, y); break; - case 0x3: snprintf (op->buf_asm, R_ASM_BUFSIZE, "xor v%1x, v%1x", x, y); break; - case 0x4: snprintf (op->buf_asm, R_ASM_BUFSIZE, "add v%1x, v%1x", x, y); break; - case 0x5: snprintf (op->buf_asm, R_ASM_BUFSIZE, "sub v%1x, v%1x", x, y); break; - case 0x6: snprintf (op->buf_asm, R_ASM_BUFSIZE, "shr v%1x, v%1x", x, y); break; - case 0x7: snprintf (op->buf_asm, R_ASM_BUFSIZE, "subn v%1x, v%1x", x, y); break; - case 0xE: snprintf (op->buf_asm, R_ASM_BUFSIZE, "shl v%1x, v%1x", x, y); break; + case 0x0: buf_asm = sdb_fmt ("ld v%1x, v%1x", x, y); break; + case 0x1: buf_asm = sdb_fmt ("or v%1x, v%1x", x, y); break; + case 0x2: buf_asm = sdb_fmt ("and v%1x, v%1x", x, y); break; + case 0x3: buf_asm = sdb_fmt ("xor v%1x, v%1x", x, y); break; + case 0x4: buf_asm = sdb_fmt ("add v%1x, v%1x", x, y); break; + case 0x5: buf_asm = sdb_fmt ("sub v%1x, v%1x", x, y); break; + case 0x6: buf_asm = sdb_fmt ("shr v%1x, v%1x", x, y); break; + case 0x7: buf_asm = sdb_fmt ("subn v%1x, v%1x", x, y); break; + case 0xE: buf_asm = sdb_fmt ("shl v%1x, v%1x", x, y); break; } } break; - case 0x9000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "sne v%1x, v%1x", x, y); break; - case 0xA000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld i, 0x%03x", nnn); break; - case 0xB000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "jp v0, 0x%03x", nnn); break; - case 0xC000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "rnd v%1x, 0x%02x", x, kk); break; - case 0xD000: snprintf (op->buf_asm, R_ASM_BUFSIZE, "drw v%1x, v%1x, 0x%01x", x, y, nibble); break; + case 0x9000: buf_asm = sdb_fmt ("sne v%1x, v%1x", x, y); break; + case 0xA000: buf_asm = sdb_fmt ("ld i, 0x%03x", nnn); break; + case 0xB000: buf_asm = sdb_fmt ("jp v0, 0x%03x", nnn); break; + case 0xC000: buf_asm = sdb_fmt ("rnd v%1x, 0x%02x", x, kk); break; + case 0xD000: buf_asm = sdb_fmt ("drw v%1x, v%1x, 0x%01x", x, y, nibble); break; case 0xE000: { if (kk == 0x9E) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "skp v%1x", x); + buf_asm = sdb_fmt ("skp v%1x", x); } else if (kk == 0xA1) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "sknp v%1x", x); + buf_asm = sdb_fmt ("sknp v%1x", x); } } break; case 0xF000: { switch (kk) { - case 0x07: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld v%1x, dt", x); break; - case 0x0A: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld v%1x, k", x); break; - case 0x15: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld dt, v%1x", x); break; - case 0x18: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld st, v%1x", x); break; - case 0x1E: snprintf (op->buf_asm, R_ASM_BUFSIZE, "add i, v%1x", x); break; - case 0x29: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld f, v%1x", x); break; - case 0x33: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld b, v%1x", x); break; - case 0x55: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld [i], v%1x", x); break; - case 0x65: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld v%1x, [i]", x); break; - case 0x30: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld hf, v%1x", x); break; - case 0x75: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld r, v%1x", x); break; - case 0x85: snprintf (op->buf_asm, R_ASM_BUFSIZE, "ld v%1x, r", x); break; + case 0x07: buf_asm = sdb_fmt ("ld v%1x, dt", x); break; + case 0x0A: buf_asm = sdb_fmt ("ld v%1x, k", x); break; + case 0x15: buf_asm = sdb_fmt ("ld dt, v%1x", x); break; + case 0x18: buf_asm = sdb_fmt ("ld st, v%1x", x); break; + case 0x1E: buf_asm = sdb_fmt ("add i, v%1x", x); break; + case 0x29: buf_asm = sdb_fmt ("ld f, v%1x", x); break; + case 0x33: buf_asm = sdb_fmt ("ld b, v%1x", x); break; + case 0x55: buf_asm = sdb_fmt ("ld [i], v%1x", x); break; + case 0x65: buf_asm = sdb_fmt ("ld v%1x, [i]", x); break; + case 0x30: buf_asm = sdb_fmt ("ld hf, v%1x", x); break; + case 0x75: buf_asm = sdb_fmt ("ld r, v%1x", x); break; + case 0x85: buf_asm = sdb_fmt ("ld v%1x, r", x); break; } } break; } + r_strbuf_set (&op->buf_asm, buf_asm); op->size = 2; return op->size; } @@ -93,9 +95,9 @@ RAsmPlugin r_asm_plugin_chip8 = { }; #ifndef CORELIB -struct RLibStruct radare_plugin = { +RLibStruct radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_chip8 - .version = R2_VERSION + .version = R2_VERSION }; #endif diff --git a/libr/asm/p/asm_cr16.c b/libr/asm/p/asm_cr16.c index 3531c9d7c8..0ca7d02607 100644 --- a/libr/asm/p/asm_cr16.c +++ b/libr/asm/p/asm_cr16.c @@ -1,22 +1,17 @@ +/* radare - LGPL - Copyright 2014-2018 - fedor.sakharov */ + #include #include #include #include #include - #include -static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) -{ - int ret = 1; +static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct cr16_cmd cmd; - - ret = cr16_decode_command (buf, &cmd); - - snprintf(op->buf_asm, R_ASM_BUFSIZE, "%s %s", cmd.instr, cmd.operands); - op->size = ret; - - return ret; + int ret = cr16_decode_command (buf, &cmd); + r_strbuf_set (&op->buf_asm, sdb_fmt ("%s %s", cmd.instr, cmd.operands)); + return op->size = ret; } RAsmPlugin r_asm_plugin_cr16 = { diff --git a/libr/asm/p/asm_cris_gnu.c b/libr/asm/p/asm_cris_gnu.c index 46e3686c9a..56b5850b69 100644 --- a/libr/asm/p/asm_cris_gnu.c +++ b/libr/asm/p/asm_cris_gnu.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2014 - pancake */ +/* radare - LGPL - Copyright 2014-2018 - pancake */ #if 0 @@ -75,11 +75,10 @@ int print_insn_crisv10_v32_with_register_prefix (bfd_vma vma, disassemble_info * static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; int mode = 2; - - op->buf_asm[0]='\0'; - if (len<4) + if (len < 4) { return -1; - buf_global = op->buf_asm; + } + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, R_MIN (len, 8)); // TODO handle thumb @@ -114,10 +113,9 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { op->size = print_insn_crisv10_v32_without_register_prefix ( (bfd_vma)Offset, &disasm_obj); } - - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); - + if (op->size == -1) { + r_strbuf_set (&op->buf_asm, "(data)"); + } return op->size; } @@ -126,6 +124,7 @@ RAsmPlugin r_asm_plugin_cris_gnu = { .arch = "cris", .cpus = "v10,v32,v10+v32", .license = "GPL3", + .author = "pancake", .bits = 32, .endian = R_SYS_ENDIAN_LITTLE, .desc = "Axis Communications 32-bit embedded processor", diff --git a/libr/asm/p/asm_dalvik.c b/libr/asm/p/asm_dalvik.c index 1de7948ba3..3e4870b4f5 100644 --- a/libr/asm/p/asm_dalvik.c +++ b/libr/asm/p/asm_dalvik.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2017 - earada, pancake */ +/* radare - LGPL - Copyright 2009-2018 - earada, pancake, h4ng3r */ #include #include @@ -20,17 +20,20 @@ static int dalvik_disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int len) { const char *flag_str; a->dataalign = 2; - op->buf_asm[0] = 0; + const char *buf_asm = NULL; if (buf[0] == 0x00) { /* nop */ + if (len < 2) { + return -1; + } switch (buf[1]) { case 0x01: /* packed-switch-payload */ // ushort size // int first_key // int[size] = relative offsets { - unsigned short array_size = buf[2] | (buf[3] << 8); + ut16 array_size = buf[2] | (buf[3] << 8); int first_key = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24); - snprintf (op->buf_asm, sizeof(op->buf_asm), "packed-switch-payload %d, %d", array_size, first_key); + buf_asm = sdb_fmt ("packed-switch-payload %d, %d", array_size, first_key); size = 8; payload = 2 * (array_size * 2); len = 0; @@ -41,10 +44,10 @@ static int dalvik_disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int len) { // int[size] keys // int[size] relative offsets { - unsigned short array_size = buf[2] | (buf[3] << 8); - snprintf (op->buf_asm, sizeof (op->buf_asm), "sparse-switch-payload %d", array_size); + ut16 array_size = buf[2] | (buf[3] << 8); + buf_asm = sdb_fmt ("sparse-switch-payload %d", array_size); size = 4; - payload = 2 * (array_size*4); + payload = 2 * (array_size * 4); len = 0; } break; @@ -53,11 +56,9 @@ static int dalvik_disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int len) { // size = 4 bytes uint // ([size*element_width+1)/2)+4 if (len > 7) { - unsigned short elem_width = buf[2] | (buf[3] << 8); - unsigned int array_size = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24); - snprintf (op->buf_asm, sizeof (op->buf_asm), - "fill-array-data-payload %d, %d", - elem_width, array_size); + ut16 elem_width = buf[2] | (buf[3] << 8); + ut32 array_size = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24); + buf_asm = sdb_fmt ("fill-array-data-payload %d, %d", elem_width, array_size); payload = array_size * elem_width; } size = 8; @@ -68,10 +69,12 @@ static int dalvik_disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int len) { break; } } + if (buf_asm) { + r_strbuf_set (&op->buf_asm, buf_asm); + } strasm = NULL; if (size <= len) { - strncpy (op->buf_asm, dalvik_opcodes[i].name, sizeof (op->buf_asm) - 1); - strasm = strdup (op->buf_asm); + strasm = strdup (dalvik_opcodes[i].name); size = dalvik_opcodes[i].len; switch (dalvik_opcodes[i].fmt) { case fmtop: break; @@ -482,8 +485,7 @@ static int dalvik_disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int len) { flag_str = R_ASM_GET_NAME (a, 'm', vB); if (flag_str) { snprintf (str, sizeof (str), " {v%i..v%i}, %s", vC, vC + vA - 1, flag_str); - } - else { + } else { snprintf (str, sizeof (str), " {v%i..v%i}, method+%i", vC, vC + vA - 1, vB); } strasm = r_str_append (strasm, str); @@ -491,8 +493,7 @@ static int dalvik_disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int len) { flag_str = R_ASM_GET_NAME (a, 'p', vH); if (flag_str) { snprintf (str, sizeof (str), ", %s", flag_str); - } - else { + } else { snprintf (str, sizeof (str), ", proto+%i", vH); } strasm = r_str_append (strasm, str); @@ -501,20 +502,13 @@ static int dalvik_disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int len) { case fmtoptinvokeIR: case fmt00: default: - strcpy (op->buf_asm, "invalid "); free (strasm); strasm = NULL; size = 2; } - if (strasm) { - strncpy (op->buf_asm, strasm, sizeof (op->buf_asm) - 1); - op->buf_asm[sizeof (op->buf_asm) - 1] = 0; - } else { - //op->buf_asm[0] = 0; - strcpy (op->buf_asm , "invalid"); - } + r_strbuf_set (&op->buf_asm, strasm? strasm: "invalid"); } else if (len > 0) { - strcpy (op->buf_asm, "invalid "); + r_strbuf_set (&op->buf_asm, "invalid"); op->size = len; size = len; } @@ -536,7 +530,9 @@ static int dalvik_assemble(RAsm *a, RAsmOp *op, const char *buf) { // TODO: use a hashtable here for (i = 0; i < 256; i++) { if (!strcmp (dalvik_opcodes[i].name, buf)) { - r_write_ble32 (op->buf, i, a->big_endian); + ut8 buf[4]; + r_write_ble32 (buf, i, a->big_endian); + r_strbuf_setbin (&op->buf, buf, sizeof (buf)); op->size = dalvik_opcodes[i].len; return op->size; } diff --git a/libr/asm/p/asm_dcpu16.c b/libr/asm/p/asm_dcpu16.c index 7c4c36e577..312e54f741 100644 --- a/libr/asm/p/asm_dcpu16.c +++ b/libr/asm/p/asm_dcpu16.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2012-2015 pancake */ +/* radare2 - LGPL - Copyright 2012-2018 pancake */ #include #include @@ -12,15 +12,17 @@ #include "../arch/dcpu16/asm.c" static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - if (len<2) return -1; // at least 2 bytes! - op->size = dcpu16_disasm (op->buf_asm, (const ut16*)buf, len, NULL); - if (op->size == -1) - strcpy (op->buf_asm, " (data)"); + char buf_asm[32]; + if (len < 2) { + return -1; // at least 2 bytes! + } + op->size = dcpu16_disasm (buf_asm, (const ut16*)buf, len, NULL); + r_strbuf_set (&op->buf_asm, (op->size > 0) ? buf_asm: "(data)"); return op->size; } static int assemble(RAsm *a, RAsmOp *op, const char *buf) { - return dcpu16_assemble (op->buf, buf); + return dcpu16_assemble ((ut8*)r_strbuf_get (&op->buf), buf); } RAsmPlugin r_asm_plugin_dcpu16 = { diff --git a/libr/asm/p/asm_ebc.c b/libr/asm/p/asm_ebc.c index 86552a7d1a..6d94c0c810 100644 --- a/libr/asm/p/asm_ebc.c +++ b/libr/asm/p/asm_ebc.c @@ -1,35 +1,26 @@ -/* radare - LGPL - Copyright 2012-2013 - pancake - 2013 - Fedor Sakharov */ +/* radare - LGPL - Copyright 2012-2018 - pancake, fedor sakharov */ #include #include #include #include #include - #include static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - int ret = 1; ebc_command_t cmd = { {0}, {0} }; - - ret = ebc_decode_command(buf, &cmd); - - if (cmd.operands[0]) { - snprintf(op->buf_asm, R_ASM_BUFSIZE, - "%s %s", cmd.instr, cmd.operands); - } else { - snprintf(op->buf_asm, R_ASM_BUFSIZE, "%s", cmd.instr); - } - - op->size = ret; - return ret; + int ret = ebc_decode_command (buf, &cmd); + const char *buf_asm = (cmd.operands[0]) + ? sdb_fmt ("%s %s", cmd.instr, cmd.operands): cmd.instr; + r_asm_op_set_asm (op, buf_asm); + return op->size = ret; } RAsmPlugin r_asm_plugin_ebc = { .name = "ebc", .license = "LGPL3", .desc = "EFI Bytecode", + .author = "Fedor Sakharov", .arch = "ebc", .bits = 32|64, .endian = R_SYS_ENDIAN_LITTLE, diff --git a/libr/asm/p/asm_gb.c b/libr/asm/p/asm_gb.c index 6812c8dd1b..720fca9af4 100644 --- a/libr/asm/p/asm_gb.c +++ b/libr/asm/p/asm_gb.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2012-2015 - pancake, condret */ +/* radare - LGPL - Copyright 2012-2018 - pancake, condret */ // fork of asm_z80.c @@ -11,9 +11,7 @@ static int disassemble(RAsm *a, RAsmOp *r_op, const ut8 *buf, int len) { int dlen = gbDisass(r_op,buf,len); - if(dlen<0) dlen=0; - r_op->size = dlen; - return dlen; + return r_op->size = R_MAX (0, dlen); } static int assemble(RAsm *a, RAsmOp *r_op, const char *buf) { @@ -24,6 +22,7 @@ RAsmPlugin r_asm_plugin_gb = { .name = "gb", .desc = "GameBoy(TM) (z80-like)", .arch = "z80", + .author = "condret", .license = "LGPL3", .bits = 16, .endian = R_SYS_ENDIAN_LITTLE, diff --git a/libr/asm/p/asm_h8300.c b/libr/asm/p/asm_h8300.c index 2482077a20..b2fb864c3e 100644 --- a/libr/asm/p/asm_h8300.c +++ b/libr/asm/p/asm_h8300.c @@ -1,22 +1,17 @@ +/* radare - LGPL - Copyright 2014-2018 - fedor.sakharov */ + #include #include #include #include #include - #include -static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) -{ - int ret = 1; +static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct h8300_cmd cmd; - - ret = h8300_decode_command(buf, &cmd); - - snprintf(op->buf_asm, R_ASM_BUFSIZE, "%s %s", cmd.instr, cmd.operands); - op->size = ret; - - return ret; + int ret = h8300_decode_command(buf, &cmd); + r_strbuf_set (&op->buf_asm, sdb_fmt ("%s %s", cmd.instr, cmd.operands)); + return op->size = ret; } RAsmPlugin r_asm_plugin_h8300 = { diff --git a/libr/asm/p/asm_hexagon.c b/libr/asm/p/asm_hexagon.c index 14cf1256d4..32c667f3ba 100644 --- a/libr/asm/p/asm_hexagon.c +++ b/libr/asm/p/asm_hexagon.c @@ -7,21 +7,18 @@ #include "hexagon.h" #include "hexagon_insn.h" -static int disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int l) -{ - HexInsn hi; - ut32 data = 0; - memset(&hi, 0, sizeof(hi)); - data = r_read_le32(buf); - int size = hexagon_disasm_instruction(data, &hi); - op->size = size; - strcpy(op->buf_asm, hi.mnem); - return size; +static int disassemble (RAsm *a, RAsmOp *op, const ut8 *buf, int l) { + HexInsn hi = {0}; + ut32 data = r_read_le32 (buf); + op->size = hexagon_disasm_instruction (data, &hi); + r_strbuf_set (&op->buf_asm, hi.mnem); + return op->size; } RAsmPlugin r_asm_plugin_hexagon = { .name = "hexagon", .arch = "hexagon", + .author = "xvilka", .license = "LGPL3", .bits = 32, .desc = "Qualcomm Hexagon (QDSP6) V6", @@ -29,7 +26,7 @@ RAsmPlugin r_asm_plugin_hexagon = { }; #ifndef CORELIB -struct r_lib_struct_t radare_plugin = { +RLibStruct radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_hexagon }; diff --git a/libr/asm/p/asm_hppa_gnu.c b/libr/asm/p/asm_hppa_gnu.c index bbdfafd752..cc7e04903b 100644 --- a/libr/asm/p/asm_hppa_gnu.c +++ b/libr/asm/p/asm_hppa_gnu.c @@ -73,16 +73,16 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; - op->buf_asm[0]='\0'; - if (len<4) + if (len < 4) { return -1; - buf_global = op->buf_asm; + } + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, 4); // TODO handle thumb /* prepare disassembler */ memset (&disasm_obj, '\0', sizeof (struct disassemble_info)); - disasm_obj.disassembler_options=(a->bits==64)?"64":""; + disasm_obj.disassembler_options = (a->bits == 64)?"64":""; disasm_obj.buffer = bytes; disasm_obj.read_memory_func = &hppa_buffer_read_memory; disasm_obj.symbol_at_address_func = &symbol_at_address; @@ -93,9 +93,9 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { disasm_obj.stream = stdout; op->size = print_insn_hppa ((bfd_vma)Offset, &disasm_obj); - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); - + if (op->size == -1) { + r_strbuf_set (&op->buf_asm, "(data)"); + } return op->size; } diff --git a/libr/asm/p/asm_i8080.c b/libr/asm/p/asm_i8080.c index 4a65d50297..ba1aeeeca6 100644 --- a/libr/asm/p/asm_i8080.c +++ b/libr/asm/p/asm_i8080.c @@ -1,17 +1,14 @@ -/* radare - LGPL - Copyright 2012-2013 - Alexander */ +/* radare - LGPL - Copyright 2012-2018 - Alexander */ #include #include #include #include - #include "../arch/i8080/i8080dis.c" -static int do_disassemble(RAsm *a, struct r_asm_op_t *op, const ut8 *buf, int len) { - int dlen = i8080_disasm (buf, op->buf_asm, len); - if (dlen<0) dlen = 0; - op->size = dlen; - return op->size; +static int do_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { + int dlen = i8080_disasm (buf, r_strbuf_get (&op->buf_asm), len); + return op->size = R_MAX (0, dlen); } RAsmPlugin r_asm_plugin_i8080 = { diff --git a/libr/asm/p/asm_java.c b/libr/asm/p/asm_java.c index 3a97fe909c..d0b38d0291 100644 --- a/libr/asm/p/asm_java.c +++ b/libr/asm/p/asm_java.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2017 - nibble, pancake */ +/* radare - LGPL - Copyright 2009-2018 - nibble, pancake */ #include #include @@ -10,7 +10,6 @@ #include "../../shlr/java/class.h" static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - //void *cp; RBinJavaObj *obj = NULL; RBin *bin = a->binb.bin; RBinPlugin *plugin = bin && bin->cur && bin->cur->o ? @@ -21,15 +20,16 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { //eprintf("Handling: %s disasm.\n", b->cur.file); } } - - op->size = r_java_disasm (obj, a->pc, buf, len, - op->buf_asm, sizeof (op->buf_asm)); + char buf_asm[256]; + op->size = r_java_disasm (obj, a->pc, buf, len, buf_asm, sizeof (buf_asm)); + r_strbuf_set (&op->buf_asm, buf_asm); return op->size; } -static int assemble(RAsm *a, RAsmOp *op, const char *buf) { +static int assemble(RAsm *a, RAsmOp *op, const char *input) { // TODO: get class info from bin if possible - return op->size = r_java_assemble (a->pc, op->buf, buf); + // XXX wrong usage of strbuf_get here + return op->size = r_java_assemble (a->pc, (ut8*)r_strbuf_get (&op->buf), input); } RAsmPlugin r_asm_plugin_java = { diff --git a/libr/asm/p/asm_lanai_gnu.c b/libr/asm/p/asm_lanai_gnu.c index db2784da88..48abfe9a22 100644 --- a/libr/asm/p/asm_lanai_gnu.c +++ b/libr/asm/p/asm_lanai_gnu.c @@ -1,20 +1,17 @@ -/* radare - LGPL - Copyright 2016 - pancake */ +/* radare - LGPL - Copyright 2016-2018 - pancake */ #include #include #include - #include #include #include #include - #include "disas-asm.h" - static unsigned long Offset = 0; static char *buf_global = NULL; -static int buf_global_size = 0; +static int buf_global_size = 64; // hardcoded sizeof (RStrBuf.buf); static unsigned char bytes[4]; static int lanai_buffer_read_memory (bfd_vma memaddr, bfd_byte *myaddr, ut32 length, struct disassemble_info *info) { @@ -56,9 +53,8 @@ static int buf_fprintf(void *stream, const char *format, ...) { } memcpy (tmp, buf_global, glen); memcpy (tmp+glen, format, flen); - tmp[flen+glen] = 0; -// XXX: overflow here? - + tmp[flen + glen] = 0; + // XXX: overflow here? vsnprintf (buf_global, buf_global_size, tmp, ap); free (tmp); va_end (ap); @@ -67,12 +63,10 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; - op->buf_asm[0]='\0'; if (len < 4) { return -1; } - buf_global = op->buf_asm; - buf_global_size = sizeof (op->buf_asm); + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, 4); // TODO handle thumb @@ -89,9 +83,8 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { disasm_obj.stream = stdout; op->size = print_insn_lanai ((bfd_vma)Offset, &disasm_obj); - if (op->size == -1) { - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); + r_strbuf_set (&op->buf_asm, "(data)"); } return op->size; } diff --git a/libr/asm/p/asm_lh5801.c b/libr/asm/p/asm_lh5801.c index 18b1a56835..2ef84ccf45 100644 --- a/libr/asm/p/asm_lh5801.c +++ b/libr/asm/p/asm_lh5801.c @@ -8,23 +8,22 @@ static int disassemble(RAsm *as, RAsmOp *op, const ut8 *buf, int len) { struct lh5801_insn insn; - if (!op) { return 0; } - int consumed = lh5801_decode (&insn, buf, len); if (consumed == -1 || consumed == 0) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "invalid"); + r_strbuf_set (&op->buf_asm, "invalid"); op->size = 1; return 0; - } else { - lh5801_print_insn (op->buf_asm, R_ASM_BUFSIZE, &insn); - op->size = consumed; - //op->payload = lh5801_insn_descs[insn.type].format & 3; - // ^ MAYBE? - return op->size; } + char buf_asm[128] = {0}; + lh5801_print_insn (buf_asm, sizeof (buf_asm), &insn); + r_strbuf_set (&op->buf_asm, buf_asm); + op->size = consumed; + //op->payload = lh5801_insn_descs[insn.type].format & 3; + // ^ MAYBE? + return op->size; } RAsmPlugin r_asm_plugin_lh5801 = { diff --git a/libr/asm/p/asm_lm32.c b/libr/asm/p/asm_lm32.c index 7b43bfb5dc..97cb2cf2e4 100644 --- a/libr/asm/p/asm_lm32.c +++ b/libr/asm/p/asm_lm32.c @@ -384,7 +384,6 @@ static int assemble(RAsm *a, RAsmOp *ao, const char *str) { //TODO return -1; } - #endif static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { @@ -392,13 +391,13 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { instr.value = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]; instr.addr = a->pc; if (r_asm_lm32_decode (&instr)) { - strcpy (op->buf_asm, "invalid"); + r_strbuf_set (&op->buf_asm, "invalid"); a->invhex = 1; return -1; } //op->buf_asm is 256 chars long, which is more than sufficient - if (r_asm_lm32_stringify (&instr, op->buf_asm)) { - strcpy (op->buf_asm, "invalid"); + if (r_asm_lm32_stringify (&instr, r_strbuf_get (&op->buf_asm))) { + r_strbuf_set (&op->buf_asm, "invalid"); a->invhex = 1; return -1; } @@ -409,6 +408,7 @@ RAsmPlugin r_asm_plugin_lm32 = { .name = "lm32", .arch = "lm32", .desc = "disassembly plugin for Lattice Micro 32 ISA", + .author = "Felix Held", .license = "BSD", .bits = 32, .endian = R_SYS_ENDIAN_BIG, diff --git a/libr/asm/p/asm_m68k_cs.c b/libr/asm/p/asm_m68k_cs.c index ba47119ddd..318edd5c36 100644 --- a/libr/asm/p/asm_m68k_cs.c +++ b/libr/asm/p/asm_m68k_cs.c @@ -49,7 +49,6 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { mode |= CS_MODE_M68K_060; if (op) { op->size = 4; - op->buf_asm[0] = 0; } if (cd == 0) { ret = cs_open (CS_ARCH_M68K, mode, &cd); @@ -81,26 +80,24 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { ret = -1; goto beach; } + const char *buf_asm = NULL; if (a->features && *a->features) { if (!check_features (a, insn)) { if (op) { - op->size = insn->size; - strcpy (op->buf_asm, "illegal"); + op->size = insn->size; + buf_asm = "illegal"; } } } if (op && !op->size) { op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", - insn->mnemonic, - insn->op_str[0]?" ":"", - insn->op_str); + buf_asm = sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]?" ":"", insn->op_str); } - if (op) { - char *p = r_str_replace (strdup (op->buf_asm), - "$", "0x", true); + if (op && buf_asm) { + char *p = r_str_replace (strdup (buf_asm), "$", "0x", true); if (p) { - strncpy (op->buf_asm, p, R_ASM_BUFSIZE-1); + r_str_rmch (p, '#'); + r_asm_op_set_asm (op, p); free (p); } } @@ -108,10 +105,12 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { beach: //cs_close (&cd); if (op) { - if (!strncmp (op->buf_asm, "dc.w", 4)) { - strcpy (op->buf_asm, "invalid"); + const char *bufasm = r_asm_op_get_asm (op); + if (!strncmp (buf_asm, "dc.w", 4)) { + r_asm_op_set_asm (op, "invalid"); + bufasm = r_asm_op_get_asm (op); } - r_str_rmch (op->buf_asm, '#'); + r_str_rmch (buf_asm, '#'); return op->size; } return ret; @@ -147,6 +146,7 @@ RAsmPlugin r_asm_plugin_m68k_cs = { .name = "m68k.cs (unsupported)", .desc = "Capstone M68K disassembler (unsupported)", .license = "BSD", + .author = "pancake", .arch = "m68k", .bits = 16 | 32, .endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG, diff --git a/libr/asm/p/asm_malbolge.c b/libr/asm/p/asm_malbolge.c index aae98aa961..fcf91e8c92 100644 --- a/libr/asm/p/asm_malbolge.c +++ b/libr/asm/p/asm_malbolge.c @@ -1,48 +1,36 @@ +/* radare - LGPL - Copyright 2014-2018 condret, pancake */ + #include #include #include #include -static int mal_dis(RAsmOp *op, ut64 c, const ut8 *buf, ut64 len) { - if(len) { - switch ((buf[0]+c)%94) { - case 4: - sprintf(op->buf_asm, "jmp [d]"); - break; - case 5: - sprintf(op->buf_asm, "out a"); - break; - case 23: - sprintf(op->buf_asm, "in a"); - break; - case 39: - sprintf(op->buf_asm, "rotr [d], mov a, [d]"); - break; - case 40: - sprintf(op->buf_asm, "mov d, [d]"); - break; - case 62: - sprintf(op->buf_asm, "crz [d], a, mov a, [d]"); - break; - case 81: - sprintf(op->buf_asm, "end"); - break; - default: - sprintf(op->buf_asm, "nop"); +static const char *mal_dis(ut64 c, const ut8 *buf, ut64 len) { + if (len) { + switch ((buf[0] + c) % 94) { + case 4: return "jmp [d]"; + case 5: return "out a"; + case 23: return "in a"; + case 39: return "rotr [d], mov a, [d]"; + case 40: return "mov d, [d]"; + case 62: return "crz [d], a, mov a, [d]"; + case 81: return "end"; + default: return "nop"; } - return true; } - return false; + return NULL; } static int __disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - return op->size = mal_dis(op, a->pc, buf, len); + const char *opstr = mal_dis (a->pc, buf, len); + return op->size = opstr? 1: 0; } RAsmPlugin r_asm_plugin_malbolge = { .name = "malbolge", .desc = "Malbolge Ternary VM", .arch = "malbolge", + .author = "condret", .license = "LGPL3", .bits = 32, .endian = R_SYS_ENDIAN_NONE, diff --git a/libr/asm/p/asm_mcs96.c b/libr/asm/p/asm_mcs96.c index 2e1e57cea4..0a31ce0ab7 100644 --- a/libr/asm/p/asm_mcs96.c +++ b/libr/asm/p/asm_mcs96.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyrigth - 2015 - condret */ +/* radare - LGPL - Copyrigth - 2015-2018 - condret */ #include #include @@ -7,23 +7,19 @@ #include "../arch/mcs96/mcs96.h" static int mcs96_len (const ut8 buf) { - if (mcs96_op[buf].type & MCS96_6B) - return 6; - if (mcs96_op[buf].type & MCS96_5B) - return 5; - if (mcs96_op[buf].type & MCS96_4B) - return 4; - if (mcs96_op[buf].type & MCS96_3B) - return 3; - if (mcs96_op[buf].type & MCS96_2B) - return 2; + if (mcs96_op[buf].type & MCS96_6B) return 6; + if (mcs96_op[buf].type & MCS96_5B) return 5; + if (mcs96_op[buf].type & MCS96_4B) return 4; + if (mcs96_op[buf].type & MCS96_3B) return 3; + if (mcs96_op[buf].type & MCS96_2B) return 2; return 1; } static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - if (len>1 && !memcmp (buf, "\xff\xff", 2)) + if (len > 1 && !memcmp (buf, "\xff\xff", 2)) { return -1; - strncpy (op->buf_asm, mcs96_op[buf[0]].ins, sizeof (op->buf_asm)-1); + } + r_strbuf_set (&op->buf_asm, mcs96_op[buf[0]].ins); op->size = mcs96_len (buf[0]); return op->size; } diff --git a/libr/asm/p/asm_mips_cs.c b/libr/asm/p/asm_mips_cs.c index ac27ca1fc8..6b1afc8374 100644 --- a/libr/asm/p/asm_mips_cs.c +++ b/libr/asm/p/asm_mips_cs.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2013-2017 - pancake */ +/* radare2 - LGPL - Copyright 2013-2018 - pancake */ #include #include @@ -47,7 +47,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF); n = cs_disasm (cd, (ut8*)buf, len, a->pc, 1, &insn); if (n < 1) { - strcpy (op->buf_asm, "invalid"); + r_asm_op_set_asm (op, "invalid"); op->size = 4; goto beach; } else { @@ -57,11 +57,13 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { goto beach; } op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", - insn->mnemonic, insn->op_str[0]? " ": "", - insn->op_str); - // remove the '$' in the string - r_str_replace_char (op->buf_asm, '$', 0); + char *str = r_str_newf ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", insn->op_str); + if (str) { + r_str_replace_char (str, '$', 0); + // remove the '$' in the string + r_asm_op_set_asm (op, str); + free (str); + } cs_free (insn, n); beach: // cs_close (&cd); @@ -70,14 +72,16 @@ fin: } static int assemble(RAsm *a, RAsmOp *op, const char *str) { - int ret = mips_assemble (str, a->pc, op->buf); + ut8 *opbuf = (ut8*)r_strbuf_get (&op->buf); + int ret = mips_assemble (str, a->pc, opbuf); if (a->big_endian) { - ut8 tmp = op->buf[0]; - op->buf[0] = op->buf[3]; - op->buf[3] = tmp; - tmp = op->buf[1]; - op->buf[1] = op->buf[2]; - op->buf[2] = tmp; + ut8 *buf = opbuf; + ut8 tmp = buf[0]; + buf[0] = buf[3]; + buf[3] = tmp; + tmp = buf[1]; + buf[1] = buf[2]; + buf[2] = tmp; } return ret; } diff --git a/libr/asm/p/asm_mips_gnu.c b/libr/asm/p/asm_mips_gnu.c index 52e1f5e2d1..0a29ec018c 100644 --- a/libr/asm/p/asm_mips_gnu.c +++ b/libr/asm/p/asm_mips_gnu.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2014 - pancake, nibble */ +/* radare - LGPL - Copyright 2009-2018 - pancake, nibble */ #include #include @@ -33,8 +33,9 @@ static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_in static void print_address(bfd_vma address, struct disassemble_info *info) { char tmp[32]; - if (!buf_global) + if (!buf_global) { return; + } sprintf (tmp, "0x%08"PFMT64x, (ut64)address); strcat (buf_global, tmp); } @@ -59,8 +60,10 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(struct r_asm_t *a, struct r_asm_op_t *op, const ut8 *buf, int len) { static struct disassemble_info disasm_obj; - if (len<4) return -1; - buf_global = op->buf_asm; + if (len < 4) { + return -1; + } + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, 4); // TODO handle thumb @@ -79,26 +82,25 @@ static int disassemble(struct r_asm_t *a, struct r_asm_op_t *op, const ut8 *buf, disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - op->buf_asm[0] = '\0'; - if (disasm_obj.endian == BFD_ENDIAN_LITTLE) { - op->size = print_insn_little_mips ((bfd_vma)Offset, &disasm_obj); - } else { - op->size = print_insn_big_mips ((bfd_vma)Offset, &disasm_obj); + op->size = (disasm_obj.endian == BFD_ENDIAN_LITTLE) + ? print_insn_little_mips ((bfd_vma)Offset, &disasm_obj) + : print_insn_big_mips ((bfd_vma)Offset, &disasm_obj); + if (op->size == -1) { + r_strbuf_set (&op->buf_asm, "(data)"); } - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); return op->size; } static int assemble(RAsm *a, RAsmOp *op, const char *str) { - int ret = mips_assemble (str, a->pc, op->buf); + ut8 *opbuf = (ut8*)r_strbuf_get (&op->buf); + int ret = mips_assemble (str, a->pc, opbuf); if (a->big_endian) { - ut8 tmp = op->buf[0]; - op->buf[0] = op->buf[3]; - op->buf[3] = tmp; - tmp = op->buf[1]; - op->buf[1] = op->buf[2]; - op->buf[2] = tmp; + ut8 tmp = opbuf[0]; + opbuf[0] = opbuf[3]; + opbuf[3] = tmp; + tmp = opbuf[1]; + opbuf[1] = opbuf[2]; + opbuf[2] = tmp; } return ret; } @@ -107,7 +109,7 @@ RAsmPlugin r_asm_plugin_mips_gnu = { .name = "mips.gnu", .arch = "mips", .license = "GPL3", - .bits = 32|64, + .bits = 32 | 64, .endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG, .desc = "MIPS CPU", .disassemble = &disassemble, diff --git a/libr/asm/p/asm_msp430.c b/libr/asm/p/asm_msp430.c index ab1dc43f81..042086c616 100644 --- a/libr/asm/p/asm_msp430.c +++ b/libr/asm/p/asm_msp430.c @@ -1,3 +1,5 @@ +/* radare - LGPL - Copyright 2014-2015 - fedor.sakharov */ + #include #include #include @@ -11,9 +13,9 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { int ret = msp430_decode_command (buf, len, &cmd); if (ret > 0) { if (cmd.operands[0]) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s", cmd.instr, cmd.operands); + r_strbuf_set (&op->buf_asm, sdb_fmt ("%s %s", cmd.instr, cmd.operands)); } else { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s", cmd.instr); + r_strbuf_set (&op->buf_asm, sdb_fmt ("%s", cmd.instr)); } } diff --git a/libr/asm/p/asm_nios2.c b/libr/asm/p/asm_nios2.c index 4b747be776..2cb1000f75 100644 --- a/libr/asm/p/asm_nios2.c +++ b/libr/asm/p/asm_nios2.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2014 - pancake */ +/* radare2 - LGPL - Copyright 2014-2018 - pancake */ #include #include @@ -32,27 +32,30 @@ static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_in } static void print_address(bfd_vma address, struct disassemble_info *info) { - char tmp[32]; - if (!buf_global) - return; - sprintf(tmp, "0x%08"PFMT64x"", (ut64)address); - strcat(buf_global, tmp); + if (buf_global) { + char tmp[32]; + snprintf (tmp, sizeof (tmp), "0x%08"PFMT64x"", (ut64)address); + strcat (buf_global, tmp); + } } static int buf_fprintf(void *stream, const char *format, ...) { int flen, glen; va_list ap; char *tmp; - if (!buf_global) + if (!buf_global) { return 0; + } va_start (ap, format); flen = strlen (format); glen = strlen (buf_global); tmp = malloc (flen + glen + 2); - if (!tmp) return 0; + if (!tmp) { + return 0; + } memcpy (tmp, buf_global, glen); memcpy (tmp+glen, format, flen); - tmp[flen+glen] = 0; + tmp[flen + glen] = 0; // XXX: overflow here? vsprintf (buf_global, tmp, ap); va_end (ap); @@ -62,9 +65,10 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, struct r_asm_op_t *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; - if (len<4) + if (len < 4) { return -1; - buf_global = op->buf_asm; + } + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, 4); // TODO handle thumb @@ -80,14 +84,14 @@ static int disassemble(RAsm *a, struct r_asm_op_t *op, const ut8 *buf, int len) disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - op->buf_asm[0]='\0'; - if (disasm_obj.endian == BFD_ENDIAN_BIG) + if (disasm_obj.endian == BFD_ENDIAN_BIG) { op->size = print_insn_big_nios2 ((bfd_vma)Offset, &disasm_obj); - else op->size = print_insn_little_nios2 ((bfd_vma)Offset, &disasm_obj); - - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); - + } else { + op->size = print_insn_little_nios2 ((bfd_vma)Offset, &disasm_obj); + } + if (op->size == -1) { + r_asm_op_set_asm (op, "(data)"); + } return op->size; } diff --git a/libr/asm/p/asm_pic.c b/libr/asm/p/asm_pic.c index 741af97f3c..27d8b60e2f 100644 --- a/libr/asm/p/asm_pic.c +++ b/libr/asm/p/asm_pic.c @@ -8,18 +8,19 @@ #include "../arch/pic/pic_midrange.h" static int asm_pic_disassemble(RAsm *a, RAsmOp *op, const ut8 *b, int l) { + int res = -1; + char opbuf[128]; + const char *opstr = opbuf; + strcpy (opbuf, "invalid"); if (a->cpu && strcasecmp (a->cpu, "baseline") == 0) { - return pic_baseline_disassemble (a, op, b, l); + res = pic_baseline_disassemble (op, opbuf, b, l); + } else if (a->cpu && strcasecmp (a->cpu, "midrange") == 0) { + res = pic_midrange_disassemble (op, opbuf, b, l); + } else if (a->cpu && strcasecmp (a->cpu, "pic18") == 0) { + res = pic_pic18_disassemble (op, opbuf, b, l); } - if (a->cpu && strcasecmp (a->cpu, "midrange") == 0) { - return pic_midrange_disassemble (a, op, b, l); - } - if (a->cpu && strcasecmp (a->cpu, "pic18") == 0) { - return pic_pic18_disassemble (a, op, b, l); - } - - snprintf (op->buf_asm, R_ASM_BUFSIZE - 1, "Unknown asm.cpu"); - return op->size = -1; + r_asm_op_set_asm (op, opstr); + return op->size = res; } RAsmPlugin r_asm_plugin_pic = { diff --git a/libr/asm/p/asm_ppc_cs.c b/libr/asm/p/asm_ppc_cs.c index fc620dde7a..1a90fdd710 100644 --- a/libr/asm/p/asm_ppc_cs.c +++ b/libr/asm/p/asm_ppc_cs.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2014-2015 - pancake */ +/* radare2 - LGPL - Copyright 2014-2018 - pancake */ #include #include @@ -24,10 +24,12 @@ static int decompile_vle(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } if (!vle_init (&handle, buf, len) && (instr = vle_next (&handle))) { op->size = instr->size; - vle_snprint (op->buf_asm, R_ASM_BUFSIZE, a->pc, instr); + char buf_asm[64]; + vle_snprint (buf_asm, sizeof (buf_asm), a->pc, instr); + r_asm_op_set_asm (op, buf_asm); vle_free (instr); } else { - strcpy (op->buf_asm, "invalid"); + r_asm_op_set_asm (op, "invalid"); op->size = 2; return -1; } @@ -41,12 +43,13 @@ static int decompile_ps(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } op->size = 4; const ut32 data = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; - if (libps_decode (data, &instr)) { - libps_snprint (op->buf_asm, R_ASM_BUFSIZE, a->pc, &instr); - } else { - strcpy (op->buf_asm, "invalid"); + if (libps_decode (data, &instr) < 1) { + r_asm_op_set_asm (op, "invalid"); return -1; } + char buf_asm[64]; + libps_snprint (buf_asm, sizeof (buf_asm), a->pc, &instr); + r_asm_op_set_asm (op, buf_asm); return op->size; } @@ -90,15 +93,12 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } } op->size = 4; - op->buf_asm[0] = 0; cs_option (handle, CS_OPT_DETAIL, CS_OPT_OFF); - n = cs_disasm (handle, (const ut8*) buf, len, off, 1, &insn); op->size = 4; if (n > 0 && insn->size > 0) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", - insn->mnemonic, insn->op_str[0] ? " " : "", - insn->op_str); + const char *opstr = sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str); + r_asm_op_set_asm (op, opstr); cs_free (insn, n); return op->size; } @@ -111,6 +111,7 @@ RAsmPlugin r_asm_plugin_ppc_cs = { .name = "ppc", .desc = "Capstone PowerPC disassembler", .license = "BSD", + .author = "pancake", .arch = "ppc", .cpus = "ppc,vle,ps", .bits = 32 | 64, diff --git a/libr/asm/p/asm_ppc_gnu.c b/libr/asm/p/asm_ppc_gnu.c index 935a3b0696..3c6faddb34 100644 --- a/libr/asm/p/asm_ppc_gnu.c +++ b/libr/asm/p/asm_ppc_gnu.c @@ -31,10 +31,10 @@ static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_in static void print_address(bfd_vma address, struct disassemble_info *info) { char tmp[32]; - if (!buf_global) - return; - sprintf(tmp, "0x%08"PFMT64x"", (ut64)address); - strcat(buf_global, tmp); + if (buf_global) { + sprintf (tmp, "0x%08"PFMT64x"", (ut64)address); + strcat (buf_global, tmp); + } } static int buf_fprintf(void *stream, const char *format, ...) { @@ -60,16 +60,16 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; - op->buf_asm[0]='\0'; - if (len<4) + if (len<4) { return -1; - buf_global = op->buf_asm; + } + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, 4); // TODO handle thumb /* prepare disassembler */ memset (&disasm_obj, '\0', sizeof (struct disassemble_info)); - disasm_obj.disassembler_options=(a->bits==64)?"64":""; + disasm_obj.disassembler_options = (a->bits==64)?"64":""; disasm_obj.buffer = bytes; disasm_obj.read_memory_func = &ppc_buffer_read_memory; disasm_obj.symbol_at_address_func = &symbol_at_address; @@ -78,14 +78,14 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { disasm_obj.endian = !a->big_endian; disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - - if (a->big_endian) + if (a->big_endian) { op->size = print_insn_big_powerpc ((bfd_vma)Offset, &disasm_obj); - else op->size = print_insn_little_powerpc ((bfd_vma)Offset, &disasm_obj); - - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); - + } else { + op->size = print_insn_little_powerpc ((bfd_vma)Offset, &disasm_obj); + } + if (op->size == -1) { + r_asm_op_set_asm (op, "(data)"); + } return op->size; } diff --git a/libr/asm/p/asm_propeller.c b/libr/asm/p/asm_propeller.c index d362bce07c..95d272024b 100644 --- a/libr/asm/p/asm_propeller.c +++ b/libr/asm/p/asm_propeller.c @@ -1,3 +1,5 @@ +/* radare - LGPL - Copyright 2014 - fedor.sakharov */ + #include #include #include @@ -6,23 +8,19 @@ #include -static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) -{ - int ret; +static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { + const char *buf_asm; struct propeller_cmd cmd; - - ret = propeller_decode_command (buf, &cmd); - + int ret = propeller_decode_command (buf, &cmd); if (cmd.prefix[0] && cmd.operands[0]) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s %s", cmd.prefix, cmd.instr, cmd.operands); + buf_asm = sdb_fmt ("%s %s %s", cmd.prefix, cmd.instr, cmd.operands); } else if (cmd.operands[0]) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s", cmd.instr, cmd.operands); + buf_asm = sdb_fmt ("%s %s", cmd.instr, cmd.operands); } else { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s", cmd.instr); + buf_asm = sdb_fmt ("%s", cmd.instr); } - + r_asm_op_set_asm (op, buf_asm); op->size = 4; - return ret; } diff --git a/libr/asm/p/asm_rsp.c b/libr/asm/p/asm_rsp.c index 24dbd47c65..a110390699 100644 --- a/libr/asm/p/asm_rsp.c +++ b/libr/asm/p/asm_rsp.c @@ -26,7 +26,6 @@ static void snappendf(char** dst, size_t* size, const char* format, ...) { } static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - ut32 iw; rsp_instruction r_instr; int i; char* buffer; @@ -39,10 +38,10 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } op->size = 4; - iw = r_read_ble32 (buf, a->big_endian); + ut32 iw = r_read_ble32 (buf, a->big_endian); r_instr = rsp_instruction_decode (a->pc, iw); - buffer = op->buf_asm; + buffer = r_strbuf_get (&op->buf_asm); size = sizeof (op->buf_asm); snappendf (&buffer, &size, r_instr.mnemonic); diff --git a/libr/asm/p/asm_sh.c b/libr/asm/p/asm_sh.c index 7996050e73..81b57485ef 100644 --- a/libr/asm/p/asm_sh.c +++ b/libr/asm/p/asm_sh.c @@ -57,8 +57,10 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { static struct disassemble_info disasm_obj; - if (len<2) return -1; - buf_global = op->buf_asm; + if (len < 2) { + return -1; + } + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, 2); @@ -73,14 +75,14 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - op->buf_asm[0] = '\0'; - if (disasm_obj.endian == BFD_ENDIAN_BIG) + if (disasm_obj.endian == BFD_ENDIAN_BIG) { op->size = print_insn_shb ((bfd_vma)Offset, &disasm_obj); - else + } else { op->size = print_insn_shl ((bfd_vma)Offset, &disasm_obj); - - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); + } + if (op->size == -1) { + r_asm_op_set_asm (op, "(data)"); + } return op->size; } diff --git a/libr/asm/p/asm_sparc_cs.c b/libr/asm/p/asm_sparc_cs.c index f9b5eba562..3eebe74b64 100644 --- a/libr/asm/p/asm_sparc_cs.c +++ b/libr/asm/p/asm_sparc_cs.c @@ -34,7 +34,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { n = cs_disasm (cd, buf, len, a->pc, 1, &insn); } if (n < 1) { - strcpy (op->buf_asm, "invalid"); + r_asm_op_set_asm (op, "invalid"); op->size = 4; ret = -1; goto beach; @@ -45,10 +45,11 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { goto beach; } op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", + char *buf_asm = sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", insn->op_str); - r_str_replace_char (op->buf_asm, '%', 0); + r_str_replace_char (buf_asm, '%', 0); + r_asm_op_set_asm (op, buf_asm); // TODO: remove the '$' in the string cs_free (insn, n); beach: diff --git a/libr/asm/p/asm_sparc_gnu.c b/libr/asm/p/asm_sparc_gnu.c index 6d701af60c..d4907eff53 100644 --- a/libr/asm/p/asm_sparc_gnu.c +++ b/libr/asm/p/asm_sparc_gnu.c @@ -38,11 +38,11 @@ static void print_address(bfd_vma address, struct disassemble_info *info) { static int buf_fprintf(void *stream, const char *format, ...) { va_list ap; char tmp[1024]; - if (!buf_global) - return 0; va_start (ap, format); - vsnprintf (tmp, sizeof (tmp), format, ap); - strcat (buf_global, tmp); + if (buf_global) { + vsnprintf (tmp, sizeof (tmp), format, ap); + strcat (buf_global, tmp); + } va_end (ap); return 0; } @@ -52,7 +52,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (len < 4) { return -1; } - buf_global = op->buf_asm; + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; // disasm inverted r_mem_swapendian (bytes, buf, 4); // TODO handle thumb @@ -71,15 +71,13 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { ? bfd_mach_sparc_v9b : 0); - op->buf_asm[0] = '\0'; op->size = print_insn_sparc ((bfd_vma)Offset, &disasm_obj); - if (!strncmp (op->buf_asm, "unknown", 7)) { - strncpy (op->buf_asm, "invalid", R_ASM_BUFSIZE); + if (!strncmp (r_strbuf_get (&op->buf_asm), "unknown", 7)) { + r_asm_op_set_asm (op, "invalid"); } - if (op->size == -1) { - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); + r_asm_op_set_asm (op, "(data)"); } return op->size; } diff --git a/libr/asm/p/asm_sysz.c b/libr/asm/p/asm_sysz.c index 38d9665f86..008a5d2c7a 100644 --- a/libr/asm/p/asm_sysz.c +++ b/libr/asm/p/asm_sysz.c @@ -30,21 +30,23 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { omode = mode; if (cd == 0) { ret = cs_open (CS_ARCH_SYSZ, mode, &cd); - if (ret) return 0; + if (ret) { + return 0; + } cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF); } n = cs_disasm (cd, (const ut8*)buf, len, off, 1, &insn); if (n>0) { if (insn->size>0) { op->size = insn->size; - char *ptrstr; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", - insn->mnemonic, insn->op_str[0]?" ":"", + char *buf_asm = sdb_fmt ("%s%s%s", + insn->mnemonic, insn->op_str[0]?" ": "", insn->op_str); - ptrstr = strstr (op->buf_asm, "ptr "); + char *ptrstr = strstr (buf_asm, "ptr "); if (ptrstr) { - memmove (ptrstr, ptrstr+4, strlen (ptrstr+4)+1); + memmove (ptrstr, ptrstr + 4, strlen (ptrstr + 4) + 1); } + r_asm_op_set_asm (op, buf_asm); } cs_free (insn, n); } diff --git a/libr/asm/p/asm_tms320.c b/libr/asm/p/asm_tms320.c index 03ebbb2e63..6bb1b8cd68 100644 --- a/libr/asm/p/asm_tms320.c +++ b/libr/asm/p/asm_tms320.c @@ -45,7 +45,7 @@ static int tms320c64x_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) } n = cs_disasm (cd, buf, len, a->pc, 1, &insn); if (n < 1) { - strcpy (op->buf_asm, "invalid"); + r_asm_op_set_asm (op, "invalid"); op->size = 4; ret = -1; goto beach; @@ -56,11 +56,10 @@ static int tms320c64x_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) goto beach; } op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", - insn->mnemonic, insn->op_str[0]? " ": "", - insn->op_str); - r_str_replace_char (op->buf_asm, '%', 0); - r_str_case (op->buf_asm, false); + char *buf_asm = sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", insn->op_str); + r_str_replace_char (buf_asm, '%', 0); + r_str_case (buf_asm, false); + r_asm_op_set_asm (op, buf_asm); cs_free (insn, n); beach: // cs_close (&cd); @@ -86,12 +85,11 @@ static int tms320_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { return tms320c64x_disassemble (a, op, buf, len); } #endif - snprintf (op->buf_asm, R_ASM_BUFSIZE - 1, "Unknown asm.cpu"); + r_asm_op_set_asm (op, "unknown asm.cpu"); return op->size = -1; } op->size = tms320_dasm (&engine, buf, len); - strncpy (op->buf_asm, engine.syntax, R_ASM_BUFSIZE - 1); - op->buf_asm[R_ASM_BUFSIZE - 1] = 0; + r_asm_op_set_asm (op, engine.syntax); return op->size; } diff --git a/libr/asm/p/asm_tms320c64x.c b/libr/asm/p/asm_tms320c64x.c index e90f8d55e6..ffdfbee941 100644 --- a/libr/asm/p/asm_tms320c64x.c +++ b/libr/asm/p/asm_tms320c64x.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2017 - pancake */ +/* radare2 - LGPL - Copyright 2017-2018 - pancake */ #include #include @@ -36,7 +36,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } n = cs_disasm (cd, buf, len, a->pc, 1, &insn); if (n < 1) { - strcpy (op->buf_asm, "invalid"); + r_asm_op_set_asm (op, "invalid"); op->size = 4; ret = -1; goto beach; @@ -47,11 +47,11 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { goto beach; } op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", + r_asm_op_set_asm (op, sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", - insn->op_str); - r_str_replace_char (op->buf_asm, '%', 0); - r_str_case (op->buf_asm, false); + insn->op_str)); + r_str_replace_char (r_strbuf_get (&op->buf_asm), '%', 0); + r_str_case (r_strbuf_get (&op->buf_asm), false); cs_free (insn, n); beach: // cs_close (&cd); diff --git a/libr/asm/p/asm_tricore.c b/libr/asm/p/asm_tricore.c index 0a5b15cd75..3e6da59ff3 100644 --- a/libr/asm/p/asm_tricore.c +++ b/libr/asm/p/asm_tricore.c @@ -94,7 +94,7 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; - buf_global = op->buf_asm; + buf_global = r_strbuf_get (&op->buf_asm); Offset = a->pc; memcpy (bytes, buf, R_MIN (len, 8)); // TODO handle thumb @@ -113,9 +113,9 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { disasm_obj.mach = 2; // select CPU TYPE op->size = print_insn_tricore ((bfd_vma)Offset, &disasm_obj); - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); - + if (op->size == -1) { + r_asm_op_set_asm (op, " (data)"); + } return op->size; } diff --git a/libr/asm/p/asm_v810.c b/libr/asm/p/asm_v810.c index 3774b33e99..f31f755561 100644 --- a/libr/asm/p/asm_v810.c +++ b/libr/asm/p/asm_v810.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2012-2015 - pancake */ +/* radare - LGPL - Copyright 2012-2018 - pancake */ #include #include @@ -13,11 +13,12 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { .instr = "", .operands = "" }; - if (len < 2) return -1; + if (len < 2) { + return -1; + } int ret = v810_decode_command (buf, len, &cmd); if (ret > 0) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s", - cmd.instr, cmd.operands); + r_asm_op_set_asm (op, sdb_fmt ("%s %s", cmd.instr, cmd.operands)); } return op->size = ret; } @@ -26,6 +27,7 @@ RAsmPlugin r_asm_plugin_v810 = { .name = "v810", .license = "LGPL3", .desc = "v810 disassembly plugin", + .author = "pancake", .arch = "v810", .bits = 32, .endian = R_SYS_ENDIAN_LITTLE, diff --git a/libr/asm/p/asm_v850.c b/libr/asm/p/asm_v850.c index 8e2a672027..21d2420fd1 100644 --- a/libr/asm/p/asm_v850.c +++ b/libr/asm/p/asm_v850.c @@ -19,8 +19,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } int ret = v850_decode_command (buf, len, &cmd); if (ret > 0) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s", - cmd.instr, cmd.operands); + r_asm_op_set_asm (op, sdb_fmt ("%s %s", cmd.instr, cmd.operands)); } return op->size = ret; } diff --git a/libr/asm/p/asm_vax.c b/libr/asm/p/asm_vax.c index 549db8bb32..90db378eda 100644 --- a/libr/asm/p/asm_vax.c +++ b/libr/asm/p/asm_vax.c @@ -50,8 +50,9 @@ static int buf_fprintf(void *stream, const char *format, ...) { int flen, glen; va_list ap; char *tmp; - if (!buf_global) + if (!buf_global) { return 0; + } va_start (ap, format); flen = strlen (format); glen = strlen (buf_global); @@ -69,10 +70,10 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; - op->buf_asm[0]='\0'; - if (len<4) + if (len < 4) { return -1; - buf_global = op->buf_asm; + } + buf_global = r_strbuf_get (&op->buf_asm); bytes = buf; bytes_size = len; Offset = a->pc; @@ -87,11 +88,11 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { disasm_obj.endian = BFD_ENDIAN_LITTLE; disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - op->size = print_insn_vax ((bfd_vma)Offset, &disasm_obj); - if (op->size == -1) - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); + if (op->size == -1) { + r_asm_op_set_asm (op, "(data)"); + } return op->size; } diff --git a/libr/asm/p/asm_wasm.c b/libr/asm/p/asm_wasm.c index 1307e49057..0481c7bea2 100644 --- a/libr/asm/p/asm_wasm.c +++ b/libr/asm/p/asm_wasm.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2017 - pancake, cgvwzq */ +/* radare - LGPL - Copyright 2017-2018 - pancake, cgvwzq */ // http://webassembly.org/docs/binary-encoding/#module-structure @@ -13,14 +13,14 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { WasmOp wop = {0}; int ret = wasm_dis (&wop, buf, len); - strncpy (op->buf_asm, wop.txt, sizeof (op->buf_asm)); - op->buf_asm[sizeof (op->buf_asm) - 1] = 0; + r_asm_op_set_asm (op, wop.txt); op->size = ret; return op->size; } static int assemble(RAsm *a, RAsmOp *op, const char *buf) { - op->size = wasm_asm (buf, op->buf, sizeof (op->buf)); + ut8 *opbuf = (ut8*)r_strbuf_get (&op->buf); + op->size = wasm_asm (buf, opbuf, 32); // XXX hardcoded opsize return op->size; } diff --git a/libr/asm/p/asm_x86_as.c b/libr/asm/p/asm_x86_as.c index 9dd1db8dec..8537b3c7b5 100644 --- a/libr/asm/p/asm_x86_as.c +++ b/libr/asm/p/asm_x86_as.c @@ -11,18 +11,19 @@ static int assemble(RAsm *a, RAsmOp *op, const char *buf) { char *ipath, *opath; - int ifd, ofd; const char *syntaxstr = ""; char asm_buf[R_ASM_BUFSIZE]; int len = 0; - ifd = r_file_mkstemp ("r_as", &ipath); - if (ifd == -1) + int ifd = r_file_mkstemp ("r_as", &ipath); + if (ifd == -1) { return -1; + } - ofd = r_file_mkstemp ("r_as", &opath); + int ofd = r_file_mkstemp ("r_as", &opath); if (ofd == -1) { free (ipath); + close (ifd); return -1; } @@ -45,22 +46,27 @@ static int assemble(RAsm *a, RAsmOp *op, const char *buf) { if (!r_sys_cmdf ("as %s -o %s", ipath, opath)) { const ut8 *begin, *end; close (ofd); - ofd = open (opath, O_BINARY|O_RDONLY); +// r_sys_cmdf ("cat %s", opath); + ofd = r_sandbox_open (opath, O_BINARY|O_RDONLY, 0644); if (ofd < 0) { free (ipath); free (opath); return -1; } - len = read (ofd, op->buf, R_ASM_BUFSIZE); - begin = r_mem_mem (op->buf, len, (const ut8*)"BEGINMARK", 9); - end = r_mem_mem (op->buf, len, (const ut8*)"ENDMARK", 7); + ut8 opbuf[512] = {0}; + len = read (ofd, opbuf, sizeof (opbuf)); + begin = r_mem_mem (opbuf, len, (const ut8*)"BEGINMARK", 9); + end = r_mem_mem (opbuf, len, (const ut8*)"ENDMARK", 7); if (!begin || !end) { eprintf ("Cannot find water marks\n"); len = 0; } else { - len = (int)(size_t)(end-begin-9); - if (len>0) memcpy (op->buf, begin+9, len); - else len = 0; + len = (int)(size_t)(end - begin - 9); + if (len > 0) { + r_asm_op_set_buf (op, begin + 9, len); + } else { + len = 0; + } } } else { eprintf ("Error running: as %s -o %s", ipath, opath); diff --git a/libr/asm/p/asm_x86_cs.c b/libr/asm/p/asm_x86_cs.c index 58eaa9309a..a7adfdbb4f 100644 --- a/libr/asm/p/asm_x86_cs.c +++ b/libr/asm/p/asm_x86_cs.c @@ -100,27 +100,29 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (a->features && *a->features) { if (!check_features (a, insn)) { op->size = insn->size; - strcpy (op->buf_asm, "illegal"); + r_asm_op_set_asm (op, "illegal"); } } if (op->size == 0 && n > 0 && insn->size > 0) { char *ptrstr; op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", + char *buf_asm = sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]?" ":"", insn->op_str); - ptrstr = strstr (op->buf_asm, "ptr "); + ptrstr = strstr (buf_asm, "ptr "); if (ptrstr) { memmove (ptrstr, ptrstr + 4, strlen (ptrstr + 4) + 1); } + r_asm_op_set_asm (op, buf_asm); } else { decompile_vm (a, op, buf, len); } if (a->syntax == R_ASM_SYNTAX_JZ) { - if (!strncmp (op->buf_asm, "je ", 3)) { - memcpy (op->buf_asm, "jz", 2); - } else if (!strncmp (op->buf_asm, "jne ", 4)) { - memcpy (op->buf_asm, "jnz", 3); + char *buf_asm = r_strbuf_get (&op->buf_asm); + if (!strncmp (buf_asm, "je ", 3)) { + memcpy (buf_asm, "jz", 2); + } else if (!strncmp (buf_asm, "jne ", 4)) { + memcpy (buf_asm, "jnz", 3); } } #if 0 diff --git a/libr/asm/p/asm_x86_nasm.c b/libr/asm/p/asm_x86_nasm.c index c01eaeb461..ec81d58116 100644 --- a/libr/asm/p/asm_x86_nasm.c +++ b/libr/asm/p/asm_x86_nasm.c @@ -31,8 +31,10 @@ static int assemble(RAsm *a, RAsmOp *op, const char *buf) { close (ifd); - if ( !r_sys_cmdf ("nasm %s -o %s", ipath, opath)) { - len = read (ofd, op->buf, R_ASM_BUFSIZE); + if (!r_sys_cmdf ("nasm %s -o %s", ipath, opath)) { + char buf[512]; // TODO: remove limits + len = read (ofd, buf, sizeof (buf)); + r_asm_op_set_buf (op, buf, len); } else { eprintf ("Error running 'nasm'\n"); len = 0; diff --git a/libr/asm/p/asm_x86_nz.c b/libr/asm/p/asm_x86_nz.c index 4ee3125faf..e781ba87c0 100644 --- a/libr/asm/p/asm_x86_nz.c +++ b/libr/asm/p/asm_x86_nz.c @@ -4541,7 +4541,8 @@ static int oprep(RAsm *a, ut8 *data, const Opcode *op) { } static int assemble(RAsm *a, RAsmOp *ao, const char *str) { - ut8 *data = ao->buf; + ut8 __data[32] = {0}; + ut8 *data = __data; char op[128]; LookupTable *lt_ptr; int retval = -1; @@ -4578,6 +4579,7 @@ static int assemble(RAsm *a, RAsmOp *ao, const char *str) { break; } } + r_asm_op_set_buf (ao, __data, retval); free (instr.mnemonic); return retval; } diff --git a/libr/asm/p/asm_x86_vm.c b/libr/asm/p/asm_x86_vm.c index 8cd81532c1..1e658dafde 100644 --- a/libr/asm/p/asm_x86_vm.c +++ b/libr/asm/p/asm_x86_vm.c @@ -28,86 +28,90 @@ #define VPCEXT2(y,x) ((y)[2]==(x)) void decompile_vm(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { + const char *buf_asm = "invalid"; if (len > 3 && buf[0] == 0x0F && buf[1] == 0x3F && (VPCEXT2 (buf, 0x01) || VPCEXT2 (buf, 0x05) || VPCEXT2 (buf, 0x07) || VPCEXT2 (buf, 0x0D) || VPCEXT2 (buf, 0x10))) { - if(a->syntax == R_ASM_SYNTAX_ATT) { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "vpcext $0x%x, $0x%x", buf[3], buf[2]); + if (a->syntax == R_ASM_SYNTAX_ATT) { + buf_asm = sdb_fmt ("vpcext $0x%x, $0x%x", buf[3], buf[2]); } else { - snprintf (op->buf_asm, R_ASM_BUFSIZE, "vpcext %xh, %xh", buf[2], buf[3]); + buf_asm = sdb_fmt ("vpcext %xh, %xh", buf[2], buf[3]); } op->size = 4; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 1 && buf[4] == 0) { /* 0F C6 28 01 00 vmcpuid */ - strcpy (op->buf_asm, "vmcpuid"); + buf_asm ="vmcpuid"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x00 && buf[4] == 0x00) { /* 0F C6 28 00 00 vmgetinfo */ - strcpy (op->buf_asm, "vmgetinfo"); + buf_asm ="vmgetinfo"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x00 && buf[4] == 0x01) { /* 0F C6 28 00 01 vmsetinfo */ - strcpy (op->buf_asm, "vmsetinfo"); + buf_asm ="vmsetinfo"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x00 && buf[4] == 0x02) { /* 0F C6 28 00 02 vmdxdsbl */ - strcpy (op->buf_asm, "vmdxdsbl"); + buf_asm ="vmdxdsbl"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x00 && buf[4] == 0x03) { /* 0F C6 28 00 03 vmdxenbl */ - strcpy (op->buf_asm, "vmdxenbl"); + buf_asm ="vmdxenbl"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x01 && buf[4] == 0x00) { /* 0F C6 28 01 00 vmcpuid */ - strcpy (op->buf_asm, "vmcpuid"); + buf_asm ="vmcpuid"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x01 && buf[4] == 0x01) { /* 0F C6 28 01 01 vmhlt */ - strcpy (op->buf_asm, "vmhlt"); + buf_asm ="vmhlt"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x01 && buf[4] == 0x02) { /* 0F C6 28 01 02 vmsplaf */ - strcpy (op->buf_asm, "vmsplaf"); + buf_asm ="vmsplaf"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x02 && buf[4] == 0x00) { /* 0F C6 28 02 00 vmpushfd */ - strcpy (op->buf_asm, "vmpushfd"); + buf_asm ="vmpushfd"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x02 && buf[4] == 0x01) { /* 0F C6 28 02 01 vmpopfd */ - strcpy (op->buf_asm, "vmpopfd"); + buf_asm ="vmpopfd"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x02 && buf[4] == 0x02) { /* 0F C6 28 02 02 vmcli */ - strcpy (op->buf_asm, "vmcli"); + buf_asm ="vmcli"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x02 && buf[4] == 0x03) { /* 0F C6 28 02 03 vmsti */ - strcpy (op->buf_asm, "vmsti"); + buf_asm ="vmsti"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x02 && buf[4] == 0x04) { /* 0F C6 28 02 04 vmiretd */ - strcpy (op->buf_asm, "vmiretd"); + buf_asm ="vmiretd"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x03 && buf[4] == 0x00) { /* 0F C6 28 03 00 vmsgdt */ - strcpy (op->buf_asm, "vmsgdt"); + buf_asm ="vmsgdt"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x03 && buf[4] == 0x01) { /* 0F C6 28 03 01 vmsidt */ - strcpy (op->buf_asm, "vmsidt"); + buf_asm ="vmsidt"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x03 && buf[4] == 0x02) { /* 0F C6 28 03 02 vmsldt */ - strcpy (op->buf_asm, "vmsldt"); + buf_asm ="vmsldt"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x03 && buf[4] == 0x03) { /* 0F C6 28 03 03 vmstr */ - strcpy (op->buf_asm, "vmstr"); + buf_asm ="vmstr"; op->size = 5; } else if (len > 4 && buf[0] == 0x0F && buf[1] == 0xC6 && buf[2] == 0x28 && buf[3] == 0x04 && buf[4] == 0x00) { /* 0F C6 28 04 00 vmsdte */ - strcpy (op->buf_asm, "vmsdte"); + buf_asm ="vmsdte"; op->size = 5; } + if (buf_asm) { + r_asm_op_set_asm (op, buf_asm); + } } #undef VPCEXT2 diff --git a/libr/asm/p/asm_xap.c b/libr/asm/p/asm_xap.c index 35f2c7a45a..6d3f8615e6 100644 --- a/libr/asm/p/asm_xap.c +++ b/libr/asm/p/asm_xap.c @@ -16,10 +16,12 @@ static int arch_xap_disasm(char *str, const unsigned char *buf, ut64 seek) { s->s_out = NULL; d = next_inst(s); if (d != NULL) { - xap_decode(s, d); - strcpy(str, d->d_asm); - free(d); - } else *str = '\0'; + xap_decode (s, d); + strcpy (str, d->d_asm); + free (d); + } else { + *str = '\0'; + } #if 0 if (s->s_ff_quirk) { sprintf(d->d_asm, "DC\t0x%x", i2u16(&d->d_inst)); @@ -29,8 +31,9 @@ static int arch_xap_disasm(char *str, const unsigned char *buf, ut64 seek) { return 0; } static int disassemble(RAsm *a, struct r_asm_op_t *op, const ut8 *buf, int len) { - arch_xap_disasm (op->buf_asm, buf, a->pc); - return (op->size=2); + char *buf_asm = r_strbuf_get (&op->buf_asm); + arch_xap_disasm (buf_asm, buf, a->pc); + return (op->size = 2); } RAsmPlugin r_asm_plugin_xap = { diff --git a/libr/asm/p/asm_xcore_cs.c b/libr/asm/p/asm_xcore_cs.c index 2dfda1d250..d0208ab34d 100644 --- a/libr/asm/p/asm_xcore_cs.c +++ b/libr/asm/p/asm_xcore_cs.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2014-2015 - pancake */ +/* radare2 - LGPL - Copyright 2014-2018 - pancake */ #include #include @@ -12,21 +12,25 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { memset (op, 0, sizeof (RAsmOp)); op->size = 4; ret = cs_open (CS_ARCH_XCORE, mode, &handle); - if (ret) goto fin; + if (ret) { + goto fin; + } cs_option (handle, CS_OPT_DETAIL, CS_OPT_OFF); n = cs_disasm (handle, (ut8*)buf, len, a->pc, 1, &insn); - if (n<1) { - strcpy (op->buf_asm, "invalid"); + if (n < 1) { + r_asm_op_set_asm (op, "invalid"); op->size = 4; ret = -1; goto beach; - } else ret = 4; - if (insn->size<1) + } + ret = 4; + if (insn->size < 1) { goto beach; + } op->size = insn->size; - snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s", + r_asm_op_set_asm (op, sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", - insn->op_str); + insn->op_str)); // TODO: remove the '$' in the string beach: cs_free (insn, n); @@ -39,6 +43,7 @@ RAsmPlugin r_asm_plugin_xcore_cs = { .name = "xcore", .desc = "Capstone XCore disassembler", .license = "BSD", + .author = "pancake", .arch = "xcore", .bits = 32, .endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG, diff --git a/libr/asm/p/asm_xtensa.c b/libr/asm/p/asm_xtensa.c index fb2eb42446..66a295ff31 100644 --- a/libr/asm/p/asm_xtensa.c +++ b/libr/asm/p/asm_xtensa.c @@ -66,8 +66,7 @@ static int buf_fprintf(void *stream, const char *format, ...) { static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { struct disassemble_info disasm_obj; - op->buf_asm[0] = '\0'; - buf_global = op->buf_asm; + buf_global = r_strbuf_get (&op->buf_asm); offset = a->pc; if (len > INSN_BUFFER_SIZE) { len = INSN_BUFFER_SIZE; @@ -89,7 +88,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { op->size = print_insn_xtensa ((bfd_vma)offset, &disasm_obj); if (op->size == -1) { - strncpy (op->buf_asm, " (data)", R_ASM_BUFSIZE); + r_asm_op_set_asm (op, "(data)"); } return op->size; } diff --git a/libr/asm/p/asm_z80.c b/libr/asm/p/asm_z80.c index 0a4a504b14..eeb7d567b7 100644 --- a/libr/asm/p/asm_z80.c +++ b/libr/asm/p/asm_z80.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2012-2017 - pancake */ +/* radare - LGPL - Copyright 2012-2018 - pancake */ #include #include @@ -13,13 +13,14 @@ static int do_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { } static int do_assemble(RAsm *a, RAsmOp *op, const char *buf) { - return op->size = z80asm (op->buf, buf); + return op->size = z80asm ((ut8*)r_strbuf_get (&op->buf), buf); } RAsmPlugin r_asm_plugin_z80 = { .name = "z80", .desc = "Zilog Z80", .license = "GPL", + .author = "condret", .arch = "z80", .bits = 8, .endian = R_SYS_ENDIAN_NONE, diff --git a/libr/bin/Makefile b/libr/bin/Makefile index 468333ad48..78ade6532d 100644 --- a/libr/bin/Makefile +++ b/libr/bin/Makefile @@ -20,7 +20,8 @@ include $(SHLR)/ar/deps.mk include $(LIBR)/magic/deps.mk STATIC_OBJS=$(addprefix $(LTOP)/bin/p/, $(STATIC_OBJ)) -OBJS=bin.o dbginfo.o bin_ldr.o bin_write.o demangle.o dwarf.o filter.o file.o obj.o open.o +OBJS=bin.o dbginfo.o bin_ldr.o bin_write.o demangle.o +OBJS+=dwarf.o filter.o bfile.o obj.o open.o OBJS+=mangling/cxx/cp-demangle.o ${STATIC_OBJS} OBJS+=mangling/demangler.o OBJS+=mangling/microsoft_demangle.o diff --git a/libr/bin/file.c b/libr/bin/bfile.c similarity index 99% rename from libr/bin/file.c rename to libr/bin/bfile.c index db9590901a..56c62dc0a3 100644 --- a/libr/bin/file.c +++ b/libr/bin/bfile.c @@ -794,7 +794,6 @@ R_API void r_bin_file_free(void /*RBinFile*/ *bf_) { if (a->curxtr && a->curxtr->destroy && a->xtr_obj) { a->curxtr->free_xtr ((void *)(a->xtr_obj)); } - r_buf_free (a->buf); // TODO: unset related sdb namespaces if (a && a->sdb_addrinfo) { sdb_free (a->sdb_addrinfo); diff --git a/libr/bin/bin.c b/libr/bin/bin.c index 2b1973c5bb..64a5e3e479 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -176,12 +176,11 @@ R_API void r_bin_info_free(RBinInfo *rb) { R_API RBinImport *r_bin_import_clone(RBinImport *o) { RBinImport *res = r_mem_dup (o, sizeof (*o)); - if (!res) { - return NULL; + if (res) { + res->name = R_STR_DUP (o->name); + res->classname = R_STR_DUP (o->classname); + res->descriptor = R_STR_DUP (o->descriptor); } - res->name = R_STR_DUP (o->name); - res->classname = R_STR_DUP (o->classname); - res->descriptor = R_STR_DUP (o->descriptor); return res; } diff --git a/libr/bin/meson.build b/libr/bin/meson.build index a8cc5f1fcf..f9ac4e66a5 100644 --- a/libr/bin/meson.build +++ b/libr/bin/meson.build @@ -5,7 +5,7 @@ files = [ 'demangle.c', 'dwarf.c', 'filter.c', - 'file.c', + 'bfile.c', 'obj.c', 'open.c', 'p/bin_any.c', diff --git a/libr/bin/p/bin_elf.c b/libr/bin/p/bin_elf.c index 123369d05b..5fc9c49084 100644 --- a/libr/bin/p/bin_elf.c +++ b/libr/bin/p/bin_elf.c @@ -483,7 +483,7 @@ static RBinSymbol *convert_symbol(struct Elf_(r_bin_elf_obj_t) *bin, if (!(ptr = R_NEW0 (RBinSymbol))) { return NULL; } - ptr->name = symbol->name[0] ? r_str_newf (namefmt, &symbol->name[0]) : strdup(""); + ptr->name = symbol->name[0] ? r_str_newf (namefmt, &symbol->name[0]) : strdup (""); ptr->forwarder = r_str_const ("NONE"); ptr->bind = r_str_const (symbol->bind); ptr->type = r_str_const (symbol->type); @@ -550,18 +550,15 @@ static RList* symbols(RBinFile *bf) { if (!symbol[i].size) { continue; } - ptr = convert_symbol (bin, &symbol[i], "imp.%s"); if (!ptr) { break; } - // special case where there is not entry in the plt for the import if (ptr->vaddr == UT32_MAX) { ptr->paddr = 0; ptr->vaddr = 0; } - insert_symbol (bin, ptr, symbol[i].is_sht_null, ret); } return ret; diff --git a/libr/core/asm.c b/libr/core/asm.c index f4708735c1..723fd46642 100644 --- a/libr/core/asm.c +++ b/libr/core/asm.c @@ -137,7 +137,7 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6 continue; } //opsz = op.size; - opst = strdup (op.buf_asm); + opst = strdup (r_strbuf_get (&op.buf_asm)); } matches = strcmp (opst, "invalid") && strcmp (opst, "unaligned"); if (matches && tokens[matchcount]) { @@ -355,17 +355,18 @@ static int handle_disassembly_overlap(RCore* core, RList *hits, ut8* buf, int le static int is_addr_in_range(ut64 start, ut64 end, ut64 start_range, ut64 end_range){ int result = false; - if (start == start_range) { return true; - } else if (start < end && start_range < end_range) { + } + if (start < end && start_range < end_range) { // ez cases - if ( start_range <= start && start < end_range ) + if (start_range <= start && start < end_range) { result = true; - else if (start_range < end && end < end_range ) + } else if (start_range < end && end < end_range) { result = true; - else if ( start <= start_range && end_range < end ) + } else if ( start <= start_range && end_range < end) { result = true; + } // XXX - these cases need to be tested // (long long) start_range < 0 < end_range } else if (start_range > end_range) { @@ -374,9 +375,9 @@ static int is_addr_in_range(ut64 start, ut64 end, ut64 start_range, ut64 end_ran result = true; else if (end <= end_range) result = true; - else if ( start_range <= start ) + else if (start_range <= start) result = true; - else if ( start_range < end ) + else if (start_range < end) result = true; // (long long) start < 0 < end } else { @@ -384,7 +385,7 @@ static int is_addr_in_range(ut64 start, ut64 end, ut64 start_range, ut64 end_ran result = true; else if (end <= end_range) result = true; - else if ( start_range <= start ) + else if (start_range <= start) result = true; } // XXX - these cases need to be tested @@ -414,7 +415,6 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) { RAsmOp op; // len = n * 32; // if (n > core->blocksize) n = core->blocksize; - ut8 *buf; ut64 at; ut32 idx = 0, hit_count; int numinstr, asmlen, ii; @@ -429,7 +429,7 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) { return NULL; } - buf = (ut8 *)malloc (len); + ut8 *buf = (ut8 *)malloc (len); if (!buf) { r_list_free (hits); return NULL; @@ -466,7 +466,7 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) { r_asm_set_pc (core->assembler, at); for (hit_count = 0; hit_count < n; hit_count++) { int instrlen = r_asm_disassemble (core->assembler, &op, - buf + len - addrbytes*(addr-at), addrbytes * (addr-at)); + buf + len - addrbytes * (addr - at), addrbytes * (addr - at)); add_hit_to_hits (hits, at, instrlen, true); at += instrlen; } @@ -545,14 +545,13 @@ static RList *r_core_asm_back_disassemble (RCore *core, ut64 addr, int len, ut64 RCoreAsmHit dummy_value; ut32 hit_count = 0; - if (disassmble_each_addr){ + if (disassmble_each_addr) { return r_core_asm_back_disassemble_all(core, addr, len, max_hit_count, extra_padding+1); } hits = r_core_asm_hit_list_new (); buf = malloc (len + extra_padding); - - if (!hits || !buf ){ + if (!hits || !buf) { if (hits) { r_list_purge (hits); free (hits); @@ -587,7 +586,7 @@ static RList *r_core_asm_back_disassemble (RCore *core, ut64 addr, int len, ut64 // so we need to account for that with current_buf_pos current_buf_pos = len - extra_padding - 1; next_buf_pos = len + extra_padding - 1; - current_instr_addr = addr-1; + current_instr_addr = addr - 1; do { if (r_cons_is_breaked ()) { break; @@ -601,19 +600,20 @@ static RList *r_core_asm_back_disassemble (RCore *core, ut64 addr, int len, ut64 eprintf("current_instr_addr: 0x%"PFMT64x", current_buf_pos: 0x%"PFMT64x", current_instr_len: %d \n", current_instr_addr, current_buf_pos, current_instr_len); ut8 *hex_str = (ut8*)r_hex_bin2strdup(buf+current_buf_pos, byte_cnt); - eprintf("==== current_instr_bytes: %s ",hex_str); + eprintf ("==== current_instr_bytes: %s ",hex_str); if (current_instr_len > 0) { - eprintf("op.buf_asm: %s\n", op.buf_asm); + eprintf("op.buf_asm: %s\n", r_strbuf_get (&op.buf_asm)); } else { eprintf("op.buf_asm: \n"); } free (hex_str); } - // disassembly invalid - if (current_instr_len == 0 || strstr (op.buf_asm, "invalid")) { - if (current_instr_len == 0) current_instr_len = 1; + if (current_instr_len == 0 || strstr (r_strbuf_get (&op.buf_asm), "invalid")) { + if (current_instr_len == 0) { + current_instr_len = 1; + } add_hit_to_sorted_hits(hits, current_instr_addr, current_instr_len, /* is_valid */ false); hit_count ++; last_num_invalid ++; diff --git a/libr/core/canal.c b/libr/core/canal.c index 1c554b5b4e..bf09cfdbec 100644 --- a/libr/core/canal.c +++ b/libr/core/canal.c @@ -850,11 +850,12 @@ R_API RAnalOp* r_core_anal_op(RCore *core, ut64 addr, int mask) { // decode instruction here r_asm_set_pc (core->assembler, addr); + r_asm_op_init (&asmop); if (r_asm_disassemble (core->assembler, &asmop, ptr, len) > 0) { - op->mnemonic = strdup (asmop.buf_asm); + op->mnemonic = strdup (r_strbuf_get (&asmop.buf_asm)); } + r_asm_op_fini (&asmop); return op; - err_op: free (op); return NULL; diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 9b5139c294..d631c78452 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -1336,7 +1336,7 @@ static void core_anal_bytes(RCore *core, const ut8 *buf, int len, int nops, int ret = r_anal_op (core->anal, &op, core->offset + idx, buf + idx, len - idx, R_ANAL_OP_MASK_ESIL); esilstr = R_STRBUF_SAFEGET (&op.esil); opexstr = R_STRBUF_SAFEGET (&op.opex); - char *mnem = strdup (asmop.buf_asm); + char *mnem = strdup (r_asm_op_get_asm (&asmop)); char *sp = strchr (mnem, ' '); if (sp) { *sp = 0; @@ -1361,16 +1361,18 @@ static void core_anal_bytes(RCore *core, const ut8 *buf, int len, int nops, int } size = (hint && hint->size)? hint->size: op.size; if (fmt == 'd') { - char *opname = strdup (asmop.buf_asm); - r_str_split (opname, ' '); - char *d = r_asm_describe (core->assembler, opname); - if (d && *d) { - r_cons_printf ("%s: %s\n", opname, d); - free (d); - } else { - eprintf ("Unknown opcode\n"); + char *opname = strdup (r_asm_op_get_asm (&asmop)); + if (opname) { + r_str_split (opname, ' '); + char *d = r_asm_describe (core->assembler, opname); + if (d && *d) { + r_cons_printf ("%s: %s\n", opname, d); + free (d); + } else { + eprintf ("Unknown opcode\n"); + } + free (opname); } - free (opname); } else if (fmt == 'e') { if (*esilstr) { if (use_color) { @@ -1398,13 +1400,13 @@ static void core_anal_bytes(RCore *core, const ut8 *buf, int len, int nops, int } else { r_cons_print (","); } - r_cons_printf ("{\"opcode\":\"%s\",", asmop.buf_asm); + r_cons_printf ("{\"opcode\":\"%s\",", r_asm_op_get_asm (&asmop)); { char strsub[128] = { 0 }; // pc+33 r_parse_varsub (core->parser, NULL, core->offset + idx, - asmop.size, asmop.buf_asm, + asmop.size, r_asm_op_get_asm (&asmop), strsub, sizeof (strsub)); { ut64 killme = UT64_MAX; @@ -1504,7 +1506,7 @@ static void core_anal_bytes(RCore *core, const ut8 *buf, int len, int nops, int if (fmt) r_cons_printf (fmt, arg);\ } printline ("address", "0x%" PFMT64x "\n", core->offset + idx); - printline ("opcode", "%s\n", asmop.buf_asm); + printline ("opcode", "%s\n", r_asm_op_get_asm (&asmop)); printline ("mnemonic", "%s\n", mnem); if (hint) { if (hint->opcode) { @@ -3051,8 +3053,7 @@ void cmd_anal_reg(RCore *core, const char *str) { break; case 'S': { // "arS" int sz; - ut8 *buf = r_reg_get_bytes ( - core->anal->reg, R_REG_TYPE_GPR, &sz); + ut8 *buf = r_reg_get_bytes (core->anal->reg, R_REG_TYPE_GPR, &sz); r_cons_printf ("%d\n", sz); free (buf); } break; @@ -3071,7 +3072,6 @@ void cmd_anal_reg(RCore *core, const char *str) { } ut8 *buf = r_reg_get_bytes (core->dbg->reg, type, &len); if (buf) { - //r_print_hexdump (core->print, 0LL, buf, len, 16, 16); r_print_hexdump (core->print, 0LL, buf, len, 32, 4, 1); free (buf); } @@ -3080,8 +3080,7 @@ void cmd_anal_reg(RCore *core, const char *str) { // TODO: set flag values with drc zf=1 { RRegItem *r; - const char *name = str + 1; - while (*name == ' ') name++; + const char *name = r_str_trim_ro (str + 1); if (*name && name[1]) { r = r_reg_cond_get (core->dbg->reg, name); if (r) { @@ -3434,7 +3433,6 @@ repeat: core->dbg->reg = reg; } else { r_anal_esil_parse (esil, R_STRBUF_SAFEGET (&op.esil)); - if (core->anal->cur && core->anal->cur->esil_post_loop) { core->anal->cur->esil_post_loop (esil, &op); } @@ -4806,7 +4804,7 @@ static void cmd_anal_aftertraps(RCore *core, const char *input) { int bufi, minop = 1; // 4 ut8 *buf; RBinFile *binfile; - RAnalOp op; + RAnalOp op = {0}; ut64 addr, addr_end; ut64 len = r_num_math (core->num, input); if (len > 0xffffff) { @@ -5431,14 +5429,15 @@ static bool cmd_anal_refs(RCore *core, const char *input) { r_asm_disassemble (core->assembler, &asmop, buf, size); char str[512]; fcn = r_anal_get_fcn_in (core->anal, ref->addr, 0); + r_str_ncpy (str, r_asm_op_get_asm (&asmop), sizeof (str) - 1); if (asm_varsub) { r_parse_varsub (core->parser, fcn, ref->addr, asmop.size, - asmop.buf_asm, asmop.buf_asm, sizeof (asmop.buf_asm)); + str, str, sizeof (str)); } r_parse_filter (core->parser, core->flags, - asmop.buf_asm, str, sizeof (str), core->print->big_endian); - - r_cons_printf ("{\"from\":%" PFMT64u ",\"type\":\"%s\",\"opcode\":\"%s\"", ref->addr, r_anal_xrefs_type_tostring (ref->type), str); + str, str, sizeof (str), core->print->big_endian); + r_cons_printf ("{\"from\":%" PFMT64u ",\"type\":\"%s\",\"opcode\":\"%s\"", + ref->addr, r_anal_xrefs_type_tostring (ref->type), str); if (fcn) { r_cons_printf (",\"fcn_addr\":%"PFMT64u",\"fcn_name\":\"%s\"", fcn->addr, fcn->name); } @@ -5495,12 +5494,17 @@ static bool cmd_anal_refs(RCore *core, const char *input) { r_asm_disassemble (core->assembler, &asmop, buf, size); fcn = r_anal_get_fcn_in (core->anal, ref->addr, 0); + int ba_len = r_strbuf_length (&asmop.buf_asm) + 128; + char *ba = malloc (ba_len); if (asm_varsub) { + strcpy (ba, r_strbuf_get (&asmop.buf_asm)); r_parse_varsub (core->parser, fcn, ref->addr, asmop.size, - asmop.buf_asm, asmop.buf_asm, sizeof (asmop.buf_asm)); + ba, ba, sizeof (asmop.buf_asm)); } r_parse_filter (core->parser, core->flags, - asmop.buf_asm, str, sizeof (str), core->print->big_endian); + ba, str, sizeof (str), core->print->big_endian); + r_asm_op_set_asm (&asmop, ba); + free (ba); if (has_color) { buf_asm = r_print_colorize_opcode (core->print, str, core->cons->pal.reg, core->cons->pal.num, false, fcn ? fcn->addr : 0); @@ -5577,7 +5581,8 @@ static bool cmd_anal_refs(RCore *core, const char *input) { r_asm_set_pc (core->assembler, ref->at); r_asm_disassemble (core->assembler, &asmop, buf, 12); r_parse_filter (core->parser, core->flags, - asmop.buf_asm, str, sizeof (str), core->print->big_endian); + r_asm_op_get_asm (&asmop), + str, sizeof (str), core->print->big_endian); if (has_color) { buf_asm = r_print_colorize_opcode (core->print, str, core->cons->pal.reg, core->cons->pal.num, false, fcn ? fcn->addr : 0); diff --git a/libr/core/cmd_cmp.c b/libr/core/cmd_cmp.c index df838c1166..3f7c715cec 100644 --- a/libr/core/cmd_cmp.c +++ b/libr/core/cmd_cmp.c @@ -326,7 +326,7 @@ static void cmd_cmp_watcher(RCore *core, const char *input) { static int cmd_cmp_disasm(RCore *core, const char *input, int mode) { RAsmOp op, op2; - int i, j, iseq; + int i, j; char colpad[80]; int hascolor = r_config_get_i (core->config, "scr.color"); int cols = r_config_get_i (core->config, "hex.cols") * 2; @@ -350,10 +350,10 @@ static int cmd_cmp_disasm(RCore *core, const char *input, int mode) { buf + j, core->blocksize - j); // show output - iseq = (!strcmp (op.buf_asm, op2.buf_asm)); - memset (colpad, ' ', sizeof(colpad)); + bool iseq = r_strbuf_equals (&op.buf_asm, &op2.buf_asm); + memset (colpad, ' ', sizeof (colpad)); { - int pos = strlen (op.buf_asm); + int pos = strlen (r_strbuf_get (&op.buf_asm)); pos = (pos > cols)? 0: cols - pos; colpad[pos] = 0; } @@ -390,7 +390,7 @@ static int cmd_cmp_disasm(RCore *core, const char *input, int mode) { buf + j, core->blocksize - j); // show output - iseq = (!strcmp (op.buf_asm, op2.buf_asm)); + bool iseq = r_strbuf_equals (&op.buf_asm, &op2.buf_asm); // (!strcmp (op.buf_asm, op2.buf_asm)); if (iseq) { r_cons_printf (" 0x%08"PFMT64x " %s\n", core->offset + i, op.buf_asm); diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index 76762d77ed..53db3bfc01 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -794,15 +794,16 @@ static int step_until_inst(RCore *core, const char *instr, bool regex) { // TODO: speedup if instructions are in the same block as the previous r_io_read_at (core->io, pc, buf, sizeof (buf)); ret = r_asm_disassemble (core->assembler, &asmop, buf, sizeof (buf)); - eprintf ("0x%08"PFMT64x" %d %s\n", pc, ret, asmop.buf_asm); + eprintf ("0x%08"PFMT64x" %d %s\n", pc, ret, r_asm_op_get_asm (&asmop)); // asmop.buf_asm); if (ret > 0) { + const char *buf_asm = r_asm_op_get_asm (&asmop); if (regex) { - if (r_regex_match (instr, "e", asmop.buf_asm)) { + if (r_regex_match (instr, "e", buf_asm)) { eprintf ("Stop.\n"); break; } } else { - if (strstr (asmop.buf_asm, instr)) { + if (strstr (buf_asm, instr)) { eprintf ("Stop.\n"); break; } diff --git a/libr/core/cmd_search.c b/libr/core/cmd_search.c index a47060e3f6..b9f81aa8ab 100644 --- a/libr/core/cmd_search.c +++ b/libr/core/cmd_search.c @@ -965,7 +965,8 @@ static RList *construct_rop_gadget(RCore *core, ut64 addr, ut8 *buf, int idx, co goto ret; } else { opsz = asmop.size; - opst = asmop.buf_asm; + // opsz = r_strbuf_length (asmop.buf); + opst = r_asm_op_get_asm (&asmop); } if (!r_str_ncasecmp (opst, "invalid", strlen ("invalid")) || !r_str_ncasecmp (opst, ".byte", strlen (".byte"))) { @@ -1040,7 +1041,7 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first) RCoreAsmHit *hit = NULL; RListIter *iter; RList *ropList = NULL; - char *buf_asm; + char *buf_asm = NULL; unsigned int size = 0; RAnalOp analop = R_EMPTY; RAsmOp asmop; @@ -1082,7 +1083,7 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first) } r_cons_printf ("{\"offset\":%"PFMT64d ",\"size\":%d," "\"opcode\":\"%s\",\"type\":\"%s\"}%s", - hit->addr, hit->len, asmop.buf_asm, + hit->addr, hit->len, r_strbuf_get (&asmop.buf_asm), r_anal_optype_to_string (analop.type), iter->n? ",": ""); free (buf); @@ -1111,18 +1112,17 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first) size += hit->len; const char *opstr = R_STRBUF_SAFEGET (&analop.esil); if (analop.type != R_ANAL_OP_TYPE_RET) { - char *opstr_n = r_str_newf (" %s", opstr); - r_list_append (ropList, (void *) opstr_n); + r_list_append (ropList, r_str_newf (" %s", opstr)); } if (esil) { r_cons_printf ("%s\n", opstr); } else if (colorize) { - buf_asm = r_print_colorize_opcode (core->print, asmop.buf_asm, + buf_asm = r_print_colorize_opcode (core->print, r_strbuf_get (&asmop.buf_asm), core->cons->pal.reg, core->cons->pal.num, false, 0); r_cons_printf (" %s%s;", buf_asm, Color_RESET); free (buf_asm); } else { - r_cons_printf (" %s;", asmop.buf_asm); + r_cons_printf (" %s;", r_strbuf_get (&asmop.buf_asm)); } free (buf); } @@ -1143,6 +1143,9 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first) continue; } ut8 *buf = malloc (1 + hit->len); + if (!buf) { + break; + } buf[hit->len] = 0; r_io_read_at (core->io, hit->addr, buf, hit->len); r_asm_set_pc (core->assembler, hit->addr); @@ -1154,7 +1157,7 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first) r_list_append (ropList, (void *) opstr_n); } if (colorize) { - buf_asm = r_print_colorize_opcode (core->print, asmop.buf_asm, + char *buf_asm = r_print_colorize_opcode (core->print, r_strbuf_get (&asmop.buf_asm), core->cons->pal.reg, core->cons->pal.num, false, 0); otype = r_print_color_op_type (core->print, analop.type); if (comment) { @@ -1168,10 +1171,10 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first) } else { if (comment) { r_cons_printf (" 0x%08"PFMT64x " %18s %s ; %s\n", - hit->addr, asmop.buf_hex, asmop.buf_asm, comment); + hit->addr, asmop.buf_hex, r_strbuf_get (&asmop.buf_asm), comment); } else { r_cons_printf (" 0x%08"PFMT64x " %18s %s\n", - hit->addr, asmop.buf_hex, asmop.buf_asm); + hit->addr, asmop.buf_hex, r_strbuf_get (&asmop.buf_asm)); } } free (buf); @@ -1217,7 +1220,6 @@ static int r_core_search_rop(RCore *core, RInterval search_itv, int opt, const c gadgetSdb = sdb_ns (core->sdb, "gadget_sdb", true); } } - if (max_count == 0) { max_count = -1; } @@ -1319,15 +1321,17 @@ static int r_core_search_rop(RCore *core, RInterval search_itv, int opt, const c } #endif epair = R_NEW0 (struct endlist_pair); - // If this arch has branch delay slots, add the next instr as well - if (end_gadget.delay) { - epair->instr_offset = i + increment; - epair->delay_size = end_gadget.delay; - } else { - epair->instr_offset = (intptr_t) i; - epair->delay_size = end_gadget.delay; + if (epair) { + // If this arch has branch delay slots, add the next instr as well + if (end_gadget.delay) { + epair->instr_offset = i + increment; + epair->delay_size = end_gadget.delay; + } else { + epair->instr_offset = (intptr_t) i; + epair->delay_size = end_gadget.delay; + } + r_list_append (end_list, (void *) (intptr_t) epair); } - r_list_append (end_list, (void *) (intptr_t) epair); } r_anal_op_fini (&end_gadget); if (r_cons_is_breaked ()) { @@ -1405,7 +1409,6 @@ static int r_core_search_rop(RCore *core, RInterval search_itv, int opt, const c if (align && (0 != ((from + i) % align))) { continue; } - if (gadgetSdb) { RListIter *iter; @@ -1800,7 +1803,7 @@ static void do_ref_search(RCore *core, ut64 addr,ut64 from, ut64 to, struct sear RAnalRef *ref; RListIter *iter; ut8 buf[12]; - RAsmOp asmop; + RAsmOp asmop; RList *list = r_anal_xrefs_get (core->anal, addr); if (list) { r_list_foreach (list, iter, ref) { @@ -1808,8 +1811,8 @@ static void do_ref_search(RCore *core, ut64 addr,ut64 from, ut64 to, struct sear r_asm_set_pc (core->assembler, ref->addr); r_asm_disassemble (core->assembler, &asmop, buf, size); fcn = r_anal_get_fcn_in (core->anal, ref->addr, 0); - r_parse_filter (core->parser, core->flags, - asmop.buf_asm, str, sizeof (str), core->print->big_endian); + r_parse_filter (core->parser, core->flags, r_strbuf_get (&asmop.buf_asm), + str, sizeof (str), core->print->big_endian); comment = r_meta_get_string (core->anal, R_META_TYPE_COMMENT, ref->addr); char *buf_fcn = comment ? r_str_newf ("%s; %s", fcn ? fcn->name : "(nofunc)", strtok (comment, "\n")) diff --git a/libr/core/core.c b/libr/core/core.c index 6c36a5b0ed..c60e968bb3 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -2560,11 +2560,12 @@ R_API int r_core_seek_align(RCore *core, ut64 align, int times) { R_API char *r_core_op_str(RCore *core, ut64 addr) { RAsmOp op = {0}; ut8 buf[64]; - int ret; r_asm_set_pc (core->assembler, addr); r_io_read_at (core->io, addr, buf, sizeof (buf)); - ret = r_asm_disassemble (core->assembler, &op, buf, sizeof (buf)); - return (ret > 0)? strdup (op.buf_asm): NULL; + int ret = r_asm_disassemble (core->assembler, &op, buf, sizeof (buf)); + char *str = (ret > 0)? strdup (r_strbuf_get (&op.buf_asm)): NULL; + r_asm_op_fini (&op); + return str; } R_API RAnalOp *r_core_op_anal(RCore *core, ut64 addr) { diff --git a/libr/core/disasm.c b/libr/core/disasm.c index 4f47ebf349..666e8fe6d7 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -828,6 +828,7 @@ static void ds_free(RDisasmState *ds) { r_io_fd_close (ds->core->io, ds->stackFd); } } + r_asm_op_fini (&ds->asmop); r_anal_op_fini (&ds->analop); r_anal_hint_free (ds->hint); ds_print_esil_anal_fini (ds); @@ -847,7 +848,7 @@ static void ds_free(RDisasmState *ds) { /* XXX move to r_print */ static char *colorize_asm_string(RCore *core, RDisasmState *ds, bool print_color) { char *spacer = NULL; - char *source = ds->opstr? ds->opstr: ds->asmop.buf_asm; + char *source = ds->opstr? ds->opstr: r_asm_op_get_asm (&ds->asmop); char *hlstr = r_meta_get_string (ds->core->anal, R_META_TYPE_HIGHLIGHT, ds->at); bool partial_reset = line_highlighted (ds) ? true : ((hlstr && *hlstr) ? true : false); RAnalFunction *f = ds->show_color_args ? fcnIn (ds, ds->vat, R_ANAL_FCN_TYPE_NULL) : NULL; @@ -897,7 +898,7 @@ static bool ds_must_strip(RDisasmState *ds) { } static void ds_highlight_word(RDisasmState * ds, char *word, char *color) { - char *source = ds->opstr? ds->opstr: ds->asmop.buf_asm; + char *source = ds->opstr? ds->opstr: r_asm_op_get_asm (&ds->asmop); const char *color_reset = line_highlighted (ds) ? ds->color_linehl : Color_RESET_BG; char *asm_str = r_str_highlight (source, word, color, color_reset); ds->opstr = asm_str? asm_str: source; @@ -906,7 +907,7 @@ static void ds_highlight_word(RDisasmState * ds, char *word, char *color) { static void ds_build_op_str(RDisasmState *ds, bool print_color) { RCore *core = ds->core; if (!ds->opstr) { - ds->opstr = strdup (ds->asmop.buf_asm); + ds->opstr = strdup (r_asm_op_get_asm (&ds->asmop)); } /* initialize */ core->parser->hint = ds->hint; @@ -1263,19 +1264,22 @@ static void ds_show_xrefs(RDisasmState *ds) { static void ds_atabs_option(RDisasmState *ds) { int n, i = 0, comma = 0, word = 0; - int size, brackets = 0; + int brackets = 0; char *t, *b; if (!ds || !ds->atabs) { return; } - size = strlen (ds->asmop.buf_asm) * (ds->atabs + 1) * 4; - if (size < 1 || size < strlen (ds->asmop.buf_asm)) { + int bufasm_len = r_strbuf_length (&ds->asmop.buf_asm); + int size = bufasm_len * (ds->atabs + 1) * 4; + if (size < 1 || size < bufasm_len) { return; } free (ds->opstr); ds->opstr = b = malloc (size + 1); - strncpy (b, ds->asmop.buf_asm, R_MIN (size, R_ASM_BUFSIZE)); - b[size] = 0; + if (!b) { + return; + } + strcpy (b, r_asm_op_get_asm (&ds->asmop)); for (; *b; b++, i++) { if (*b == '(' || *b == '[') { brackets++; @@ -2099,6 +2103,7 @@ static int ds_disassemble(RDisasmState *ds, ut8 *buf, int len) { ds->opstr = strdup (ds->hint->opcode); return true; } + r_asm_op_fini (&ds->asmop); ret = r_asm_disassemble (core->assembler, &ds->asmop, buf, len); if (ds->asmop.size < 1) { ds->asmop.size = 1; @@ -2143,20 +2148,17 @@ static int ds_disassemble(RDisasmState *ds, ut8 *buf, int len) { } int sz = R_MIN (16, meta->size - (ds->at - meta->from)); ds->asmop.size = sz; - r_hex_bin2str (buf, sz, ds->asmop.buf_hex); + r_asm_op_set_hexbuf (&ds->asmop, buf, sz); switch (meta->type) { case R_META_TYPE_STRING: - snprintf (ds->asmop.buf_asm, sizeof (ds->asmop.buf_asm) - 11, - ".string \"%s\"", meta->str); + r_asm_op_set_asm (&ds->asmop, sdb_fmt (".string \"%s\"", meta->str)); break; // case R_META_TYPE_DATA: // break; default: - snprintf (ds->asmop.buf_asm, sizeof (ds->asmop.buf_asm), - ".hex %.*s", (int)sizeof (ds->asmop.buf_asm) - 6, ds->asmop.buf_hex); + r_asm_op_set_asm (&ds->asmop, sdb_fmt (".hex %s", r_asm_op_get_hex (&ds->asmop))); break; } - ds->asmop.buf_asm[sizeof (ds->asmop.buf_asm) - 1] = 0; // strcpy (ds->asmop.buf_hex, "0102030405060708"); //return i; ds->oplen = sz; //ds->asmop.size; @@ -2165,7 +2167,7 @@ static int ds_disassemble(RDisasmState *ds, ut8 *buf, int len) { } if (ds->show_nodup) { - const char *opname = (ret < 1)? "invalid": ds->asmop.buf_asm; + const char *opname = (ret < 1)? "invalid": r_asm_op_get_asm (&ds->asmop); if (ds->prev_ins && !strcmp (ds->prev_ins, opname)) { if (!ds->prev_ins_eq) { ds->prev_ins_eq = true; @@ -2182,7 +2184,7 @@ static int ds_disassemble(RDisasmState *ds, ut8 *buf, int len) { if (ds->prev_ins) { R_FREE (ds->prev_ins); } - ds->prev_ins = strdup (ds->asmop.buf_asm); + ds->prev_ins = strdup (r_asm_op_get_asm (&ds->asmop)); } ds->oplen = ds->asmop.size; @@ -2210,15 +2212,16 @@ static int ds_disassemble(RDisasmState *ds, ut8 *buf, int len) { if (ds->pseudo) { r_parse_parse (core->parser, ds->opstr ? ds->opstr - : ds->asmop.buf_asm, + : r_asm_op_get_asm (&ds->asmop), ds->str); free (ds->opstr); ds->opstr = strdup (ds->str); } if (ds->acase) { - r_str_case (ds->asmop.buf_asm, 1); + r_str_case (r_asm_op_get_asm (&ds->asmop), 1); } else if (ds->capitalize) { - ds->asmop.buf_asm[0] = toupper (ds->asmop.buf_asm[0]); + char *ba = r_asm_op_get_asm (&ds->asmop); + *ba = toupper (*ba); } if (info && mt_sz != UT64_MAX) { ds->oplen = mt_sz; @@ -2920,7 +2923,7 @@ static void ds_print_show_bytes(RDisasmState *ds) { pad[j] = '\0'; str = strdup (""); } else { - str = strdup (ds->asmop.buf_hex); + str = strdup (r_asm_op_get_hex (&ds->asmop)); if (r_str_ansi_len (str) > ds->nb) { char *p = (char *)r_str_ansi_chrn (str, ds->nb); if (p) { @@ -4359,7 +4362,7 @@ static void ds_print_comments_right(RDisasmState *ds) { RCore *core = ds->core; ds_print_relocs (ds); if (ds->asm_describe && !ds->has_description) { - char *op, *locase = strdup (ds->asmop.buf_asm); + char *op, *locase = strdup (r_asm_op_get_asm (&ds->asmop)); if (!locase) { return; } @@ -5097,9 +5100,9 @@ R_API int r_core_print_disasm_instructions(RCore *core, int nb_bytes, int nb_opc hasanal = true; } tmpopstr = r_anal_op_to_string (core->anal, &ds->analop); - ds->opstr = (tmpopstr)? tmpopstr: strdup (ds->asmop.buf_asm); + ds->opstr = (tmpopstr)? tmpopstr: strdup (r_asm_op_get_asm (&ds->asmop)); } else if (ds->immtrim) { - ds->opstr = strdup (ds->asmop.buf_asm); + ds->opstr = strdup (r_asm_op_get_asm (&ds->asmop)); r_parse_immtrim (ds->opstr); } else if (ds->use_esil) { if (!hasanal) { @@ -5130,8 +5133,8 @@ R_API int r_core_print_disasm_instructions(RCore *core, int nb_bytes, int nb_opc } core->parser->hint = ds->hint; ds->hint = NULL; - r_parse_filter (core->parser, core->flags, ds->asmop.buf_asm, ds->str, - sizeof (ds->str), core->print->big_endian); + r_parse_filter (core->parser, core->flags, r_asm_op_get_asm (&ds->asmop), + ds->str, sizeof (ds->str), core->print->big_endian); ds->opstr = strdup (ds->str); asm_str = colorize_asm_string (core, ds, true); core->parser->flagspace = ofs; @@ -5139,10 +5142,11 @@ R_API int r_core_print_disasm_instructions(RCore *core, int nb_bytes, int nb_opc ds->opstr = asm_str; core->parser->flagspace = ofs; // ??? } else { - ds->opstr = strdup (ds->asmop.buf_asm); + ds->opstr = strdup (r_asm_op_get_asm (&ds->asmop)); } if (ds->immtrim) { - ds->opstr = strdup (ds->asmop.buf_asm); + free (ds->opstr); + ds->opstr = strdup (r_asm_op_get_asm (&ds->asmop)); r_parse_immtrim (ds->opstr); } } @@ -5291,22 +5295,30 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte result = true; continue; } - char *opstr = strdup (asmop.buf_asm); + char *opstr = strdup (r_asm_op_get_asm (&asmop)); ds->has_description = false; r_anal_op_fini (&ds->analop); r_anal_op (core->anal, &ds->analop, at, buf + i, nb_bytes - i, R_ANAL_OP_MASK_ALL); if (ds->pseudo) { - r_parse_parse (core->parser, asmop.buf_asm, asmop.buf_asm); + char *aba = r_asm_op_get_asm (&asmop); + r_parse_parse (core->parser, aba, aba); } // f = r_anal_get_fcn_in (core->anal, at, f = fcnIn (ds, at, R_ANAL_FCN_TYPE_FCN | R_ANAL_FCN_TYPE_SYM); if (ds->varsub && f) { core->parser->varlist = r_anal_var_list_dynamic; - r_parse_varsub (core->parser, f, at, ds->analop.size, - asmop.buf_asm, asmop.buf_asm, sizeof (asmop.buf_asm)); + int ba_len = r_strbuf_length (&asmop.buf_asm) + 128; + char *ba = malloc (ba_len); + if (ba) { + strcpy (ba, r_asm_op_get_asm (&asmop)); + r_parse_varsub (core->parser, f, at, ds->analop.size, + ba, ba, ba_len); + r_asm_op_set_asm (&asmop, ba); + free (ba); + } } oplen = r_asm_op_get_size (&asmop); ds->oplen = oplen; @@ -5324,7 +5336,7 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte core->parser->relsub_addr = killme; } } - r_parse_filter (core->parser, core->flags, asmop.buf_asm, str, + r_parse_filter (core->parser, core->flags, r_asm_op_get_asm (&asmop), str, sizeof (str), core->print->big_endian); r_cons_printf (j > 0 ? ",{" : "{"); @@ -5502,31 +5514,29 @@ R_API int r_core_print_disasm_all(RCore *core, ut64 addr, int l, int len, int mo count ++; switch (mode) { case 'i': - r_parse_filter (core->parser, core->flags, asmop.buf_asm, + r_parse_filter (core->parser, core->flags, r_asm_op_get_asm (&asmop), str, sizeof (str), core->print->big_endian); if (scr_color) { - char *buf_asm; RAnalOp aop; RAnalFunction *f = fcnIn (ds, ds->vat, R_ANAL_FCN_TYPE_NULL); r_anal_op (core->anal, &aop, addr, buf+i, l-i, R_ANAL_OP_MASK_ALL); - buf_asm = r_print_colorize_opcode (core->print, str, + char *buf_asm = r_print_colorize_opcode (core->print, str, core->cons->pal.reg, core->cons->pal.num, false, f ? f->addr : 0); - r_cons_printf ("%s%s\n", - r_print_color_op_type (core->print, aop.type), - buf_asm); - free (buf_asm); + if (buf_asm) { + r_cons_printf ("%s%s\n", r_print_color_op_type (core->print, aop.type), buf_asm); + free (buf_asm); + } } else { - r_cons_println (asmop.buf_asm); + r_cons_println (r_asm_op_get_asm (&asmop)); } break; case '=': if (i < 28) { - char *str = r_str_newf ("0x%08"PFMT64x" %60s %s\n", - ds->vat, "", asmop.buf_asm); + char *str = r_str_newf ("0x%08"PFMT64x" %60s %s\n", ds->vat, "", r_asm_op_get_asm (&asmop)); char *sp = strchr (str, ' '); if (sp) { char *end = sp + 60 + 1; - const char *src = asmop.buf_hex; + const char *src = r_asm_op_get_hex (&asmop); char *dst = sp + 1 + (i * 2); int len = strlen (src); if (dst < end) { @@ -5543,11 +5553,12 @@ R_API int r_core_print_disasm_all(RCore *core, ut64 addr, int l, int len, int mo break; case 'j': r_cons_printf ("{\"addr\":%08"PFMT64d",\"bytes\":\"%s\",\"inst\":\"%s\"}%s", - addr + i, asmop.buf_hex, asmop.buf_asm, ","); + addr + i, r_asm_op_get_hex (&asmop), r_asm_op_get_asm (&asmop), ","); break; default: r_cons_printf ("0x%08"PFMT64x" %20s %s\n", - addr + i, asmop.buf_hex, asmop.buf_asm); + addr + i, r_asm_op_get_hex (&asmop), + r_asm_op_get_asm (&asmop)); } } } @@ -5967,7 +5978,7 @@ toro: r_cons_println (esil); } else { if (decode) { - opstr = (tmpopstr)? tmpopstr: (asmop.buf_asm); + opstr = tmpopstr? tmpopstr: r_asm_op_get_asm (&(asmop)); } else if (esil) { opstr = (R_STRBUF_SAFEGET (&analop.esil)); } diff --git a/libr/core/vmenus.c b/libr/core/vmenus.c index 759b4b9537..6daebe2eb0 100644 --- a/libr/core/vmenus.c +++ b/libr/core/vmenus.c @@ -152,12 +152,12 @@ R_API bool r_core_visual_esil(RCore *core) { r_cons_printf ("r2's esil debugger:\n\n"); r_cons_printf ("pos: %d\n", x); { - char *res = r_print_hexpair (core->print, asmop.buf_hex, -1); + char *res = r_print_hexpair (core->print, r_asm_op_get_hex (&asmop), -1); r_cons_printf ("hex: %s\n"Color_RESET, res); free (res); } { - char *op = colorize_asm_string (core, asmop.buf_asm, analopType, core->offset); + char *op = colorize_asm_string (core, r_asm_op_get_asm (&asmop), analopType, core->offset); r_cons_printf (Color_RESET"asm: %s\n"Color_RESET, op); free (op); } @@ -290,7 +290,7 @@ static bool edit_bits (RCore *core) { analopType = analop.type & R_ANAL_OP_TYPE_MASK; r_cons_printf ("r2's bit editor:\n\n"); { - char *res = r_print_hexpair (core->print, asmop.buf_hex, -1); + char *res = r_print_hexpair (core->print, r_asm_op_get_hex (&asmop), -1); r_cons_printf ("hex: %s\n"Color_RESET, res); free (res); } @@ -300,7 +300,7 @@ static bool edit_bits (RCore *core) { r_cons_printf ("shift: >> %d << %d\n", word, (asmop.size * 8) - word - 1); } { - char *op = colorize_asm_string (core, asmop.buf_asm, analopType, core->offset); + char *op = colorize_asm_string (core, r_asm_op_get_asm (&asmop), analopType, core->offset); r_cons_printf (Color_RESET"asm: %s\n"Color_RESET, op); free (op); } diff --git a/libr/include/r_asm.h b/libr/include/r_asm.h index 20c8d9890a..abe3e57b7c 100644 --- a/libr/include/r_asm.h +++ b/libr/include/r_asm.h @@ -15,7 +15,7 @@ extern "C" { R_LIB_VERSION_HEADER(r_asm); -// XXX too big! +// XXX deprecate!!! too big! // the 256th character is left for the null terminator #define R_ASM_BUFSIZE 255 @@ -66,21 +66,25 @@ enum { }; typedef struct r_asm_op_t { - int size; // instruction size - int bitsize; // instruction size in bits (or 0 if fits in 8bit bytes) + int size; // instruction size (must be deprecated. just use buf.len + int bitsize; // instruction size in bits (or 0 if fits in 8bit bytes) // wtf why dupe this field? :D int payload; // size of payload (opsize = (size-payload)) // But this is pretty slow..so maybe we should add some accessors - ut8 buf[R_ASM_BUFSIZE + 1]; - char buf_asm[R_ASM_BUFSIZE + 1]; - char buf_hex[R_ASM_BUFSIZE + 1]; - RBuffer *buf_inc; + RStrBuf buf; + RStrBuf buf_asm; + RStrBuf buf_hex; + RBuffer *buf_inc; // must die } RAsmOp; typedef struct r_asm_code_t { +#if 1 int len; ut8 *buf; char *buf_hex; char *buf_asm; +#else + RAsmOp op; // we have those fields already inside RAsmOp +#endif RList *equs; // TODO: must be a hash ut64 code_offset; ut64 data_offset; @@ -120,7 +124,7 @@ typedef struct r_asm_t { int seggrn; } RAsm; -typedef int (*RAsmModifyCallback)(RAsm *a, ut8 *buf, int field, ut64 val); +typedef bool (*RAsmModifyCallback)(RAsm *a, ut8 *buf, int field, ut64 val); typedef struct r_asm_plugin_t { const char *name; @@ -146,9 +150,8 @@ typedef struct r_asm_plugin_t { #ifdef R_API /* asm.c */ R_API RAsm *r_asm_new(void); -#define r_asm_op_free free R_API RAsm *r_asm_free(RAsm *a); -R_API int r_asm_modify(RAsm *a, ut8 *buf, int field, ut64 val); +R_API bool r_asm_modify(RAsm *a, ut8 *buf, int field, ut64 val); R_API char *r_asm_mnemonics(RAsm *a, int id, bool json); R_API int r_asm_mnemonics_byname(RAsm *a, const char *name); R_API void r_asm_set_user_ptr(RAsm *a, void *user); @@ -187,10 +190,25 @@ R_API void r_asm_equ_item_free(RAsmEqu *equ); R_API bool r_asm_code_set_equ (RAsmCode *code, const char *key, const char *value); R_API char *r_asm_code_equ_replace (RAsmCode *code, char *str); +/* op.c */ +R_API RAsmOp *r_asm_op_new(); +R_API void r_asm_op_free(RAsmOp *op); +R_API void r_asm_op_init(RAsmOp *op); +R_API void r_asm_op_fini(RAsmOp *op); +R_API char *r_asm_op_get_hex(RAsmOp *op); +R_API char *r_asm_op_get_asm(RAsmOp *op); +R_API int r_asm_op_get_size(RAsmOp *op); +R_API void r_asm_op_set_asm(RAsmOp *op, const char *str); +R_API void r_asm_op_set_hex(RAsmOp *op, const char *str); +R_API void r_asm_op_set_hexbuf(RAsmOp *op, const ut8 *buf, int len); +R_API void r_asm_op_set_buf(RAsmOp *op, const ut8 *str, int len); + +#if 0 // accessors, to make bindings happy R_API char *r_asm_op_get_hex(RAsmOp *op); R_API char *r_asm_op_get_asm(RAsmOp *op); R_API int r_asm_op_get_size(RAsmOp *op); +#endif /* plugin pointers */ extern RAsmPlugin r_asm_plugin_bf; diff --git a/libr/include/r_util/r_buf.h b/libr/include/r_util/r_buf.h index f7e07e7349..2f0168dd7e 100644 --- a/libr/include/r_util/r_buf.h +++ b/libr/include/r_util/r_buf.h @@ -74,6 +74,7 @@ R_API int r_buf_fread_at(RBuffer *b, ut64 addr, ut8 *buf, const char *fmt, int n R_API int r_buf_write_at(RBuffer *b, ut64 addr, const ut8 *buf, int len); R_API int r_buf_fwrite_at(RBuffer *b, ut64 addr, ut8 *buf, const char *fmt, int n); R_API void r_buf_free(RBuffer *b); +R_API void r_buf_fini(RBuffer *b); R_API char *r_buf_free_to_string(RBuffer *b); R_API const ut8 *r_buf_buffer(RBuffer *b); R_API ut64 r_buf_size(RBuffer *b); diff --git a/libr/include/r_util/r_strbuf.h b/libr/include/r_util/r_strbuf.h index fa81a481cd..8baa5a3495 100644 --- a/libr/include/r_util/r_strbuf.h +++ b/libr/include/r_util/r_strbuf.h @@ -6,24 +6,28 @@ extern "C" { #endif typedef struct { + char buf[32]; int len; char *ptr; int ptrlen; - char buf[64]; } RStrBuf; #define R_STRBUF_SAFEGET(sb) (r_strbuf_get (sb) ? r_strbuf_get (sb) : "") R_API RStrBuf *r_strbuf_new(const char *s); R_API bool r_strbuf_set(RStrBuf *sb, const char *s); +R_API bool r_strbuf_setbin(RStrBuf *sb, const ut8 *s, int len); +R_API ut8* r_strbuf_getbin(RStrBuf *sb, int *len); R_API bool r_strbuf_setf(RStrBuf *sb, const char *fmt, ...); R_API int r_strbuf_append(RStrBuf *sb, const char *s); R_API int r_strbuf_append_n(RStrBuf *sb, const char *s, int l); R_API int r_strbuf_appendf(RStrBuf *sb, const char *fmt, ...); R_API char *r_strbuf_get(RStrBuf *sb); R_API char *r_strbuf_drain(RStrBuf *sb); +R_API int r_strbuf_length(RStrBuf *sb); R_API void r_strbuf_free(RStrBuf *sb); R_API void r_strbuf_fini(RStrBuf *sb); R_API void r_strbuf_init(RStrBuf *sb); +R_API bool r_strbuf_equals(RStrBuf *sa, RStrBuf *sb); #ifdef __cplusplus } diff --git a/libr/util/buf.c b/libr/util/buf.c index d71c276fc5..07960045b4 100644 --- a/libr/util/buf.c +++ b/libr/util/buf.c @@ -837,11 +837,14 @@ R_API void r_buf_deinit(RBuffer *b) { } else R_FREE (b->buf); } -R_API void r_buf_free(RBuffer *b) { +R_API void r_buf_fini(RBuffer *b) { if (!b) { return; } if (b->parent) { + if (b->parent == b) { + return; + } r_buf_free (b->parent); b->parent = NULL; } @@ -852,7 +855,11 @@ R_API void r_buf_free(RBuffer *b) { if (!b->ro) { r_buf_deinit (b); } - R_FREE (b); +} + +R_API void r_buf_free(RBuffer *b) { + r_buf_fini (b); + free (b); } R_API int r_buf_append_string (RBuffer *b, const char *str) { diff --git a/libr/util/strbuf.c b/libr/util/strbuf.c index 7791e5005f..c31649ab1d 100644 --- a/libr/util/strbuf.c +++ b/libr/util/strbuf.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2013-2014 - pancake */ +/* radare - LGPL - Copyright 2013-2018 - pancake */ #include "r_types.h" #include "r_util.h" @@ -12,23 +12,25 @@ R_API RStrBuf *r_strbuf_new(const char *str) { return s; } +R_API bool r_strbuf_equals(RStrBuf *sa, RStrBuf *sb) { + if (!sa || !sb || sa->len != sb->len) { // faster comparisons + return false; + } + return strcmp (r_strbuf_get (sa), r_strbuf_get (sb)) == 0; +} + +R_API int r_strbuf_length(RStrBuf *sb) { + return sb? sb->len: 0; +} + R_API void r_strbuf_init(RStrBuf *sb) { memset (sb, 0, sizeof (RStrBuf)); } -R_API bool r_strbuf_set(RStrBuf *sb, const char *s) { - int l; - if (!sb) { - return false; - } - if (!s) { - r_strbuf_init (sb); - return true; - } - l = strlen (s); +R_API bool r_strbuf_setbin(RStrBuf *sb, const ut8 *s, int l) { if (l >= sizeof (sb->buf)) { char *ptr = sb->ptr; - if (!ptr || l+1 > sb->ptrlen) { + if (!ptr || l + 1 > sb->ptrlen) { ptr = malloc (l + 1); if (!ptr) { return false; @@ -36,15 +38,28 @@ R_API bool r_strbuf_set(RStrBuf *sb, const char *s) { sb->ptrlen = l + 1; sb->ptr = ptr; } - memcpy (ptr, s, l+1); + memcpy (ptr, s, l); } else { sb->ptr = NULL; - memcpy (sb->buf, s, l+1); + memcpy (sb->buf, s, l); } sb->len = l; return true; } +R_API bool r_strbuf_set(RStrBuf *sb, const char *s) { + if (!sb) { + return false; + } + if (!s) { + r_strbuf_init (sb); + return true; + } + bool res = r_strbuf_setbin (sb, (const ut8*)s, strlen (s) + 1); + sb->len --; + return res; +} + R_API bool r_strbuf_setf(RStrBuf *sb, const char *fmt, ...) { int rc; bool ret; @@ -141,6 +156,16 @@ R_API char *r_strbuf_get(RStrBuf *sb) { return sb? (sb->ptr? sb->ptr: sb->buf) : NULL; } +R_API ut8 *r_strbuf_getbin(RStrBuf *sb, int *len) { + if (sb) { + if (len) { + *len = sb->len; + } + return (ut8*)(sb->ptr? sb->ptr: sb->buf); + } + return NULL; +} + R_API char *r_strbuf_drain(RStrBuf *sb) { char *ret = NULL; if (sb) { @@ -156,6 +181,7 @@ R_API void r_strbuf_free(RStrBuf *sb) { } R_API void r_strbuf_fini(RStrBuf *sb) { - if (sb && sb->ptr) + if (sb && sb->ptr) { R_FREE (sb->ptr); + } }