Rizin
unix-like reverse engineering framework and cli tools
parse_chip8_pseudo.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2019 Vasilij Schneidermann <mail@vasilij.de>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_lib.h>
5 #include <rz_util.h>
6 #include <rz_flag.h>
7 #include <rz_analysis.h>
8 #include <rz_parse.h>
9 
10 #define MAXARGS 4
11 #define BUFSIZE 64
12 
13 static void concat(char *buf, size_t len, char **args) {
14  char *arg;
15  char *dest = buf;
16  int arg_len;
17 
18  while ((arg = *args++)) {
19  if (snprintf(dest, len, "%s", arg) >= len) {
20  break;
21  }
22  arg_len = strlen(arg);
23  dest += arg_len;
24  len -= arg_len;
25  }
26 }
27 
28 static int replace(int argc, char *argv[], char *newstr, size_t len) {
29  int i;
30  struct {
31  char *op;
32  char **res;
33  } ops[] = {
34  { "add", (char *[]){ argv[1], " += ", argv[2], NULL } },
35  { "and", (char *[]){ argv[1], " &= ", argv[2], NULL } },
36  { "cls", (char *[]){ "clear_screen()", NULL } },
37  { "drw", (char *[]){ "draw(", argv[1], ", ", argv[2], ", ", argv[3], ")", NULL } },
38  { "exit", (char *[]){ "exit()", NULL } },
39  { "high", (char *[]){ "high_res()", NULL } },
40  { "jp", (char *[]){ "goto ", argv[1], NULL } },
41  { "ld", (char *[]){ argv[1], " = ", argv[2], NULL } },
42  { "low", (char *[]){ "low_res()", NULL } },
43  { "or", (char *[]){ argv[1], " |= ", argv[2], NULL } },
44  { "rnd", (char *[]){ argv[1], " = random(256) & ", argv[2], NULL } },
45  { "scd", (char *[]){ "scroll_down(", argv[1], ")", NULL } },
46  { "scl", (char *[]){ "scroll_left()", NULL } },
47  { "scr", (char *[]){ "scroll_right()", NULL } },
48  { "se", (char *[]){ "skip_next_instr if ", argv[1], " == ", argv[2], NULL } },
49  { "shl", (char *[]){ argv[1], " <<= 1", NULL } },
50  { "shr", (char *[]){ argv[1], " >>= 1", NULL } },
51  { "sknp", (char *[]){ "skip_next_instr if !key_pressed(", argv[1], ")", NULL } },
52  { "skp", (char *[]){ "skip_next_instr if key_pressed(", argv[1], ")", NULL } },
53  { "sne", (char *[]){ "skip_next_instr if ", argv[1], " != ", argv[2], NULL } },
54  { "sub", (char *[]){ argv[1], " -= ", argv[2], NULL } },
55  { "subn", (char *[]){ argv[1], " = ", argv[2], " - ", argv[1], NULL } },
56  { "xor", (char *[]){ argv[1], " ^= ", argv[2], NULL } },
57  { NULL }
58  };
59 
60  for (i = 0; ops[i].op; i++) {
61  if (!strcmp(ops[i].op, argv[0]) && newstr) {
62  concat(newstr, len, ops[i].res);
63  return true;
64  }
65  }
66 
67  return false;
68 }
69 
70 static int tokenize(const char *in, char *out[]) {
71  int len = strlen(in), count = 0, i = 0, tokenlen = 0, seplen = 0;
72  char *token, *buf = (char *)in;
73  const char *tokcharset = ", \t\n";
74 
75  while (i < len) {
76  tokenlen = strcspn(buf, tokcharset);
77  token = calloc(tokenlen + 1, sizeof(char));
78  memcpy(token, buf, tokenlen);
79  out[count] = token;
80  i += tokenlen;
81  buf += tokenlen;
82  count++;
83 
84  seplen = strspn(buf, tokcharset);
85  i += seplen;
86  buf += seplen;
87  }
88 
89  return count;
90 }
91 
92 static bool parse(RzParse *p, const char *data, RzStrBuf *sb) {
93  int i;
94  char str[1024] = { 0 };
95  char *argv[MAXARGS] = { NULL, NULL, NULL, NULL };
96  int argc = tokenize(data, argv);
97 
98  if (!replace(argc, argv, str, BUFSIZE)) {
99  strcpy(str, data);
100  }
101 
102  for (i = 0; i < MAXARGS; i++) {
103  free(argv[i]);
104  }
105  rz_strbuf_set(sb, str);
106 
107  return true;
108 }
109 
111  .name = "chip8.pseudo",
112  .desc = "chip8 pseudo syntax",
113  .parse = parse,
114 };
115 
116 #ifndef RZ_PLUGIN_INCORE
120  .version = RZ_VERSION
121 };
122 #endif
size_t len
Definition: 6502dis.c:15
static struct @29 ops[]
ut8 op
Definition: 6502dis.c:13
lzma_index ** i
Definition: index.h:629
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
static RASN1String * newstr(const char *string)
Definition: astr.c:23
static SblHeader sb
Definition: bin_mbn.c:26
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
char * dest
Definition: lz4.h:697
int args
Definition: mipsasm.c:18
static bool parse(RzParse *p, const char *data, RzStrBuf *sb)
static int replace(int argc, char *argv[], char *newstr, size_t len)
#define MAXARGS
RZ_API RzLibStruct rizin_plugin
static void concat(char *buf, size_t len, char **args)
#define BUFSIZE
RzParsePlugin rz_parse_plugin_chip8_pseudo
static int tokenize(const char *in, char *out[])
@ RZ_LIB_TYPE_PARSE
Definition: rz_lib.h:74
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
#define RZ_VERSION
Definition: rz_version.h:8
Definition: dis.c:32