Compiler.HelperFunc.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. unit Compiler.HelperFunc;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Additional compiler functions
  8. }
  9. interface
  10. uses
  11. Windows, Classes, SysUtils, Shared.FileClass;
  12. type
  13. TColor = $7FFFFFFF-1..$7FFFFFFF;
  14. const
  15. clScrollBar = TColor(COLOR_SCROLLBAR or $80000000);
  16. clBackground = TColor(COLOR_BACKGROUND or $80000000);
  17. clActiveCaption = TColor(COLOR_ACTIVECAPTION or $80000000);
  18. clInactiveCaption = TColor(COLOR_INACTIVECAPTION or $80000000);
  19. clMenu = TColor(COLOR_MENU or $80000000);
  20. clWindow = TColor(COLOR_WINDOW or $80000000);
  21. clWindowFrame = TColor(COLOR_WINDOWFRAME or $80000000);
  22. clMenuText = TColor(COLOR_MENUTEXT or $80000000);
  23. clWindowText = TColor(COLOR_WINDOWTEXT or $80000000);
  24. clCaptionText = TColor(COLOR_CAPTIONTEXT or $80000000);
  25. clActiveBorder = TColor(COLOR_ACTIVEBORDER or $80000000);
  26. clInactiveBorder = TColor(COLOR_INACTIVEBORDER or $80000000);
  27. clAppWorkSpace = TColor(COLOR_APPWORKSPACE or $80000000);
  28. clHighlight = TColor(COLOR_HIGHLIGHT or $80000000);
  29. clHighlightText = TColor(COLOR_HIGHLIGHTTEXT or $80000000);
  30. clBtnFace = TColor(COLOR_BTNFACE or $80000000);
  31. clBtnShadow = TColor(COLOR_BTNSHADOW or $80000000);
  32. clGrayText = TColor(COLOR_GRAYTEXT or $80000000);
  33. clBtnText = TColor(COLOR_BTNTEXT or $80000000);
  34. clInactiveCaptionText = TColor(COLOR_INACTIVECAPTIONTEXT or $80000000);
  35. clBtnHighlight = TColor(COLOR_BTNHIGHLIGHT or $80000000);
  36. cl3DDkShadow = TColor(COLOR_3DDKSHADOW or $80000000);
  37. cl3DLight = TColor(COLOR_3DLIGHT or $80000000);
  38. clInfoText = TColor(COLOR_INFOTEXT or $80000000);
  39. clInfoBk = TColor(COLOR_INFOBK or $80000000);
  40. clBlack = TColor($000000);
  41. clMaroon = TColor($000080);
  42. clGreen = TColor($008000);
  43. clOlive = TColor($008080);
  44. clNavy = TColor($800000);
  45. clPurple = TColor($800080);
  46. clTeal = TColor($808000);
  47. clGray = TColor($808080);
  48. clSilver = TColor($C0C0C0);
  49. clRed = TColor($0000FF);
  50. clLime = TColor($00FF00);
  51. clYellow = TColor($00FFFF);
  52. clBlue = TColor($FF0000);
  53. clFuchsia = TColor($FF00FF);
  54. clAqua = TColor($FFFF00);
  55. clLtGray = TColor($C0C0C0);
  56. clDkGray = TColor($808080);
  57. clWhite = TColor($FFFFFF);
  58. clNone = TColor($1FFFFFFF);
  59. clDefault = TColor($20000000);
  60. function StringToColor(const S: string): TColor;
  61. function IsRelativePath(const Filename: String): Boolean;
  62. function CreateMemoryStreamFromFile(const Filename: String; const CheckTrust: Boolean = False;
  63. const OnCheckedTrust: TProc<Boolean> = nil): TMemoryStream;
  64. function FileSizeAndCRCIs(const Filename: String; const Size: Cardinal;
  65. const CRC: Longint): Boolean;
  66. function IsX86OrX64Executable(const F: TFile): Boolean;
  67. function CountChars(const S: String; C: Char): Integer;
  68. function IsValidIdentString(const S: String; AllowBackslash, AllowOperators: Boolean): Boolean;
  69. procedure SkipWhitespace(var S: PChar);
  70. function ExtractWords(var S: PChar; const Sep: Char): String;
  71. function UnescapeBraces(const S: String): String;
  72. procedure GenerateRandomBytes(var Buffer; Bytes: Cardinal);
  73. implementation
  74. uses
  75. TrustFunc, Shared.CommonFunc, Shared.Int64Em,
  76. Compression.Base, Compiler.Messages;
  77. type
  78. TColorEntry = record
  79. Value: TColor;
  80. Name: string;
  81. end;
  82. const
  83. Colors: array[0..41] of TColorEntry = (
  84. (Value: clBlack; Name: 'clBlack'),
  85. (Value: clMaroon; Name: 'clMaroon'),
  86. (Value: clGreen; Name: 'clGreen'),
  87. (Value: clOlive; Name: 'clOlive'),
  88. (Value: clNavy; Name: 'clNavy'),
  89. (Value: clPurple; Name: 'clPurple'),
  90. (Value: clTeal; Name: 'clTeal'),
  91. (Value: clGray; Name: 'clGray'),
  92. (Value: clSilver; Name: 'clSilver'),
  93. (Value: clRed; Name: 'clRed'),
  94. (Value: clLime; Name: 'clLime'),
  95. (Value: clYellow; Name: 'clYellow'),
  96. (Value: clBlue; Name: 'clBlue'),
  97. (Value: clFuchsia; Name: 'clFuchsia'),
  98. (Value: clAqua; Name: 'clAqua'),
  99. (Value: clWhite; Name: 'clWhite'),
  100. (Value: clScrollBar; Name: 'clScrollBar'),
  101. (Value: clBackground; Name: 'clBackground'),
  102. (Value: clActiveCaption; Name: 'clActiveCaption'),
  103. (Value: clInactiveCaption; Name: 'clInactiveCaption'),
  104. (Value: clMenu; Name: 'clMenu'),
  105. (Value: clWindow; Name: 'clWindow'),
  106. (Value: clWindowFrame; Name: 'clWindowFrame'),
  107. (Value: clMenuText; Name: 'clMenuText'),
  108. (Value: clWindowText; Name: 'clWindowText'),
  109. (Value: clCaptionText; Name: 'clCaptionText'),
  110. (Value: clActiveBorder; Name: 'clActiveBorder'),
  111. (Value: clInactiveBorder; Name: 'clInactiveBorder'),
  112. (Value: clAppWorkSpace; Name: 'clAppWorkSpace'),
  113. (Value: clHighlight; Name: 'clHighlight'),
  114. (Value: clHighlightText; Name: 'clHighlightText'),
  115. (Value: clBtnFace; Name: 'clBtnFace'),
  116. (Value: clBtnShadow; Name: 'clBtnShadow'),
  117. (Value: clGrayText; Name: 'clGrayText'),
  118. (Value: clBtnText; Name: 'clBtnText'),
  119. (Value: clInactiveCaptionText; Name: 'clInactiveCaptionText'),
  120. (Value: clBtnHighlight; Name: 'clBtnHighlight'),
  121. (Value: cl3DDkShadow; Name: 'cl3DDkShadow'),
  122. (Value: cl3DLight; Name: 'cl3DLight'),
  123. (Value: clInfoText; Name: 'clInfoText'),
  124. (Value: clInfoBk; Name: 'clInfoBk'),
  125. (Value: clNone; Name: 'clNone'));
  126. function StringToColor(const S: string): TColor;
  127. function IdentToColor(const Ident: string; var Color: LongInt): Boolean;
  128. begin
  129. for var I := Low(Colors) to High(Colors) do
  130. if CompareText(Colors[I].Name, Ident) = 0 then
  131. begin
  132. Result := True;
  133. Color := LongInt(Colors[I].Value);
  134. Exit;
  135. end;
  136. Result := False;
  137. end;
  138. begin
  139. if not IdentToColor(S, Longint(Result)) then
  140. Result := TColor(StrToInt(S));
  141. end;
  142. function IsRelativePath(const Filename: String): Boolean;
  143. var
  144. L: Integer;
  145. begin
  146. Result := True;
  147. L := Length(Filename);
  148. if ((L >= 1) and (Filename[1] = '\')) or
  149. ((L >= 2) and CharInSet(Filename[1], ['A'..'Z', 'a'..'z']) and (Filename[2] = ':')) then
  150. Result := False;
  151. end;
  152. function CreateMemoryStreamFromFile(const Filename: String; const CheckTrust: Boolean;
  153. const OnCheckedTrust: TProc<Boolean>): TMemoryStream;
  154. { Creates a TMemoryStream and loads the contents of the specified file into it }
  155. var
  156. F: TFile;
  157. SizeOfFile: Cardinal;
  158. begin
  159. Result := TMemoryStream.Create;
  160. try
  161. var FS: TFileStream;
  162. if CheckTrust then begin
  163. try
  164. FS := CheckFileTrust(Filename, [cftoKeepOpen]);
  165. except
  166. raise Exception.CreateFmt(SCompilerCheckPrecompiledFileTrustError, [GetExceptMessage]);
  167. end;
  168. end else
  169. FS := nil;
  170. if Assigned(OnCheckedTrust) then
  171. OnCheckedTrust(CheckTrust);
  172. try
  173. { Why not use TMemoryStream.LoadFromFile here?
  174. 1. On Delphi 2 it opens files for exclusive access (not good).
  175. 2. It doesn't give specific error messages. }
  176. F := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
  177. try
  178. SizeOfFile := F.CappedSize;
  179. Result.SetSize(SizeOfFile);
  180. F.ReadBuffer(Result.Memory^, SizeOfFile);
  181. finally
  182. F.Free;
  183. end;
  184. finally
  185. FS.Free;
  186. end;
  187. except
  188. Result.Free;
  189. raise Exception.CreateFmt(SCompilerReadError, [Filename, GetExceptMessage]);
  190. end;
  191. end;
  192. function FileSizeAndCRCIs(const Filename: String; const Size: Cardinal;
  193. const CRC: Longint): Boolean;
  194. var
  195. F: TFile;
  196. SizeOfFile: Integer64;
  197. Buf: AnsiString;
  198. begin
  199. Result := False;
  200. try
  201. F := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
  202. try
  203. SizeOfFile := F.Size;
  204. if (SizeOfFile.Lo = Size) and (SizeOfFile.Hi = 0) then begin
  205. SetLength(Buf, Size);
  206. F.ReadBuffer(Buf[1], Size);
  207. if GetCRC32(Buf[1], Size) = CRC then
  208. Result := True;
  209. end;
  210. finally
  211. F.Free;
  212. end;
  213. except
  214. end;
  215. end;
  216. const
  217. IMAGE_NT_SIGNATURE = $00004550; { 'PE'#0#0 }
  218. IMAGE_NT_OPTIONAL_HDR32_MAGIC = $10b;
  219. type
  220. TImageFileHeader = packed record
  221. Machine: Word;
  222. NumberOfSections: Word;
  223. TimeDateStamp: DWORD;
  224. PointerToSymbolTable: DWORD;
  225. NumberOfSymbols: DWORD;
  226. SizeOfOptionalHeader: Word;
  227. Characteristics: Word;
  228. end;
  229. function IsX86OrX64Executable(const F: TFile): Boolean;
  230. const
  231. IMAGE_FILE_MACHINE_I386 = $014C;
  232. IMAGE_FILE_MACHINE_AMD64 = $8664;
  233. var
  234. DosHeader: array[0..63] of Byte;
  235. PEHeaderOffset: Longint;
  236. PESigAndHeader: packed record
  237. Sig: DWORD;
  238. Machine: Word;
  239. end;
  240. begin
  241. Result := False;
  242. if F.Read(DosHeader, SizeOf(DosHeader)) = SizeOf(DosHeader) then begin
  243. if (DosHeader[0] = Ord('M')) and (DosHeader[1] = Ord('Z')) then begin
  244. PEHeaderOffset := PLongint(@DosHeader[60])^;
  245. if PEHeaderOffset > 0 then begin
  246. F.Seek(PEHeaderOffset);
  247. if F.Read(PESigAndHeader, SizeOf(PESigAndHeader)) = SizeOf(PESigAndHeader) then begin
  248. if (PESigAndHeader.Sig = IMAGE_NT_SIGNATURE) and
  249. ((PESigAndHeader.Machine = IMAGE_FILE_MACHINE_I386) or
  250. (PESigAndHeader.Machine = IMAGE_FILE_MACHINE_AMD64)) then
  251. Result := True;
  252. end;
  253. end;
  254. end;
  255. end;
  256. F.Seek(0);
  257. end;
  258. function CountChars(const S: String; C: Char): Integer;
  259. var
  260. I: Integer;
  261. begin
  262. Result := 0;
  263. for I := 1 to Length(S) do
  264. if S[I] = C then
  265. Inc(Result);
  266. end;
  267. function IsValidIdentString(const S: String; AllowBackslash, AllowOperators: Boolean): Boolean;
  268. var
  269. I, N: Integer;
  270. begin
  271. if S = '' then
  272. Result := False
  273. else if not AllowOperators and ((CompareText(S, 'not') = 0) or
  274. (CompareText(S, 'and') = 0) or (CompareText(S, 'or') = 0)) then
  275. Result := False
  276. else begin
  277. N := Length(S);
  278. for I := 1 to N do
  279. if not (CharInSet(S[I], ['A'..'Z', 'a'..'z', '_']) or
  280. ((I > 1) and CharInSet(S[I], ['0'..'9'])) or
  281. (AllowBackslash and (I > 1) and (I < N) and (S[I] = '\'))) then begin
  282. Result := False;
  283. Exit;
  284. end;
  285. Result := True;
  286. end;
  287. end;
  288. procedure SkipWhitespace(var S: PChar);
  289. begin
  290. while CharInSet(S^, [#1..' ']) do
  291. Inc(S);
  292. end;
  293. function ExtractWords(var S: PChar; const Sep: Char): String;
  294. { Extracts characters from S until it reaches the character Sep or the end
  295. of S. The returned string has trailing whitespace characters trimmed off. }
  296. var
  297. StartPos, EndPos: PChar;
  298. begin
  299. StartPos := S;
  300. EndPos := S;
  301. while (S^ <> #0) and (S^ <> Sep) do begin
  302. if S^ > ' ' then
  303. EndPos := S + 1;
  304. Inc(S);
  305. end;
  306. SetString(Result, StartPos, EndPos - StartPos);
  307. end;
  308. function UnescapeBraces(const S: String): String;
  309. { Changes all '{{' to '{'. Assumes that S does not contain any constants; you
  310. should check before calling. }
  311. var
  312. I: Integer;
  313. begin
  314. Result := S;
  315. I := 1;
  316. while I < Length(Result) do begin
  317. if Result[I] = '{' then begin
  318. Inc(I);
  319. if Result[I] = '{' then
  320. Delete(Result, I, 1);
  321. end
  322. else
  323. Inc(I);
  324. end;
  325. end;
  326. type
  327. HCRYPTPROV = DWORD;
  328. const
  329. PROV_RSA_FULL = 1;
  330. CRYPT_VERIFYCONTEXT = $F0000000;
  331. function CryptAcquireContext(var phProv: HCRYPTPROV; pszContainer: PAnsiChar;
  332. pszProvider: PAnsiChar; dwProvType: DWORD; dwFlags: DWORD): BOOL;
  333. stdcall; external advapi32 name 'CryptAcquireContextA';
  334. function CryptReleaseContext(hProv: HCRYPTPROV; dwFlags: DWORD): BOOL;
  335. stdcall; external advapi32 name 'CryptReleaseContext';
  336. function CryptGenRandom(hProv: HCRYPTPROV; dwLen: DWORD; pbBuffer: Pointer): BOOL;
  337. stdcall; external advapi32 name 'CryptGenRandom';
  338. var
  339. CryptProv: HCRYPTPROV;
  340. procedure GenerateRandomBytes(var Buffer; Bytes: Cardinal);
  341. var
  342. ErrorCode: DWORD;
  343. begin
  344. if CryptProv = 0 then begin
  345. if not CryptAcquireContext(CryptProv, nil, nil, PROV_RSA_FULL,
  346. CRYPT_VERIFYCONTEXT) then begin
  347. ErrorCode := GetLastError;
  348. raise Exception.CreateFmt(SCompilerFunctionFailedWithCode,
  349. ['CryptAcquireContext', ErrorCode, Win32ErrorString(ErrorCode)]);
  350. end;
  351. { Note: CryptProv is released in the 'finalization' section of this unit }
  352. end;
  353. FillChar(Buffer, Bytes, 0);
  354. if not CryptGenRandom(CryptProv, Bytes, @Buffer) then begin
  355. ErrorCode := GetLastError;
  356. raise Exception.CreateFmt(SCompilerFunctionFailedWithCode,
  357. ['CryptGenRandom', ErrorCode, Win32ErrorString(ErrorCode)]);
  358. end;
  359. end;
  360. initialization
  361. finalization
  362. if CryptProv <> 0 then begin
  363. CryptReleaseContext(CryptProv, 0);
  364. CryptProv := 0;
  365. end;
  366. end.