forked from radareorg/radare2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
450 lines (416 loc) · 10.4 KB
/
search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* radare - LGPL - Copyright 2008-2016 pancake */
#include <r_search.h>
#include <r_list.h>
#include <ctype.h>
// Experimental search engine (fails, because stops at first hit of every block read
#define USE_BMH 0
R_LIB_VERSION (r_search);
R_API RSearch *r_search_new(int mode) {
RSearch *s = R_NEW0 (RSearch);
if (!s) return NULL;
if (!r_search_set_mode (s, mode)) {
free (s);
eprintf ("Cannot init search for mode %d\n", mode);
return false;
}
s->inverse = false;
s->user = NULL;
s->callback = NULL;
s->align = 0;
s->distance = 0;
s->contiguous = 0;
s->overlap = false;
s->pattern_size = 0;
s->string_max = 255;
s->string_min = 3;
s->hits = r_list_new ();
// TODO: review those mempool sizes. ensure never gets NULL
s->pool = r_mem_pool_new (sizeof (RSearchHit), 1024, 10);
s->kws = r_list_new ();
if (!s->kws) {
r_search_free (s);
return NULL;
}
s->kws->free = (RListFree) r_search_keyword_free;
return s;
}
R_API RSearch *r_search_free(RSearch *s) {
if (!s) return NULL;
// TODO: it leaks
r_mem_pool_free (s->pool);
r_list_free (s->hits);
r_list_free (s->kws);
//r_io_free(s->iob.io); this is suposed to be a weak reference
free (s);
return NULL;
}
R_API int r_search_set_string_limits(RSearch *s, ut32 min, ut32 max) {
if (max < min)
return false;
s->string_min = min;
s->string_max = max;
return true;
}
R_API int r_search_magic_update(void *_s, ut64 from, const ut8 *buf, int len) {
eprintf ("TODO: import libr/core/cmd_search.c /m implementation into rsearch\n");
return false;
}
R_API int r_search_set_mode(RSearch *s, int mode) {
s->update = NULL;
switch (mode) {
case R_SEARCH_KEYWORD: s->update = r_search_mybinparse_update; break;
case R_SEARCH_XREFS: s->update = r_search_xrefs_update; break;
case R_SEARCH_REGEXP: s->update = r_search_regexp_update; break;
case R_SEARCH_AES: s->update = r_search_aes_update; break;
case R_SEARCH_STRING: s->update = r_search_strings_update; break;
case R_SEARCH_DELTAKEY: s->update = r_search_deltakey_update; break;
case R_SEARCH_MAGIC: s->update = r_search_magic_update; break;
}
if (s->update || mode == R_SEARCH_PATTERN) {
s->mode = mode;
return true;
}
return false;
}
R_API int r_search_begin(RSearch *s) {
RListIter *iter;
RSearchKeyword *kw;
r_list_foreach (s->kws, iter, kw) {
kw->count = 0;
kw->idx[0] = 0;
kw->distance = 0; //s->distance;
kw->last = 0;
}
#if 0
/* TODO: compile regexpes */
switch(s->mode) {
case R_SEARCH_REGEXP:
break;
}
#endif
return true;
}
R_API int r_search_hit_new(RSearch *s, RSearchKeyword *kw, ut64 addr) {
RSearchHit* hit;
if (s->align && (addr%s->align)) {
eprintf ("0x%08"PFMT64x" unaligned\n", addr);
return true;
}
if (!s->contiguous) {
if (kw->last && addr == kw->last) {
kw->count--;
kw->last = addr + kw->keyword_length;
eprintf ("0x%08"PFMT64x" Sequencial hit ignored.\n", addr);
return true;
}
kw->last = addr + kw->keyword_length;
}
if (s->callback)
return s->callback (kw, s->user, addr);
if (!(hit = r_mem_pool_alloc (s->pool)))
return false;
hit->kw = kw;
hit->addr = addr;
r_list_append (s->hits, hit);
return true;
}
R_API int r_search_deltakey_update(void *_s, ut64 from, const ut8 *buf, int len) {
RListIter *iter;
unsigned char pch = 0;
int i, j, count = 0;
RSearch *s = (RSearch*)_s;
for (i=0; i<len; i++) {
RSearchKeyword *kw;
r_list_foreach (s->kws, iter, kw) {
for (j=0; j<=kw->distance; j++) {
char ch = kw->bin_keyword[kw->idx[j]]; // signed char
ut8 ch2 = buf[i];
/* no icase in delta keys */
/* no binmask in delta keys */
/* no inverse support for delta keys */
if (pch+ch == ch2) {
kw->idx[j]++;
if (kw->idx[j] == kw->keyword_length) {
if (!r_search_hit_new (s, kw, (ut64)
from + i - kw->keyword_length + 1))
return -1;
kw->idx[j] = 0;
//kw->idx[0] = 0;
kw->distance = 0;
kw->count++;
count++;
//s->nhits++;
}
}
pch = ch2;
}
}
count = 0;
}
return count;
}
/* Boyer-Moore-Horspool pattern matching */
#if USE_BMH
R_API int r_search_bmh (const RSearchKeyword *kw, const ut64 from, const ut8 *buf, const int len, ut64 *out) {
ut64 bad_char_shift[UT8_MAX + 1];
ut64 pos = from;
int i, kw_len;
ut8 ch;
kw_len = kw->keyword_length - 1;
if (kw_len < 0)
return false;
for (i = 0; i < 256; i++)
bad_char_shift[i] = kw->keyword_length;
for (i = 0; i < kw_len; i++) {
ch = kw->bin_keyword[i];
bad_char_shift[kw->icase?tolower(ch):ch] = kw_len - i;
}
while (pos < len) {
for (i = kw_len; ; i--) {
ut8 ch1 = buf[pos + i];
ut8 ch2 = kw->bin_keyword[i];
if (kw->icase) {
ch1 = tolower(ch1);
ch2 = tolower(ch2);
}
if (kw->binmask_length && i < kw->binmask_length) {
ch1 &= kw->bin_binmask[i];
ch2 &= kw->bin_binmask[i];
}
if (ch1 != ch2)
break;
if (i == 0) {
if (out)
*out = pos;
return true;
}
}
ch = buf[pos + kw_len];
pos += bad_char_shift[kw->icase?tolower(ch):ch];
}
return false;
}
#endif
static int maskcmp(RSearchKeyword *kw, const ut8* buf, int kwidx) {
int k;
ut8 a, b;
kwidx = kw->idx[kwidx];
k = (kwidx % kw->binmask_length);
a = *buf;
b = kw->bin_keyword[kwidx];
if (kw->icase) {
a = tolower (a);
b = tolower (b);
}
a &= kw->bin_binmask[k];
b &= kw->bin_binmask[k];
return (a == b)? 1: 0;
}
R_API int r_search_mybinparse_update(void *_s, ut64 from, const ut8 *buf, int len) {
RSearch *s = (RSearch*)_s;
RSearchKeyword *kw;
RListIter *iter;
int count = 0;
#if USE_BMH
ut64 offset;
ut64 match_pos;
RSearchKeyword *kw;
r_list_foreach (s->kws, iter, kw) {
offset = 0;
while (offset < len && r_search_bmh (kw, offset, buf, len, &match_pos)) {
if (!r_search_hit_new (s, kw, from + match_pos)) {
eprintf ("Something very bad has happened...\n");
return -1;
}
offset += match_pos + kw->keyword_length;
kw->count++;
count++;
/* Stop at the first occurrence */
if (s->inverse) {
return -1;
}
}
}
#else
int i, j, hit;
#if 0
int hasmask;
// XXX this shouldnt be necessary
/** remove unnecesary binmasks here */
r_list_foreach (s->kws, iter, kw) {
if (kw->binmask_length) {
hasmask = 1;
for (i=0;i<kw->binmask_length; i++) {
if (kw->bin_binmask[i] != 0xff) {
hasmask = 0;
break;
}
}
if (!hasmask)
kw->binmask_length = 0;
}
}
#endif
for (i = 0; i < len; i++) {
r_list_foreach (s->kws, iter, kw) {
if (s->inverse && s->nhits > 0) {
//eprintf ("nhits = %d\n", s->nhits);
return -1;
}
for (j = 0; j <= kw->distance; j++) {
// TODO: refactor: hit = checkKeyword()
// TODO: assert len(kw) == len(bm)
ut8 ch, ch2;
bool match = false;
hit = false;
if (kw->binmask_length > 0) {
match = (maskcmp (kw, buf + i, j) > 0);
} else {
ch = kw->bin_keyword[kw->idx[j]];
ch2 = buf[i];
if (kw->icase) {
ch = tolower (ch);
ch2 = tolower (ch2);
}
match = (ch == ch2);
}
if (match) {
hit = true;
} else {
if (s->inverse) {
if (!r_search_hit_new (s, kw, (ut64)from + i - kw->keyword_length + 1))
return -1;
kw->idx[j] = 0;
//kw->idx[0] = 0;
kw->distance = 0;
//eprintf ("HIT FOUND !!! %x %x 0x%llx %d\n", ch, ch2, from+i, i);
kw->count++;
s->nhits++;
return 1; // only return 1 keyword if inverse mode
}
if (kw->distance < s->distance) {
kw->idx[kw->distance + 1] = kw->idx[kw->distance];
kw->distance++;
hit = true;
} else {
// Works for 0a0a0a0b
bool isTail = false;
int k = kw->idx[j];
if (k > 0) {
int q = 1;
int model = kw->bin_keyword[0];
for (isTail = true; q < k; q++) {
if (kw->bin_keyword[q] != model)
isTail = false;
}
}
if (isTail) {
kw->idx[j]--;
} else {
kw->idx[j] = 0;
}
kw->distance = 0;
if (kw->binmask_length > 0) {
hit = (maskcmp (kw, buf + i, j) > 0);
} else {
ch = kw->bin_keyword[kw->idx[j]];
hit = (ch == ch2);
}
}
}
if (hit) {
kw->idx[j]++;
if (kw->idx[j] == kw->keyword_length) {
if (s->inverse) {
kw->idx[j] = 0;
continue;
}
if (!r_search_hit_new (s, kw, (ut64)from + i - kw->keyword_length + 1)) {
return -1;
}
kw->idx[j] = 0;
kw->distance = 0;
kw->count++;
count++;
if (s->overlap) {
if (kw->keyword_length > 1) {
i -= kw->keyword_length - 1;
}
}
//s->nhits++;
}
}
}
}
count = 0;
}
#endif
return count;
}
R_API void r_search_set_distance(RSearch *s, int dist) {
if (dist>=R_SEARCH_DISTANCE_MAX) {
eprintf ("Invalid distance\n");
s->distance = 0;
} else s->distance = (dist>0)?dist:0;
}
// deprecate? or standarize with ->align ??
R_API void r_search_pattern_size(RSearch *s, int size) {
s->pattern_size = size;
}
R_API void r_search_set_callback(RSearch *s, RSearchCallback(callback), void *user) {
s->callback = callback;
s->user = user;
}
/* TODO: initialize update callback in _init or begin... */
R_API int r_search_update(RSearch *s, ut64 *from, const ut8 *buf, long len) {
int ret = -1;
if (s->update) {
ret = s->update (s, *from, buf, len);
if (s->mode == R_SEARCH_AES) {
ret = R_MIN (R_SEARCH_AES_BOX_SIZE, len);
}
} else {
eprintf ("r_search_update: No search method defined\n");
}
return ret;
}
R_API int r_search_update_i(RSearch *s, ut64 from, const ut8 *buf, long len) {
return r_search_update (s, &from, buf, len);
}
static int listcb(RSearchKeyword *k, void *user, ut64 addr) {
RSearchHit *hit = R_NEW0 (RSearchHit);
if (!hit) {
return false;
}
hit->kw = k;
hit->addr = addr;
r_list_append (user, hit);
return true;
}
R_API RList *r_search_find(RSearch *s, ut64 addr, const ut8 *buf, int len) {
RList *ret = r_list_new ();
r_search_set_callback (s, listcb, ret);
r_search_update (s, &addr, buf, len);
return ret;
}
/* --- keywords --- */
R_API int r_search_kw_add(RSearch *s, RSearchKeyword *kw) {
if (!kw) return false;
kw->kwidx = s->n_kws++;
r_list_append (s->kws, kw);
return true;
}
R_API void r_search_kw_reset(RSearch *s) {
r_list_free (s->kws);
s->kws = r_list_new ();
}
R_API void r_search_reset(RSearch *s, int mode) {
r_list_purge (s->hits);
s->nhits = 0;
s->hits = r_list_new ();
if (!s->hits) return;
s->hits->free = free;
r_search_kw_reset (s);
if (!r_search_set_mode (s, mode))
eprintf ("Cannot init search for mode %d\n", mode);
}