Rizin
unix-like reverse engineering framework and cli tools
bin_xtr_fatmach0.c File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_lib.h>
#include <rz_bin.h>
#include "mach0/fatmach0.h"
#include "mach0/mach0.h"

Go to the source code of this file.

Functions

static RzBinXtrDataextract (RzBin *bin, int idx)
 
static bool checkHeader (RzBuffer *b)
 
static bool check_buffer (RzBuffer *buf)
 
static void free_xtr (void *xtr_obj)
 
static void destroy (RzBin *bin)
 
static bool load (RzBin *bin)
 
static int size (RzBin *bin)
 
static void fill_metadata_info_from_hdr (RzBinXtrMetadata *meta, struct MACH0_(mach_header) *hdr)
 
static RzBinXtrDataoneshot_buffer (RzBin *bin, RzBuffer *b, int idx)
 
static RzListoneshotall_buffer (RzBin *bin, RzBuffer *b)
 

Variables

RzBinXtrPlugin rz_bin_xtr_plugin_xtr_fatmach0
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ check_buffer()

static bool check_buffer ( RzBuffer buf)
static

Definition at line 40 of file bin_xtr_fatmach0.c.

40  {
41  rz_return_val_if_fail(buf, false);
42  return checkHeader(buf);
43 }
static bool checkHeader(RzBuffer *b)
voidpf void * buf
Definition: ioapi.h:138
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References checkHeader(), and rz_return_val_if_fail.

◆ checkHeader()

static bool checkHeader ( RzBuffer b)
static

Definition at line 14 of file bin_xtr_fatmach0.c.

14  {
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 }
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
int off
Definition: pal.c:13
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 ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define b(i)
Definition: sha256.c:42
#define h(i)
Definition: sha256.c:48
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58

References addr, b, h, off, rz_buf_read_at(), rz_buf_read_be32_at, rz_buf_size(), and ut64().

Referenced by check_buffer().

◆ destroy()

static void destroy ( RzBin bin)
static

Definition at line 49 of file bin_xtr_fatmach0.c.

49  {
50  free_xtr(bin->cur->xtr_obj);
51 }
static void free_xtr(void *xtr_obj)
Definition: malloc.c:26

References free_xtr().

◆ extract()

static RzBinXtrData * extract ( RzBin bin,
int  idx 
)
static

Definition at line 72 of file bin_xtr_fatmach0.c.

72  {
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 }
RZ_API RzBinXtrData * rz_bin_xtrdata_new(RzBuffer *buf, ut64 offset, ut64 size, ut32 file_count, RzBinXtrMetadata *metadata)
Definition: bin.c:47
static void fill_metadata_info_from_hdr(RzBinXtrMetadata *meta, struct MACH0_(mach_header) *hdr)
#define NULL
Definition: cris-opc.c:27
cs_arch arch
Definition: cstool.c:13
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define MACH0_(name)
Definition: mach0_specs.h:20
int idx
Definition: setup.py:197
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
#define RZ_NEW0(x)
Definition: rz_types.h:284

References arch, fill_metadata_info_from_hdr(), free(), setup::idx, MACH0_, NULL, rz_bin_fatmach0_extract(), rz_bin_xtrdata_new(), rz_buf_free(), and RZ_NEW0.

◆ fill_metadata_info_from_hdr()

static void fill_metadata_info_from_hdr ( RzBinXtrMetadata meta,
struct MACH0_(mach_header) *  hdr 
)
inlinestatic

Definition at line 62 of file bin_xtr_fatmach0.c.

62  {
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 }
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
const char * xtr_type
Definition: rz_bin.h:374

References rz_bin_xtr_metadata_t::arch, rz_bin_xtr_metadata_t::bits, get_bits_from_hdr(), get_cpusubtype_from_hdr(), get_cputype_from_hdr(), get_filetype_from_hdr(), rz_bin_xtr_metadata_t::libname, MACH0_, rz_bin_xtr_metadata_t::machine, NULL, strdup(), rz_bin_xtr_metadata_t::type, and rz_bin_xtr_metadata_t::xtr_type.

