1 #ifndef R2_SIGN_H
2 #define R2_SIGN_H
3 
4 #include <r_types.h>
5 #include <r_anal.h>
6 #include <r_search.h>
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 R_LIB_VERSION_HEADER(r_sign);
13 
14 // XXX those limits should go away
15 #define R_SIGN_KEY_MAXSZ 1024
16 #define R_SIGN_VAL_MAXSZ 10240
17 
18 #define ZIGN_HASH "sha256"
19 #define R_ZIGN_HASH R_HASH_SHA256
20 
21 typedef enum {
22 	R_SIGN_BYTES     = 'b', // bytes pattern
23 	R_SIGN_BYTES_MASK= 'm', // bytes pattern
24 	R_SIGN_BYTES_SIZE= 's', // bytes pattern
25 	R_SIGN_ANAL      = 'a', // bytes pattern (anal mask) // wtf ?
26 	R_SIGN_COMMENT   = 'c', // comment
27 	R_SIGN_GRAPH     = 'g', // graph metrics
28 	R_SIGN_OFFSET    = 'o', // addr
29 	R_SIGN_NAME      = 'n', // real name
30 	R_SIGN_REFS      = 'r', // references
31 	R_SIGN_XREFS     = 'x', // xrefs
32 	R_SIGN_VARS      = 'v', // variables
33 	R_SIGN_TYPES     = 't', // types
34 	R_SIGN_BBHASH    = 'h', // basic block hash
35 } RSignType;
36 
37 typedef struct r_sign_graph_t {
38 	int cc;
39 	int nbbs;
40 	int edges;
41 	int ebbs;
42 	int bbsum;
43 } RSignGraph;
44 
45 typedef struct r_sign_bytes_t {
46 	int size;
47 	ut8 *bytes;
48 	ut8 *mask;
49 } RSignBytes;
50 
51 typedef struct r_sign_hash_t {
52 	char *bbhash;
53 } RSignHash;
54 
55 typedef struct r_sign_item_t {
56 	char *name;
57 	char *realname;
58 	char *comment;
59 	const RSpace *space;
60 
61 	RSignBytes *bytes;
62 	RSignGraph *graph;
63 	ut64 addr;
64 	RList *refs;
65 	RList *xrefs;
66 	RList *vars;
67 	RList *types;
68 	RSignHash *hash;
69 } RSignItem;
70 
71 typedef int (*RSignForeachCallback)(RSignItem *it, void *user);
72 typedef int (*RSignSearchCallback)(RSignItem *it, RSearchKeyword *kw, ut64 addr, void *user);
73 typedef int (*RSignMatchCallback)(RSignItem *it, RAnalFunction *fcn, RSignType type, bool seen, void *user);
74 
75 typedef struct r_sign_search_met {
76 	/* types is an 0 terminated array of RSignTypes that are going to be
77 	 * searched for. Valid types are: graph, offset, refs, bbhash, types, vars
78 	 */
79 	RSignType types[7];
80 	int mincc; // min complexity for graph search
81 	RAnal *anal;
82 	void *user; // user data for callback function
83 	RSignMatchCallback cb;
84 	RAnalFunction *fcn;
85 } RSignSearchMetrics;
86 
87 typedef struct r_sign_search_t {
88 	RSearch *search;
89 	RList *items;
90 	RSignSearchCallback cb;
91 	void *user;
92 } RSignSearch;
93 
94 typedef struct r_sign_options_t {
95 	double bytes_diff_threshold;
96 	double graph_diff_threshold;
97 } RSignOptions;
98 
99 typedef struct {
100 	double score;
101 	double bscore;
102 	double gscore;
103 	RSignItem *item;
104 } RSignCloseMatch;
105 
106 #ifdef R_API
107 R_API bool r_sign_add_bytes(RAnal *a, const char *name, ut64 size, const ut8 *bytes, const ut8 *mask);
108 R_API bool r_sign_add_anal(RAnal *a, const char *name, ut64 size, const ut8 *bytes, ut64 at);
109 R_API bool r_sign_add_graph(RAnal *a, const char *name, RSignGraph graph);
110 R_API bool r_sign_addto_item(RAnal *a, RSignItem *it, RAnalFunction *fcn, RSignType type);
111 R_API bool r_sign_add_addr(RAnal *a, const char *name, ut64 addr);
112 R_API bool r_sign_add_name(RAnal *a, const char *name, const char *realname);
113 R_API bool r_sign_add_comment(RAnal *a, const char *name, const char *comment);
114 R_API bool r_sign_add_refs(RAnal *a, const char *name, RList *refs);
115 R_API bool r_sign_add_xrefs(RAnal *a, const char *name, RList *xrefs);
116 R_API bool r_sign_add_vars(RAnal *a, const char *name, RList *vars);
117 R_API bool r_sign_add_types(RAnal *a, const char *name, RList *vars);
118 R_API bool r_sign_delete(RAnal *a, const char *name);
119 R_API void r_sign_list(RAnal *a, int format);
120 R_API RList *r_sign_get_list(RAnal *a);
121 R_API bool r_sign_add_hash(RAnal *a, const char *name, int type, const char *val, int len);
122 R_API bool r_sign_add_bb_hash(RAnal *a, RAnalFunction *fcn, const char *name);
123 R_API char *r_sign_calc_bbhash(RAnal *a, RAnalFunction *fcn);
124 R_API bool r_sign_deserialize(RAnal *a, RSignItem *it, const char *k, const char *v);
125 R_API RSignItem *r_sign_get_item(RAnal *a, const char *name);
126 R_API bool r_sign_add_item(RAnal *a, RSignItem *it);
127 
128 R_API bool r_sign_foreach(RAnal *a, RSignForeachCallback cb, void *user);
129 
130 R_API RSignSearch *r_sign_search_new(void);
131 R_API void r_sign_search_free(RSignSearch *ss);
132 R_API void r_sign_search_init(RAnal *a, RSignSearch *ss, int minsz, RSignSearchCallback cb, void *user);
133 R_API int r_sign_search_update(RAnal *a, RSignSearch *ss, ut64 *at, const ut8 *buf, int len);
134 R_API int r_sign_fcn_match_metrics(RSignSearchMetrics *sm);
135 
136 R_API bool r_sign_load(RAnal *a, const char *file);
137 R_API bool r_sign_load_gz(RAnal *a, const char *filename);
138 R_API char *r_sign_path(RAnal *a, const char *file);
139 R_API bool r_sign_save(RAnal *a, const char *file);
140 
141 R_API RSignItem *r_sign_item_new(void);
142 R_API void r_sign_item_free(RSignItem *item);
143 R_API void r_sign_graph_free(RSignGraph *graph);
144 R_API void r_sign_bytes_free(RSignBytes *bytes);
145 
146 R_API RList *r_sign_fcn_refs(RAnal *a, RAnalFunction *fcn);
147 R_API RList *r_sign_fcn_xrefs(RAnal *a, RAnalFunction *fcn);
148 R_API RList *r_sign_fcn_vars(RAnal *a, RAnalFunction *fcn);
149 R_API RList *r_sign_fcn_types(RAnal *a, RAnalFunction *fcn);
150 
151 R_API int r_sign_is_flirt(RBuffer *buf);
152 R_API void r_sign_flirt_dump(const RAnal *anal, const char *flirt_file);
153 R_API void r_sign_flirt_scan(RAnal *anal, const char *flirt_file);
154 
155 R_API RList *r_sign_find_closest_sig(RAnal *a, RSignItem *it, int count, double score_threshold);
156 R_API RList *r_sign_find_closest_fcn(RAnal *a, RSignItem *it, int count, double score_threshold);
157 R_API void r_sign_close_match_free(RSignCloseMatch *match);
158 R_API bool r_sign_diff(RAnal *a, RSignOptions *options, const char *other_space_name);
159 R_API bool r_sign_diff_by_name(RAnal *a, RSignOptions *options, const char *other_space_name, bool not_matching);
160 
161 R_API RSignOptions *r_sign_options_new(const char *bytes_thresh, const char *graph_thresh);
162 R_API void r_sign_options_free(RSignOptions *options);
163 #endif
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #endif
170