Rizin
unix-like reverse engineering framework and cli tools
bin_bf.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2013-2019 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 #include <rz_bin.h>
8 
9 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb) {
10  return true;
11 }
12 
13 static void destroy(RzBinFile *bf) {
14  rz_buf_free(bf->o->bin_obj);
15 }
16 
17 static ut64 baddr(RzBinFile *bf) {
18  return 0;
19 }
20 
21 static RzList *strings(RzBinFile *bf) {
22  return NULL;
23 }
24 
25 static RzBinInfo *info(RzBinFile *bf) {
26  RzBinInfo *ret = NULL;
27  if (!(ret = RZ_NEW0(RzBinInfo))) {
28  return NULL;
29  }
30  ret->lang = NULL;
31  ret->file = bf->file ? strdup(bf->file) : NULL;
32  ret->type = strdup("brainfuck");
33  ret->bclass = strdup("1.0");
34  ret->rclass = strdup("program");
35  ret->os = strdup("any");
36  ret->subsystem = strdup("unknown");
37  ret->machine = strdup("brainfuck");
38  ret->arch = strdup("bf");
39  ret->has_va = 1;
40  ret->bits = 64; // RzIL emulation of bf uses 64bit values
41  ret->big_endian = 0;
42  ret->dbg_info = 0;
43 
44  RZ_LOG_INFO("Brainfuck debugger setup:\n"
45  "f input 128 @ 0x3000\n"
46  "o malloc://128 0x3000\n"
47  "f screen 80*25 @ 0x4000\n"
48  "o malloc://80*25 0x4000\n"
49  "f stack 0x200 @ 0x5000\n"
50  "o malloc://0x200 0x5000\n"
51  "f data 0x1000 @ 0x6000\n"
52  "o malloc://0x1000 0x6000\n"
53  "ar\n" // hack to init
54  "ar brk=stack\n"
55  "ar scr=screen\n"
56  "ar kbd=input\n"
57  "ar ptr=data\n"
58  "e cmd.vprompt=\"pxa 32@stack;pxa 32@screen;pxa 32@data\"\n"
59  "s 0\n"
60  "e asm.bits=32\n"
61  "dL bf\n");
62  return ret;
63 }
64 
65 static bool check_buffer(RzBuffer *buf) {
66  rz_return_val_if_fail(buf, false);
67 
68  ut8 tmp[16];
69  int read_length = rz_buf_read_at(buf, 0, tmp, sizeof(tmp));
70  if (read_length <= 0) {
71  return false;
72  }
73 
74  const ut8 *p = (const ut8 *)tmp;
75  int i;
76  for (i = 0; i < read_length; i++) {
77  switch (p[i]) {
78  case '+':
79  case '-':
80  case '>':
81  case '<':
82  case '[':
83  case ']':
84  case ',':
85  case '.':
86  case ' ':
87  case '\n':
88  case '\r':
89  break;
90  default:
91  return false;
92  }
93  }
94  return true;
95 }
96 
97 static bool check_filename(const char *filename) {
98  return rz_str_endswith_icase(filename, ".bf");
99 }
100 
101 static RzList *entries(RzBinFile *bf) {
102  RzList *ret;
103  RzBinAddr *ptr = NULL;
104 
105  if (!(ret = rz_list_newf(free))) {
106  return NULL;
107  }
108  if (!(ptr = RZ_NEW0(RzBinAddr))) {
109  return ret;
110  }
111  ptr->paddr = ptr->vaddr = 0;
112  rz_list_append(ret, ptr);
113  return ret;
114 }
115 
116 static RzList *maps(RzBinFile *bf) {
118  if (!ret) {
119  return NULL;
120  }
121 
123  if (!map) {
124  rz_list_free(ret);
125  return NULL;
126  }
127  map->paddr = 0;
128  map->vaddr = 0;
129  map->psize = bf->size;
130  map->vsize = bf->size;
131  map->perm = RZ_PERM_RWX;
132  map->name = strdup("code");
133  rz_list_append(ret, map);
134 
135  map = RZ_NEW0(RzBinMap);
136  if (!map) {
137  rz_list_free(ret);
138  return NULL;
139  }
140  map->paddr = 0;
141  map->vaddr = 0x10000;
142  map->psize = 0;
143  map->vsize = 30000;
144  map->perm = RZ_PERM_RW;
145  map->name = strdup("mem");
146  rz_list_append(ret, map);
147  return ret;
148 }
149 
151  .name = "bf",
152  .desc = "brainfuck",
153  .license = "LGPL3",
154  .load_buffer = &load_buffer,
155  .destroy = &destroy,
156  .check_buffer = &check_buffer,
157  .check_filename = &check_filename,
158  .baddr = &baddr,
159  .entries = entries,
160  .strings = &strings,
161  .maps = &maps,
162  .info = &info,
163 };
164 
165 #ifndef RZ_PLUGIN_INCORE
168  .data = &rz_bin_plugin_bf,
170 };
171 #endif
lzma_index ** i
Definition: index.h:629
RZ_API void rz_bin_map_free(RzBinMap *map)
Definition: bin.c:1023
static bool check_buffer(RzBuffer *buf)
Definition: bin_bf.c:65
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_bf.c:9
static RzList * strings(RzBinFile *bf)
Definition: bin_bf.c:21
static void destroy(RzBinFile *bf)
Definition: bin_bf.c:13
RZ_API RzLibStruct rizin_plugin
Definition: bin_bf.c:166
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_bf.c:25
static ut64 baddr(RzBinFile *bf)
Definition: bin_bf.c:17
RzBinPlugin rz_bin_plugin_bf
Definition: bin_bf.c:150
static RzList * entries(RzBinFile *bf)
Definition: bin_bf.c:101
static RzList * maps(RzBinFile *bf)
Definition: bin_bf.c:116
static bool check_filename(const char *filename)
Definition: bin_bf.c:97
#define RZ_API
#define NULL
Definition: cris-opc.c:27
size_t map(int syms, int left, int len)
Definition: enough.c:237
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char * filename
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
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
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
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")
#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
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_LOG_INFO(fmtstr,...)
Definition: rz_log.h:54
RZ_API bool rz_str_endswith_icase(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string ends with a specifc sequence of characters (case insensitive)
Definition: str.c:3340
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_PERM_RW
Definition: rz_types.h:96
#define RZ_PERM_RWX
Definition: rz_types.h:98
#define RZ_VERSION
Definition: rz_version.h:8
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
XX curplugin == o->plugin.
Definition: rz_bin.h:298
RzBinObject * o
Definition: rz_bin.h:305
char * file
Definition: rz_bin.h:299
int has_va
Definition: rz_bin.h:228
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * subsystem
Definition: rz_bin.h:220
char * machine
Definition: rz_bin.h:216
const char * lang
Definition: rz_bin.h:224
char * bclass
Definition: rz_bin.h:212
char * file
Definition: rz_bin.h:210
ut64 dbg_info
Definition: rz_bin.h:240
char * rclass
Definition: rz_bin.h:213
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235
Description of a single memory mapping into virtual memory from a binary.
Definition: rz_bin.h:602
void * bin_obj
Definition: rz_bin.h:293
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()