ISPP.Funcs.pas 61 KB

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