Skip to content

Commit

Permalink
core/file: add API for the oC command (#4531)
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka authored Jun 4, 2024
1 parent 255f6fd commit 3ceafb5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 deletions.
43 changes: 43 additions & 0 deletions librz/core/cfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,49 @@ RZ_API RZ_BORROW RzCoreFile *rz_core_file_open(RZ_NONNULL RzCore *r, RZ_NONNULL
return fh;
}

/**
* Allocates the memory chunk of \p len size, copies the contents at the current offset, and opens it as a file.
* \param core RzCore instanced
* \param len Size of the chunk to be created and copied
* \param offset Offset from where to copy bytes
* \return true on success, false otherwise
*/
RZ_API bool rz_core_file_malloc_copy_chunk(RzCore *core, size_t len, ut64 offset) {
rz_return_val_if_fail(core && len, false);

bool res = false;
ut8 *data = RZ_NEWS(ut8, len);
if (!data) {
return false;
}
if (!rz_io_read_at(core->io, offset, data, len)) {
RZ_LOG_ERROR("Cannot read %zu bytes from offset 0x%" PFMT64x ".\n", len, offset);
goto err;
}

char uri[100];
rz_strf(uri, "malloc://%zu", len);
RzCoreFile *cfile = rz_core_file_open(core, uri, RZ_PERM_RWX, 0);
if (!cfile) {
RZ_LOG_ERROR("Cannot open '%s'.\n", uri);
goto err;
}

if (!rz_core_bin_load(core, uri, 0)) {
RZ_LOG_ERROR("Cannot load binary info of '%s'.\n", uri);
goto err;
}

RzIODesc *desc = rz_io_desc_get(core->io, cfile->fd);
rz_warn_if_fail(desc);
rz_io_desc_write_at(desc, 0, data, len);
res = true;

err:
free(data);
return res;
}

RZ_IPI void rz_core_file_io_desc_closed(RzCore *core, RzIODesc *desc) {
// remove all references to the closed desc
RzListIter *it;
Expand Down
33 changes: 1 addition & 32 deletions librz/core/cmd/cmd_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,38 +877,7 @@ RZ_IPI RzCmdStatus rz_open_malloc_handler(RzCore *core, int argc, const char **a
RZ_LOG_ERROR("Invalid length %d.\n", len);
return RZ_CMD_STATUS_ERROR;
}

RzCmdStatus res = RZ_CMD_STATUS_ERROR;
ut8 *data = RZ_NEWS(ut8, len);
if (!data) {
return RZ_CMD_STATUS_ERROR;
}
if (!rz_io_read_at(core->io, core->offset, data, len)) {
RZ_LOG_ERROR("Cannot read %d bytes from current offset.\n", len);
goto err;
}

char uri[100];
rz_strf(uri, "malloc://%d", len);
RzCoreFile *cfile = rz_core_file_open(core, uri, RZ_PERM_RWX, 0);
if (!cfile) {
RZ_LOG_ERROR("Cannot open '%s'.\n", uri);
goto err;
}

if (!rz_core_bin_load(core, uri, 0)) {
RZ_LOG_ERROR("Cannot load binary info of '%s'.\n", uri);
goto err;
}

RzIODesc *desc = rz_io_desc_get(core->io, cfile->fd);
rz_warn_if_fail(desc);
rz_io_desc_write_at(desc, 0, data, len);
res = RZ_CMD_STATUS_OK;

err:
free(data);
return res;
return bool2status(rz_core_file_malloc_copy_chunk(core, len, core->offset));
}

static RzCmdStatus open_nobin_file(RzCore *core, const char *uri, ut64 addr, int perms) {
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ RZ_API int rz_core_setup_debugger(RzCore *r, const char *debugbackend, bool atta
RZ_API bool rz_core_file_open_load(RZ_NONNULL RzCore *core, RZ_NONNULL const char *filepath, ut64 addr, int perms, bool write_mode);
RZ_API RZ_BORROW RzCoreFile *rz_core_file_open(RZ_NONNULL RzCore *core, RZ_NONNULL const char *file, int flags, ut64 loadaddr);
RZ_API RZ_BORROW RzCoreFile *rz_core_file_open_many(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *file, int perm, ut64 loadaddr);
RZ_API bool rz_core_file_malloc_copy_chunk(RzCore *core, size_t len, ut64 offset);
RZ_API RzCoreFile *rz_core_file_get_by_fd(RzCore *core, int fd);
RZ_API void rz_core_file_close(RzCoreFile *fh);
RZ_API bool rz_core_file_close_fd(RzCore *core, int fd);
Expand Down
5 changes: 3 additions & 2 deletions test/integration/test_analysis_il.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ static bool test_analysis_il() {
rz_core_file_open_load(core, "malloc://0x1000", 0x40000, RZ_PERM_R, false);
rz_core_file_open_load(core, "malloc://0x10", 0x50000, RZ_PERM_R, false);

rz_core_cmd_lines(core, "oC 0x10 @ obj.seckrit # New file mapping from 0x0-0xf\n");

ut64 obj_seckrit = rz_num_get(core->num, "obj.seckrit");
// New file mapping from 0x0-0xf
rz_core_file_malloc_copy_chunk(core, 0x10, obj_seckrit);

RzIOMap *map = rz_io_map_get(core->io, 0);
rz_io_map_remap(core->io, map->id, obj_seckrit);

Expand Down

0 comments on commit 3ceafb5

Please sign in to comment.