2014-01-26 00:58:25 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2014 - pancake, nibble */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-06-01 12:50:24 +00:00
|
|
|
#include <getopt.c> /* getopt.h is not portable :D */
|
2009-02-05 21:08:46 +00:00
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_asm.h>
|
2014-08-24 12:35:30 +00:00
|
|
|
#include <r_anal.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
#include <r_util.h>
|
2009-02-18 02:47:40 +00:00
|
|
|
#include <r_lib.h>
|
2013-04-04 00:32:13 +00:00
|
|
|
#include "../blob/version.c"
|
2009-02-18 02:47:40 +00:00
|
|
|
|
2014-08-24 12:35:30 +00:00
|
|
|
static RLib *l = NULL;
|
|
|
|
static RAsm *a = NULL;
|
|
|
|
static RAnal *anal = NULL;
|
2010-02-05 11:21:37 +00:00
|
|
|
static int coutput = R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2014-08-24 12:35:30 +00:00
|
|
|
static const char *has_esil(RAnal *a, const char *name) {
|
|
|
|
RListIter *iter;
|
|
|
|
RAnalPlugin *h;
|
|
|
|
r_list_foreach (a->plugins, iter, h) {
|
|
|
|
if (!strcmp (name, h->name)) {
|
|
|
|
if (h->esil)
|
|
|
|
return "Ae";
|
|
|
|
return "A_";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "__";
|
|
|
|
}
|
2014-02-05 23:26:17 +00:00
|
|
|
static void rasm2_list(RAsm *a, const char *arch) {
|
|
|
|
int i;
|
2014-02-25 16:03:12 +00:00
|
|
|
char bits[32];
|
2014-08-24 12:35:30 +00:00
|
|
|
const char *feat2, *feat;
|
2010-05-25 23:42:22 +00:00
|
|
|
RAsmPlugin *h;
|
2010-05-25 22:59:10 +00:00
|
|
|
RListIter *iter;
|
2011-10-12 01:24:19 +00:00
|
|
|
r_list_foreach (a->plugins, iter, h) {
|
2014-02-05 23:26:17 +00:00
|
|
|
if (arch) {
|
|
|
|
if (h->cpus && !strcmp (arch, h->name)) {
|
|
|
|
char *c = strdup (h->cpus);
|
|
|
|
int n = r_str_split (c, ',');
|
|
|
|
for (i=0;i<n;i++)
|
2014-02-25 16:03:12 +00:00
|
|
|
printf ("%s\n", r_str_word_get0 (c, i));
|
2014-02-05 23:26:17 +00:00
|
|
|
free (c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-25 16:03:12 +00:00
|
|
|
bits[0] = 0;
|
|
|
|
if (h->bits&8) strcat (bits, "8 ");
|
|
|
|
if (h->bits&16) strcat (bits, "16 ");
|
|
|
|
if (h->bits&32) strcat (bits, "32 ");
|
|
|
|
if (h->bits&64) strcat (bits, "64 ");
|
2014-08-24 12:35:30 +00:00
|
|
|
feat = "__";
|
2014-02-05 23:26:17 +00:00
|
|
|
if (h->assemble && h->disassemble) feat = "ad";
|
|
|
|
if (h->assemble && !h->disassemble) feat = "a_";
|
|
|
|
if (!h->assemble && h->disassemble) feat = "_d";
|
2014-08-24 12:35:30 +00:00
|
|
|
feat2 = has_esil (anal, h->name);
|
|
|
|
printf ("%s%s %-9s %-11s %-7s %s\n",
|
|
|
|
feat, feat2, bits, h->name,
|
2014-02-25 16:03:12 +00:00
|
|
|
h->license?h->license:"unknown", h->desc);
|
2014-02-05 23:26:17 +00:00
|
|
|
}
|
2011-10-12 01:24:19 +00:00
|
|
|
}
|
2010-05-25 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 10:11:20 +00:00
|
|
|
static int rasm_show_help(int v) {
|
|
|
|
printf ("Usage: rasm2 [-CdDehLBvw] [-a arch] [-b bits] [-o addr] [-s syntax]\n"
|
2013-10-14 23:54:37 +00:00
|
|
|
" [-f file] [-F fil:ter] [-i skip] [-l len] 'code'|hex|-\n");
|
2013-04-02 10:11:20 +00:00
|
|
|
if (v)
|
2013-06-17 01:26:48 +00:00
|
|
|
printf (" -a [arch] Set architecture to assemble/disassemble (see -L)\n"
|
2013-04-02 10:11:20 +00:00
|
|
|
" -b [bits] Set cpu register size (8, 16, 32, 64) (RASM2_BITS)\n"
|
2013-06-17 01:26:48 +00:00
|
|
|
" -c [cpu] Select specific CPU (depends on arch)\n"
|
2013-03-28 08:27:15 +00:00
|
|
|
" -C Output in C format\n"
|
2013-04-02 10:11:20 +00:00
|
|
|
" -d, -D Disassemble from hexpair bytes (-D show hexpairs)\n"
|
|
|
|
" -e Use big endian instead of little endian\n"
|
2013-02-24 20:12:30 +00:00
|
|
|
" -f [file] Read data from file\n"
|
2011-10-05 00:38:37 +00:00
|
|
|
" -F [in:out] Specify input and/or output filters (att2intel, x86.pseudo, ...)\n"
|
2013-04-02 10:11:20 +00:00
|
|
|
" -h Show this help\n"
|
2013-10-14 23:54:37 +00:00
|
|
|
" -i [len] ignore/skip N bytes of the input buffer\n"
|
2013-04-21 23:09:27 +00:00
|
|
|
" -k [kernel] Select operating system (linux, windows, darwin, ..)\n"
|
2013-04-02 10:11:20 +00:00
|
|
|
" -l [len] Input/Output length\n"
|
2014-12-09 03:13:26 +00:00
|
|
|
" -L List supported asm plugins + features:\n"
|
|
|
|
" a___ asm, _d__ disasm, __A_ analyzer, ___e ESIL\n"
|
2012-10-22 00:28:42 +00:00
|
|
|
" -o [offset] Set start address for code (default 0)\n"
|
2014-01-26 00:58:25 +00:00
|
|
|
" -O [file] Output file name (rasm2 -Bf a.asm -O a)\n"
|
2009-03-08 23:49:15 +00:00
|
|
|
" -s [syntax] Select syntax (intel, att)\n"
|
2009-04-05 14:02:17 +00:00
|
|
|
" -B Binary input/output (-l is mandatory for binary input)\n"
|
2011-07-25 10:22:55 +00:00
|
|
|
" -v Show version information\n"
|
2013-02-24 20:12:30 +00:00
|
|
|
" -w What's this instruction for? describe opcode\n"
|
2009-04-05 14:02:17 +00:00
|
|
|
" If '-l' value is greater than output length, output is padded with nops\n"
|
2012-10-22 00:28:42 +00:00
|
|
|
" If the last argument is '-' reads from stdin\n");
|
2009-04-14 23:19:42 +00:00
|
|
|
return 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 03:01:55 +00:00
|
|
|
static int rasm_disasm(char *buf, ut64 offset, int len, int bits, int ascii, int bin, int hex) {
|
|
|
|
RAsmCode *acode;
|
2011-11-29 11:28:02 +00:00
|
|
|
ut8 *data = NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
char *ptr = buf;
|
|
|
|
int ret = 0;
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 word = 0, clen = 0;
|
2012-12-03 03:01:55 +00:00
|
|
|
if (bits == 1)
|
|
|
|
len /= 8;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-04-05 13:23:36 +00:00
|
|
|
if (bin) {
|
2013-02-14 19:08:42 +00:00
|
|
|
if (len<0) return R_FALSE;
|
2010-09-15 08:50:43 +00:00
|
|
|
clen = len; // XXX
|
2009-07-08 11:49:55 +00:00
|
|
|
data = (ut8*)buf;
|
2009-04-05 13:23:36 +00:00
|
|
|
} else if (ascii) {
|
2010-02-12 12:45:03 +00:00
|
|
|
clen = strlen (buf);
|
2009-07-08 11:49:55 +00:00
|
|
|
data = (ut8*)buf;
|
2009-04-05 13:23:36 +00:00
|
|
|
} else {
|
2010-09-15 08:50:43 +00:00
|
|
|
for (; *ptr; ptr++)
|
2011-08-10 09:24:15 +00:00
|
|
|
if (*ptr!=' ' && *ptr!='\n' && *ptr!='\r')
|
2010-09-15 08:50:43 +00:00
|
|
|
if (!(++word%2)) clen++;
|
2011-12-05 23:27:57 +00:00
|
|
|
data = malloc (clen+1);
|
2015-03-31 23:28:28 +00:00
|
|
|
if (r_hex_str2bin (buf, data)<1) {
|
|
|
|
ret = 0;
|
2011-11-29 11:28:02 +00:00
|
|
|
goto beach;
|
2015-03-31 23:28:28 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 13:23:36 +00:00
|
|
|
if (!len || clen <= len)
|
|
|
|
len = clen;
|
|
|
|
|
2011-11-13 03:08:08 +00:00
|
|
|
if (hex) {
|
|
|
|
RAsmOp op;
|
|
|
|
r_asm_set_pc (a, offset);
|
2013-07-08 01:38:44 +00:00
|
|
|
while (len-ret > 0) {
|
|
|
|
int dr = r_asm_disassemble (a, &op, data+ret, len-ret);
|
2013-12-06 04:18:57 +00:00
|
|
|
if (dr == -1 || op.size<1) {
|
|
|
|
op.size = 1;
|
2013-07-08 01:38:44 +00:00
|
|
|
strcpy (op.buf_asm, "invalid");
|
|
|
|
sprintf (op.buf_hex, "%02x", data[ret]);
|
|
|
|
}
|
|
|
|
printf ("0x%08"PFMT64x" %2d %24s %s\n",
|
2013-12-06 04:18:57 +00:00
|
|
|
a->pc, op.size, op.buf_hex, op.buf_asm);
|
|
|
|
ret += op.size;
|
2011-11-13 03:08:08 +00:00
|
|
|
r_asm_set_pc (a, offset+ret);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r_asm_set_pc (a, offset);
|
|
|
|
if (!(acode = r_asm_mdisassemble (a, data, len)))
|
2011-11-29 11:28:02 +00:00
|
|
|
goto beach;
|
2011-11-13 03:08:08 +00:00
|
|
|
printf ("%s", acode->buf_asm);
|
|
|
|
ret = acode->len;
|
|
|
|
r_asm_code_free (acode);
|
|
|
|
}
|
2011-11-29 11:28:02 +00:00
|
|
|
beach:
|
|
|
|
if (data && data != (ut8*)buf) free (data);
|
2009-04-26 22:34:54 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-02-05 11:21:37 +00:00
|
|
|
static void print_buf(char *str) {
|
|
|
|
int i;
|
|
|
|
if (coutput) {
|
|
|
|
printf ("\"");
|
|
|
|
for (i=1; *str; str+=2, i+=2) {
|
|
|
|
if (!(i%41)) {
|
|
|
|
printf ("\" \\\n\"");
|
|
|
|
i=1;
|
|
|
|
}
|
|
|
|
printf ("\\x%c%c", *str, str[1]);
|
|
|
|
}
|
|
|
|
printf ("\"\n");
|
|
|
|
} else printf ("%s\n", str);
|
|
|
|
}
|
|
|
|
|
2012-12-03 03:01:55 +00:00
|
|
|
static int rasm_asm(char *buf, ut64 offset, ut64 len, int bits, int bin) {
|
2013-04-02 10:11:20 +00:00
|
|
|
RAsmCode *acode;
|
|
|
|
int i, j, ret = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_set_pc (a, offset);
|
|
|
|
if (!(acode = r_asm_massemble (a, buf)))
|
2009-04-14 23:19:42 +00:00
|
|
|
return 0;
|
2012-12-03 03:01:55 +00:00
|
|
|
if (acode->len) {
|
2013-01-03 14:54:58 +00:00
|
|
|
ret = acode->len;
|
2012-12-03 03:01:55 +00:00
|
|
|
if (bin) {
|
|
|
|
write (1, acode->buf, acode->len);
|
|
|
|
} else {
|
|
|
|
int b = acode->len;
|
|
|
|
if (bits==1) {
|
|
|
|
int bytes = (b/8)+1;
|
2013-04-02 10:11:20 +00:00
|
|
|
for (i=0; i<bytes; i++)
|
|
|
|
for (j=0; j<8 && b--; j++)
|
2012-12-03 03:01:55 +00:00
|
|
|
printf ("%c", (acode->buf[i] & (1<<j))?'1':'0');
|
|
|
|
printf ("\n");
|
|
|
|
} else print_buf (acode->buf_hex);
|
|
|
|
}
|
2009-04-05 14:02:17 +00:00
|
|
|
}
|
2010-02-12 12:45:03 +00:00
|
|
|
r_asm_code_free (acode);
|
2013-01-03 14:54:58 +00:00
|
|
|
return ret > 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-02-18 02:47:40 +00:00
|
|
|
/* asm callback */
|
2010-09-15 08:50:43 +00:00
|
|
|
static int __lib_asm_cb(struct r_lib_plugin_t *pl, void *user, void *data) {
|
2010-05-25 23:42:22 +00:00
|
|
|
RAsmPlugin *hand = (struct r_asm_plugin_t *)data;
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_add (a, hand);
|
2009-02-18 02:47:40 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
static int __lib_asm_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
|
|
|
|
|
2010-09-15 08:50:43 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2013-11-28 04:51:09 +00:00
|
|
|
const char *path;
|
2013-03-28 08:27:15 +00:00
|
|
|
const char *env_arch = r_sys_getenv ("RASM2_ARCH");
|
|
|
|
const char *env_bits = r_sys_getenv ("RASM2_BITS");
|
2013-02-14 19:08:42 +00:00
|
|
|
char buf[R_ASM_BUFSIZE];
|
2013-06-17 01:26:48 +00:00
|
|
|
char *arch = NULL, *file = NULL, *filters = NULL, *kernel = NULL, *cpu = NULL;
|
2012-10-22 00:28:42 +00:00
|
|
|
ut64 offset = 0;
|
2014-01-26 00:58:25 +00:00
|
|
|
int fd =-1, dis = 0, ascii = 0, bin = 0, ret = 0, bits = 32, c, whatsop = 0;
|
2013-10-14 23:54:37 +00:00
|
|
|
ut64 len = 0, idx = 0, skip = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2014-05-24 21:37:29 +00:00
|
|
|
if (argc<2)
|
|
|
|
return rasm_show_help (0);
|
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
a = r_asm_new ();
|
2014-08-24 12:35:30 +00:00
|
|
|
anal = r_anal_new ();
|
2010-05-20 15:40:58 +00:00
|
|
|
l = r_lib_new ("radare_plugin");
|
|
|
|
r_lib_add_handler (l, R_LIB_TYPE_ASM, "(dis)assembly plugins",
|
2009-02-18 02:47:40 +00:00
|
|
|
&__lib_asm_cb, &__lib_asm_dt, NULL);
|
2013-11-28 04:51:09 +00:00
|
|
|
path = r_sys_getenv ("LIBR_PLUGINS");
|
|
|
|
if (!path || !*path)
|
2015-04-22 22:46:42 +00:00
|
|
|
path = R2_LIBDIR"/radare2/"R2_VERSION;
|
2013-11-28 04:51:09 +00:00
|
|
|
r_lib_opendir (l, path);
|
2015-04-22 22:46:42 +00:00
|
|
|
if (1) { //where & R_CORE_LOADLIBS_SYSTEM) {
|
|
|
|
r_lib_opendir (l, R2_LIBDIR"/radare2-extras/"R2_VERSION);
|
|
|
|
r_lib_opendir (l, R2_LIBDIR"/radare2-bindings/"R2_VERSION);
|
|
|
|
}
|
2009-02-18 02:47:40 +00:00
|
|
|
|
2009-02-18 15:20:14 +00:00
|
|
|
|
2011-11-12 03:51:45 +00:00
|
|
|
r_asm_use (a, R_SYS_ARCH);
|
2013-06-17 01:26:48 +00:00
|
|
|
r_asm_set_big_endian (a, R_FALSE);
|
2014-01-26 00:58:25 +00:00
|
|
|
while ((c = getopt (argc, argv, "i:k:DCc:eva:b:s:do:Bl:hLf:F:wO:")) != -1) {
|
2009-09-25 02:04:51 +00:00
|
|
|
switch (c) {
|
2013-04-21 23:09:27 +00:00
|
|
|
case 'k':
|
|
|
|
kernel = optarg;
|
|
|
|
break;
|
2011-11-13 03:08:08 +00:00
|
|
|
case 'D':
|
|
|
|
dis = 2;
|
|
|
|
break;
|
2010-02-12 12:45:03 +00:00
|
|
|
case 'f':
|
2010-02-12 17:40:05 +00:00
|
|
|
file = optarg;
|
2010-02-12 12:45:03 +00:00
|
|
|
break;
|
2011-10-05 00:38:37 +00:00
|
|
|
case 'F':
|
|
|
|
filters = optarg;
|
|
|
|
break;
|
2013-06-17 01:26:48 +00:00
|
|
|
case 'c':
|
|
|
|
cpu = optarg;
|
|
|
|
break;
|
2010-02-05 11:21:37 +00:00
|
|
|
case 'C':
|
|
|
|
coutput = R_TRUE;
|
|
|
|
break;
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'a':
|
2009-02-19 17:13:34 +00:00
|
|
|
arch = optarg;
|
2009-02-19 13:24:51 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2010-02-05 11:21:37 +00:00
|
|
|
bits = r_num_math (NULL, optarg);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2010-02-05 11:21:37 +00:00
|
|
|
if (!strcmp (optarg, "att"))
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_set_syntax (a, R_ASM_SYNTAX_ATT);
|
|
|
|
else r_asm_set_syntax (a, R_ASM_SYNTAX_INTEL);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dis = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
2010-02-12 12:45:03 +00:00
|
|
|
offset = r_num_math (NULL, optarg);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
2014-01-26 00:58:25 +00:00
|
|
|
case 'O':
|
|
|
|
fd = open (optarg, O_TRUNC|O_RDWR|O_CREAT, 0644);
|
|
|
|
if (fd != -1)
|
|
|
|
dup2 (fd, 1);
|
|
|
|
break;
|
2009-04-05 13:23:36 +00:00
|
|
|
case 'B':
|
|
|
|
bin = 1;
|
|
|
|
break;
|
2013-10-14 23:54:37 +00:00
|
|
|
case 'i':
|
|
|
|
skip = r_num_math (NULL, optarg);
|
|
|
|
break;
|
2009-04-05 13:23:36 +00:00
|
|
|
case 'l':
|
2010-02-05 11:21:37 +00:00
|
|
|
len = r_num_math (NULL, optarg);
|
2009-04-05 13:23:36 +00:00
|
|
|
break;
|
2009-04-11 21:24:37 +00:00
|
|
|
case 'L':
|
2014-02-05 23:26:17 +00:00
|
|
|
rasm2_list (a, argv[optind]);
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = 1;
|
|
|
|
goto beach;
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'e':
|
2013-06-17 01:26:48 +00:00
|
|
|
r_asm_set_big_endian (a, !!!a->big_endian);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
2011-07-25 10:22:55 +00:00
|
|
|
case 'v':
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = blob_version ("rasm2");
|
|
|
|
goto beach;
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'h':
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = rasm_show_help (1);
|
|
|
|
goto beach;
|
2013-02-24 20:12:30 +00:00
|
|
|
case 'w':
|
|
|
|
whatsop = R_TRUE;
|
|
|
|
break;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-19 17:13:34 +00:00
|
|
|
if (arch) {
|
2010-05-20 15:40:58 +00:00
|
|
|
if (!r_asm_use (a, arch)) {
|
2013-02-14 19:08:42 +00:00
|
|
|
eprintf ("rasm2: Unknown asm plugin '%s'\n", arch);
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = 0;
|
|
|
|
goto beach;
|
2009-02-19 17:13:34 +00:00
|
|
|
}
|
2010-02-12 12:45:03 +00:00
|
|
|
if (!strcmp (arch, "bf"))
|
2010-01-08 17:25:25 +00:00
|
|
|
ascii = 1;
|
2013-03-28 08:27:15 +00:00
|
|
|
} else if (env_arch) {
|
|
|
|
if (!r_asm_use (a, env_arch)) {
|
|
|
|
eprintf ("rasm2: Unknown asm plugin '%s'\n", env_arch);
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = 0;
|
|
|
|
goto beach;
|
2013-03-28 08:27:15 +00:00
|
|
|
}
|
2010-05-20 15:40:58 +00:00
|
|
|
} else if (!r_asm_use (a, "x86")) {
|
2013-02-14 19:08:42 +00:00
|
|
|
eprintf ("rasm2: Cannot find asm.x86 plugin\n");
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = 0;
|
|
|
|
goto beach;
|
2009-02-19 17:13:34 +00:00
|
|
|
}
|
2013-06-17 01:26:48 +00:00
|
|
|
r_asm_set_cpu (a, cpu);
|
2013-04-02 10:11:20 +00:00
|
|
|
r_asm_set_bits (a, (env_bits && *env_bits)? atoi (env_bits): bits);
|
2013-04-21 23:09:27 +00:00
|
|
|
a->syscall = r_syscall_new ();
|
|
|
|
r_syscall_setup (a->syscall, arch, kernel, bits);
|
2009-02-19 17:13:34 +00:00
|
|
|
|
2013-02-24 20:12:30 +00:00
|
|
|
if (whatsop) {
|
|
|
|
const char *s = r_asm_describe (a, argv[optind]);
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = 1;
|
2013-02-24 20:12:30 +00:00
|
|
|
if (s) {
|
|
|
|
printf ("%s\n", s);
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = 0;
|
2013-02-24 20:12:30 +00:00
|
|
|
}
|
2014-05-24 21:37:29 +00:00
|
|
|
goto beach;
|
2013-02-24 20:12:30 +00:00
|
|
|
}
|
|
|
|
|
2011-10-05 00:38:37 +00:00
|
|
|
if (filters) {
|
|
|
|
char *p = strchr (filters, ':');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
if (*filters) r_asm_filter_input (a, filters);
|
|
|
|
if (p[1]) r_asm_filter_output (a, p+1);
|
|
|
|
*p = ':';
|
|
|
|
} else {
|
|
|
|
if (dis) r_asm_filter_output (a, filters);
|
|
|
|
else r_asm_filter_input (a, filters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 17:40:05 +00:00
|
|
|
if (file) {
|
|
|
|
char *content;
|
2014-09-20 12:43:18 +00:00
|
|
|
int length = 0;
|
2010-02-15 21:59:26 +00:00
|
|
|
if (!strcmp (file, "-")) {
|
2013-02-14 19:08:42 +00:00
|
|
|
ret = read (0, buf, sizeof (buf)-1);
|
2010-02-15 21:59:26 +00:00
|
|
|
if (ret == R_ASM_BUFSIZE)
|
2013-02-14 19:08:42 +00:00
|
|
|
eprintf ("rasm2: Cannot slurp all stdin data\n");
|
|
|
|
if (ret>=0) // only for text
|
2010-02-15 21:59:26 +00:00
|
|
|
buf[ret] = '\0';
|
2013-02-24 20:12:30 +00:00
|
|
|
len = ret;
|
2013-10-14 23:54:37 +00:00
|
|
|
if (dis) {
|
|
|
|
if (skip && length>skip) {
|
|
|
|
if (bin) {
|
|
|
|
memmove (buf, buf+skip, length-skip);
|
|
|
|
length -= skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = rasm_disasm (buf, offset, len,
|
2012-12-03 03:01:55 +00:00
|
|
|
a->bits, ascii, bin, dis-1);
|
2013-10-14 23:54:37 +00:00
|
|
|
} else ret = rasm_asm (buf, offset, len, a->bits, bin);
|
2010-02-15 21:59:26 +00:00
|
|
|
} else {
|
|
|
|
content = r_file_slurp (file, &length);
|
|
|
|
if (content) {
|
2013-02-15 12:24:09 +00:00
|
|
|
if (len && len>0 && len<length)
|
|
|
|
length = len;
|
2010-02-15 21:59:26 +00:00
|
|
|
content[length] = '\0';
|
2012-12-03 03:01:55 +00:00
|
|
|
if (dis) ret = rasm_disasm (content, offset,
|
2013-02-14 19:08:42 +00:00
|
|
|
length, a->bits, ascii, bin, dis-1);
|
|
|
|
else ret = rasm_asm (content, offset, length, a->bits, bin);
|
2014-10-19 16:16:53 +00:00
|
|
|
ret = !!! ret;
|
2010-02-15 21:59:26 +00:00
|
|
|
free (content);
|
2014-10-19 16:16:53 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("rasm2: Cannot open file %s\n", file);
|
|
|
|
ret = 1;
|
|
|
|
}
|
2010-02-15 21:59:26 +00:00
|
|
|
}
|
2010-02-12 17:40:05 +00:00
|
|
|
} else if (argv[optind]) {
|
|
|
|
if (!strcmp (argv[optind], "-")) {
|
2013-02-15 12:24:09 +00:00
|
|
|
int length;
|
2013-02-14 19:08:42 +00:00
|
|
|
do {
|
2013-02-15 12:24:09 +00:00
|
|
|
length = read (0, buf, sizeof (buf)-1);
|
2013-04-21 22:05:35 +00:00
|
|
|
if (length<1) break;
|
2013-02-15 12:24:09 +00:00
|
|
|
if (len>0 && len < length)
|
|
|
|
length = len;
|
2014-02-10 15:09:40 +00:00
|
|
|
buf[length] = 0;
|
2010-02-12 17:40:05 +00:00
|
|
|
if ((!bin || !dis) && feof (stdin))
|
|
|
|
break;
|
2013-10-14 23:54:37 +00:00
|
|
|
if (skip && length>skip) {
|
|
|
|
if (bin) {
|
2014-02-10 15:09:40 +00:00
|
|
|
memmove (buf, buf+skip, length-skip+1);
|
2013-10-14 23:54:37 +00:00
|
|
|
length -= skip;
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 03:01:48 +00:00
|
|
|
if (!bin || !dis) {
|
|
|
|
int buflen = strlen (buf);
|
|
|
|
if (buf[buflen]=='\n')
|
|
|
|
buf[buflen-1]='\0';
|
|
|
|
}
|
2012-12-03 03:01:55 +00:00
|
|
|
if (dis) ret = rasm_disasm (buf, offset,
|
2013-02-15 12:24:09 +00:00
|
|
|
length, a->bits, ascii, bin, dis-1);
|
|
|
|
else ret = rasm_asm (buf, offset, length, a->bits, bin);
|
2010-02-12 17:40:05 +00:00
|
|
|
idx += ret;
|
|
|
|
offset += ret;
|
2014-01-26 00:58:25 +00:00
|
|
|
if (!ret) goto beach;
|
2013-02-15 12:24:09 +00:00
|
|
|
} while (!len || idx<length);
|
2014-05-24 21:37:29 +00:00
|
|
|
ret = idx;
|
|
|
|
goto beach;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2013-10-14 23:54:37 +00:00
|
|
|
if (dis) {
|
|
|
|
char *buf = argv[optind];
|
|
|
|
len = strlen (buf);
|
|
|
|
if (skip && len>skip) {
|
|
|
|
skip *= 2;
|
2014-02-10 15:09:40 +00:00
|
|
|
//eprintf ("SKIP (%s) (%lld)\n", buf, skip);
|
2013-10-14 23:54:37 +00:00
|
|
|
memmove (buf, buf+skip, len-skip);
|
|
|
|
len -= skip;
|
|
|
|
buf[len] = 0;
|
|
|
|
}
|
2013-11-14 03:24:29 +00:00
|
|
|
if (!strncmp (buf, "0x", 2))
|
|
|
|
buf += 2;
|
2013-10-14 23:54:37 +00:00
|
|
|
ret = rasm_disasm (buf, offset, len,
|
|
|
|
a->bits, ascii, bin, dis-1);
|
|
|
|
} else ret = rasm_asm (argv[optind], offset, len, a->bits, bin);
|
2012-08-30 16:47:14 +00:00
|
|
|
if (!ret) eprintf ("invalid\n");
|
2014-01-26 00:58:25 +00:00
|
|
|
ret = !!!ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2014-01-26 00:58:25 +00:00
|
|
|
beach:
|
2014-05-24 21:37:29 +00:00
|
|
|
if (a)
|
|
|
|
r_asm_free (a);
|
|
|
|
if (l)
|
|
|
|
r_lib_free (l);
|
2014-01-26 00:58:25 +00:00
|
|
|
if (fd != -1)
|
|
|
|
close (fd);
|
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|