Skip to content

Commit

Permalink
Fix rax2 -l behaviour ##tools
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae authored Feb 8, 2024
1 parent f368c8c commit 1e0c6b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
23 changes: 16 additions & 7 deletions libr/main/rax2.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static int help(void) {
" -I IP address <-> LONG ; rax2 -I 3530468537\n"
" -k keep base ; rax2 -k 33+3 -> 36\n"
" -K randomart ; rax2 -K 0x34 1020304050\n"
" -l ; append newline to output (for -E/-D/-r/..\n"
" -l newline ; append newline to output (for -E/-D/-r/..\n"
" -L bin -> hex(bignum) ; rax2 -L 111111111 # 0x1ff\n"
" -n binary number ; rax2 -n 0x1234 # 34120000\n"
" -N binary number ; rax2 -N 0x1234 # \\x34\\x12\\x00\\x00\n"
Expand All @@ -182,7 +182,6 @@ static int help(void) {
static bool rax(RNum *num, char *str, int len, int last, ut64 *_flags, int *fm) {
const char *errstr = NULL;
ut64 flags = *_flags;
const char *nl = "";
ut8 *buf;
char *p, out_mode = (flags & 128)? 'I': '0';
int i;
Expand All @@ -209,7 +208,10 @@ static bool rax(RNum *num, char *str, int len, int last, ut64 *_flags, int *fm)
if (*str == '-') {
while (str[1] && str[1] != ' ') {
switch (str[1]) {
case 'l': nl = "\n"; break;
case 'l':
*_flags |= (1 << 24); // nl
flags = *_flags;
break;
case 'a': print_ascii_table (); return 0;
case 's': flags ^= 1 << 0; break;
case 'e': flags ^= 1 << 1; break;
Expand Down Expand Up @@ -283,7 +285,7 @@ static bool rax(RNum *num, char *str, int len, int last, ut64 *_flags, int *fm)
#if __EMSCRIPTEN__
puts ("");
#else
if (R_STR_ISNOTEMPTY (nl)) {
if (flags & (1 << 24)) {
puts ("");
}
#endif
Expand Down Expand Up @@ -476,12 +478,16 @@ static bool rax(RNum *num, char *str, int len, int last, ut64 *_flags, int *fm)
r_list_free (split);
return true;
} else if (flags & (1 << 12)) { // -E
// TODO: use the dynamic b64 encoder so we dont have to manually calloc here
/* http://stackoverflow.com/questions/4715415/base64-what-is-the-worst-possible-increase-in-space-usage */
char *out = calloc (1, (len + 2) / 3 * 4 + 1); // ceil(n/3)*4 plus 1 for NUL
if (out) {
r_base64_encode (out, (const ut8 *)str, len);
printf ("%s%s", out, nl);
fflush (stdout);
int olen = r_base64_encode (out, (const ut8 *)str, len);
if (olen > 0) {
const char *nl = (flags & (1 << 24))? "\n": "";
printf ("%s%s", out, nl);
fflush (stdout);
}
free (out);
}
return true;
Expand All @@ -492,6 +498,9 @@ static bool rax(RNum *num, char *str, int len, int last, ut64 *_flags, int *fm)
n = r_base64_decode (out, str, n);
if (n > 0) {
fwrite (out, n, 1, stdout);
if (flags & (1 << 24)) {
puts ("");
}
fflush (stdout);
} else {
R_LOG_ERROR ("Cannot decode");
Expand Down
8 changes: 5 additions & 3 deletions test/db/tools/rax2
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ RUN

NAME=rax2 -E | rax2 -D
FILE=-
CMDS=!rax2 -E hello |rax2 -D |rax2 -E | rax2 -D |rax2 -E|rax2 -D |rax2 -E | rax2 -D
CMDS=!rax2 -E hello | rax2 -D | rax2 -E | rax2 -D | rax2 -E | rax2 -D | rax2 -E | rax2 -D
EXPECT=hello
RUN

NAME=rax2 -E | rax2 -D 2
FILE=-
CMDS=!rax2 -E hello |rax2 -D |rax2 -E | rax2 -D |rax2 -E|rax2 -D |rax2 -E | rax2 -Dl
EXPECT=hello
CMDS=!rax2 -E hello | rax2 -D | rax2 -E | rax2 -D | rax2 -E | rax2 -D | rax2 -E | rax2 -lD
EXPECT=<<EOF
hello
EOF
RUN

NAME=rax2 -E | rax2 -D 3
Expand Down

0 comments on commit 1e0c6b2

Please sign in to comment.