diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 5f9cde160f..a2146708dd 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -1559,7 +1559,7 @@ static int cmd_anal_fcn(RCore *core, const char *input) { r_cons_println (fcn->cc); break; default: - eprintf ("See afC?\n"); + r_cons_println ("See afC?"); } }break; case 'B': // "afB" // set function bits @@ -4917,7 +4917,7 @@ static void cmd_anal_trace(RCore *core, const char *input) { break; case '?': r_core_cmd_help (core, help_msg); - eprintf ("Current Tag: %d\n", core->dbg->trace->tag); + r_cons_printf ("Current Tag: %d", core->dbg->trace->tag); break; case 'a': eprintf ("NOTE: Ensure given addresses are in 0x%%08" PFMT64x " format\n"); @@ -5322,7 +5322,7 @@ static int cmd_anal_all(RCore *core, const char *input) { case '\0': // "aa" case 'a': if (input[0] && (input[1] == '?' || (input[1] && input[2] == '?'))) { - eprintf ("Usage: See aa? for more help\n"); + r_cons_println ("Usage: See aa? for more help"); } else { ut64 curseek = core->offset; rowlog (core, "Analyze all flags starting with sym. and entry0 (aa)"); @@ -5584,7 +5584,7 @@ static int cmd_anal(void *data, const char *input) { "ab", " [hexpairs]", "analyze bytes", "abb", " [len]", "analyze N basic blocks in [len] (section.size by default)", "aa", "[?]", "analyze all (fcns + bbs) (aa0 to avoid sub renaming)", - "ac", "[?] [cycles]", "analyze which op could be executed in [cycles]", + "ac", " [cycles]", "analyze which op could be executed in [cycles]", "ad", "[?]", "analyze data trampoline (wip)", "ad", " [from] [to]", "analyze data pointers to (from-to)", "ae", "[?] [expr]", "analyze opcode eval expression (see ao)", diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index eb990f5da1..23805ed85a 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -2967,9 +2967,9 @@ static void r_core_debug_kill (RCore *core, const char *input) { NULL }; r_core_cmd_help (core, help_msg); - eprintf ("NOTE: [signal] can be a number or a string that resolves with dk?\n" + r_cons_println ("NOTE: [signal] can be a number or a string that resolves with dk?\n" " skip means do not enter into the signal handler\n" - " continue means enter into the signal handler\n"); + " continue means enter into the signal handler"); } } } else if (*input == 'j') { @@ -3355,8 +3355,8 @@ static int cmd_debug_step (RCore *core, const char *input) { step_until (core, r_num_math (core->num, input + 2)); // XXX dupped by times break; default: - eprintf ("Usage: dsu[fei] [arg] . step until address ' '," - " 'f'lag, 'e'sil or 'i'nstruction matching\n"); + r_cons_println ("Usage: dsu[fei] [arg] . step until address ' '," + " 'f'lag, 'e'sil or 'i'nstruction matching"); return 0; } break; @@ -3472,7 +3472,7 @@ static int cmd_debug(void *data, const char *input) { switch (input[1]) { case 'c': // "dtc" if (input[2] == '?') { - eprintf ("Usage: dtc [addr] ([from] [to] [addr]) - trace calls in debugger\n"); + r_cons_println ("Usage: dtc [addr] ([from] [to] [addr]) - trace calls in debugger"); } else { debug_trace_calls (core, input + 2); } diff --git a/libr/core/cmd_egg.c b/libr/core/cmd_egg.c index 0af66b7875..9ade9b81c0 100644 --- a/libr/core/cmd_egg.c +++ b/libr/core/cmd_egg.c @@ -160,7 +160,7 @@ static int cmd_egg(void *data, const char *input) { RListIter *iter; REggPlugin *p; r_list_foreach (egg->plugins, iter, p) { - printf ("%s %6s : %s\n", + r_cons_printf ("%s %6s : %s\n", (p->type == R_EGG_PLUGIN_SHELLCODE)? "shc": "enc", p->name, p->desc); } diff --git a/libr/core/cmd_flag.c b/libr/core/cmd_flag.c index 79f4fda1f6..6da0ca4d3d 100644 --- a/libr/core/cmd_flag.c +++ b/libr/core/cmd_flag.c @@ -7,13 +7,13 @@ static void cmd_fz(RCore *core, const char *input) { switch (*input) { case '?': - eprintf ("Usage: fz[?|-name| name] [@addr]\n"); - eprintf (" fz math add new flagzone named 'math'\n"); - eprintf (" fz-math remove the math flagzone\n"); - eprintf (" fz-* remove all flagzones\n"); - eprintf (" fz. show around flagzone context\n"); - eprintf (" fz: show what's in scr.flagzone for visual\n"); - eprintf (" fz* dump into r2 commands, for projects\n"); + r_cons_println ("Usage: fz[?|-name| name] [@addr]"); + r_cons_println (" fz math add new flagzone named 'math'"); + r_cons_println (" fz-math remove the math flagzone"); + r_cons_println (" fz-* remove all flagzones"); + r_cons_println (" fz. show around flagzone context"); + r_cons_println (" fz: show what's in scr.flagzone for visual"); + r_cons_println (" fz* dump into r2 commands, for projects"); break; case '.': { @@ -240,32 +240,39 @@ rep: r_flag_get_i2 (core->flags, r_num_math (core->num, input+1)); break; case 'R': // "fR" - { - if (*str == '\0'){ + switch(*str) { + case '\0': eprintf ("Usage: fR [from] [to] ([mask])\n"); eprintf ("Example to relocate PIE flags on debugger:\n" " > fR entry0 `dm~:1[1]`\n"); break; - } - char *p = strchr (str+1, ' '); - ut64 from, to, mask = 0xffff; - int ret; - if (p) { - char *q = strchr (p+1, ' '); - *p = 0; - if (q) { - *q = 0; - mask = r_num_math (core->num, q+1); + case '?': + r_cons_println ("Usage: fR [from] [to] ([mask])"); + r_cons_println ("Example to relocate PIE flags on debugger:\n" + " > fR entry0 `dm~:1[1]`"); + break; + default: + { + char *p = strchr (str+1, ' '); + ut64 from, to, mask = 0xffff; + int ret; + if (p) { + char *q = strchr (p+1, ' '); + *p = 0; + if (q) { + *q = 0; + mask = r_num_math (core->num, q+1); + } + from = r_num_math (core->num, str+1); + to = r_num_math (core->num, p+1); + ret = r_flag_relocate (core->flags, from, mask, to); + eprintf ("Relocated %d flags\n", ret); + } else { + eprintf ("Usage: fR [from] [to] ([mask])\n"); + eprintf ("Example to relocate PIE flags on debugger:\n" + " > fR entry0 `dm~:1[1]`\n"); + } } - from = r_num_math (core->num, str+1); - to = r_num_math (core->num, p+1); - ret = r_flag_relocate (core->flags, from, mask, to); - eprintf ("Relocated %d flags\n", ret); - } else { - eprintf ("Usage: fR [from] [to] ([mask])\n"); - eprintf ("Example to relocate PIE flags on debugger:\n" - " > fR entry0 `dm~:1[1]`\n"); - } } break; case 'b': // "fb" diff --git a/libr/core/cmd_meta.c b/libr/core/cmd_meta.c index 159cef384c..e5e8b510bc 100644 --- a/libr/core/cmd_meta.c +++ b/libr/core/cmd_meta.c @@ -427,7 +427,7 @@ static int cmd_meta_hsdmf(RCore *core, const char *input) { "to match the total struct size in mem.\n"); break; default: - eprintf ("See C?\n"); + r_cons_println ("See C?"); break; } case '-': diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index 49e6ddd054..68e9739b5b 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -2819,6 +2819,173 @@ static inline int cmd_pxb_k(const ut8 *buffer, int x) { return buffer[3 - x] << (8 * x); } +static const char* bits_to_c_code_fmtstr(int bits) { + switch (bits) { + case 16: + return "0x%04x"; + case 32: + return "0x%08xU"; + case 64: + return "0x%016" PFMT64x "ULL"; + default: + return "0x%02x"; + } +} + +static void print_c_code(RPrint *p, ut64 addr, ut8 *buf, int len, int ws, int w) { + const char *fmtstr; + int i, bits; + + ws = R_MAX (1, R_MIN (ws, 8)); + bits = ws * 8; + fmtstr = bits_to_c_code_fmtstr (bits); + len /= ws; + + p->cb_printf ("#define _BUFFER_SIZE %d\n", len); + p->cb_printf ("const uint%d_t buffer[%d] = {", bits, len); + + p->interrupt = 0; + + for (i = 0; !p->interrupt && i < len; i++) { + if (!(i % w)) { + p->cb_printf ("\n "); + } + r_print_cursor (p, i, 1); + p->cb_printf (fmtstr, r_read_ble (buf, p->big_endian, bits)); + if ((i + 1) < len) { + p->cb_printf (","); + + if ((i + 1) % w) { + p->cb_printf (" "); + } + } + r_print_cursor (p, i, 0); + buf += ws; + } + p->cb_printf ("\n};\n"); +} + +R_API void r_print_code(RPrint *p, ut64 addr, ut8 *buf, int len, char lang) { + int i, w = p->cols * 0.7; + if (w < 1) { + w = 1; + } + switch (lang) { + case '?': + r_cons_println ("Valid print code formats are: JSON, C, Python, Cstring (pcj, pc, pcp, pcs) \n" + " pc C\n" + " pc* print 'wx' r2 commands\n" + " pch C half-words (2 byte)\n" + " pcw C words (4 byte)\n" + " pcd C dwords (8 byte)\n" + " pca GAS .byte blob\n" + " pcA .bytes with instructions in comments\n" + " pcs string\n" + " pcS shellscript that reconstructs the bin\n" + " pcj json\n" + " pcJ javascript\n" + " pcp python"); + break; + case '*': + p->cb_printf ("wx "); + for (i = 0; !p->interrupt && i < len; i++) { + if (i && !(i % 16)) { + p->cb_printf (";s+16\nwx "); + } + p->cb_printf ("%02x", buf[i]); + } + if (i && !(i % 16)) { + p->cb_printf (";s+16\n"); + } else { + p->cb_printf (";s+%d\n", (i % 16)); + } + p->cb_printf ("s-%d\n", len); + break; + case 'A': // "pcA" + /* implemented in core because of disasm :( */ + break; + case 'a': // "pca" + p->cb_printf ("shellcode:"); + for (i = 0; !p->interrupt && i < len; i++) { + if (!(i % 8)) { + p->cb_printf ("\n.byte "); + } else { + p->cb_printf (", "); + } + p->cb_printf ("0x%02x", buf[i]); + } + p->cb_printf ("\n.equ shellcode_len, %d\n", len); + break; + case 's': // "pcs" + p->cb_printf ("\""); + for (i = 0; !p->interrupt && i < len; i++) { + p->cb_printf ("\\x%02x", buf[i]); + } + p->cb_printf ("\"\n"); + break; + case 'S': // "pcS" + { + const int trunksize = 16; + for (i = 0; !p->interrupt && i < len; i++) { + if (!(i % trunksize)) { + p->cb_printf ("printf \""); + } + p->cb_printf ("\\%03o", buf[i]); + if ((i % trunksize) == (trunksize - 1)) { + p->cb_printf ("\" %s bin\n", (i <= trunksize)? ">": ">>"); + } + } + if ((i % trunksize)) { + p->cb_printf ("\" %s bin\n", (i <= trunksize)? ">": ">>"); + } + } break; + case 'J': { + char *out = malloc (len * 3); + p->cb_printf ("var buffer = new Buffer(\""); + out[0] = 0; + r_base64_encode (out, buf, len); + p->cb_printf ("%s", out); + p->cb_printf ("\", 'base64');\n"); + free (out); + } break; + case 'j': + p->cb_printf ("["); + for (i = 0; !p->interrupt && i < len; i++) { + r_print_cursor (p, i, 1); + p->cb_printf ("%d%s", buf[i], (i + 1 < len)? ",": ""); + r_print_cursor (p, i, 0); + } + p->cb_printf ("]\n"); + break; + case 'P': + case 'p': // pcp" + p->cb_printf ("import struct\nbuf = struct.pack (\"%dB\", *[", len); + for (i = 0; !p->interrupt && i < len; i++) { + if (!(i % w)) { + p->cb_printf ("\n"); + } + r_print_cursor (p, i, 1); + p->cb_printf ("0x%02x%s", buf[i], (i + 1 < len)? ",": "])"); + r_print_cursor (p, i, 0); + } + p->cb_printf ("\n"); + break; + case 'h': + print_c_code (p, addr, buf, len, 2, p->cols / 2); // 9 + break; + case 'w': + print_c_code (p, addr, buf, len, 4, p->cols / 3); // 6); + break; + case 'd': + print_c_code (p, addr, buf, len, 8, p->cols / 5); //3); + break; + default: + print_c_code (p, addr, buf, len, 1, p->cols / 1.5); // 12); + break; + } +} + + static int cmd_print(void *data, const char *input) { int mode, w, p, i, l, len, total[10]; ut64 off, from, to, at, ate, piece; diff --git a/libr/util/print.c b/libr/util/print.c index ae5d88c753..b500224c7f 100644 --- a/libr/util/print.c +++ b/libr/util/print.c @@ -494,172 +494,6 @@ R_API void r_print_byte(RPrint *p, const char *fmt, int idx, ut8 ch) { r_print_cursor (p, idx, 0); } -static const char* bits_to_c_code_fmtstr(int bits) { - switch (bits) { - case 16: - return "0x%04x"; - case 32: - return "0x%08xU"; - case 64: - return "0x%016" PFMT64x "ULL"; - default: - return "0x%02x"; - } -} - -static void print_c_code(RPrint *p, ut64 addr, ut8 *buf, int len, int ws, int w) { - const char *fmtstr; - int i, bits; - - ws = R_MAX (1, R_MIN (ws, 8)); - bits = ws * 8; - fmtstr = bits_to_c_code_fmtstr (bits); - len /= ws; - - p->cb_printf ("#define _BUFFER_SIZE %d\n", len); - p->cb_printf ("const uint%d_t buffer[%d] = {", bits, len); - - p->interrupt = 0; - - for (i = 0; !p->interrupt && i < len; i++) { - if (!(i % w)) { - p->cb_printf ("\n "); - } - r_print_cursor (p, i, 1); - p->cb_printf (fmtstr, r_read_ble (buf, p->big_endian, bits)); - if ((i + 1) < len) { - p->cb_printf (","); - - if ((i + 1) % w) { - p->cb_printf (" "); - } - } - r_print_cursor (p, i, 0); - buf += ws; - } - p->cb_printf ("\n};\n"); -} - -R_API void r_print_code(RPrint *p, ut64 addr, ut8 *buf, int len, char lang) { - int i, w = p->cols * 0.7; - if (w < 1) { - w = 1; - } - switch (lang) { - case '?': - eprintf ("Valid print code formats are: JSON, C, Python, Cstring (pcj, pc, pcp, pcs) \n" - " pc C\n" - " pc* print 'wx' r2 commands\n" - " pch C half-words (2 byte)\n" - " pcw C words (4 byte)\n" - " pcd C dwords (8 byte)\n" - " pca GAS .byte blob\n" - " pcA .bytes with instructions in comments\n" - " pcs string\n" - " pcS shellscript that reconstructs the bin\n" - " pcj json\n" - " pcJ javascript\n" - " pcp python\n"); - break; - case '*': - p->cb_printf ("wx "); - for (i = 0; !p->interrupt && i < len; i++) { - if (i && !(i % 16)) { - p->cb_printf (";s+16\nwx "); - } - p->cb_printf ("%02x", buf[i]); - } - if (i && !(i % 16)) { - p->cb_printf (";s+16\n"); - } else { - p->cb_printf (";s+%d\n", (i % 16)); - } - p->cb_printf ("s-%d\n", len); - break; - case 'A': // "pcA" - /* implemented in core because of disasm :( */ - break; - case 'a': // "pca" - p->cb_printf ("shellcode:"); - for (i = 0; !p->interrupt && i < len; i++) { - if (!(i % 8)) { - p->cb_printf ("\n.byte "); - } else { - p->cb_printf (", "); - } - p->cb_printf ("0x%02x", buf[i]); - } - p->cb_printf ("\n.equ shellcode_len, %d\n", len); - break; - case 's': // "pcs" - p->cb_printf ("\""); - for (i = 0; !p->interrupt && i < len; i++) { - p->cb_printf ("\\x%02x", buf[i]); - } - p->cb_printf ("\"\n"); - break; - case 'S': // "pcS" - { - const int trunksize = 16; - for (i = 0; !p->interrupt && i < len; i++) { - if (!(i % trunksize)) { - p->cb_printf ("printf \""); - } - p->cb_printf ("\\%03o", buf[i]); - if ((i % trunksize) == (trunksize - 1)) { - p->cb_printf ("\" %s bin\n", (i <= trunksize)? ">": ">>"); - } - } - if ((i % trunksize)) { - p->cb_printf ("\" %s bin\n", (i <= trunksize)? ">": ">>"); - } - } break; - case 'J': { - char *out = malloc (len * 3); - p->cb_printf ("var buffer = new Buffer(\""); - out[0] = 0; - r_base64_encode (out, buf, len); - p->cb_printf ("%s", out); - p->cb_printf ("\", 'base64');\n"); - free (out); - } break; - case 'j': - p->cb_printf ("["); - for (i = 0; !p->interrupt && i < len; i++) { - r_print_cursor (p, i, 1); - p->cb_printf ("%d%s", buf[i], (i + 1 < len)? ",": ""); - r_print_cursor (p, i, 0); - } - p->cb_printf ("]\n"); - break; - case 'P': - case 'p': // pcp" - p->cb_printf ("import struct\nbuf = struct.pack (\"%dB\", *[", len); - for (i = 0; !p->interrupt && i < len; i++) { - if (!(i % w)) { - p->cb_printf ("\n"); - } - r_print_cursor (p, i, 1); - p->cb_printf ("0x%02x%s", buf[i], (i + 1 < len)? ",": "])"); - r_print_cursor (p, i, 0); - } - p->cb_printf ("\n"); - break; - case 'h': - print_c_code (p, addr, buf, len, 2, p->cols / 2); // 9 - break; - case 'w': - print_c_code (p, addr, buf, len, 4, p->cols / 3); // 6); - break; - case 'd': - print_c_code (p, addr, buf, len, 8, p->cols / 5); //3); - break; - default: - print_c_code (p, addr, buf, len, 1, p->cols / 1.5); // 12); - break; - } -} - R_API int r_print_string(RPrint *p, ut64 seek, const ut8 *buf, int len, int options) { int i; bool wide = (options & R_PRINT_STRING_WIDE);