-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdisplay.cpp
677 lines (519 loc) · 18.4 KB
/
display.cpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
Patchdiff2
Portions (C) 2011 Alexander Pick
Portions (C) 2010 Nicolas Pouvesle
Portions (C) 2007 - 2009 Tenable Network Security, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ida.hpp>
#include <idp.hpp>
#include <graph.hpp>
#include <kernwin.hpp>
#include "diff.hpp"
#include "display.hpp"
#include "os.hpp"
#include "parser.hpp"
#include "pgraph.hpp"
#include "options.hpp"
#include "system.hpp"
static uint32 idaapi sizer_dlist(slist_t * sl)
{
if (sl)
return sl->num;
return 0;
}
static uint32 idaapi sizer_match(void *obj)
{
deng_t * d = (deng_t *)obj;
return sizer_dlist(d ? d->mlist : NULL);
}
static uint32 idaapi sizer_identical(void *obj)
{
deng_t * d = (deng_t *)obj;
return sizer_dlist(d ? d->ilist : NULL);
}
static uint32 idaapi sizer_unmatch(void *obj)
{
if (obj)
return ((deng_t *)obj)->ulist->num;
return 0;
}
static void idaapi close(void *obj)
{
deng_t * d = (deng_t *)obj;
d->wnum--;
if (!d->wnum)
ipc_close();
return;
}
static void idaapi desc_dlist(slist_t * sl,uint32 n,char * const *arrptr)
{
int i;
/* header */
if (n == 0)
{
for (i = 0; i < qnumber (header_match); i++)
qsnprintf(arrptr[i], MAXSTR, "%s", header_match[i]);
}
else
{
qsnprintf(arrptr[0], MAXSTR, "%u", sl->sigs[n-1]->mtype);
qsnprintf(arrptr[1], MAXSTR, "%s", sl->sigs[n-1]->name);
qsnprintf(arrptr[2], MAXSTR, "%s", sl->sigs[n-1]->msig->name);
qsnprintf(arrptr[3], MAXSTR, "%lu", (unsigned long)sl->sigs[n-1]->startEA);
qsnprintf(arrptr[4], MAXSTR, "%lu", (unsigned long)sl->sigs[n-1]->msig->startEA);
qsnprintf(arrptr[5], MAXSTR, "%c", sl->sigs[n-1]->id_crc ? '+' : '-');
qsnprintf(arrptr[6], MAXSTR, "%lu", sl->sigs[n-1]->crc_hash);
qsnprintf(arrptr[7], MAXSTR, "%lu", sl->sigs[n-1]->msig->crc_hash);
}
}
/*------------------------------------------------*/
/* function : desc_match */
/* description: Fills matched list */
/*------------------------------------------------*/
static void idaapi desc_match(void *obj,uint32 n,char * const *arrptr)
{
deng_t * d = (deng_t *)obj;
desc_dlist(d ? d->mlist : NULL, n, arrptr);
}
/*------------------------------------------------*/
/* function : desc_identical */
/* description: Fills identical list */
/*------------------------------------------------*/
static void idaapi desc_identical(void *obj,uint32 n,char * const *arrptr)
{
deng_t * d = (deng_t *)obj;
desc_dlist(d ? d->ilist : NULL, n, arrptr);
}
/*------------------------------------------------*/
/* function : desc_unmatch */
/* description: Fills unmatched list */
/*------------------------------------------------*/
static void idaapi desc_unmatch(void *obj,uint32 n,char * const *arrptr)
{
int i;
slist_t * sl;
/* header */
if (n == 0)
{
for (i = 0; i < qnumber (header_unmatch); i++)
qsnprintf(arrptr[i], MAXSTR, "%s", header_unmatch[i]);
}
else
{
sl = ((deng_t *)obj)->ulist;
qsnprintf(arrptr[0], MAXSTR, "%u", sl->sigs[n-1]->nfile);
qsnprintf(arrptr[1], MAXSTR, "%s", sl->sigs[n-1]->name);
qsnprintf(arrptr[2], MAXSTR, "%u", (unsigned int)sl->sigs[n-1]->startEA);
qsnprintf(arrptr[3], MAXSTR, "%.8X", (unsigned int)sl->sigs[n-1]->sig);
qsnprintf(arrptr[4], MAXSTR, "%.8X", (unsigned int)sl->sigs[n-1]->hash);
qsnprintf(arrptr[5], MAXSTR, "%.8X", (unsigned int)sl->sigs[n-1]->crc_hash);
}
}
static void idaapi enter_list(slist_t * sl,uint32 n)
{
jumpto(sl->sigs[n-1]->startEA);
os_copy_to_clipboard(NULL);
}
/*------------------------------------------------*/
/* function : enter_match */
/* description: Jumps to code for element n in */
/* matched list */
/*------------------------------------------------*/
static void idaapi enter_match(void *obj,uint32 n)
{
enter_list(((deng_t *)obj)->mlist, n);
}
/*------------------------------------------------*/
/* function : enter_match */
/* description: Jumps to code for element n in */
/* identical list */
/*------------------------------------------------*/
static void idaapi enter_identical(void *obj,uint32 n)
{
enter_list(((deng_t *)obj)->ilist, n);
}
/*------------------------------------------------*/
/* function : enter_match */
/* description: Jumps to code for element n in */
/* unmatched list */
/*------------------------------------------------*/
static void idaapi enter_unmatch(void *obj,uint32 n)
{
slist_t * sl = ((deng_t *)obj)->ulist;
if (sl->sigs[n-1]->nfile == 1)
jumpto(sl->sigs[n-1]->startEA);
else
os_copy_to_clipboard(NULL);
}
static uint32 idaapi graph_list(slist_t * sl,uint32 n, options_t * opt)
{
slist_t * sl1 = NULL;
slist_t * sl2 = NULL;
msg ("parsing second function...\n");
sl2 = parse_second_fct(sl->sigs[n-1]->msig->startEA, sl->file, opt);
if (!sl2)
{
msg("Error: FCT2 parsing failed.\n");
return 0;
}
msg ("parsing first function...\n");
sl1 = parse_fct(sl->sigs[n-1]->startEA, dto.graph.s_showpref);
if (!sl1)
{
msg("Error: FCT1 parsing failed.\n");
siglist_free(sl2);
return 0;
}
sl1->sigs[0]->nfile = 1;
sl2->sigs[0]->nfile = 2;
msg ("diffing functions...\n");
generate_diff(NULL, sl1, sl2, NULL, false, NULL);
pgraph_display(sl1, sl2);
msg ("done!\n");
return 1;
}
/*------------------------------------------------*/
/* function : graph_match */
/* description: Draws graph from element n in */
/* matched list */
/*------------------------------------------------*/
static void idaapi graph_match(void *obj,uint32 n)
{
slist_t * sl = ((deng_t *)obj)->mlist;
options_t * opt = ((deng_t *)obj)->opt;
graph_list(sl, n, opt);
return;
}
/*------------------------------------------------*/
/* function : graph_identical */
/* description: Draws graph from element n in */
/* identical list */
/*------------------------------------------------*/
static void idaapi graph_identical(void *obj,uint32 n)
{
slist_t * sl = ((deng_t *)obj)->ilist;
options_t * opt = ((deng_t *)obj)->opt;
graph_list(sl, n, opt);
return;
}
/*------------------------------------------------*/
/* function : graph_unmatch */
/* description: Draws graph from element n in */
/* unmatched list */
/*------------------------------------------------*/
static void idaapi graph_unmatch(void *obj,uint32 n)
{
slist_t * sl = NULL, * tmp = ((deng_t *)obj)->ulist;
if (tmp->sigs[n-1]->nfile == 2)
{
msg ("parsing second function...\n");
sl = parse_second_fct(tmp->sigs[n-1]->startEA, tmp->file, ((deng_t *)obj)->opt);
if (!sl)
{
msg("Error: FCT2 parsing failed.\n");
return;
}
sl->sigs[0]->nfile = 2;
}
else
{
msg ("parsing first function...\n");
sl = parse_fct(tmp->sigs[n-1]->startEA, dto.graph.s_showpref);
if (!sl)
{
msg("Error: FCT1 parsing failed.\n");
return;
}
sl->sigs[0]->nfile = 1;
}
pgraph_display_one(sl);
msg ("done!\n");
return;
}
static uint32 idaapi res_unmatch(deng_t * d,uint32 n, int type)
{
slist_t * sl;
if (type == 0)
sl = d->ilist;
else
sl = d->mlist;
sl->sigs[n-1]->nfile = 1;
sl->sigs[n-1]->msig->nfile = 2;
siglist_add(d->ulist, sl->sigs[n-1]);
siglist_add(d->ulist, sl->sigs[n-1]->msig);
sl->sigs[n-1]->msig->msig = NULL;
sl->sigs[n-1]->msig = NULL;
siglist_remove(sl, n-1);
refresh_chooser(title_unmatch);
return 1;
}
/*------------------------------------------------*/
/* function : res_iunmatch */
/* description: Unmatches element n from identical*/
/* list */
/*------------------------------------------------*/
static uint32 idaapi res_iunmatch(void *obj,uint32 n)
{
return res_unmatch((deng_t *)obj, n, 0);
}
/*------------------------------------------------*/
/* function : res_munmatch */
/* description: Unmatches element n from matched */
/* list */
/*------------------------------------------------*/
static uint32 idaapi res_munmatch(void *obj,uint32 n)
{
return res_unmatch((deng_t *)obj, n, 1);
}
/*------------------------------------------------*/
/* function : propagate_match */
/* description: Propagates new matched result if */
/* option is set in dialog box */
/*------------------------------------------------*/
void propagate_match(deng_t * eng, psig_t * s1, psig_t * s2, int options)
{
size_t i;
deng_t * d = NULL;
slist_t * l1, * l2;
if (options)
{
show_wait_box ("PatchDiff is in progress ...");
l1 = siglist_init(eng->ulist->num, eng->ulist->file);
l2 = siglist_init(eng->ulist->num, eng->ulist->file);
for (i=0; i<eng->ulist->num; i++)
if (!eng->ulist->sigs[i]->msig)
{
if (eng->ulist->sigs[i]->nfile == 1)
siglist_add(l1, eng->ulist->sigs[i]);
else
siglist_add(l2, eng->ulist->sigs[i]);
}
generate_diff(&d, l1, l2, eng->ulist->file, false, NULL);
siglist_partial_free(l1);
siglist_partial_free(l2);
hide_wait_box();
}
i = 0;
while (i<eng->ulist->num)
{
s1 = eng->ulist->sigs[i];
s2 = s1->msig;
if (!s2)
i++;
else
{
if (s1->nfile == 1)
{
if (sig_equal(s1, s2, DIFF_EQUAL_SIG_HASH))
siglist_add(eng->ilist, s1);
else
siglist_add(eng->mlist, s1);
}
siglist_remove(eng->ulist, i);
}
}
}
/*------------------------------------------------*/
/* function : res_match */
/* description: Matches 2 elements from unmatched */
/* list */
/*------------------------------------------------*/
static uint32 idaapi res_match(void *obj,uint32 n)
{
deng_t * eng = (deng_t *)obj;
psig_t * s1, * s2;
int option;
ea_t ea = BADADDR;
size_t i;
const char format[] =
"STARTITEM 0\n"
"Set Match\n"
"<Match address:$:32:32::>\n\n"
"Options :\n"
"<Propagate :C>>\n\n"
;
option = 1;
if (AskUsingForm_c(format, &ea, &option))
{
s1 = eng->ulist->sigs[n-1];
for (i=0; i<eng->ulist->num; i++)
{
s2 = eng->ulist->sigs[i];
if (s2->startEA != ea || (s2->nfile == s1->nfile))
continue;
sig_set_matched_sig(s1, s2, DIFF_MANUAL);
propagate_match(eng, s1, s2, option);
refresh_chooser(title_match);
refresh_chooser(title_identical);
return 1;
}
warning("Address '%u' is not valid.", (unsigned int)ea);
return 0;
}
return 1;
}
/*------------------------------------------------*/
/* function : res_mtoi */
/* description: Switches element n from matched */
/* to identical list */
/*------------------------------------------------*/
static uint32 idaapi res_mtoi(void *obj,uint32 n)
{
deng_t * d = (deng_t *)obj;
d->mlist->sigs[n-1]->mtype = d->mlist->sigs[n-1]->msig->mtype = DIFF_MANUAL;
siglist_add(d->ilist, d->mlist->sigs[n-1]);
siglist_remove(d->mlist, n-1);
refresh_chooser(title_identical);
return 1;
}
/*------------------------------------------------*/
/* function : res_itom */
/* description: Switches element n from identical */
/* to matched list */
/*------------------------------------------------*/
static uint32 idaapi res_itom(void *obj,uint32 n)
{
deng_t * d = (deng_t *)obj;
d->ilist->sigs[n-1]->mtype = d->ilist->sigs[n-1]->msig->mtype = DIFF_MANUAL;
siglist_add(d->mlist, d->ilist->sigs[n-1]);
siglist_remove(d->ilist, n-1);
refresh_chooser(title_match);
return 1;
}
/*------------------------------------------------*/
/* function : res_flagged */
/* description: Sets element as flagged/unflagged */
/*------------------------------------------------*/
static uint32 idaapi res_flagged(void *obj,uint32 n)
{
deng_t * d = (deng_t *)obj;
d->mlist->sigs[n-1]->flag = !d->mlist->sigs[n-1]->flag;
refresh_chooser(title_match);
return 1;
}
/*------------------------------------------------*/
/* function : display_matched */
/* description: Displays matched list */
/*------------------------------------------------*/
void display_matched(deng_t * eng)
{
choose2(false, // non-modal window
-1, -1, -1, -1, // position is determined by Windows
eng, // pass the created function list to the window
qnumber(header_match),// number of columns
widths_match, // widths of columns
sizer_match, // function that returns number of lines
desc_match, // function that generates a line
title_match, // window title
-1, // use the default icon for the window
0, // position the cursor on the first line
NULL, // "kill" callback
NULL, // "new" callback
NULL, // "update" callback
graph_match, // "edit" callback
enter_match, // function to call when the user pressed Enter
close, // function to call when the window is closed
popup_match, // use default popup menu items
NULL);
eng->wnum++;
add_chooser_command(title_match, "Unmatch", res_munmatch, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT);
add_chooser_command(title_match, "Set as identical", res_mtoi, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT);
add_chooser_command(title_match, "Flag/unflag", res_flagged, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT);
enable_chooser_item_attrs(title_match, 1);
}
/*------------------------------------------------*/
/* function : display_identical */
/* description: Displays identical list */
/*------------------------------------------------*/
void display_identical(deng_t * eng)
{
choose2(false, // non-modal window
-1, -1, -1, -1, // position is determined by Windows
eng, // pass the created function list to the window
qnumber(header_match),// number of columns
widths_match, // widths of columns
sizer_identical, // function that returns number of lines
desc_identical, // function that generates a line
title_identical, // window title
-1, // use the default icon for the window
0, // position the cursor on the first line
NULL, // "kill" callback
NULL, // "new" callback
NULL, // "update" callback
graph_identical, // "edit" callback
enter_identical, // function to call when the user pressed Enter
close, // function to call when the window is closed
popup_match, // use default popup menu items
NULL);
eng->wnum++;
add_chooser_command(title_identical, "Unmatch", res_iunmatch, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT);
add_chooser_command(title_identical, "Set as matched", res_itom, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT);
}
/*------------------------------------------------*/
/* function : display_unmatched */
/* description: Displays unmatched list */
/*------------------------------------------------*/
void display_unmatched(deng_t * eng)
{
choose2(false, // non-modal window
-1, -1, -1, -1, // position is determined by Windows
eng, // pass the created function list to the window
qnumber(header_unmatch),// number of columns
widths_unmatch, // widths of columns
sizer_unmatch, // function that returns number of lines
desc_unmatch, // function that generates a line
title_unmatch, // window title
-1, // use the default icon for the window
0, // position the cursor on the first line
NULL, // "kill" callback
NULL, // "new" callback
NULL, // "update" callback
graph_unmatch, // "edit" callback
enter_unmatch, // function to call when the user pressed Enter
close, // function to call when the window is closed
popup_unmatch, // use default popup menu items
NULL); // use the same icon for all lines
eng->wnum++;
add_chooser_command(title_unmatch, "Set match", res_match, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT);
}
/*------------------------------------------------*/
/* function : ui_callback */
/* description: Catchs lists to change bg color */
/*------------------------------------------------*/
int ui_callback(void * data, int event_id, va_list va)
{
if ( event_id == ui_get_chooser_item_attrs )
{
void * co = va_arg(va, void *);
uint32 n = va_arg(va, uint32);
chooser_item_attrs_t *attrs = va_arg(va, chooser_item_attrs_t *);
if (attrs != NULL)
{
deng_t * d = (deng_t *)co;
if (d && d->magic == 0x0BADF00D)
if (d->mlist->sigs[n-1]->flag == 1)
attrs->color = 0x908070;
}
}
return 0;
}
/*------------------------------------------------*/
/* function : display_results */
/* description: Displays diff results */
/*------------------------------------------------*/
void display_results(deng_t * eng)
{
hook_to_notification_point(HT_UI, ui_callback, NULL);
display_matched(eng);
display_unmatched(eng);
display_identical(eng);
}