|
1 | | -/* radare2 - LGPL - Copyright 2007-2025 - pancake */ |
| 1 | +/* radare2 - LGPL - Copyright 2007-2026 - pancake */ |
2 | 2 |
|
3 | 3 | #include <r_util/r_print.h> |
4 | 4 | #include <r_util/r_str.h> |
@@ -383,84 +383,71 @@ R_API void r_print_cursor(RPrint *p, int cur, int len, int set) { |
383 | 383 | } |
384 | 384 | } |
385 | 385 |
|
386 | | -R_API void r_print_addr(RPrint *p, ut64 addr) { |
387 | | - char space[32] = { |
388 | | - 0 |
389 | | - }; |
390 | | - char padstr[32]; |
391 | | - const char *white = ""; |
392 | | -#define PREOFF(x) (p && p->consb.cons && p->consb.cons->context && p->consb.cons->context->pal.x)? p->consb.cons->context->pal.x |
393 | | - bool use_segoff = p? (p->flags & R_PRINT_FLAGS_SEGOFF): false; |
394 | | - bool use_color = p? (p->flags & R_PRINT_FLAGS_COLOR): false; |
395 | | - bool dec = p? (p->flags & R_PRINT_FLAGS_ADDRDEC): false; |
396 | | - bool mod = p? (p->flags & R_PRINT_FLAGS_ADDRMOD): false; |
397 | | - char ch = p? ((p->addrmod && mod)? ((addr % p->addrmod)? ' ': ','): ' '): ' '; |
398 | | - if (p && p->flags & R_PRINT_FLAGS_COMPACT && p->col == 1) { |
| 386 | +R_API int r_print_addr_tostring(RPrint *p, ut64 addr, char *buf, size_t buf_size) { |
| 387 | + R_RETURN_VAL_IF_FAIL (buf && buf_size > 0, 0); |
| 388 | + bool use_segoff = p ? (p->flags & R_PRINT_FLAGS_SEGOFF) : false; |
| 389 | + bool use_color = p ? (p->flags & R_PRINT_FLAGS_COLOR) : false; |
| 390 | + bool dec = p ? (p->flags & R_PRINT_FLAGS_ADDRDEC) : false; |
| 391 | + bool mod = p ? (p->flags & R_PRINT_FLAGS_ADDRMOD) : false; |
| 392 | + char ch = p ? ((p->addrmod && mod) ? ((addr % p->addrmod) ? ' ' : ',') : ' ') : ' '; |
| 393 | + if (p && (p->flags & R_PRINT_FLAGS_COMPACT) && p->col == 1) { |
399 | 394 | ch = '|'; |
400 | 395 | } |
401 | 396 | if (p && p->pava) { |
402 | 397 | p->iob.p2v (p->iob.io, addr, &addr); |
403 | 398 | } |
| 399 | + const char *pre = Color_GREEN; |
| 400 | + const char *fin = Color_RESET; // AITODO: confirm this is used only in `use_color` code paths, because maybe we can just inline the value or simplify this code a little bit more |
| 401 | + if (use_color && p) { |
| 402 | + RCons *cons = p->consb.cons; |
| 403 | + if (cons) { |
| 404 | + if (p->flags & R_PRINT_FLAGS_RAINBOW) { |
| 405 | + if (cons->rgbstr) { |
| 406 | + static R_TH_LOCAL char rgbstr[32]; |
| 407 | + pre = cons->rgbstr (cons, rgbstr, sizeof (rgbstr), addr); |
| 408 | + } |
| 409 | + } else if (cons->context && cons->context->pal.addr) { |
| 410 | + pre = cons->context->pal.addr; |
| 411 | + } |
| 412 | + } |
| 413 | + } |
404 | 414 | if (use_segoff) { |
405 | | - ut32 a = addr & 0xffff; |
406 | | - ut32 s = (addr - a) >> ((p && p->config)? p->config->seggrn: 4); |
| 415 | + const ut32 a = addr & 0xffff; |
| 416 | + const ut32 s = (addr - a) >> ((p && p->config) ? p->config->seggrn : 4); |
407 | 417 | if (dec) { |
408 | | - snprintf (space, sizeof (space), "%d:%d", s & 0xffff, a & 0xffff); |
409 | | - white = r_str_pad (padstr, sizeof (padstr), ' ', 9 - strlen (space)); |
410 | | - } |
411 | | - if (use_color) { |
412 | | - const char *pre = PREOFF (addr): Color_GREEN; |
413 | | - const char *fin = Color_RESET; |
414 | | - if (dec) { |
415 | | - r_print_printf (p, "%s%s%s%s%c", pre, white, space, fin, ch); |
416 | | - } else { |
417 | | - r_print_printf (p, "%s%04x:%04x%s%c", pre, s & 0xffff, a & 0xffff, fin, ch); |
418 | | - } |
419 | | - } else { |
420 | | - if (dec) { |
421 | | - r_print_printf (p, "%s%s%c", white, space, ch); |
422 | | - } else { |
423 | | - r_print_printf (p, "%04x:%04x%c", s & 0xffff, a & 0xffff, ch); |
| 418 | + if (use_color) { |
| 419 | + return snprintf (buf, buf_size, "%s%9d:%-5d%s%c", pre, s & 0xffff, a & 0xffff, fin, ch); |
424 | 420 | } |
| 421 | + return snprintf (buf, buf_size, "%9d:%-5d%c", s & 0xffff, a & 0xffff, ch); |
425 | 422 | } |
426 | | - } else { |
427 | | - if (dec) { |
428 | | - snprintf (space, sizeof (space), "%" PFMT64d, addr); |
429 | | - int w = R_MAX (10 - strlen (space), 0); |
430 | | - white = r_str_pad (padstr, sizeof (padstr), ' ', w); |
| 423 | + if (use_color) { |
| 424 | + return snprintf (buf, buf_size, "%s%04x:%04x%s%c", pre, s & 0xffff, a & 0xffff, fin, ch); |
431 | 425 | } |
| 426 | + return snprintf (buf, buf_size, "%04x:%04x%c", s & 0xffff, a & 0xffff, ch); |
| 427 | + } |
| 428 | + if (dec) { |
432 | 429 | if (use_color) { |
433 | | - const char *pre = PREOFF (addr): Color_GREEN; |
434 | | - const char *fin = Color_RESET; |
435 | | - if (p && p->flags & R_PRINT_FLAGS_RAINBOW) { |
436 | | - if (p->consb.cons && p->consb.cons->rgbstr) { |
437 | | - static R_TH_LOCAL char rgbstr[32]; |
438 | | - pre = p->consb.cons->rgbstr (p->consb.cons, rgbstr, sizeof (rgbstr), addr); |
439 | | - } |
440 | | - } |
441 | | - if (dec) { |
442 | | - r_print_printf (p, "%s%s%" PFMT64d "%s%c", pre, white, addr, fin, ch); |
443 | | - } else { |
444 | | - if (p && p->wide_offsets) { |
445 | | - // TODO: make %016 depend on asm.bits |
446 | | - r_print_printf (p, "%s0x%016" PFMT64x "%s%c", pre, addr, fin, ch); |
447 | | - } else { |
448 | | - r_print_printf (p, "%s0x%08" PFMT64x "%s%c", pre, addr, fin, ch); |
449 | | - } |
450 | | - } |
451 | | - } else { |
452 | | - if (dec) { |
453 | | - r_print_printf (p, "%s%" PFMT64d "%c", white, addr, ch); |
454 | | - } else { |
455 | | - if (p && p->wide_offsets) { |
456 | | - // TODO: make %016 depend on asm.bits |
457 | | - r_print_printf (p, "0x%016" PFMT64x "%c", addr, ch); |
458 | | - } else { |
459 | | - r_print_printf (p, "0x%08" PFMT64x "%c", addr, ch); |
460 | | - } |
461 | | - } |
| 430 | + return snprintf (buf, buf_size, "%s%10" PFMT64d "%s%c", pre, addr, fin, ch); |
| 431 | + } |
| 432 | + return snprintf (buf, buf_size, "%10" PFMT64d "%c", addr, ch); |
| 433 | + } |
| 434 | + if (use_color) { |
| 435 | + if (p && p->wide_offsets) { |
| 436 | + return snprintf (buf, buf_size, "%s0x%016" PFMT64x "%s%c", pre, addr, fin, ch); |
462 | 437 | } |
| 438 | + return snprintf (buf, buf_size, "%s0x%08" PFMT64x "%s%c", pre, addr, fin, ch); |
463 | 439 | } |
| 440 | + if (p && p->wide_offsets) { |
| 441 | + return snprintf (buf, buf_size, "0x%016" PFMT64x "%c", addr, ch); |
| 442 | + } |
| 443 | + return snprintf (buf, buf_size, "0x%08" PFMT64x "%c", addr, ch); |
| 444 | +} |
| 445 | + |
| 446 | +// TODO: deprecate this function. r_print functions must not use RCons! just work with strbuf |
| 447 | +R_API void r_print_addr(RPrint *p, ut64 addr) { |
| 448 | + char buf[64]; |
| 449 | + r_print_addr_tostring (p, addr, buf, sizeof (buf)); |
| 450 | + r_print_printf (p, "%s", buf); |
464 | 451 | } |
465 | 452 |
|
466 | 453 | R_API char* r_print_hexpair(RPrint *p, const char *str, int n) { |
|
0 commit comments