Rizin
unix-like reverse engineering framework and cli tools
cflag.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2020 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2007-2020 ret2libc <sirmy15@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_cmd.h>
6 #include <rz_flag.h>
7 #include <rz_util.h>
8 #include <rz_cons.h>
9 
10 struct print_flag_t {
12  PJ *pj;
13  bool in_range;
17  bool real;
18 };
19 
20 static bool print_flag_name(RzFlagItem *fi, void *user) {
21  rz_cons_printf("%s\n", fi->name);
22  return true;
23 }
24 
25 static bool print_flag_json(RzFlagItem *flag, void *user) {
26  struct print_flag_t *u = (struct print_flag_t *)user;
27  if (u->in_range && (flag->offset < u->range_from || flag->offset >= u->range_to)) {
28  return true;
29  }
30  pj_o(u->pj);
31  pj_ks(u->pj, "name", flag->name);
32  if (flag->name != flag->realname) {
33  pj_ks(u->pj, "realname", flag->realname);
34  }
35  pj_ki(u->pj, "size", flag->size);
36  if (flag->alias) {
37  pj_ks(u->pj, "alias", flag->alias);
38  } else {
39  pj_kn(u->pj, "offset", flag->offset);
40  }
41  if (flag->comment) {
42  pj_ks(u->pj, "comment", flag->comment);
43  }
44  pj_end(u->pj);
45  return true;
46 }
47 
48 static bool print_flag_rizin(RzFlagItem *flag, void *user) {
49  struct print_flag_t *u = (struct print_flag_t *)user;
50  char *comment_b64 = NULL, *tmp = NULL;
51  if (u->in_range && (flag->offset < u->range_from || flag->offset >= u->range_to)) {
52  return true;
53  }
54  if (!u->fs || flag->space != u->fs) {
55  u->fs = flag->space;
56  rz_cons_printf("fs %s\n", u->fs ? u->fs->name : "*");
57  }
58  if (RZ_STR_ISNOTEMPTY(flag->comment)) {
59  comment_b64 = rz_base64_encode_dyn((const ut8 *)flag->comment, strlen(flag->comment));
60  // prefix the armored string with "base64:"
61  if (comment_b64) {
62  tmp = rz_str_newf("base64:%s", comment_b64);
63  free(comment_b64);
64  comment_b64 = tmp;
65  }
66  }
67  if (flag->alias) {
68  rz_cons_printf("fa %s %s\n", flag->name, flag->alias);
69  if (comment_b64) {
70  rz_cons_printf("\"fC %s %s\"\n",
71  flag->name, rz_str_get(comment_b64));
72  }
73  } else {
74  rz_cons_printf("f %s %" PFMT64d " 0x%08" PFMT64x " %s\n",
75  flag->name, flag->size, flag->offset,
76  rz_str_get(comment_b64));
77  }
78 
79  free(comment_b64);
80  return true;
81 }
82 
83 static bool print_flag_orig_name(RzFlagItem *flag, void *user) {
84  struct print_flag_t *u = (struct print_flag_t *)user;
85  if (u->in_range && (flag->offset < u->range_from || flag->offset >= u->range_to)) {
86  return true;
87  }
88  if (flag->alias) {
89  const char *n = u->real ? flag->realname : flag->name;
90  rz_cons_printf("%s %" PFMT64d " %s\n", flag->alias, flag->size, n);
91  } else {
92  const char *n = u->real ? flag->realname : (u->f->realnames ? flag->realname : flag->name);
93  rz_cons_printf("0x%08" PFMT64x " %" PFMT64d " %s\n", flag->offset, flag->size, n);
94  }
95  return true;
96 }
97 
98 typedef struct {
100 } FlagTableData;
101 
102 static bool __tableItemCallback(RzFlagItem *flag, void *user) {
103  FlagTableData *ftd = user;
104  if (!RZ_STR_ISEMPTY(flag->name)) {
105  RzTable *t = ftd->t;
106  const char *spaceName = (flag->space && flag->space->name) ? flag->space->name : "";
107  rz_table_add_rowf(t, "Xdss", flag->offset, flag->size, spaceName, flag->name);
108  }
109  return true;
110 }
111 
112 static void flag_print(RzFlag *f, RzCmdStateOutput *state, ut64 range_from, ut64 range_to, bool in_range) {
114  switch (state->mode) {
116  rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_name, f);
117  break;
119  struct print_flag_t u = {
120  .f = f,
121  .in_range = in_range,
122  .range_from = range_from,
123  .range_to = range_to,
124  .real = false
125  };
126  rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_orig_name, &u);
127  break;
128  }
129  case RZ_OUTPUT_MODE_JSON: {
130  struct print_flag_t u = {
131  .f = f,
132  .pj = state->d.pj,
133  .in_range = in_range,
134  .range_from = range_from,
135  .range_to = range_to,
136  .real = false
137  };
138  pj_a(state->d.pj);
139  rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_json, &u);
140  pj_end(state->d.pj);
141  break;
142  }
143  case RZ_OUTPUT_MODE_RIZIN: {
144  struct print_flag_t u = {
145  .f = f,
146  .in_range = in_range,
147  .range_from = range_from,
148  .range_to = range_to,
149  .fs = NULL,
150  };
151  rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_rizin, &u);
152  break;
153  }
154  case RZ_OUTPUT_MODE_TABLE: {
155  FlagTableData ftd = { 0 };
156  ftd.t = state->d.t;
157  rz_cmd_state_output_set_columnsf(state, "Xdss", "addr", "size", "space", "name");
158 
159  RzSpace *curSpace = rz_flag_space_cur(f);
160  rz_flag_foreach_space(f, curSpace, __tableItemCallback, &ftd);
161  break;
162  }
163  default:
165  break;
166  }
167 }
168 
170  flag_print(f, state, UT64_MAX, UT64_MAX, false);
171 }
172 
174  struct print_flag_t u = {
175  .f = f,
176  .in_range = false,
177  .range_from = UT64_MAX,
178  .range_to = UT64_MAX,
179  .real = true
180  };
181  rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_orig_name, &u);
182 }
183 
186 }
#define RZ_IPI
Definition: analysis_wasm.c:11
static bool print_flag_name(RzFlagItem *fi, void *user)
Definition: cflag.c:20
static bool __tableItemCallback(RzFlagItem *flag, void *user)
Definition: cflag.c:102
RZ_IPI void rz_core_flag_range_print(RzFlag *f, RzCmdStateOutput *state, ut64 range_from, ut64 range_to)
Definition: cflag.c:184
static void flag_print(RzFlag *f, RzCmdStateOutput *state, ut64 range_from, ut64 range_to, bool in_range)
Definition: cflag.c:112
RZ_IPI void rz_core_flag_real_name_print(RzFlag *f, RzCmdStateOutput *state)
Definition: cflag.c:173
static bool print_flag_rizin(RzFlagItem *flag, void *user)
Definition: cflag.c:48
static bool print_flag_json(RzFlagItem *flag, void *user)
Definition: cflag.c:25
static bool print_flag_orig_name(RzFlagItem *flag, void *user)
Definition: cflag.c:83
RZ_IPI void rz_core_flag_print(RzFlag *f, RzCmdStateOutput *state)
Definition: cflag.c:169
RZ_API void rz_cmd_state_output_set_columnsf(RzCmdStateOutput *state, const char *fmt,...)
Specify the columns of the command output.
Definition: cmd_api.c:2589
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
#define NULL
Definition: cris-opc.c:27
RZ_API void rz_flag_foreach_space(RzFlag *f, const RzSpace *space, RzFlagItemCb cb, void *user)
Definition: flag.c:826
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
int n
Definition: mipsasm.c:19
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
RZ_API char * rz_base64_encode_dyn(const ut8 *bin, size_t sz)
Definition: ubase64.c:92
RZ_API PJ * pj_ki(PJ *j, const char *k, int d)
Definition: pj.c:149
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_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API PJ * pj_kn(PJ *j, const char *k, ut64 n)
Definition: pj.c:121
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
static const char * rz_str_get(const char *str)
Definition: rz_str.h:187
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API void rz_table_add_rowf(RzTable *t, const char *fmt,...)
Definition: table.c:316
#define PFMT64d
Definition: rz_types.h:394
@ RZ_OUTPUT_MODE_TABLE
Definition: rz_types.h:46
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_QUIET
Definition: rz_types.h:42
@ RZ_OUTPUT_MODE_RIZIN
Definition: rz_types.h:41
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
#define PFMT64x
Definition: rz_types.h:393
#define UT64_MAX
Definition: rz_types_base.h:86
#define f(i)
Definition: sha256.c:46
RzTable * t
Definition: cflag.c:99
Definition: rz_pj.h:12
bool real
Definition: cflag.c:17
PJ * pj
Definition: cflag.c:12
ut64 range_from
Definition: cflag.c:14
RzFlag * f
Definition: cflag.c:11
RzSpace * fs
Definition: cflag.c:16
ut64 range_to
Definition: cflag.c:15
bool in_range
Definition: cflag.c:13
Represent the output state of a command handler.
Definition: rz_cmd.h:91
RzSpace * space
Definition: rz_flag.h:40
char * alias
Definition: rz_flag.h:43
ut64 offset
Definition: rz_flag.h:38
char * comment
Definition: rz_flag.h:42
char * realname
Definition: rz_flag.h:36
char * name
Definition: rz_flag.h:35
bool realnames
Definition: rz_flag.h:49
char * name
Definition: rz_spaces.h:29
Definition: dis.h:43
ut64(WINAPI *w32_GetEnabledXStateFeatures)()