Rizin
unix-like reverse engineering framework and cli tools
class_method.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "class_method.h"
5 #include "class_private.h"
6 
7 #define METHOD_ACCESS_FLAGS_SIZE 16
9  { METHOD_ACCESS_FLAG_PUBLIC, "public" },
10  { METHOD_ACCESS_FLAG_PRIVATE, "private" },
11  { METHOD_ACCESS_FLAG_PROTECTED, "protected" },
12  { METHOD_ACCESS_FLAG_STATIC, "static" },
13  { METHOD_ACCESS_FLAG_FINAL, "final" },
14  { METHOD_ACCESS_FLAG_SYNCHRONIZED, "synchronized" },
15  { METHOD_ACCESS_FLAG_BRIDGE, "bridge" },
16  { METHOD_ACCESS_FLAG_VARARGS, "varargs" },
17  { METHOD_ACCESS_FLAG_NATIVE, "native" },
18  { METHOD_ACCESS_FLAG_INTERFACE, "interface" },
19  { METHOD_ACCESS_FLAG_ABSTRACT, "abstract" },
20  { METHOD_ACCESS_FLAG_STRICT, "strict" },
21  { METHOD_ACCESS_FLAG_SYNTHETIC, "synthetic" },
22  { METHOD_ACCESS_FLAG_ANNOTATION, "annotation" },
23  { METHOD_ACCESS_FLAG_ENUM, "enum" },
24 };
25 
27  rz_return_val_if_fail(method, NULL);
28  RzStrBuf *sb = NULL;
29 
30  for (ut32 i = 0; i < METHOD_ACCESS_FLAGS_SIZE; ++i) {
32  if (method->access_flags & afr->flag) {
33  if (!sb) {
34  sb = rz_strbuf_new(afr->readable);
35  if (!sb) {
36  return NULL;
37  }
38  } else {
39  rz_strbuf_appendf(sb, " %s", afr->readable);
40  }
41  }
42  }
43 
44  return sb ? rz_strbuf_drain(sb) : NULL;
45 }
46 
47 static bool java_method_new_aux(RzBuffer *buf, Method *method) {
48  return rz_buf_read_be16(buf, &method->access_flags) &&
49  rz_buf_read_be16(buf, &method->name_index) &&
52 }
53 
54 Method *java_method_new(ConstPool **pool, ut32 poolsize, RzBuffer *buf, ut64 offset, bool is_oak) {
55  Method *method = RZ_NEW0(Method);
56  rz_return_val_if_fail(method, NULL);
57  method->offset = offset;
58  ut64 base = offset - rz_buf_tell(buf);
59 
60  if (!java_method_new_aux(buf, method)) {
61  free(method);
62  return NULL;
63  }
64 
65  if (method->attributes_count < 1) {
66  return method;
67  }
68 
69  method->attributes = RZ_NEWS0(Attribute *, method->attributes_count);
70  if (!method->attributes) {
71  free(method);
73  return NULL;
74  }
75 
76  for (ut32 i = 0; i < method->attributes_count; ++i) {
77  offset = rz_buf_tell(buf) + base;
79  if (attr && java_attribute_resolve(pool, poolsize, attr, buf, is_oak)) {
80  method->attributes[i] = attr;
81  } else {
82  java_attribute_free(attr);
83  break;
84  }
85  }
86  return method;
87 }
88 
89 void java_method_free(Method *method) {
90  if (!method) {
91  return;
92  }
93  if (method->attributes) {
94  for (ut32 i = 0; i < method->attributes_count; ++i) {
96  }
97  free(method->attributes);
98  }
99  free(method);
100 }
101 
102 bool java_method_is_global(const Method *method) {
104  return method && (method->access_flags & flag) == flag;
105 }
lzma_index ** i
Definition: index.h:629
static SblHeader sb
Definition: bin_mbn.c:26
bool java_attribute_resolve(ConstPool **pool, ut32 poolsize, Attribute *attr, RzBuffer *buf, bool is_oak)
Attribute * java_attribute_new(RzBuffer *buf, ut64 offset)
void java_attribute_free(Attribute *attr)
Method * java_method_new(ConstPool **pool, ut32 poolsize, RzBuffer *buf, ut64 offset, bool is_oak)
Definition: class_method.c:54
static const AccessFlagsReadable access_flags_list[METHOD_ACCESS_FLAGS_SIZE]
Definition: class_method.c:8
static bool java_method_new_aux(RzBuffer *buf, Method *method)
Definition: class_method.c:47
bool java_method_is_global(const Method *method)
Definition: class_method.c:102
#define METHOD_ACCESS_FLAGS_SIZE
Definition: class_method.c:7
char * java_method_access_flags_readable(const Method *method)
Definition: class_method.c:26
void java_method_free(Method *method)
Definition: class_method.c:89
@ METHOD_ACCESS_FLAG_ABSTRACT
Definition: class_method.h:22
@ METHOD_ACCESS_FLAG_SYNTHETIC
Definition: class_method.h:24
@ METHOD_ACCESS_FLAG_PRIVATE
Definition: class_method.h:13
@ METHOD_ACCESS_FLAG_SYNCHRONIZED
Definition: class_method.h:17
@ METHOD_ACCESS_FLAG_STRICT
Definition: class_method.h:23
@ METHOD_ACCESS_FLAG_PROTECTED
Definition: class_method.h:14
@ METHOD_ACCESS_FLAG_BRIDGE
Definition: class_method.h:18
@ METHOD_ACCESS_FLAG_PUBLIC
Definition: class_method.h:12
@ METHOD_ACCESS_FLAG_ANNOTATION
Definition: class_method.h:25
@ METHOD_ACCESS_FLAG_ENUM
Definition: class_method.h:26
@ METHOD_ACCESS_FLAG_INTERFACE
Definition: class_method.h:21
@ METHOD_ACCESS_FLAG_VARARGS
Definition: class_method.h:19
@ METHOD_ACCESS_FLAG_FINAL
Definition: class_method.h:16
@ METHOD_ACCESS_FLAG_STATIC
Definition: class_method.h:15
@ METHOD_ACCESS_FLAG_NATIVE
Definition: class_method.h:20
#define NULL
Definition: cris-opc.c:27
uint16_t ut16
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API ut64 rz_buf_tell(RZ_NONNULL RzBuffer *b)
Return the current cursor position.
Definition: buf.c:1238
#define rz_buf_read_be16(b, result)
Definition: rz_buf.h:280
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
Attribute ** attributes
Definition: class_method.h:35
ut16 attributes_count
Definition: class_method.h:34
ut16 descriptor_index
Definition: class_method.h:33
ut64(WINAPI *w32_GetEnabledXStateFeatures)()