Rizin
unix-like reverse engineering framework and cli tools
class_field.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_field.h"
5 #include "class_private.h"
6 
7 #define FIELD_ACCESS_FLAGS_SIZE 16
9  { FIELD_ACCESS_FLAG_PUBLIC, "public" },
10  { FIELD_ACCESS_FLAG_PRIVATE, "private" },
11  { FIELD_ACCESS_FLAG_PROTECTED, "protected" },
12  { FIELD_ACCESS_FLAG_STATIC, "static" },
13  { FIELD_ACCESS_FLAG_FINAL, "final" },
14  { FIELD_ACCESS_FLAG_VOLATILE, "volatile" },
15  { FIELD_ACCESS_FLAG_TRANSIENT, "transient" },
16  { FIELD_ACCESS_FLAG_SYNTHETIC, "synthetic" },
17  { FIELD_ACCESS_FLAG_ENUM, "enum" },
18 };
19 
22  RzStrBuf *sb = NULL;
23 
24  for (ut32 i = 0; i < FIELD_ACCESS_FLAGS_SIZE; ++i) {
26  if (field->access_flags & afr->flag) {
27  if (!sb) {
28  sb = rz_strbuf_new(afr->readable);
29  if (!sb) {
30  return NULL;
31  }
32  } else {
33  rz_strbuf_appendf(sb, " %s", afr->readable);
34  }
35  }
36  }
37 
38  return sb ? rz_strbuf_drain(sb) : NULL;
39 }
40 
42  Field *field = RZ_NEW0(Field);
44  field->offset = offset;
45  ut64 base = offset - rz_buf_tell(buf);
46 
47  if (!rz_buf_read_be16(buf, &field->access_flags) ||
48  !rz_buf_read_be16(buf, &field->name_index) ||
51  free(field);
52  return NULL;
53  }
54 
55  if (field->attributes_count < 1) {
56  return field;
57  }
58  field->attributes = RZ_NEWS0(Attribute *, field->attributes_count);
59  if (!field->attributes) {
60  free(field);
62  return NULL;
63  }
64 
65  for (ut32 i = 0; i < field->attributes_count; ++i) {
66  offset = rz_buf_tell(buf) + base;
68  if (attr && java_attribute_resolve(pool, poolsize, attr, buf, false)) {
69  field->attributes[i] = attr;
70  } else {
71  java_attribute_free(attr);
72  break;
73  }
74  }
75  return field;
76 }
77 
78 void java_field_free(Field *field) {
79  if (!field) {
80  return;
81  }
82  if (field->attributes) {
83  for (ut32 i = 0; i < field->attributes_count; ++i) {
85  }
86  free(field->attributes);
87  }
88  free(field);
89 }
90 
91 bool java_field_is_global(const Field *field) {
93  return field && (field->access_flags & flag) == flag;
94 }
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)
bool java_field_is_global(const Field *field)
Definition: class_field.c:91
Field * java_field_new(ConstPool **pool, ut32 poolsize, RzBuffer *buf, ut64 offset)
Definition: class_field.c:41
static const AccessFlagsReadable access_flags_list[FIELD_ACCESS_FLAGS_SIZE]
Definition: class_field.c:8
void java_field_free(Field *field)
Definition: class_field.c:78
char * java_field_access_flags_readable(const Field *field)
Definition: class_field.c:20
#define FIELD_ACCESS_FLAGS_SIZE
Definition: class_field.c:7
@ FIELD_ACCESS_FLAG_PUBLIC
Definition: class_field.h:12
@ FIELD_ACCESS_FLAG_STATIC
Definition: class_field.h:15
@ FIELD_ACCESS_FLAG_VOLATILE
Definition: class_field.h:17
@ FIELD_ACCESS_FLAG_ENUM
Definition: class_field.h:20
@ FIELD_ACCESS_FLAG_FINAL
Definition: class_field.h:16
@ FIELD_ACCESS_FLAG_PRIVATE
Definition: class_field.h:13
@ FIELD_ACCESS_FLAG_TRANSIENT
Definition: class_field.h:18
@ FIELD_ACCESS_FLAG_SYNTHETIC
Definition: class_field.h:19
@ FIELD_ACCESS_FLAG_PROTECTED
Definition: class_field.h:14
#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_field.h:29
ut16 access_flags
Definition: class_field.h:25
ut16 descriptor_index
Definition: class_field.h:27
ut16 attributes_count
Definition: class_field.h:28
ut16 name_index
Definition: class_field.h:26
ut64(WINAPI *w32_GetEnabledXStateFeatures)()