-
Notifications
You must be signed in to change notification settings - Fork 23
/
sig.cpp
1299 lines (992 loc) · 27.7 KB
/
sig.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Patchdiff2
Portions (C) 2010 - 2011 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 "precomp.hpp"
#include "sig.hpp"
#include "x86.hpp"
#include "ppc.hpp"
#include "patchdiff.hpp"
#include "pchart.hpp"
#include "os.hpp"
extern cpu_t patchdiff_cpu;
/*------------------------------------------------*/
/* function : pget_func_name */
/* description: Gets function name */
/*------------------------------------------------*/
char * pget_func_name(ea_t ea, char * buffer, size_t blen)
{
char * pos;
char tmp[512];
if (!get_func_name(ea, buffer, blen))
return NULL;
// make sure this is not a c++ class/struct badly defined as a function
demangle_name(tmp, blen, buffer, inf.long_demnames);
if ( (strstr(tmp, "public: static") || strstr(tmp, "private: static")) &&
(!strstr(tmp, "(") || strstr(tmp, "public: static long (__stdcall")) )
return NULL;
demangle_name(buffer, blen, buffer, inf.short_demnames);
// remove duplicates of the same name
pos = strstr(buffer, "Z$0");
if (pos)
pos[0] = '\0';
return buffer;
}
/*------------------------------------------------*/
/* function : sig_init */
/* description: Allocates and initializes a new */
/* function signature */
/*------------------------------------------------*/
psig_t * sig_init()
{
psig_t * sig;
sig = (psig_t *)qalloc(sizeof(*sig));
if (!sig)
return NULL;
memset(sig, 0, sizeof(*sig));
sig->mtype = DIFF_UNMATCHED;
sig->msig = NULL;
return sig;
}
/*------------------------------------------------*/
/* function : frefs_free */
/* description: Frees chained list */
/*------------------------------------------------*/
void frefs_free(frefs_t * frefs)
{
fref_t * fref, * next;
fref = frefs->list;
while (fref)
{
next = fref->next;
qfree(fref);
fref = next;
}
qfree(frefs);
}
/*------------------------------------------------*/
/* function : dsig_free */
/* description: Frees chained list */
/*------------------------------------------------*/
void dsig_free(dpsig_t * ds)
{
dpsig_t * next;
while (ds)
{
next = ds->next;
qfree(ds);
ds = next;
}
qfree(ds);
}
/*------------------------------------------------*/
/* function : clist_free */
/* description: Frees clist_t structure */
/*------------------------------------------------*/
void clist_free(clist_t * cl)
{
dsig_free(cl->sigs);
dsig_free(cl->msigs);
qfree(cl);
}
/*------------------------------------------------*/
/* function : sig_free */
/* description: Frees signature */
/*------------------------------------------------*/
void sig_free(psig_t * sig)
{
if (sig->name)
{
qfree(sig->name);
sig->name = NULL;
}
if (sig->dl.lines != NULL)
qfree(sig->dl.lines);
if (sig->prefs) frefs_free(sig->prefs);
if (sig->srefs) frefs_free(sig->srefs);
if (sig->cp) clist_free(sig->cp);
if (sig->cs) clist_free(sig->cs);
qfree(sig);
}
/*------------------------------------------------*/
/* function : sig_set_name */
/* description: Sets function signature name */
/*------------------------------------------------*/
void sig_set_name(psig_t * sig, const char * name)
{
sig->name = qstrdup(name);
}
/*------------------------------------------------*/
/* function : sig_set_start */
/* description: Sets function start address */
/*------------------------------------------------*/
void sig_set_start(psig_t * sig, ea_t ea)
{
sig->startEA = ea;
}
/*------------------------------------------------*/
/* function : sig_get_start */
/* description: Returns function start address */
/*------------------------------------------------*/
ea_t sig_get_start(psig_t * sig)
{
return sig->startEA;
}
/*------------------------------------------------*/
/* function : sig_get_preds */
/* description: Returns signature pred xrefs */
/*------------------------------------------------*/
frefs_t * sig_get_preds(psig_t * sig)
{
return sig->prefs;
}
/*------------------------------------------------*/
/* function : sig_get_succs */
/* description: Returns signature succ xrefs */
/*------------------------------------------------*/
frefs_t * sig_get_succs(psig_t * sig)
{
return sig->srefs;
}
/*------------------------------------------------*/
/* function : sig_get_crefs */
/* description: Returns signature cxrefs */
/*------------------------------------------------*/
clist_t * sig_get_crefs(psig_t * sig, int type)
{
if (type == SIG_PRED)
return sig->cp;
if (type == SIG_SUCC)
return sig->cs;
return NULL;
}
/*------------------------------------------------*/
/* function : sig_set_crefs */
/* description: Sets signature cxrefs */
/*------------------------------------------------*/
void sig_set_crefs(psig_t * sig, int type, clist_t * cl)
{
if (type == SIG_PRED)
sig->cp = cl;
else if (type == SIG_SUCC)
sig->cs = cl;
}
/*------------------------------------------------*/
/* function : sig_set_nfile */
/* description: Sets file number */
/*------------------------------------------------*/
void sig_set_nfile(psig_t * sig, int num)
{
sig->nfile = num;
}
/*------------------------------------------------*/
/* function : sig_set_matched_sig */
/* description: Sets matched address */
/*------------------------------------------------*/
void sig_set_matched_sig(psig_t * sig, psig_t * sig2, int type)
{
sig->msig = sig2;
sig->matchedEA = sig2->startEA;
sig2->msig = sig;
sig2->matchedEA = sig->startEA;
sig->mtype = sig2->mtype = type;
if (sig->crc_hash != sig2->crc_hash)
sig->id_crc = sig2->id_crc = 1;
}
/*------------------------------------------------*/
/* function : sig_get_matched_sig */
/* description: Returns matched address */
/*------------------------------------------------*/
psig_t * sig_get_matched_sig(psig_t * sig)
{
return sig->msig;
}
/*------------------------------------------------*/
/* function : sig_get_matched_type */
/* description: Returns matched type */
/*------------------------------------------------*/
int sig_get_matched_type(psig_t * sig)
{
return sig->mtype;
}
/*------------------------------------------------*/
/* function : sig_add_fref */
/* description: Adds a function reference to the */
/* signature */
/*------------------------------------------------*/
int sig_add_fref(frefs_t ** frefs, ea_t ea, int type, char rtype)
{
fref_t * ref, * next;
if (!*frefs)
{
*frefs = (frefs_t *)qalloc(sizeof(**frefs));
if (!*frefs) return -1;
memset(*frefs, 0, sizeof(**frefs));
}
else
{
//don't add duplicates
next = (*frefs)->list;
while(next)
{
if (next->ea == ea)
return -1;
next = next->next;
}
}
ref = (fref_t *)qalloc(sizeof(*ref));
if (!ref) return -1;
ref->ea = ea;
ref->type = type;
ref->rtype = rtype;
ref->next = (*frefs)->list;
(*frefs)->num++;
(*frefs)->list = ref;
return 0;
}
/*------------------------------------------------*/
/* function : sig_add_pref */
/* description: Adds a function reference to the */
/* signature */
/*------------------------------------------------*/
int sig_add_pref(psig_t * sig, ea_t ea, int type, char rtype)
{
return sig_add_fref(&sig->prefs, ea, type, rtype);
}
/*------------------------------------------------*/
/* function : sig_add_sref */
/* description: Adds a function reference to the */
/* signature */
/*------------------------------------------------*/
int sig_add_sref(psig_t * sig, ea_t ea, int type, char rtype)
{
return sig_add_fref(&sig->srefs, ea, type, rtype);
}
/*------------------------------------------------*/
/* function : is_fake_jump */
/* description: Returns TRUE if the instruction at*/
/* ea is a jump */
/*------------------------------------------------*/
bool is_fake_jump(ea_t ea)
{
switch(patchdiff_cpu)
{
case CPU_X8632:
case CPU_X8664:
if (x86_get_fake_jump(ea) != BADADDR)
return true;
default:
return false;
}
}
/*------------------------------------------------*/
/* function : ignore_jump */
/* description: Returns TRUE if the instruction at*/
/* ea is a jump that must be ignored */
/* in the signature */
/*------------------------------------------------*/
bool ignore_jump(ea_t ea)
{
switch(patchdiff_cpu)
{
case CPU_X8632:
case CPU_X8664:
if (!x86_is_direct_jump(ea))
return false;
default:
return true;
}
}
/*------------------------------------------------*/
/* function : is_jump */
/* description: Returns TRUE if the instruction at*/
/* ea is a jump */
/*------------------------------------------------*/
bool is_jump(psig_t * sig, ea_t ea, bool * call, bool * cj)
{
xrefblk_t xb;
cref_t cr;
*call = false;
*cj = false;
if (xb.first_from(ea, XREF_FAR))
{
cr = (cref_t)xb.type;
if (xb.iscode && (cr == fl_JF || cr == fl_JN)) {
if (ignore_jump(ea)) {
return true;
} else {
*cj = true;
}
}
if (xb.iscode && (cr == fl_CF || cr == fl_CN))
{
if (sig->type == 1)
sig_add_sref(sig, xb.to, 0, CHECK_REF);
*call = true;
}
}
else
return is_fake_jump(ea);
return false;
}
/*------------------------------------------------*/
/* function : remove_instr */
/* description: Returns TRUE if the instruction at*/
/* ea must not be added to the sig */
/*------------------------------------------------*/
bool remove_instr(unsigned char byte, ea_t ea)
{
switch (patchdiff_cpu)
{
case CPU_X8632:
case CPU_X8664:
return x86_remove_instr(byte, ea);
case CPU_PPC:
return ppc_remove_instr(byte, ea);
default:
return false;
}
}
/*------------------------------------------------*/
/* function : get_byte_with_optimization */
/* description: Returns byte at address ea */
/* note: Uses the processor optimized function if */
/* available */
/*------------------------------------------------*/
char get_byte_with_optimization(ea_t ea)
{
switch (patchdiff_cpu)
{
case CPU_X8632:
case CPU_X8664:
return x86_get_byte(ea);
case CPU_PPC:
return ppc_get_byte(ea);
default:
{
decode_insn(ea);
return (char)cmd.itype;
}
}
}
unsigned long ror(unsigned long val, int r)
{
return (val >> r) | (val << (32-r));
}
/*------------------------------------------------*/
/* function : dline_add */
/* description: Adds a disassembled line to the */
/* signature */
/*------------------------------------------------*/
int dline_add(dline_t * dl, ea_t ea, char options)
{
char buf[256];
char tmp[256];
char dis[256];
char addr[30];
char * dll;
int len;
flags_t f;
buf[0] = '\0';
f = getFlags(ea);
generate_disasm_line(ea, dis, sizeof(dis));
decode_insn(ea);
init_output_buffer(buf, sizeof(buf));
// Adds block label
if (has_dummy_name(f))
{
get_nice_colored_name(ea,tmp,sizeof(tmp),GNCN_NOSEG|GNCN_NOFUNC);
out_snprintf("%s", tmp);
out_line(":\n", COLOR_DATNAME);
}
if (options)
{
qsnprintf(addr, sizeof(addr), "%a", ea);
out_snprintf("%s ", addr);
}
out_insert(get_output_ptr(), dis);
term_output_buffer();
len = strlen(buf);
if (dl->available < (len+3))
{
dll = (char *)qrealloc(dl->lines, sizeof(char*) * (dl->num+len+256));
if (!dll) return -1;
dl->available = len+256;
dl->lines = dll;
}
if (dl->num)
{
dl->lines[dl->num] = '\n';
dl->num++;
}
memcpy(&dl->lines[dl->num], buf, len);
dl->available -= len+1;
dl->num += len;
dl->lines[dl->num] = '\0';
return 0;
}
/*------------------------------------------------*/
/* function : sig_add_address */
/* description: Adds an address to the signature */
/*------------------------------------------------*/
int sig_add_address(psig_t * sig, short opcodes[256], ea_t ea, bool b, bool line, char options)
{
unsigned char byte;
unsigned char buf[200];
uint32 s, i;
bool call;
bool cj;
ea_t tea;
flags_t f;
if (line)
dline_add(&sig->dl, ea, options);
if (is_jump(sig, ea, &call, &cj))
return -1;
byte = get_byte_with_optimization(ea);
if (remove_instr(byte, ea))
return -1;
sig->lines++;
opcodes[byte]++;
if (!b && !call)
{
if (cj)
{
buf[0] = byte;
s = 1;
}
else
{
s = (uint32)get_item_size(ea);
if (s > sizeof(buf)) s = sizeof(buf);
get_many_bytes(ea, buf, s);
}
for (i=0; i<s; i++)
{
sig->crc_hash += buf[i];
sig->crc_hash += ( sig->crc_hash << 10 );
sig->crc_hash ^= ( sig->crc_hash >> 6 );
}
}
else if (b)
{
tea = get_first_dref_from(ea);
if (tea != BADADDR)
{
f = getFlags(tea);
if (isASCII(f))
{
opinfo_t op_info;
get_opinfo(tea, 0, f, &op_info);
s = get_max_ascii_length(tea, op_info.strtype);
if (!get_ascii_contents2(tea, s, op_info.strtype, buf, sizeof(buf)))
s = sizeof(buf);
for (i=0; i<s; i++)
sig->str_hash += buf[i]*i;
}
}
}
return 0;
}
/*------------------------------------------------*/
/* function : sig_add_block */
/* description: Adds a block to the signature */
/*------------------------------------------------*/
int sig_add_block(psig_t * sig, short opcodes[256], ea_t startEA, ea_t endEA, bool line, char options)
{
ea_t ea;
flags_t flags;
bool b;
ea = startEA;
while (ea < endEA)
{
flags = getFlags (ea);
if (!isCode (flags))
return -1;
b = get_first_dref_from(ea) != BADADDR ? true : false;
sig_add_address(sig, opcodes, ea, isOff(flags, OPND_ALL) || b, line, options);
ea += get_item_size(ea);
}
return 0;
}
int OS_CDECL compare(const void *arg1, const void *arg2)
{
return *((short *)arg1) - *((short *)arg2);
}
/*------------------------------------------------*/
/* function : sig_calc_sighash */
/* description: generates a sig/hash for the */
/* signature opcodes */
/*------------------------------------------------*/
int sig_calc_sighash(psig_t * sig, short _opcodes[256], int do_sig)
{
short tmp;
short opcodes[256];
int i, j;
memcpy(opcodes, _opcodes, sizeof(opcodes));
qsort(opcodes, 256, sizeof(short), compare);
for (i=0; i<256; i++)
for (j=0; j<255; j++)
if (opcodes[j] > opcodes[j+1])
{
tmp = opcodes[j+1];
opcodes[j+1] = opcodes[j];
opcodes[j] = tmp;
}
sig->hash2 = 0;
if (do_sig) sig->sig = 0;
for (i=0; i<256; i++)
{
if (do_sig) sig->sig += opcodes[i] * i;
sig->hash2 = ror(sig->hash2, 13);
sig->hash2 += _opcodes[i];
}
return 0;
}
/*------------------------------------------------*/
/* function : sig_parse_dref_list */
/* description: checks if the data ref is a class */
/* like structure. Returns class ea */
/* on success */
/*------------------------------------------------*/
ea_t sig_parse_dref_list(psig_t * sig, ea_t ea)
{
ea_t fref;
flags_t f;
// scan up
do
{
fref = get_first_dref_from(ea);
if (fref == BADADDR)
return BADADDR;
f = getFlags(fref);
if (!isCode(f))
return BADADDR;
fref = get_first_dref_to(ea);
if (fref != BADADDR)
{
f = getFlags(fref);
if (!isCode(f))
return BADADDR;
return ea;
}
ea = prev_visea(ea);
} while(ea != BADADDR);
return ea;
}
/*------------------------------------------------*/
/* function : sig_is_class */
/* description: Returns true is the signature is */
/* a class */
/*------------------------------------------------*/
bool sig_is_class(psig_t * sig)
{
if (sig->sig == CLASS_SIG && sig->hash == CLASS_SIG && sig->crc_hash == CLASS_SIG)
return true;
return false;
}
/*------------------------------------------------*/
/* function : sig_class_generate */
/* description: generates a signature for the */
/* class structure */
/*------------------------------------------------*/
psig_t * sig_class_generate(ea_t ea)
{
func_t * xfct;
psig_t * sig;
ea_t fref;
char buf[512];
sig = sig_init();
if (!sig)
return NULL;
// Adds function start address
sig_set_start(sig, ea);
// Adds function name
qsnprintf(buf, sizeof(buf), "sub_%a", ea);
sig_set_name(sig, buf);
// Adds class references
fref = get_first_dref_to(ea);
while (fref != BADADDR)
{
xfct = get_func(fref);
if (xfct)
sig_add_sref(sig, xfct->startEA, 0, CHECK_REF);
fref = get_next_dref_to(ea, fref);
}
sig->hash = sig->crc_hash = sig->sig = CLASS_SIG;
return sig;
}
/*------------------------------------------------*/
/* function : sig_generate */
/* description: generates a signature for the */
/* given function */
/*------------------------------------------------*/
psig_t * sig_generate(size_t fct_num, qvector<ea_t> & class_l)
{
func_t * fct, * xfct;
pflow_chart_t * fchart;
psig_t * sig;
ea_t fref, ea;
int bnum, i;
char buf[512];
short opcodes[256];
qvector<int> call_list;
flags_t f;
fct = getn_func(fct_num);
memset(opcodes, '\0', sizeof(opcodes));
fchart = new pflow_chart_t(fct);
sig = sig_init();
if (!sig)
{
delete fchart;
return NULL;
}
sig->type = 1;
// Adds function start address
sig_set_start(sig, fct->startEA);
// Adds function name
if (pget_func_name(fct->startEA, buf, sizeof(buf)))
sig_set_name(sig, buf);
else return NULL;
// Adds function references
fref = get_first_dref_to(fct->startEA);
while (fref != BADADDR)
{
f = getFlags(fref);
if (isCode(f))
{
xfct = get_func(fref);
if (xfct && xfct->startEA != fct->startEA)
sig_add_pref(sig, xfct->startEA, 0, CHECK_REF);
}
else
{
ea = sig_parse_dref_list(sig, fref);
if (ea != BADADDR)
{
sig_add_pref(sig, ea, 0, CHECK_REF);
class_l.add_unique(ea);
}
}
fref = get_next_dref_to(fct->startEA, fref);
}
// Adds each block to the signature
bnum = fchart->nproper;
sig->hash = 0;
sig->sig = 0;
for (i=0; i<bnum; i++)
{
int j;
int ttype;
int smax = fchart->nsucc(i);
sig->sig += (i+1) + smax*i;
sig_add_block(sig, opcodes, fchart->blocks[i].startEA, fchart->blocks[i].endEA, 0, 0);
for(j=0; j<smax; j++)
{
sig->hash = ror(sig->hash, 13);
ttype = fchart->blocks[i].succ[j].type;
if (ttype == 2) ttype--;
sig->hash += ttype;
}
}
sig_calc_sighash(sig, opcodes, 0);
delete fchart;
return sig;
}
/*------------------------------------------------*/
/* function : sig_save */
/* description: Saves signature refs to disk */
/*------------------------------------------------*/
void sig_save_refs(FILE * fp, frefs_t * refs)
{
int num, i;
fref_t * tmp;
if (refs)
{
num = refs->num;
qfwrite(fp, &num, sizeof(num));
tmp = refs->list;
for (i=0; i<num; i++)
{
qfwrite(fp, &tmp->ea, sizeof(tmp->ea));
qfwrite(fp, &tmp->type, sizeof(tmp->type));
tmp = tmp->next;
}
}
else
{
num = 0;
qfwrite(fp, &num, sizeof(num));
}
}
/*------------------------------------------------*/
/* function : sig_save */
/* description: Saves signature to disk */
/*------------------------------------------------*/
int sig_save(psig_t * sig, FILE * fp)
{
size_t len;
// saves function name
len = strlen(sig->name);
qfwrite(fp, &len, sizeof(len));
qfwrite(fp, sig->name, len);
// saves function start address
qfwrite(fp, &sig->startEA, sizeof(sig->startEA));
// saves function lines
qfwrite(fp, &sig->dl.num, sizeof(sig->dl.num));
qfwrite(fp, sig->dl.lines, sig->dl.num);
// saves sig/hash
qfwrite(fp, &sig->sig, sizeof(sig->sig));
qfwrite(fp, &sig->hash, sizeof(sig->hash));
qfwrite(fp, &sig->hash2, sizeof(sig->hash2));
qfwrite(fp, &sig->crc_hash, sizeof(sig->crc_hash));
qfwrite(fp, &sig->str_hash, sizeof(sig->str_hash));
// saves function refs
sig_save_refs(fp, sig->prefs);
sig_save_refs(fp, sig->srefs);
return 0;
}
/*------------------------------------------------*/
/* function : sig_load_prefs */
/* description: Loads signature refs from disk */
/*------------------------------------------------*/
void sig_load_prefs(psig_t * sig, FILE * fp, int type)
{
int num, i;
pedge_t * eatab;
// loads function refs in reverse order
qfread(fp, &num, sizeof(num));
eatab = (pedge_t *)qalloc(num * sizeof(*eatab));
for (i=0; i<num; i++)
{
qfread(fp, &eatab[i].ea, sizeof(eatab[i].ea));
qfread(fp, &eatab[i].type, sizeof(eatab[i].type));
}