Skip to content

Commit

Permalink
Port /x search commands; Fix JSON printing
Browse files Browse the repository at this point in the history
  • Loading branch information
DMaroo committed Feb 9, 2022
1 parent 5a5bdec commit 4e52b84
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
59 changes: 58 additions & 1 deletion librz/core/cmd/cmd_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ RZ_IPI RzCmdStatus rz_cmd_search_string_handler(RzCore *core, int argc, const ch
goto beach;
}
param.pj = state->d.pj;
param.outmode = state->mode;
param.outmode = state->mode == RZ_OUTPUT_MODE_JSON ? RZ_MODE_JSON : RZ_MODE_PRINT;
if (param.outmode == RZ_MODE_JSON) {
pj_o(param.pj);
}
Expand Down Expand Up @@ -402,3 +402,60 @@ RZ_IPI RzCmdStatus rz_cmd_search_string_handler(RzCore *core, int argc, const ch
rz_free(param.search_itv);
return RZ_CMD_STATUS_OK;
}

// /x
RZ_IPI RzCmdStatus rz_cmd_search_hex_string_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
if (!check_if_search_possible(core)) {
return RZ_CMD_STATUS_ERROR;
}

UPDATE_LASTSEARCH(argv[1]);

struct search_parameters param;
if (!setup_params(core, &param)) {
goto beach;
}
param.pj = state->d.pj;
param.outmode = state->mode == RZ_OUTPUT_MODE_JSON ? RZ_MODE_JSON : RZ_MODE_PRINT;
if (param.outmode == RZ_MODE_JSON) {
pj_o(param.pj);
}

RzSearchKeyword *kw;
rz_search_reset(core->search, RZ_SEARCH_KEYWORD);
rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance"));
char *mask_sep = strchr(argv[1], ':');

if (mask_sep) {
char *search_string;
search_string = rz_str_ndup(argv[1], mask_sep - argv[1]);
kw = rz_search_keyword_new_hex(search_string, mask_sep + 1, NULL);
free(search_string);
} else {
kw = rz_search_keyword_new_hexmask(argv[1], NULL);
}

if (kw) {
rz_search_kw_add(core->search, kw);
rz_search_begin(core->search);
} else {
RZ_LOG_ERROR("No keyword\n");
return RZ_CMD_STATUS_ERROR;
}

rz_config_set_i(core->config, "search.kwidx", core->search->n_kws);
do_string_search(core, &param);

beach:
core->num->value = core->search->nhits;
core->in_search = false;
rz_flag_space_pop(core->flags);
if (param.outmode == RZ_MODE_JSON) {
pj_end(param.pj);
}
rz_list_free(param.boundaries);
rz_search_kw_reset(core->search);
rz_free(param.search_itv);

return RZ_CMD_STATUS_OK;
}
33 changes: 33 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static const RzCmdDescDetail system_to_cons_details[2];
static const RzCmdDescDetail hash_bang_details[2];
static const RzCmdDescDetail pointer_details[2];
static const RzCmdDescDetail interpret_macro_multiple_details[2];
static const RzCmdDescDetail cmd_search_hex_string_details[3];
static const RzCmdDescDetail analysis_reg_cond_details[4];
static const RzCmdDescDetail ar_details[2];
static const RzCmdDescDetail analysis_hint_set_arch_details[2];
Expand Down Expand Up @@ -70,6 +71,7 @@ static const RzCmdDescArg interpret_pipe_args[2];
static const RzCmdDescArg interpret_macro_args[4];
static const RzCmdDescArg interpret_macro_multiple_args[4];
static const RzCmdDescArg cmd_search_string_args[2];
static const RzCmdDescArg cmd_search_hex_string_args[2];
static const RzCmdDescArg remote_args[3];
static const RzCmdDescArg remote_send_args[3];
static const RzCmdDescArg remote_add_args[2];
Expand Down Expand Up @@ -917,6 +919,35 @@ static const RzCmdDescHelp cmd_search_string_help = {
.args = cmd_search_string_args,
};

static const RzCmdDescDetailEntry cmd_search_hex_string_Ignoring_space_bytes_detail_entries[] = {
{ .text = "Ignore specific bytes while searching by using '.'", .arg_str = NULL, .comment = "/x 1ee7..0f....d34d" },
{ 0 },
};

static const RzCmdDescDetailEntry cmd_search_hex_string_Making_space_bytes_detail_entries[] = {
{ .text = "Mask bytes while searching by using :", .arg_str = NULL, .comment = "/x ff43:ffd0" },
{ 0 },
};
static const RzCmdDescDetail cmd_search_hex_string_details[] = {
{ .name = "Ignoring bytes", .entries = cmd_search_hex_string_Ignoring_space_bytes_detail_entries },
{ .name = "Making bytes", .entries = cmd_search_hex_string_Making_space_bytes_detail_entries },
{ 0 },
};
static const RzCmdDescArg cmd_search_hex_string_args[] = {
{
.name = "hex",
.type = RZ_CMD_ARG_TYPE_STRING,
.flags = RZ_CMD_ARG_FLAG_LAST,

},
{ 0 },
};
static const RzCmdDescHelp cmd_search_hex_string_help = {
.summary = "Search for hexadecimal string <hex>",
.details = cmd_search_hex_string_details,
.args = cmd_search_hex_string_args,
};

static const RzCmdDescHelp R_help = {
.summary = "Connect with other instances of rizin",
};
Expand Down Expand Up @@ -11755,6 +11786,8 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {

RzCmdDesc *slash__cd = rz_cmd_desc_group_state_new(core->rcmd, root_cd, "/", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_cmd_search_string_handler, &cmd_search_string_help, &slash__help);
rz_warn_if_fail(slash__cd);
RzCmdDesc *cmd_search_hex_string_cd = rz_cmd_desc_argv_state_new(core->rcmd, slash__cd, "/x", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_cmd_search_hex_string_handler, &cmd_search_hex_string_help);
rz_warn_if_fail(cmd_search_hex_string_cd);

RzCmdDesc *R_cd = rz_cmd_desc_group_new(core->rcmd, root_cd, "R", rz_remote_handler, &remote_help, &R_help);
rz_warn_if_fail(R_cd);
Expand Down
1 change: 1 addition & 0 deletions librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ RZ_IPI RzCmdStatus rz_interpret_pipe_handler(RzCore *core, int argc, const char
RZ_IPI RzCmdStatus rz_interpret_macro_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_interpret_macro_multiple_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_search_string_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
RZ_IPI RzCmdStatus rz_cmd_search_hex_string_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
RZ_IPI RzCmdStatus rz_remote_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_remote_send_handler(RzCore *core, int argc, const char **argv);
RZ_IPI int rz_io_system_run_oldhandler(void *data, const char *input);
Expand Down
19 changes: 19 additions & 0 deletions librz/core/cmd_descs/cmd_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,22 @@ commands:
args:
- name: string
type: RZ_CMD_ARG_TYPE_STRING
- name: /x
summary: Search for hexadecimal string <hex>
cname: cmd_search_hex_string
type: RZ_CMD_DESC_TYPE_ARGV_STATE
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
args:
- name: hex
type: RZ_CMD_ARG_TYPE_STRING
details:
- name: Ignoring bytes
entries:
- text: "Ignore specific bytes while searching by using '.'"
comment: "/x 1ee7..0f....d34d"
- name: Making bytes
entries:
- text: "Mask bytes while searching by using :"
comment: "/x ff43:ffd0"

0 comments on commit 4e52b84

Please sign in to comment.