-
Notifications
You must be signed in to change notification settings - Fork 2
/
Disasm.cpp
2127 lines (2071 loc) · 69.9 KB
/
Disasm.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
/************************************************************************
* disasm.cpp *
* This is a fairly big class, even having split off the disio class. *
* The class is the class responsible for maintaining the main *
* disassembly structures of Borg, and for performing the disassembly *
* analysis. The class has some very old code in it, and hasn't *
* undergone much improvement for some time, so parts could do with *
* rewriting. *
************************************************************************/
#include <windows.h>
#include <stdio.h>
#include "disasm.h"
#include "dasm.h"
#include "xref.h"
#include "data.h"
#include "gname.h"
#include "schedule.h"
#include "relocs.h"
#include "debug.h"
/************************************************************************
* constructor function *
* - resets a few class globals *
************************************************************************/
disasm::disasm()
{ lastdis=NULL;
itables=0;
jtables=0;
irefs=0;
}
/************************************************************************
* destructor function is currently null *
************************************************************************/
disasm::~disasm()
{
}
/************************************************************************
* initnewdsm *
* - this is some common code i pulled out and put into a function on *
* its own. when a new disassembly item is created it initialises some *
* of the stucture. *
************************************************************************/
void disasm::initnewdsm(dsmitem *newdsm,lptr loc,dsmitemtype typ)
{ newdsm->addr=loc;
newdsm->type=typ;
newdsm->override=over_null;
newdsm->modrm=0;
newdsm->mode32=options.mode32;
newdsm->flags=0;
newdsm->displayflags=0;
}
/************************************************************************
* dissettable *
* - this function sets up the processor table. It should be called once *
* when the processor has been selected and before disassembly begins *
************************************************************************/
void disasm::dissettable(void)
{ int i=0;
while((procnames[i].num!=0)&&(procnames[i].num!=options.processor))
i++;
itable=procnames[i].tab;
}
/************************************************************************
* nextiter_code *
* - a basic building block piece of code which skips to the next *
* code/data item in the disassembly (identified by having a length), *
* or returns null if it gets to the end *
************************************************************************/
dsmitem *disasm::nextiter_code(dsmitem *tblock)
{ if(tblock!=NULL)
{ while(!tblock->length)
{ tblock=nextiterator();
if(tblock==NULL)
break;
}
}
return tblock;
}
/************************************************************************
* dsmitem_contains_loc *
* - a basic building block piece of code which returns true if the *
* dsmitem straddles loc *
* - added in v2.22 *
************************************************************************/
bool disasm::dsmitem_contains_loc(dsmitem *d,lptr loc)
{ if((d->addr<loc)&&(d->addr+d->length>loc))
return true;
return false;
}
/************************************************************************
* dsmfindaddrtype *
* - a basic building block piece of code which returns a dsmitem *
* pointer using the list class find to find it by loc and type *
* - added in v2.22 *
************************************************************************/
dsmitem *disasm::dsmfindaddrtype(lptr loc,dsmitemtype typ)
{ dsmitem fnd;
fnd.addr=loc;
fnd.type=typ;
return find(&fnd);
}
/************************************************************************
* oktoname *
* - this checks that we arent trying to name a location which is in the *
* middle of an instruction and returns true if it is ok to assign a *
* name here *
************************************************************************/
bool disasm::oktoname(lptr loc)
{ dsmitem *checker;
// check table for the given address
checker=dsmfindaddrtype(loc,dsmcode);
// NULL returned - must be ok.
if(checker==NULL)
return true;
// check bounds.
if(dsmitem_contains_loc(checker,loc))
return false;
return true;
}
/************************************************************************
* checkvalid *
* - this is called when adding an instruction disassembly to the list *
* - it checks that an instruction isnt being added which would overlap *
* with instructions already in the database *
* - it will delete any names and xrefs or comments which get in the way *
************************************************************************/
bool disasm::checkvalid(dsmitem *newdsm)
{ dsmitem *lstdsm,*deldsm;
lstdsm=dsmfindaddrtype(newdsm->addr,dsmnull);
if(lstdsm==NULL)
return true;
// go through the disassembly items nearby and check for any overlaps.
do
{ if(lstdsm->length)
{ if(newdsm->addr.between(lstdsm->addr,lstdsm->addr+lstdsm->length-1))
return false;
if(lstdsm->addr.between(newdsm->addr,newdsm->addr+newdsm->length-1))
return false;
}
if(lstdsm->addr>=(newdsm->addr+newdsm->length))
break;
lstdsm=nextiterator();
} while(lstdsm!=NULL);
deldsm=dsmfindaddrtype(newdsm->addr,dsmnull);
// now go through them again, and this time delete any names/xrefs which get
// in the way.
do
{ if((!deldsm->length)&&(deldsm->addr.segm==newdsm->addr.segm))
{ if(dsmitem_contains_loc(newdsm,deldsm->addr))
{ switch(deldsm->type)
{ case dsmnameloc:
#ifdef DEBUG
DebugMessage("Deleting Name : %s",deldsm->tptr);
#endif
name.delname(deldsm->addr);
break;
case dsmxref:
#ifdef DEBUG
DebugMessage("Deleting Xref at : %04lx:%04lx",deldsm->addr.segm,deldsm->addr.offs);
#endif
break;
default:
break;
}
delfrom(deldsm);
deldsm=dsmfindaddrtype(newdsm->addr,dsmnull);
if(deldsm==NULL)
return true;
}
}
if(newdsm->addr+newdsm->length<=deldsm->addr)
break;
deldsm=nextiterator();
} while(lstdsm!=NULL);
return true;
}
/************************************************************************
* setcodeoverride *
* - sets a particular override for a given location *
* - this subfunction was created in v211 as it was duplicated code in *
* several functions *
************************************************************************/
void disasm::setcodeoverride(lptr loc,byteoverride typ)
{ dsmitem *findit;
findit=dsmfindaddrtype(loc,dsmcode);
if(findit!=NULL)
{ if((findit->addr==loc)&&(findit->type==dsmcode))
{ findit->override=typ;
dio.updatewindowifinrange(loc);
}
}
}
/************************************************************************
* disargnegate *
* - as well as an override I added some displayflags for a disassembly *
* item. There is a negation item which allows immediates to be *
* negated when displayed, and this function sets or resets the flag *
************************************************************************/
void disasm::disargnegate(lptr loc)
{ dsmitem *findit;
findit=dsmfindaddrtype(loc,dsmcode);
if(findit!=NULL)
{ if((findit->addr==loc)&&(findit->type==dsmcode))
{ findit->displayflags=(byte)(findit->displayflags^DISPFLAG_NEGATE);
dio.updatewindowifinrange(loc);
}
}
}
/************************************************************************
* disargoverdec *
* - sets the decimal override for a disassembly item, given the *
* location *
************************************************************************/
void disasm::disargoverdec(lptr loc)
{ setcodeoverride(loc,over_decimal);
}
/************************************************************************
* disargoversingle *
* - sets the single (float) override for a disassembly item, given the *
* location *
************************************************************************/
void disasm::disargoversingle(lptr loc)
{ setcodeoverride(loc,over_single);
}
/************************************************************************
* disargoverhex *
* - sets the hex (null) override for a disassembly item, given the *
* location *
************************************************************************/
void disasm::disargoverhex(lptr loc)
{ setcodeoverride(loc,over_null);
}
/************************************************************************
* disargoverchar *
* - sets the character override for a disassembly item, given the *
* location *
************************************************************************/
void disasm::disargoverchar(lptr loc)
{ setcodeoverride(loc,over_char);
}
/************************************************************************
* disargoveroffsetdseg *
* - sets the dseg override for a disassembly item, given the location *
* - NB at present it does not affect xrefs, to be done....... *
************************************************************************/
void disasm::disargoveroffsetdseg(lptr loc)
{ dsmitem *findit;
lptr j;
findit=dsmfindaddrtype(loc,dsmcode);
if(findit!=NULL)
{ if((findit->addr==loc)&&(findit->type==dsmcode))
{ findit->override=over_dsoffset;
dio.updatewindowifinrange(loc);
if((options.mode32)&&(findit->length>=4))
{ j.assign(options.dseg,((dword *)(&findit->data[findit->length-4]))[0]);
xrefs.addxref(j,loc);
}
}
}
}
/************************************************************************
* disdatastring *
* - disassembles a string at location loc. *
* - also names the location using the string. *
* - C style *
************************************************************************/
void disasm::disdatastring(lptr loc)
{ dsegitem *dblock;
dsmitem *newdsm;
dword maxlen,actuallen;
char callit[GNAME_MAXLEN+1];
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<2)
return;
actuallen=0;
while((dblock->data+(loc-dblock->addr)+actuallen)[0])
{ actuallen++;
maxlen--;
if(!maxlen)
return;
}
actuallen++;
if(actuallen>0xffff)
return; // tooo big
newdsm=new dsmitem;
initnewdsm(newdsm,loc,dsmcode);
newdsm->tptr=&asmstr[0];
newdsm->length=(word)actuallen; // string length
newdsm->data=dblock->data+(loc-dblock->addr);
if(checkvalid(newdsm))
{ callit[0]='s';
callit[1]='_';
if(actuallen>GNAME_MAXLEN-2)
{ callit[GNAME_MAXLEN]=0;
lstrcpyn(callit+2,(char *)newdsm->data,GNAME_MAXLEN-3);
}
else lstrcpy(callit+2,(char *)newdsm->data);
cleanstring(callit);
addto(newdsm);
name.addname(newdsm->addr,callit);
//check if need to update window.
dio.updatewindowifinrange(loc);
}
else
delete newdsm;
}
/************************************************************************
* disdataucstring *
* - disassembles a string at location loc. *
* - also names the location using the string. *
* - unicode C style *
************************************************************************/
void disasm::disdataucstring(lptr loc)
{ // unicode c-style string.
dsegitem *dblock;
dsmitem *newdsm;
dword maxlen,actuallen;
unsigned int i;
char callit[GNAME_MAXLEN+1];
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<2)
return;
actuallen=0;
while((dblock->data+(loc-dblock->addr)+actuallen)[0])
{ actuallen++;
maxlen--;
if(!maxlen)
return;
actuallen++;
maxlen--;
if(!maxlen)
return;
}
actuallen+=2;
if(actuallen>0xffff)
return; // tooo big
newdsm=new dsmitem;
initnewdsm(newdsm,loc,dsmcode);
newdsm->tptr=&asmstr[3];
newdsm->length=(word)actuallen; // string length
newdsm->data=dblock->data+(loc-dblock->addr);
if(checkvalid(newdsm))
{ callit[0]='s';
callit[1]='_';
i=2;
while(i<GNAME_MAXLEN-1)
{ callit[i]=newdsm->data[(i-2)*2];
i++;
if(i*2>actuallen)
break;
}
callit[i]=0;
cleanstring(callit);
addto(newdsm);
name.addname(newdsm->addr,callit);
//check if need to update window.
dio.updatewindowifinrange(loc);
}
else
delete newdsm;
}
/************************************************************************
* disdataupstring *
* - disassembles a string at location loc. *
* - also names the location using the string. *
* - Unicode Pascal style *
************************************************************************/
void disasm::disdataupstring(lptr loc)
{ // unicode pascal style string.
dsegitem *dblock;
dsmitem *newdsm;
word tlen;
dword maxlen,actuallen;
unsigned int i;
char callit[GNAME_MAXLEN+1];
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<2)
return;
maxlen-=1;
actuallen=0;
tlen=((word *)(dblock->data+(loc-dblock->addr)))[0];
while(tlen)
{ actuallen++;
tlen--;
maxlen--;
if(!maxlen)
return;
actuallen++;
maxlen--;
if(!maxlen)
return;
}
actuallen+=2;
if(actuallen>0xffff)
return; // tooo big
newdsm=new dsmitem;
initnewdsm(newdsm,loc,dsmcode);
newdsm->tptr=&asmstr[4];
newdsm->length=(word)actuallen; // string length
newdsm->data=dblock->data+(loc-dblock->addr);
if(checkvalid(newdsm))
{ callit[0]='s';
callit[1]='_';
i=2;
while(i<GNAME_MAXLEN-1)
{ callit[i]=newdsm->data[(i-1)*2];
i++;
if(i*2>actuallen)
break;
}
callit[i]=0;
cleanstring(callit);
addto(newdsm);
name.addname(newdsm->addr,callit);
//check if need to update window.
dio.updatewindowifinrange(loc);
}
else
delete newdsm;
}
/************************************************************************
* disdatadosstring *
* - disassembles a string at location loc. *
* - also names the location using the string. *
* - DOS style *
************************************************************************/
void disasm::disdatadosstring(lptr loc)
{ dsegitem *dblock;
dsmitem *newdsm;
dword maxlen,actuallen;
char callit[GNAME_MAXLEN+1];
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<2)
return;
actuallen=0;
while((dblock->data+(loc-dblock->addr)+actuallen)[0]!='$')
{ actuallen++;
maxlen--;
if(!maxlen)
return;
}
actuallen++;
if(actuallen>0xffff)
return; // tooo big
newdsm=new dsmitem;
initnewdsm(newdsm,loc,dsmcode);
newdsm->tptr=&asmstr[2];
newdsm->length=(word)actuallen; // string length
newdsm->data=dblock->data+(loc-dblock->addr);
if(checkvalid(newdsm))
{ callit[0]='s';
callit[1]='_';
callit[GNAME_MAXLEN]=0;
if(actuallen>GNAME_MAXLEN-2)
lstrcpyn(callit+2,(char *)newdsm->data,GNAME_MAXLEN-3);
else
lstrcpyn(callit+2,(char *)newdsm->data,actuallen);
cleanstring(callit);
addto(newdsm);
name.addname(newdsm->addr,callit);
//check if need to update window.
dio.updatewindowifinrange(loc);
}
else
delete newdsm;
}
/************************************************************************
* disdatageneralstring *
* - disassembles a string at location loc. *
* - also names the location using the string. *
* - general string is defined as printable characters *
************************************************************************/
void disasm::disdatageneralstring(lptr loc)
{ dsegitem *dblock;
dsmitem *newdsm;
dword maxlen,actuallen;
char callit[GNAME_MAXLEN+1];
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<2)
return;
actuallen=0;
while(isprint((dblock->data+(loc-dblock->addr)+actuallen)[0]))
{ actuallen++;
maxlen--;
if(!maxlen)
return;
}
actuallen++;
if(actuallen>0xffff)
return; // tooo big
newdsm=new dsmitem;
initnewdsm(newdsm,loc,dsmcode);
newdsm->tptr=&asmstr[2];
newdsm->length=(word)actuallen; // string length
newdsm->data=dblock->data+(loc-dblock->addr);
if(checkvalid(newdsm))
{ callit[0]='s';
callit[1]='_';
callit[GNAME_MAXLEN]=0;
if(actuallen>GNAME_MAXLEN-2)
lstrcpyn(callit+2,(char *)newdsm->data,GNAME_MAXLEN-3);
else
lstrcpyn(callit+2,(char *)newdsm->data,actuallen);
cleanstring(callit);
addto(newdsm);
name.addname(newdsm->addr,callit);
//check if need to update window.
dio.updatewindowifinrange(loc);
}
else
delete newdsm;
}
/************************************************************************
* disdatapstring *
* - disassembles a string at location loc. *
* - also names the location using the string. *
* - Pascal style *
************************************************************************/
void disasm::disdatapstring(lptr loc)
{ dsegitem *dblock;
dsmitem *newdsm;
byte tlen;
dword maxlen,actuallen;
char callit[GNAME_MAXLEN+1];
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<2)
return;
actuallen=0;
tlen=(dblock->data+(loc-dblock->addr))[0];
while(tlen)
{ actuallen++;
tlen--;
maxlen--;
if(!maxlen)
return;
}
actuallen++;
if(actuallen>0xffff)
return; // tooo big
newdsm=new dsmitem;
initnewdsm(newdsm,loc,dsmcode);
newdsm->tptr=&asmstr[1];
newdsm->length=(word)actuallen; // string length
newdsm->data=dblock->data+(loc-dblock->addr);
if(checkvalid(newdsm))
{ callit[0]='s';
callit[1]='_';
if(actuallen>GNAME_MAXLEN-2)
{ callit[GNAME_MAXLEN]=0;
lstrcpyn(callit+2,(char *)newdsm->data+1,GNAME_MAXLEN-3);
}
else
{ callit[actuallen+2]=0;
lstrcpyn(callit+2,(char *)newdsm->data+1,actuallen);
}
cleanstring(callit);
addto(newdsm);
name.addname(newdsm->addr,callit);
//check if need to update window.
dio.updatewindowifinrange(loc);
}
else
delete newdsm;
}
/************************************************************************
* disdata *
* - disassembles a dataitem, the common parts of the routines which *
* disassemble particular types (words,dwords, etc) *
************************************************************************/
void disasm::disdata(lptr loc,asminstdata *asmwd,byte len,byteoverride overr)
{ dsegitem *dblock;
dsmitem *newdsm;
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
newdsm=new dsmitem;
initnewdsm(newdsm,loc,dsmcode);
newdsm->tptr=asmwd;
newdsm->length=len;
newdsm->data=dblock->data+(loc-dblock->addr);
newdsm->override=overr;
if(checkvalid(newdsm))
{ addto(newdsm);
//check if need to update window.
dio.updatewindowifinrange(loc);
}
else
delete newdsm;
}
/************************************************************************
* disdataword *
* - disassembles a word at location loc. *
************************************************************************/
void disasm::disdataword(lptr loc)
{ disdata(loc,&asmword[0],2,over_null);
}
/************************************************************************
* disdatadword *
* - disassembles a dword at location loc. *
************************************************************************/
void disasm::disdatadword(lptr loc)
{ disdata(loc,&asmdword[0],4,over_null);
}
/************************************************************************
* disdatasingle *
* - disassembles a single (float) at location loc. *
************************************************************************/
void disasm::disdatasingle(lptr loc)
{ disdata(loc,&asm_fp[0],4,over_null);
}
/************************************************************************
* disdatadouble *
* - disassembles a double (float) at location loc. *
************************************************************************/
void disasm::disdatadouble(lptr loc)
{ disdata(loc,&asm_fp[1],8,over_null);
}
/************************************************************************
* disdatalongdouble *
* - disassembles a long double at location loc. *
************************************************************************/
void disasm::disdatalongdouble(lptr loc)
{ disdata(loc,&asm_fp[2],10,over_null);
}
/************************************************************************
* disdatadsoffword *
* - disassembles an offset data item stored at a location *
* - short for disdatadword and changing it to an offset,ie dword offset *
************************************************************************/
void disasm::disdatadsoffword(lptr loc)
{ disdata(loc,&asmdword[0],4,over_dsoffset);
}
/************************************************************************
* addcomment *
* - called to add an auto comment to the disassembly, where the auto *
* comment will be edittable by the user. this is used in resource *
* analysis. *
************************************************************************/
void disasm::addcomment(lptr loc,char *comment)
{ char *nm;
nm=new char[strlen(comment)+1];
strcpy(nm,comment);
disautocomment(loc,dsmcomment,(unsigned char *)nm);
}
/************************************************************************
* disname_or_ordinal *
* - part of the resource item analysis, this looks for either an *
* ordinal number, or a name, and disassembles the data adding auto *
* comments too. *
************************************************************************/
int disasm::disname_or_ordinal(lptr loc,bool comment_ctrl)
{ dsegitem *dblock;
dword maxlen,idnum;
dblock=dta.findseg(loc);
if(dblock==NULL)
return 0;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<2)
return 0;
idnum=((word *)(dblock->data+(loc-dblock->addr)))[0];
if(idnum==0xffff) // ordinal follows
{ disdataword(loc);
idnum=((word *)(dblock->data+(loc-dblock->addr)))[1];
disdataword(loc+2);
// ctrl class -> add description for class
if(comment_ctrl)
{ switch(idnum)
{ case 0x0080:
addcomment(loc+2,"[Button]");
break;
case 0x0081:
addcomment(loc+2,"[Edit]");
break;
case 0x0082:
addcomment(loc+2,"[Static]");
break;
case 0x0083:
addcomment(loc+2,"[List Box]");
break;
case 0x0084:
addcomment(loc+2,"[Scroll Bar]");
break;
case 0x0085:
addcomment(loc+2,"[Combo Box]");
break;
default:
break;
}
}
return 4;
}
disdataucstring(loc);
return getlength(loc);
}
/************************************************************************
* disdialog *
* - disassembles a dialog resource, by which I mean that data items are *
* disassembled to dwords, strings, etc, and the whole lot is *
* commented with what the fields are *
* - at the moment basename is unused, for future refinement ? *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
void disasm::disdialog(lptr loc,char *basename)
{ lptr cloc;
int ilen,numctrls;
dsmitem *findit;
bool exd;
dsegitem *dblock;
dword maxlen,idnum,hdrstyle;
// ho hum, things are never simple
// - after adding the dialog format i found that some dialogs were just completely
// different - the so called dialogex dialogs, and after much hunting around i found
// some details on microsofts site (wow). so then i hacked the code up to do it....
// dialog header
exd=false;
dblock=dta.findseg(loc);
if(dblock==NULL)
return;
maxlen=dblock->size-(loc-dblock->addr);
if(maxlen<4)
return;
idnum=((dword *)(dblock->data+(loc-dblock->addr)))[0];
if(idnum==0xffff0001)
exd=true; // whoah, dialogex found
/* basic struct is as follows:
struct dialogboxheader
{ unsigned long style,extendedstyle;
unsigned short numberofitems;
unsigned short x,y;
unsigned short cx,cy;
};*/
if(exd)
{ addcomment(loc,"Signature+Version");
disdatadword(loc);
addcomment(loc+4,"HelpID");
disdatadword(loc+4);
loc=loc+8;
}
if(exd)
addcomment(loc,"Extended Style");
else
addcomment(loc,"Style");
disdatadword(loc);
if(exd)
addcomment(loc+4,"Style");
else
addcomment(loc+4,"Extended Style");
disdatadword(loc+4);
addcomment(loc+8,"Number of Items");
disdataword(loc+8);
addcomment(loc+10,"x");
disdataword(loc+10);
addcomment(loc+12,"y");
disdataword(loc+12);
addcomment(loc+14,"cx");
disdataword(loc+14);
addcomment(loc+16,"cy");
disdataword(loc+16);
cloc=loc+18;
addcomment(cloc,"Menu name/ordinal");
ilen=disname_or_ordinal(cloc,false);
cloc+=ilen;
addcomment(cloc,"Class name/ordinal");
ilen=disname_or_ordinal(cloc,false);
cloc+=ilen;
addcomment(cloc,"Caption");
disdataucstring(cloc);
cloc+=getlength(cloc);
findit=dsmfindaddrtype(loc+4*(exd?1:0),dsmcode);
hdrstyle=0;
if(findit!=NULL)
{ if((findit->addr==loc+4*(exd?1:0))&&(findit->type==dsmcode))
hdrstyle=((dword *)findit->data)[0];
}
// i noticed a reference to DS_SHELLFONT on msdn, but what is that ????????
if (hdrstyle&DS_SETFONT) // if ds_setfont then 2 more items...in theory
{ addcomment(cloc,"font pointsize");
disdataword(cloc);
cloc+=2;
if(exd) // more options with exd
{ addcomment(cloc,"weight");
disdataword(cloc);
cloc+=2;
addcomment(cloc,"italic");
//disdatabyte(cloc);
cloc+=1;
addcomment(cloc,"charset");
//disdatabyte(cloc);
cloc+=1;
}
addcomment(cloc,"font");
disdataucstring(cloc);
cloc+=getlength(cloc);
}
// now do controls
findit=dsmfindaddrtype(loc+8,dsmcode);
numctrls=0;
if(findit!=NULL)
{ if((findit->addr==loc+8)&&(findit->type==dsmcode))
numctrls=((word *)findit->data)[0];
}
/*struct ctrlheader
{ unsigned long style,extendedstyle;
unsigned short x,y;
unsigned short cx,cy;
unsigned short wid;
};*/
while(numctrls)
{ if(cloc.offs&0x03)
cloc.offs=(cloc.offs|0x03)+1;
if(exd)
{ addcomment(cloc,"HelpID");
disdatadword(cloc);
cloc+=4;
}
if(exd)
addcomment(cloc,"Extended Style");
else
addcomment(cloc,"Style");
disdatadword(cloc);
if(exd)
addcomment(cloc+4,"Style");
else
addcomment(cloc+4,"Extended Style");
disdatadword(cloc+4);
addcomment(cloc+8,"x");
disdataword(cloc+8);
addcomment(cloc+10,"y");
disdataword(cloc+10);
addcomment(cloc+12,"cx");
disdataword(cloc+12);
addcomment(cloc+14,"cy");
disdataword(cloc+14);
addcomment(cloc+16,"wid");
disdataword(cloc+16);
cloc+=18;
if(exd)
if(cloc.offs&0x03)
cloc.offs=(cloc.offs|0x03)+1;
addcomment(cloc,"Class id");
ilen=disname_or_ordinal(cloc,true);
cloc+=ilen;
addcomment(cloc,"Text");
ilen=disname_or_ordinal(cloc,false);
cloc+=ilen;
addcomment(cloc,"Extra Stuff");
disdataword(cloc);
findit=dsmfindaddrtype(cloc,dsmcode);
if(findit!=NULL)
{ if((findit->addr==cloc)&&(findit->type==dsmcode))
cloc+=((word *)findit->data)[0];
}
cloc+=2;
numctrls--;
}
}
#ifdef __BORLANDC__
#pragma warn +par
#endif
/************************************************************************
* disstringtable *
* - disassembles a string table, by which I mean that the strings are *
* disassembled into strings and the locations are named according to *
* the strings id numbers (I chose this in preference to naming by *
* string because they are a resource, and so the locations will not *
* be referenced, but if the program wants to use them then it will *
* use the id numbers, and make an API call to get hold of them). *
************************************************************************/
void disasm::disstringtable(lptr loc,char *basename)
{ int i;
lptr cloc;
char callit[40];
dword idnum;
int ipt;
dsmitem *lastdsm;
ipt=0;
idnum=0;
if(basename!=NULL)
{ while((basename[ipt])&&(basename[ipt]!=':'))
ipt++;
if(basename[ipt]==':')
{ ipt++;
while(basename[ipt])
{ if(basename[ipt]>='a')
idnum=idnum*16+basename[ipt]-'a'+10;
else
idnum=idnum*16+basename[ipt]-'0';
ipt++;
if(basename[ipt]=='h')
break;
}
}
}
cloc=loc;
for(i=0;i<16;i++) // 16 strings in a stringtable, of type unicode_pascal.
{ disdataupstring(cloc);
if(idnum)
{ wsprintf(callit,"String_ID_%lx",(idnum-1)*16+i);
name.addname(cloc,callit);
}
lastdsm=dsmfindaddrtype(cloc,dsmcode);
if(lastdsm==NULL)
break;
if((lastdsm->type!=dsmcode)||(lastdsm->addr!=cloc))
break;
cloc+=lastdsm->length;
}
}
/************************************************************************
* codeseek *
* - this is the aggressive code search if it is chosen as an option. It *
* has a low priority, and so appears near the end of the queue. It *
* hunts for possible code to disassemble in the code segments (added *
* as a task during file load for each code segment). When it finds an *
* undisassembled byte it tries to disassemble from that point, and *
* drops into the background again until disassembly has been done *
* - note that if it doesnt find anything in a short time it exits and *
* puts a continuation request in to the scheduler, this ensures that *
* userrequests are answered quickly *
************************************************************************/
void disasm::codeseek(lptr loc)
{ bool doneblock;
dsegitem *dblock;
dsmitem *tblock;
int dcount,ilength;
// check if already done.
dblock=dta.findseg(loc); // find segment item.
dcount=0;
if(dblock==NULL)
return;
do{
doneblock=false;
ilength=1;
dsmfindaddrtype(loc,dsmnull);
tblock=nextiterator();
tblock=nextiter_code(tblock);
if(tblock!=NULL)
{ if((tblock->addr.segm==loc.segm)&&(dsmitem_contains_loc(tblock,loc)))
doneblock=true;
while(tblock->addr==loc)
{ if(tblock->length)
ilength=tblock->length;
if(tblock->type!=dsmcomment)
{ doneblock=true;
break;