Skip to content

Commit

Permalink
Minor cleanup in cmd_print
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed Feb 16, 2022
1 parent 33ce7e7 commit 8cba363
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
10 changes: 5 additions & 5 deletions libr/core/casm.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,6 @@ static int is_hit_inrange(RCoreAsmHit *hit, ut64 start_range, ut64 end_range){
R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
RAsmOp op;
// if (n > core->blocksize) n = core->blocksize;
ut64 at;
ut32 idx = 0, hit_count;
int numinstr, asmlen, ii;
const int addrbytes = core->io->addrbytes;
Expand All @@ -494,8 +493,7 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
if (!hits) {
return NULL;
}

len = R_MIN (len - len % addrbytes, addrbytes * addr);
len = R_MIN (len - (len % addrbytes), addrbytes * addr);
if (len < 1) {
r_list_free (hits);
return NULL;
Expand All @@ -509,7 +507,7 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
free (buf);
return NULL;
}
if (!r_io_read_at (core->io, addr - len / addrbytes, buf, len)) {
if (!r_io_read_at (core->io, addr - (len / addrbytes), buf, len)) {
r_list_free (hits);
free (buf);
return NULL;
Expand All @@ -536,7 +534,9 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
break;
}
}
at = addr - idx / addrbytes;

ut64 at = addr - idx / addrbytes;

r_asm_set_pc (core->rasm, at);
for (hit_count = 0; hit_count < n; hit_count++) {
int instrlen = r_asm_disassemble (core->rasm, &op,
Expand Down
41 changes: 20 additions & 21 deletions libr/core/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -4934,6 +4934,7 @@ static ut8 *decode_text(RCore *core, ut64 offset, size_t len, bool zeroend) {
}

static bool cmd_pi(RCore *core, const char *input, int len, int l, ut8 *block) {
// len is block_len
char ch = input[1];
if (ch == '+' || ch == '-') {
ch = ' ';
Expand Down Expand Up @@ -5618,12 +5619,9 @@ static int cmd_print(void *data, const char *input) {
return 0;
}

const char *sp = NULL;
if (input[1] == '.' || input[1] == '+') {
sp = input + 2;
} else {
sp = strchr (input + 1, ' ');
}
const char *sp = (input[1] == '.' || input[1] == '+')
? input + 2: strchr (input + 1, ' ');

if (!sp && input[1] == '-') {
sp = input + 1;
}
Expand All @@ -5648,12 +5646,15 @@ static int cmd_print(void *data, const char *input) {
l = use_blocksize;
}
// may be unnecessary, fixes 'pd 1;pdj 100;pd 1' bug
#if 1
core->offset = at; // "pd" doesnt know about the current offset for pd -X
r_core_block_read (core);
#endif

switch (input[1]) {
case 'C': // "pdC"
r_core_disasm_pdi (core, l, 0, 'C');
pd_result = 0;
pd_result = false;
processed_cmd = true;
break;
case 'd': // "pdd" // r2dec
Expand All @@ -5670,13 +5671,13 @@ static int cmd_print(void *data, const char *input) {
break;
case 'c': // "pdc" // "pDc"
r_core_pseudo_code (core, input + 2);
pd_result = 0;
pd_result = false;
processed_cmd = true;
break;
case ',': // "pd,"
case 't': // "pdt"
r_core_disasm_table (core, l, r_str_trim_head_ro (input + 2));
pd_result = 0;
pd_result = false;
processed_cmd = true;
break;
case 'k': // "pdk" -print class
Expand All @@ -5692,7 +5693,7 @@ static int cmd_print(void *data, const char *input) {
} else {
r_core_disasm_pdi (core, l, 0, 0);
}
pd_result = 0;
pd_result = false;
break;
case 'a': // "pda"
processed_cmd = true;
Expand Down Expand Up @@ -5785,7 +5786,7 @@ static int cmd_print(void *data, const char *input) {
input[2] == 'J', NULL, NULL);
}
free (block);
pd_result = 0;
pd_result = false;
}
} else {
eprintf ("Cannot find function at 0x%08"PFMT64x "\n", core->offset);
Expand Down Expand Up @@ -5862,7 +5863,7 @@ static int cmd_print(void *data, const char *input) {
pj_end (pj);
r_cons_printf ("%s\n", pj_string (pj));
pj_free (pj);
pd_result = 0;
pd_result = false;
r_config_set (core->config, "asm.bbmiddle", orig_bb_middle);
} else if (f) {
ut64 linearsz = r_anal_function_linear_size (f);
Expand All @@ -5880,7 +5881,7 @@ static int cmd_print(void *data, const char *input) {
// r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, f->_size > 0 ? f->_size: r_anal_function_realsize (f), f->addr);
}
}
pd_result = 0;
pd_result = false;
} else {
eprintf ("pdf: Cannot find function at 0x%08"PFMT64x "\n", core->offset);
processed_cmd = true;
Expand Down Expand Up @@ -5922,17 +5923,17 @@ static int cmd_print(void *data, const char *input) {
}
}
r_cons_break_pop ();
pd_result = 0;
pd_result = false;
}
break;
case 'j': // pdj
case 'j': // "pdj"
processed_cmd = true;
if (*input == 'D') {
cmd_pDj (core, input + 2);
} else {
cmd_pdj (core, input + 2, block);
}
pd_result = 0;
pd_result = false;
break;
case 'J': // pdJ
formatted_json = true;
Expand All @@ -5946,7 +5947,7 @@ static int cmd_print(void *data, const char *input) {
case '?': // "pd?"
processed_cmd = true;
r_core_cmd_help (core, help_msg_pd);
pd_result = 0;
pd_result = false;
case '.':
case '-':
case '+':
Expand Down Expand Up @@ -7378,10 +7379,8 @@ static int cmd_print(void *data, const char *input) {
break;
}
beach:
if (myblock) {
free (block);
}
if (tmpseek != UT64_MAX) {
free (block);
if (tmpseek != UT64_MAX && tmpseek != core->offset) {
r_core_seek (core, tmpseek, SEEK_SET);
r_core_block_read (core);
}
Expand Down
5 changes: 2 additions & 3 deletions libr/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6271,7 +6271,6 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte
eprintf ("Too many backward instructions\n");
return false;
}

if (r_core_prevop_addr (core, core->offset, nb_opcodes, &addr)) {
nbytes = old_offset - addr;
} else if (!r_core_asm_bwdis_len (core, &nbytes, &addr, nb_opcodes)) {
Expand All @@ -6291,10 +6290,10 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte
count = R_MIN (nb_bytes, nbytes);
if (count > 0) {
r_io_read_at (core->io, addr, buf, count);
r_io_read_at (core->io, addr+count, buf+count, nb_bytes-count);
r_io_read_at (core->io, addr + count, buf + count, nb_bytes-count);
} else {
if (nb_bytes > 0) {
memset (buf, 0xff, nb_bytes);
memset (buf, core->io->Oxff, nb_bytes);
}
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions libr/util/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,6 @@ R_API void r_print_hexii(RPrint *rp, ut64 addr, const ut8 *buf, int len, int ste
/* set screen_bounds to addr if the cursor is not visible on the screen anymore.
* Note: screen_bounds is set only the first time this happens. */
R_API void r_print_set_screenbounds(RPrint *p, ut64 addr) {
int r, rc;

r_return_if_fail (p);

if (!p->screen_bounds) {
Expand All @@ -805,6 +803,7 @@ R_API void r_print_set_screenbounds(RPrint *p, ut64 addr) {
}

if (p->screen_bounds == 1) {
int r, rc;
(void)p->consbind.get_size (&r);
(void)p->consbind.get_cursor (&rc);

Expand Down

0 comments on commit 8cba363

Please # to comment.