Skip to content

Commit e9bdcbe

Browse files
committed
Refactor r_print_byte and improve pxe to lower the rcons pressure
1 parent f7f5830 commit e9bdcbe

File tree

4 files changed

+112
-42
lines changed

4 files changed

+112
-42
lines changed

AGENTS.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ radare2 is a modular reverse engineering framework.
1313
## Formatting
1414

1515
Run `clang-format-radare2` on modified files before submitting.
16+
Run `sys/lint.sh` script to enforce some extra rules.
1617

1718
- Indent code with **tabs**, spaces for comments and no trailing spaces
1819
- Space before opening parenthesis: `if (a)`, `foo ()`, `sizeof (int)`
@@ -25,14 +26,6 @@ Run `clang-format-radare2` on modified files before submitting.
2526
- Use types from `<r_types.h>` (`ut8`, `ut16`, `ut32`, `ut64`) instead of `<stdint.h>`
2627
- Use `PFMT64` macros instead of `%lld` for portable formatting
2728

28-
The `sys/lint.sh` script enforces additional style rules including:
29-
- No `for (int ...)` declarations
30-
- No trailing whitespace or tabs
31-
- Proper spacing around keywords and parentheses
32-
- No `eprintf` with "Error:" prefix (use `R_LOG_ERROR` instead)
33-
- Strings passed to `R_LOG_` can't have newlines
34-
- No direct `free()` calls on expressions (assign to variable first)
35-
3629
## Coding Rules
3730

3831
### Memory Management

libr/core/cmd_print.inc.c

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6938,6 +6938,93 @@ repeat:;
69386938
r_core_seek (core, initial_addr, true);
69396939
}
69406940

