-
Notifications
You must be signed in to change notification settings - Fork 20
/
Misc.pas
2501 lines (2379 loc) · 63.8 KB
/
Misc.pas
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
unit Misc;
interface
Uses Windows,Classes,Infos,KnowledgeBase,Decompiler, Graphics,Def_main,Def_know;
Var
IDRVersion:AnsiString;
//global API
Procedure FreeInvalidate(var Obj);
Function FirstWord(const str:AnsiString;from:Integer=1;Last:Integer=0):Integer;
Function StrTok(const str:AnsiString;Delim:TChars):AnsiString;
Function MakeString(p:PAnsiChar;L:Integer):AnsiString;
Function Adr2Pos(adr:Integer):Integer;
function Val2Str(Val:Integer;dig:Byte = 0): AnsiString;
function Pos2Adr(_Pos:Integer): Integer;
Function AddToBSSInfos(Adr:Integer; AName, ATypeName:AnsiString):InfoRec;
Procedure AddFieldXref(fInfo:FIELDINFO; ProcAdr:Integer; ProcOfs:Integer; _type:Char);
procedure AddPicode(_Pos:Integer; Op:Byte; _Name:AnsiString; Ofs:Integer);
Function ArgsCmpFunction(item1, item2:Pointer):Integer;
Function CanReplace(var fromName, toName:AnsiString):Boolean;
procedure ClearFlag(flag:TCflagSet; p:Integer);
procedure ClearFlags(flag:TCflagSet; p, num:Integer);
Procedure Copy2Clipboard(items:TStrings; leftMargin:Integer; asmCode:Boolean);
Function ExportsCmpFunction(item1, item2:Pointer):Integer;
Function ExtractClassName(const AName:AnsiString):AnsiString;
Function ExtractProcName(const AName:AnsiString):AnsiString;
Function ExtractName(const AName:AnsiString):AnsiString;
Function ExtractType(const AName:AnsiString):AnsiString;
procedure FillArgInfo(k:Integer; callkind:Byte; argInfo:PArgInfo; var p:PAnsiChar; Var s:Integer);
function FloatNameToFloatType(AName:AnsiString): TFloatKind;
Function GetArrayElementType(arrType:AnsiString):AnsiString;
Function GetArrayElementTypeSize(arrType:AnsiString):Integer;
Function GetArrayIndexes(arrType:AnsiString; ADim:Integer; var LowIdx, HighIdx:Integer):Boolean;
Function GetArraySize(arrType:AnsiString):Integer;
Function GetChildAdr(Adr:Integer):Integer;
Function GetClassAdr(const AName:AnsiString):Integer;
Function GetClassSize(Adr:Integer):Integer;
Function GetClsName(Adr:Integer):AnsiString;
Function GetDefaultProcName(Adr:Integer):AnsiString;
Function GetDynaInfo(adr:Integer; id:Word; var dynAdr:Integer):AnsiString;
Function GetDynArrayTypeName(adr:Integer):AnsiString;
Function GetEnumerationString(TypeName:AnsiString; Val:Variant):AnsiString;
Function GetImmString(Val:Integer):AnsiString; Overload;
Function GetImmString(TypeName:AnsiString; Val:Integer):AnsiString; Overload;
function GetInfoRec(adr:Integer): InfoRec;
Function GetLastLocPos(fromAdr:Integer):Integer;
Function GetModuleVersion(const module:AnsiString):AnsiString;
Function GetNearestArgA(fromPos:Integer):Integer;
Function GetNearestDownInstruction(fromPos:Integer):Integer; Overload;
function GetNearestDownInstruction(fromPos:Integer; Instruction:AnsiString): Integer; overload;
Function GetNearestUpPrefixFs(fromPos:Integer):Integer;
Function GetNthUpInstruction(fromPos, N:Integer):Integer;
Function GetNearestUpInstruction(fromPos:Integer):Integer; Overload;
Function GetNearestUpInstruction(fromPos, toPos:Integer):Integer; Overload;
Function GetNearestUpInstruction(fromPos, toPos, no:Integer):Integer; Overload;
function GetNearestUpInstruction1(fromPos, toPos:Integer; Instruction:AnsiString): Integer;
Function GetNearestUpInstruction2(fromPos, toPos:Integer; Instruction1, Instruction2:PAnsiChar):Integer;
Function GetParentAdr(Adr:Integer):Integer;
Function GetParentName(adr:Integer):AnsiString; Overload;
Function GetParentName(const ClasName:AnsiString):AnsiString; Overload;
Function GetParentSize(Adr:Integer):Integer;
Function GetProcRetBytes(pInfo:MProcInfo):Integer;
Function GetProcSize(fromAdr:Integer):Integer;
Function GetRecordSize(AName:AnsiString):Integer;
Function GetRecordFields(AOfs:Integer; ARecType:AnsiString):AnsiString;
Function GetAsmRegisterName(Idx:Integer):AnsiString;
Function GetDecompilerRegisterName(Idx:Integer):AnsiString;
Function GetSetString(TypeName:AnsiString; ValAdr:PAnsiChar):AnsiString;
Function GetStopAt(vmtAdr:Integer):Integer;
Function GetOwnTypeAdr(AName:AnsiString):Integer;
Function GetOwnTypeByName(AName:AnsiString):PTypeRec;
Function GetTypeDeref(ATypeName:AnsiString):AnsiString;
Function GetTypeKind(AName:AnsiString; var size:Integer):LKind;
Function GetPackedTypeSize(AName:AnsiString):Integer;
Function GetTypeName(Adr:Integer):AnsiString;
Function GetTypeSize(AName:AnsiString):Integer;
Function ImportsCmpFunction(item1, item2:Pointer):Integer;
Function IsADC(Idx:Integer):Boolean;
Function IsDefaultName(AName:AnsiString):Boolean;
Function IsInheritsByAdr(const Adr1, Adr2:Integer):Boolean;
Function IsInheritsByClassName(const Name1, Name2:AnsiString):Boolean;
Function IsInheritsByProcName(const Name1, Name2:AnsiString):Boolean;
Function IsSameRegister(Idx1, Idx2:Integer):Boolean;
Function IsValidCodeAdr(Adr:Integer):Boolean;
Function IsValidCString(p:Integer):Boolean;
Function IsValidImageAdr(Adr:Integer):Boolean;
Function IsValidName(len, p:Integer):Boolean;
Function IsValidString(len, p:Integer):Boolean;
Procedure MakeGvar(recN:InfoRec; adr, xrefAdr:Integer);
Function MakeGvarName(adr:Integer):AnsiString;
Function MethodsCmpFunction(item1, item2:Pointer):Integer;
procedure OutputDecompilerHeader(var f:TextFile);
function IsFlagSet(flag:TCflagSet; p:Integer): Boolean;
procedure SetFlag(flag:TCflagSet; p:Integer);
procedure SetFlags(flag:TCFlagSet; p, num:Integer);
Function SortUnitsByAdr(item1, item2:Pointer):Integer;
Function SortUnitsByNam(item1, item2:Pointer):Integer;
Function SortUnitsByOrd(item1, item2:Pointer):Integer;
Function TransformString(str:PAnsiChar; len:Integer):AnsiString;
Function TransformUString(codePage:Word; data:PWideChar; len:Integer):AnsiString;
Function TrimTypeName(const TypeName:AnsiString):AnsiString;
Function TypeKind2Name(kind:LKind):AnsiString;
//Decompiler
Function InputDialogExec(caption, labelText, text:AnsiString):AnsiString;
Function ManualInput(procAdr, curAdr:Integer; caption, labelText:AnsiString):AnsiString;
Procedure SaveCanvas(ACanvas:TCanvas);
Procedure RestoreCanvas(ACanvas:TCanvas);
Procedure DrawOneItem(AItem:AnsiString; ACanvas:TCanvas; var ARect:TRect; AColor:TColor; flags:Integer);
Function OutputHex(v:Integer;sign:Boolean;dig:Integer=0):AnsiString;
var
StringBuf:Array [0..MAXSTRBUFFER] of Char; //Buffer to make string
implementation
Uses TypInfo,Types,Forms,SysUtils,Main,Def_disasm,Def_info,Variants,StrUtils,
Controls,Clipbrd,InputDlg,Scanf;
const
{TypeKinds:Array[0..21] of AnsiString =
(
'Unknown',
'Integer',
'Char',
'Enumeration',
'Float',
'ShortString',
'Set',
'Class',
'Method',
'WChar',
'AnsiString',
'WideString',
'Variant',
'Array',
'Record',
'Interface',
'Int64',
'DynArray',
'UString',
'ClassRef',
'Pointer',
'Procedure'
);}
IdxToIdx32Tab:Array[0..23] of Integer =
(16, 17, 18, 19, 16, 17, 18, 19, 16, 17, 18, 19, 20, 21, 22, 23, 16, 17, 18, 19, 20, 21, 22, 23);
Var
SavedPenColor, SavedBrushColor, SavedFontColor:TColor;
SavedBrushStyle:TBrushStyle;
Procedure FreeInvalidate(var Obj);
var
temp:TObject;
Begin
temp:=TObject(Obj);
Pointer(Obj):=Pointer(1);
temp.Free;
end;
Function FirstWord(const str:AnsiString;from:Integer=1;Last:Integer=0):Integer;
var
k:Integer;
Begin
Result:=from;
if Last<>0 then k:=Last else k:=Length(str);
while (Result<=k)and not(str[Result] in [' ',#13,#10,#9]) Do Inc(Result);
Dec(Result);
end;
Function StrTok(const str:AnsiString;Delim:TChars):AnsiString;
Const
s:Ansistring = '';
p:Integer = 1;
var
j,k:Integer;
Begin
if str<>'' then
begin
s:=str;
p:=1;
end;
k:=Length(s);
while (p<=k) and (s[p] in delim) do Inc(p);
j:=p;
while (p<=k) and not (s[p] in delim) do Inc(p);
Result:=Copy(s,j,p-j);
end;
Function MakeString(p:PAnsiChar;L:Integer):AnsiString;
Begin
SetLength(Result,L);
if L<>0 then StrLCopy(PAnsiChar(Result),p,L);
end;
Function Adr2Pos (adr:Integer):Integer;
var
n,ofs:Integer;
segInfo:PSegmentInfo;
Begin
ofs:=0;
for n := 0 to SegmentList.Count-1 do
begin
segInfo := SegmentList[n];
if (segInfo.Start <= adr) and (adr < segInfo.Start + segInfo.Size) then
begin
if (segInfo.Flags and IMAGE_SCN_MEM_PRELOAD)<>0 then Result:= -1
Else Result:=ofs + (adr - segInfo.Start);
Exit;
End;
if (segInfo.Flags and IMAGE_SCN_MEM_PRELOAD)=0 then Inc(ofs, segInfo.Size);
End;
Result:= -2;
end;
Function Pos2Adr (_Pos:Integer):Integer;
var
n, fromPos, toPos:Integer;
segInfo:PSegmentInfo;
Begin
toPos:=0;
for n := 0 to SegmentList.Count-1 do
begin
segInfo := SegmentList[n];
if (segInfo.Flags and IMAGE_SCN_MEM_PRELOAD)=0 then
begin
fromPos := toPos;
Inc(toPos, segInfo.Size);
if (fromPos <= _Pos) and (_Pos < toPos) then
begin
Result:=segInfo.Start + (_Pos - fromPos);
Exit;
End;
end;
End;
Result:=0;
end;
//Can replace type "fromName" to type "toName"?
Function CanReplace (Var fromName, toName:AnsiString):Boolean;
Begin
Result:=False;
//Skip empty toName
if toName <> '' then
//We can replace empty "fromName" or name "byte", "word", "dword'
Result:= (fromName = '') or SameText(fromName, 'byte') or SameText(fromName, 'word')
or SameText(fromName, 'dword');
end;
Function GetDefaultProcName (Adr:Integer):AnsiString;
Begin
Result:= 'sub_' + Val2Str(adr,8);
end;
Function Val2Str (Val:Integer;dig:Byte = 0):AnsiString;
Begin
Result:=IntToHex(Val, dig);
end;
Function AddToBSSInfos (Adr:Integer; AName, ATypeName:AnsiString):InfoRec;
var
_key:AnsiString;
idx:Integer;
Begin
_key := Val2Str(Adr,8);
idx := BSSInfos.IndexOf(_key);
if idx = -1 then
begin
Result := InfoRec.Create(-1, ikData);
Result.SetName(AName);
Result._type := ATypeName;
BSSInfos.AddObject(_key, Result);
end
else
begin
Result := InfoRec(BSSInfos.Objects[idx]);
if Result._type = '' then Result._type := ATypeName;
end;
end;
Function MakeGvarName (adr:Integer):AnsiString;
Begin
Result:='gvar_' + Val2Str(adr,8);
end;
Procedure MakeGvar (recN:InfoRec; adr, xrefAdr:Integer);
Begin
if not recN.HasName then recN.SetName(MakeGvarName(adr));
if xrefAdr<>0 then recN.AddXref('C', xrefAdr, 0);
end;
Procedure FillArgInfo (k:Integer; callkind:Byte; argInfo:PArgInfo; var p:PAnsiChar; Var s:Integer);
var
ndx,locflags:Integer;
wlen:Word;
Begin
argInfo.Tag := PByte(p)^;
Inc(p);
locflags := PInteger(p)^;
Inc(p,4);
argInfo.in_Reg := (locflags and 8)<>0;
ndx := PInteger(p)^;
Inc(p, 4);
if callkind=0 then
begin
//fastcall
if argInfo.in_Reg and (k < 3) then argInfo.Ndx := k // << 24;???
else argInfo.Ndx := ndx;
end
else
begin
//stdcall, cdecl, pascal
argInfo.Ndx := s;
Inc(s, 4);
end;
argInfo.Size := 4;
wlen := PWord(p)^;
Inc(p,2);
argInfo.Name := MakeString(p, wlen);
Inc(p, wlen + 1);
wlen := PWord(p)^;
Inc(p, 2);
argInfo.TypeDef := TrimTypeName(MakeString(p, wlen));
Inc(p, wlen + 1);
end;
Function TrimTypeName (Const TypeName:AnsiString):AnsiString;
var
_pos:Integer;
i:Integer;
Begin
if TypeName='' then Result:=''
Else
begin
_pos := Pos('.',TypeName);
//No '.' in TypeName or TypeName begins with '.'
if (_pos = 0) or (_pos = 1) then Result:=TypeName
//èëè ýòî èìÿ òèïà range
else if TypeName[_pos + 1] = '.' then Result:=TypeName
else
begin
//Check special symbols upto '.'
for i:=1 to Length(TypeName) do
begin
if TypeName[i] = '.' then break
Else if (TypeName[i] < '0') or (TypeName[i]= '<') then
begin
Result:=TypeName;
Exit;
End;
end;
Result:=ExtractProcName(TypeName);
End;
end;
end;
Function IsValidImageAdr (Adr:Integer):Boolean;
Begin
Result:= (Adr >= Integer(CodeBase)) and (Adr < Integer(CodeBase) + ImageSize);
end;
Function IsValidCodeAdr (Adr:Integer):Boolean;
Begin
Result:=(Adr >= Integer(CodeBase)) and (Adr < Integer(CodeBase) + CodeSize);
end;
Function ExtractClassName (Const AName:AnsiString):AnsiString;
var
p:Integer;
Begin
Result:='';
if AName <>'' then
begin
p:= Pos('.',AName);
if p<>0 then Result:=Copy(AName,1, p - 1);
End;
end;
Function ExtractProcName (Const AName:AnsiString):AnsiString;
var
p:Integer;
Begin
Result:='';
if AName <>'' then
begin
p:= Pos('.',AName);
if p<>0 then Result:=Copy(AName,p+1, Length(AName))
else Result:=AName;
End;
end;
Function ExtractName (Const AName:AnsiString):AnsiString;
var
p:Integer;
Begin
Result:='';
if AName <>'' then
begin
p:= Pos(':',AName);
if p<>0 then Result:=Copy(AName,1, p-1);
End;
end;
Function ExtractType (Const AName:AnsiString):AnsiString;
var
p:Integer;
Begin
Result:='';
if AName <>'' then
begin
p:= Pos(':',AName);
if p<>0 then Result:=Copy(AName,p+1, Length(AName));
End;
end;
//Return position of nearest up argument eax from position fromPos
Function GetNearestArgA (fromPos:Integer):Integer;
Begin
Result := fromPos-1;
while true do
begin
if IsFlagSet([cfInstruction], Result) then
begin
if IsFlagSet([cfProcStart], Result) then break;
if IsFlagSet([cfSetA], Result) then Exit;
End;
Dec(Result);
end;
Result:= -1;
end;
//Return position of nearest up instruction with segment prefix fs:
Function GetNearestUpPrefixFs (fromPos:Integer):Integer;
Var
_disInfo:TDisInfo;
Begin
if fromPos>=0 then
begin
Result:=fromPos - 1;
while Result>=0 Do
begin
if IsFlagSet([cfInstruction], Result) then
begin
frmDisasm.Disassemble(Pos2Adr(Result), @_disInfo, Nil);
if _disInfo.SegPrefix = 4 Then Exit;
End;
if IsFlagSet([cfProcStart], Result) then break;
Dec(Result);
End;
End;
Result:= -1;
end;
//Return position of nearest up instruction from position fromPos
Function GetNearestUpInstruction (fromPos:Integer):Integer; Overload;
Begin
if fromPos >= 0 Then
begin
Result:= fromPos - 1;
while Result>=0 do
begin
if IsFlagSet([cfInstruction], Result) then Exit;
if IsFlagSet([cfProcStart], Result) then break;
Dec(Result);
End;
end;
result:=-1;
end;
//Return position of N-th up instruction from position fromPos
Function GetNthUpInstruction (fromPos, N:Integer):Integer;
Begin
if fromPos>=0 then
begin
Result:= fromPos - 1;
while Result>= 0 do
begin
if IsFlagSet([cfInstruction], Result) then
begin
Dec(N);
if N=0 then Exit;
End;
if IsFlagSet([cfProcStart], Result) then break;
Dec(Result);
End;
end;
Result:= -1;
end;
//Return position of nearest up instruction from position fromPos
Function GetNearestUpInstruction (fromPos, toPos:Integer):Integer; Overload;
Begin
If fromPos>=0 then
begin
Result:= fromPos - 1;
While Result>= toPos do
begin
if IsFlagSet([cfInstruction], Result) then Exit;
if IsFlagSet([cfProcStart], Result) then break;
Dec(Result);
End;
end;
Result:=-1;
end;
Function GetNearestUpInstruction (fromPos, toPos, no:Integer):Integer; Overload;
Begin
If fromPos>=0 then
begin
Result:= fromPos - 1;
While Result>= toPos do
begin
if IsFlagSet([cfInstruction], Result) then
begin
Dec(no);
if no=0 Then Exit;
End;
Dec(Result);
end;
end;
Result:=-1;
end;
//Return position of nearest up instruction from position fromPos
Function GetNearestUpInstruction1 (fromPos, toPos:Integer; Instruction:AnsiString):Integer;
var
len:Integer;
_DisInfo:TDisInfo;
Begin
If fromPos>=0 then
begin
len:=Length(Instruction);
Result:= fromPos - 1;
while Result>= toPos do
begin
if IsFlagSet([cfInstruction], Result) then
begin
frmDisasm.Disassemble(Pos2Adr(Result), @_DisInfo, Nil);
if (len<>0) and (_DisInfo.Mnem=Instruction) then Exit;
end;
if IsFlagSet([cfProcStart], Result) then break;
Dec(Result);
End;
End;
Result:=-1;
end;
//Return position of nearest up instruction from position fromPos
Function GetNearestUpInstruction2 (fromPos, toPos:Integer; Instruction1, Instruction2:PAnsiChar):Integer;
var
len1,len2:Integer;
_DisInfo:TDisInfo;
Begin
If fromPos>=0 then
begin
len1:=Length(Instruction1);
len2:=Length(Instruction2);
Result:= fromPos - 1;
while Result>= toPos do
begin
if IsFlagSet([cfInstruction], Result) then
begin
frmDisasm.Disassemble(Pos2Adr(Result), @_DisInfo, Nil);
if ((len1<>0) and (_DisInfo.Mnem=Instruction1)) or
((len2<>0) and (_DisInfo.Mnem=Instruction2)) Then Exit;
End;
if IsFlagSet([cfProcStart], result) then break;
Dec(Result);
End;
End;
Result:=-1;
end;
//Return position of nearest down instruction from position fromPos
Function GetNearestDownInstruction (fromPos:Integer):Integer; Overload;
var
instrLen:Integer;
DisInfo:TDisInfo;
begin
Result:=-1;
If fromPos >= 0 then
begin
instrLen := frmDisasm.Disassemble(Pos2Adr(fromPos), @DisInfo, Nil);
if instrLen<>0 then Result:= fromPos + instrLen;
End;
end;
//Return position of nearest down "Instruction" from position fromPos
Function GetNearestDownInstruction (fromPos:Integer; Instruction:AnsiString):Integer; Overload;
var
instrLen, len:Integer;
curPos:Integer;
DisInfo:TDisInfo;
begin
Result:=-1;
len:=Length(Instruction);
curPos := fromPos;
if fromPos >= 0 then
while true do
begin
instrLen := frmDisasm.Disassemble(Pos2Adr(curPos), @DisInfo, Nil);
if instrLen=0 then
begin
Inc(curPos);
continue;
End;
if (len<>0) and (DisInfo.Mnem=Instruction) then
Begin
Result:= curPos + instrLen;
Exit;
end;
if DisInfo.Ret then break;
Inc(curPos, instrLen);
end;
end;
Function IsFlagSet (flag:TCflagSet; p:Integer):Boolean;
Begin
//!!!
if (p < 0) or (p >= TotalSize) then
begin
dummy := 1;
Result:=false;
End
else Result:=(flag * FlagList[p] <> []);
end;
Procedure SetFlag (flag:TCflagSet; p:Integer);
Begin
//!!!
if (p < 0) or (p >= TotalSize) then dummy := 1
else if Assigned(FlagList) then FlagList[p]:=FlagList[p] + flag;
end;
Procedure SetFlags (flag:TCFlagSet; p, num:Integer);
var
i:Integer;
Begin
//!!!
if (p < 0) or (p + num >= TotalSize) then dummy := 1
Else if Assigned(FlagList) then for i := P to p + num-1 do
FlagList[i]:=FlagList[i] + flag;
end;
Procedure ClearFlag (flag:TCflagSet; p:Integer);
Begin
//!!!
if (p < 0) or (p >= TotalSize) then dummy := 1
else if Assigned(FlagList) then FlagList[p]:=FlagList[p] - flag;
end;
Procedure ClearFlags (flag:TCflagSet; p, num:Integer);
var
i:Integer;
Begin
if (p < 0) or (p + num > TotalSize) Then dummy := 1
else if Assigned(FlagList) then for i := p to p + num-1 do
FlagList[i]:=FlagList[i] - flag;
end;
//pInfo must contain pInfo->
Function GetProcRetBytes (pInfo:MProcInfo):Integer;
var
_pos,_curAdr:Integer;
_disInfo:TDisInfo;
Begin
_pos := pInfo.DumpSz - 1;
_curAdr := Integer(CodeBase) + _pos;
while _pos >= 0 do
begin
frmDisasm.Disassemble(pInfo.Dump + _pos, _curAdr, @_disInfo, Nil);
if _disInfo.Ret then
begin
//ImmPresent
if _disInfo.OpType[0] = otIMM then Result:=_disInfo.Immediate
else Result:=0;
Exit;
End;
Dec(_pos);
Dec(_curAdr);
End;
Result:=0;
end;
Function GetProcSize (fromAdr:Integer):Integer;
var
recN:InfoRec;
Begin
Result := 0;
recN := GetInfoRec(fromAdr);
if Assigned(recN) and Assigned(recN.procInfo) then Result := recN.procInfo.procSize;
if Result=0 then Result := FMain.EstimateProcSize(fromAdr);
(*
int pos, size = 0;
for (pos = Adr2Pos(fromAdr); pos < TotalSize; pos++)
{
size++;
if (IsFlagSet(cfProcEnd, pos)) break;
}
return size;
*)
end;
Function GetRecordSize (AName:AnsiString):Integer;
var
len:Byte;
_uses:TWordDynArray;
idx, _pos:Integer;
tInfo:MTypeInfo;
recT:PTypeRec;
str:AnsiString;
recFile:Text;
Begin
//Manual
Result:=0;
//File
str:=FMain.WrkDir + '\types.idr';
If FileExists(str) then
begin
AssignFile(recFile,str);
Reset(recFile);
try
while not Eof(recFile) do
begin
Readln(recFile,str);
if Pos(AName+'=',str) = 1 then
begin
_pos := Pos('size=',str);
if _pos<>0 then
begin
sscanf(PAnsiChar(str)+_pos+5,'%lX',[@Result]);
Exit;
End;
End;
end;
Finally
CloseFile(recFile);
End;
End;
//KB
_uses := KBase.GetTypeUses(PAnsiChar(AName));
idx := KBase.GetTypeIdxByModuleIds(_uses, PAnsiChar(AName));
_uses:=Nil;
if idx <> -1 then
begin
idx := KBase.TypeOffsets[idx].NamId;
if KBase.GetTypeInfo(idx, [INFO_DUMP], tInfo) then
begin
Result:=tInfo.Size;
Exit;
end;
End;
//RTTI
recT := GetOwnTypeByName(AName);
if Assigned(recT) and (recT.kind = ikRecord) then
begin
_pos := Adr2Pos(recT.adr);
Inc(_pos, 4);//SelfPtr
Inc(_pos);//TypeKind
len := Byte(Code[_pos]);
Inc(_pos, len + 1);//Name
Result:=PInteger(Code + _pos)^;
Exit;
End;
end;
Function GetRecordFields (AOfs:Integer; ARecType:AnsiString):AnsiString;
var
len, numOps:Byte;
kind:LKind;
p:PAnsiChar;
dw, len1:Word;
_uses:TWordDynArray;
i, k, _idx, _pos, _elNum, _elOfs, _size, _case, _offset:Integer;
typeAdr:Integer;
recT:PTypeRec;
tInfo:MTypeInfo;
str, _name, _typeName:AnsiString;
recFile:Text;
Begin
Result:='';
if ARecType = '' then Exit;
//File
str:=FMain.WrkDir + '\types.idr';
if FileExists(str) Then
begin
AssignFile(recFile,str);
Reset(recFile);
try
while not Eof(recFile) do
begin
Readln(recFile,str);
if Pos(ARecType+'=',str) = 1 then
begin
while not eof(recFile) do
begin
Readln(recFile,str);
if Pos('end;',str)<>0 then break;
if Pos('//' + Val2Str(AOfs),str)<>0 then
begin
result := str;
_pos := LastDelimiter(';',Result);
if _pos<>0 then SetLength(Result,_pos - 1);
Exit;
End;
End;
End;
End;
Finally
closeFile(recFile);
end;
end;
//KB
_uses := KBase.GetTypeUses(PAnsiChar(ARecType));
_idx := KBase.GetTypeIdxByModuleIds(_uses, PAnsiChar(ARecType));
_uses:=Nil;
if _idx <> -1 then
begin
_idx := KBase.TypeOffsets[_idx].NamId;
if KBase.GetTypeInfo(_idx, [INFO_FIELDS], tInfo) then
begin
if tInfo.FieldsNum<>0 then
begin
p := tInfo.Fields;
for k := 1 to tInfo.FieldsNum do
begin
//Scope
Inc(p);
_offset := PInteger(p)^;
Inc(p, 4);
_case := PInteger(p)^;
Inc(p, 4);
//Name
len1 := PWord(p)^;
Inc(p, 2);
_name := MakeString(p, len1);
Inc(p, len1 + 1);
//Type
len1 := PWord(p)^;
Inc(p, 2);
_typeName := TrimTypeName(MakeString(p, len1));
Inc(p, len1 + 1);
kind := GetTypeKind(_typeName, _size);
if kind = ikRecord then
begin
_size := GetRecordSize(_typeName);
if (AOfs >= _offset) and (AOfs < _offset + _size) then
result:=result + _name + '.' + GetRecordFields(AOfs - _offset, _typeName);
end
else if (AOfs >= _offset) and (AOfs < _offset + _size) then
begin
if _size > 4 then
result := _name + '+' + IntToStr(AOfs - _offset) + ':' + _typeName
else
result := _name + ':' + _typeName;
End;
End;
End;
End;
End;
if result <> '' Then Exit;
//RTTI
recT := GetOwnTypeByName(ARecType);
if Assigned(recT) and (recT.kind = ikRecord) then
begin
_pos := Adr2Pos(recT.adr);
Inc(_pos, 4);//SelfPtr
Inc(_pos);//TypeKind
len := Byte(Code[_pos]);
Inc(_pos, len + 1);//Name
Inc(_pos, 4);//Size
_elNum := PInteger(Code + _pos)^;
Inc(_pos, 4);
for i := 1 to _elNum do
begin
typeAdr := PInteger(Code + _pos)^;
Inc(_pos, 4);
_elOfs := PInteger(Code + _pos)^;
Inc(_pos, 4);
_typeName := GetTypeName(typeAdr);
kind := GetTypeKind(_typeName, _size);
if kind = ikRecord then
begin
_size := GetRecordSize(_typeName);
if (AOfs >= _elOfs) and (AOfs < _elOfs + _size) then
result := 'f' + Val2Str(_elOfs) + '.' + GetRecordFields(AOfs - _elOfs, _typeName);
end
else if (AOfs >= _elOfs) and (AOfs < _elOfs + _size) then
begin
if _size > 4 then
result := 'f' + Val2Str(_elOfs) + '+' + IntToStr(AOfs - _elOfs) + ':' + _typeName
else
result := 'f' + Val2Str(_elOfs) + ':' + _typeName;
End;
End;
if DelphiVersion >= 2010 then
begin
//NumOps
numOps := Byte(Code[_pos]);
Inc(_pos);
for i := 1 to numOps do //RecOps
Inc(_pos, 4);
_elNum := PInteger(Code + _pos)^;
Inc(_pos, 4); //RecFldCnt
for i := 1 to _elNum do
begin
//TypeRef
typeAdr := PInteger(Code + _pos)^;
Inc(_pos, 4);
//FldOffset
_elOfs := PInteger(Code + _pos)^;
Inc(_pos, 4);
//Flags
Inc(_pos);
//Name
len := Byte(Code[_pos]);
Inc(_pos);
_name := MakeString(Code + _pos, len);
Inc(_pos, len);
_typeName := GetTypeName(typeAdr);
kind := GetTypeKind(_typeName, _size);
if kind = ikRecord then
begin
_size := GetRecordSize(_typeName);
if (AOfs >= _elOfs) and (AOfs < _elOfs + _size) then
result := _name + '.' + GetRecordFields(AOfs - _elOfs, _typeName);
end
else if (AOfs >= _elOfs) and (AOfs < _elOfs + _size) then
begin
if _size > 4 then
result := _name + '+' + IntToStr(AOfs - _elOfs) + ':' + _typeName
else
result := _name + ':' + _typeName;
End;
//AttrData
dw := PWord(Code + _pos)^;
Inc(_pos, dw);//ATR!!
End;
End;
End;
end;
Function GetAsmRegisterName (Idx:Integer):AnsiString;
Begin
Result:='';
if (Idx >= 0) and (Idx < 32) then
begin
if Idx >= 31 then Result:='ST(' + IntToStr(Idx - 31) + ')'
Else if Idx = 30 then Result:= 'ST'
else if Idx >= 24 then Result:= SegRegTab[Idx - 24]
else if Idx >= 16 then Result:=Reg32Tab[Idx - 16]
Else if Idx >= 8 then Result:=Reg16Tab[Idx - 8]
else Result:=Reg8Tab[Idx];
End;
end;
Function GetDecompilerRegisterName (Idx:Integer):AnsiString;
Begin
Result:='';
If (Idx >= 0) and (Idx < 32) Then
begin
if Idx >= 16 then Result:=UpperCase(Reg32Tab[Idx - 16])
Else if Idx >= 8 then Result:=UpperCase(Reg32Tab[Idx - 8])
else Result:=UpperCase(Reg32Tab[Idx]);
End;
end;
Function IsValidName (len, p:Integer):Boolean;
var
i:Integer;
b:Char;
Begin
Result:=False;
if len=0 then Exit;
for i := P to p + len-1 do
begin
//if IsFlagSet(cfCode, i) then Exit;
b := Code[i];
//first symbol may be letter or '_' or '.' or ':'
if i = p then
begin
if b in ['A'..'z','.','_',':'] then continue
else Exit;
End;
if b > #127 then Exit;
//if ((b < '0' || b > 'z') && b != '.' && b != '_' && b != ':' && b != '$') return false;
End;
Result:=True;
end;
Function IsValidString (len, p:Integer):Boolean;
var
i:Integer;
Begin
Result:=False;