-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuIced.pas
7688 lines (6733 loc) · 283 KB
/
uIced.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 uIced;
{
Iced (Dis)Assembler
TetzkatLipHoka 2022-2024
}
// MS Finish details ..
interface
{$WARN UNSAFE_CODE OFF}
{$WARN COMPARING_SIGNED_UNSIGNED OFF}
{$IF CompilerVersion >= 22}
{$LEGACYIFEND ON}
{$IFEND}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{.$DEFINE DECODER_LOCAL_POSITION} // Buffer-Position is saved in Local-Variable, updated each Decode
{.$DEFINE SynUnicode}
{.$DEFINE AssemblyTools} // MS test again in the end
{.$DEFINE TEST_FUNCTIONS}
uses
Classes, SysUtils,
{$IFDEF TEST_FUNCTIONS}Windows,{$ENDIF}
{$IF NOT Defined( UNICODE ) AND Defined( SynUnicode )}SynUnicode,{$IFEND}
{$IFDEF AssemblyTools}uAssemblyTools,{$ENDIF}
uIced.Imports, uIced.Types;
type
TIcedDecoder = class
private
fHandle : Pointer;
fBitness : TIcedBitness;
fData : PByte;
fSize : NativeUInt;
fIPosition: NativeUInt;
{$IFDEF DECODER_LOCAL_POSITION}
fPosition : NativeUInt;
{$ENDIF DECODER_LOCAL_POSITION}
function GetHandle : Pointer;
function GetBitness : TIcedBitness;
procedure SetBitness( Value : TIcedBitness );
function GetIP : UInt64;
procedure SetIP( Value : UInt64 );
function GetPosition : NativeUInt;
procedure SetPosition( Value : NativeUInt );
function GetMaxPosition : NativeUInt;
function GetInstructionFirstByte : PByte;
function GetCurrentByte : PByte;
function GetLastError : TDecoderError;
public
constructor Create( Bitness : TIcedBitness = bt64 ); reintroduce;
destructor Destroy; override;
procedure SetData( Data : PByte; Size : NativeUInt; IP : UInt64 = UInt64( 0 ); Options : Cardinal = doNONE ); {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Decode( var Instruction : TInstruction ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
{$IFDEF AssemblyTools}
procedure GenerateDetails( const Instruction : TInstruction; var Details : TIcedDetails ); {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Decode( var Instruction : TInstruction; var Details : TIcedDetails ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
{$ENDIF AssemblyTools}
procedure DecodeToEnd( Callback : TDecoderCallback; UserData : Pointer = nil ); {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure GetConstantOffsets( const Instruction : TInstruction; var ConstantOffsets : TConstantOffsets ); {$IF CompilerVersion >= 23}inline;{$IFEND}
property Handle : Pointer read GetHandle;
property Bitness : TIcedBitness read GetBitness write SetBitness;
function CanDecode : Boolean;
property IP : UInt64 read GetIP write SetIP;
property Position : NativeUInt read GetPosition write SetPosition;
property MaxPosition : NativeUInt read GetMaxPosition;
property InstructionFirstByte : PByte read GetInstructionFirstByte;
property CurrentByte : PByte read GetCurrentByte;
property LastError : TDecoderError read GetLastError;
end;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TIcedEncoder = class
private
fHandle : Pointer;
fBitness : TIcedBitness;
fCapacity : NativeUInt;
function GetHandle : Pointer;
function GetBitness : TIcedBitness;
procedure SetBitness( Value : TIcedBitness );
function GetCapacity : NativeUInt;
procedure SetCapacity( Value : NativeUInt );
function GetPreventVex2 : Boolean;
procedure SetPreventVex2( Value : Boolean );
function GetVexWig : Cardinal;
procedure SetVexWig( Value : Cardinal );
function GetVexLig : Cardinal;
procedure SetVexLig( Value : Cardinal );
function GetEvexWig : Cardinal;
procedure SetEvexWig( Value : Cardinal );
function GetEvexLig : Cardinal;
procedure SetEvexLig( Value : Cardinal );
function GetMvexWig : Cardinal;
procedure SetMvexWig( Value : Cardinal );
public
constructor Create( Bitness : TIcedBitness = bt64; Capacity : NativeUInt = 0 ); reintroduce;
destructor Destroy; override;
procedure Encode( var Instruction : TInstruction ); {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Write( Byte : Byte ); {$IF CompilerVersion >= 23}inline;{$IFEND}
function GetBuffer( Buffer : PByte; Size : NativeUInt ) : Boolean;
// function SetBuffer( Buffer : PByte; Size : NativeUInt ) : Boolean;
procedure GetConstantOffsets( var ConstantOffsets : TConstantOffsets ); {$IF CompilerVersion >= 23}inline;{$IFEND}
property Handle : Pointer read GetHandle;
property Bitness : TIcedBitness read GetBitness write SetBitness;
property Capacity : NativeUInt read GetCapacity write SetCapacity;
property PreventVex2 : Boolean read GetPreventVex2 write SetPreventVex2;
property VexWig : Cardinal read GetVexWig write SetVexWig;
property VexLig : Cardinal read GetVexLig write SetVexLig;
property EvexWig : Cardinal read GetEvexWig write SetEvexWig;
property EvexLig : Cardinal read GetEvexLig write SetEvexLig;
property MvexWig : Cardinal read GetMvexWig write SetMvexWig;
end;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TIcedBlockEncoder = class
private
fBitness : TIcedBitness;
fOptions : Cardinal;
fMemory : Pointer;
function GetBitness : TIcedBitness;
procedure SetBitness( Value : TIcedBitness );
function GetOptions : Cardinal;
procedure SetOptions( Value : Cardinal );
public
constructor Create( Bitness : TIcedBitness = bt64; Options : Cardinal = beoNONE ); reintroduce;
destructor Destroy; override;
property Bitness : TIcedBitness read GetBitness write SetBitness;
property Options : Cardinal read GetOptions write SetOptions;
procedure Encode( RIP : UInt64; var Instructions : TInstruction; Count : NativeUInt; var Results : TBlockEncoderResult ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Encode( RIP : UInt64; var Instructions : TInstructionArray; var Results : TBlockEncoderResult ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Encode( RIP : UInt64; var Instructions : TInstructionList; var Results : TBlockEncoderResult ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Clear; {$IF CompilerVersion >= 23}inline;{$IFEND}
end;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TInstructionInfoFactory = class
private
fHandle : Pointer;
fOptions : Cardinal;
function GetHandle : Pointer;
function GetOptions : Cardinal;
procedure SetOptions( Value : Cardinal );
public
constructor Create( Options : Cardinal = iioNone ); reintroduce;
destructor Destroy; override;
procedure Info( const Instruction: TInstruction; var InstructionInfo : TInstructionInfo ); {$IF CompilerVersion >= 23}inline;{$IFEND}
property Handle : Pointer read GetHandle;
property Options : Cardinal read GetOptions write SetOptions;
end;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TIcedFormatterSettings = record
// Options
// Masm
AddDsPrefix32 : Boolean;
SymbolDisplacementInBrackets : Boolean;
DisplacementInBrackets : Boolean;
// Nasm
ShowSignExtendedImmediateSize : Boolean;
// Gas
NakedRegisters : Boolean;
ShowMnemonicSizeSuffix : Boolean;
SpaceAfterMemoryOperandComma : Boolean;
// Specialized
UseHexPrefix : Boolean;
AlwaysShowMemorySize : Boolean;
// Common
SpaceAfterOperandSeparator : Boolean;
AlwaysShowSegmentRegister : Boolean;
UsePseudoOps : Boolean;
RipRelativeAddresses : Boolean;
ShowSymbolAddress : Boolean;
UpperCaseHex : Boolean;
// Common (All but Fast/Specialized)
UpperCasePrefixes : Boolean;
UpperCaseMnemonics : Boolean;
UpperCaseRegisters : Boolean;
UpperCaseKeyWords : Boolean;
UpperCaseDecorators : Boolean;
UpperCaseEverything : Boolean;
FirstOperandCharIndex : Cardinal;
TabSize : Cardinal;
SpaceAfterMemoryBracket : Boolean;
SpaceBetweenMemoryAddOperators : Boolean;
SpaceBetweenMemoryMulOperators : Boolean;
ScaleBeforeIndex : Boolean;
AlwaysShowScale : Boolean;
ShowZeroDisplacements : Boolean;
HexPrefix : AnsiString;
HexSuffix : AnsiString;
HexDigitGroupSize : Cardinal;
DecimalPrefix : AnsiString;
DecimalSuffix : AnsiString;
DecimalDigitGroupSize : Cardinal;
OctalPrefix : AnsiString;
OctalSuffix : AnsiString;
OctalDigitGroupSize : Cardinal;
BinaryPrefix : AnsiString;
BinarySuffix : AnsiString;
BinaryDigitGroupSize : Cardinal;
DigitSeparator : AnsiString;
LeadingZeros : Boolean;
SmallHexNumbersInDecimal : Boolean;
AddLeadingZeroToHexNumbers : Boolean;
NumberBase : TNumberBase;
BranchLeadingZeros : Boolean;
SignedImmediateOperands : Boolean;
SignedMemoryDisplacements : Boolean;
DisplacementLeadingZeros : Boolean;
MemorySizeOptions : TMemorySizeOptions;
ShowBranchSize : Boolean;
PreferST0 : Boolean;
ShowUselessPrefixes : Boolean;
CC_b : TCC_b;
CC_ae : TCC_ae;
CC_e : TCC_e;
CC_ne : TCC_ne;
CC_be : TCC_be;
CC_a : TCC_a;
CC_p : TCC_p;
CC_np : TCC_np;
CC_l : TCC_l;
CC_ge : TCC_ge;
CC_le : TCC_le;
CC_g : TCC_g;
end;
PIcedFormatterSettings = ^TIcedFormatterSettings;
TIcedFormatter = class
private
fType : TIcedFormatterType;
fHandle : Pointer;
fOptions : TIcedFormatterSettings;
fSpecialized : TIcedSpecializedFormatterOptions;
fSymbolResolver : TSymbolResolverCallback;
fOptionsProvider : TFormatterOptionsProviderCallback;
fFormatterOutput : TFormatterOutputCallback;
fOutputHandle : Pointer;
fShowSymbols : Boolean;
{$IFDEF AssemblyTools}
fSymbolHandler : TSymbolHandler;
{$ENDIF AssemblyTools}
procedure CreateHandle( KeepConfiguration : Boolean = False );
function GetHandle : Pointer;
procedure SetType( Value : TIcedFormatterType );
function GetSymbolResolver : TSymbolResolverCallback;
procedure SetSymbolResolver( Value : TSymbolResolverCallback );
function GetOptionsProvider : TFormatterOptionsProviderCallback;
procedure SetOptionsProvider( Value : TFormatterOptionsProviderCallback );
function GetFormatterOutput : TFormatterOutputCallback;
procedure SetFormatterOutput( Value : TFormatterOutputCallback );
procedure SetShowSymbols( Value : Boolean );
{$IFDEF AssemblyTools}
procedure SetSymbolHandler( Value : TSymbolHandler );
{$ENDIF AssemblyTools}
// Options
function GetOptions : TIcedFormatterSettings;
procedure SetOptions( Value : TIcedFormatterSettings );
procedure SetCapstoneOptions;
// Masm
function GetAddDsPrefix32 : Boolean;
procedure SetAddDsPrefix32( Value : Boolean );
function GetSymbolDisplacementInBrackets : Boolean;
procedure SetSymbolDisplacementInBrackets( Value : Boolean );
function GetDisplacementInBrackets : Boolean;
procedure SetDisplacementInBrackets( Value : Boolean );
// Nasm
function GetShowSignExtendedImmediateSize : Boolean;
procedure SetShowSignExtendedImmediateSize( Value : Boolean );
// Gas
function GetNakedRegisters : Boolean;
procedure SetNakedRegisters( Value : Boolean );
function GetShowMnemonicSizeSuffix : Boolean;
procedure SetShowMnemonicSizeSuffix( Value : Boolean );
function GetSpaceAfterMemoryOperandComma : Boolean;
procedure SetSpaceAfterMemoryOperandComma( Value : Boolean );
// Specialized
function GetUseHexPrefix : Boolean;
procedure SetUseHexPrefix( Value : Boolean );
function GetAlwaysShowMemorySize : Boolean;
procedure SetAlwaysShowMemorySize( Value : Boolean );
function GetEnable_DB_DW_DD_DQ : Boolean;
procedure SetEnable_DB_DW_DD_DQ( Value : Boolean );
function GetVerifyOutputHasEnoughBytesLeft : Boolean;
procedure SetVerifyOutputHasEnoughBytesLeft( Value : Boolean );
// Common
function GetSpaceAfterOperandSeparator : Boolean;
procedure SetSpaceAfterOperandSeparator( Value : Boolean );
function GetAlwaysShowSegmentRegister : Boolean;
procedure SetAlwaysShowSegmentRegister( Value : Boolean );
function GetUsePseudoOps : Boolean;
procedure SetUsePseudoOps( Value : Boolean );
function GetRipRelativeAddresses : Boolean;
procedure SetRipRelativeAddresses( Value : Boolean );
function GetShowSymbolAddress : Boolean;
procedure SetShowSymbolAddress( Value : Boolean );
function GetUpperCaseHex : Boolean;
procedure SetUpperCaseHex( Value : Boolean );
// Common (All but Fast/Specialized)
function GetUpperCasePrefixes : Boolean;
procedure SetUpperCasePrefixes( Value : Boolean );
function GetUpperCaseMnemonics : Boolean;
procedure SetUpperCaseMnemonics( Value : Boolean );
function GetUpperCaseRegisters : Boolean;
procedure SetUpperCaseRegisters( Value : Boolean );
function GetUpperCaseKeyWords : Boolean;
procedure SetUpperCaseKeyWords( Value : Boolean );
function GetUpperCaseDecorators : Boolean;
procedure SetUpperCaseDecorators( Value : Boolean );
function GetUpperCaseEverything : Boolean;
procedure SetUpperCaseEverything( Value : Boolean );
function GetFirstOperandCharIndex : Cardinal;
procedure SetFirstOperandCharIndex( Value : Cardinal );
function GetTabSize : Cardinal;
procedure SetTabSize( Value : Cardinal );
function GetSpaceAfterMemoryBracket : Boolean;
procedure SetSpaceAfterMemoryBracket( Value : Boolean );
function GetSpaceBetweenMemoryAddOperators : Boolean;
procedure SetSpaceBetweenMemoryAddOperators( Value : Boolean );
function GetSpaceBetweenMemoryMulOperators : Boolean;
procedure SetSpaceBetweenMemoryMulOperators( Value : Boolean );
function GetScaleBeforeIndex : Boolean;
procedure SetScaleBeforeIndex( Value : Boolean );
function GetAlwaysShowScale : Boolean;
procedure SetAlwaysShowScale( Value : Boolean );
function GetShowZeroDisplacements : Boolean;
procedure SetShowZeroDisplacements( Value : Boolean );
function GetHexPrefix : AnsiString;
procedure SetHexPrefix( Value : AnsiString );
function GetHexSuffix : AnsiString;
procedure SetHexSuffix( Value : AnsiString );
function GetHexDigitGroupSize : Cardinal;
procedure SetHexDigitGroupSize( Value : Cardinal );
function GetDecimalPrefix : AnsiString;
procedure SetDecimalPrefix( Value : AnsiString );
function GetDecimalSuffix : AnsiString;
procedure SetDecimalSuffix( Value : AnsiString );
function GetDecimalDigitGroupSize : Cardinal;
procedure SetDecimalDigitGroupSize( Value : Cardinal );
function GetOctalPrefix : AnsiString;
procedure SetOctalPrefix( Value : AnsiString );
function GetOctalSuffix : AnsiString;
procedure SetOctalSuffix( Value : AnsiString );
function GetOctalDigitGroupSize : Cardinal;
procedure SetOctalDigitGroupSize( Value : Cardinal );
function GetBinaryPrefix : AnsiString;
procedure SetBinaryPrefix( Value : AnsiString );
function GetBinarySuffix : AnsiString;
procedure SetBinarySuffix( Value : AnsiString );
function GetBinaryDigitGroupSize : Cardinal;
procedure SetBinaryDigitGroupSize( Value : Cardinal );
function GetDigitSeparator : AnsiString;
procedure SetDigitSeparator( Value : AnsiString );
function GetLeadingZeros : Boolean;
procedure SetLeadingZeros( Value : Boolean );
function GetSmallHexNumbersInDecimal : Boolean;
procedure SetSmallHexNumbersInDecimal( Value : Boolean );
function GetAddLeadingZeroToHexNumbers : Boolean;
procedure SetAddLeadingZeroToHexNumbers( Value : Boolean );
function GetNumberBase : TNumberBase;
procedure SetNumberBase( Value : TNumberBase );
function GetBranchLeadingZeros : Boolean;
procedure SetBranchLeadingZeros( Value : Boolean );
function GetSignedImmediateOperands : Boolean;
procedure SetSignedImmediateOperands( Value : Boolean );
function GetSignedMemoryDisplacements : Boolean;
procedure SetSignedMemoryDisplacements( Value : Boolean );
function GetDisplacementLeadingZeros : Boolean;
procedure SetDisplacementLeadingZeros( Value : Boolean );
function GetMemorySizeOptions : TMemorySizeOptions;
procedure SetMemorySizeOptions( Value : TMemorySizeOptions );
function GetShowBranchSize : Boolean;
procedure SetShowBranchSize( Value : Boolean );
function GetPreferST0 : Boolean;
procedure SetPreferST0( Value : Boolean );
function GetShowUselessPrefixes : Boolean;
procedure SetShowUselessPrefixes( Value : Boolean );
function GetCC_b : TCC_b;
procedure SetCC_b( Value : TCC_b );
function GetCC_ae : TCC_ae;
procedure SetCC_ae( Value : TCC_ae );
function GetCC_e : TCC_e;
procedure SetCC_e( Value : TCC_e );
function GetCC_ne : TCC_ne;
procedure SetCC_ne( Value : TCC_ne );
function GetCC_be : TCC_be;
procedure SetCC_be( Value : TCC_be );
function GetCC_a : TCC_a;
procedure SetCC_a( Value : TCC_a );
function GetCC_p : TCC_p;
procedure SetCC_p( Value : TCC_p );
function GetCC_np : TCC_np;
procedure SetCC_np( Value : TCC_np );
function GetCC_l : TCC_l;
procedure SetCC_l( Value : TCC_l );
function GetCC_ge : TCC_ge;
procedure SetCC_ge( Value : TCC_ge );
function GetCC_le : TCC_le;
procedure SetCC_le( Value : TCC_le );
function GetCC_g : TCC_g;
procedure SetCC_g( Value : TCC_g );
public
constructor Create( FormatterType : TIcedFormatterType = DEFAULT_FORMATTER; SymbolResolver : TSymbolResolverCallback = nil; OptionsProvider : TFormatterOptionsProviderCallback = nil ); reintroduce;
destructor Destroy; override;
procedure DefaultSettings;
function FormatToString( const Instruction: TInstruction ) : PAnsiChar; {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Format( const Instruction: TInstruction; var AOutput: PAnsiChar; Size : PNativeUInt = nil ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
procedure Format( const Instruction: TInstruction ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND} // TFormatterOutputCallback
property Handle : Pointer read GetHandle;
property FormatterType : TIcedFormatterType read fType write SetType;
property SymbolResolver : TSymbolResolverCallback read GetSymbolResolver write SetSymbolResolver;
property OptionsProvider : TFormatterOptionsProviderCallback read GetOptionsProvider write SetOptionsProvider;
property Callback : TFormatterOutputCallback read GetFormatterOutput write SetFormatterOutput;
property ShowSymbols : Boolean read fShowSymbols write SetShowSymbols;
{$IFDEF AssemblyTools}
property SymbolHandler : TSymbolHandler read fSymbolHandler write SetSymbolHandler;
{$ENDIF AssemblyTools}
// Options
property Options : TIcedFormatterSettings read GetOptions write SetOptions;
procedure SettingsToStringList( Settings : TIcedFormatterSettings; var StrL : TStringList );
function StringListToSettings( StrL : TStringList; Settings : PIcedFormatterSettings = nil ) : boolean;
// Masm
property AddDsPrefix32 : Boolean read GetAddDsPrefix32 write SetAddDsPrefix32;
property SymbolDisplacementInBrackets : Boolean read GetSymbolDisplacementInBrackets write SetSymbolDisplacementInBrackets;
property DisplacementInBrackets : Boolean read GetDisplacementInBrackets write SetDisplacementInBrackets;
// Nasm
property ShowSignExtendedImmediateSize : Boolean read GetShowSignExtendedImmediateSize write SetShowSignExtendedImmediateSize;
// Gas
property NakedRegisters : Boolean read GetNakedRegisters write SetNakedRegisters;
property ShowMnemonicSizeSuffix : Boolean read GetShowMnemonicSizeSuffix write SetShowMnemonicSizeSuffix;
property SpaceAfterMemoryOperandComma : Boolean read GetSpaceAfterMemoryOperandComma write SetSpaceAfterMemoryOperandComma;
// Specialized
property UseHexPrefix : Boolean read GetUseHexPrefix write SetUseHexPrefix;
property AlwaysShowMemorySize : Boolean read GetAlwaysShowMemorySize write SetAlwaysShowMemorySize;
property Enable_DB_DW_DD_DQ : Boolean read GetEnable_DB_DW_DD_DQ write SetEnable_DB_DW_DD_DQ;
property VerifyOutputHasEnoughBytesLeft : Boolean read GetVerifyOutputHasEnoughBytesLeft write SetVerifyOutputHasEnoughBytesLeft;
// Common
property SpaceAfterOperandSeparator : Boolean read GetSpaceAfterOperandSeparator write SetSpaceAfterOperandSeparator;
property AlwaysShowSegmentRegister : Boolean read GetAlwaysShowSegmentRegister write SetAlwaysShowSegmentRegister;
property UsePseudoOps : Boolean read GetUsePseudoOps write SetUsePseudoOps;
property RipRelativeAddresses : Boolean read GetRipRelativeAddresses write SetRipRelativeAddresses;
property ShowSymbolAddress : Boolean read GetShowSymbolAddress write SetShowSymbolAddress;
property UpperCaseHex : Boolean read GetUpperCaseHex write SetUpperCaseHex;
// Common (All but Fast/Specialized)
property UpperCasePrefixes : Boolean read GetUpperCasePrefixes write SetUpperCasePrefixes;
property UpperCaseMnemonics : Boolean read GetUpperCaseMnemonics write SetUpperCaseMnemonics;
property UpperCaseRegisters : Boolean read GetUpperCaseRegisters write SetUpperCaseRegisters;
property UpperCaseKeyWords : Boolean read GetUpperCaseKeyWords write SetUpperCaseKeyWords;
property UpperCaseDecorators : Boolean read GetUpperCaseDecorators write SetUpperCaseDecorators;
property UpperCaseEverything : Boolean read GetUpperCaseEverything write SetUpperCaseEverything;
property FirstOperandCharIndex : Cardinal read GetFirstOperandCharIndex write SetFirstOperandCharIndex;
property TabSize : Cardinal read GetTabSize write SetTabSize;
property SpaceAfterMemoryBracket : Boolean read GetSpaceAfterMemoryBracket write SetSpaceAfterMemoryBracket;
property SpaceBetweenMemoryAddOperators : Boolean read GetSpaceBetweenMemoryAddOperators write SetSpaceBetweenMemoryAddOperators;
property SpaceBetweenMemoryMulOperators : Boolean read GetSpaceBetweenMemoryMulOperators write SetSpaceBetweenMemoryMulOperators;
property ScaleBeforeIndex : Boolean read GetScaleBeforeIndex write SetScaleBeforeIndex;
property AlwaysShowScale : Boolean read GetAlwaysShowScale write SetAlwaysShowScale;
property ShowZeroDisplacements : Boolean read GetShowZeroDisplacements write SetShowZeroDisplacements;
property HexPrefix : AnsiString read GetHexPrefix write SetHexPrefix;
property HexSuffix : AnsiString read GetHexSuffix write SetHexSuffix;
property HexDigitGroupSize : Cardinal read GetHexDigitGroupSize write SetHexDigitGroupSize;
property DecimalPrefix : AnsiString read GetDecimalPrefix write SetDecimalPrefix;
property DecimalSuffix : AnsiString read GetDecimalSuffix write SetDecimalSuffix;
property DecimalDigitGroupSize : Cardinal read GetDecimalDigitGroupSize write SetDecimalDigitGroupSize;
property OctalPrefix : AnsiString read GetOctalPrefix write SetOctalPrefix;
property OctalSuffix : AnsiString read GetOctalSuffix write SetOctalSuffix;
property OctalDigitGroupSize : Cardinal read GetOctalDigitGroupSize write SetOctalDigitGroupSize;
property BinaryPrefix : AnsiString read GetBinaryPrefix write SetBinaryPrefix;
property BinarySuffix : AnsiString read GetBinarySuffix write SetBinarySuffix;
property BinaryDigitGroupSize : Cardinal read GetBinaryDigitGroupSize write SetBinaryDigitGroupSize;
property DigitSeparator : AnsiString read GetDigitSeparator write SetDigitSeparator;
property LeadingZeros : Boolean read GetLeadingZeros write SetLeadingZeros;
property SmallHexNumbersInDecimal : Boolean read GetSmallHexNumbersInDecimal write SetSmallHexNumbersInDecimal;
property AddLeadingZeroToHexNumbers : Boolean read GetAddLeadingZeroToHexNumbers write SetAddLeadingZeroToHexNumbers;
property NumberBase : TNumberBase read GetNumberBase write SetNumberBase;
property BranchLeadingZeros : Boolean read GetBranchLeadingZeros write SetBranchLeadingZeros;
property SignedImmediateOperands : Boolean read GetSignedImmediateOperands write SetSignedImmediateOperands;
property SignedMemoryDisplacements : Boolean read GetSignedMemoryDisplacements write SetSignedMemoryDisplacements;
property DisplacementLeadingZeros : Boolean read GetDisplacementLeadingZeros write SetDisplacementLeadingZeros;
property MemorySizeOptions : TMemorySizeOptions read GetMemorySizeOptions write SetMemorySizeOptions;
property ShowBranchSize : Boolean read GetShowBranchSize write SetShowBranchSize;
property PreferST0 : Boolean read GetPreferST0 write SetPreferST0;
property ShowUselessPrefixes : Boolean read GetShowUselessPrefixes write SetShowUselessPrefixes;
property CC_b : TCC_b read GetCC_b write SetCC_b;
property CC_ae : TCC_ae read GetCC_ae write SetCC_ae;
property CC_e : TCC_e read GetCC_e write SetCC_e;
property CC_ne : TCC_ne read GetCC_ne write SetCC_ne;
property CC_be : TCC_be read GetCC_be write SetCC_be;
property CC_a : TCC_a read GetCC_a write SetCC_a;
property CC_p : TCC_p read GetCC_p write SetCC_p;
property CC_np : TCC_np read GetCC_np write SetCC_np;
property CC_l : TCC_l read GetCC_l write SetCC_l;
property CC_ge : TCC_ge read GetCC_ge write SetCC_ge;
property CC_le : TCC_le read GetCC_le write SetCC_le;
property CC_g : TCC_g read GetCC_g write SetCC_g;
end;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tIcedPointerScanResult = record
Origin : UInt64;
Instruction : string;
vPointer : UInt64;
Count : Word;
end;
tIcedPointerScanResults = Array of tIcedPointerScanResult;
PIcedPointerScanResults = ^tIcedPointerScanResults;
tIcedReferenceScanResult = record
Origin : UInt64;
Instruction : string;
end;
tIcedReferenceScanResults = Array of tIcedReferenceScanResult;
PIcedReferenceScanResults = ^tIcedReferenceScanResults;
tIcedAssemblyScanMode = (
asmEqual, asmSimiliar, asmWildcard
{$IF CompilerVersion >= 22}, asmRegExp{$IFEND} // XE
);
tIcedPointerScanProcessEvent = procedure( Current, Total : Cardinal; var Cancel : Boolean ) of object;
tIcedAssemblyCompareMode = ( acmCenter, acmForward, acmBackward );
{$IF NOT Declared( TByteInstruction )} // IFNDEF AssemblyTools
const
MAX_INSTRUCTION_LEN_ = 15;
MASKING_OFFSET = $10000;
type
TByteInstruction = Array [ 0..MAX_INSTRUCTION_LEN_-1 ] of Byte;
pByteInstruction = ^TByteInstruction;
{$IFEND}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TIced = class
private
fDecoder : TIcedDecoder;
fEncoder : TIcedEncoder;
fBlockEncoder : TIcedBlockEncoder;
fInfoFactory : TInstructionInfoFactory;
fFormatter : TIcedFormatter;
public
constructor Create( Bitness : TIcedBitness = bt64 ); reintroduce;
destructor Destroy; override;
property Decoder : TIcedDecoder read fDecoder;
property Encoder : TIcedEncoder read fEncoder;
property BlockEncoder : TIcedBlockEncoder read fBlockEncoder;
property InfoFactory : TInstructionInfoFactory read fInfoFactory;
property Formatter : TIcedFormatter read fFormatter;
procedure DecodeFormat( var Instruction: TInstruction; var AOutput: PAnsiChar; Size : PNativeUInt = nil ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
{$IFDEF AssemblyTools}
procedure DecodeFormat( var Instruction: TInstruction; var AOutput: PAnsiChar; Size : PNativeUInt; var Details : TIcedDetails ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
{$ENDIF AssemblyTools}
procedure DecodeFormat( var Instruction: TInstruction ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND} // TFormatterOutputCallback
{$IFDEF AssemblyTools}
procedure DecodeFormat( var Instruction: TInstruction; var Details : TIcedDetails ); overload; {$IF CompilerVersion >= 23}inline;{$IFEND}
{$ENDIF AssemblyTools}
procedure DecodeFormatToEnd( Callback : TDecoderFormatCallback; UserData : Pointer = nil ); {$IF CompilerVersion >= 23}inline;{$IFEND}
{$IF Declared( SynUnicode )}
function DecodeFromFile( FileName : String; Size : Cardinal; Offset : UInt64; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : Cardinal; overload;
function DecodeFromStream( Data : TMemoryStream; Size : Cardinal; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : Cardinal; overload;
function Decode( Data : PByte; Size : Cardinal; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
function Decode( Data : PByte; Size : Cardinal; Count : Cardinal; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
function Decode( Data : string; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : boolean; overload;
function Decode( Data : TStrings; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : boolean; overload;
function DecodeCombineNOP( Data : PByte; Size : Cardinal; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
function DecodeCombineNOP( Data : PByte; Size : Cardinal; Count : Cardinal; StrL_Assembly : TUnicodeStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TUnicodeStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
{$IFEND UNICODE}
function DecodeFromFile( FileName : String; Size : Cardinal; Offset : UInt64; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : Cardinal; overload;
function DecodeFromStream( Data : TMemoryStream; Size : Cardinal; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : Cardinal; overload;
function Decode( Data : PByte; Size : Cardinal; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
function Decode( Data : PByte; Size : Cardinal; Count : Cardinal; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
function Decode( Data : PByte; Size : Cardinal; var Instruction: String; CodeOffset : UInt64 = UInt64( 0 ); InstructionBytes : pString = nil; Offset : PUInt64 = nil; {$IFDEF AssemblyTools}Detail : pIcedDetail = nil;{$ENDIF AssemblyTools} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : Cardinal; overload;
function Decode( Data : string; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : boolean; overload;
function Decode( Data : TStrings; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE; {$IFDEF AssemblyTools}CalcJumpLines : Boolean = True;{$ENDIF} CombineNOP : boolean = False ) : boolean; overload;
function DecodeCombineNOP( Data : PByte; Size : Cardinal; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
function DecodeCombineNOP( Data : PByte; Size : Cardinal; Count : Cardinal; StrL_Assembly : TStrings; CodeOffset : UInt64 = UInt64( 0 ); StrL_Hex : TStrings = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE{$IFDEF AssemblyTools}; CalcJumpLines : Boolean = True{$ENDIF} ) : Cardinal; overload;
{$IFDEF AssemblyTools}
function DecodeAddress( var Data : TByteInstruction; Address : UInt64; Instruction : PString = nil; {$IFDEF AssemblyTools}Detail : pIcedDetail = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
function DecodeAddress( Data : PByte; Size : Cardinal; Address : UInt64; InstructionBytes : pByteInstruction = nil; Instruction : PString = nil; {$IFDEF AssemblyTools}Detail : pIcedDetail = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
{$ENDIF AssemblyTools}
function Compare( Data : PByte; Size : Cardinal; Offset : UInt64; DataCompare : PByte; SizeCompare : Cardinal; OffsetCompare : UInt64; Count : Word; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyCompareMode = acmCenter ) : Cardinal; overload;
function Compare( Data : TMemoryStream; Offset : UInt64; DataCompare : TMemoryStream; OffsetCompare : UInt64; Count : Word; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyCompareMode = acmCenter ) : Cardinal; overload;
function PointerScan( Data : PByte; Size : Cardinal; var results : tIcedPointerScanResults; CodeOffset : UInt64 = UInt64( 0 ); ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function PointerScan( Data : TMemoryStream; var results : tIcedPointerScanResults; CodeOffset : UInt64 = UInt64( 0 ); ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function ReferenceScan( Data : PByte; Size : Cardinal; Reference : UInt64; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function ReferenceScan( Data : TMemoryStream; Reference : UInt64; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function AssemblyScan( Data : PByte; Size : Cardinal; Assembly : String; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyScanMode = asmEqual; ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function AssemblyScan( Data : TMemoryStream; Assembly : String; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyScanMode = asmEqual; ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function AssemblyScan( Data : PByte; Size : Cardinal; Assembly : PInstruction; AssemblyCount : Cardinal; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyScanMode = asmEqual; ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function AssemblyScan( Data : TMemoryStream; Assembly : PInstruction; AssemblyCount : Cardinal; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyScanMode = asmEqual; ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function AssemblyScan( Data : PByte; Size : Cardinal; var Assembly : TInstructionArray; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyScanMode = asmEqual; ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function AssemblyScan( Data : TMemoryStream; var Assembly : TInstructionArray; var results : tIcedReferenceScanResults; CodeOffset : UInt64 = UInt64( 0 ); Mode : tIcedAssemblyScanMode = asmEqual; ProcessEvent : tIcedPointerScanProcessEvent = nil ) : Cardinal; overload;
function FindInstruction( Data : PByte; Size : Cardinal; Offset : UInt64; CodeOffset : UInt64 = UInt64( 0 ); {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
function FindInstruction( Data : TMemoryStream; Offset : UInt64; CodeOffset : UInt64 = UInt64( 0 ); {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
function FindNextInstruction( Data : TMemoryStream; CodeOffset : UInt64; Address : UInt64; var Offset : UInt64; InstructionCount : Word = 1; InstructionBytes : pByteInstruction = nil; Instruction : PString = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
function FindNextInstruction( Data : PByte; Size : Cardinal; CodeOffset : UInt64; Address : UInt64; var Offset : UInt64; InstructionCount : Word = 1; InstructionBytes : pByteInstruction = nil; Instruction : PString = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
// MS
// function FindPreviousInstructionCount( Data : TMemoryStream; CodeOffset : UInt64; Address : UInt64; var Offset : UInt64; InstructionCount : Word = 1; InstructionBytes : pByteInstruction = nil; Instruction : PString = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
// function FindPreviousInstructionCount( Data : PByte; Size : Cardinal; CodeOffset : UInt64; Address : UInt64; var Offset : UInt64; InstructionCount : Word = 1; InstructionBytes : pByteInstruction = nil; Instruction : PString = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
// function FindPreviousInstruction( Data : TMemoryStream; CodeOffset : UInt64; Address : UInt64; var Offset : UInt64; DesiredInstructionOffset : UInt64; InstructionBytes : pByteInstruction = nil; Instruction : PString = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
// function FindPreviousInstruction( Data : PByte; Size : Cardinal; CodeOffset : UInt64; Address : UInt64; var Offset : UInt64; DesiredInstructionOffset : UInt64; InstructionBytes : pByteInstruction = nil; Instruction : PString = nil; {$IFDEF AssemblyTools}Details : pIcedDetails = nil;{$ENDIF} DecoderSettings : Cardinal = doNONE ) : Cardinal; overload;
end;
var
Iced : TIced;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$IFDEF TEST_FUNCTIONS}
const
EXAMPLE_RIP: UInt64 = UInt64( $7FFAC46ACDA4 );
EXAMPLE_CODE: Array [ 0..61 ] of Byte = (
$48, $89, $5C, $24, $10, $48, $89, $74, $24, $18, $55, $57, $41, $56, $48, $8D,
$AC, $24, $00, $FF, $FF, $FF, $48, $81, $EC, $00, $02, $00, $00, $48, $8B, $05,
$18, $57, $0A, $00, $48, $33, $C4, $48, $89, $85, $F0, $00, $00, $00, $4C, $8B,
$05, $2F, $24, $0A, $00, $48, $8D, $05, $78, $7C, $04, $00, $33, $FF
);
function Test( StrL : TStringList ) : Boolean;
function Test_Decode( Data : PByte; Size : Cardinal; ARIP : UInt64; AOutput : TStringList; FormatterType : TIcedFormatterType = ftMasm;
SymbolResolver : TSymbolResolverCallback = nil;
FormatterOptionsProviderCallback : TFormatterOptionsProviderCallback = nil;
FormatterOutputCallback : TFormatterOutputCallback = nil;
Assembly : Boolean = True; DecodeInfos : Boolean = True; Infos : Boolean = True ) : Boolean;
function Test_ReEncode( Data : PByte; Size : Cardinal; ARIP : UInt64; {LocalBuffer : Boolean = False;} BlockEncode : Boolean = False; AOutput : TStringList = nil ) : Boolean;
procedure Test_Assemble( AOutput : TStringList; ARIP : UInt64 = UInt64( $00001248FC840000 ) );
function Benchmark( Data : PByte; Size : Cardinal; Bitness : TIcedBitness; AFormat : Boolean = False ) : Double; overload;
function Benchmark( FileName : String; Bitness : TIcedBitness; AFormat : Boolean = False ) : Double; overload;
{$ENDIF TEST_FUNCTIONS}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
implementation
{$IF CompilerVersion >= 22} // XE
uses
RegularExpressions;
{$IFEND}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
constructor TIcedDecoder.Create( Bitness : TIcedBitness = bt64 );
begin
inherited Create;
fHandle := nil;
fBitness := Bitness;
fData := nil;
fSize := 0;
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
fIPosition:= 0;
{$IFDEF DECODER_LOCAL_POSITION}
fPosition := 0;
{$ENDIF DECODER_LOCAL_POSITION}
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
end;
destructor TIcedDecoder.Destroy;
begin
if ( fHandle <> nil ) then
IcedFreeMemory( fHandle );
inherited;
end;
function TIcedDecoder.GetHandle : Pointer;
begin
result := nil;
if ( self = nil ) then
Exit;
result := fHandle;
end;
function TIcedDecoder.GetBitness : TIcedBitness;
begin
result := bt16;
if ( self = nil ) then
Exit;
result := fBitness;
// result := TIcedBitness( Decoder_GetBitness( fHandle ) );
end;
procedure TIcedDecoder.SetBitness( Value : TIcedBitness );
begin
if ( self = nil ) then
Exit;
fBitness := Value;
if ( fHandle <> nil ) then
begin
IcedFreeMemory( fHandle );
fHandle := nil;
end;
end;
procedure TIcedDecoder.SetData( Data : PByte; Size : NativeUInt; IP : UInt64 = UInt64( 0 ); Options : Cardinal = doNONE );
begin
if ( self = nil ) then
Exit;
if NOT uIced.Imports.IsInitDLL then
Exit;
if ( fHandle <> nil ) then
IcedFreeMemory( fHandle );
if ( fData = Data ) AND ( fSize = 0 ) then
begin
Position := 0;
Exit;
end;
fData := Data;
fSize := Size;
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
fIPosition:= 0;
{$IFDEF DECODER_LOCAL_POSITION}
fPosition := 0;
{$ENDIF DECODER_LOCAL_POSITION}
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
fHandle := Decoder_Create( Cardinal( fBitness ), Data, Size, IP, Options );
end;
function TIcedDecoder.CanDecode : Boolean;
begin
result := False;
if ( self = nil ) then
Exit;
result := Decoder_CanDecode( fHandle );
end;
function TIcedDecoder.GetIP : UInt64;
begin
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
result := 0;
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
if ( self = nil ) then
Exit;
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
result := Decoder_GetIP( fHandle );
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
end;
procedure TIcedDecoder.SetIP( Value : UInt64 );
begin
if ( self = nil ) then
Exit;
Decoder_SetIP( fHandle, Value );
end;
function TIcedDecoder.GetPosition : NativeUInt;
begin
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
result := 0;
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
if ( self = nil ) then
Exit;
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
{$IFDEF DECODER_LOCAL_POSITION}
result := fPosition;
{$ELSE}
result := Decoder_GetPosition( fHandle );
{$ENDIF DECODER_LOCAL_POSITION}
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
end;
procedure TIcedDecoder.SetPosition( Value : NativeUInt );
begin
if ( self = nil ) then
Exit;
{$IFDEF DECODER_LOCAL_POSITION}
if ( fPosition = Value ) then
Exit;
if ( Value > fSize ) then
Exit;
fPosition := Value;
{$ELSE}
if ( Value > fSize ) then
Exit;
{$ENDIF DECODER_LOCAL_POSITION}
Decoder_SetPosition( fHandle, Value );
end;
function TIcedDecoder.GetMaxPosition : NativeUInt;
begin
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
result := 0;
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
if ( self = nil ) then
Exit;
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
result := fSize;
// result := Decoder_GetMaxPosition( fHandle );
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
end;
function TIcedDecoder.GetInstructionFirstByte : PByte;
begin
result := nil;
if ( self = nil ) then
Exit;
if ( fData = nil ) OR ( fSize = 0 ) then
Exit;
{$IFDEF DECODER_LOCAL_POSITION}
if ( fPosition = 0 ) then
Exit;
{$ELSE}
if ( Position = 0 ) then
Exit;
{$ENDIF DECODER_LOCAL_POSITION}
result := PByte( PAnsiChar( fData ) + fIPosition );
end;
function TIcedDecoder.GetCurrentByte : PByte;
begin
result := nil;
if ( self = nil ) then
Exit;
if ( fData = nil ) OR ( fSize = 0 ) then
Exit;
{$IFDEF DECODER_LOCAL_POSITION}
result := PByte( PAnsiChar( fData ) + fPosition );
{$ELSE}
result := PByte( PAnsiChar( fData ) + Position );
{$ENDIF DECODER_LOCAL_POSITION}
end;
function TIcedDecoder.GetLastError : TDecoderError;
begin
result.DecoderError := deNone;
if ( self = nil ) then
Exit;
result := Decoder_GetLastError( fHandle );
end;
procedure TIcedDecoder.Decode( var Instruction : TInstruction );
begin
if ( self = nil ) then
begin
FillChar( Instruction, SizeOf( Instruction ), 0 );
Exit;
end;
if ( fHandle = nil ) then
begin
FillChar( Instruction, SizeOf( Instruction ), 0 );
Exit;
end;
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
{$IFDEF DECODER_LOCAL_POSITION}
fIPosition := fPosition;
{$ELSE}
fIPosition := Position;
{$ENDIF DECODER_LOCAL_POSITION}
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
Decoder_Decode( fHandle, Instruction );
{$IFDEF DECODER_LOCAL_POSITION}
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
// fPosition := Position;
Inc( fPosition, Instruction.len );
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
{$ENDIF DECODER_LOCAL_POSITION}
end;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$IFDEF AssemblyTools}
procedure TIcedDecoder.GenerateDetails( const Instruction : TInstruction; var Details : TIcedDetails );
const
JUMPTARGET_BLOCKSIZE = 1000;
var
Offsets : TConstantOffsets;
begin
if ( self = nil ) then
begin
// FillChar( Detail, SizeOf( Detail ), 0 );
Exit;
end;
if ( fHandle = nil ) then
begin
// FillChar( Detail, SizeOf( Detail ), 0 );
Exit;
end;
// Details
Details.Items[ Details.Count ].Code := Instruction.code;
Move( Instruction.Regs[ 0 ], Details.Items[ Details.Count ].Registers[ 0 ], Length( Details.Items[ Details.Count ].Registers )*SizeOf( Details.Items[ Details.Count ].Registers[ 0 ] ) );
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
Details.Items[ Details.Count ].Offset := Instruction.RIP;
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
Details.Items[ Details.Count ].Size := Instruction.len;
FillChar( Details.Items[ Details.Count ].Registers_, SizeOf( Details.Items[ Details.Count ].Registers_ ), 0 ); // MS ?
// Details.Items[ Details.Count ].Registers_.Target := Instruction. // MS working for all but lea
// Details.Items[ Details.Count ].Registers_.Source := Instruction.
// Details.Items[ Details.Count ].Registers_.Parameter := Instruction.
{
LEA
Multiplier is always Byte 4 before displacement
If Length = 3 or Displacement starts at 3 its not used
488D84 1A 00010000 = 1A = 1
488D84 5A 00010000 = 1A = 2
488D84 9A 00010000 = 1A = 4
488D84 DA 00010000 = 1A = 8
488D841A00010000 lea rax, [rdx+rbx+0x100]
488D845A00010000 lea rax, [rdx+rbx*2+0x100]
488D849A00010000 lea rax, [rdx+rbx*4+0x100]
488D84DA00010000 lea rax, [rdx+rbx*8+0x100]
488D041A lea rax, [rdx+rbx*1]
488D045A lea rax, [rdx+rbx*2]
488D049A lea rax, [rdx+rbx*4]
488D04DA lea rax, [rdx+rbx*8]
678D041A lea eax, [edx+ebx*1]
678D045A lea eax, [edx+ebx*2]
678D049A lea eax, [edx+ebx*4]
678D04DA lea eax, [edx+ebx*8]
}
Decoder_GetConstantOffsets( fHandle, Instruction, Offsets );
{if ForceMask AND ( Offsets.displacement_offset <> 0 ) then
Details.Items[ Details.Count ].Mask := True
else} if ( Offsets.displacement_offset <> 0 ) AND ( Offsets.displacement_size = 4 ) then
Details.Items[ Details.Count ].Mask := ( ABS( Integer( Instruction.mem_displ-Instruction.Rip-Instruction.Len ) ) >= MASKING_OFFSET ); // value is bigger than 65535 (positive and negative)
if Details.Items[ Details.Count ].IsJump AND ( ( Instruction.op_kinds[ 0 ] <> okRegister_ ) OR ( Instruction.mem_displ <> 0 ) ) then
begin
Details.Items[ Details.Count ].JumpTargetID := Details.TargetCount;
Inc( Details.TargetCount );
if ( Details.TargetCount >= Length( Details.JumpTargets ) ) then
SetLength( Details.JumpTargets, Details.TargetCount+JUMPTARGET_BLOCKSIZE );
FillChar( Details.JumpTargets[ Details.Items[ Details.Count ].JumpTargetID ], SizeOf( Details.JumpTargets[ Details.Items[ Details.Count ].JumpTargetID ] ), 0 );
// Details.JumpTargets[ Details.Items[ Details.Count ].JumpTargetID ].IsRegJump := Instruction.IsRegJump;
{$IF CompilerVersion < 23}{$RANGECHECKS OFF}{$IFEND} // RangeCheck might cause Internal-Error C1118
Details.JumpTargets[ Details.Items[ Details.Count ].JumpTargetID ].Target := Instruction.mem_displ;
{$IF CompilerVersion < 23}{$RANGECHECKS ON}{$IFEND} // RangeCheck might cause Internal-Error C1118
end
else
Details.Items[ Details.Count ].JumpTargetID := 0;
end;
procedure TIcedDecoder.Decode( var Instruction : TInstruction; var Details : TIcedDetails );
begin
if ( self = nil ) then
begin
FillChar( Instruction, SizeOf( Instruction ), 0 );
// FillChar( Detail, SizeOf( Detail ), 0 );
Exit;