Rizin
unix-like reverse engineering framework and cli tools
fatmach0.c File Reference
#include <stdio.h>
#include <rz_types.h>
#include <rz_util.h>
#include "fatmach0.h"

Go to the source code of this file.

Functions

static int rz_bin_fatmach0_init (struct rz_bin_fatmach0_obj_t *bin)
 
struct rz_bin_fatmach0_arch_trz_bin_fatmach0_extract (struct rz_bin_fatmach0_obj_t *bin, int idx, int *narch)
 
void * rz_bin_fatmach0_free (struct rz_bin_fatmach0_obj_t *bin)
 
struct rz_bin_fatmach0_obj_trz_bin_fatmach0_new (const char *file)
 
struct rz_bin_fatmach0_obj_trz_bin_fatmach0_from_buffer_new (RzBuffer *b)
 
struct rz_bin_fatmach0_obj_trz_bin_fatmach0_from_bytes_new (const ut8 *buf, ut64 size)
 

Function Documentation

◆ rz_bin_fatmach0_extract()

struct rz_bin_fatmach0_arch_t* rz_bin_fatmach0_extract ( struct rz_bin_fatmach0_obj_t bin,
int  idx,
int narch 
)

Definition at line 53 of file fatmach0.c.

53  {
54  if (!bin || (idx < 0) || (idx > bin->nfat_arch)) {
55  return NULL;
56  }
57  if (bin->archs[idx].offset > bin->size ||
58  bin->archs[idx].offset + bin->archs[idx].size > bin->size) {
59  return NULL;
60  }
61  if (narch) {
62  *narch = bin->nfat_arch;
63  }
65  if (ret) {
66  ret->size = bin->archs[idx].size;
67  if (!ret->size || ret->size > bin->size) {
68  eprintf("Skipping corrupted sub-bin %d arch %d\n", idx, bin->archs[idx].size);
69  free(ret);
70  return NULL;
71  }
72  ret->offset = bin->archs[idx].offset;
73  ret->b = rz_buf_new_slice(bin->b, ret->offset, ret->size);
74  }
75  return ret;
76 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int idx
Definition: setup.py:197
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API RZ_OWN RzBuffer * rz_buf_new_slice(RzBuffer *b, ut64 offset, ut64 size)
Creates a new buffer from a slice of another buffer.
Definition: buf.c:364
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: malloc.c:26

References rz_bin_fatmach0_arch_t::b, eprintf, free(), setup::idx, NULL, rz_bin_fatmach0_arch_t::offset, rz_buf_new_slice(), RZ_NEW0, and rz_bin_fatmach0_arch_t::size.

Referenced by extract(), and oneshot_buffer().

◆ rz_bin_fatmach0_free()

void* rz_bin_fatmach0_free ( struct rz_bin_fatmach0_obj_t bin)

Definition at line 78 of file fatmach0.c.

78  {
79  if (!bin) {
80  return NULL;
81  }
82  free(bin->archs);
83  rz_buf_free(bin->b);
84  RZ_FREE(bin);
85  return NULL;
86 }
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_FREE(x)
Definition: rz_types.h:369

References free(), NULL, rz_buf_free(), and RZ_FREE.

Referenced by free_xtr(), rz_bin_fatmach0_from_buffer_new(), rz_bin_fatmach0_from_bytes_new(), and rz_bin_fatmach0_new().

◆ rz_bin_fatmach0_from_buffer_new()

struct rz_bin_fatmach0_obj_t* rz_bin_fatmach0_from_buffer_new ( RzBuffer b)

Definition at line 112 of file fatmach0.c.

112  {
115  if (bo) {
116  bo->b = rz_buf_ref(b);
117  bo->size = rz_buf_size(bo->b); // XXX implicit in bo->b
118  if (!rz_bin_fatmach0_init(bo)) {
119  return rz_bin_fatmach0_free(bo);
120  }
121  }
122  return bo;
123 }
static int rz_bin_fatmach0_init(struct rz_bin_fatmach0_obj_t *bin)
Definition: fatmach0.c:9
void * rz_bin_fatmach0_free(struct rz_bin_fatmach0_obj_t *bin)
Definition: fatmach0.c:78
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
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

References rz_bin_fatmach0_obj_t::b, b, NULL, rz_bin_fatmach0_free(), rz_bin_fatmach0_init(), rz_buf_ref(), rz_buf_size(), RZ_NEW0, rz_return_val_if_fail, and rz_bin_fatmach0_obj_t::size.

Referenced by oneshot_buffer().

◆ rz_bin_fatmach0_from_bytes_new()

struct rz_bin_fatmach0_obj_t* rz_bin_fatmach0_from_bytes_new ( const ut8 buf,
ut64  size 
)

Definition at line 125 of file fatmach0.c.

125  {
127  if (!bin) {
128  return NULL;
129  }
130  if (!buf) {
131  return rz_bin_fatmach0_free(bin);
132  }
133  bin->b = rz_buf_new_with_bytes(NULL, 0);
134  bin->size = size;
135  if (!rz_buf_set_bytes(bin->b, buf, size)) {
136  return rz_bin_fatmach0_free(bin);
137  }
138  if (!rz_bin_fatmach0_init(bin)) {
139  return rz_bin_fatmach0_free(bin);
140  }
141  return bin;
142 }
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
RZ_API bool rz_buf_set_bytes(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 len)
Replace the content of the buffer with the bytes array.
Definition: buf.c:905
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_bytes(RZ_NULLABLE RZ_BORROW const ut8 *bytes, ut64 len)
Creates a new buffer with a bytes array.
Definition: buf.c:465

References NULL, rz_bin_fatmach0_free(), rz_bin_fatmach0_init(), rz_buf_new_with_bytes(), rz_buf_set_bytes(), and RZ_NEW0.

◆ rz_bin_fatmach0_init()

static int rz_bin_fatmach0_init ( struct rz_bin_fatmach0_obj_t bin)
static

Definition at line 9 of file fatmach0.c.

9  {
10  ut32 size;
11  ut32 i;
12  ut8 hdrbytes[sizeof(struct fat_header)] = { 0 };
13  int len = rz_buf_read_at(bin->b, 0, &hdrbytes[0], sizeof(struct fat_header));
14  if (len != sizeof(struct fat_header)) {
15  RZ_LOG_ERROR("Cannot read fat_header\n");
16  return false;
17  }
18  bin->hdr.magic = rz_read_be32(&hdrbytes[0]);
19  bin->hdr.nfat_arch = rz_read_be32(&hdrbytes[4]);
20  bin->nfat_arch = bin->hdr.nfat_arch;
21  if (sizeof(struct fat_header) + bin->nfat_arch * sizeof(struct fat_arch) > bin->size) {
22  return false;
23  }
24  if (bin->hdr.magic != FAT_MAGIC || !bin->nfat_arch || bin->nfat_arch < 1) {
25  RZ_LOG_ERROR("Invalid FAT_MAGIC\n");
26  return false;
27  }
28  size = bin->nfat_arch * sizeof(struct fat_arch);
29  if (size < bin->nfat_arch) {
30  return false;
31  }
32  if (!(bin->archs = malloc(size))) {
33  RZ_LOG_ERROR("Cannot allocate fat_arch with size %u bytes\n", size);
34  return false;
35  }
36  for (i = 0; i < bin->nfat_arch; i++) {
37  ut8 archbytes[sizeof(struct fat_arch)] = { 0 };
38  len = rz_buf_read_at(bin->b, 8 + i * sizeof(struct fat_arch), &archbytes[0], sizeof(struct fat_arch));
39  if (len != sizeof(struct fat_arch)) {
40  RZ_LOG_ERROR("Cannot read fat_arch\n");
41  RZ_FREE(bin->archs);
42  return false;
43  }
44  bin->archs[i].cputype = rz_read_be32(&archbytes[0]);
45  bin->archs[i].cpusubtype = rz_read_be32(&archbytes[4]);
46  bin->archs[i].offset = rz_read_be32(&archbytes[8]);
47  bin->archs[i].size = rz_read_be32(&archbytes[12]);
48  bin->archs[i].align = rz_read_be32(&archbytes[16]);
49  }
50  return true;
51 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
@ FAT_MAGIC
Definition: mach0_defines.h:65
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
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58

References FAT_MAGIC, i, len, malloc(), rz_buf_read_at(), RZ_FREE, RZ_LOG_ERROR, and rz_read_be32().

Referenced by rz_bin_fatmach0_from_buffer_new(), rz_bin_fatmach0_from_bytes_new(), and rz_bin_fatmach0_new().

◆ rz_bin_fatmach0_new()

struct rz_bin_fatmach0_obj_t* rz_bin_fatmach0_new ( const char *  file)

Definition at line 88 of file fatmach0.c.

88  {
90  if (!bin) {
91  return NULL;
92  }
93  bin->file = file;
94  size_t binsz;
95  ut8 *buf = (ut8 *)rz_file_slurp(file, &binsz);
96  bin->size = binsz;
97  if (!buf) {
98  return rz_bin_fatmach0_free(bin);
99  }
100  bin->b = rz_buf_new_with_bytes(NULL, 0);
101  if (!rz_buf_set_bytes(bin->b, buf, bin->size)) {
102  free(buf);
103  return rz_bin_fatmach0_free(bin);
104  }
105  free(buf);
106  if (!rz_bin_fatmach0_init(bin)) {
107  return rz_bin_fatmach0_free(bin);
108  }
109  return bin;
110 }
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
Definition: gzappend.c:170
static int file
Definition: z80asm.c:58

References file, free(), NULL, rz_bin_fatmach0_free(), rz_bin_fatmach0_init(), rz_buf_new_with_bytes(), rz_buf_set_bytes(), rz_file_slurp(), and RZ_NEW0.

Referenced by load().