Rizin
unix-like reverse engineering framework and cli tools
bin_xtr_fatmach0.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2019 nibble <nibble.ds@gmail.com>
2 // SPDX-FileCopyrightText: 2009-2019 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_types.h>
6 #include <rz_util.h>
7 #include <rz_lib.h>
8 #include <rz_bin.h>
9 #include "mach0/fatmach0.h"
10 #include "mach0/mach0.h"
11 
12 static RzBinXtrData *extract(RzBin *bin, int idx);
13 
14 static bool checkHeader(RzBuffer *b) {
15  ut8 buf[4];
16  const ut64 sz = rz_buf_size(b);
17  rz_buf_read_at(b, 0, buf, 4);
18  if (sz >= 0x300 && !memcmp(buf, "\xca\xfe\xba\xbe", 4)) {
19  ut64 addr = 4 * sizeof(32);
20 
21  ut32 off;
22  if (!rz_buf_read_be32_at(b, addr, &off)) {
23  return false;
24  }
25 
26  if (off > 0 && off + 4 < sz) {
27  ut64 h = 0;
28  rz_buf_read_at(b, h + off, buf, 4);
29  if (!memcmp(buf, "\xce\xfa\xed\xfe", 4) ||
30  !memcmp(buf, "\xfe\xed\xfa\xce", 4) ||
31  !memcmp(buf, "\xfe\xed\xfa\xcf", 4) ||
32  !memcmp(buf, "\xcf\xfa\xed\xfe", 4)) {
33  return true;
34  }
35  }
36  }
37  return false;
38 }
39 
40 static bool check_buffer(RzBuffer *buf) {
41  rz_return_val_if_fail(buf, false);
42  return checkHeader(buf);
43 }
44 
45 static void free_xtr(void *xtr_obj) {
47 }
48 
49 static void destroy(RzBin *bin) {
50  free_xtr(bin->cur->xtr_obj);
51 }
52 
53 static bool load(RzBin *bin) {
54  return ((bin->cur->xtr_obj = rz_bin_fatmach0_new(bin->file)) != NULL);
55 }
56 
57 static int size(RzBin *bin) {
58  // TODO
59  return 0;
60 }
61 
62 static inline void fill_metadata_info_from_hdr(RzBinXtrMetadata *meta, struct MACH0_(mach_header) * hdr) {
63  meta->arch = strdup(MACH0_(get_cputype_from_hdr)(hdr));
64  meta->bits = MACH0_(get_bits_from_hdr)(hdr);
66  meta->type = MACH0_(get_filetype_from_hdr)(hdr);
67  meta->libname = NULL;
68  meta->xtr_type = "fat";
69 }
70 
71 // XXX deprecate
72 static RzBinXtrData *extract(RzBin *bin, int idx) {
73  int narch;
74  struct rz_bin_fatmach0_obj_t *fb = bin->cur->xtr_obj;
76  if (!arch) {
77  return NULL;
78  }
80  if (!metadata) {
81  rz_buf_free(arch->b);
82  free(arch);
83  return NULL;
84  }
85  struct MACH0_(mach_header) *hdr = MACH0_(get_hdr)(arch->b);
86  if (!hdr) {
87  free(metadata);
88  free(arch);
89  free(hdr);
90  return NULL;
91  }
92  fill_metadata_info_from_hdr(metadata, hdr);
93  RzBinXtrData *res = rz_bin_xtrdata_new(arch->b, arch->offset, arch->size, narch, metadata);
94  rz_buf_free(arch->b);
95  free(arch);
96  free(hdr);
97  return res;
98 }
99 
101  rz_return_val_if_fail(bin && bin->cur, NULL);
102 
103  if (!bin->cur->xtr_obj) {
104  bin->cur->xtr_obj = rz_bin_fatmach0_from_buffer_new(b);
105  }
106  int narch;
107  struct rz_bin_fatmach0_obj_t *fb = bin->cur->xtr_obj;
109  if (arch) {
111  if (metadata) {
112  struct MACH0_(mach_header) *hdr = MACH0_(get_hdr)(arch->b);
113  if (hdr) {
114  fill_metadata_info_from_hdr(metadata, hdr);
115  RzBinXtrData *res = rz_bin_xtrdata_new(arch->b, arch->offset, arch->size, narch, metadata);
116  rz_buf_free(arch->b);
117  free(arch);
118  free(hdr);
119  return res;
120  }
121  free(metadata);
122  }
123  free(arch);
124  }
125  return NULL;
126 }
127 
129  RzBinXtrData *data = oneshot_buffer(bin, b, 0);
130  if (data) {
131  // XXX - how do we validate a valid narch?
132  int narch = data->file_count;
134  if (!res) {
135  rz_bin_xtrdata_free(data);
136  return NULL;
137  }
138  rz_list_append(res, data);
139  int i = 0;
140  for (i = 1; data && i < narch; i++) {
141  data = oneshot_buffer(bin, b, i);
142  if (data) {
143  rz_list_append(res, data);
144  }
145  }
146  return res;
147  }
148  return NULL;
149 }
150 
152  .name = "xtr.fatmach0",
153  .desc = "fat mach0 bin extractor plugin",
154  .license = "LGPL3",
155  .load = &load,
156  .size = &size,
157  .extract = &extract,
158  .destroy = &destroy,
159  .extract_from_buffer = &oneshot_buffer,
160  .extractall_from_buffer = &oneshotall_buffer,
161  .free_xtr = &free_xtr,
162  .check_buffer = check_buffer,
163 };
164 
165 #ifndef RZ_PLUGIN_INCORE
168  .data = &rz_bin_xtr_plugin_fatmach0,
169  .version = RZ_VERSION
170 };
171 #endif
lzma_index ** i
Definition: index.h:629
RZ_API void rz_bin_xtrdata_free(void *data_)
Definition: bin.c:61
RZ_API RzBinXtrData * rz_bin_xtrdata_new(RzBuffer *buf, ut64 offset, ut64 size, ut32 file_count, RzBinXtrMetadata *metadata)
Definition: bin.c:47
static bool check_buffer(RzBuffer *buf)
RzBinXtrPlugin rz_bin_xtr_plugin_xtr_fatmach0
static RzBinXtrData * extract(RzBin *bin, int idx)
static RzList * oneshotall_buffer(RzBin *bin, RzBuffer *b)
static void free_xtr(void *xtr_obj)
static void destroy(RzBin *bin)
static int size(RzBin *bin)
RZ_API RzLibStruct rizin_plugin
static bool load(RzBin *bin)
static void fill_metadata_info_from_hdr(RzBinXtrMetadata *meta, struct MACH0_(mach_header) *hdr)
static bool checkHeader(RzBuffer *b)
static RzBinXtrData * oneshot_buffer(RzBin *bin, RzBuffer *b, int idx)
#define RZ_API
#define NULL
Definition: cris-opc.c:27
cs_arch arch
Definition: cstool.c:13
uint32_t ut32
struct rz_bin_fatmach0_arch_t * rz_bin_fatmach0_extract(struct rz_bin_fatmach0_obj_t *bin, int idx, int *narch)
Definition: fatmach0.c:53
struct rz_bin_fatmach0_obj_t * rz_bin_fatmach0_from_buffer_new(RzBuffer *b)
Definition: fatmach0.c:112
struct rz_bin_fatmach0_obj_t * rz_bin_fatmach0_new(const char *file)
Definition: fatmach0.c:88
void * rz_bin_fatmach0_free(struct rz_bin_fatmach0_obj_t *bin)
Definition: fatmach0.c:78
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
char *MACH0_() get_filetype_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3559
int MACH0_() get_bits_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3299
const char *MACH0_() get_cputype_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3336
char *MACH0_() get_cpusubtype_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3541
#define MACH0_(name)
Definition: mach0_specs.h:20
int idx
Definition: setup.py:197
int off
Definition: pal.c:13
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
#define rz_buf_read_be32_at(b, addr, result)
Definition: rz_buf.h:285
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
@ RZ_LIB_TYPE_BIN_XTR
Definition: rz_lib.h:76
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
#define h(i)
Definition: sha256.c:48
Definition: malloc.c:26
const char * xtr_type
Definition: rz_bin.h:374
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58