Compiler.HelperFunc.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. unit Compiler.HelperFunc;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2024 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, 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 IdentToColor(const Ident: string; var Color: Longint): Boolean;
  61. function StringToColor(const S: string): TColor;
  62. function IsRelativePath(const Filename: String): Boolean;
  63. function CreateMemoryStreamFromFile(const Filename: String): 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. SysUtils, 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 IdentToColor(const Ident: string; var Color: Longint): Boolean;
  127. var
  128. I: Integer;
  129. begin
  130. for I := Low(Colors) to High(Colors) do
  131. if CompareText(Colors[I].Name, Ident) = 0 then
  132. begin
  133. Result := True;
  134. Color := Longint(Colors[I].Value);
  135. Exit;
  136. end;
  137. Result := False;
  138. end;
  139. function StringToColor(const S: string): TColor;
  140. begin
  141. if not IdentToColor(S, Longint(Result)) then
  142. Result := TColor(StrToInt(S));
  143. end;
  144. function IsRelativePath(const Filename: String): Boolean;
  145. var
  146. L: Integer;
  147. begin
  148. Result := True;
  149. L := Length(Filename);
  150. if ((L >= 1) and (Filename[1] = '\')) or
  151. ((L >= 2) and CharInSet(Filename[1], ['A'..'Z', 'a'..'z']) and (Filename[2] = ':')) then
  152. Result := False;
  153. end;
  154. function CreateMemoryStreamFromFile(const Filename: String): TMemoryStream;
  155. { Creates a TMemoryStream and loads the contents of the specified file into it }
  156. var
  157. F: TFile;
  158. SizeOfFile: Cardinal;
  159. begin
  160. Result := TMemoryStream.Create;
  161. try
  162. { Why not use TMemoryStream.LoadFromFile here?
  163. 1. On Delphi 2 it opens files for exclusive access (not good).
  164. 2. It doesn't give specific error messages. }
  165. F := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
  166. try
  167. SizeOfFile := F.CappedSize;
  168. Result.SetSize(SizeOfFile);
  169. F.ReadBuffer(Result.Memory^, SizeOfFile);
  170. finally
  171. F.Free;
  172. end;
  173. except
  174. Result.Free;
  175. raise Exception.CreateFmt(SCompilerReadError, [Filename, GetExceptMessage]);
  176. end;
  177. end;
  178. function FileSizeAndCRCIs(const Filename: String; const Size: Cardinal;
  179. const CRC: Longint): Boolean;
  180. var
  181. F: TFile;
  182. SizeOfFile: Integer64;
  183. Buf: AnsiString;
  184. begin
  185. Result := False;
  186. try
  187. F := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
  188. try
  189. SizeOfFile := F.Size;
  190. if (SizeOfFile.Lo = Size) and (SizeOfFile.Hi = 0) then begin
  191. SetLength(Buf, Size);
  192. F.ReadBuffer(Buf[1], Size);
  193. if GetCRC32(Buf[1], Size) = CRC then
  194. Result := True;
  195. end;
  196. finally
  197. F.Free;
  198. end;
  199. except
  200. end;
  201. end;
  202. const
  203. IMAGE_NT_SIGNATURE = $00004550; { 'PE'#0#0 }
  204. IMAGE_NT_OPTIONAL_HDR32_MAGIC = $10b;
  205. type
  206. TImageFileHeader = packed record
  207. Machine: Word;
  208. NumberOfSections: Word;
  209. TimeDateStamp: DWORD;
  210. PointerToSymbolTable: DWORD;
  211. NumberOfSymbols: DWORD;
  212. SizeOfOptionalHeader: Word;
  213. Characteristics: Word;
  214. end;
  215. function IsX86OrX64Executable(const F: TFile): Boolean;
  216. const
  217. IMAGE_FILE_MACHINE_I386 = $014C;
  218. IMAGE_FILE_MACHINE_AMD64 = $8664;
  219. var
  220. DosHeader: array[0..63] of Byte;
  221. PEHeaderOffset: Longint;
  222. PESigAndHeader: packed record
  223. Sig: DWORD;
  224. Machine: Word;
  225. end;
  226. begin
  227. Result := False;
  228. if F.Read(DosHeader, SizeOf(DosHeader)) = SizeOf(DosHeader) then begin
  229. if (DosHeader[0] = Ord('M')) and (DosHeader[1] = Ord('Z')) then begin
  230. PEHeaderOffset := PLongint(@DosHeader[60])^;
  231. if PEHeaderOffset > 0 then begin
  232. F.Seek(PEHeaderOffset);
  233. if F.Read(PESigAndHeader, SizeOf(PESigAndHeader)) = SizeOf(PESigAndHeader) then begin
  234. if (PESigAndHeader.Sig = IMAGE_NT_SIGNATURE) and
  235. ((PESigAndHeader.Machine = IMAGE_FILE_MACHINE_I386) or
  236. (PESigAndHeader.Machine = IMAGE_FILE_MACHINE_AMD64)) then
  237. Result := True;
  238. end;
  239. end;
  240. end;
  241. end;
  242. F.Seek(0);
  243. end;
  244. function CountChars(const S: String; C: Char): Integer;
  245. var
  246. I: Integer;
  247. begin
  248. Result := 0;
  249. for I := 1 to Length(S) do
  250. if S[I] = C then
  251. Inc(Result);
  252. end;
  253. function IsValidIdentString(const S: String; AllowBackslash, AllowOperators: Boolean): Boolean;
  254. var
  255. I, N: Integer;
  256. begin
  257. if S = '' then
  258. Result := False
  259. else if not AllowOperators and ((CompareText(S, 'not') = 0) or
  260. (CompareText(S, 'and') = 0) or (CompareText(S, 'or') = 0)) then
  261. Result := False
  262. else begin
  263. N := Length(S);
  264. for I := 1 to N do
  265. if not (CharInSet(S[I], ['A'..'Z', 'a'..'z', '_']) or
  266. ((I > 1) and CharInSet(S[I], ['0'..'9'])) or
  267. (AllowBackslash and (I > 1) and (I < N) and (S[I] = '\'))) then begin
  268. Result := False;
  269. Exit;
  270. end;
  271. Result := True;
  272. end;
  273. end;
  274. procedure SkipWhitespace(var S: PChar);
  275. begin
  276. while CharInSet(S^, [#1..' ']) do
  277. Inc(S);
  278. end;
  279. function ExtractWords(var S: PChar; const Sep: Char): String;
  280. { Extracts characters from S until it reaches the character Sep or the end
  281. of S. The returned string has trailing whitespace characters trimmed off. }
  282. var
  283. StartPos, EndPos: PChar;
  284. begin
  285. StartPos := S;
  286. EndPos := S;
  287. while (S^ <> #0) and (S^ <> Sep) do begin
  288. if S^ > ' ' then
  289. EndPos := S + 1;
  290. Inc(S);
  291. end;
  292. SetString(Result, StartPos, EndPos - StartPos);
  293. end;
  294. function UnescapeBraces(const S: String): String;
  295. { Changes all '{{' to '{'. Assumes that S does not contain any constants; you
  296. should check before calling. }
  297. var
  298. I: Integer;
  299. begin
  300. Result := S;
  301. I := 1;
  302. while I < Length(Result) do begin
  303. if Result[I] = '{' then begin
  304. Inc(I);
  305. if Result[I] = '{' then
  306. Delete(Result, I, 1);
  307. end
  308. else
  309. Inc(I);
  310. end;
  311. end;
  312. type
  313. HCRYPTPROV = DWORD;
  314. const
  315. PROV_RSA_FULL = 1;
  316. CRYPT_VERIFYCONTEXT = $F0000000;
  317. function CryptAcquireContext(var phProv: HCRYPTPROV; pszContainer: PAnsiChar;
  318. pszProvider: PAnsiChar; dwProvType: DWORD; dwFlags: DWORD): BOOL;
  319. stdcall; external advapi32 name 'CryptAcquireContextA';
  320. function CryptReleaseContext(hProv: HCRYPTPROV; dwFlags: DWORD): BOOL;
  321. stdcall; external advapi32 name 'CryptReleaseContext';
  322. function CryptGenRandom(hProv: HCRYPTPROV; dwLen: DWORD; pbBuffer: Pointer): BOOL;
  323. stdcall; external advapi32 name 'CryptGenRandom';
  324. var
  325. CryptProv: HCRYPTPROV;
  326. procedure GenerateRandomBytes(var Buffer; Bytes: Cardinal);
  327. var
  328. ErrorCode: DWORD;
  329. begin
  330. if CryptProv = 0 then begin
  331. if not CryptAcquireContext(CryptProv, nil, nil, PROV_RSA_FULL,
  332. CRYPT_VERIFYCONTEXT) then begin
  333. ErrorCode := GetLastError;
  334. raise Exception.CreateFmt(SCompilerFunctionFailedWithCode,
  335. ['CryptAcquireContext', ErrorCode, Win32ErrorString(ErrorCode)]);
  336. end;
  337. { Note: CryptProv is released in the 'finalization' section of this unit }
  338. end;
  339. FillChar(Buffer, Bytes, 0);
  340. if not CryptGenRandom(CryptProv, Bytes, @Buffer) then begin
  341. ErrorCode := GetLastError;
  342. raise Exception.CreateFmt(SCompilerFunctionFailedWithCode,
  343. ['CryptGenRandom', ErrorCode, Win32ErrorString(ErrorCode)]);
  344. end;
  345. end;
  346. initialization
  347. finalization
  348. if CryptProv <> 0 then begin
  349. CryptReleaseContext(CryptProv, 0);
  350. CryptProv := 0;
  351. end;
  352. end.