- Fixed binary output of rasm2
  - Added R_ASM_BUFSIZE
  - vapi update
  - Added assemble test into asm.vala
This commit is contained in:
Nibble 2009-04-16 19:20:03 +02:00
parent e2b855b6e4
commit 161b7b745d
14 changed files with 42 additions and 32 deletions

View file

@ -20,7 +20,7 @@ static int r_asm_asciz(void *data, const char *input)
if (arg && (len = strlen(arg+1))) {
arg += 1; len += 1;
r_hex_bin2str((u8*)arg, len, (*aop)->buf_hex);
strncpy((char*)(*aop)->buf, arg, 1024);
strncpy((char*)(*aop)->buf, arg, R_ASM_BUFSIZE);
}
return len;
}
@ -33,7 +33,7 @@ static int r_asm_byte(void *data, const char *input)
if (arg) {
arg += 1;
len = r_hex_str2bin(arg, (*aop)->buf);
strncpy((*aop)->buf_hex, r_str_trim(arg), 1024);
strncpy((*aop)->buf_hex, r_str_trim(arg), R_ASM_BUFSIZE);
}
return len;
}
@ -196,7 +196,7 @@ R_API int r_asm_assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char
}
if (aop && ret > 0) {
r_hex_bin2str(aop->buf, ret, aop->buf_hex);
strncpy(aop->buf_asm, buf, 1024);
strncpy(aop->buf_asm, buf, R_ASM_BUFSIZE);
}
return ret;
}
@ -207,9 +207,10 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)
char name[256];
u64 offset;
} flags[1024];
char *lbuf = NULL, buf_hex[1024], *ptr = NULL, *ptr_start = NULL, *tokens[1024],
*label_name = NULL, buf_token[1024], buf_token2[1024];
u8 buf_bin[1024];
char *lbuf = NULL, *ptr = NULL, *ptr_start = NULL, *label_name = NULL,
*tokens[R_ASM_BUFSIZE], buf_hex[R_ASM_BUFSIZE],
buf_token[R_ASM_BUFSIZE], buf_token2[R_ASM_BUFSIZE];
u8 buf_bin[R_ASM_BUFSIZE];
int labels = 0, stage, ret, idx, ctr, i, j;
u64 label_offset;
@ -232,8 +233,9 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)
for (stage = 0; stage < 2; stage++) {
if (stage == 0 && !labels)
continue;
for (ret = i = j = 0, label_offset = a->pc; i <= ctr && j < 1024; i++, label_offset+=ret) {
strncpy(buf_token, tokens[i], 1024);
for (idx = ret = i = j = 0, label_offset = a->pc; i <= ctr;
i++, idx += ret, label_offset += ret) {
strncpy(buf_token, tokens[i], R_ASM_BUFSIZE);
if (stage == 1)
r_asm_set_pc(a, a->pc + ret);
if (labels) { /* Labels */
@ -241,7 +243,7 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)
while ((ptr = strchr(ptr_start, '_'))) {
if ((label_name = r_str_word_get_first(ptr))) {
if ((ptr == ptr_start)) {
if (stage == 0) {
if (stage == 0 && j < 1024) {
strncpy(flags[j].name, label_name, 256);
flags[j].offset = label_offset;
j++;
@ -258,9 +260,9 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)
if (j == 1024)
return 0;
}
snprintf(buf_token2, 1024, "%s0x%llx%s",
snprintf(buf_token2, R_ASM_BUFSIZE, "%s0x%llx%s",
ptr_start, label_offset, ptr+strlen(label_name));
strncpy(buf_token, buf_token2, 1024);
strncpy(buf_token, buf_token2, R_ASM_BUFSIZE);
ptr_start = buf_token;
}
free(label_name);
@ -276,15 +278,15 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)
if (!ret) {
return 0;
} else if (stage == 1) {
for (j = 0; j < ret && idx+j < 1024; j++)
for (j = 0; j < ret && idx+j < R_ASM_BUFSIZE; j++)
buf_bin[idx+j] = aop->buf[j];
strcat(buf_hex, aop->buf_hex);
}
}
}
memcpy(aop->buf, buf_bin, 1024);
memcpy(aop->buf_hex, buf_hex, 1024);
memcpy(aop->buf, buf_bin, R_ASM_BUFSIZE);
memcpy(aop->buf_hex, buf_hex, R_ASM_BUFSIZE);
return idx;
}

View file

@ -82,7 +82,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
aop->disasm_obj = &disasm_obj;
if (aop->inst_len == -1)
strcpy(aop->buf_asm, " (data)");
strncpy(aop->buf_asm, " (data)", R_ASM_BUFSIZE);
return aop->inst_len;
}

View file

@ -27,7 +27,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
dp.instr = bof;
M68k_Disassemble(&dp);
aop->disasm_obj = &dp;
sprintf(aop->buf_asm, "%s %s", opcode, operands);
snprintf(aop->buf_asm, R_ASM_BUFSIZE, "%s %s", opcode, operands);
aop->inst_len = 4;
return aop->inst_len;

View file

@ -86,7 +86,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
aop->disasm_obj = &disasm_obj;
if (aop->inst_len == -1)
strcpy(aop->buf_asm, " (data)");
strncpy(aop->buf_asm, " (data)", R_ASM_BUFSIZE);
return aop->inst_len;
}