Referenced by extract(), and oneshot_buffer().

◆ free_xtr()

static void free_xtr ( void *  xtr_obj)
static

Definition at line 45 of file bin_xtr_fatmach0.c.

45  {
47 }
void * rz_bin_fatmach0_free(struct rz_bin_fatmach0_obj_t *bin)
Definition: fatmach0.c:78

References rz_bin_fatmach0_free().

Referenced by destroy().

◆ load()

static bool load ( RzBin bin)
static

Definition at line 53 of file bin_xtr_fatmach0.c.

53  {
54  return ((bin->cur->xtr_obj = rz_bin_fatmach0_new(bin->file)) != NULL);
55 }
struct rz_bin_fatmach0_obj_t * rz_bin_fatmach0_new(const char *file)
Definition: fatmach0.c:88

References NULL, and rz_bin_fatmach0_new().

◆ oneshot_buffer()

static RzBinXtrData* oneshot_buffer ( RzBin bin,
RzBuffer b,
int  idx 
)
static

Definition at line 100 of file bin_xtr_fatmach0.c.

100  {
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 }
struct rz_bin_fatmach0_obj_t * rz_bin_fatmach0_from_buffer_new(RzBuffer *b)
Definition: fatmach0.c:112

References arch, b, fill_metadata_info_from_hdr(), free(), setup::idx, MACH0_, NULL, rz_bin_fatmach0_extract(), rz_bin_fatmach0_from_buffer_new(), rz_bin_xtrdata_new(), rz_buf_free(), RZ_NEW0, and rz_return_val_if_fail.

Referenced by oneshotall_buffer().

◆ oneshotall_buffer()

static RzList* oneshotall_buffer ( RzBin bin,
RzBuffer b 
)
static

Definition at line 128 of file bin_xtr_fatmach0.c.

128  {
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 }
lzma_index ** i
Definition: index.h:629
RZ_API void rz_bin_xtrdata_free(void *data_)
Definition: bin.c:61
static RzBinXtrData * oneshot_buffer(RzBin *bin, RzBuffer *b, int idx)
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

References b, rz_bin_xtr_extract_t::file_count, i, NULL, oneshot_buffer(), rz_bin_xtrdata_free(), rz_list_append(), and rz_list_newf().

◆ size()

static int size ( RzBin bin)
static

Definition at line 57 of file bin_xtr_fatmach0.c.

57  {
58  // TODO
59  return 0;
60 }

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.data = &rz_bin_xtr_plugin_fatmach0,
.version = RZ_VERSION
}
@ RZ_LIB_TYPE_BIN_XTR
Definition: rz_lib.h:76
#define RZ_VERSION
Definition: rz_version.h:8

Definition at line 166 of file bin_xtr_fatmach0.c.

◆ rz_bin_xtr_plugin_xtr_fatmach0

RzBinXtrPlugin rz_bin_xtr_plugin_xtr_fatmach0
Initial value:
= {
.name = "xtr.fatmach0",
.desc = "fat mach0 bin extractor plugin",
.license = "LGPL3",
.load = &load,
.size = &size,
.extract = &extract,
.destroy = &destroy,
.extract_from_buffer = &oneshot_buffer,
.extractall_from_buffer = &oneshotall_buffer,
.free_xtr = &free_xtr,
.check_buffer = check_buffer,
}
static bool check_buffer(RzBuffer *buf)
static RzBinXtrData * extract(RzBin *bin, int idx)
static RzList * oneshotall_buffer(RzBin *bin, RzBuffer *b)
static void destroy(RzBin *bin)
static int size(RzBin *bin)
static bool load(RzBin *bin)

Definition at line 151 of file bin_xtr_fatmach0.c.