forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompile.pas
9167 lines (8479 loc) · 321 KB
/
Compile.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 Compile;
{
Inno Setup
Copyright (C) 1997-2016 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Compiler
}
{x$DEFINE STATICPREPROC}
{ For debugging purposes, remove the 'x' to have it link the ISPP code
into this program and not depend on ISPP.dll. Most useful when combined
with CompForm's STATICCOMPILER. Note: the ISPP source doesn't support
Delphi 3 which is normally used for the ANSI compiler, and the IS source
code doesn't support Delphi 7 which is normally used for ANSI ISPP. So
use Unicode. }
{$I VERSION.INC}
interface
uses
Windows, SysUtils, CompInt;
function ISCompileScript(const Params: TCompileScriptParamsEx;
const PropagateExceptions: Boolean): Integer;
function ISGetVersion: PCompilerVersionInfo;
type
EISCompileError = class(Exception);
implementation
uses
CompPreprocInt, Commctrl, {$IFDEF IS_DXE2}Vcl.Consts{$ELSE}Consts{$ENDIF}, Classes, IniFiles, TypInfo,
PathFunc, CmnFunc2, Struct, Int64Em, CompMsgs, SetupEnt,
FileClass, Compress, CompressZlib, bzlib, LZMA, ArcFour, SHA1,
MsgIDs, DebugStruct, VerInfo, ResUpdate, CompResUpdate,
{$IFDEF STATICPREPROC}
IsppPreprocess,
{$ENDIF}
ScriptCompiler, SimpleExpression, SetupTypes;
type
TParamInfo = record
Name: String;
Flags: set of (piRequired, piNoEmpty, piNoQuotes);
end;
TParamValue = record
Found: Boolean;
Data: String;
end;
TEnumIniSectionProc = procedure(const Line: PChar; const Ext, Ext2: Integer) of object;
TSetupSectionDirectives = (
ssAllowCancelDuringInstall,
ssAllowNetworkDrive,
ssAllowNoIcons,
ssAllowRootDirectory,
ssAllowUNCPath,
ssAlwaysRestart,
ssAlwaysShowComponentsList,
ssAlwaysShowDirOnReadyPage,
ssAlwaysShowGroupOnReadyPage,
ssAlwaysUsePersonalGroup,
ssAppCopyright,
ssAppendDefaultDirName,
ssAppendDefaultGroupName,
ssAppComments,
ssAppContact,
ssAppId,
ssAppModifyPath,
ssAppMutex,
ssAppName,
ssAppPublisher,
ssAppPublisherURL,
ssAppReadmeFile,
ssAppSupportPhone,
ssAppSupportURL,
ssAppUpdatesURL,
ssAppVerName,
ssAppVersion,
ssArchitecturesAllowed,
ssArchitecturesInstallIn64BitMode,
ssASLRCompatible,
ssBackColor,
ssBackColor2,
ssBackColorDirection,
ssBackSolid,
ssChangesAssociations,
ssChangesEnvironment,
ssCloseApplications,
ssCloseApplicationsFilter,
ssCompression,
ssCompressionThreads,
ssCreateAppDir,
ssCreateUninstallRegKey,
ssDefaultDialogFontName,
ssDefaultDirName,
ssDefaultGroupName,
ssDefaultUserInfoName,
ssDefaultUserInfoOrg,
ssDefaultUserInfoSerial,
ssDEPCompatible,
ssDirExistsWarning,
ssDisableDirPage,
ssDisableFinishedPage,
ssDisableProgramGroupPage,
ssDisableReadyMemo,
ssDisableReadyPage,
ssDisableStartupPrompt,
ssDisableWelcomePage,
ssDiskClusterSize,
ssDiskSliceSize,
ssDiskSpanning,
ssDontMergeDuplicateFiles,
ssEnableDirDoesntExistWarning,
ssEncryption,
ssExtraDiskSpaceRequired,
ssFlatComponentsList,
ssInfoAfterFile,
ssInfoBeforeFile,
ssInternalCompressLevel,
ssLanguageDetectionMethod,
ssLicenseFile,
ssLZMAAlgorithm,
ssLZMABlockSize,
ssLZMADictionarySize,
ssLZMAMatchFinder,
ssLZMANumBlockThreads,
ssLZMANumFastBytes,
ssLZMAUseSeparateProcess,
ssMergeDuplicateFiles,
ssMessagesFile,
ssMinVersion,
ssOnlyBelowVersion,
ssOutput,
ssOutputBaseFilename,
ssOutputDir,
ssOutputManifestFile,
ssPassword,
ssPrivilegesRequired,
ssReserveBytes,
ssRestartApplications,
ssRestartIfNeededByRun,
ssSetupIconFile,
ssSetupLogging,
ssSetupMutex,
ssShowComponentSizes,
ssShowLanguageDialog,
ssShowTasksTreeLines,
ssShowUndisplayableLanguages,
ssSignedUninstaller,
ssSignedUninstallerDir,
ssSignTool,
ssSignToolRetryCount,
ssSlicesPerDisk,
ssSolidCompression,
ssSourceDir,
ssTerminalServicesAware,
ssTimeStampRounding,
ssTimeStampsInUTC,
ssTouchDate,
ssTouchTime,
ssUpdateUninstallLogAppName,
ssUninstallable,
ssUninstallDisplayIcon,
ssUninstallDisplayName,
ssUninstallDisplaySize,
ssUninstallFilesDir,
ssUninstallIconFile,
ssUninstallLogMode,
ssUninstallRestartComputer,
ssUninstallStyle,
ssUsePreviousAppDir,
ssUsePreviousGroup,
ssUsePreviousLanguage,
ssUsePreviousSetupType,
ssUsePreviousTasks,
ssUsePreviousUserInfo,
ssUseSetupLdr,
ssUserInfoPage,
ssVersionInfoCompany,
ssVersionInfoCopyright,
ssVersionInfoDescription,
ssVersionInfoProductName,
ssVersionInfoProductVersion,
ssVersionInfoProductTextVersion,
ssVersionInfoTextVersion,
ssVersionInfoVersion,
ssWindowResizable,
ssWindowShowCaption,
ssWindowStartMaximized,
ssWindowVisible,
ssWizardImageAlphaFormat,
ssWizardImageBackColor,
ssWizardImageFile,
ssWizardImageStretch,
ssWizardSmallImageBackColor,
ssWizardSmallImageFile,
ssWizardStyle);
TLangOptionsSectionDirectives = (
lsCopyrightFontName,
lsCopyrightFontSize,
lsDialogFontName,
lsDialogFontSize,
lsDialogFontStandardHeight,
lsLanguageCodePage,
lsLanguageID,
lsLanguageName,
lsRightToLeft,
lsTitleFontName,
lsTitleFontSize,
lsWelcomeFontName,
lsWelcomeFontSize);
TAllowedConst = (acOldData, acBreak);
TAllowedConsts = set of TAllowedConst;
TLineInfo = class
public
FileName: String;
FileLineNumber: Integer;
end;
{$IFDEF UNICODE}
TPreLangData = class
public
Name: String;
LanguageCodePage: Integer;
end;
{$ENDIF}
TLangData = class
public
MessagesDefined: array[TSetupMessageID] of Boolean;
Messages: array[TSetupMessageID] of String;
end;
TSignTool = class
Name, Command: String;
end;
TNameAndAccessMask = record
Name: String;
Mask: DWORD;
end;
TLowFragList = class(TList)
protected
procedure Grow; override;
end;
TLowFragStringList = class
private
FInternalList: TLowFragList;
function Get(Index: Integer): String;
function GetCount: Integer;
procedure Put(Index: Integer; const Value: String);
public
constructor Create;
destructor Destroy; override;
function Add(const S: String): Integer;
procedure Clear;
property Count: Integer read GetCount;
property Strings[Index: Integer]: String read Get write Put; default;
end;
THashStringItem = record
Hash: Longint;
Str: String;
end;
PHashStringItemList = ^THashStringItemList;
THashStringItemList = array[0..MaxListSize-1] of THashStringItem;
THashStringList = class
private
FCapacity: Integer;
FCount: Integer;
FList: PHashStringItemList;
procedure Grow;
public
destructor Destroy; override;
function Add(const S: String): Integer;
function CaseInsensitiveIndexOf(const S: String): Integer;
procedure Clear;
function Get(Index: Integer): String;
property Count: Integer read FCount;
property Strings[Index: Integer]: String read Get; default;
end;
PScriptFileLine = ^TScriptFileLine;
TScriptFileLine = record
LineFilename: String;
LineNumber: Integer;
LineText: String;
end;
TScriptFileLines = class
private
FLines: TLowFragList;
function Get(Index: Integer): PScriptFileLine;
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
procedure Add(const LineFilename: String; const LineNumber: Integer;
const LineText: String);
property Count: Integer read GetCount;
property Lines[Index: Integer]: PScriptFileLine read Get; default;
end;
TCheckOrInstallKind = (cikCheck, cikDirectiveCheck, cikInstall);
TSetupCompiler = class
private
ScriptFiles: TStringList;
PreprocOptionsString: String;
PreprocCleanupProc: TPreprocCleanupProc;
PreprocCleanupProcData: Pointer;
LanguageEntries,
CustomMessageEntries,
PermissionEntries,
TypeEntries,
ComponentEntries,
TaskEntries,
DirEntries,
FileEntries,
FileLocationEntries,
IconEntries,
IniEntries,
RegistryEntries,
InstallDeleteEntries,
UninstallDeleteEntries,
RunEntries,
UninstallRunEntries: TList;
FileLocationEntryFilenames: THashStringList;
WarningsList: TLowFragStringList;
ExpectedCustomMessageNames: TStringList;
DefaultLangData: TLangData;
{$IFDEF UNICODE} PreLangDataList, {$ENDIF} LangDataList: TList;
SignToolList: TList;
SignTools, SignToolsParams: TStringList;
SignToolRetryCount: Integer;
OutputDir, OutputBaseFilename, OutputManifestFile, SignedUninstallerDir,
ExeFilename: String;
Output, FixedOutput, FixedOutputDir, FixedOutputBaseFilename: Boolean;
CompressMethod: TSetupCompressMethod;
InternalCompressLevel, CompressLevel: Integer;
InternalCompressProps, CompressProps: TLZMACompressorProps;
UseSolidCompression: Boolean;
DontMergeDuplicateFiles: Boolean;
CryptKey: String;
TimeStampsInUTC: Boolean;
TimeStampRounding: Integer;
TouchDateOption: (tdCurrent, tdNone, tdExplicit);
TouchDateYear, TouchDateMonth, TouchDateDay: Integer;
TouchTimeOption: (ttCurrent, ttNone, ttExplicit);
TouchTimeHour, TouchTimeMinute, TouchTimeSecond: Integer;
SetupHeader: TSetupHeader;
SetupDirectiveLines: array[TSetupSectionDirectives] of Integer;
UseSetupLdr, DiskSpanning, BackSolid, TerminalServicesAware, DEPCompatible, ASLRCompatible: Boolean;
DiskSliceSize, DiskClusterSize, SlicesPerDisk, ReserveBytes: Longint;
LicenseFile, InfoBeforeFile, InfoAfterFile, WizardImageFile: String;
WizardSmallImageFile: String;
DefaultDialogFontName: String;
VersionInfoVersion, VersionInfoProductVersion: TFileVersionNumbers;
VersionInfoVersionOriginalValue, VersionInfoCompany, VersionInfoCopyright,
VersionInfoDescription, VersionInfoTextVersion, VersionInfoProductName,
VersionInfoProductTextVersion, VersionInfoProductVersionOriginalValue: String;
SetupIconFilename: String;
CodeText: TStringList;
CodeCompiler: TScriptCompiler;
CompiledCodeText: AnsiString;
CompileWasAlreadyCalled: Boolean;
LineFilename: String;
LineNumber: Integer;
DebugInfo, CodeDebugInfo: TMemoryStream;
DebugEntryCount, VariableDebugEntryCount: Integer;
CompiledCodeTextLength, CompiledCodeDebugInfoLength: Integer;
TotalBytesToCompress, BytesCompressedSoFar: Integer64;
CompressionInProgress: Boolean;
CompressionStartTick: DWORD;
CachedUserDocsDir: String;
procedure AddStatus(const S: String);
procedure AddStatusFmt(const Msg: String; const Args: array of const);
procedure AbortCompile(const Msg: String);
procedure AbortCompileFmt(const Msg: String; const Args: array of const);
procedure AbortCompileOnLine(const Msg: String);
procedure AbortCompileOnLineFmt(const Msg: String;
const Args: array of const);
procedure AbortCompileParamError(const Msg, ParamName: String);
function PrependDirName(const Filename, Dir: String): String;
function PrependSourceDirName(const Filename: String): String;
procedure CallIdleProc;
procedure DoCallback(const Code: Integer; var Data: TCompilerCallbackData);
procedure EnumIniSection(const EnumProc: TEnumIniSectionProc;
const SectionName: String; const Ext, Ext2: Integer; const Verbose, SkipBlankLines: Boolean;
const Filename: String; const AnsiLanguageFile, Pre: Boolean);
function EvalCheckOrInstallIdentifier(Sender: TSimpleExpression; const Name: String;
const Parameters: array of const): Boolean;
procedure CheckCheckOrInstall(const ParamName, ParamData: String;
const Kind: TCheckOrInstallKind);
function CheckConst(const S: String; const MinVersion: TSetupVersionData;
const AllowedConsts: TAllowedConsts): Boolean;
procedure CheckCustomMessageDefinitions;
procedure CheckCustomMessageReferences;
procedure EnumTypes(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumComponents(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumTasks(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumDirs(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumIcons(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumINI(const Line: PChar; const Ext, Ext2: Integer);
{$IFDEF UNICODE}
procedure EnumLangOptionsPre(const Line: PChar; const Ext, Ext2: Integer);
{$ENDIF}
procedure EnumLangOptions(const Line: PChar; const Ext, Ext2: Integer);
{$IFDEF UNICODE}
procedure EnumLanguagesPre(const Line: PChar; const Ext, Ext2: Integer);
{$ENDIF}
procedure EnumLanguages(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumRegistry(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumDelete(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumFiles(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumRun(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumSetup(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumMessages(const Line: PChar; const Ext, Ext2: Integer);
procedure EnumCustomMessages(const Line: PChar; const Ext, Ext2: Integer);
procedure ExtractParameters(S: PChar; const ParamInfo: array of TParamInfo;
var ParamValues: array of TParamValue);
function FindLangEntryIndexByName(const AName: String; const Pre: Boolean): Integer;
function FindSignToolIndexByName(const AName: String): Integer;
function GetLZMAExeFilename(const Allow64Bit: Boolean): String;
procedure InitBzipDLL;
procedure InitCryptDLL;
{$IFDEF UNICODE}
procedure InitPreLangData(const APreLangData: TPreLangData);
{$ENDIF}
procedure InitLanguageEntry(var ALanguageEntry: TSetupLanguageEntry);
procedure InitLZMADLL;
procedure InitPreprocessor;
procedure InitZipDLL;
function ParseFilename: String;
procedure PopulateLanguageEntryData;
procedure ProcessMinVersionParameter(const ParamValue: TParamValue;
var AMinVersion: TSetupVersionData);
procedure ProcessOnlyBelowVersionParameter(const ParamValue: TParamValue;
var AOnlyBelowVersion: TSetupVersionData);
procedure ProcessPermissionsParameter(ParamData: String;
const AccessMasks: array of TNameAndAccessMask; var PermissionsEntry: Smallint);
function EvalComponentIdentifier(Sender: TSimpleExpression; const Name: String;
const Parameters: array of const): Boolean;
function EvalTaskIdentifier(Sender: TSimpleExpression; const Name: String;
const Parameters: array of const): Boolean;
function EvalLanguageIdentifier(Sender: TSimpleExpression; const Name: String;
const Parameters: array of const): Boolean;
procedure ProcessExpressionParameter(const ParamName,
ParamData: String; OnEvalIdentifier: TSimpleExpressionOnEvalIdentifier;
SlashConvert: Boolean; var ProcessedParamData: String);
procedure ProcessWildcardsParameter(const ParamData: String;
const AWildcards: TStringList; const TooLongMsg: String);
procedure ReadDefaultMessages;
{$IFDEF UNICODE}
procedure ReadMessagesFromFilesPre(const AFiles: String; const ALangIndex: Integer);
{$ENDIF}
procedure ReadMessagesFromFiles(const AFiles: String; const ALangIndex: Integer);
{$IFDEF UNICODE}
procedure ReadMessagesFromScriptPre;
{$ENDIF}
procedure ReadMessagesFromScript;
function ReadScriptFile(const Filename: String; const UseCache: Boolean;
const AnsiConvertCodePage: Cardinal): TScriptFileLines;
procedure EnumCode(const Line: PChar; const Ext, Ext2: Integer);
procedure ReadCode;
procedure CodeCompilerOnLineToLineInfo(const Line: LongInt; var Filename: String; var FileLine: LongInt);
procedure CodeCompilerOnUsedLine(const Filename: String; const Line, Position: LongInt);
procedure CodeCompilerOnUsedVariable(const Filename: String; const Line, Col, Param1, Param2, Param3: LongInt; const Param4: AnsiString);
procedure CodeCompilerOnError(const Msg: String; const ErrorFilename: String; const ErrorLine: LongInt);
procedure CodeCompilerOnWarning(const Msg: String);
procedure CompileCode;
procedure ReadTextFile(const Filename: String; const LangIndex: Integer; var Text: AnsiString);
procedure SeparateDirective(const Line: PChar; var Key, Value: String);
procedure ShiftDebugEntryIndexes(AKind: TDebugEntryKind);
procedure Sign(AExeFilename: String);
procedure SignCommand(const ACommand, AParams, AExeFilename: String; const RetryCount: Integer);
procedure WriteDebugEntry(Kind: TDebugEntryKind; Index: Integer);
procedure WriteCompiledCodeText(const CompiledCodeText: Ansistring);
procedure WriteCompiledCodeDebugInfo(const CompiledCodeDebugInfo: AnsiString);
public
AppData: Longint;
CallbackProc: TCompilerCallbackProc;
CompilerDir, SourceDir, OriginalSourceDir: String;
constructor Create(AOwner: TComponent);
destructor Destroy; override;
procedure AddSignTool(const Name, Command: String);
procedure Compile;
end;
var
{$IFNDEF UNICODE}
CompilerLeadBytes: TLeadByteSet;
{$ENDIF}
ZipInitialized, BzipInitialized, LZMAInitialized, CryptInitialized: Boolean;
PreprocessorInitialized: Boolean;
PreprocessScriptProc: TPreprocessScriptProc;
const
ParamCommonFlags = 'Flags';
ParamCommonComponents = 'Components';
ParamCommonTasks = 'Tasks';
ParamCommonLanguages = 'Languages';
ParamCommonCheck = 'Check';
ParamCommonBeforeInstall = 'BeforeInstall';
ParamCommonAfterInstall = 'AfterInstall';
ParamCommonMinVersion = 'MinVersion';
ParamCommonOnlyBelowVersion = 'OnlyBelowVersion';
DefaultTypeEntryNames: array[0..2] of PChar = ('full', 'compact', 'custom');
MaxDiskSliceSize = 2100000000;
type
TColor = $7FFFFFFF-1..$7FFFFFFF;
const
clScrollBar = TColor(COLOR_SCROLLBAR or $80000000);
clBackground = TColor(COLOR_BACKGROUND or $80000000);
clActiveCaption = TColor(COLOR_ACTIVECAPTION or $80000000);
clInactiveCaption = TColor(COLOR_INACTIVECAPTION or $80000000);
clMenu = TColor(COLOR_MENU or $80000000);
clWindow = TColor(COLOR_WINDOW or $80000000);
clWindowFrame = TColor(COLOR_WINDOWFRAME or $80000000);
clMenuText = TColor(COLOR_MENUTEXT or $80000000);
clWindowText = TColor(COLOR_WINDOWTEXT or $80000000);
clCaptionText = TColor(COLOR_CAPTIONTEXT or $80000000);
clActiveBorder = TColor(COLOR_ACTIVEBORDER or $80000000);
clInactiveBorder = TColor(COLOR_INACTIVEBORDER or $80000000);
clAppWorkSpace = TColor(COLOR_APPWORKSPACE or $80000000);
clHighlight = TColor(COLOR_HIGHLIGHT or $80000000);
clHighlightText = TColor(COLOR_HIGHLIGHTTEXT or $80000000);
clBtnFace = TColor(COLOR_BTNFACE or $80000000);
clBtnShadow = TColor(COLOR_BTNSHADOW or $80000000);
clGrayText = TColor(COLOR_GRAYTEXT or $80000000);
clBtnText = TColor(COLOR_BTNTEXT or $80000000);
clInactiveCaptionText = TColor(COLOR_INACTIVECAPTIONTEXT or $80000000);
clBtnHighlight = TColor(COLOR_BTNHIGHLIGHT or $80000000);
cl3DDkShadow = TColor(COLOR_3DDKSHADOW or $80000000);
cl3DLight = TColor(COLOR_3DLIGHT or $80000000);
clInfoText = TColor(COLOR_INFOTEXT or $80000000);
clInfoBk = TColor(COLOR_INFOBK or $80000000);
clBlack = TColor($000000);
clMaroon = TColor($000080);
clGreen = TColor($008000);
clOlive = TColor($008080);
clNavy = TColor($800000);
clPurple = TColor($800080);
clTeal = TColor($808000);
clGray = TColor($808080);
clSilver = TColor($C0C0C0);
clRed = TColor($0000FF);
clLime = TColor($00FF00);
clYellow = TColor($00FFFF);
clBlue = TColor($FF0000);
clFuchsia = TColor($FF00FF);
clAqua = TColor($FFFF00);
clLtGray = TColor($C0C0C0);
clDkGray = TColor($808080);
clWhite = TColor($FFFFFF);
clNone = TColor($1FFFFFFF);
clDefault = TColor($20000000);
type
TColorEntry = record
Value: TColor;
Name: string;
end;
const
Colors: array[0..41] of TColorEntry = (
(Value: clBlack; Name: 'clBlack'),
(Value: clMaroon; Name: 'clMaroon'),
(Value: clGreen; Name: 'clGreen'),
(Value: clOlive; Name: 'clOlive'),
(Value: clNavy; Name: 'clNavy'),
(Value: clPurple; Name: 'clPurple'),
(Value: clTeal; Name: 'clTeal'),
(Value: clGray; Name: 'clGray'),
(Value: clSilver; Name: 'clSilver'),
(Value: clRed; Name: 'clRed'),
(Value: clLime; Name: 'clLime'),
(Value: clYellow; Name: 'clYellow'),
(Value: clBlue; Name: 'clBlue'),
(Value: clFuchsia; Name: 'clFuchsia'),
(Value: clAqua; Name: 'clAqua'),
(Value: clWhite; Name: 'clWhite'),
(Value: clScrollBar; Name: 'clScrollBar'),
(Value: clBackground; Name: 'clBackground'),
(Value: clActiveCaption; Name: 'clActiveCaption'),
(Value: clInactiveCaption; Name: 'clInactiveCaption'),
(Value: clMenu; Name: 'clMenu'),
(Value: clWindow; Name: 'clWindow'),
(Value: clWindowFrame; Name: 'clWindowFrame'),
(Value: clMenuText; Name: 'clMenuText'),
(Value: clWindowText; Name: 'clWindowText'),
(Value: clCaptionText; Name: 'clCaptionText'),
(Value: clActiveBorder; Name: 'clActiveBorder'),
(Value: clInactiveBorder; Name: 'clInactiveBorder'),
(Value: clAppWorkSpace; Name: 'clAppWorkSpace'),
(Value: clHighlight; Name: 'clHighlight'),
(Value: clHighlightText; Name: 'clHighlightText'),
(Value: clBtnFace; Name: 'clBtnFace'),
(Value: clBtnShadow; Name: 'clBtnShadow'),
(Value: clGrayText; Name: 'clGrayText'),
(Value: clBtnText; Name: 'clBtnText'),
(Value: clInactiveCaptionText; Name: 'clInactiveCaptionText'),
(Value: clBtnHighlight; Name: 'clBtnHighlight'),
(Value: cl3DDkShadow; Name: 'cl3DDkShadow'),
(Value: cl3DLight; Name: 'cl3DLight'),
(Value: clInfoText; Name: 'clInfoText'),
(Value: clInfoBk; Name: 'clInfoBk'),
(Value: clNone; Name: 'clNone'));
function IdentToColor(const Ident: string; var Color: Longint): Boolean;
var
I: Integer;
begin
for I := Low(Colors) to High(Colors) do
if CompareText(Colors[I].Name, Ident) = 0 then
begin
Result := True;
Color := Longint(Colors[I].Value);
Exit;
end;
Result := False;
end;
function StringToColor(const S: string): TColor;
begin
if not IdentToColor(S, Longint(Result)) then
Result := TColor(StrToInt(S));
end;
function IsRelativePath(const Filename: String): Boolean;
var
L: Integer;
begin
Result := True;
L := Length(Filename);
if ((L >= 1) and (Filename[1] = '\')) or
((L >= 2) and CharInSet(Filename[1], ['A'..'Z', 'a'..'z']) and (Filename[2] = ':')) then
Result := False;
end;
function GetSelfFilename: String;
{ Returns Filename of the calling DLL or application. (ParamStr(0) can only
return the filename of the calling application.) }
var
Buf: array[0..MAX_PATH-1] of Char;
begin
SetString(Result, Buf, GetModuleFileName(HInstance, Buf, SizeOf(Buf)))
end;
function CreateMemoryStreamFromFile(const Filename: String): TMemoryStream;
{ Creates a TMemoryStream and loads the contents of the specified file into it }
var
F: TFile;
SizeOfFile: Cardinal;
begin
Result := TMemoryStream.Create;
try
{ Why not use TMemoryStream.LoadFromFile here?
1. On Delphi 2 it opens files for exclusive access (not good).
2. It doesn't give specific error messages. }
F := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
try
SizeOfFile := F.CappedSize;
Result.SetSize(SizeOfFile);
F.ReadBuffer(Result.Memory^, SizeOfFile);
finally
F.Free;
end;
except
Result.Free;
raise Exception.CreateFmt(SCompilerReadError, [Filename, GetExceptMessage]);
end;
end;
function FileSizeAndCRCIs(const Filename: String; const Size: Cardinal;
const CRC: Longint): Boolean;
var
F: TFile;
SizeOfFile: Integer64;
Buf: AnsiString;
begin
Result := False;
try
F := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
try
SizeOfFile := F.Size;
if (SizeOfFile.Lo = Size) and (SizeOfFile.Hi = 0) then begin
SetLength(Buf, Size);
F.ReadBuffer(Buf[1], Size);
if GetCRC32(Buf[1], Size) = CRC then
Result := True;
end;
finally
F.Free;
end;
except
end;
end;
const
IMAGE_NT_SIGNATURE = $00004550; { 'PE'#0#0 }
IMAGE_NT_OPTIONAL_HDR32_MAGIC = $10b;
type
TImageFileHeader = packed record
Machine: Word;
NumberOfSections: Word;
TimeDateStamp: DWORD;
PointerToSymbolTable: DWORD;
NumberOfSymbols: DWORD;
SizeOfOptionalHeader: Word;
Characteristics: Word;
end;
function SeekToPEHeader(const F: TCustomFile): Boolean;
var
DosHeader: packed record
Sig: array[0..1] of AnsiChar;
Other: array[0..57] of Byte;
PEHeaderOffset: LongWord;
end;
Sig: DWORD;
begin
Result := False;
F.Seek(0);
if F.Read(DosHeader, SizeOf(DosHeader)) = SizeOf(DosHeader) then begin
if (DosHeader.Sig[0] = 'M') and (DosHeader.Sig[1] = 'Z') and
(DosHeader.PEHeaderOffset <> 0) then begin
F.Seek(DosHeader.PEHeaderOffset);
if F.Read(Sig, SizeOf(Sig)) = SizeOf(Sig) then
if Sig = IMAGE_NT_SIGNATURE then
Result := True;
end;
end;
end;
function IsX86OrX64Executable(const F: TFile): Boolean;
const
IMAGE_FILE_MACHINE_I386 = $014C;
IMAGE_FILE_MACHINE_AMD64 = $8664;
var
DosHeader: array[0..63] of Byte;
PEHeaderOffset: Longint;
PESigAndHeader: packed record
Sig: DWORD;
Machine: Word;
end;
begin
Result := False;
if F.Read(DosHeader, SizeOf(DosHeader)) = SizeOf(DosHeader) then begin
if (DosHeader[0] = Ord('M')) and (DosHeader[1] = Ord('Z')) then begin
PEHeaderOffset := PLongint(@DosHeader[60])^;
if PEHeaderOffset > 0 then begin
F.Seek(PEHeaderOffset);
if F.Read(PESigAndHeader, SizeOf(PESigAndHeader)) = SizeOf(PESigAndHeader) then begin
if (PESigAndHeader.Sig = IMAGE_NT_SIGNATURE) and
((PESigAndHeader.Machine = IMAGE_FILE_MACHINE_I386) or
(PESigAndHeader.Machine = IMAGE_FILE_MACHINE_AMD64)) then
Result := True;
end;
end;
end;
end;
F.Seek(0);
end;
function Is64BitPEImage(const Filename: String): Boolean;
{ Returns True if the specified file is a non-32-bit PE image, False
otherwise. }
var
F: TFile;
DosHeader: packed record
Sig: array[0..1] of AnsiChar;
Other: array[0..57] of Byte;
PEHeaderOffset: LongWord;
end;
PESigAndHeader: packed record
Sig: DWORD;
Header: TImageFileHeader;
OptHeaderMagic: Word;
end;
begin
Result := False;
F := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
try
if F.Read(DosHeader, SizeOf(DosHeader)) = SizeOf(DosHeader) then begin
if (DosHeader.Sig[0] = 'M') and (DosHeader.Sig[1] = 'Z') and
(DosHeader.PEHeaderOffset <> 0) then begin
F.Seek(DosHeader.PEHeaderOffset);
if F.Read(PESigAndHeader, SizeOf(PESigAndHeader)) = SizeOf(PESigAndHeader) then begin
if (PESigAndHeader.Sig = IMAGE_NT_SIGNATURE) and
(PESigAndHeader.OptHeaderMagic <> IMAGE_NT_OPTIONAL_HDR32_MAGIC) then
Result := True;
end;
end;
end;
finally
F.Free;
end;
end;
procedure UpdateSetupPEHeaderFields(const F: TCustomFile;
const IsTSAware, IsDEPCompatible, IsASLRCompatible: Boolean);
const
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = $0040;
IMAGE_DLLCHARACTERISTICS_NX_COMPAT = $0100;
IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = $8000;
OffsetOfImageVersion = $2C;
OffsetOfDllCharacteristics = $46;
var
Header: TImageFileHeader;
Ofs: Cardinal;
OptMagic, DllChars, OrigDllChars: Word;
ImageVersion: packed record
Major, Minor: Word;
end;
begin
if SeekToPEHeader(F) then begin
if (F.Read(Header, SizeOf(Header)) = SizeOf(Header)) and
(Header.SizeOfOptionalHeader = 224) then begin
Ofs := F.Position.Lo;
if (F.Read(OptMagic, SizeOf(OptMagic)) = SizeOf(OptMagic)) and
(OptMagic = IMAGE_NT_OPTIONAL_HDR32_MAGIC) then begin
{ Update MajorImageVersion and MinorImageVersion to 6.0.
Works around apparent bug in Vista (still present in Vista SP1;
not reproducible on Server 2008): When UAC is turned off,
launching an uninstaller (as admin) from ARP and answering No at the
ConfirmUninstall message box causes a "This program might not have
uninstalled correctly" dialog to be displayed, even if the EXE
has a proper "Vista-aware" manifest. I discovered that if the EXE's
image version is set to 6.0, like the EXEs that ship with Vista
(notepad.exe), the dialog does not appear. (This is reproducible
with notepad.exe too if its image version is changed to anything
other than 6.0 exactly.) }
F.Seek(Ofs + OffsetOfImageVersion);
ImageVersion.Major := 6;
ImageVersion.Minor := 0;
F.WriteBuffer(ImageVersion, SizeOf(ImageVersion));
{ Update DllCharacteristics }
F.Seek(Ofs + OffsetOfDllCharacteristics);
if F.Read(DllChars, SizeOf(DllChars)) = SizeOf(DllChars) then begin
OrigDllChars := DllChars;
if IsTSAware then
DllChars := DllChars or IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE
else
DllChars := DllChars and not IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
if IsDEPCompatible then
DllChars := DllChars or IMAGE_DLLCHARACTERISTICS_NX_COMPAT
else
DllChars := DllChars and not IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
{ Note: because we stripped relocations from Setup(Ldr).e32 during
compilation IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE won't actually
enable ASLR, but allow setting it anyway to make checkers happy. }
if IsASLRCompatible then
DllChars := DllChars or IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
else
DllChars := DllChars and not IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
if DllChars <> OrigDllChars then begin
F.Seek(Ofs + OffsetOfDllCharacteristics);
F.WriteBuffer(DllChars, SizeOf(DllChars));
end;
Exit;
end;
end;
end;
end;
raise Exception.Create('UpdateSetupPEHeaderFields failed');
end;
function CountChars(const S: String; C: Char): Integer;
var
I: Integer;
begin
Result := 0;
for I := 1 to Length(S) do
if S[I] = C then
Inc(Result);
end;
function IsValidIdentString(const S: String; AllowBackslash, AllowOperators: Boolean): Boolean;
var
I, N: Integer;
begin
if S = '' then
Result := False
else if not AllowOperators and ((CompareText(S, 'not') = 0) or
(CompareText(S, 'and') = 0) or (CompareText(S, 'or') = 0)) then
Result := False
else begin
N := Length(S);
for I := 1 to N do
if not (CharInSet(S[I], ['A'..'Z', 'a'..'z', '_']) or
((I > 1) and CharInSet(S[I], ['0'..'9'])) or
(AllowBackslash and (I > 1) and (I < N) and (S[I] = '\'))) then begin
Result := False;
Exit;
end;
Result := True;
end;
end;
procedure SkipWhitespace(var S: PChar);
begin
while CharInSet(S^, [#1..' ']) do
Inc(S);
end;
function ExtractWords(var S: PChar; const Sep: Char): String;
{ Extracts characters from S until it reaches the character Sep or the end
of S. The returned string has trailing whitespace characters trimmed off. }
var
StartPos, EndPos: PChar;
begin
StartPos := S;
EndPos := S;
while (S^ <> #0) and (S^ <> Sep) do begin
if S^ > ' ' then
EndPos := S + 1;
Inc(S);
end;
SetString(Result, StartPos, EndPos - StartPos);
end;
function UnescapeBraces(const S: String): String;
{ Changes all '{{' to '{'. Assumes that S does not contain any constants; you
should check before calling. }
var
I: Integer;
begin
Result := S;
I := 1;
while I < Length(Result) do begin
if Result[I] = '{' then begin
Inc(I);
if Result[I] = '{' then
Delete(Result, I, 1);
end
else begin
{$IFNDEF UNICODE}
if Result[I] in CompilerLeadBytes then
Inc(I);
{$ENDIF}
Inc(I);
end;
end;
end;
type
HCRYPTPROV = DWORD;
const
PROV_RSA_FULL = 1;
CRYPT_VERIFYCONTEXT = $F0000000;
function CryptAcquireContext(var phProv: HCRYPTPROV; pszContainer: PAnsiChar;
pszProvider: PAnsiChar; dwProvType: DWORD; dwFlags: DWORD): BOOL;
stdcall; external advapi32 name 'CryptAcquireContextA';
function CryptReleaseContext(hProv: HCRYPTPROV; dwFlags: DWORD): BOOL;
stdcall; external advapi32 name 'CryptReleaseContext';
function CryptGenRandom(hProv: HCRYPTPROV; dwLen: DWORD; pbBuffer: Pointer): BOOL;
stdcall; external advapi32 name 'CryptGenRandom';
var
CryptProv: HCRYPTPROV;
procedure GenerateRandomBytes(var Buffer; Bytes: Cardinal);
var
ErrorCode: DWORD;
begin
if CryptProv = 0 then begin
if not CryptAcquireContext(CryptProv, nil, nil, PROV_RSA_FULL,