Skip to content

Commit

Permalink
Rename rax2 =<base> to -b <base> ##tools
Browse files Browse the repository at this point in the history
  • Loading branch information
astralia authored and trufae committed May 9, 2024
1 parent 7b66e77 commit edf215e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 98 deletions.
165 changes: 86 additions & 79 deletions libr/main/rax2.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ typedef struct {
bool ipaddr2num; // -I
bool newline; // -n
bool jsonbases; // -j
bool forcebase; // -b
} RaxActions;

static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int *fm, PJ **pj);
typedef struct {
char imode;
char omode;
} RaxMode;

static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, RaxMode *mode, PJ **pj);

static int use_stdin(RNum *num, RaxActions *flags, int *fm, PJ **pj) {
static int use_stdin(RNum *num, RaxActions *flags, RaxMode *mode, PJ **pj) {
r_return_val_if_fail (num && flags, -1);
int rc = 0;
if (flags->slurphex) {
char buf[1]= {0};
if (!rax (num, buf, 1, 0, flags, fm, pj)) {
if (!rax (num, buf, 1, 0, flags, mode, pj)) {
rc = 1;
}
} else {
Expand All @@ -56,7 +62,7 @@ static int use_stdin(RNum *num, RaxActions *flags, int *fm, PJ **pj) {
if (!buf) {
break;
}
if (!rax (num, buf, l, 0, flags, fm, pj)) {
if (!rax (num, buf, l, 0, flags, mode, pj)) {
rc = 1;
}
free (buf);
Expand All @@ -76,16 +82,16 @@ static void rax2_newline(RaxActions flags) {
fflush (stdout);
}

static bool format_output(RNum *num, char mode, const char *s, int force_mode, RaxActions flags) {
static bool format_output(RNum *num, char mode, const char *s, RaxMode m, RaxActions flags) {
const char *errstr = NULL;
ut64 n = r_num_calc (num, s, &errstr);
if (errstr) {
R_LOG_ERROR (errstr);
return false;
}
char strbits[65];
if (force_mode) {
mode = force_mode;
if (!flags.forcebase) {
m.omode = mode;
}
if (flags.swapendian) {
ut64 n2 = n;
Expand All @@ -94,7 +100,7 @@ static bool format_output(RNum *num, char mode, const char *s, int force_mode, R
n >>= 32;
}
}
switch (mode) {
switch (m.omode) {
case 'I':
printf ("%" PFMT64d "\n", n);
break;
Expand Down Expand Up @@ -136,7 +142,7 @@ static bool format_output(RNum *num, char mode, const char *s, int force_mode, R
}
break;
default:
R_LOG_ERROR ("Unknown output mode %d", mode);
R_LOG_ERROR ("Unknown output mode %d", m.omode);
return false;
}
return true;
Expand All @@ -148,54 +154,54 @@ static void help_usage(void) {

static int help(void) {
printf (
" =[base] ; rax2 =10 0x46 -> output in base 10\n"
" 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"
" int -> ternary ; rax2 t42\n"
" bin -> int ; rax2 1010d\n"
" ternary -> int ; rax2 1010dt\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"
" ternary -> hex ; rax2 212t\n"
" hex -> ternary ; rax2 Tx23\n"
" raw -> hex ; rax2 -S < /binfile\n"
" hex -> raw ; rax2 -s 414141\n"
" -a show ascii table ; rax2 -a\n"
" -B str -> bin ; rax2 -B hello\n"
" -c output in C string ; rax2 -c 0x1234 # \\x34\\x12\\x00\\x00\n"
" -d force integer ; rax2 -d 3 -> 3 instead of 0x3\n"
" -e swap endianness ; rax2 -e 0x33\n"
" -D base64 decode ; rax2 -D \"aGVsbG8=\"\n"
" -E base64 encode ; rax2 -E \"hello\"\n"
" -f floating point ; rax2 -f 6.3+2.1\n"
" -F stdin slurp code hex ; rax2 -F < shellcode.[c/py/js]\n"
" -h help ; rax2 -h\n"
" -i dump as C byte array ; rax2 -i < bytes\n"
" -I IP address <-> LONG ; rax2 -I 3530468537\n"
" -j json format output ; rax2 -j 0x1234 # same as r2 -c '?j 0x1234'\n"
" -k keep base ; rax2 -k 33+3 -> 36\n"
" -K randomart ; rax2 -K 0x34 1020304050\n"
" -l bin -> str ; rax2 -l 01000101 01110110\n"
" -L bin -> hex(bignum) ; rax2 -L 111111111 # 0x1ff\n"
" -n newline ; append newline to output (for -E/-D/-r/..)\n"
" -o octalstr -> raw ; rax2 -o \\162 \\62 # r2\n"
" -r r2 style output ; rax2 -r 0x1234 # same as r2 -c '? 0x1234'\n"
" -s hexstr -> raw ; rax2 -s 43 4a 50\n"
" -S raw -> hexstr ; rax2 -S < /bin/ls > ls.hex\n"
" -rS raw -> hex.r2 ; rax2 -rS < /bin/ls > ls.r2\n"
" -t tstamp -> str ; rax2 -t 1234567890\n"
" -u units ; rax2 -u 389289238 # 317.0M\n"
" -v version ; rax2 -v\n"
" -w signed word ; rax2 -w 16 0xffff\n"
" -x output in hexpairs ; rax2 -x 0x1234 # 34120000\n"
" -X hash string ; rax2 -X linux osx\n"
" 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"
" int -> ternary ; rax2 t42\n"
" bin -> int ; rax2 1010d\n"
" ternary -> int ; rax2 1010dt\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"
" ternary -> hex ; rax2 212t\n"
" hex -> ternary ; rax2 Tx23\n"
" raw -> hex ; rax2 -S < /binfile\n"
" hex -> raw ; rax2 -s 414141\n"
" -a show ascii table ; rax2 -a\n"
" -b <base> output in <base> ; rax2 -b 10 0x46\n"
" -B str -> bin ; rax2 -B hello\n"
" -c output in C string ; rax2 -c 0x1234 # \\x34\\x12\\x00\\x00\n"
" -d force integer ; rax2 -d 3 -> 3 instead of 0x3\n"
" -e swap endianness ; rax2 -e 0x33\n"
" -D base64 decode ; rax2 -D \"aGVsbG8=\"\n"
" -E base64 encode ; rax2 -E \"hello\"\n"
" -f floating point ; rax2 -f 6.3+2.1\n"
" -F stdin slurp code hex ; rax2 -F < shellcode.[c/py/js]\n"
" -h help ; rax2 -h\n"
" -i dump as C byte array ; rax2 -i < bytes\n"
" -I IP address <-> LONG ; rax2 -I 3530468537\n"
" -j json format output ; rax2 -j 0x1234 # same as r2 -c '?j 0x1234'\n"
" -k keep base ; rax2 -k 33+3 -> 36\n"
" -K randomart ; rax2 -K 0x34 1020304050\n"
" -l bin -> str ; rax2 -l 01000101 01110110\n"
" -L bin -> hex(bignum) ; rax2 -L 111111111 # 0x1ff\n"
" -n newline ; append newline to output (for -E/-D/-r/..)\n"
" -o octalstr -> raw ; rax2 -o \\162 \\62 # r2\n"
" -r r2 style output ; rax2 -r 0x1234 # same as r2 -c '? 0x1234'\n"
" -s hexstr -> raw ; rax2 -s 43 4a 50\n"
" -S raw -> hexstr ; rax2 -S < /bin/ls > ls.hex\n"
" -rS raw -> hex.r2 ; rax2 -rS < /bin/ls > ls.r2\n"
" -t tstamp -> str ; rax2 -t 1234567890\n"
" -u units ; rax2 -u 389289238 # 317.0M\n"
" -v version ; rax2 -v\n"
" -w signed word ; rax2 -w 16 0xffff\n"
" -x output in hexpairs ; rax2 -x 0x1234 # 34120000\n"
" -X hash string ; rax2 -X linux osx\n"
);
return true;
}
Expand All @@ -213,7 +219,7 @@ static bool invalid_length(RaxActions flags) {
return true;
}

static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int *fm, PJ **pj) {
static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, RaxMode *mode, PJ **pj) {
const char *errstr = NULL;
ut8 *buf;
char *p, out_mode = (flags->decimal)? 'I': '0';
Expand All @@ -225,19 +231,7 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int
if (flags->raw2hexstr) {
goto dotherax;
}
if (*str == '=') {
int force_mode = 0;
switch (atoi (str + 1)) {
case 0: force_mode = str[1]; break;
case 2: force_mode = 'B'; break;
case 3: force_mode = 'T'; break;
case 8: force_mode = 'O'; break;
case 10: force_mode = 'I'; break;
case 16: force_mode = '0'; break;
}
*fm = force_mode;
return true;
}

bool usedflags = false;
if (*str == '-') {
while (str[1] && str[1] != ' ') {
Expand Down Expand Up @@ -268,8 +262,9 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int
case 'o': flags->octal2raw = !flags->octal2raw; break;
case 'I': flags->ipaddr2num = !flags->ipaddr2num; break;
case 'j': flags->jsonbases = !flags->jsonbases; break;
case 'b': flags->forcebase = !flags->forcebase; break;
case 'v': return r_main_version_print ("rax2", 0);
case '\0': return !use_stdin (num, flags, fm, pj);
case '\0': return !use_stdin (num, flags, mode, pj);
default:
/* not as complete as for positive numbers */
out_mode = !flags->keepbase? '0': 'I';
Expand All @@ -279,7 +274,7 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int
} else if (r_str_endswith (str, "f")) {
out_mode = 'l';
}
return format_output (num, out_mode, str, *fm, *flags);
return format_output (num, out_mode, str, *mode, *flags);
}
help_usage ();
return help ();
Expand All @@ -288,7 +283,7 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int
}
usedflags = true;
if (last) {
return !use_stdin (num, flags, fm, pj);
return !use_stdin (num, flags, mode, pj);
}
return true;
}
Expand Down Expand Up @@ -380,7 +375,7 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int
free (s);
free (m);
return true;
}
}
if (flags->binarynum) { // -x
ut64 n = r_num_calc (num, str, &errstr);
if (errstr) {
Expand Down Expand Up @@ -730,6 +725,17 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int
}
return true;
}
if (flags->forcebase && mode->omode == 0) {
switch (atoi (str)) {
case 0: mode->omode = str[0]; break;
case 2: mode->omode = 'B'; break;
case 3: mode->omode = 'T'; break;
case 8: mode->omode = 'O'; break;
case 10: mode->omode = 'I'; break;
case 16: mode->omode = '0'; break;
}
return true;
}
// no flags passed

if (str[0] == '0' && (tolower ((ut8)str[1]) == 'x')) {
Expand Down Expand Up @@ -765,16 +771,16 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, int
}
while ((p = strchr (str, ' '))) {
*p = 0;
if (!format_output (num, out_mode, str, *fm, *flags)) {
if (!format_output (num, out_mode, str, *mode, *flags)) {
return false;
}
str = p + 1;
}
return *str? format_output (num, out_mode, str, *fm, *flags): true;
return *str? format_output (num, out_mode, str, *mode, *flags): true;
}

R_API int r_main_rax2(int argc, const char **argv) {
int i, fm = 0;
int i;
int rc = 0;
int len = 0;

Expand All @@ -784,12 +790,13 @@ R_API int r_main_rax2(int argc, const char **argv) {
} else {
RNum *num = r_num_new (NULL, NULL, NULL);
RaxActions flags = {0};
RaxMode mode = {0};
PJ *pj = NULL;
for (i = 1; i < argc; i++) {
char *argv_i = strdup (argv[i]);
if (argv_i) {
len = r_str_unescape (argv_i);
if (!rax (num, argv_i, len, i == argc - 1, &flags, &fm, &pj)) {
if (!rax (num, argv_i, len, i == argc - 1, &flags, &mode, &pj)) {
rc = 1;
}
free (argv_i);
Expand Down
28 changes: 13 additions & 15 deletions man/rax2.1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ interpretations.
.Bl -tag -width Fl
.It Fl a
Show ASCII table
.It Fl b
Force output mode (numeric base)
.Pp
f floating point
2 binary
3 ternary
8 octal
10 decimal
16 hexadecimal
.It Fl B
Convert from character string to binary (rax2 \-B hello)
.It Fl c
Expand Down Expand Up @@ -79,17 +88,6 @@ Display the result as 16bit signed integer value
Show hexpairs from integer value
.It Fl X
Convert a string into a hash
.Sh OUTPUT BASES
.Pp
Force output mode (numeric base)
.Pp
=f floating point
=2 binary
=3 ternary
=8 octal
=10 decimal
=16 hexadecimal
.Pp
.Sh USAGE
Available variable types are:
.Pp
Expand Down Expand Up @@ -134,12 +132,12 @@ Encode and decode binary file using base64:
$ cmp /bin/ls ls && echo $?
0
.Pp
Use -B/-b to convert between binary and hexadecimal:
Use -l/-B to convert between binary and string:
.Pp
$ rax2 -B 0x1123
$ rax2 -B hello
001100000111100000110001001100010011001000110011
$ rax2 -b 001100000111100000110001001100010011001000110011
0x1123
$ rax2 -l 0110100001100101011011000110110001101111
hello
.Pp
It is a very useful tool for scripting, so you can read floating point values, or get the integer offset of a jump or a stack delta when analyzing programs.
.Pp
Expand Down
8 changes: 4 additions & 4 deletions test/db/tools/rax2
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ EXPECT=<<EOF
EOF
RUN

NAME=rax2 =10 0x46
NAME=rax2 -b 10 0x46
FILE=-
CMDS=!rax2 =10 0x46
CMDS=!rax2 -b 10 0x46
EXPECT=<<EOF
70
EOF
RUN

NAME=rax2 =16 0x3+0x3
NAME=rax2 -b 16 0x3+0x3
FILE=-
CMDS=!rax2 =16 0x3+0x3
CMDS=!rax2 -b 16 0x3+0x3
EXPECT=<<EOF
0x6
EOF
Expand Down

0 comments on commit edf215e

Please sign in to comment.