Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert po commands to the rzshell #3146

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions librz/core/cio.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,9 +927,8 @@ RZ_API bool rz_core_write_string_zero_at(RzCore *core, ut64 addr, const char *s)
* \param buflen Used to return the length of the returned buffer
* \return The transformed buffer
*/
RZ_API RZ_OWN ut8 *rz_core_transform_op(RzCore *core, ut64 addr, RzCoreWriteOp op, ut8 *hex, int hexlen, int *buflen) {
RZ_API RZ_OWN ut8 *rz_core_transform_op(RzCore *core, ut64 addr, RzCoreWriteOp op, RZ_NULLABLE ut8 *hex, size_t hexlen, size_t *buflen) {
rz_return_val_if_fail(core, NULL);
rz_return_val_if_fail(!hex || hexlen >= 0, NULL);
rz_return_val_if_fail(buflen, NULL);

switch (op) {
Expand All @@ -942,7 +941,7 @@ RZ_API RZ_OWN ut8 *rz_core_transform_op(RzCore *core, ut64 addr, RzCoreWriteOp o
case RZ_CORE_WRITE_OP_XOR:
case RZ_CORE_WRITE_OP_SHIFT_LEFT:
case RZ_CORE_WRITE_OP_SHIFT_RIGHT:
rz_return_val_if_fail(hex && hexlen >= 0, NULL);
rz_return_val_if_fail(hex, NULL);
break;
default:
break;
Expand Down Expand Up @@ -1017,7 +1016,7 @@ RZ_API RZ_OWN ut8 *rz_core_transform_op(RzCore *core, ut64 addr, RzCoreWriteOp o
break;
}
}
*buflen = len;
*buflen = (size_t)len;
return buf;
}

Expand All @@ -1031,8 +1030,8 @@ RZ_API RZ_OWN ut8 *rz_core_transform_op(RzCore *core, ut64 addr, RzCoreWriteOp o
* \param hexlen Optional length of the \p hex string. Must be present if \p hex is specified.
* \return true if the write operation succeeds, false otherwise
*/
RZ_API bool rz_core_write_block_op_at(RzCore *core, ut64 addr, RzCoreWriteOp op, ut8 *hex, int hexlen) {
int buflen;
RZ_API bool rz_core_write_block_op_at(RzCore *core, ut64 addr, RzCoreWriteOp op, RZ_NULLABLE ut8 *hex, size_t hexlen) {
size_t buflen;
ut8 *buf = rz_core_transform_op(core, addr, op, hex, hexlen, &buflen);
if (!buf) {
return false;
Expand Down
167 changes: 56 additions & 111 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,23 +345,6 @@ static const char *help_msg_pif[] = {
"print instructions of function in JSON format",
};

static const char *help_msg_po[] = {
"Usage:", "po[24aAdlmorsx]", " [hexpairs] @ addr[!bsize]",
"po[24aAdlmorsx]", "", "without hexpair values, clipboard is used",
"po2", " [val]", "2= 2 byte endian swap",
"po4", " [val]", "4= 4 byte endian swap",
"poa", " [val]", "+= addition (f.ex: poa 0102)",
"poA", " [val]", "&= and",
"pod", " [val]", "/= divide",
"pol", " [val]", "<<= shift left",
"pom", " [val]", "*= multiply",
"poo", " [val]", "|= or",
"por", " [val]", ">>= shift right",
"pos", " [val]", "-= substraction",
"pox", " [val]", "^= xor (f.ex: pox 0x90)",
NULL
};

static const char *help_msg_ps[] = {
"Usage:", "ps[bijqpsuwWxz+] [N]", "Print String",
"ps", "", "print string",
Expand Down Expand Up @@ -2315,105 +2298,22 @@ static bool cmd_print_pxA(RzCore *core, int len, RzOutputMode mode) {
return true;
}

static ut8 *old_transform_op(RzCore *core, const char *val, char op, int *buflen) {
RzCoreWriteOp wop;
switch (op) {
case '2':
wop = RZ_CORE_WRITE_OP_BYTESWAP2;
break;
case '4':
wop = RZ_CORE_WRITE_OP_BYTESWAP4;
break;
case '8':
wop = RZ_CORE_WRITE_OP_BYTESWAP8;
break;
case 'a':
wop = RZ_CORE_WRITE_OP_ADD;
break;
case 'A':
wop = RZ_CORE_WRITE_OP_AND;
break;
case 'd':
wop = RZ_CORE_WRITE_OP_DIV;
break;
case 'l':
wop = RZ_CORE_WRITE_OP_SHIFT_LEFT;
break;
case 'm':
wop = RZ_CORE_WRITE_OP_MUL;
break;
case 'o':
wop = RZ_CORE_WRITE_OP_OR;
break;
case 'r':
wop = RZ_CORE_WRITE_OP_SHIFT_RIGHT;
break;
case 's':
wop = RZ_CORE_WRITE_OP_SUB;
break;
case 'x':
wop = RZ_CORE_WRITE_OP_XOR;
break;
default:
wop = RZ_CORE_WRITE_OP_XOR;
rz_warn_if_reached();
break;
}

/* Uses data from clipboard if value is NULL */
static bool print_operation_transform(RzCore *core, RzCoreWriteOp op, RZ_NULLABLE const char *val) {
ut8 *hex = NULL;
int hexlen = -1;
size_t hexlen = 0, buflen = 0;
if (val) {
val = rz_str_trim_head_ro(val);
hex = RZ_NEWS(ut8, (strlen(val) + 1) / 2);
if (!hex) {
return NULL;
return false;
}

hexlen = rz_hex_str2bin(val, hex);
}
ut8 *result = rz_core_transform_op(core, core->offset, wop, hex, hexlen, buflen);
ut8 *buf = rz_core_transform_op(core, core->offset, op, hex, hexlen, &buflen);
XVilka marked this conversation as resolved.
Show resolved Hide resolved
free(hex);
return result;
}

static void cmd_print_op(RzCore *core, const char *input) {
ut8 *buf;
int buflen = -1;

if (!input[0])
return;
switch (input[1]) {
case 'a':
case 's':
case 'A':
case 'x':
case 'r':
case 'l':
case 'm':
case 'd':
case 'o':
case '2':
case '4':
if (input[2]) { // parse val from arg
buf = old_transform_op(core, input + 3, input[1], &buflen);
} else { // use clipboard instead of val
buf = old_transform_op(core, NULL, input[1], &buflen);
}
break;
case 'n':
buf = old_transform_op(core, "ff", 'x', &buflen);
break;
case '\0':
case '?':
default:
rz_core_cmd_help(core, help_msg_po);
return;
}
if (buf) {
rz_core_print_hexdump(core, core->offset, buf,
buflen, 16, 1, 1);
free(buf);
}
rz_core_print_hexdump(core, core->offset, buf, buflen, 16, 1, 1);
free(buf);
return true;
}

static void printraw(RzCore *core, int len) {
Expand Down Expand Up @@ -4579,9 +4479,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) {
break;
}
break;
case 'o': // "po"
cmd_print_op(core, input);
break;
case 'x': // "px"
{
bool show_offset = rz_config_get_i(core->config, "hex.offset");
Expand Down Expand Up @@ -6573,3 +6470,51 @@ RZ_IPI RzCmdStatus rz_print_pattern_num_handler(RzCore *core, int argc, const ch
free(buf);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_print_operation_2swap_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_BYTESWAP2, NULL));
}

RZ_IPI RzCmdStatus rz_print_operation_4swap_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_BYTESWAP4, NULL));
}

RZ_IPI RzCmdStatus rz_print_operation_8swap_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_BYTESWAP8, NULL));
}

RZ_IPI RzCmdStatus rz_print_operation_add_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_ADD, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_and_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_AND, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_div_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_DIV, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_shl_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_SHIFT_LEFT, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_mul_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_MUL, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_or_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_OR, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_shr_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_SHIFT_RIGHT, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_sub_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_SUB, argv[1]));
}

RZ_IPI RzCmdStatus rz_print_operation_xor_handler(RzCore *core, int argc, const char **argv) {
return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_XOR, argv[1]));
}
Loading