ISPP.Funcs.pas 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. {
  2. Inno Setup Preprocessor
  3. Copyright (C) 2001-2002 Alex Yackimoff
  4. Inno Setup
  5. Copyright (C) 1997-2025 Jordan Russell
  6. Portions by Martijn Laan
  7. For conditions of distribution and use, see LICENSE.TXT.
  8. }
  9. unit ISPP.Funcs;
  10. interface
  11. uses
  12. Windows, Classes,
  13. ISPP.VarUtils, ISPP.Intf, ISPP.Preprocessor, ISPP.Parser;
  14. procedure RegisterFunctions(Preproc: TPreprocessor);
  15. implementation
  16. uses
  17. SysUtils, IniFiles, Registry, Math, DateUtils,
  18. MD5, SHA1, SHA256, PathFunc, UnsignedFunc,
  19. Shared.FileClass, Shared.CommonFunc,
  20. ISPP.Sessions, ISPP.Consts, ISPP.Base, ISPP.IdentMan;
  21. var
  22. IsWin64: Boolean;
  23. function PrependPath(const Ext: NativeInt; const Filename: String): String;
  24. begin
  25. var Preprocessor := TObject(Ext) as TPreprocessor;
  26. Result := PathExpand(Preprocessor.PrependDirName(Filename,
  27. Preprocessor.SourcePath));
  28. end;
  29. function CheckParams(const Params: IIsppFuncParams;
  30. Types: array of TIsppVarType; Minimum: Integer; var Error: TIsppFuncResult): Boolean;
  31. var
  32. I: Integer;
  33. begin
  34. FillChar(Error, SizeOf(TIsppFuncResult), 0);
  35. Result := False;
  36. if Params.GetCount < Minimum then
  37. begin
  38. Error.ErrParam := Minimum;
  39. Error.Error := ISPPFUNC_INSUFARGS;
  40. Exit;
  41. end
  42. else if Params.GetCount > (High(Types) + 1) then
  43. begin
  44. Error.ErrParam := High(Types) + 1;
  45. Error.Error := ISPPFUNC_MANYARGS;
  46. Exit;
  47. end
  48. else
  49. with IInternalFuncParams(Params) do
  50. for I := 0 to Params.GetCount - 1 do
  51. begin
  52. if (Types[I] = evSpecial) or (Get(I)^.Typ = evNull) then Continue;
  53. if Types[I] <> Get(I)^.Typ then
  54. begin
  55. if Types[I] = evStr then
  56. Error.Error := ISPPFUNC_STRWANTED
  57. else
  58. Error.Error := ISPPFUNC_INTWANTED;
  59. Error.ErrParam := I;
  60. Exit;
  61. end;
  62. end;
  63. Result := True;
  64. end;
  65. function Int(Ext: NativeInt; const Params: IIsppFuncParams;
  66. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  67. procedure MakeError(E: Exception);
  68. begin
  69. FuncResult.Error(PChar(E.Message));
  70. Result.Error := ISPPFUNC_FAIL;
  71. end;
  72. begin
  73. if CheckParams(Params, [evSpecial, evInt], 1, Result) then
  74. try
  75. with IInternalFuncParams(Params) do
  76. ResPtr^ := ToInt(Get(0)^);
  77. except
  78. on E: EConvertError do
  79. with IInternalFuncParams(Params) do
  80. begin
  81. if GetCount > 1 then
  82. ResPtr^ := Get(1)^
  83. else
  84. MakeError(E);
  85. end;
  86. on E: Exception do
  87. MakeError(E);
  88. end;
  89. end;
  90. function Str(Ext: NativeInt; const Params: IIsppFuncParams;
  91. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  92. begin
  93. if CheckParams(Params, [evSpecial], 1, Result) then
  94. try
  95. with IInternalFuncParams(Params) do
  96. ResPtr^ := ToStr(Get(0)^);
  97. except
  98. on E: Exception do
  99. begin
  100. FuncResult.Error(PChar(E.Message));
  101. Result.Error := ISPPFUNC_FAIL
  102. end;
  103. end;
  104. end;
  105. {FileExists(<filename>)}
  106. function FileExists(Ext: NativeInt; const Params: IIsppFuncParams; const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  107. begin
  108. if CheckParams(Params, [evStr], 1, Result) then
  109. try
  110. with IInternalFuncParams(Params) do
  111. MakeBool(ResPtr^, NewFileExists(PrependPath(Ext, Get(0).AsStr)));
  112. except
  113. on E: Exception do
  114. begin
  115. FuncResult.Error(PChar(E.Message));
  116. Result.Error := ISPPFUNC_FAIL
  117. end;
  118. end;
  119. end;
  120. function DirExists(Ext: NativeInt; const Params: IIsppFuncParams; const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  121. begin
  122. if CheckParams(Params, [evStr], 1, Result) then
  123. try
  124. with IInternalFuncParams(Params) do
  125. MakeBool(ResPtr^, Shared.CommonFunc.DirExists(PrependPath(Ext, Get(0).AsStr)));
  126. except
  127. on E: Exception do
  128. begin
  129. FuncResult.Error(PChar(E.Message));
  130. Result.Error := ISPPFUNC_FAIL
  131. end;
  132. end;
  133. end;
  134. function ForceDirectoriesFunc(Ext: NativeInt; const Params: IIsppFuncParams; const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  135. begin
  136. if CheckParams(Params, [evStr], 1, Result) then
  137. try
  138. with IInternalFuncParams(Params) do
  139. MakeBool(ResPtr^, ForceDirectories(PrependPath(Ext, Get(0).AsStr)));
  140. except
  141. on E: Exception do
  142. begin
  143. FuncResult.Error(PChar(E.Message));
  144. Result.Error := ISPPFUNC_FAIL
  145. end;
  146. end;
  147. end;
  148. {FileSize(<filename>)}
  149. function FileSize(Ext: NativeInt; const Params: IIsppFuncParams; const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  150. var
  151. SearchRec: TSearchRec;
  152. begin
  153. if CheckParams(Params, [evStr], 1, Result) then
  154. try
  155. with IInternalFuncParams(Params) do
  156. begin
  157. if FindFirst(PrependPath(Ext, Get(0).AsStr), faAnyFile, SearchRec) = 0 then begin
  158. try
  159. MakeInt(ResPtr^, SearchRec.Size);
  160. finally
  161. FindClose(SearchRec);
  162. end;
  163. end else
  164. MakeInt(ResPtr^, -1);
  165. end
  166. except
  167. on E: Exception do
  168. begin
  169. FuncResult.Error(PChar(E.Message));
  170. Result.Error := ISPPFUNC_FAIL
  171. end;
  172. end;
  173. end;
  174. {ReadIni(<file:str>,<section:str>,<name:str>,[<default:str>])}
  175. function ReadIni(Ext: NativeInt; const Params: IIsppFuncParams;
  176. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  177. var
  178. Default: string;
  179. begin
  180. if CheckParams(Params, [evStr, evStr, evStr, evStr], 3, Result) then
  181. try
  182. with IInternalFuncParams(Params) do
  183. with TIniFile.Create(Get(0).AsStr) do
  184. try
  185. if GetCount < 4 then Default := '' else Default := Get(3).AsStr;
  186. MakeStr(ResPtr^, ReadString(Get(1).AsStr, Get(2).AsStr, Default));
  187. finally
  188. Free
  189. end;
  190. except
  191. on E: Exception do
  192. begin
  193. FuncResult.Error(PChar(E.Message));
  194. Result.Error := ISPPFUNC_FAIL
  195. end;
  196. end;
  197. end;
  198. function WriteIni(Ext: NativeInt; const Params: IIsppFuncParams;
  199. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  200. begin
  201. if CheckParams(Params, [evStr, evStr, evStr, evSpecial], 4, Result) then
  202. try
  203. with IInternalFuncParams(Params) do
  204. with TIniFile.Create(Get(0).AsStr) do
  205. try
  206. case Get(3).Typ of
  207. evInt: WriteInt64(Get(1).AsStr, Get(2).AsStr, Get(3).AsInt64);
  208. evStr: WriteString(Get(1).AsStr, Get(2).AsStr, Get(3).AsStr);
  209. else
  210. WriteString(Get(1).AsStr, Get(2).AsStr, '');
  211. end;
  212. ResPtr^ := NULL;
  213. finally
  214. Free;
  215. end;
  216. except
  217. on E: Exception do
  218. begin
  219. FuncResult.Error(PChar(E.Message));
  220. Result.Error := ISPPFUNC_FAIL
  221. end;
  222. end;
  223. end;
  224. {ReadReg(<root:int>,<key:str>,[<name:str>,<default:str>])}
  225. function ReadReg(Ext: NativeInt; const Params: IIsppFuncParams;
  226. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  227. const
  228. ISPPRootKeyFlagMask = $7F000000;
  229. ISPPRootKeyFlag64Bit = $02000000;
  230. ISPPRootKeyValidFlags = ISPPRootKeyFlag64Bit;
  231. procedure CrackISPPRootKey(const ISPPRootKey: Int64; var RegView64: Boolean;
  232. var RootKey: HKEY);
  233. begin
  234. { Allow only predefined key handles (8xxxxxxx). Can't accept handles to
  235. open keys because they might have our special flag bits set.
  236. Also reject unknown flags which may have a meaning in the future. }
  237. if (ISPPRootKey shr 31 <> 1) or
  238. ((ISPPRootKey and ISPPRootKeyFlagMask) and not ISPPRootKeyValidFlags <> 0) then
  239. raise Exception.Create('Invalid root key value');
  240. if ISPPRootKey and ISPPRootKeyFlag64Bit <> 0 then begin
  241. if not IsWin64 then
  242. raise Exception.Create('Cannot access 64-bit registry keys on this version of Windows');
  243. RegView64 := True
  244. end
  245. else
  246. RegView64 := False;
  247. RootKey := HKEY(ISPPRootKey and not ISPPRootKeyFlagMask);
  248. end;
  249. var
  250. Name: string;
  251. Default: TIsppVariant;
  252. RegView64: Boolean;
  253. ARootKey: HKEY;
  254. AAccess: Cardinal;
  255. begin
  256. if CheckParams(Params, [evInt, evStr, evStr, evSpecial], 2, Result) then
  257. try
  258. with IInternalFuncParams(Params) do begin
  259. CrackISPPRootKey(Get(0).AsInt64, RegView64, ARootKey);
  260. AAccess := KEY_QUERY_VALUE;
  261. if RegView64 then
  262. AAccess := AAccess or KEY_WOW64_64KEY;
  263. with TRegistry.Create(AAccess) do
  264. try
  265. RootKey := ARootKey;
  266. if GetCount < 3 then Name := '' else Name := Get(2).AsStr;
  267. if GetCount < 4 then Default := NULL else Default := Get(3)^;
  268. if OpenKey(Get(1).AsStr, False) and ((Name = '') or ValueExists(Name)) then
  269. case GetDataType(Name) of
  270. rdString, rdExpandString: MakeStr(ResPtr^, ReadString(Name));
  271. rdInteger: MakeInt(ResPtr^, ReadInteger(Name));
  272. else
  273. CopyExpVar(Default, ResPtr^);
  274. end
  275. else
  276. CopyExpVar(Default, ResPtr^);
  277. finally
  278. Free
  279. end;
  280. end;
  281. except
  282. on E: Exception do
  283. begin
  284. FuncResult.Error(PChar(E.Message));
  285. Result.Error := ISPPFUNC_FAIL
  286. end;
  287. end;
  288. end;
  289. function GetEnvFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  290. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  291. begin
  292. if CheckParams(Params, [evStr], 1, Result) then
  293. try
  294. with IInternalFuncParams(Params) do
  295. MakeStr(ResPtr^, GetEnv(Get(0).AsStr));
  296. except
  297. on E: Exception do
  298. begin
  299. FuncResult.Error(PChar(E.Message));
  300. Result.Error := ISPPFUNC_FAIL
  301. end;
  302. end;
  303. end;
  304. const
  305. SSetup = '[Setup]';
  306. function SetupSetting(Ext: NativeInt; const Params: IIsppFuncParams;
  307. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  308. function Find(L: TStrings; const S: string): string;
  309. var
  310. I, J: Integer;
  311. InSetupSection: Boolean;
  312. N: string;
  313. begin
  314. InSetupSection := False;
  315. Result := '';
  316. with L do
  317. for I := 0 to Count - 1 do
  318. begin
  319. if Trim(Strings[I]) = '' then Continue;
  320. if InSetupSection then
  321. begin
  322. if (Trim(Strings[I])[1] = '[') then
  323. begin
  324. if not SameText(Trim(Strings[I]), SSetup) then
  325. InSetupSection := False;
  326. Continue;
  327. end;
  328. J := Pos('=', Strings[I]);
  329. if J > 0 then N := Trim(Copy(Strings[I], 1, J - 1));
  330. if CompareText(N, S) = 0 then
  331. begin
  332. Result := Trim(Copy(Strings[I], J + 1, MaxInt));
  333. Break;
  334. end;
  335. end
  336. else
  337. if SameText(Trim(Strings[I]), SSetup) then
  338. InSetupSection := True;
  339. end;
  340. end;
  341. begin
  342. if CheckParams(Params, [evStr], 1, Result) then
  343. try
  344. with IInternalFuncParams(Params) do
  345. begin
  346. MakeStr(ResPtr^, Find(TPreprocessor(Ext).StringList, Get(0).AsStr));
  347. end;
  348. except
  349. on E: Exception do
  350. begin
  351. FuncResult.Error(PChar(E.Message));
  352. Result.Error := ISPPFUNC_FAIL
  353. end;
  354. end;
  355. end;
  356. {SetSetupSetting(<SetupSectionParameterName>,<ParameterValue>)}
  357. function SetSetupSetting(Ext: NativeInt; const Params: IIsppFuncParams;
  358. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  359. procedure DoSet(L: TStrings; const S, V: string);
  360. var
  361. I, J, FirstSetupSectionLine: Integer;
  362. InSetupSection: Boolean;
  363. N: string;
  364. begin
  365. FirstSetupSectionLine := -1;
  366. InSetupSection := False;
  367. with L do
  368. begin
  369. for I := 0 to Count - 1 do
  370. begin
  371. if Trim(Strings[I]) = '' then Continue;
  372. if InSetupSection then
  373. begin
  374. if (Trim(Strings[I])[1] = '[') then
  375. begin
  376. if not SameText(Trim(Strings[I]), SSetup) then
  377. InSetupSection := False;
  378. Continue;
  379. end;
  380. J := Pos('=', Strings[I]);
  381. if J > 0 then N := Trim(Copy(Strings[I], 1, J - 1));
  382. if CompareText(N, S) = 0 then
  383. begin
  384. Strings[I] := S + '=' + V;
  385. Exit;
  386. end;
  387. end
  388. else
  389. if SameText(Trim(Strings[I]), SSetup) then
  390. begin
  391. InSetupSection := True;
  392. if FirstSetupSectionLine < 0 then
  393. FirstSetupSectionLine := I;
  394. end;
  395. end;
  396. if FirstSetupSectionLine < 0 then
  397. FirstSetupSectionLine := L.Add(SSetup);
  398. L.Insert(FirstSetupSectionLine + 1, S + '=' + V);
  399. end;
  400. end;
  401. begin
  402. if CheckParams(Params, [evStr, evStr], 2, Result) then
  403. try
  404. with IInternalFuncParams(Params) do
  405. begin
  406. ResPtr^.Typ := evNull;
  407. DoSet(TPreprocessor(Ext).StringList, Get(0).AsStr, Get(1).AsStr);
  408. end;
  409. except
  410. on E: Exception do
  411. begin
  412. FuncResult.Error(PChar(E.Message));
  413. Result.Error := ISPPFUNC_FAIL
  414. end;
  415. end;
  416. end;
  417. {EntryCount(<SectionName>)}
  418. function EntryCountFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  419. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  420. var
  421. I, J: Integer;
  422. DoCount: Boolean;
  423. N, S: string;
  424. begin
  425. if CheckParams(Params, [evStr], 1, Result) then
  426. try
  427. J := 0;
  428. DoCount := False;
  429. with IInternalFuncParams(Params), TStringList(TPreprocessor(Ext).StringList) do
  430. begin
  431. S := Get(0).AsStr;
  432. for I := 0 to Count - 1 do
  433. begin
  434. N := Trim(Strings[I]);
  435. if (N <> '') and (N[1] <> ';') and (N[1] = '[') then
  436. begin
  437. if DoCount then
  438. DoCount := False
  439. else
  440. if CompareText(Copy(N, 2, Length(N) - 2), S) = 0 then
  441. DoCount := True;
  442. Continue;
  443. end;
  444. if DoCount and (N <> '') and (N[1] <> ';') then Inc(J);
  445. end;
  446. MakeInt(ResPtr^, J);
  447. end;
  448. except
  449. on E: Exception do
  450. begin
  451. FuncResult.Error(PChar(E.Message));
  452. Result.Error := ISPPFUNC_FAIL
  453. end;
  454. end;
  455. end;
  456. {SaveToFile(<Filename>)}
  457. function SaveToFile(Ext: NativeInt; const Params: IIsppFuncParams;
  458. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  459. begin
  460. if CheckParams(Params, [evStr], 1, Result) then
  461. try
  462. with IInternalFuncParams(Params) do
  463. TPreprocessor(Ext).SaveToFile(PrependPath(Ext, Get(0).AsStr));
  464. except
  465. on E: Exception do
  466. begin
  467. FuncResult.Error(PChar(E.Message));
  468. Result.Error := ISPPFUNC_FAIL
  469. end;
  470. end;
  471. end;
  472. {Find(<what>[,<contains>[,<what>,<contains>[,<what>[,<contains>]]]])}
  473. function FindLine(Ext: NativeInt; const Params: IIsppFuncParams;
  474. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  475. const
  476. FIND_WHEREMASK = $01 or $02;
  477. FIND_SENSITIVE = $04;
  478. FIND_OR = $08;
  479. FIND_NOT = $10;
  480. FIND_TRIM = $20;
  481. type
  482. TFindWhere = (fwMatch, fwBegin, fwEnd, fwContains);
  483. var
  484. I: Integer;
  485. StartFromLine: Integer;
  486. Found, MoreFound, Second, Third: Boolean;
  487. Flags: array[0..2] of Integer;
  488. Strs: array[0..2] of string;
  489. Str: string;
  490. function Compare(const S1, S2: string; Sensitive: Boolean): Boolean;
  491. begin
  492. if Sensitive then
  493. Result := AnsiCompareStr(S1, S2) = 0
  494. else
  495. Result := AnsiCompareText(S1, S2) = 0;
  496. end;
  497. function Contains(const Substr: string; Sensitive: Boolean): Boolean;
  498. var
  499. L, I: Integer;
  500. begin
  501. Result := True;
  502. L := Length(Substr);
  503. for I := 1 to Length(Str) - L + 1 do
  504. if Compare(Substr, Copy(Str, I, L), Sensitive) then Exit;
  505. Result := False;
  506. end;
  507. function Meets(const Substr: string; Sensitive: Boolean; Where: Integer): Boolean;
  508. begin
  509. Result := False;
  510. case Where of
  511. 1: if Length(Substr) <= Length(Str) then
  512. Result := Compare(Substr, Copy(Str, 1, Length(Substr)), Sensitive);
  513. 2: if Length(Substr) <= Length(Str) then
  514. Result := Compare(Substr, Copy(Str, Length(Str) - Length(Substr) + 1, Length(Substr)), Sensitive);
  515. 3: if Length(Substr) <= Length(Str) then
  516. Result := Contains(Substr, Sensitive);
  517. else Result := Compare(Substr, Str, Sensitive);
  518. end;
  519. end;
  520. begin
  521. if CheckParams(Params, [evInt, evStr, evInt, evStr, evInt, evStr, evInt], 2, Result) then
  522. try
  523. with IInternalFuncParams(Params) do
  524. begin
  525. FillChar(Flags, SizeOf(Flags), 0);
  526. Strs[0] := Get(1).AsStr;
  527. Second := False;
  528. Third := False;
  529. if GetCount > 2 then
  530. begin
  531. Flags[0] := Get(2).AsInteger;
  532. if GetCount > 3 then
  533. begin
  534. Strs[1] := Get(3).AsStr;
  535. Second := True;
  536. if GetCount > 4 then
  537. begin
  538. Flags[1] := Get(4).AsInteger;
  539. if GetCount > 5 then
  540. begin
  541. Strs[2] := Get(5).AsStr;
  542. Third := True;
  543. if GetCount > 6 then Flags[2] := Get(6).AsInteger;
  544. end
  545. end;
  546. end
  547. end;
  548. StartFromLine := Get(0).AsInteger;
  549. if StartFromLine < 0 then StartFromLine := 0;
  550. with TStringList(TPreprocessor(Ext).StringList) do
  551. for I := StartFromLine to Count - 1 do
  552. begin
  553. Str := Strings[I];
  554. if Flags[0] and FIND_TRIM <> 0 then
  555. Str := Trim(Str);
  556. Found := Meets(Strs[0], Flags[0] and FIND_SENSITIVE <> 0,
  557. Flags[0] and FIND_WHEREMASK) xor (Flags[0] and FIND_NOT <> 0);
  558. if Second and (((Flags[1] and FIND_OR <> 0{OR}) and not Found) or
  559. ((Flags[1] and FIND_OR = 0{AND}) and Found)) then
  560. begin
  561. MoreFound := Meets(Strs[1], Flags[1] and FIND_SENSITIVE <> 0,
  562. Flags[1] and FIND_WHEREMASK) xor (Flags[1] and FIND_NOT <> 0);
  563. if Flags[1] and FIND_OR <> 0 then
  564. Found := Found or MoreFound
  565. else
  566. Found := Found and MoreFound;
  567. end;
  568. if Third and (((Flags[2] and FIND_OR <> 0{OR}) and not Found) or
  569. ((Flags[2] and FIND_OR = 0{AND}) and Found)) then
  570. begin
  571. MoreFound := Meets(Strs[2], Flags[2] and FIND_SENSITIVE <> 0,
  572. Flags[2] and FIND_WHEREMASK) xor (Flags[2] and FIND_NOT <> 0);
  573. if Flags[2] and FIND_OR <> 0 then
  574. Found := Found or MoreFound
  575. else
  576. Found := Found and MoreFound;
  577. end;
  578. if Found then
  579. begin
  580. MakeInt(ResPtr^, I);
  581. Exit;
  582. end;
  583. end;
  584. MakeInt(ResPtr^, -2);
  585. end;
  586. except
  587. on E: Exception do
  588. begin
  589. FuncResult.Error(PChar(E.Message));
  590. Result.Error := ISPPFUNC_FAIL
  591. end;
  592. end;
  593. end;
  594. function Exec(const Filename, Params: String; WorkingDir: String;
  595. const WaitUntilTerminated: Boolean; const ShowCmd: Word;
  596. const Preprocessor: TPreprocessor; const OutputReader: TCreateProcessOutputReader;
  597. var ResultCode: Integer): Boolean;
  598. var
  599. CmdLine: String;
  600. WorkingDirP: PChar;
  601. StartupInfo: TStartupInfo;
  602. ProcessInfo: TProcessInformation;
  603. begin
  604. {This function is a combination of InstFuncs' InstExec and Compile's InternalSignCommand }
  605. if Filename = '>' then
  606. CmdLine := Params
  607. else begin
  608. if (Filename = '') or (Filename[1] <> '"') then
  609. CmdLine := '"' + Filename + '"'
  610. else
  611. CmdLine := Filename;
  612. if Params <> '' then
  613. CmdLine := CmdLine + ' ' + Params;
  614. if SameText(PathExtractExt(Filename), '.bat') or
  615. SameText(PathExtractExt(Filename), '.cmd') then begin
  616. { See InstExec for explanation }
  617. CmdLine := '"' + AddBackslash(GetSystemDir) + 'cmd.exe" /C "' + CmdLine + '"'
  618. end;
  619. if WorkingDir = '' then
  620. WorkingDir := PathExtractDir(Filename);
  621. end;
  622. FillChar (StartupInfo, SizeOf(StartupInfo), 0);
  623. StartupInfo.cb := SizeOf(StartupInfo);
  624. StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  625. StartupInfo.wShowWindow := ShowCmd;
  626. if WorkingDir <> '' then
  627. WorkingDirP := PChar(WorkingDir)
  628. else
  629. WorkingDirP := nil;
  630. var InheritHandles := False;
  631. var dwCreationFlags: DWORD := CREATE_DEFAULT_ERROR_MODE;
  632. if (OutputReader <> nil) and WaitUntilTerminated then begin
  633. OutputReader.UpdateStartupInfo(StartupInfo);
  634. InheritHandles := True;
  635. dwCreationFlags := dwCreationFlags or CREATE_NO_WINDOW;
  636. end;
  637. Result := CreateProcess(nil, PChar(CmdLine), nil, nil, InheritHandles,
  638. dwCreationFlags, nil, WorkingDirP, StartupInfo, ProcessInfo);
  639. if not Result then begin
  640. DWORD(ResultCode) := GetLastError;
  641. Exit;
  642. end;
  643. { Don't need the thread handle, so close it now }
  644. CloseHandle(ProcessInfo.hThread);
  645. if OutputReader <> nil then
  646. OutputReader.NotifyCreateProcessDone;
  647. try
  648. if WaitUntilTerminated then begin
  649. while True do begin
  650. case WaitForSingleObject(ProcessInfo.hProcess, 50) of
  651. WAIT_OBJECT_0: Break;
  652. WAIT_TIMEOUT:
  653. begin
  654. if OutputReader <> nil then
  655. OutputReader.Read(False);
  656. Preprocessor.CallIdleProc; { Doesn't allow an Abort }
  657. end;
  658. else
  659. Preprocessor.RaiseError('Exec: WaitForSingleObject failed');
  660. end;
  661. end;
  662. if OutputReader <> nil then
  663. OutputReader.Read(True);
  664. end;
  665. { Get the exit code. Will be set to STILL_ACTIVE if not yet available }
  666. if not GetExitCodeProcess(ProcessInfo.hProcess, DWORD(ResultCode)) then
  667. ResultCode := -1; { just in case }
  668. finally
  669. CloseHandle(ProcessInfo.hProcess);
  670. end;
  671. end;
  672. procedure ExecLog(const S: String; const Error, FirstLine: Boolean; const Data: NativeInt);
  673. begin
  674. var Preprocessor := TPreprocessor(Data);
  675. if Error then
  676. Preprocessor.WarningMsg(S)
  677. else
  678. Preprocessor.StatusMsg('Exec output: %s', [S]);
  679. end;
  680. {
  681. int Exec(str FileName, str Params, str WorkingDir, int Wait, int ShowCmd, int Log)
  682. }
  683. function ExecFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  684. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  685. begin
  686. if CheckParams(Params, [evStr, evStr, evStr, evInt, evInt, evInt], 1, Result) then
  687. try
  688. with IInternalFuncParams(Params) do
  689. begin
  690. var ParamsS, WorkingDir: String;
  691. var WaitUntilTerminated := True;
  692. var ShowCmd: Word := SW_SHOWNORMAL;
  693. if GetCount > 1 then ParamsS := Get(1).AsStr;
  694. if GetCount > 2 then WorkingDir := PrependPath(Ext, Get(2).AsStr);
  695. if (GetCount > 3) and (Get(3).Typ <> evNull) then WaitUntilTerminated := Get(3).AsBoolean;
  696. if (GetCount > 4) and (Get(4).Typ <> evNull) then ShowCmd := Get(4).AsWord;
  697. var Preprocessor := TPreprocessor(Ext);
  698. var ResultCode: Integer;
  699. var OutputReader := TCreateProcessOutputReader.Create(ExecLog, NativeInt(Preprocessor));
  700. try
  701. var Success := Exec(Get(0).AsStr, ParamsS, WorkingDir, WaitUntilTerminated,
  702. ShowCmd, Preprocessor, OutputReader, ResultCode);
  703. if not WaitUntilTerminated then
  704. MakeBool(ResPtr^, Success)
  705. else
  706. MakeInt(ResPtr^, ResultCode);
  707. finally
  708. OutputReader.Free;
  709. end;
  710. end;
  711. except
  712. on E: Exception do
  713. begin
  714. FuncResult.Error(PChar(E.Message));
  715. Result.Error := ISPPFUNC_FAIL
  716. end;
  717. end;
  718. end;
  719. type
  720. PExecAndGetFirstLineLogData = ^TExecAndGetFirstLineLogData;
  721. TExecAndGetFirstLineLogData = record
  722. Preprocessor: TPreprocessor;
  723. Line: String;
  724. end;
  725. procedure ExecAndGetFirstLineLog(const S: String; const Error, FirstLine: Boolean; const Data: NativeInt);
  726. begin
  727. var Data2 := PExecAndGetFirstLineLogData(Data);
  728. if not Error and (Data2.Line = '') and (S.Trim <> '') then
  729. Data2.Line := S;
  730. ExecLog(S, Error, FirstLine, NativeInt(Data2.Preprocessor));
  731. end;
  732. {
  733. str ExecAndGetFirstLine(str FileName, str Params, str WorkingDir,)
  734. }
  735. function ExecAndGetFirstLineFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  736. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  737. begin
  738. if CheckParams(Params, [evStr, evStr, evStr], 1, Result) then
  739. try
  740. with IInternalFuncParams(Params) do
  741. begin
  742. var ParamsS, WorkingDir: String;
  743. if GetCount > 1 then ParamsS := Get(1).AsStr;
  744. if GetCount > 2 then WorkingDir := PrependPath(Ext, Get(2).AsStr);
  745. var Data: TExecAndGetFirstLineLogData;
  746. Data.Preprocessor := TPreprocessor(Ext);
  747. Data.Line := '';
  748. var ResultCode: Integer;
  749. var OutputReader := TCreateProcessOutputReader.Create(ExecAndGetFirstLineLog, NativeInt(@Data));
  750. try
  751. var Success := Exec(Get(0).AsStr, ParamsS, WorkingDir, True,
  752. SW_SHOWNORMAL, Data.Preprocessor, OutputReader, ResultCode);
  753. if Success then
  754. MakeStr(ResPtr^, Data.Line)
  755. else begin
  756. Data.Preprocessor.WarningMsg('CreateProcess failed (%d).', [ResultCode]);
  757. ResPtr^.Typ := evNull;
  758. end;
  759. finally
  760. OutputReader.Free;
  761. end;
  762. end;
  763. except
  764. on E: Exception do
  765. begin
  766. FuncResult.Error(PChar(E.Message));
  767. Result.Error := ISPPFUNC_FAIL
  768. end;
  769. end;
  770. end;
  771. function LenFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  772. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  773. begin
  774. if CheckParams(Params, [evStr], 1, Result) then
  775. try
  776. with IInternalFuncParams(Params) do
  777. MakeInt(ResPtr^, Length(Get(0).AsStr));
  778. except
  779. on E: Exception do
  780. begin
  781. FuncResult.Error(PChar(E.Message));
  782. Result.Error := ISPPFUNC_FAIL
  783. end;
  784. end;
  785. end;
  786. function CopyFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  787. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  788. var
  789. S: string;
  790. B, C: Int64;
  791. begin
  792. if CheckParams(Params, [evStr, evInt, evInt], 2, Result) then
  793. try
  794. with IInternalFuncParams(Params) do
  795. begin
  796. S := Get(0).AsStr;
  797. B := Get(1).AsInt64;
  798. if GetCount > 2 then C := Get(2).AsInt64 else C := MaxInt;
  799. { Constrain 64-bit arguments to 32 bits without truncating them }
  800. if B < 1 then
  801. B := 1;
  802. if C > MaxInt then
  803. C := MaxInt;
  804. if (B > MaxInt) or (C < 0) then begin
  805. { Result should be empty in these cases }
  806. B := 1;
  807. C := 0;
  808. end;
  809. MakeStr(ResPtr^, Copy(S, Integer(B), Integer(C)));
  810. end;
  811. except
  812. on E: Exception do
  813. begin
  814. FuncResult.Error(PChar(E.Message));
  815. Result.Error := ISPPFUNC_FAIL
  816. end;
  817. end;
  818. end;
  819. function PosFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  820. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  821. begin
  822. if CheckParams(Params, [evStr, evStr], 2, Result) then
  823. try
  824. with IInternalFuncParams(Params) do
  825. begin
  826. MakeInt(ResPtr^, Pos(Get(0).AsStr, Get(1).AsStr));
  827. end;
  828. except
  829. on E: Exception do
  830. begin
  831. FuncResult.Error(PChar(E.Message));
  832. Result.Error := ISPPFUNC_FAIL
  833. end;
  834. end;
  835. end;
  836. function LowerCaseFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  837. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  838. begin
  839. if CheckParams(Params, [evStr], 1, Result) then
  840. try
  841. with IInternalFuncParams(Params) do
  842. MakeStr(ResPtr^, LowerCase(Get(0).AsStr));
  843. except
  844. on E: Exception do
  845. begin
  846. FuncResult.Error(PChar(E.Message));
  847. Result.Error := ISPPFUNC_FAIL
  848. end;
  849. end;
  850. end;
  851. function UpperCaseFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  852. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  853. begin
  854. if CheckParams(Params, [evStr], 1, Result) then
  855. try
  856. with IInternalFuncParams(Params) do
  857. MakeStr(ResPtr^, UpperCase(Get(0).AsStr));
  858. except
  859. on E: Exception do
  860. begin
  861. FuncResult.Error(PChar(E.Message));
  862. Result.Error := ISPPFUNC_FAIL
  863. end;
  864. end;
  865. end;
  866. function RPosFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  867. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  868. function RPos(const Substr, S: string): Integer;
  869. begin
  870. for Result := Length(S) - Length(Substr) + 1 downto 1 do
  871. if Copy(S, Result, Length(Substr)) = Substr then
  872. Exit;
  873. Result := 0;
  874. end;
  875. begin
  876. if CheckParams(Params, [evStr, evStr], 2, Result) then
  877. try
  878. with IInternalFuncParams(Params) do
  879. begin
  880. MakeInt(ResPtr^, RPos(Get(0).AsStr, Get(1).AsStr));
  881. end;
  882. except
  883. on E: Exception do
  884. begin
  885. FuncResult.Error(PChar(E.Message));
  886. Result.Error := ISPPFUNC_FAIL
  887. end;
  888. end;
  889. end;
  890. function GetVersionNumbersStringFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  891. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  892. var
  893. Filename: string;
  894. VersionHandle: Cardinal;
  895. SIZE: Cardinal;
  896. S: UINT;
  897. Buf: Pointer;
  898. FI: PVSFixedFileInfo;
  899. begin
  900. if CheckParams(Params, [evStr], 1, Result) then
  901. try
  902. with IInternalFuncParams(Params) do
  903. begin
  904. ResPtr^.Typ := evNull;
  905. Filename := PrependPath(Ext, Get(0).AsStr);
  906. Size := GetFileVersionInfoSize(PChar(Filename), VersionHandle);
  907. if Size > 0 then
  908. begin
  909. GetMem(Buf, Size);
  910. try
  911. GetFileVersionInfo(PChar(Filename), VersionHandle, Size, Buf);
  912. if VerQueryValue(Buf, '\', Pointer(FI), S) then
  913. begin
  914. MakeStr(ResPtr^,
  915. IntToStr((FI.dwFileVersionMS and $FFFF0000) shr 16) + '.' +
  916. IntToStr(FI.dwFileVersionMS and $FFFF) + '.' +
  917. IntToStr((FI.dwFileVersionLS and $FFFF0000) shr 16) + '.' +
  918. IntToStr(FI.dwFileVersionLS and $FFFF)
  919. );
  920. end;
  921. finally
  922. FreeMem(Buf)
  923. end;
  924. end
  925. end;
  926. except
  927. on E: Exception do
  928. begin
  929. FuncResult.Error(PChar(E.Message));
  930. Result.Error := ISPPFUNC_FAIL
  931. end;
  932. end;
  933. end;
  934. function ComparePackedVersionFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  935. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  936. begin
  937. if CheckParams(Params, [evInt, evInt], 2, Result) then
  938. try
  939. with IInternalFuncParams(Params) do
  940. MakeInt(ResPtr^, CompareInt64(Get(0).AsInt64, Get(1).AsInt64));
  941. except
  942. on E: Exception do
  943. begin
  944. FuncResult.Error(PChar(E.Message));
  945. Result.Error := ISPPFUNC_FAIL
  946. end;
  947. end;
  948. end;
  949. function SamePackedVersionFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  950. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  951. begin
  952. if CheckParams(Params, [evInt, evInt], 2, Result) then
  953. try
  954. with IInternalFuncParams(Params) do
  955. if Get(0).AsInt64 = Get(1).AsInt64 then
  956. MakeInt(ResPtr^, 1)
  957. else
  958. MakeInt(ResPtr^, 0)
  959. except
  960. on E: Exception do
  961. begin
  962. FuncResult.Error(PChar(E.Message));
  963. Result.Error := ISPPFUNC_FAIL
  964. end;
  965. end;
  966. end;
  967. {str GetStringFileInfo(str FileName, str StringName, int Lang)}
  968. function GetFileVersionInfoItem(Ext: NativeInt; const Params: IIsppFuncParams;
  969. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  970. var
  971. Buf: Pointer;
  972. function GetStringFileInfo(Lang: UINT; const Name: string; var Value: string): Boolean;
  973. var
  974. InfoBuf: Pointer;
  975. InfoBufSize: Cardinal;
  976. begin
  977. Result := VerQueryValue(Buf, PChar('\StringFileInfo\' + IntToHex(LoWord(Lang), 4) +
  978. IntToHex(HiWord(Lang), 4) +
  979. '\' + Name), InfoBuf, InfoBufSize) and (InfoBufSize > 0);
  980. if Result then SetString(Value, PChar(InfoBuf), InfoBufSize - 1)
  981. end;
  982. type
  983. TUINTArray = array[0..$100] of UINT;
  984. PUINTArray = ^TUINTArray;
  985. var
  986. Filename: string;
  987. VersionHandle: Cardinal;
  988. Langs: PUINTArray;
  989. Lang, LangsSize: UINT;
  990. Value: string;
  991. Success: Boolean;
  992. begin
  993. if CheckParams(Params, [evStr, evStr, evInt], 2, Result) then
  994. try
  995. with IInternalFuncParams(Params) do
  996. begin
  997. Success := False;
  998. ResPtr^.Typ := evNull;
  999. Filename := PrependPath(Ext, Get(0).AsStr);
  1000. const Size = GetFileVersionInfoSize(PChar(Filename), VersionHandle);
  1001. if Size > 0 then
  1002. begin
  1003. GetMem(Buf, Size);
  1004. try
  1005. GetFileVersionInfo(PChar(Filename), VersionHandle, Size, Buf);
  1006. if GetCount > 2 then
  1007. begin
  1008. Lang := Get(2).AsCardinal;
  1009. Success := GetStringFileInfo(Lang, Get(1).AsStr, Value);
  1010. end
  1011. else
  1012. begin
  1013. if VerQueryValue(Buf, PChar('\VarFileInfo\Translation'), Pointer(Langs),
  1014. LangsSize) then
  1015. begin
  1016. const LangCount = LangsSize div 4;
  1017. for var I := 0 to LangCount - 1 do
  1018. begin
  1019. Success := GetStringFileInfo(Langs[I], Get(1).AsStr, Value);
  1020. if Success then Break;
  1021. end;
  1022. end;
  1023. end;
  1024. if Success then
  1025. MakeStr(ResPtr^, Value);
  1026. finally
  1027. FreeMem(Buf)
  1028. end;
  1029. end;
  1030. end;
  1031. except
  1032. on E: Exception do
  1033. begin
  1034. FuncResult.Error(PChar(E.Message));
  1035. Result.Error := ISPPFUNC_FAIL
  1036. end;
  1037. end;
  1038. end;
  1039. function DelFileFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1040. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1041. begin
  1042. if CheckParams(Params, [evStr], 1, Result) then
  1043. try
  1044. with IInternalFuncParams(Params) do
  1045. begin
  1046. QueueFileForDeletion(PrependPath(Ext, Get(0).AsStr));
  1047. ResPtr^.Typ := evNull;
  1048. end;
  1049. except
  1050. on E: Exception do
  1051. begin
  1052. FuncResult.Error(PChar(E.Message));
  1053. Result.Error := ISPPFUNC_FAIL
  1054. end;
  1055. end;
  1056. end;
  1057. function DelFileNowFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1058. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1059. begin
  1060. if CheckParams(Params, [evStr], 1, Result) then
  1061. try
  1062. with IInternalFuncParams(Params) do
  1063. begin
  1064. DeleteFile(PChar(PrependPath(Ext, Get(0).AsStr)));
  1065. ResPtr^.Typ := evNull;
  1066. end;
  1067. except
  1068. on E: Exception do
  1069. begin
  1070. FuncResult.Error(PChar(E.Message));
  1071. Result.Error := ISPPFUNC_FAIL
  1072. end;
  1073. end;
  1074. end;
  1075. function CopyFileFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1076. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1077. begin
  1078. if CheckParams(Params, [evStr, evStr], 2, Result) then
  1079. try
  1080. with IInternalFuncParams(Params) do
  1081. begin
  1082. CopyFile(PChar(PrependPath(Ext, Get(0).AsStr)), PChar(PrependPath(Ext, Get(1).AsStr)), False);
  1083. ResPtr^.Typ := evNull;
  1084. end;
  1085. except
  1086. on E: Exception do
  1087. begin
  1088. FuncResult.Error(PChar(E.Message));
  1089. Result.Error := ISPPFUNC_FAIL
  1090. end;
  1091. end;
  1092. end;
  1093. type
  1094. PSearchRec = ^TSearchRec;
  1095. procedure GarbageCloseFind(Item: Pointer);
  1096. begin
  1097. FindClose(PSearchRec(Item)^);
  1098. Dispose(Item);
  1099. end;
  1100. function FindFirstFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1101. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1102. var
  1103. Filename: string;
  1104. SR: PSearchRec;
  1105. begin
  1106. if CheckParams(Params, [evStr, evInt], 2, Result) then
  1107. try
  1108. with IInternalFuncParams(Params) do
  1109. begin
  1110. Filename := PrependPath(Ext, Get(0).AsStr);
  1111. New(SR);
  1112. ResPtr^.Typ := evInt;
  1113. if FindFirst(Filename, Get(1).AsInteger, SR^) = 0 then
  1114. begin
  1115. ResPtr^.AsInt64 := NativeInt(SR);
  1116. TPreprocessor(Ext).CollectGarbage(SR, Addr(GarbageCloseFind));
  1117. end
  1118. else
  1119. begin
  1120. ResPtr^.AsInt64 := 0;
  1121. Dispose(SR);
  1122. end;
  1123. end;
  1124. except
  1125. on E: Exception do
  1126. begin
  1127. FuncResult.Error(PChar(E.Message));
  1128. Result.Error := ISPPFUNC_FAIL
  1129. end;
  1130. end;
  1131. end;
  1132. function FindNextFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1133. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1134. begin
  1135. if CheckParams(Params, [evInt], 1, Result) then
  1136. try
  1137. with IInternalFuncParams(Params) do
  1138. begin
  1139. ResPtr.Typ := evInt;
  1140. if FindNext(PSearchRec(Get(0).AsInt64)^) = 0 then
  1141. ResPtr^.AsInt64 := 1
  1142. else
  1143. ResPtr^.AsInt64 := 0;
  1144. end;
  1145. except
  1146. on E: Exception do
  1147. begin
  1148. FuncResult.Error(PChar(E.Message));
  1149. Result.Error := ISPPFUNC_FAIL
  1150. end;
  1151. end;
  1152. end;
  1153. function FindGetFileName(Ext: NativeInt; const Params: IIsppFuncParams;
  1154. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1155. begin
  1156. if CheckParams(Params, [evInt], 1, Result) then
  1157. try
  1158. with IInternalFuncParams(Params) do
  1159. begin
  1160. MakeStr(ResPtr^, PSearchRec(Get(0).AsInt64)^.Name);
  1161. end;
  1162. except
  1163. on E: Exception do
  1164. begin
  1165. FuncResult.Error(PChar(E.Message));
  1166. Result.Error := ISPPFUNC_FAIL
  1167. end;
  1168. end;
  1169. end;
  1170. function FindCloseFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1171. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1172. begin
  1173. if CheckParams(Params, [evInt], 1, Result) then
  1174. try
  1175. with IInternalFuncParams(Params) do
  1176. begin
  1177. FindClose(PSearchRec(Get(0).AsInt64)^);
  1178. Dispose(PSearchRec(Get(0).AsInt64));
  1179. TPreprocessor(Ext).UncollectGarbage(Pointer(Get(0).AsInt64));
  1180. end;
  1181. except
  1182. on E: Exception do
  1183. begin
  1184. FuncResult.Error(PChar(E.Message));
  1185. Result.Error := ISPPFUNC_FAIL
  1186. end;
  1187. end;
  1188. end;
  1189. type
  1190. PTextFile = ^TextFile;
  1191. procedure GarbageCloseFile(Item: Pointer);
  1192. var
  1193. F: PTextFile;
  1194. begin
  1195. F := Item;
  1196. Close(F^);
  1197. Dispose(F);
  1198. end;
  1199. function FileOpenFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1200. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1201. var
  1202. F: PTextFile;
  1203. begin
  1204. if CheckParams(Params, [evStr], 1, Result) then
  1205. try
  1206. New(F);
  1207. try
  1208. with IInternalFuncParams(Params) do
  1209. begin
  1210. FileMode := fmOpenRead or fmShareDenyWrite;
  1211. AssignFile(F^, PrependPath(Ext, Get(0).AsStr));
  1212. {$I-}
  1213. Reset(F^);
  1214. {$I+}
  1215. if IOResult <> 0 then
  1216. begin
  1217. Dispose(F);
  1218. MakeInt(ResPtr^, 0)
  1219. end
  1220. else
  1221. begin
  1222. MakeInt(ResPtr^, NativeInt(F));
  1223. TPreprocessor(Ext).CollectGarbage(F, Addr(GarbageCloseFile));
  1224. end;
  1225. end;
  1226. except
  1227. Dispose(F);
  1228. raise
  1229. end;
  1230. except
  1231. on E: Exception do
  1232. begin
  1233. FuncResult.Error(PChar(E.Message));
  1234. Result.Error := ISPPFUNC_FAIL
  1235. end;
  1236. end;
  1237. end;
  1238. function FileGetHandle(const Params: IInternalFuncParams; const Index: Integer): PTextFile;
  1239. begin
  1240. Result := PTextFile(Params.Get(Index).AsInt64);
  1241. if Result = nil then
  1242. raise Exception.Create('Invalid file handle');
  1243. end;
  1244. function FileReadFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1245. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1246. var
  1247. S: string;
  1248. begin
  1249. if CheckParams(Params, [evInt], 1, Result) then
  1250. try
  1251. const P = IInternalFuncParams(Params);
  1252. const F = FileGetHandle(P, 0);
  1253. {$I-}
  1254. Readln(F^, S);
  1255. {$I+}
  1256. if IOResult <> 0 then
  1257. P.ResPtr^ := NULL
  1258. else
  1259. MakeStr(P.ResPtr^, S);
  1260. except
  1261. on E: Exception do
  1262. begin
  1263. FuncResult.Error(PChar(E.Message));
  1264. Result.Error := ISPPFUNC_FAIL
  1265. end;
  1266. end;
  1267. end;
  1268. function FileResetFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1269. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1270. begin
  1271. if CheckParams(Params, [evInt], 1, Result) then
  1272. try
  1273. const P = IInternalFuncParams(Params);
  1274. const F = FileGetHandle(P, 0);
  1275. {$I-}
  1276. Reset(F^);
  1277. {$I+}
  1278. if IOResult <> 0 then
  1279. raise Exception.Create('Failed to reset a file')
  1280. else
  1281. P.ResPtr^ := NULL
  1282. except
  1283. on E: Exception do
  1284. begin
  1285. FuncResult.Error(PChar(E.Message));
  1286. Result.Error := ISPPFUNC_FAIL
  1287. end;
  1288. end;
  1289. end;
  1290. function FileEofFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1291. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1292. var
  1293. IsEof: Boolean;
  1294. begin
  1295. if CheckParams(Params, [evInt], 1, Result) then
  1296. try
  1297. const P = IInternalFuncParams(Params);
  1298. const F = FileGetHandle(P, 0);
  1299. {$I-}
  1300. IsEof := Eof(F^);
  1301. {$I+}
  1302. if IOResult <> 0 then
  1303. P.ResPtr^ := NULL
  1304. else
  1305. MakeBool(P.ResPtr^, IsEof);
  1306. except
  1307. on E: Exception do
  1308. begin
  1309. FuncResult.Error(PChar(E.Message));
  1310. Result.Error := ISPPFUNC_FAIL
  1311. end;
  1312. end;
  1313. end;
  1314. function FileCloseFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1315. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1316. begin
  1317. if CheckParams(Params, [evInt], 1, Result) then
  1318. try
  1319. const P = IInternalFuncParams(Params);
  1320. const F = FileGetHandle(P, 0);
  1321. {$I-}
  1322. Close(F^);
  1323. {$I+}
  1324. P.ResPtr^ := NULL;
  1325. Dispose(F);
  1326. TPreprocessor(Ext).UncollectGarbage(Pointer(F));
  1327. except
  1328. on E: Exception do
  1329. begin
  1330. FuncResult.Error(PChar(E.Message));
  1331. Result.Error := ISPPFUNC_FAIL
  1332. end;
  1333. end;
  1334. end;
  1335. function SaveStringToFileFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1336. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1337. var
  1338. Filename: String;
  1339. F: TextFile;
  1340. DoAppend: Boolean;
  1341. CodePage: Word;
  1342. begin
  1343. if CheckParams(Params, [evStr, evStr, evInt, evInt], 2, Result) then
  1344. try
  1345. with IInternalFuncParams(Params) do
  1346. begin
  1347. Filename := PrependPath(Ext, Get(0).AsStr);
  1348. if (GetCount < 3) or Get(2).AsBoolean then DoAppend := True else DoAppend := False;
  1349. if (GetCount < 4) or Get(3).AsBoolean then CodePage := CP_UTF8 else CodePage := 0;
  1350. DoAppend := DoAppend and NewFileExists(Filename);
  1351. AssignFile(F, FileName, CodePage);
  1352. {$I-}
  1353. if DoAppend then
  1354. Append(F)
  1355. else
  1356. Rewrite(F);
  1357. {$I+}
  1358. if IOResult <> 0 then
  1359. MakeInt(ResPtr^, 0)
  1360. else begin
  1361. try
  1362. MakeInt(ResPtr^, 1);
  1363. if not DoAppend and (CodePage = CP_UTF8) then
  1364. Write(F, #$FEFF); //Strings are UTF-16 so this UTF-16 BOM will actually be saved as an UTF-8 BOM
  1365. Write(F, Get(1).AsStr);
  1366. finally
  1367. CloseFile(F);
  1368. end;
  1369. end;
  1370. end;
  1371. except
  1372. on E: Exception do
  1373. begin
  1374. FuncResult.Error(PChar(E.Message));
  1375. Result.Error := ISPPFUNC_FAIL
  1376. end;
  1377. end;
  1378. end;
  1379. type
  1380. PDateTime = ^TDateTime;
  1381. procedure GarbageReleaseDateTime(Item: Pointer);
  1382. begin
  1383. Dispose(Item);
  1384. end;
  1385. function FileGetDate(Ext: NativeInt; const Params: IIsppFuncParams;
  1386. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1387. var
  1388. FileDate: PDateTime;
  1389. Age: TDateTime;
  1390. begin
  1391. if CheckParams(Params, [evStr], 1, Result) then
  1392. try
  1393. with IInternalFuncParams(Params) do
  1394. begin
  1395. if FileAge(PrependPath(Ext, Get(0).AsStr), Age) then
  1396. begin
  1397. New(FileDate);
  1398. FileDate^ := Age;
  1399. TPreprocessor(Ext).CollectGarbage(FileDate, GarbageReleaseDateTime);
  1400. MakeInt(ResPtr^, Int64(FileDate));
  1401. end
  1402. else
  1403. MakeInt(ResPtr^, -1);
  1404. end;
  1405. except
  1406. on E: Exception do
  1407. begin
  1408. FuncResult.Error(PChar(E.Message));
  1409. Result.Error := ISPPFUNC_FAIL
  1410. end;
  1411. end;
  1412. end;
  1413. function GetNow(Ext: NativeInt; const Params: IIsppFuncParams;
  1414. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1415. var
  1416. DateTime: PDateTime;
  1417. begin
  1418. if CheckParams(Params, [], 0, Result) then
  1419. try
  1420. with IInternalFuncParams(Params) do
  1421. begin
  1422. New(DateTime);
  1423. DateTime^ := Now;
  1424. TPreprocessor(Ext).CollectGarbage(DateTime, GarbageReleaseDateTime);
  1425. MakeInt(ResPtr^, Int64(DateTime));
  1426. end;
  1427. except
  1428. on E: Exception do
  1429. begin
  1430. FuncResult.Error(PChar(E.Message));
  1431. Result.Error := ISPPFUNC_FAIL
  1432. end;
  1433. end;
  1434. end;
  1435. function GetDateFromDT(Ext: NativeInt; const Params: IIsppFuncParams;
  1436. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1437. begin
  1438. if CheckParams(Params, [evInt], 1, Result) then
  1439. try
  1440. with IInternalFuncParams(Params) do
  1441. begin
  1442. MakeInt(ResPtr^, DateTimeToTimeStamp(PDateTime(Get(0).AsInt64)^).Date);
  1443. end;
  1444. except
  1445. on E: EAccessViolation do
  1446. begin
  1447. FuncResult.Error('Invalid datetime value');
  1448. Result.Error := ISPPFUNC_FAIL;
  1449. end;
  1450. on E: Exception do
  1451. begin
  1452. FuncResult.Error(PChar(E.Message));
  1453. Result.Error := ISPPFUNC_FAIL
  1454. end;
  1455. end;
  1456. end;
  1457. function GetTimeFromDT(Ext: NativeInt; const Params: IIsppFuncParams;
  1458. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1459. begin
  1460. if CheckParams(Params, [evInt], 1, Result) then
  1461. try
  1462. with IInternalFuncParams(Params) do
  1463. begin
  1464. MakeInt(ResPtr^, DateTimeToTimeStamp(PDateTime(Get(0).AsInt64)^).Time);
  1465. end;
  1466. except
  1467. on E: EAccessViolation do
  1468. begin
  1469. FuncResult.Error('Invalid datetime value');
  1470. Result.Error := ISPPFUNC_FAIL;
  1471. end;
  1472. on E: Exception do
  1473. begin
  1474. FuncResult.Error(PChar(E.Message));
  1475. Result.Error := ISPPFUNC_FAIL
  1476. end;
  1477. end;
  1478. end;
  1479. function GetDateTimeString(Ext: NativeInt; const Params: IIsppFuncParams;
  1480. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1481. var
  1482. NewDateSeparatorString, NewTimeSeparatorString: String;
  1483. OldDateSeparator, OldTimeSeparator: Char;
  1484. begin
  1485. if CheckParams(Params, [evStr, evStr, evStr], 3, Result) then
  1486. try
  1487. with IInternalFuncParams(Params) do
  1488. begin
  1489. OldDateSeparator := FormatSettings.DateSeparator;
  1490. OldTimeSeparator := FormatSettings.TimeSeparator;
  1491. try
  1492. NewDateSeparatorString := Get(1).AsStr;
  1493. NewTimeSeparatorString := Get(2).AsStr;
  1494. if NewDateSeparatorString <> '' then
  1495. FormatSettings.DateSeparator := NewDateSeparatorString[1];
  1496. if NewTimeSeparatorString <> '' then
  1497. FormatSettings.TimeSeparator := NewTimeSeparatorString[1];
  1498. MakeStr(ResPtr^, FormatDateTime(Get(0).AsStr, Now()));
  1499. finally
  1500. FormatSettings.TimeSeparator := OldTimeSeparator;
  1501. FormatSettings.DateSeparator := OldDateSeparator;
  1502. end;
  1503. end;
  1504. except
  1505. on E: Exception do
  1506. begin
  1507. FuncResult.Error(PChar(E.Message));
  1508. Result.Error := ISPPFUNC_FAIL
  1509. end;
  1510. end;
  1511. end;
  1512. function GetFileDateTimeString(Ext: NativeInt; const Params: IIsppFuncParams;
  1513. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1514. var
  1515. NewDateSeparatorString, NewTimeSeparatorString: String;
  1516. OldDateSeparator, OldTimeSeparator: Char;
  1517. Age: TDateTime;
  1518. begin
  1519. if CheckParams(Params, [evStr, evStr, evStr, evStr], 4, Result) then
  1520. try
  1521. with IInternalFuncParams(Params) do
  1522. begin
  1523. OldDateSeparator := FormatSettings.DateSeparator;
  1524. OldTimeSeparator := FormatSettings.TimeSeparator;
  1525. try
  1526. NewDateSeparatorString := Get(2).AsStr;
  1527. NewTimeSeparatorString := Get(3).AsStr;
  1528. if NewDateSeparatorString <> '' then
  1529. FormatSettings.DateSeparator := NewDateSeparatorString[1];
  1530. if NewTimeSeparatorString <> '' then
  1531. FormatSettings.TimeSeparator := NewTimeSeparatorString[1];
  1532. if not FileAge(PrependPath(Ext, Get(0).AsStr), Age) then begin
  1533. FuncResult.Error('Invalid file name');
  1534. Result.Error := ISPPFUNC_FAIL
  1535. end else
  1536. MakeStr(ResPtr^, FormatDateTime(Get(1).AsStr, Age));
  1537. finally
  1538. FormatSettings.TimeSeparator := OldTimeSeparator;
  1539. FormatSettings.DateSeparator := OldDateSeparator;
  1540. end;
  1541. end;
  1542. except
  1543. on E: Exception do
  1544. begin
  1545. FuncResult.Error(PChar(E.Message));
  1546. Result.Error := ISPPFUNC_FAIL
  1547. end;
  1548. end;
  1549. end;
  1550. function GetMD5OfFile(Ext: NativeInt; const Params: IIsppFuncParams;
  1551. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1552. var
  1553. Buf: array[0..65535] of Byte;
  1554. begin
  1555. if CheckParams(Params, [evStr], 1, Result) then
  1556. try
  1557. with IInternalFuncParams(Params) do
  1558. begin
  1559. var Context: TMD5Context;
  1560. MD5Init(Context);
  1561. var F := TFile.Create(PrependPath(Ext, Get(0).AsStr), fdOpenExisting, faRead, fsReadWrite);
  1562. try
  1563. while True do begin
  1564. var NumRead := F.Read(Buf, SizeOf(Buf));
  1565. if NumRead = 0 then
  1566. Break;
  1567. MD5Update(Context, Buf, NumRead);
  1568. end;
  1569. finally
  1570. F.Free;
  1571. end;
  1572. MakeStr(ResPtr^, MD5DigestToString(MD5Final(Context)));
  1573. end;
  1574. except
  1575. on E: Exception do
  1576. begin
  1577. FuncResult.Error(PChar(E.Message));
  1578. Result.Error := ISPPFUNC_FAIL
  1579. end;
  1580. end;
  1581. end;
  1582. function GetMD5OfString(Ext: NativeInt; const Params: IIsppFuncParams;
  1583. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1584. begin
  1585. if CheckParams(Params, [evStr], 1, Result) then
  1586. try
  1587. with IInternalFuncParams(Params) do
  1588. begin
  1589. var S := AnsiString(Get(0).AsStr);
  1590. MakeStr(ResPtr^, MD5DigestToString(MD5Buf(Pointer(S)^, ULength(S)*SizeOf(S[1]))));
  1591. end;
  1592. except
  1593. on E: Exception do
  1594. begin
  1595. FuncResult.Error(PChar(E.Message));
  1596. Result.Error := ISPPFUNC_FAIL
  1597. end;
  1598. end;
  1599. end;
  1600. function GetMD5OfUnicodeString(Ext: NativeInt; const Params: IIsppFuncParams;
  1601. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1602. begin
  1603. if CheckParams(Params, [evStr], 1, Result) then
  1604. try
  1605. with IInternalFuncParams(Params) do
  1606. begin
  1607. var S := Get(0).AsStr;
  1608. MakeStr(ResPtr^, MD5DigestToString(MD5Buf(Pointer(S)^, ULength(S)*SizeOf(S[1]))));
  1609. end;
  1610. except
  1611. on E: Exception do
  1612. begin
  1613. FuncResult.Error(PChar(E.Message));
  1614. Result.Error := ISPPFUNC_FAIL
  1615. end;
  1616. end;
  1617. end;
  1618. function GetSHA1OfFile(Ext: NativeInt; const Params: IIsppFuncParams;
  1619. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1620. var
  1621. Buf: array[0..65535] of Byte;
  1622. begin
  1623. if CheckParams(Params, [evStr], 1, Result) then
  1624. try
  1625. with IInternalFuncParams(Params) do
  1626. begin
  1627. var Context: TSHA1Context;
  1628. SHA1Init(Context);
  1629. var F := TFile.Create(PrependPath(Ext, Get(0).AsStr), fdOpenExisting, faRead, fsReadWrite);
  1630. try
  1631. while True do begin
  1632. var NumRead := F.Read(Buf, SizeOf(Buf));
  1633. if NumRead = 0 then
  1634. Break;
  1635. SHA1Update(Context, Buf, NumRead);
  1636. end;
  1637. finally
  1638. F.Free;
  1639. end;
  1640. MakeStr(ResPtr^, SHA1DigestToString(SHA1Final(Context)));
  1641. end;
  1642. except
  1643. on E: Exception do
  1644. begin
  1645. FuncResult.Error(PChar(E.Message));
  1646. Result.Error := ISPPFUNC_FAIL
  1647. end;
  1648. end;
  1649. end;
  1650. function GetSHA1OfString(Ext: NativeInt; const Params: IIsppFuncParams;
  1651. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1652. begin
  1653. if CheckParams(Params, [evStr], 1, Result) then
  1654. try
  1655. with IInternalFuncParams(Params) do
  1656. begin
  1657. var S := AnsiString(Get(0).AsStr);
  1658. MakeStr(ResPtr^, SHA1DigestToString(SHA1Buf(Pointer(S)^, ULength(S)*SizeOf(S[1]))));
  1659. end;
  1660. except
  1661. on E: Exception do
  1662. begin
  1663. FuncResult.Error(PChar(E.Message));
  1664. Result.Error := ISPPFUNC_FAIL
  1665. end;
  1666. end;
  1667. end;
  1668. function GetSHA1OfUnicodeString(Ext: NativeInt; const Params: IIsppFuncParams;
  1669. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1670. begin
  1671. if CheckParams(Params, [evStr], 1, Result) then
  1672. try
  1673. with IInternalFuncParams(Params) do
  1674. begin
  1675. var S := Get(0).AsStr;
  1676. MakeStr(ResPtr^, SHA1DigestToString(SHA1Buf(Pointer(S)^, ULength(S)*SizeOf(S[1]))));
  1677. end;
  1678. except
  1679. on E: Exception do
  1680. begin
  1681. FuncResult.Error(PChar(E.Message));
  1682. Result.Error := ISPPFUNC_FAIL
  1683. end;
  1684. end;
  1685. end;
  1686. function GetSHA256OfFile(Ext: NativeInt; const Params: IIsppFuncParams;
  1687. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1688. var
  1689. Buf: array[0..65535] of Byte;
  1690. begin
  1691. if CheckParams(Params, [evStr], 1, Result) then
  1692. try
  1693. with IInternalFuncParams(Params) do
  1694. begin
  1695. var Context: TSHA256Context;
  1696. SHA256Init(Context);
  1697. var F := TFile.Create(PrependPath(Ext, Get(0).AsStr), fdOpenExisting, faRead, fsReadWrite);
  1698. try
  1699. while True do begin
  1700. var NumRead := F.Read(Buf, SizeOf(Buf));
  1701. if NumRead = 0 then
  1702. Break;
  1703. SHA256Update(Context, Buf, NumRead);
  1704. end;
  1705. finally
  1706. F.Free;
  1707. end;
  1708. MakeStr(ResPtr^, SHA256DigestToString(SHA256Final(Context)));
  1709. end;
  1710. except
  1711. on E: Exception do
  1712. begin
  1713. FuncResult.Error(PChar(E.Message));
  1714. Result.Error := ISPPFUNC_FAIL
  1715. end;
  1716. end;
  1717. end;
  1718. function GetSHA256OfString(Ext: NativeInt; const Params: IIsppFuncParams;
  1719. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1720. begin
  1721. if CheckParams(Params, [evStr], 1, Result) then
  1722. try
  1723. with IInternalFuncParams(Params) do
  1724. begin
  1725. var S := AnsiString(Get(0).AsStr);
  1726. MakeStr(ResPtr^, SHA256DigestToString(SHA256Buf(Pointer(S)^, ULength(S)*SizeOf(S[1]))));
  1727. end;
  1728. except
  1729. on E: Exception do
  1730. begin
  1731. FuncResult.Error(PChar(E.Message));
  1732. Result.Error := ISPPFUNC_FAIL
  1733. end;
  1734. end;
  1735. end;
  1736. function GetSHA256OfUnicodeString(Ext: NativeInt; const Params: IIsppFuncParams;
  1737. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1738. begin
  1739. if CheckParams(Params, [evStr], 1, Result) then
  1740. try
  1741. with IInternalFuncParams(Params) do
  1742. begin
  1743. var S := Get(0).AsStr;
  1744. MakeStr(ResPtr^, SHA256DigestToString(SHA256Buf(Pointer(S)^, ULength(S)*SizeOf(S[1]))));
  1745. end;
  1746. except
  1747. on E: Exception do
  1748. begin
  1749. FuncResult.Error(PChar(E.Message));
  1750. Result.Error := ISPPFUNC_FAIL
  1751. end;
  1752. end;
  1753. end;
  1754. function TrimFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1755. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1756. begin
  1757. if CheckParams(Params, [evStr], 1, Result) then
  1758. try
  1759. with IInternalFuncParams(Params) do
  1760. MakeStr(ResPtr^, Trim(Get(0).AsStr));
  1761. except
  1762. on E: Exception do
  1763. begin
  1764. FuncResult.Error(PChar(E.Message));
  1765. Result.Error := ISPPFUNC_FAIL
  1766. end;
  1767. end;
  1768. end;
  1769. function StringChangeFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1770. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1771. var
  1772. S: String;
  1773. begin
  1774. if CheckParams(Params, [evStr, evStr, evStr], 1, Result) then
  1775. try
  1776. with IInternalFuncParams(Params) do
  1777. begin
  1778. S := Get(0).AsStr;
  1779. StringChangeEx(S, Get(1).AsStr, Get(2).AsStr, True);
  1780. MakeStr(ResPtr^, S);
  1781. end;
  1782. except
  1783. on E: Exception do
  1784. begin
  1785. FuncResult.Error(PChar(E.Message));
  1786. Result.Error := ISPPFUNC_FAIL
  1787. end;
  1788. end;
  1789. end;
  1790. function IsWin64Func(Ext: NativeInt; const Params: IIsppFuncParams;
  1791. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1792. begin
  1793. if CheckParams(Params, [], 0, Result) then
  1794. try
  1795. with IInternalFuncParams(Params) do
  1796. begin
  1797. MakeBool(ResPtr^, IsWin64);
  1798. end;
  1799. except
  1800. on E: Exception do
  1801. begin
  1802. FuncResult.Error(PChar(E.Message));
  1803. Result.Error := ISPPFUNC_FAIL
  1804. end;
  1805. end;
  1806. end;
  1807. function MessageFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1808. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1809. begin
  1810. if CheckParams(Params, [evStr], 1, Result) then
  1811. try
  1812. with IInternalFuncParams(Params) do begin
  1813. { Also see Pragma in IsppPreprocessor }
  1814. TPreprocessor(Ext).StatusMsg(Get(0).AsStr);
  1815. ResPtr^ := NULL;
  1816. end;
  1817. except
  1818. on E: Exception do
  1819. begin
  1820. FuncResult.Error(PChar(E.Message));
  1821. Result.Error := ISPPFUNC_FAIL
  1822. end;
  1823. end;
  1824. end;
  1825. function WarningFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1826. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1827. begin
  1828. if CheckParams(Params, [evStr], 1, Result) then
  1829. try
  1830. with IInternalFuncParams(Params) do begin
  1831. { Also see Pragma in IsppPreprocessor }
  1832. TPreprocessor(Ext).WarningMsg(Get(0).AsStr);
  1833. ResPtr^ := NULL;
  1834. end;
  1835. except
  1836. on E: Exception do
  1837. begin
  1838. FuncResult.Error(PChar(E.Message));
  1839. Result.Error := ISPPFUNC_FAIL
  1840. end;
  1841. end;
  1842. end;
  1843. function ErrorFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1844. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1845. var
  1846. CatchException: Boolean;
  1847. ErrorMsg: String;
  1848. begin
  1849. CatchException := True;
  1850. if CheckParams(Params, [evStr], 1, Result) then
  1851. try
  1852. with IInternalFuncParams(Params) do begin
  1853. { Also see Pragma and pcErrorDir in IsppPreprocessor }
  1854. ErrorMsg := Get(0).AsStr;
  1855. if ErrorMsg = '' then ErrorMsg := 'Error';
  1856. CatchException := False;
  1857. TPreprocessor(Ext).RaiseError(ErrorMsg);
  1858. end;
  1859. except
  1860. on E: Exception do
  1861. begin
  1862. if CatchException then begin
  1863. FuncResult.Error(PChar(E.Message));
  1864. Result.Error := ISPPFUNC_FAIL
  1865. end else
  1866. raise;
  1867. end;
  1868. end;
  1869. end;
  1870. function AddQuotesFunc(Ext: NativeInt; const Params: IIsppFuncParams;
  1871. const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
  1872. begin
  1873. if CheckParams(Params, [evStr], 1, Result) then
  1874. try
  1875. with IInternalFuncParams(Params) do
  1876. begin
  1877. MakeStr(ResPtr^, AddQuotes(Get(0).AsStr));
  1878. end;
  1879. except
  1880. on E: Exception do
  1881. begin
  1882. FuncResult.Error(PChar(E.Message));
  1883. Result.Error := ISPPFUNC_FAIL
  1884. end;
  1885. end;
  1886. end;
  1887. procedure RegisterFunctions(Preproc: TPreprocessor);
  1888. begin
  1889. with Preproc do
  1890. begin
  1891. { -1 as Ext parameter means that function will be called with Ext set to
  1892. preprocessor instance instead of -1. }
  1893. RegisterFunction('Int', Int, -1);
  1894. RegisterFunction('Str', Str, -1);
  1895. RegisterFunction('FileExists', FileExists, -1);
  1896. RegisterFunction('DirExists', DirExists, -1);
  1897. RegisterFunction('ForceDirectories', ForceDirectoriesFunc, -1);
  1898. RegisterFunction('FileSize', FileSize, -1);
  1899. RegisterFunction('ReadIni', ReadIni, -1);
  1900. RegisterFunction('WriteIni', WriteIni, -1);
  1901. RegisterFunction('ReadReg', ReadReg, -1);
  1902. RegisterFunction('Exec', ExecFunc, -1);
  1903. RegisterFunction('ExecAndGetFirstLine', ExecAndGetFirstLineFunc, -1);
  1904. RegisterFunction('Copy', CopyFunc, -1);
  1905. RegisterFunction('Pos', PosFunc, -1);
  1906. RegisterFunction('RPos', RPosFunc, -1);
  1907. RegisterFunction('Len', LenFunc, -1);
  1908. RegisterFunction('GetVersionNumbersString', GetVersionNumbersStringFunc, -1);
  1909. RegisterFunction('ComparePackedVersion', ComparePackedVersionFunc, -1);
  1910. RegisterFunction('SamePackedVersion', SamePackedVersionFunc, -1);
  1911. RegisterFunction('GetStringFileInfo', GetFileVersionInfoItem, -1);
  1912. RegisterFunction('SaveToFile', ISPP.Funcs.SaveToFile, -1);
  1913. RegisterFunction('Find', FindLine, -1);
  1914. RegisterFunction('SetupSetting', SetupSetting, -1);
  1915. RegisterFunction('SetSetupSetting', SetSetupSetting, -1);
  1916. RegisterFunction('LowerCase', LowerCaseFunc, -1);
  1917. RegisterFunction('UpperCase', UpperCaseFunc, -1);
  1918. RegisterFunction('EntryCount', EntryCountFunc, -1);
  1919. RegisterFunction('GetEnv', GetEnvFunc, -1);
  1920. RegisterFunction('DeleteFile', DelFileFunc, -1);
  1921. RegisterFunction('DeleteFileNow', DelFileNowFunc, -1);
  1922. RegisterFunction('CopyFile', CopyFileFunc, -1);
  1923. RegisterFunction('ReadEnv', GetEnvFunc, -1);
  1924. RegisterFunction('FindFirst', FindFirstFunc, -1);
  1925. RegisterFunction('FindNext', FindNextFunc, -1);
  1926. RegisterFunction('FindGetFileName', FindGetFileName, -1);
  1927. RegisterFunction('FindClose', FindCloseFunc, -1);
  1928. RegisterFunction('FileOpen', FileOpenFunc, -1);
  1929. RegisterFunction('FileRead', FileReadFunc, -1);
  1930. RegisterFunction('FileReset', FileResetFunc, -1);
  1931. RegisterFunction('FileEof', FileEofFunc, -1);
  1932. RegisterFunction('FileClose', FileCloseFunc, -1);
  1933. RegisterFunction('SaveStringToFile', SaveStringToFileFunc, -1);
  1934. RegisterFunction('FileGetDateTime', FileGetDate, -1);
  1935. RegisterFunction('Now', GetNow, -1);
  1936. RegisterFunction('DateTimeToDate', GetDateFromDT, -1);
  1937. RegisterFunction('DateTimeToTime', GetTimeFromDT, -1);
  1938. RegisterFunction('GetDateTimeString', GetDateTimeString, -1);
  1939. RegisterFunction('GetFileDateTimeString', GetFileDateTimeString, -1);
  1940. RegisterFunction('GetMD5OfFile', GetMD5OfFile, -1);
  1941. RegisterFunction('GetMD5OfString', GetMD5OfString, -1);
  1942. RegisterFunction('GetMD5OfUnicodeString', GetMD5OfUnicodeString, -1);
  1943. RegisterFunction('GetSHA1OfFile', GetSHA1OfFile, -1);
  1944. RegisterFunction('GetSHA1OfString', GetSHA1OfString, -1);
  1945. RegisterFunction('GetSHA1OfUnicodeString', GetSHA1OfUnicodeString, -1);
  1946. RegisterFunction('GetSHA256OfFile', GetSHA256OfFile, -1);
  1947. RegisterFunction('GetSHA256OfString', GetSHA256OfString, -1);
  1948. RegisterFunction('GetSHA256OfUnicodeString', GetSHA256OfUnicodeString, -1);
  1949. RegisterFunction('Trim', TrimFunc, -1);
  1950. RegisterFunction('StringChange', StringChangeFunc, -1);
  1951. RegisterFunction('IsWin64', IsWin64Func, -1);
  1952. RegisterFunction('Message', MessageFunc, -1);
  1953. RegisterFunction('Warning', WarningFunc, -1);
  1954. RegisterFunction('Error', ErrorFunc, -1);
  1955. RegisterFunction('AddQuotes', AddQuotesFunc, -1)
  1956. end;
  1957. end;
  1958. procedure InitIsWin64;
  1959. var
  1960. IsWow64ProcessFunc: function(hProcess: THandle; var Wow64Process: BOOL): BOOL; stdcall;
  1961. Wow64Process: BOOL;
  1962. begin
  1963. IsWow64ProcessFunc := GetProcAddress(GetModuleHandle(kernel32), 'IsWow64Process');
  1964. IsWin64 := Assigned(IsWow64ProcessFunc) and
  1965. IsWow64ProcessFunc(GetCurrentProcess, Wow64Process) and
  1966. Wow64Process;
  1967. end;
  1968. initialization
  1969. InitIsWin64;
  1970. end.