Rizin
unix-like reverse engineering framework and cli tools
parse_chip8_pseudo.c File Reference
#include <rz_lib.h>
#include <rz_util.h>
#include <rz_flag.h>
#include <rz_analysis.h>
#include <rz_parse.h>

Go to the source code of this file.

Macros

#define MAXARGS   4
 
#define BUFSIZE   64
 

Functions

static void concat (char *buf, size_t len, char **args)
 
static int replace (int argc, char *argv[], char *newstr, size_t len)
 
static int tokenize (const char *in, char *out[])
 
static bool parse (RzParse *p, const char *data, RzStrBuf *sb)
 

Variables

RzParsePlugin rz_parse_plugin_chip8_pseudo
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ BUFSIZE

#define BUFSIZE   64

Definition at line 11 of file parse_chip8_pseudo.c.

◆ MAXARGS

#define MAXARGS   4

Definition at line 10 of file parse_chip8_pseudo.c.

Function Documentation

◆ concat()

static void concat ( char *  buf,
size_t  len,
char **  args 
)
static

Definition at line 13 of file parse_chip8_pseudo.c.

13  {
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 }
size_t len
Definition: 6502dis.c:15
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
char * dest
Definition: lz4.h:697
int args
Definition: mipsasm.c:18

References arg(), args, dest, len, and snprintf.

Referenced by replace().

◆ parse()

static bool parse ( RzParse p,
const char *  data,
RzStrBuf sb 
)
static

Definition at line 92 of file parse_chip8_pseudo.c.

92  {
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 }
lzma_index ** i
Definition: index.h:629
static SblHeader sb
Definition: bin_mbn.c:26
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static int replace(int argc, char *argv[], char *newstr, size_t len)
#define MAXARGS
#define BUFSIZE
static int tokenize(const char *in, char *out[])
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153

References argv, BUFSIZE, free(), i, MAXARGS, NULL, replace(), rz_strbuf_set(), sb, cmd_descs_generate::str, and tokenize().

◆ replace()

static int replace ( int  argc,
char *  argv[],
char *  newstr,
size_t  len 
)
static

Definition at line 28 of file parse_chip8_pseudo.c.

28  {
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 }
static struct @29 ops[]
ut8 op
Definition: 6502dis.c:13
static RASN1String * newstr(const char *string)
Definition: astr.c:23
static void concat(char *buf, size_t len, char **args)
Definition: dis.c:32

References argv, concat(), i, len, newstr(), NULL, op, and ops.

Referenced by parse().

◆ tokenize()

static int tokenize ( const char *  in,
char *  out[] 
)
static

Definition at line 70 of file parse_chip8_pseudo.c.

70  {
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 }
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
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
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

References calloc(), count, i, in, len, memcpy(), and out.

Referenced by parse().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.version = RZ_VERSION
}
RzParsePlugin rz_parse_plugin_chip8_pseudo
@ RZ_LIB_TYPE_PARSE
Definition: rz_lib.h:74
#define RZ_VERSION
Definition: rz_version.h:8

Definition at line 117 of file parse_chip8_pseudo.c.

◆ rz_parse_plugin_chip8_pseudo

RzParsePlugin rz_parse_plugin_chip8_pseudo
Initial value:
= {
.name = "chip8.pseudo",
.desc = "chip8 pseudo syntax",
.parse = parse,
}
static bool parse(RzParse *p, const char *data, RzStrBuf *sb)

Definition at line 110 of file parse_chip8_pseudo.c.