Rizin
unix-like reverse engineering framework and cli tools
core_java.c File Reference
#include <rz_types.h>
#include <rz_lib.h>
#include <rz_demangler.h>
#include <rz_cmd.h>
#include <rz_core.h>
#include <rz_cons.h>
#include <string.h>
#include <rz_analysis.h>
#include "../format/java/class_bin.h"

Go to the source code of this file.

Macros

#define name_args(name)   (cmd_##name##_args)
 
#define name_help(name)   (cmd_##name##_help)
 
#define name_handler(name)   (rz_cmd_##name##_handler)
 
#define static_description_without_args(command, summ)
 
#define rz_cmd_desc_argv_modes_new_warn(rcmd, root, cmd, flags)    rz_warn_if_fail(rz_cmd_desc_argv_modes_new(rcmd, root, #cmd, flags, name_handler(cmd), &name_help(cmd)))
 
#define rz_cmd_desc_argv_new_warn(rcmd, root, cmd)    rz_warn_if_fail(rz_cmd_desc_argv_new(rcmd, root, #cmd, name_handler(cmd), &name_help(cmd)))
 

Functions

static RzBinJavaClasscore_java_get_class (RzCore *core)
 
RZ_IPI RzCmdStatus rz_cmd_javac_handler (RzCore *core, int argc, const char **argv, RzOutputMode mode)
 
RZ_IPI RzCmdStatus rz_cmd_javap_handler (RzCore *core, int argc, const char **argv, RzOutputMode mode)
 
RZ_IPI RzCmdStatus rz_cmd_javai_handler (RzCore *core, int argc, const char **argv, RzOutputMode mode)
 
RZ_IPI RzCmdStatus rz_cmd_javam_handler (RzCore *core, int argc, const char **argv, RzOutputMode mode)
 
RZ_IPI RzCmdStatus rz_cmd_javaf_handler (RzCore *core, int argc, const char **argv, RzOutputMode mode)
 
RZ_IPI RzCmdStatus rz_cmd_javas_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_javar_handler (RzCore *core, int argc, const char **argv)
 
 static_description_without_args (javac, "prints the class structure")
 
 static_description_without_args (javaf, "prints the class fields")
 
 static_description_without_args (javai, "prints the class interfaces")
 
 static_description_without_args (javam, "prints the class methods")
 
 static_description_without_args (javap, "prints the class constant pool")
 
 static_description_without_args (javas, "prints the class like a java source code")
 
static const RzCmdDescArg name_args (javar)[]
 
static const RzCmdDescHelp name_help (javar)
 
static bool rz_cmd_java_init_handler (RzCore *core)
 

Variables

static const RzCmdDescHelp java_usage
 
RzCorePlugin rz_core_plugin_java
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ name_args

#define name_args (   name)    (cmd_##name##_args)

Definition at line 15 of file core_java.c.

◆ name_handler

#define name_handler (   name)    (rz_cmd_##name##_handler)

Definition at line 17 of file core_java.c.

◆ name_help

#define name_help (   name)    (cmd_##name##_help)

Definition at line 16 of file core_java.c.

◆ rz_cmd_desc_argv_modes_new_warn

