Rizin
unix-like reverse engineering framework and cli tools
fatmach0.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2010-2013 nibble <nibble.ds@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <stdio.h>
5 #include <rz_types.h>
6 #include <rz_util.h>
7 #include "fatmach0.h"
8 
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 }
52 
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 }
77 
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 }
87 
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 }
111 
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 }
124 
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
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_bytes_new(const ut8 *buf, ut64 size)
Definition: fatmach0.c:125
static int rz_bin_fatmach0_init(struct rz_bin_fatmach0_obj_t *bin)
Definition: fatmach0.c:9
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 uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
@ FAT_MAGIC
Definition: mach0_defines.h:65
int idx
Definition: setup.py:197
#define eprintf(x, y...)
Definition: rlcc.c:7
#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 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
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 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
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
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define b(i)
Definition: sha256.c:42
Definition: malloc.c:26
Definition: gzappend.c:170
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int file
Definition: z80asm.c:58