1 /* radare - LGPL - Copyright 2009-2018 - pancake, nibble */
2 
3 #ifndef R2_PARSE_H
4 #define R2_PARSE_H
5 
6 #include <r_types.h>
7 #include <r_flag.h>
8 #include <r_anal.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 R_LIB_VERSION_HEADER(r_parse);
15 
16 typedef RList* (*RAnalVarList)(RAnalFunction *fcn, int kind);
17 
18 typedef struct r_parse_t {
19 	void *user;
20 	RSpace *flagspace;
21 	RSpace *notin_flagspace;
22 	bool pseudo;
23 	bool subreg; // replace registers with their respective alias/role name (rdi=A0, ...)
24 	bool subrel; // replace rip relative expressions in instruction
25 	bool subtail; // replace any immediate relative to current address with .. prefix syntax
26 	bool localvar_only; // if true use only the local variable name (e.g. [local_10h] instead of [ebp + local10h])
27 	ut64 subrel_addr;
28 	int maxflagnamelen;
29 	int minval;
30 	char *retleave_asm;
31 	struct r_parse_plugin_t *cur;
32 	// RAnal *anal; // weak anal ref XXX do not use. use analb.anal
33 	RList *parsers;
34 	RAnalVarList varlist;
35 	st64 (*get_ptr_at)(RAnalFunction *fcn, st64 delta, ut64 addr);
36 	const char *(*get_reg_at)(RAnalFunction *fcn, st64 delta, ut64 addr);
37 	char* (*get_op_ireg)(void *user, ut64 addr);
38 	RAnalBind analb;
39 	RFlagGetAtAddr flag_get; // XXX
40 	RAnalLabelAt label_get;
41 } RParse;
42 
43 typedef struct r_parse_plugin_t {
44 	char *name;
45 	char *desc;
46 	bool (*init)(RParse *p, void *user);
47 	int (*fini)(RParse *p, void *user);
48 	int (*parse)(RParse *p, const char *data, char *str);
49 	bool (*assemble)(RParse *p, char *data, char *str);
50 	int (*filter)(RParse *p, ut64 addr, RFlag *f, char *data, char *str, int len, bool big_endian);
51 	bool (*subvar)(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len);
52 	int (*replace)(int argc, const char *argv[], char *newstr);
53 } RParsePlugin;
54 
55 #ifdef R_API
56 
57 /* lifecycle */
58 R_API struct r_parse_t *r_parse_new(void);
59 R_API void r_parse_free(RParse *p);
60 
61 /* plugins */
62 R_API void r_parse_set_user_ptr(RParse *p, void *user);
63 R_API bool r_parse_add(RParse *p, RParsePlugin *foo);
64 R_API bool r_parse_use(RParse *p, const char *name);
65 
66 /* action */
67 R_API bool r_parse_parse(RParse *p, const char *data, char *str);
68 R_API bool r_parse_assemble(RParse *p, char *data, char *str); // XXX deprecate, unused and probably useless, related to write-hack
69 R_API bool r_parse_filter(RParse *p, ut64 addr, RFlag *f, RAnalHint *hint, char *data, char *str, int len, bool big_endian);
70 R_API bool r_parse_subvar(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len);
71 R_API char *r_parse_immtrim(char *opstr);
72 
73 /* c */
74 // why we have anal scoped things in rparse
75 R_API char *r_parse_c_string(RAnal *anal, const char *code, char **error_msg);
76 R_API char *r_parse_c_file(RAnal *anal, const char *path, const char *dir, char **error_msg);
77 R_API void r_parse_c_reset(RParse *p);
78 
79 /* ctype */
80 // Parses strings like "const char * [0x42] const * [23]" to RParseCTypeType
81 
82 typedef struct r_parse_ctype_t RParseCType;
83 
84 typedef enum {
85 	R_PARSE_CTYPE_TYPE_KIND_IDENTIFIER,
86 	R_PARSE_CTYPE_TYPE_KIND_POINTER,
87 	R_PARSE_CTYPE_TYPE_KIND_ARRAY
88 } RParseCTypeTypeKind;
89 
90 typedef enum {
91 	R_PARSE_CTYPE_IDENTIFIER_KIND_UNSPECIFIED,
92 	R_PARSE_CTYPE_IDENTIFIER_KIND_STRUCT,
93 	R_PARSE_CTYPE_IDENTIFIER_KIND_UNION,
94 	R_PARSE_CTYPE_IDENTIFIER_KIND_ENUM
95 } RParseCTypeTypeIdentifierKind;
96 
97 typedef struct r_parse_ctype_type_t RParseCTypeType;
98 struct r_parse_ctype_type_t {
99 	RParseCTypeTypeKind kind;
100 	union {
101 		struct {
102 			RParseCTypeTypeIdentifierKind kind;
103 			char *name;
104 			bool is_const;
105 		} identifier;
106 		struct {
107 			RParseCTypeType *type;
108 			bool is_const;
109 		} pointer;
110 		struct {
111 			RParseCTypeType *type;
112 			ut64 count;
113 		} array;
114 	};
115 };
116 
117 R_API RParseCType *r_parse_ctype_new(void);
118 R_API void r_parse_ctype_free(RParseCType *ctype);
119 R_API RParseCTypeType *r_parse_ctype_parse(RParseCType *ctype, const char *str, char **error);
120 R_API void r_parse_ctype_type_free(RParseCTypeType *type);
121 
122 /* plugin pointers */
123 extern RParsePlugin r_parse_plugin_6502_pseudo;
124 extern RParsePlugin r_parse_plugin_arm_pseudo;
125 extern RParsePlugin r_parse_plugin_att2intel;
126 extern RParsePlugin r_parse_plugin_avr_pseudo;
127 extern RParsePlugin r_parse_plugin_chip8_pseudo;
128 extern RParsePlugin r_parse_plugin_dalvik_pseudo;
129 extern RParsePlugin r_parse_plugin_dummy;
130 extern RParsePlugin r_parse_plugin_m68k_pseudo;
131 extern RParsePlugin r_parse_plugin_mips_pseudo;
132 extern RParsePlugin r_parse_plugin_ppc_pseudo;
133 extern RParsePlugin r_parse_plugin_sh_pseudo;
134 extern RParsePlugin r_parse_plugin_wasm_pseudo;
135 extern RParsePlugin r_parse_plugin_riscv_pseudo;
136 extern RParsePlugin r_parse_plugin_x86_pseudo;
137 extern RParsePlugin r_parse_plugin_z80_pseudo;
138 extern RParsePlugin r_parse_plugin_tms320_pseudo;
139 extern RParsePlugin r_parse_plugin_v850_pseudo;
140 #endif
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif
147