#define rz_cmd_desc_argv_modes_new_warn (   rcmd,
  root,
  cmd,
  flags 
)     rz_warn_if_fail(rz_cmd_desc_argv_modes_new(rcmd, root, #cmd, flags, name_handler(cmd), &name_help(cmd)))

Definition at line 26 of file core_java.c.

◆ rz_cmd_desc_argv_new_warn

#define rz_cmd_desc_argv_new_warn (   rcmd,
  root,
  cmd 
)     rz_warn_if_fail(rz_cmd_desc_argv_new(rcmd, root, #cmd, name_handler(cmd), &name_help(cmd)))

Definition at line 29 of file core_java.c.

◆ static_description_without_args

#define static_description_without_args (   command,
  summ 
)
Value:
static const RzCmdDescArg name_args(command)[] = { \
{ 0 }, \
}; \
static const RzCmdDescHelp name_help(command) = { \
.summary = summ, \
.args = name_args(command), \
}
#define name_help(name)
Definition: core_java.c:16
#define name_args(name)
Definition: core_java.c:15
const char * command
Definition: main.c:7

Definition at line 18 of file core_java.c.

Function Documentation

◆ core_java_get_class()

static RzBinJavaClass* core_java_get_class ( RzCore core)
static

Definition at line 32 of file core_java.c.

32  {
33  if (!core) {
34  return NULL;
35  }
36  RzAnalysis *analysis = core->analysis;
37  if (!analysis || !analysis->binb.bin) {
38  return NULL;
39  }
40  RzBin *b = analysis->binb.bin;
41  if (!b->cur || !b->cur->o) {
42  return NULL;
43  }
44  RzBinPlugin *plugin = b->cur->o->plugin;
45  return plugin && !strcmp(plugin->name, "java") ? (RzBinJavaClass *)b->cur->o->bin_obj : NULL;
46 }
#define NULL
Definition: cris-opc.c:27
#define b(i)
Definition: sha256.c:42
RzBinBind binb
Definition: rz_analysis.h:579
RzBin * bin
Definition: rz_bin.h:807
char * name
Definition: rz_bin.h:509
RzAnalysis * analysis
Definition: rz_core.h:322

References rz_core_t::analysis, b, rz_bin_bind_t::bin, rz_analysis_t::binb, rz_bin_plugin_t::name, and NULL.

Referenced by rz_cmd_javac_handler(), rz_cmd_javaf_handler(), rz_cmd_javai_handler(), rz_cmd_javam_handler(), rz_cmd_javap_handler(), rz_cmd_javar_handler(), and rz_cmd_javas_handler().

◆ name_args()

static const RzCmdDescArg name_args ( javar  )
static

References RZ_CMD_ARG_TYPE_NUM.

◆ name_help()

static const RzCmdDescHelp name_help ( javar  )
static

References name_args.

◆ rz_cmd_java_init_handler()

static bool rz_cmd_java_init_handler ( RzCore core)
static

Definition at line 293 of file core_java.c.

293  {
294  RzCmd *rcmd = core->rcmd;
296  if (!root_cd) {
297  return false;
298  }
299 
300  RzCmdDesc *java = rz_cmd_desc_group_new(rcmd, root_cd, "java", NULL, NULL, &java_usage);
301  if (!java) {
303  return false;
304  }
305 
311  rz_cmd_desc_argv_new_warn(rcmd, java, javas);
312  rz_cmd_desc_argv_new_warn(rcmd, java, javar);
313 
314  return true;
315 }
RZ_API RzCmdDesc * rz_cmd_get_root(RzCmd *cmd)
Get the root command descriptor.
Definition: cmd_api.c:230
RZ_API RzCmdDesc * rz_cmd_desc_group_new(RzCmd *cmd, RzCmdDesc *parent, const char *name, RzCmdArgvCb cb, const RzCmdDescHelp *help, const RzCmdDescHelp *group_help)
Create a new command descriptor for a name that is used both as a group but that has a sub-command wi...
Definition: cmd_api.c:2233
#define rz_cmd_desc_argv_new_warn(rcmd, root, cmd)
Definition: core_java.c:29
static const RzCmdDescHelp java_usage
Definition: core_java.c:269
#define rz_cmd_desc_argv_modes_new_warn(rcmd, root, cmd, flags)
Definition: core_java.c:26
#define rz_warn_if_reached()
Definition: rz_assert.h:29
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
RzCmd * rcmd
Definition: rz_core.h:319

References java_usage, NULL, rz_core_t::rcmd, cmd_descs_generate::root_cd, rz_cmd_desc_argv_modes_new_warn, rz_cmd_desc_argv_new_warn, rz_cmd_desc_group_new(), rz_cmd_get_root(), RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_STANDARD, and rz_warn_if_reached.

◆ rz_cmd_javac_handler()

RZ_IPI RzCmdStatus rz_cmd_javac_handler ( RzCore core,
int  argc,
const char **  argv,
RzOutputMode  mode 
)

Definition at line 48 of file core_java.c.

48  {
49  if (argc != 1) {
51  }
52 
54  if (!jclass) {
55  return RZ_CMD_STATUS_ERROR;
56  }
57 
58  if (mode & RZ_OUTPUT_MODE_JSON) {
59  PJ *pj = pj_new();
60  if (!pj) {
61  return RZ_CMD_STATUS_ERROR;
62  }
63 
66  pj_free(pj);
67  } else {
68  RzStrBuf *sb = rz_strbuf_new("");
69  if (!sb) {
70  return RZ_CMD_STATUS_ERROR;
71  }
72 
74  rz_cons_print(rz_strbuf_get(sb));
76  }
77 
78  return RZ_CMD_STATUS_OK;
79 }
static SblHeader sb
Definition: bin_mbn.c:26
RZ_API void rz_bin_java_class_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the class info as json.
Definition: class_bin.c:521
RZ_API void rz_bin_java_class_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class info as text.
Definition: class_bin.c:591
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
static RzBinJavaClass * core_java_get_class(RzCore *core)
Definition: core_java.c:32
const char int mode
Definition: ioapi.h:137
jobject jclass
Definition: jni.h:104
@ RZ_CMD_STATUS_OK
command handler exited in the right way
Definition: rz_cmd.h:24
@ RZ_CMD_STATUS_WRONG_ARGS
command handler could not handle the arguments passed to it
Definition: rz_cmd.h:25
@ RZ_CMD_STATUS_ERROR
command handler had issues while running (e.g. allocation error, etc.)
Definition: rz_cmd.h:26
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
Definition: rz_pj.h:12

References core_java_get_class(), pj_free(), pj_new(), pj_string(), rz_bin_java_class_as_json(), rz_bin_java_class_as_text(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_cons_println(), RZ_OUTPUT_MODE_JSON, rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_new(), and sb.

◆ rz_cmd_javaf_handler()

RZ_IPI RzCmdStatus rz_cmd_javaf_handler ( RzCore core,
int  argc,
const char **  argv,
RzOutputMode  mode 
)

Definition at line 180 of file core_java.c.

180  {
181  if (argc != 1) {
183  }
184 
186  if (!jclass) {
187  return RZ_CMD_STATUS_ERROR;
188  }
189 
190  if (mode & RZ_OUTPUT_MODE_JSON) {
191  PJ *pj = pj_new();
192  if (!pj) {
193  return RZ_CMD_STATUS_ERROR;
194  }
195 
198  pj_free(pj);
199  } else {
200  RzStrBuf *sb = rz_strbuf_new("");
201  if (!sb) {
202  return RZ_CMD_STATUS_ERROR;
203  }
204 
206  rz_cons_print(rz_strbuf_get(sb));
208  }
209 
210  return RZ_CMD_STATUS_OK;
211 }
RZ_API void rz_bin_java_class_fields_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the fields in json format via PJ arg.
Definition: class_bin.c:1366
RZ_API void rz_bin_java_class_fields_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the fields in text format via RzStrBuf arg.
Definition: class_bin.c:1318

References core_java_get_class(), pj_free(), pj_new(), pj_string(), rz_bin_java_class_fields_as_json(), rz_bin_java_class_fields_as_text(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_cons_println(), RZ_OUTPUT_MODE_JSON, rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_new(), and sb.

◆ rz_cmd_javai_handler()

RZ_IPI RzCmdStatus rz_cmd_javai_handler ( RzCore core,
int  argc,
const char **  argv,
RzOutputMode  mode 
)

Definition at line 114 of file core_java.c.

114  {
115  if (argc != 1) {
117  }
118 
120  if (!jclass) {
121  return RZ_CMD_STATUS_ERROR;
122  }
123 
124  if (mode & RZ_OUTPUT_MODE_JSON) {
125  PJ *pj = pj_new();
126  if (!pj) {
127  return RZ_CMD_STATUS_ERROR;
128  }
129 
132  pj_free(pj);
133  } else {
134  RzStrBuf *sb = rz_strbuf_new("");
135  if (!sb) {
136  return RZ_CMD_STATUS_ERROR;
137  }
138 
140  rz_cons_print(rz_strbuf_get(sb));
142  }
143 
144  return RZ_CMD_STATUS_OK;
145 }
RZ_API void rz_bin_java_class_interfaces_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class interfaces as text via RzStrBuf arg.
Definition: class_bin.c:1898
RZ_API void rz_bin_java_class_interfaces_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the class interfaces as json via PJ arg.
Definition: class_bin.c:1928

References core_java_get_class(), pj_free(), pj_new(), pj_string(), rz_bin_java_class_interfaces_as_json(), rz_bin_java_class_interfaces_as_text(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_cons_println(), RZ_OUTPUT_MODE_JSON, rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_new(), and sb.

◆ rz_cmd_javam_handler()

RZ_IPI RzCmdStatus rz_cmd_javam_handler ( RzCore core,
int  argc,
const char **  argv,
RzOutputMode  mode 
)

Definition at line 147 of file core_java.c.

147  {
148  if (argc != 1) {
150  }
151 
153  if (!jclass) {
154  return RZ_CMD_STATUS_ERROR;
155  }
156 
157  if (mode & RZ_OUTPUT_MODE_JSON) {
158  PJ *pj = pj_new();
159  if (!pj) {
160  return RZ_CMD_STATUS_ERROR;
161  }
162 
165  pj_free(pj);
166  } else {
167  RzStrBuf *sb = rz_strbuf_new("");
168  if (!sb) {
169  return RZ_CMD_STATUS_ERROR;
170  }
171 
173  rz_cons_print(rz_strbuf_get(sb));
175  }
176 
177  return RZ_CMD_STATUS_OK;
178 }
RZ_API void rz_bin_java_class_methods_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the methods in text format via RzStrBuf arg.
Definition: class_bin.c:1121
RZ_API void rz_bin_java_class_methods_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the methods in json format via PJ arg.
Definition: class_bin.c:1169

References core_java_get_class(), pj_free(), pj_new(), pj_string(), rz_bin_java_class_methods_as_json(), rz_bin_java_class_methods_as_text(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_cons_println(), RZ_OUTPUT_MODE_JSON, rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_new(), and sb.

◆ rz_cmd_javap_handler()

RZ_IPI RzCmdStatus rz_cmd_javap_handler ( RzCore core,
int  argc,
const char **  argv,
RzOutputMode  mode 
)

Definition at line 81 of file core_java.c.

81  {
82  if (argc != 1) {
84  }
85 
87  if (!jclass) {
88  return RZ_CMD_STATUS_ERROR;
89  }
90 
91  if (mode & RZ_OUTPUT_MODE_JSON) {
92  PJ *pj = pj_new();
93  if (!pj) {
94  return RZ_CMD_STATUS_ERROR;
95  }
96 
99  pj_free(pj);
100  } else {
101  RzStrBuf *sb = rz_strbuf_new("");
102  if (!sb) {
103  return RZ_CMD_STATUS_ERROR;
104  }
105 
107  rz_cons_print(rz_strbuf_get(sb));
109  }
110 
111  return RZ_CMD_STATUS_OK;
112 }
RZ_API void rz_bin_java_class_const_pool_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the class const pool in json format via PJ arg.
Definition: class_bin.c:1650
RZ_API void rz_bin_java_class_const_pool_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class const pool in text format via RzStrBuf arg.
Definition: class_bin.c:1605

References core_java_get_class(), pj_free(), pj_new(), pj_string(), rz_bin_java_class_const_pool_as_json(), rz_bin_java_class_const_pool_as_text(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_cons_println(), RZ_OUTPUT_MODE_JSON, rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_new(), and sb.

◆ rz_cmd_javar_handler()

RZ_IPI RzCmdStatus rz_cmd_javar_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 234 of file core_java.c.

234  {
235  if (argc != 2) {
237  }
238 
240  if (!jclass) {
241  return RZ_CMD_STATUS_ERROR;
242  }
243 
244  st32 index = rz_num_math(core->num, argv[1]);
245 
246  if (index < 1) {
247  RZ_LOG_ERROR("can't resolve constant pool index %d\n", index);
248  return RZ_CMD_STATUS_INVALID;
249  }
250 
251  char *resolved = rz_bin_java_class_const_pool_resolve_index(jclass, index);
252  if (!resolved) {
253  RZ_LOG_ERROR("can't resolve constant pool index %d\n", index);
254  return RZ_CMD_STATUS_INVALID;
255  }
256 
257  char *demangled = rz_demangler_java(resolved);
258  if (demangled) {
259  rz_cons_println(demangled);
260  } else {
261  rz_cons_println(resolved);
262  }
263 
264  free(resolved);
265  free(demangled);
266  return RZ_CMD_STATUS_OK;
267 }
RZ_API RZ_OWN char * rz_bin_java_class_const_pool_resolve_index(RZ_NONNULL RzBinJavaClass *bin, st32 index)
Returns the string linked to the class const pool index.
Definition: class_bin.c:644
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API RZ_OWN char * rz_demangler_java(RZ_NULLABLE const char *symbol)
Demangles java symbols.
Definition: demangler.c:38
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
@ RZ_CMD_STATUS_INVALID
command could not be executed (e.g. shell level error, bad expression, etc.)
Definition: rz_cmd.h:27
#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
#define st32
Definition: rz_types_base.h:12
RzNum * num
Definition: rz_core.h:316

References argv, core_java_get_class(), free(), rz_core_t::num, rz_bin_java_class_const_pool_resolve_index(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_INVALID, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_cons_println(), rz_demangler_java(), RZ_LOG_ERROR, rz_num_math(), and st32.

◆ rz_cmd_javas_handler()

RZ_IPI RzCmdStatus rz_cmd_javas_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 213 of file core_java.c.

213  {
214  if (argc != 1) {
216  }
217 
219  if (!jclass) {
220  return RZ_CMD_STATUS_ERROR;
221  }
222 
223  RzStrBuf *sb = rz_strbuf_new("");
224  if (!sb) {
225  return RZ_CMD_STATUS_ERROR;
226  }
227 
229  rz_cons_print(rz_strbuf_get(sb));
231  return RZ_CMD_STATUS_OK;
232 }
RZ_API void rz_bin_java_class_as_source_code(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class info as text source code.
Definition: class_bin.c:708

References core_java_get_class(), rz_bin_java_class_as_source_code(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_new(), and sb.

◆ static_description_without_args() [1/6]

static_description_without_args ( javac  ,
"prints the class structure"   
)

◆ static_description_without_args() [2/6]

static_description_without_args ( javaf  ,
"prints the class fields  
)

◆ static_description_without_args() [3/6]

static_description_without_args ( javai  ,
"prints the class interfaces  
)

◆ static_description_without_args() [4/6]

static_description_without_args ( javam  ,
"prints the class methods"   
)

◆ static_description_without_args() [5/6]

static_description_without_args ( javap  ,
"prints the class constant pool"   
)

◆ static_description_without_args() [6/6]

static_description_without_args ( javas  ,
"prints the class like a java source code  
)

Variable Documentation

◆ java_usage

const RzCmdDescHelp java_usage
static
Initial value:
= {
.summary = "Core plugin to visualize java class information",
}

Definition at line 269 of file core_java.c.

Referenced by rz_cmd_java_init_handler().

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
}
RzCorePlugin rz_core_plugin_java
Definition: core_java.c:317
@ RZ_LIB_TYPE_CORE
Definition: rz_lib.h:83
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_core.h:128

Definition at line 327 of file core_java.c.

◆ rz_core_plugin_java

RzCorePlugin rz_core_plugin_java
Initial value:
= {
.name = "java",
.desc = "Suite of java commands, type `java` for more info",
.license = "LGPL-3.0-only",
.author = "deroad",
.version = "1.0",
}
static bool rz_cmd_java_init_handler(RzCore *core)
Definition: core_java.c:293

Definition at line 317 of file core_java.c.