1 #ifndef R2_LIB_H
2 #define R2_LIB_H
3 
4 #include "r_types.h"
5 #include "r_list.h"
6 
7 #if __UNIX__
8 #include <dlfcn.h>
9 #endif
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 R_LIB_VERSION_HEADER (r_lib);
16 
17 // rename to '.' ??
18 #define R_LIB_SEPARATOR "."
19 #define R_LIB_SYMNAME "radare_plugin"
20 #define R_LIB_SYMFUNC "radare_plugin_function"
21 
22 #define R_LIB_ENV "R2_LIBR_PLUGINS"
23 
24 /* TODO: This must depend on HOST_OS, and maybe move into r_types */
25 #if __WINDOWS__
26 #include <windows.h>
27 #define R_LIB_EXT "dll"
28 #elif __APPLE__
29 #define R_LIB_EXT "dylib"
30 #else
31 #define R_LIB_EXT "so"
32 #endif
33 
34 /* store list of loaded plugins */
35 typedef struct r_lib_plugin_t {
36 	int type;
37 	char *file;
38 	void *data; /* user pointer */
39 	struct r_lib_handler_t *handler;
40 	void *dl_handler; // DL HANDLER
41 	char *author;
42 	char *version;
43 	void (*free)(void *data);
44 } RLibPlugin;
45 
46 /* store list of initialized plugin handlers */
47 typedef struct r_lib_handler_t {
48 	int type;
49 	char desc[128]; // TODO: use char *
50 	void *user; /* user pointer */
51 	int (*constructor)(RLibPlugin *, void *user, void *data);
52 	int (*destructor)(RLibPlugin *, void *user, void *data);
53 } RLibHandler;
54 
55 /* this structure should be pointed by the 'radare_plugin' symbol
56    found in the loaded .so */
57 typedef struct r_lib_struct_t {
58 	int type;
59 	void *data; /* pointer to data handled by plugin handler */
60 	const char *version; /* r2 version */
61 	void (*free)(void *data);
62 	const char *pkgname; /* pkgname associated to this plugin */
63 } RLibStruct;
64 
65 typedef RLibStruct* (*RLibStructFunc) (void);
66 
67 // order matters because of libr/util/lib.c
68 enum {
69 	R_LIB_TYPE_IO,      /* io layer */
70 	R_LIB_TYPE_DBG,     /* debugger */
71 	R_LIB_TYPE_LANG,    /* language */
72 	R_LIB_TYPE_ASM,     /* assembler */
73 	R_LIB_TYPE_ANAL,    /* analysis */
74 	R_LIB_TYPE_PARSE,   /* parsers */
75 	R_LIB_TYPE_BIN,     /* bin headers */
76 	R_LIB_TYPE_BIN_XTR, /* bin extractors */
77 	R_LIB_TYPE_BIN_LDR, /* bin loaders */
78 	R_LIB_TYPE_BP,      /* breakpoint */
79 	R_LIB_TYPE_SYSCALL, /* syscall */
80 	R_LIB_TYPE_FASTCALL,/* fastcall */
81 	R_LIB_TYPE_CRYPTO,  /* cryptography */
82 	R_LIB_TYPE_CORE,    /* RCore commands */
83 	R_LIB_TYPE_EGG,     /* r_egg plugin */
84 	R_LIB_TYPE_FS,      /* r_fs plugin */
85 	R_LIB_TYPE_ESIL,    /* r_anal.esil plugin */
86 	R_LIB_TYPE_LAST
87 };
88 
89 typedef struct r_lib_t {
90 	/* linked list with all the plugin handler */
91 	/* only one handler per handler-id allowed */
92 	/* this is checked in add_handler function */
93 	char *symname;
94 	char *symnamefunc;
95 	RList /*RLibPlugin*/ *plugins;
96 	RList /*RLibHandler*/ *handlers;
97 } RLib;
98 
99 #ifdef R_API
100 /* low level api */
101 R_API void *r_lib_dl_open(const char *libname);
102 
103 R_API void *r_lib_dl_sym(void *handler, const char *name);
104 R_API int r_lib_dl_close(void *handler);
105 
106 /* high level api */
107 typedef int (*RLibCallback)(RLibPlugin *, void *, void *);
108 R_API RLib *r_lib_new(const char *symname, const char *symnamefunc);
109 R_API void r_lib_free(RLib *lib);
110 R_API int r_lib_run_handler(RLib *lib, RLibPlugin *plugin, RLibStruct *symbol);
111 R_API RLibHandler *r_lib_get_handler(RLib *lib, int type);
112 R_API int r_lib_open(RLib *lib, const char *file);
113 R_API bool r_lib_opendir(RLib *lib, const char *path);
114 R_API int r_lib_open_ptr (RLib *lib, const char *file, void *handler, RLibStruct *stru);
115 R_API char *r_lib_path(const char *libname);
116 R_API void r_lib_list(RLib *lib);
117 R_API bool r_lib_add_handler(RLib *lib, int type, const char *desc, RLibCallback ct, RLibCallback dt, void *user);
118 R_API bool r_lib_del_handler(RLib *lib, int type);
119 R_API int r_lib_close(RLib *lib, const char *file);
120 
121 R_API const char *r_lib_types_get(int idx);
122 R_API int r_lib_types_get_i(const char *str);
123 #endif
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif
130