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

Go to the source code of this file.

Classes

struct  rz_bin_fatmach0_obj_t
 
struct  rz_bin_fatmach0_arch_t
 

Functions

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_bytes_new (const ut8 *buf, ut64 size)
 
struct rz_bin_fatmach0_obj_trz_bin_fatmach0_from_buffer_new (RzBuffer *b)
 

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_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 }
uint8_t ut8
Definition: lh5801.h:11
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().