View file

@ -27,7 +27,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
dp.instr = bof;
PPC_Disassemble(&dp, a->big_endian);
aop->disasm_obj = &dp;
sprintf(aop->buf_asm, "%s %s", opcode, operands);
snprintf(aop->buf_asm, R_ASM_BUFSIZE, "%s %s", opcode, operands);
aop->inst_len = 4;
return aop->inst_len;

View file

@ -81,7 +81,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
aop->disasm_obj = &disasm_obj;
if (aop->inst_len == -1)
strcpy(aop->buf_asm, " (data)");
strncpy(aop->buf_asm, " (data)", R_ASM_BUFSIZE);
return aop->inst_len;
}

View file

@ -26,7 +26,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
ud_disassemble(&disasm_obj);
aop->disasm_obj = &disasm_obj;
aop->inst_len = ud_insn_len(&disasm_obj);
snprintf(aop->buf_asm, 256, "%s", ud_insn_asm(&disasm_obj));
snprintf(aop->buf_asm, R_ASM_BUFSIZE, "%s", ud_insn_asm(&disasm_obj));
return aop->inst_len;
}

View file

@ -28,7 +28,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
aop->inst_len = Disasm(&disasm_obj);
aop->disasm_obj = &disasm_obj;
snprintf(aop->buf_asm, 256, disasm_obj.CompleteInstr);
snprintf(aop->buf_asm, R_ASM_BUFSIZE, disasm_obj.CompleteInstr);
return aop->inst_len;
}

View file

@ -14,12 +14,12 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
static int assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char *buf)
{
int len = 0;
char cmd[128];
char cmd[R_ASM_BUFSIZE];
u8 *out;
sprintf(cmd, "nasm /dev/stdin -o /dev/stdout <<__\nBITS %i\nORG 0x%llx\n%s\n__", a->bits, a->pc, buf);
out = (u8 *)r_sys_cmd_str(cmd, "", &len);
if (out) {
memcpy(aop->buf, out, len);
memcpy(aop->buf, out, len<=R_ASM_BUFSIZE?len:R_ASM_BUFSIZE);
free(out);
}
aop->inst_len = len;

View file

@ -16,7 +16,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
lowercase=1;
aop->inst_len = Disasm_olly(buf, len, a->pc, &disasm_obj, DISASM_FILE);
snprintf(aop->buf_asm, 256, "%s", disasm_obj.result);
snprintf(aop->buf_asm, R_ASM_BUFSIZE, "%s", disasm_obj.result);
return aop->inst_len;
}
@ -44,7 +44,7 @@ static int assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char *buf)
aop->disasm_obj = &asm_obj;
if (aop->inst_len > 0)
memcpy(aop->buf, asm_obj.code, aop->inst_len);
memcpy(aop->buf, asm_obj.code, aop->inst_len<=R_ASM_BUFSIZE?aop->inst_len:R_ASM_BUFSIZE);
return aop->inst_len;
}

View file

@ -184,9 +184,9 @@ int main(int argc, char *argv[])
if (argv[optind]) {
if (!strcmp(argv[optind], "-")) {
char buf[1024];
char buf[R_ASM_BUFSIZE];
for(;;) {
fgets(buf, 1024, stdin);
fgets(buf, R_ASM_BUFSIZE, stdin);
if ((!bin || !dis) && feof(stdin))
break;
if (!bin || !dis) buf[strlen(buf)-1]='\0';

View file

@ -7,6 +7,7 @@
#include <r_cmd.h>
#include <list.h>
#define R_ASM_BUFSIZE 1024
enum {
R_ASM_SYN_NULL = 0,
@ -16,10 +17,10 @@ enum {
struct r_asm_aop_t {
int inst_len;
u8 buf[1024];
char buf_asm[1024];
char buf_hex[1024];
char buf_err[1024];
u8 buf[R_ASM_BUFSIZE];
char buf_asm[R_ASM_BUFSIZE];
char buf_hex[R_ASM_BUFSIZE];
char buf_err[R_ASM_BUFSIZE];
void *disasm_obj;
};

View file

@ -72,6 +72,7 @@ namespace Radare {
public bool set_parser(Asm.Parser parser, parse_cb cb, void *aux);
public int disassemble(out Aop aop, uint8 *buf, uint64 length);
public int assemble(out Aop aop, string buf);
public int massemble(out Aop aop, string buf);
public int parse();
public void free();
}

View file

@ -22,10 +22,16 @@ public class AsmExample
Asm.Aop op;
uint8 *buf = "\x83\xe4\xf0";
string buf2 = "jmp _foo;nop;nop;nop;_foo:push eax";
if (st.disassemble(out op, buf, 3) <1) {
stderr.printf("internal error\n");
} else {
stdout.printf("asm: %s\n", op.buf_asm);
stdout.printf("disasm: %s\n", op.buf_asm);
}
if (st.massemble(out op, buf2) <1) {
stderr.printf("internal error\n");
} else {
stdout.printf("asm: %s\n", op.buf_hex);
}
}
}