* Move R_SYS_DIR and R_SYS_HOME into r_types.h

* Parse space separated words in rax2
* Added r_str_binstr2bin() helper function in r_util/str
  - Converts binary string to raw bytes
  - rax2 -b is the commandline frontend for this
This commit is contained in:
pancake 2011-05-13 00:25:36 +02:00
parent 05f0e7e56b
commit c2d5f9215b
4 changed files with 83 additions and 41 deletions

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2010 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2007-2011 pancake<nopcode.org> */
#include <r_util.h>
@ -39,29 +39,31 @@ static int format_output (char mode, ut64 n) {
}
static int help () {
printf (" int -> hex ; rax2 10\n"
" hex -> int ; rax2 0xa\n"
" -int -> hex ; rax2 -77\n"
" -hex -> int ; rax2 0xffffffb3\n"
" int -> bin ; rax2 b30\n"
" bin -> int ; rax2 1010d\n"
" float -> hex ; rax2 3.33f\n"
" hex -> float ; rax2 Fx40551ed8\n"
" oct -> hex ; rax2 35o\n"
" hex -> oct ; rax2 Ox12 (O is a letter)\n"
" bin -> hex ; rax2 1100011b\n"
" hex -> bin ; rax2 Bx63\n"
" -e swap endianness ; rax2 -e 0x33\n"
" -s swap hex to bin ; rax2 -s 43 4a 50\n"
" -S swap bin to hex ; rax2 -S C J P\n"
" -V version ; rax2 -V\n"
" -h help ; rax2 -h\n");
printf (
" int -> hex ; rax2 10\n"
" hex -> int ; rax2 0xa\n"
" -int -> hex ; rax2 -77\n"
" -hex -> int ; rax2 0xffffffb3\n"
" int -> bin ; rax2 b30\n"
" bin -> int ; rax2 1010d\n"
" float -> hex ; rax2 3.33f\n"
" hex -> float ; rax2 Fx40551ed8\n"
" oct -> hex ; rax2 35o\n"
" hex -> oct ; rax2 Ox12 (O is a letter)\n"
" bin -> hex ; rax2 1100011b\n"
" hex -> bin ; rax2 Bx63\n"
" -e swap endianness ; rax2 -e 0x33\n"
" -b binstr -> bin ; rax2 -b 01000101 01110110\n"
" -s hexstr -> bin ; rax2 -s 43 4a 50\n"
" -S bin -> hexstr ; rax2 -S C J P\n"
" -V version ; rax2 -V\n"
" -h help ; rax2 -h\n");
return R_TRUE;
}
static int rax (char *str) {
float f;
char *buf, out_mode = '0';
char *p, *buf, out_mode = '0';
int i;
if (*str=='-') {
@ -69,11 +71,14 @@ static int rax (char *str) {
case 's':
flags ^= 1;
break;
case 'e':
flags ^= 2;
break;
case 'S':
flags ^= 4;
break;
case 'e':
flags ^= 2;
case 'b':
flags ^= 8;
break;
case 'V':
printf ("rax2 v"R2_VERSION"\n");
@ -89,9 +94,8 @@ static int rax (char *str) {
if (*str=='q')
return R_FALSE;
else
if (*str=='h' || *str=='?') {
if (*str=='h' || *str=='?')
return help ();
}
if (flags & 1) {
ut64 n = ((strlen (str))>>1)+1;
@ -108,6 +112,15 @@ static int rax (char *str) {
printf ("\n");
return R_TRUE;
}
if (flags & 8) {
int i, len;
ut8 buf[4096];
len = r_str_binstr2bin (str, buf, sizeof (buf));
if (len>0)
for (i=0; i<len; i++)
printf ("%c", buf[i]);
return R_TRUE;
}
if (str[0]=='0' && str[1]=='x') {
out_mode = 'I';
@ -133,13 +146,20 @@ static int rax (char *str) {
printf ("Fx%02x%02x%02x%02x\n", p[0], p[1], p[2], p[3]);
return R_TRUE;
}
return format_output (out_mode, r_num_math (NULL, str));
while ((p = strchr (str, ' '))) {
*p = 0;
format_output (out_mode, r_num_math (NULL, str));
str = p+1;
}
if (*str)
format_output (out_mode, r_num_math (NULL, str));
return 0;
}
static int use_stdin () {
char buf[1024];
char buf[4096]; // TODO: remove this limit
while (!feof (stdin)) {
fgets (buf, sizeof (buf)-1, stdin);
fgets (buf, sizeof (buf), stdin);
if (feof (stdin)) break;
buf[strlen (buf)-1] = '\0';
if (!rax (buf)) break;
@ -148,11 +168,10 @@ static int use_stdin () {
}
int main (int argc, char **argv) {
int i=1;
int i;
if (argc == 1)
return use_stdin ();
for (;i<argc; i++)
for (i=1; i<argc; i++)
rax (argv[i]);
return 0;
}

View File

@ -15,11 +15,16 @@
#include <stdarg.h>
#include <sys/time.h>
// TODO: FS or R_SYS_DIR ??
#undef FS
#if __WINDOWS__
#define FS "\\"
#define R_SYS_DIR "\\"
#define R_SYS_HOME "USERPROFILE"
#else
#define FS "/"
#define R_SYS_DIR "/"
#define R_SYS_HOME "HOME"
#endif
/* provide a per-module debug-enabled feature */

View File

@ -276,6 +276,7 @@ R_API char *r_str_concatch(char *x, char y);
R_API void r_str_case(char *str, int up);
R_API void r_str_chop_path (char *s);
R_API int r_str_binstr2bin(const char *str, ut8 *out, int outlen);
R_API int r_hex_pair2bin(const char *arg);
R_API int r_hex_str2binmask(const char *in, ut8 *out, ut8 *mask);
R_API int r_hex_str2bin(const char *in, ut8 *out);

View File

@ -107,6 +107,32 @@ static int hex2int (ut8 *val, ut8 c) {
return 0;
}
R_API int r_str_binstr2bin(const char *str, ut8 *out, int outlen) {
int n, i, j, k, ret, len;
len = strlen (str);
for (n=i=0; i<len; i+=8) {
ret = 0;
while (str[i]==' ')
str++;
if (i+7<len)
for (k=0, j=i+7; j>=i; j--, k++) {
// INVERSE for (k=0,j=i; j<i+8; j++,k++) {
if (str[j]==' ') {
//k--;
continue;
}
// printf ("---> j=%d (%c) (%02x)\n", j, str[j], str[j]);
if (str[j]=='1') ret|=1<<k;
else if (str[j]!='0') return n;
}
// printf ("-======> %02x\n", ret);
out[n++] = ret;
if (n==outlen)
return n;
}
return n;
}
R_API int r_str_rwx(const char *str) {
int ret = atoi (str);
if (!ret) {
@ -147,23 +173,15 @@ R_API void r_str_case(char *str, int up) {
}
}
#if __WINDOWS__
#define __ENV_HOME "USERPROFILE"
#define __ENV_DIR "\\"
#else
#define __ENV_HOME "HOME"
#define __ENV_DIR "/"
#endif
R_API char *r_str_home(const char *str) {
char *dst;
const char *home = r_sys_getenv (__ENV_HOME);
const char *home = r_sys_getenv (R_SYS_HOME);
if (home == NULL)
return NULL;
dst = (char *)malloc (strlen (home) + strlen (str)+2);
strcpy (dst, home);
if (str && *str) {
strcat (dst, __ENV_DIR);
strcat (dst, R_SYS_DIR);
strcat (dst, str);
}
return dst;
@ -394,8 +412,7 @@ R_API const char *r_str_get(const char *str) {
}
R_API char *r_str_dup(char *ptr, const char *string) {
if (ptr)
free (ptr);
if (ptr) free (ptr);
ptr = strdup (string);
return ptr;
}