ISPP.Funcs.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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. 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. procedure InitIsWin64;
  1965. var
  1966. IsWow64ProcessFunc: function(hProcess: THandle; var Wow64Process: BOOL): BOOL; stdcall;
  1967. Wow64Process: BOOL;
  1968. begin
  1969. IsWow64ProcessFunc := GetProcAddress(GetModuleHandle(kernel32), 'IsWow64Process');
  1970. IsWin64 := Assigned(IsWow64ProcessFunc) and
  1971. IsWow64ProcessFunc(GetCurrentProcess, Wow64Process) and
  1972. Wow64Process;
  1973. end;
  1974. initialization
  1975. InitIsWin64;
  1976. end.