-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCU_Out.pas
1514 lines (1351 loc) · 38.4 KB
/
DCU_Out.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 DCU_Out;
interface
(*
The output module of the DCU32INT utility by Alexei Hmelnov.
(Pay attention on the SoftNL technique for pretty-printing.)
----------------------------------------------------------------------------
E-Mail: [email protected]
http://hmelnov.icc.ru/DCU/
----------------------------------------------------------------------------
See the file "readme.txt" for more details.
------------------------------------------------------------------------
IMPORTANT NOTE:
This software is provided 'as-is', without any expressed or implied warranty.
In no event will the author be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented, you must not
claim that you wrote the original software.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*)
uses
{$IFDEF UNICODE}AnsiStrings, {$ENDIF}SysUtils, FixUp;
type
TIncPtr = PAnsiChar;
TDasmMode = (dasmSeq, dasmCtlFlow, dasmDataFlow);
TOutFmt = (ofmtText, ofmtHTM);
const
DefaultExt: array [TOutFmt] of String = ('.int', '.htm');
{ Options: }
var
InterfaceOnly: boolean = false;
ShowImpNames: boolean = true;
ShowTypeTbl: boolean = false;
ShowAddrTbl: boolean = false;
ShowDataBlock: boolean = false;
ShowFixupTbl: boolean = false;
ShowLocVarTbl: boolean = false;
ShowFileOffsets: boolean = false;
ShowAuxValues: boolean = false;
ResolveMethods: boolean = true;
ResolveConsts: boolean = true;
FixCommentChars: boolean = true;
ShowDotTypes: boolean = false;
ShowSelf: boolean = false;
ShowVMT: boolean = false;
ShowHeuristicRefs: boolean = true;
ShowImpNamesUnits: boolean = false;
DasmMode: TDasmMode = dasmSeq;
OutFmt: TOutFmt = ofmtText;
var
GenVarCAsVars: boolean = false;
var
NoNamePrefix: AnsiString = '_N%_';
DotNamePrefix: AnsiString = '_D%_';
procedure SetShowAll;
procedure PutCh(ch: Char);
procedure PutS(const S: AnsiString);
procedure PutSFmt(const Fmt: AnsiString; const Args: array of const);
function ShiftNLOfs(d: Integer): Integer { Old NLOfs };
procedure NL;
procedure NLAux;
procedure SoftNL;
procedure PutSpace;
procedure SetShowAuxValues(V: boolean);
procedure OpenAux;
procedure CloseAux;
function HideAux: Integer; // Aux0
procedure RestoreAux(Aux0: Integer);
procedure RemOpen0;
procedure RemOpen;
procedure RemClose0;
procedure RemClose;
procedure AuxRemOpen;
procedure AuxRemClose;
procedure PutSFmtRem(const Fmt: AnsiString; const Args: array of const);
procedure PutSFmtRemAux(const Fmt: AnsiString; const Args: array of const);
procedure PutKW(const S: AnsiString);
procedure PutKWSp(const S: AnsiString);
procedure PutStrConst(const S: AnsiString);
procedure PutStrConstQ(const S: AnsiString);
procedure PutAddrDefStr(const S: AnsiString; hDef: Integer);
procedure PutMemRefStr(const S: AnsiString; Ofs: Integer);
procedure PutHexOffset(Ofs: LongInt);
procedure PutInt(i: LongInt);
procedure PutHex(i: LongInt);
procedure MarkDefStart(hDef: Integer);
procedure MarkMemOfs(Ofs: Integer);
function CharDumpStr(var V; N: Integer): ShortString;
function DumpStr(var V; N: Integer): AnsiString;
function IntLStr(DP: Pointer; Sz: Cardinal; Neg: boolean): AnsiString;
function CharStr(ch: AnsiChar): AnsiString;
function WCharStr(WCh: WideChar): AnsiString;
function BoolStr(DP: Pointer; DS: Cardinal): AnsiString;
function StrConstStr(CP: PAnsiChar; L: Integer): AnsiString;
function ShowStrConst(DP: Pointer; DS: Cardinal): Integer { Size used };
function ShowUnicodeStrConst(DP: Pointer; DS: Cardinal): Integer { Size used }; // Ver >=verD12
function ShowUnicodeResStrConst(DP: Pointer; DS: Cardinal): Integer { Size used }; // Ver >=verD12
function TryShowPCharConst(DP: PAnsiChar; DS: Cardinal): Integer { Size used };
function FixFloatToStr(const E: Extended): AnsiString;
const
cSoftNL = #0;
// cSepCh=#1;
MaxOutWidth: Cardinal = 75;
MaxNLOfs: Cardinal = 31 { Should be < Ord(' ') };
type
TStrInfoRec = packed record
Ofs: Word;
Inf: SmallInt;
DP: Pointer;
end;
type
TBaseWriter = class
protected
FAuxLevel: Integer;
FRemLevel: Integer;
FOutLineNum: Integer;
FNLOfs: Cardinal;
BufNLOfs: Cardinal;
BufLen: Cardinal;
WasSoftNL: boolean;
StrInfoCnt: Integer;
StrInfoOfsLast: Integer;
Buf: array [0 .. $800 - 1] of AnsiChar;
StrInfoTbl: array [0 .. $7F] of TStrInfoRec;
procedure PutStrInfo(Info: Integer; Data: Pointer);
function GetSoftNLOfs(var ResNLOfs: Cardinal): Integer;
procedure FlushBufRange(Start, W: Integer);
procedure FlushStrInfo(Info: Integer; Data: Pointer);
procedure FillNL(ANLOfs: Cardinal);
procedure FlushBufPart(W, ANLOfs: Integer);
function FlushSoftNL(W: Cardinal): boolean;
procedure BufChars(CP: PAnsiChar; Len: Integer);
protected
FInfo: Integer;
FData: Pointer;
FStarted: boolean;
procedure WriteEnd; virtual;
procedure WriteCP(CP: PAnsiChar; Len: Integer); virtual; abstract;
procedure NL; virtual; abstract;
function OpenStrInfo(Info: Integer; Data: Pointer): boolean; virtual;
procedure CloseStrInfo; virtual;
procedure MarkDefStart(hDef: Integer); virtual;
procedure MarkMemOfs(Ofs: Integer); virtual;
procedure Flush; virtual;
public
constructor Create;
destructor Destroy; override;
procedure WriteStart; virtual;
procedure Reset; virtual;
property OutLineNum: Integer read FOutLineNum;
property AuxLevel: Integer read FAuxLevel;
property RemLevel: Integer read FRemLevel;
property NLOfs: Cardinal read FNLOfs write FNLOfs;
end;
TTextFileWriter = class(TBaseWriter)
protected
FRes: TextFile;
procedure WriteCP(CP: PAnsiChar; Len: Integer); override;
procedure NL; override;
procedure Flush; override;
public
constructor Create(const FNRes: String);
destructor Destroy; override;
end;
THTMWriter = class(TTextFileWriter)
public
procedure WriteStart; override;
protected
FWasStr: boolean;
procedure WriteEnd; override;
procedure WriteCP(CP: PAnsiChar; Len: Integer); override;
procedure NL; override;
function OpenStrInfo(Info: Integer; Data: Pointer): boolean; override;
procedure CloseStrInfo; override;
procedure MarkDefStart(hDef: Integer); override;
procedure MarkMemOfs(Ofs: Integer); override;
end;
TStringWriter = class(TBaseWriter)
protected
FPrev: TBaseWriter;
FBuf: AnsiString;
FPos: Integer;
procedure WriteCP(CP: PAnsiChar; Len: Integer); override;
procedure NL; override;
public
function GetResult: AnsiString;
procedure Reset; override;
end;
var
Writer: TBaseWriter = Nil;
function InitOut(const FNRes: String): TBaseWriter;
// procedure DoneOut;
procedure FlushOut;
procedure ReportExc(const Msg: AnsiString);
function ReplaceWriter(W: TBaseWriter): TBaseWriter;
function SetStringWriter: TStringWriter;
procedure RestorePrevWriter;
const
siEnd = 0;
siRem = 1;
siKeyWord = 2;
siStrConst = 3;
siAddrDef = 4;
siMemRef = 5;
siMaxRange = 5;
siDefStart = 6;
siMemOfs = 7;
siMax = 8;
procedure ShowDump(DP, DPFile0: TIncPtr; FileSize, SizeDispl, Size: Cardinal;
Ofs0Displ, Ofs0, WMin: Cardinal; FixCnt: Integer; FixTbl: PFixupTbl;
FixUpNames, ShowFileOfs: boolean);
implementation
uses
DCU32 {CurUnit} , DCU_In;
procedure SetShowAll;
begin
ShowImpNames := true;
ShowTypeTbl := true;
ShowAddrTbl := true;
ShowDataBlock := true;
ShowFixupTbl := true;
ShowLocVarTbl := true;
ShowFileOffsets := true;
ShowAuxValues := true;
ResolveMethods := true;
ResolveConsts := true;
FixCommentChars := false;
ShowDotTypes := true;
ShowSelf := true;
ShowVMT := true;
ShowImpNamesUnits := true;
end;
{ TBaseWriter. }
constructor TBaseWriter.Create;
begin
inherited Create;
Reset;
end;
destructor TBaseWriter.Destroy;
begin
if FStarted then
WriteEnd;
inherited Destroy;
end;
procedure TBaseWriter.Reset;
// Restore the initial state of the writer (as if it was just created)
begin
FNLOfs := 0;
BufLen := 0;
BufNLOfs := FNLOfs;
FOutLineNum := 0;
WasSoftNL := false;
FAuxLevel := 0;
FRemLevel := 0;
StrInfoCnt := 0;
StrInfoOfsLast := 0;
FInfo := 0;
FData := Nil;
FStarted := false;
end;
procedure TBaseWriter.WriteStart;
begin
FStarted := true;
end;
procedure TBaseWriter.WriteEnd;
begin
FStarted := false;
end;
function TBaseWriter.OpenStrInfo(Info: Integer; Data: Pointer): boolean;
begin
if Info <= siMaxRange then begin
FInfo := Info;
FData := Data;
Result := true;
Exit;
end;
Result := false;
case Info of
siDefStart: MarkDefStart(Integer(Data));
siMemOfs: MarkMemOfs(Integer(Data));
end;
end;
procedure TBaseWriter.CloseStrInfo;
begin
FInfo := 0;
FData := Nil;
end;
procedure TBaseWriter.MarkDefStart(hDef: Integer);
begin
end;
procedure TBaseWriter.MarkMemOfs(Ofs: Integer);
begin
end;
procedure TBaseWriter.Flush;
begin
end;
{ ----- }
procedure TBaseWriter.PutStrInfo(Info: Integer; Data: Pointer);
begin
if (RemLevel > 0) and (Info <= siMaxRange { Embedded ranges are not allowed } ) then
Exit;
if StrInfoCnt > High(StrInfoTbl) then
Exit;
with StrInfoTbl[StrInfoCnt] do begin
Ofs := BufLen - StrInfoOfsLast;
Inf := Info;
DP := Data;
end;
StrInfoOfsLast := BufLen;
Inc(StrInfoCnt);
end;
function TBaseWriter.GetSoftNLOfs(var ResNLOfs: Cardinal): Integer;
var
i: Integer;
MinOfs, Ofs: Integer;
begin
MinOfs := Ord(' ');
Result := BufLen;
for i := BufLen - 1 downto 0 do begin
Ofs := Ord(Buf[i]);
if Ofs < MinOfs then begin
MinOfs := Ofs;
Result := i;
end;
end;
if MinOfs < Ord(' ') then
ResNLOfs := MinOfs
else
ResNLOfs := FNLOfs;
end;
procedure TBaseWriter.FlushBufRange(Start, W: Integer);
var
DP: PAnsiChar;
SaveCh: AnsiChar;
begin
if W <= 0 then
Exit;
DP := Buf + Start;
SaveCh := DP[W];
DP[W] := #0;
WriteCP(DP, W); // Write(FRes,DP);
DP[W] := SaveCh;
end;
procedure TBaseWriter.FlushStrInfo(Info: Integer; Data: Pointer);
begin
if Info = 0 then
CloseStrInfo
else
OpenStrInfo(Info, Data);
end;
procedure TBaseWriter.FillNL(ANLOfs: Cardinal);
var
S: array [Byte] of AnsiChar { ShortString };
W: Integer;
begin
W := ANLOfs;
if W < 0 then
W := 0
else if W > MaxNLOfs then
W := MaxNLOfs;
{ S[0] := Chr(W);
FillChar(S[1],W,' '); }
FillChar(S[0], W, ' ');
S[W] := #0;
WriteCP(S, W);
end;
procedure TBaseWriter.FlushBufPart(W, ANLOfs: Integer);
var
i, hSI: Integer;
// S: String;
SIOfs { ,SIW } : Integer;
procedure FlushSI(Skip: boolean);
var
SIW: Integer;
begin
while hSI < StrInfoCnt do begin
SIW := StrInfoTbl[hSI].Ofs;
if SIOfs + SIW > W then
break;
if not Skip then
FlushBufRange(SIOfs, SIW);
Inc(SIOfs, SIW);
with StrInfoTbl[hSI] do
FlushStrInfo(Inf, DP);
Inc(hSI);
end;
end;
begin
SIOfs := 0;
hSI := 0;
if W > 0 then begin
for i := 0 to W - 1 do
if Buf[i] < ' ' then
Buf[i] := ' ';
FillNL(BufNLOfs);
// SetString(S,Buf,W);
// Write(FRes,S);
FlushSI(false { Skip } );
if SIOfs < W then
FlushBufRange(SIOfs, W - SIOfs);
end;
NL; // Writeln(FRes);
Inc(FOutLineNum);
while (W < BufLen) and (Buf[W] <= ' ') do
Inc(W);
FlushSI(true { Skip } );
if W < BufLen then
move(Buf[W], Buf, BufLen - W);
BufLen := BufLen - W;
BufNLOfs := ANLOfs;
if hSI >= StrInfoCnt then
StrInfoCnt := 0
else begin
if hSI > 0 then begin
Dec(StrInfoCnt, hSI);
move(StrInfoTbl[hSI], StrInfoTbl[0], StrInfoCnt * SizeOf(TStrInfoRec));
end;
if StrInfoCnt > 0 then
Dec(StrInfoTbl[0].Ofs, W - SIOfs);
end;
if StrInfoCnt > 0 then
Dec(StrInfoOfsLast, W)
else
StrInfoOfsLast := 0;
end;
function TBaseWriter.FlushSoftNL(W: Cardinal): boolean;
var
Split: Integer;
ResNLOfs: Cardinal;
begin
while ((BufNLOfs + BufLen + W) > MaxOutWidth) and (BufLen > 0) do begin
Split := GetSoftNLOfs(ResNLOfs);
{ Break only at the soft NL splits: }
if Split >= BufLen then
break;
FlushBufPart(Split, ResNLOfs);
WasSoftNL := true;
end;
Result := (BufNLOfs + BufLen + W) <= MaxOutWidth;
end;
procedure TBaseWriter.BufChars(CP: PAnsiChar; Len: Integer);
var
ch: AnsiChar;
begin
// FlushSoftNL(Len);
While Len > 0 do begin
if BufLen >= High(Buf) then
Exit { Just in case };
ch := CP^;
Inc(CP);
Dec(Len);
if ch < ' ' then begin
if FNLOfs > MaxNLOfs then
ch := AnsiChar(MaxNLOfs)
else
ch := AnsiChar(FNLOfs);
end
else if (RemLevel > 0) and FixCommentChars then begin
if ch = '{' then
ch := '('
else if ch = '}' then
ch := ')';
end;
Buf[BufLen] := ch;
Inc(BufLen);
if ch < ' ' then
FlushSoftNL(0);
end;
FlushSoftNL(0);
// move(S[1],Buf[BufLen],Length(S));
// Inc(BufLen,Length(S));
{ if FlushSoftNL(Length(S)) then begin
move(S[1],Buf[BufLen],Length(S));
Inc(BufLen,Length(S));
end
else begin
FillNL(BufNLOfs);
Write(FRes,S);
Writeln(FRes);
end ; }
end;
{ TTextFileWriter. }
constructor TTextFileWriter.Create(const FNRes: String);
begin
inherited Create;
AssignFile(FRes, FNRes);
TTextRec(FRes).Mode := fmClosed;
Rewrite(FRes); // Test whether the FNRes is a correct file name
end;
destructor TTextFileWriter.Destroy;
begin
if TTextRec(FRes).Mode <> fmClosed then begin
WriteEnd;
Close(FRes);
end;
inherited Destroy;
end;
procedure TTextFileWriter.WriteCP(CP: PAnsiChar; Len: Integer);
begin
Write(FRes, CP);
end;
procedure TTextFileWriter.NL;
begin
Writeln(FRes);
end;
procedure TTextFileWriter.Flush;
begin
System.Flush(FRes);
end;
{ THTMWriter. }
procedure THTMWriter.WriteStart;
begin
inherited WriteStart;
Writeln(FRes, '<HTML><HEAD><STYLE TYPE="text/css"> I {color: #008080} ' +
'EM {color: #008000} A:link, A:visited, A:active {text-decoration: none; color: #800000} ' +
'A:hover { text-decoration: underline; color: #C08000 }</STYLE></HEAD><BODY><PRE>');
end;
procedure THTMWriter.WriteEnd;
begin
Writeln(FRes, '</PRE></BODY></HTML>');
inherited WriteEnd;
end;
procedure THTMWriter.WriteCP(CP: PAnsiChar; Len: Integer);
const
sTags: array [1 .. siMaxRange] of String = ('<I>', '<B>', '<EM>', '', '');
var
S: String;
var
Buf: ShortString;
i, j: Integer;
ch: AnsiChar;
procedure FlushBuf;
begin
Buf[0] := AnsiChar(j);
Write(FRes, Buf);
j := 0;
end;
procedure PutS(C: PAnsiChar);
begin
while C^ <> #0 do begin
Inc(j);
Buf[j] := C^;
Inc(C);
end;
end;
begin
if not FWasStr and (CP^ <> #0) and (FInfo > 0) then begin
case FInfo of
siAddrDef: S := Format('<A HREF="#A%d">', [Integer(FData)]);
siMemRef: S := Format('<A HREF="#M%x">', [Integer(FData)]);
else
S := sTags[FInfo];
end;
Write(FRes, S);
FWasStr := true;
end;
j := 0;
for i := 0 to Len - 1 do begin
ch := CP[i];
case ch of
'<': PutS('<');
'>': PutS('>');
'&': PutS('&');
'"': PutS('"');
else
Inc(j);
Buf[j] := ch;
end;
if j > 240 then
FlushBuf;
end;
FlushBuf;
// inherited WriteCP(CP,Len);
end;
procedure THTMWriter.NL;
begin
Writeln(FRes);
end;
function THTMWriter.OpenStrInfo(Info: Integer; Data: Pointer): boolean;
begin
Result := inherited OpenStrInfo(Info, Data);
if Result then
FWasStr := false;
end;
procedure THTMWriter.CloseStrInfo;
const
sTags: array [1 .. siMaxRange] of String = ('</I>', '</B>', '</EM>', '</A>', '</A>');
begin
if FWasStr and (FInfo > 0) then
Write(FRes, sTags[FInfo]);
inherited CloseStrInfo;
FWasStr := false;
end;
procedure THTMWriter.MarkDefStart(hDef: Integer);
begin
Write(FRes, Format('<A NAME=A%d>', [hDef]));
end;
procedure THTMWriter.MarkMemOfs(Ofs: Integer);
begin
Write(FRes, Format('<A NAME=M%x>', [Ofs]));
end;
{ TStringWriter. }
procedure TStringWriter.Reset;
begin
inherited Reset;
FPos := 0;
end;
procedure TStringWriter.WriteCP(CP: PAnsiChar; Len: Integer);
var
P: Integer;
begin
if Len <= 0 then
Exit;
P := FPos + Len;
if P > Length(FBuf) then begin
P := P * 2;
if P < 256 then
P := 256;
SetLength(FBuf, P);
end;
move(CP^, FBuf[FPos + 1], Len * SizeOf(AnsiChar));
Inc(FPos, Len);
end;
procedure TStringWriter.NL;
begin
WriteCP(' ', 1); { replace by space }
end;
function TStringWriter.GetResult: AnsiString;
begin
FlushOut; // !!!
if FPos <= 0 then
Result := ''
else
SetString(Result, PAnsiChar(FBuf), FPos - 1 { Remove last NL } );
end;
procedure PutStrInfoEnd;
begin
Writer.PutStrInfo(0, Nil);
end;
procedure PutCh(ch: Char);
begin
if Writer.AuxLevel > 0 then
Exit;
Writer.BufChars(@ch, 1);
end;
procedure PutS(const S: AnsiString);
begin
if Writer.AuxLevel > 0 then
Exit;
if S = '' then
Exit;
Writer.BufChars(PAnsiChar(S), Length(S));
end;
procedure PutSFmt(const Fmt: AnsiString; const Args: array of const);
begin
if Writer.AuxLevel > 0 then
Exit;
PutS({$IFDEF UNICODE}AnsiStrings.{$ENDIF}Format(Fmt, Args));
end;
procedure FlushOut;
begin
Writer.FlushBufPart(Writer.BufLen, Writer.NLOfs);
end;
procedure ReportExc(const Msg: AnsiString);
begin
if Writer = Nil then
Exit;
Writer.NL;
Writer.WriteCP(PAnsiChar(Msg), Length(Msg));
Writer.NL;
Writer.Flush;
end;
function ReplaceWriter(W: TBaseWriter): TBaseWriter;
begin
Result := Writer;
Writer := W;
end;
var
StringWriterList: TStringWriter = Nil;
function SetStringWriter: TStringWriter;
begin
if StringWriterList = Nil then
Result := TStringWriter.Create
else begin
Result := StringWriterList;
StringWriterList := TStringWriter(StringWriterList.FPrev);
Result.Reset;
end;
Result.FPrev := ReplaceWriter(Result);
end;
procedure RestorePrevWriter;
var
SW: TStringWriter;
begin
if (Writer = Nil) or not(Writer is TStringWriter) then
raise Exception.Create('Error restoring previous writer');
SW := TStringWriter(ReplaceWriter(TStringWriter(Writer).FPrev));
SW.FPrev := StringWriterList;
StringWriterList := SW;
end;
procedure FreeStringWriterList;
var
SW: TStringWriter;
begin
while StringWriterList <> Nil do begin
SW := StringWriterList;
StringWriterList := TStringWriter(StringWriterList.FPrev);
SW.Free;
end;
end;
function ShiftNLOfs(d: Integer): Integer { Old NLOfs };
begin
Result := Writer.FNLOfs;
Writer.FNLOfs := Result + d;
end;
procedure NL;
begin
if Writer.AuxLevel > 0 then
Exit;
if not Writer.WasSoftNL or (Writer.BufLen > 0) then
FlushOut
else
Writer.BufNLOfs := Writer.NLOfs;
Writer.WasSoftNL := false;
end;
procedure NLAux;
begin
Inc(Writer.FAuxLevel);
NL;
Dec(Writer.FAuxLevel);
end;
procedure SoftNL;
begin
PutCh(cSoftNL);
end;
function InitOut(const FNRes: String): TBaseWriter;
begin
if Writer = Nil then begin
case OutFmt of
ofmtHTM: Writer := THTMWriter.Create(FNRes);
else
Writer := TTextFileWriter.Create(FNRes);
end;
Writer.WriteStart;
end;
Result := Writer;
end;
{
procedure DoneOut;
begin
if Writer<>Nil then begin
Writer.WriteEnd;
Writer.Free;
Writer := Nil;
end ;
end ;
}
function CharDumpStr(var V; N: Integer): ShortString;
var
C: array [1 .. 255] of AnsiChar absolute V;
i: Integer;
begin
if N > 255 then
N := 255;
CharDumpStr[0] := AnsiChar(N);
for i := 1 to N do
if C[i] < ' ' then
CharDumpStr[i] := '.'
else
CharDumpStr[i] := C[i];
end;
function CharNStr(ch: AnsiChar; N: Integer): ShortString;
begin
SetLength(Result, N);
FillChar(Result[1], N, ch);
end;
type
TByteChars = packed record
Ch0, Ch1: AnsiChar end;
const
Digit: array [0 .. 15] of AnsiChar = '0123456789ABCDEF';
function ByteChars(B: Byte): Word;
var
ch: TByteChars;
begin
ch.Ch0 := Digit[B shr 4];
ch.Ch1 := Digit[B and $F];
ByteChars := Word(ch);
end;
function DumpStr(var V; N: Integer): AnsiString;
var
i: Integer;
BP: ^Byte;
P: Pointer;
begin
if N <= 0 then begin
Result := '';
Exit;
end;
SetLength(Result, N * 3 - 1);
P := @Result[1];
BP := @V;
for i := 1 to N do begin
Word(P^) := ByteChars(BP^);
Inc(TIncPtr(P), 2);
AnsiChar(P^) := ' ';
Inc(TIncPtr(P));
Inc(TIncPtr(BP));
end;
Dec(TIncPtr(P));
AnsiChar(P^) := #0;
end;
const
OfsFmtS = '%0.0x: %2:s';
FileOfsFmtS = '%0.0x_%0.0x: %s';
function GetNumHexDigits(Sz: Cardinal): Cardinal;
begin
Result := 0;
while Sz > 0 do begin
Inc(Result);
Sz := Sz shr 4;
end;
end;
procedure SetHexFmtNumDigits(var FmtS: AnsiString; P: Integer; Sz: Cardinal);
var
N: Cardinal;
LCh: AnsiChar;
begin
N := GetNumHexDigits(Sz);
LCh := AnsiChar(Ord('0') + N);
FmtS[P] := LCh;
FmtS[P + 2] := LCh;
end;
procedure ShowDump(DP, { File 0 address, show file offsets if present }
DPFile0: TIncPtr; { Dump address }
FileSize, SizeDispl { used to calculate display offset digits } ,
Size { Dump size } : Cardinal;
Ofs0Displ { initial display offset } ,
Ofs0 { offset in DCU data block - for fixups } ,
WMin { Minimal dump width (in bytes) } : Cardinal;
FixCnt: Integer; FixTbl: PFixupTbl;
FixUpNames, ShowFileOfs: boolean);
var
LP: TIncPtr;
{ LS, } W: Cardinal;
FmtS, DS, FixS, FS, DumpFmt: AnsiString;
DSP, CP: PAnsiChar;
{ Sz, } LSz, dOfs: Cardinal;
// Ch: Char;
// IsBig: boolean;
FP: PFixupRec;
K: Byte;
// N: PName;
begin
if Integer(Size) <= 0 then begin
PutS('[]');
Exit;
end;
if not ShowFileOfs { DPFile0=Nil } then
FmtS := OfsFmtS
else begin
FmtS := FileOfsFmtS;
SetHexFmtNumDigits(FmtS, 8, FileSize);
end;
if SizeDispl = 0 then
SizeDispl := Size;
SetHexFmtNumDigits(FmtS, 2, Ofs0Displ + SizeDispl);
W := 16;
LP := DP;
// IsBig := Size>W;
if Size < W then begin
W := Size;
if W < WMin then
W := WMin;
end;
if WMin > 0 then
DumpFmt := '|%-' + IntToStr(3 * W - 1) + 's|'
else
DumpFmt := '|%s|';
FP := Pointer(FixTbl);
if FP = Nil then
FixCnt := 0 { Just in case };
repeat
LSz := W;
if LSz > Size then
LSz := Size;
MarkMemOfs(LP - DPFile0);