Rizin
unix-like reverse engineering framework and cli tools
cmd_resize.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Anton Kochkov <anton.kochkov@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <string.h>
5 #include <rz_core.h>
6 
7 static int rebase_helper(RzCore *core, ut64 oldbase, ut64 newbase) {
8  rz_debug_bp_rebase(core->dbg, oldbase, newbase);
9  rz_bin_set_baddr(core->bin, newbase);
10  rz_flag_move(core->flags, oldbase, newbase);
12  // TODO: rz_analysis_move :??
13  // TODO: differentiate analysis by map ranges (associated with files or memory maps)
14  return 0;
15 }
16 
17 RZ_IPI RzCmdStatus rz_rebase_handler(RzCore *core, int argc, const char **argv) {
18  ut64 oldbase = rz_num_math(core->num, argv[1]);
19  // old base = addr
20  // new base = core->offset
21  return rebase_helper(core, oldbase, core->offset);
22 }
23 
25  if (!core->file) {
26  return RZ_CMD_STATUS_ERROR;
27  }
28  ut64 oldsize = (core->file) ? rz_io_fd_size(core->io, core->file->fd) : 0;
29  if (oldsize == -1) {
30  return RZ_CMD_STATUS_ERROR;
31  }
33  return RZ_CMD_STATUS_OK;
34 }
35 
37  if (!core->file) {
38  return RZ_CMD_STATUS_ERROR;
39  }
40  ut64 oldsize = (core->file) ? rz_io_fd_size(core->io, core->file->fd) : 0;
41  if (oldsize == -1) {
42  return RZ_CMD_STATUS_ERROR;
43  }
44  if (argc > 1) {
45  ut64 newsize = rz_num_math(core->num, argv[1]);
46  if (newsize == 0) {
47  RZ_LOG_ERROR("Invalid new file size");
48  return RZ_CMD_STATUS_ERROR;
49  }
50  rz_core_file_resize(core, newsize);
51  } else {
52  switch (state->mode) {
53  case RZ_OUTPUT_MODE_JSON: {
54  PJ *pj = state->d.pj;
55  pj_o(pj);
56  pj_kn(pj, "size", oldsize);
57  pj_end(pj);
58  break;
59  }
61  rz_cons_printf("%" PFMT64d "\n", oldsize);
62  break;
63  default:
65  break;
66  }
67  }
68  return RZ_CMD_STATUS_OK;
69 }
70 
71 RZ_IPI RzCmdStatus rz_resize_remove_handler(RzCore *core, int argc, const char **argv) {
72  st64 delta = (st64)rz_num_math(core->num, argv[1]);
73  return resize_helper(core, -delta);
74 }
75 
76 RZ_IPI RzCmdStatus rz_resize_insert_handler(RzCore *core, int argc, const char **argv) {
77  st64 delta = (st64)rz_num_math(core->num, argv[1]);
78  return resize_helper(core, delta);
79 }
80 
81 RZ_IPI RzCmdStatus rz_resize_human_handler(RzCore *core, int argc, const char **argv) {
82  if (!core->file) {
83  return RZ_CMD_STATUS_ERROR;
84  }
85  ut64 oldsize = (core->file) ? rz_io_fd_size(core->io, core->file->fd) : 0;
86  if (oldsize == -1) {
87  return RZ_CMD_STATUS_ERROR;
88  }
89  char humansz[8];
90  rz_num_units(humansz, sizeof(humansz), oldsize);
91  rz_cons_println(humansz);
92  return RZ_CMD_STATUS_OK;
93 }
#define RZ_IPI
Definition: analysis_wasm.c:11
RZ_API RzBinFile * rz_bin_cur(RzBin *bin)
Definition: bin.c:895
RZ_API void rz_bin_set_baddr(RzBin *bin, ut64 baddr)
Definition: bin.c:549
RZ_API bool rz_core_bin_apply_all_info(RzCore *r, RzBinFile *binfile)
Definition: cbin.c:311
RZ_API bool rz_core_file_resize(RzCore *core, ut64 newsize)
Definition: cfile.c:518
RZ_API bool rz_core_file_resize_delta(RzCore *core, st64 delta)
Definition: cfile.c:522
RZ_IPI RzCmdStatus rz_resize_insert_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_resize.c:76
RZ_IPI RzCmdStatus rz_rebase_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_resize.c:17
RZ_IPI RzCmdStatus rz_resize_remove_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_resize.c:71
static int rebase_helper(RzCore *core, ut64 oldbase, ut64 newbase)
Definition: cmd_resize.c:7
RZ_IPI RzCmdStatus rz_resize_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state)
Definition: cmd_resize.c:36
RZ_IPI RzCmdStatus rz_resize_human_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_resize.c:81
static RzCmdStatus resize_helper(RzCore *core, st64 delta)
Definition: cmd_resize.c:24
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
RZ_API bool rz_flag_move(RzFlag *f, ut64 at, ut64 to)
Definition: flag.c:741
RZ_API void rz_debug_bp_rebase(RzDebug *dbg, ut64 old_base, ut64 new_base)
Definition: debug.c:1737
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define rz_warn_if_reached()
Definition: rz_assert.h:29
enum rz_cmd_status_t RzCmdStatus
@ RZ_CMD_STATUS_OK
command handler exited in the right way
Definition: rz_cmd.h:24
@ RZ_CMD_STATUS_ERROR
command handler had issues while running (e.g. allocation error, etc.)
Definition: rz_cmd.h:26
RZ_API ut64 rz_io_fd_size(RzIO *io, int fd)
Definition: io_fd.c:42
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
RZ_API char * rz_num_units(char *buf, size_t len, ut64 number)
Definition: unum.c:108
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_kn(PJ *j, const char *k, ut64 n)
Definition: pj.c:121
#define PFMT64d
Definition: rz_types.h:394
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
#define st64
Definition: rz_types_base.h:10
Definition: rz_pj.h:12
Represent the output state of a command handler.
Definition: rz_cmd.h:91
RzBin * bin
Definition: rz_core.h:298
ut64 offset
Definition: rz_core.h:301
RzDebug * dbg
Definition: rz_core.h:329
RzIO * io
Definition: rz_core.h:313
RzNum * num
Definition: rz_core.h:316
RzFlag * flags
Definition: rz_core.h:330
RzCoreFile * file
Definition: rz_core.h:314
Definition: dis.h:43
static st64 delta
Definition: vmenus.c:2425
ut64(WINAPI *w32_GetEnabledXStateFeatures)()