6941+
static void cmd_pxe(RCore *core, const char *input, int len) {
6942+
if (input[1] == '?') {
6943+
r_core_cmd_help_contains (core, help_msg_px, "pxe");
6944+
return;
6945+
}
6946+
if (len < 1) {
6947+
return;
6948+
}
6949+
// Emoji lookup table: each entry is a 2-byte suffix for UTF-8 emoji (0xF0 0x9F + suffix)
6950+
// Maps byte value 0x00-0xFF to emoji characters in the range U+1F300-U+1F585
6951+
// Each emoji at index i is stored at positions i*2 and i*2+1
6952+
static const ut8 emoji[512] = {
6953+
0x8c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 0x8e,0x8e, 0x8e,0x8f, 0x8e,0x92,
6965+
0x8e,0x93, 0x8e,0xa0, 0x8e,0xa1, 0x8e,0xa2, 0x8e,0xa3, 0x8e,0xa4, 0x8e,0xa5, 0x8e,0xa6,
6966+
0x8e,0xa7, 0x8e,0xa8, 0x8e,0xa9, 0x8e,0xaa, 0x8e,0xab, 0x8e,0xac, 0x8e,0xad, 0x8e,0xae,
6967+
0x8e,0xaf, 0x8e,0xb0, 0x8e,0xb1, 0x8e,0xb2, 0x8e,0xb3, 0x8e,0xb4, 0x8e,0xb5, 0x8e,0xb7,
6968+
0x8e,0xb8, 0x8e,0xb9, 0x8e,0xba, 0x8e,0xbb, 0x8e,0xbd, 0x8e,0xbe, 0x8e,0xbf, 0x8f,0x80,
6969+
0x8f,0x81, 0x8f,0x82, 0x8f,0x83, 0x8f,0x84, 0x8f,0x86, 0x8f,0x87, 0x8f,0x88, 0x8f,0x89,
6970+
0x8f,0x8a, 0x90,0x80, 0x90,0x81, 0x90,0x82, 0x90,0x83, 0x90,0x84, 0x90,0x85, 0x90,0x86,
6971+
0x90,0x87, 0x90,0x88, 0x90,0x89, 0x90,0x8a, 0x90,0x8b, 0x90,0x8c, 0x90,0x8d, 0x90,0x8e,
6972+
0x90,0x8f, 0x90,0x90, 0x90,0x91, 0x90,0x92, 0x90,0x93, 0x90,0x94, 0x90,0x95, 0x90,0x96,
6973+
0x90,0x97, 0x90,0x98, 0x90,0x99, 0x90,0x9a, 0x90,0x9b, 0x90,0x9c, 0x90,0x9d, 0x90,0x9e,
6974+
0x90,0x9f, 0x90,0xa0, 0x90,0xa1, 0x90,0xa2, 0x90,0xa3, 0x90,0xa4, 0x90,0xa5, 0x90,0xa6,
6975+
0x90,0xa7, 0x90,0xa8, 0x90,0xa9, 0x90,0xaa, 0x90,0xab, 0x90,0xac, 0x90,0xad, 0x90,0xae,
6976+
0x90,0xaf, 0x90,0xb0, 0x90,0xb1, 0x90,0xb2, 0x90,0xb3, 0x90,0xb4, 0x90,0xb5, 0x90,0xb6,
6977+
0x90,0xb7, 0x90,0xb8, 0x90,0xb9, 0x90,0xba, 0x90,0xbb, 0x90,0xbc, 0x90,0xbd, 0x90,0xbe,
6978+
0x91,0x80, 0x91,0x82, 0x91,0x83, 0x91,0x84, 0x91,0x85, 0x91,0x86, 0x91,0x87, 0x91,0x88,
6979+
0x91,0x89, 0x91,0x8a, 0x91,0x8b, 0x91,0x8c, 0x91,0x8d, 0x91,0x8e, 0x91,0x8f, 0x91,0x90,
6980+
0x91,0x91, 0x91,0x92, 0x91,0x93, 0x91,0x94, 0x91,0x95, 0x91,0x96, 0x91,0x97, 0x91,0x98,
6981+
0x91,0x99, 0x91,0x9a, 0x91,0x9b, 0x91,0x9c, 0x91,0x9d, 0x91,0x9e, 0x91,0x9f, 0x91,0xa0,
6982+
0x91,0xa1, 0x91,0xa2, 0x91,0xa3, 0x91,0xa4, 0x91,0xa5, 0x91,0xa6, 0x91,0xa7, 0x91,0xa8,
6983+
0x91,0xa9, 0x91,0xaa, 0x91,0xae, 0x91,0xaf, 0x91,0xba, 0x91,0xbb, 0x91,0xbc, 0x91,0xbd,
6984+
0x91,0xbe, 0x91,0xbf, 0x92,0x80, 0x92,0x81, 0x92,0x82, 0x92,0x83, 0x92,0x84, 0x92,0x85
6985+
};
6986+
int cols = core->print->cols;
6987+
if (cols < 1) {
6988+
cols = 1;
6989+
}
6990+
ut8 *block = malloc (len);
6991+
if (!block) {
6992+
return;
6993+
}
6994+
ut64 at = core->addr;
6995+
if (!r_io_read_at (core->io, at, block, len)) {
6996+
R_LOG_ERROR ("Unable to read at 0x%08"PFMT64x, at);
6997+
free (block);
6998+
return;
6999+
}
7000+
RStrBuf *sb = r_strbuf_new ("");
7001+
char addrbuf[128];
7002+
char bytebuf[64];
7003+
int i, j;
7004+
for (i = 0; i < len; i += cols) {
7005+
r_print_addr_tostring (core->print, at + i, addrbuf, sizeof (addrbuf));
7006+
r_strbuf_append (sb, addrbuf);
7007+
for (j = i; j < i + cols; j++) {
7008+
if (j < len) {
7009+
const ut8 bj = block[j];
7010+
r_strbuf_appendf (sb, "\xf0\x9f%c%c ", emoji[bj * 2], emoji[bj * 2 + 1]);
7011+
} else {
7012+
r_strbuf_append (sb, " ");
7013+
}
7014+
}
7015+
r_strbuf_append (sb, " ");
7016+
for (j = i; j < len && j < i + cols; j++) {
7017+
r_print_byte_tostring (core->print, at + j, "%c", j, block[j], bytebuf, sizeof (bytebuf));
7018+
r_strbuf_append (sb, bytebuf);
7019+
}
7020+
r_strbuf_append (sb, "\n");
7021+
}
7022+
free (block);
7023+
char *s = r_strbuf_drain (sb);
7024+
r_cons_print (core->cons, s);
7025+
free (s);
7026+
}
7027+
69417028
static void p8fm(RCore *core, ut64 addr, int mode) {
69427029
if (mode == '?') {
69437030
r_core_cmd_help_contains (core, help_msg_p8, "p8fm");
@@ -9118,33 +9205,7 @@ static int cmd_print(void *data, const char *input) {
91189205
}
91199206
break;
91209207
case 'e': // "pxe" // emoji dump
9121-
if (l != 0) {
9122-
int j;
9123-
char emoji[] = {
9124-
'\x8c', '\x80', '\x8c', '\x82', '\x8c', '\x85', '\x8c', '\x88', '\x8c', '\x99', '\x8c', '\x9e', '\x8c', '\x9f', '\x8c', '\xa0', '\x8c', '\xb0', '\x8c', '\xb1', '\x8c', '\xb2', '\x8c', '\xb3', '\x8c', '\xb4', '\x8c', '\xb5', '\x8c', '\xb7', '\x8c', '\xb8', '\x8c', '\xb9', '\x8c', '\xba', '\x8c', '\xbb', '\x8c', '\xbc', '\x8c', '\xbd', '\x8c', '\xbe', '\x8c', '\xbf', '\x8d', '\x80', '\x8d', '\x81', '\x8d', '\x82', '\x8d', '\x83', '\x8d', '\x84', '\x8d', '\x85', '\x8d', '\x86', '\x8d', '\x87', '\x8d', '\x88', '\x8d', '\x89', '\x8d', '\x8a', '\x8d', '\x8b', '\x8d', '\x8c', '\x8d', '\x8d', '\x8d', '\x8e', '\x8d', '\x8f', '\x8d', '\x90', '\x8d', '\x91', '\x8d', '\x92', '\x8d', '\x93', '\x8d', '\x94', '\x8d', '\x95', '\x8d', '\x96', '\x8d', '\x97', '\x8d', '\x98', '\x8d', '\x9c', '\x8d', '\x9d', '\x8d', '\x9e', '\x8d', '\x9f', '\x8d', '\xa0', '\x8d', '\xa1', '\x8d', '\xa2', '\x8d', '\xa3', '\x8d', '\xa4', '\x8d', '\xa5', '\x8d', '\xa6', '\x8d', '\xa7', '\x8d', '\xa8', '\x8d', '\xa9', '\x8d', '\xaa', '\x8d', '\xab', '\x8d', '\xac', '\x8d', '\xad', '\x8d', '\xae', '\x8d', '\xaf', '\x8d', '\xb0', '\x8d', '\xb1', '\x8d', '\xb2', '\x8d', '\xb3', '\x8d', '\xb4', '\x8d', '\xb5', '\x8d', '\xb6', '\x8d', '\xb7', '\x8d', '\xb8', '\x8d', '\xb9', '\x8d', '\xba', '\x8d', '\xbb', '\x8d', '\xbc', '\x8e', '\x80', '\x8e', '\x81', '\x8e', '\x82', '\x8e', '\x83', '\x8e', '\x84', '\x8e', '\x85', '\x8e', '\x88', '\x8e', '\x89', '\x8e', '\x8a', '\x8e', '\x8b', '\x8e', '\x8c', '\x8e', '\x8d', '\x8e', '\x8e', '\x8e', '\x8f', '\x8e', '\x92', '\x8e', '\x93', '\x8e', '\xa0', '\x8e', '\xa1', '\x8e', '\xa2', '\x8e', '\xa3', '\x8e', '\xa4', '\x8e', '\xa5', '\x8e', '\xa6', '\x8e', '\xa7', '\x8e', '\xa8', '\x8e', '\xa9', '\x8e', '\xaa', '\x8e', '\xab', '\x8e', '\xac', '\x8e', '\xad', '\x8e', '\xae', '\x8e', '\xaf', '\x8e', '\xb0', '\x8e', '\xb1', '\x8e', '\xb2', '\x8e', '\xb3', '\x8e', '\xb4', '\x8e', '\xb5', '\x8e', '\xb7', '\x8e', '\xb8', '\x8e', '\xb9', '\x8e', '\xba', '\x8e', '\xbb', '\x8e', '\xbd', '\x8e', '\xbe', '\x8e', '\xbf', '\x8f', '\x80', '\x8f', '\x81', '\x8f', '\x82', '\x8f', '\x83', '\x8f', '\x84', '\x8f', '\x86', '\x8f', '\x87', '\x8f', '\x88', '\x8f', '\x89', '\x8f', '\x8a', '\x90', '\x80', '\x90', '\x81', '\x90', '\x82', '\x90', '\x83', '\x90', '\x84', '\x90', '\x85', '\x90', '\x86', '\x90', '\x87', '\x90', '\x88', '\x90', '\x89', '\x90', '\x8a', '\x90', '\x8b', '\x90', '\x8c', '\x90', '\x8d', '\x90', '\x8e', '\x90', '\x8f', '\x90', '\x90', '\x90', '\x91', '\x90', '\x92', '\x90', '\x93', '\x90', '\x94', '\x90', '\x95', '\x90', '\x96', '\x90', '\x97', '\x90', '\x98', '\x90', '\x99', '\x90', '\x9a', '\x90', '\x9b', '\x90', '\x9c', '\x90', '\x9d', '\x90', '\x9e', '\x90', '\x9f', '\x90', '\xa0', '\x90', '\xa1', '\x90', '\xa2', '\x90', '\xa3', '\x90', '\xa4', '\x90', '\xa5', '\x90', '\xa6', '\x90', '\xa7', '\x90', '\xa8', '\x90', '\xa9', '\x90', '\xaa', '\x90', '\xab', '\x90', '\xac', '\x90', '\xad', '\x90', '\xae', '\x90', '\xaf', '\x90', '\xb0', '\x90', '\xb1', '\x90', '\xb2', '\x90', '\xb3', '\x90', '\xb4', '\x90', '\xb5', '\x90', '\xb6', '\x90', '\xb7', '\x90', '\xb8', '\x90', '\xb9', '\x90', '\xba', '\x90', '\xbb', '\x90', '\xbc', '\x90', '\xbd', '\x90', '\xbe', '\x91', '\x80', '\x91', '\x82', '\x91', '\x83', '\x91', '\x84', '\x91', '\x85', '\x91', '\x86', '\x91', '\x87', '\x91', '\x88', '\x91', '\x89', '\x91', '\x8a', '\x91', '\x8b', '\x91', '\x8c', '\x91', '\x8d', '\x91', '\x8e', '\x91', '\x8f', '\x91', '\x90', '\x91', '\x91', '\x91', '\x92', '\x91', '\x93', '\x91', '\x94', '\x91', '\x95', '\x91', '\x96', '\x91', '\x97', '\x91', '\x98', '\x91', '\x99', '\x91', '\x9a', '\x91', '\x9b', '\x91', '\x9c', '\x91', '\x9d', '\x91', '\x9e', '\x91', '\x9f', '\x91', '\xa0', '\x91', '\xa1', '\x91', '\xa2', '\x91', '\xa3', '\x91', '\xa4', '\x91', '\xa5', '\x91', '\xa6', '\x91', '\xa7', '\x91', '\xa8', '\x91', '\xa9', '\x91', '\xaa', '\x91', '\xae', '\x91', '\xaf', '\x91', '\xba', '\x91', '\xbb', '\x91', '\xbc', '\x91', '\xbd', '\x91', '\xbe', '\x91', '\xbf', '\x92', '\x80', '\x92', '\x81', '\x92', '\x82', '\x92', '\x83', '\x92', '\x84', '\x92', '\x85'
9125-
};
9126-
int cols = core->print->cols;
9127-
if (cols < 1) {
9128-
cols = 1;
9129-
}
9130-
for (i = 0; i < len; i += cols) {
9131-
r_print_addr (core->print, core->addr + i);
9132-
for (j = i; j < i + cols; j += 1) {
9133-
ut8 *p = (ut8 *)core->block + j;
9134-
if (j < len) {
9135-
r_cons_printf (core->cons, "\xf0\x9f%c%c ", emoji[*p * 2], emoji[*p * 2 + 1]);
9136-
} else {
9137-
r_cons_print (core->cons, " ");
9138-
}
9139-
}
9140-
r_cons_print (core->cons, " ");
9141-
for (j = i; j < len && j < i + cols; j += 1) {
9142-
ut8 *p = (ut8 *)core->block + j;
9143-
r_print_byte (core->print, core->addr + j, "%c", j, *p);
9144-
}
9145-
r_cons_newline (core->cons);
9146-
}
9147-
}
9208+
cmd_pxe (core, input, l);
91489209
break;
91499210
case 'l': // "pxl"
91509211
len = core->print->cols * len;

libr/include/r_util/r_print.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ R_API void r_print_bytes(RPrint *p, const ut8* buf, int len, const char *fmt, co
197197
R_API void r_print_fill(RPrint *p, const ut8 *arr, int size, ut64 addr, int step);
198198
R_API void r_print_byte(RPrint *p, ut64 addr, const char *fmt, int idx, ut8 ch);
199199
R_API const char *r_print_byte_color(RPrint *p, ut64 addr, int ch);
200+
R_API int r_print_byte_tostring(RPrint *p, ut64 addr, const char *fmt, int idx, ut8 ch, char *buf, size_t buf_size);
200201
R_API void r_print_c(RPrint *p, const ut8 *str, int len);
201202
R_API void r_print_raw(RPrint *p, ut64 addr, const ut8* buf, int len, int offlines);
202203
R_API bool r_print_have_cursor(RPrint *p, int cur, int len);

libr/util/print.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,27 +570,41 @@ R_API const char *r_print_byte_color(RPrint *p, ut64 addr, int ch) {
570570
return NULL;
571571
}
572572

573-
R_API void r_print_byte(RPrint *p, ut64 addr, const char *fmt, int idx, ut8 ch) {
573+
574+
R_API int r_print_byte_tostring(RPrint *p, ut64 addr, const char *fmt, int idx, ut8 ch, char *buf, size_t buf_size) {
575+
R_RETURN_VAL_IF_FAIL (buf && buf_size > 0, 0);
574576
ut8 rch = ch;
575577
if (!IS_PRINTABLE (ch) && fmt[0] == '%' && fmt[1] == 'c') {
576578
rch = '.';
577579
}
578-
r_print_cursor (p, idx, 1, 1);
580+
int len = 0;
581+
bool have_cursor = p && r_print_have_cursor (p, idx, 1);
582+
if (have_cursor) {
583+
len += snprintf (buf + len, buf_size - len, "%s", R_CONS_INVERT (1, 1));
584+
}
579585
if (p && p->flags & R_PRINT_FLAGS_COLOR) {
580586
const char *bytecolor = r_print_byte_color (p, addr, ch);
581587
if (bytecolor) {
582-
r_print_printf (p, "%s", bytecolor);
588+
len += snprintf (buf + len, buf_size - len, "%s", bytecolor);
583589
}
584-
r_print_printf (p, fmt, rch);
590+
len += snprintf (buf + len, buf_size - len, fmt, rch);
585591
if (bytecolor) {
586-
r_print_printf (p, "%s", Color_RESET);
592+
len += snprintf (buf + len, buf_size - len, "%s", Color_RESET);
587593
}
588594
} else {
589-
r_print_printf (p, fmt, rch);
595+
len += snprintf (buf + len, buf_size - len, fmt, rch);
596+
}
597+
if (have_cursor) {
598+
len += snprintf (buf + len, buf_size - len, "%s", R_CONS_INVERT (0, 1));
590599
}
591-
r_print_cursor (p, idx, 1, 0);
600+
return len;
592601
}
593602

603+
R_API void r_print_byte(RPrint *p, ut64 addr, const char *fmt, int idx, ut8 ch) {
604+
char buf[64];
605+
r_print_byte_tostring (p, addr, fmt, idx, ch, buf, sizeof (buf));
606+
r_print_printf (p, "%s", buf);
607+
}
594608
R_API int r_print_string(RPrint *p, ut64 seek, const ut8 *buf, int len, int options) {
595609
RCons *cons = (p && p->consb.cons)? p->consb.cons: NULL;
596610
int i;
@@ -1647,6 +1661,7 @@ R_API void r_print_bytes(RPrint *p, const ut8 *buf, int len, const char *fmt, co
16471661
}
16481662
printf ("\n");
16491663
}
1664+
16501665
}
16511666

16521667
R_API void r_print_raw(RPrint *p, ut64 addr, const ut8 *buf, int len, int offlines) {

0 commit comments

Comments
 (0)