Skip to content

Commit

Permalink
Convert pm to rz-shell (#1686)
Browse files Browse the repository at this point in the history
* Add a test for `pmj` and remove an unwanted newline
  • Loading branch information
officialcjunior authored Sep 24, 2021
1 parent 618ed4e commit 2c4601f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
19 changes: 19 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ static const RzCmdDescArg plugins_io_print_args[2];
static const RzCmdDescArg cmd_print_gadget_add_args[6];
static const RzCmdDescArg cmd_print_gadget_move_args[6];
static const RzCmdDescArg cmd_print_msg_digest_args[2];
static const RzCmdDescArg cmd_print_magic_args[2];
static const RzCmdDescArg project_save_args[2];
static const RzCmdDescArg project_open_args[2];
static const RzCmdDescArg project_open_no_bin_io_args[2];
Expand Down Expand Up @@ -3419,6 +3420,21 @@ static const RzCmdDescHelp cmd_print_timestamp_ntfs_help = {
.args = cmd_print_timestamp_ntfs_args,
};

static const RzCmdDescArg cmd_print_magic_args[] = {
{
.name = "file/directory",
.type = RZ_CMD_ARG_TYPE_STRING,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_magic_help = {
.summary = "Print libmagic data",
.args = cmd_print_magic_args,
};

static const RzCmdDescHelp P_help = {
.summary = "Project management",
};
Expand Down Expand Up @@ -6382,6 +6398,9 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *cmd_print_timestamp_ntfs_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_timestamp_cd, "ptn", rz_cmd_print_timestamp_ntfs_handler, &cmd_print_timestamp_ntfs_help);
rz_warn_if_fail(cmd_print_timestamp_ntfs_cd);

RzCmdDesc *cmd_print_magic_cd = rz_cmd_desc_argv_modes_new(core->rcmd, cmd_print_cd, "pm", RZ_OUTPUT_MODE_JSON, rz_cmd_print_magic_handler, &cmd_print_magic_help);
rz_warn_if_fail(cmd_print_magic_cd);

RzCmdDesc *P_cd = rz_cmd_desc_group_new(core->rcmd, root_cd, "P", NULL, NULL, &P_help);
rz_warn_if_fail(P_cd);
RzCmdDesc *project_save_cd = rz_cmd_desc_argv_new(core->rcmd, P_cd, "Ps", rz_project_save_handler, &project_save_help);
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 @@ -262,6 +262,7 @@ RZ_IPI RzCmdStatus rz_cmd_print_timestamp_current_handler(RzCore *core, int argc
RZ_IPI RzCmdStatus rz_cmd_print_timestamp_dos_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_timestamp_hfs_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_timestamp_ntfs_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_cmd_print_magic_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode);
RZ_IPI int rz_cmd_print(void *data, const char *input);
RZ_IPI RzCmdStatus rz_project_save_handler(RzCore *core, int argc, const char **argv);
RZ_IPI RzCmdStatus rz_project_open_handler(RzCore *core, int argc, const char **argv);
Expand Down
9 changes: 9 additions & 0 deletions librz/core/cmd_descs/cmd_print.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ commands:
summary: Print NTFS time (64 bit `cfg.bigendian`, since January 1, 1601)
cname: cmd_print_timestamp_ntfs
args: []
- name: pm
summary: Print libmagic data
cname: cmd_print_magic
args:
- name: file/directory
type: RZ_CMD_ARG_TYPE_STRING
optional: true
modes:
- RZ_OUTPUT_MODE_JSON
3 changes: 0 additions & 3 deletions librz/core/cmd_magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ static void rz_core_magic(RzCore *core, const char *file, int v, PJ *pj) {
int hits = 0;
magicdepth = rz_config_get_i(core->config, "magic.depth"); // TODO: do not use global var here
rz_core_magic_at(core, file, addr, magicdepth, v, pj, &hits);
if (pj) {
rz_cons_newline();
}
if (addr != core->offset) {
rz_core_seek(core, addr, true);
}
Expand Down
13 changes: 13 additions & 0 deletions librz/core/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -3230,6 +3230,19 @@ RZ_IPI RzCmdStatus rz_cmd_print_msg_digest_algo_list_handler(RzCore *core, int a
return rz_core_hash_plugins_print(state);
}

RZ_IPI RzCmdStatus rz_cmd_print_magic_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) {
if (mode == RZ_OUTPUT_MODE_JSON) {
PJ *pj = pj_new();
rz_core_magic(core, argv[1], true, pj);
rz_cons_println(pj_string(pj));
pj_free(pj);
} else {
// XXX: need cmd_magic header for rz_core_magic
rz_core_magic(core, argv[1], true, NULL);
}
return RZ_CMD_STATUS_OK;
}

// XXX blocksize is missing
static void cmd_print_pv(RzCore *core, const char *input, bool useBytes) {
const char *stack[] = {
Expand Down
9 changes: 9 additions & 0 deletions test/db/cmd/cmd_print
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,15 @@ EXPECT=<<EOF
EOF
RUN

NAME=pmj elf/main
FILE=bins/elf/analysis/main
ARGS=-n
CMDS=pmj
EXPECT=<<EOF
{"offset":0,"depth":1,"info":"ELF 64-bit LSB executable, x86-64, version 1"}
EOF
RUN

NAME=pm mach0/fatmach0-3true
FILE=bins/mach0/fatmach0-3true
ARGS=-n
Expand Down

0 comments on commit 2c4601f

Please sign in to comment.