chmreader.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. { Copyright (C) <2005> <Andrew Haines> chmreader.pas
  2. This library is free software; you can redistribute it and/or modify it
  3. under the terms of the GNU Library General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or (at your
  5. option) any later version.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  9. for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; if not, write to the Free Software Foundation,
  12. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. }
  14. {
  15. See the file COPYING.modifiedLGPL, included in this distribution,
  16. for details about the copyright.
  17. }
  18. unit chmreader;
  19. {$mode objfpc}{$H+}
  20. //{$DEFINE CHM_DEBUG}
  21. { $DEFINE CHM_DEBUG_CHUNKS}
  22. interface
  23. uses
  24. Classes, SysUtils, chmbase, paslzx;
  25. type
  26. TLZXResetTableArr = array of QWord;
  27. PContextItem = ^TContextItem;
  28. TContextItem = record
  29. Context: THelpContext;
  30. Url: String;
  31. end;
  32. TContextList = class(TList)
  33. public
  34. procedure AddContext(Context: THelpContext; Url: String);
  35. function GetURL(Context: THelpContext): String;
  36. procedure Clear; override;
  37. end;
  38. { TITSFReader }
  39. TFileEntryForEach = procedure(Name: String; Offset, UncompressedSize, Section: Integer) of object;
  40. TITSFReader = class(TObject)
  41. protected
  42. fStream: TStream;
  43. fFreeStreamOnDestroy: Boolean;
  44. fChmHeader: TITSFHeader;
  45. fHeaderSuffix: TITSFHeaderSuffix;
  46. fDirectoryHeader: TITSPHeader;
  47. fDirectoryHeaderPos: Int64;
  48. fDirectoryHeaderLength: QWord;
  49. fDirectoryEntriesStartPos: Int64;
  50. fDirectoryEntries: array of TPMGListChunkEntry;
  51. fCachedEntry: TPMGListChunkEntry; //contains the last entry found by ObjectExists
  52. fDirectoryEntriesCount: LongWord;
  53. private
  54. procedure ReadHeader;
  55. function GetChunkType(Stream: TMemoryStream; ChunkIndex: LongInt): TPMGchunktype;
  56. function GetDirectoryChunk(Index: Integer; OutStream: TStream): Integer;
  57. function ReadPMGLchunkEntryFromStream(Stream: TMemoryStream; var PMGLEntry: TPMGListChunkEntry): Boolean;
  58. function ReadPMGIchunkEntryFromStream(Stream: TMemoryStream; var PMGIEntry: TPMGIIndexChunkEntry): Boolean;
  59. procedure LookupPMGLchunk(Stream: TMemoryStream; out PMGLChunk: TPMGListChunk);
  60. procedure LookupPMGIchunk(Stream: TMemoryStream; out PMGIChunk: TPMGIIndexChunk);
  61. procedure GetSections(out Sections: TStringList);
  62. function GetBlockFromSection(SectionPrefix: String; StartPos: QWord; BlockLength: QWord): TMemoryStream;
  63. function FindBlocksFromUnCompressedAddr(var ResetTableEntry: TPMGListChunkEntry;
  64. out CompressedSize: Int64; out UnCompressedSize: Int64; out LZXResetTable: TLZXResetTableArr): QWord; // Returns the blocksize
  65. public
  66. constructor Create(AStream: TStream; FreeStreamOnDestroy: Boolean); virtual;
  67. destructor Destroy; override;
  68. public
  69. ChmLastError: LongInt;
  70. function IsValidFile: Boolean;
  71. procedure GetCompleteFileList(ForEach: TFileEntryForEach);
  72. function ObjectExists(Name: String): QWord; // zero if no. otherwise it is the size of the object
  73. // NOTE directories will return zero size even if they exist
  74. function GetObject(Name: String): TMemoryStream; // YOU must Free the stream
  75. property CachedEntry: TPMGListChunkEntry read fCachedEntry;
  76. end;
  77. { TChmReader }
  78. TChmReader = class(TITSFReader)
  79. protected
  80. fDefaultPage: String;
  81. fIndexFile: String;
  82. fTOCFile: String;
  83. fTitle: String;
  84. fPreferedFont: String;
  85. fContextList: TContextList;
  86. fLocaleID: DWord;
  87. private
  88. procedure ReadCommonData;
  89. public
  90. constructor Create(AStream: TStream; FreeStreamOnDestroy: Boolean); override;
  91. destructor Destroy; override;
  92. public
  93. function GetContextUrl(Context: THelpContext): String;
  94. function HasContextList: Boolean;
  95. property DefaultPage: String read fDefaultPage;
  96. property IndexFile: String read fIndexFile;
  97. property TOCFile: String read fTOCFile;
  98. property Title: String read fTitle write fTitle;
  99. property PreferedFont: String read fPreferedFont;
  100. property LocaleID: dword read fLocaleID;
  101. end;
  102. { TChmFileList }
  103. TChmFileList = class;
  104. TChmFileOpenEvent = procedure(ChmFileList: TChmFileList; Index: Integer) of object;
  105. TChmFileList = class(TStringList)
  106. protected
  107. fLastChm: TChmReader;
  108. fUnNotifiedFiles: TList;
  109. fOnOpenNewFile: TChmFileOpenEvent;
  110. procedure Delete(Index: Integer); override;
  111. function GetChm(AIndex: Integer): TChmReader;
  112. function GetFileName(AIndex: Integer): String;
  113. procedure OpenNewFile(AFileName: String);
  114. function CheckOpenFile(AFileName: String): Boolean;
  115. function MetaObjectExists(var Name: String): QWord;
  116. function MetaGetObject(Name: String): TMemoryStream;
  117. procedure SetOnOpenNewFile(AValue: TChmFileOpenEvent);
  118. public
  119. constructor Create(PrimaryFileName: String);
  120. destructor Destroy; override;
  121. function GetObject(Name: String): TMemoryStream;
  122. function IsAnOpenFile(AFileName: String): Boolean;
  123. function ObjectExists(Name: String; fChm: TChmReader = nil): QWord;
  124. //properties
  125. property Chm[Index: Integer]: TChmReader read GetChm;
  126. property FileName[Index: Integer]: String read GetFileName;
  127. property OnOpenNewFile: TChmFileOpenEvent read fOnOpenNewFile write SetOnOpenNewFile;
  128. end;
  129. //ErrorCodes
  130. const
  131. ERR_NO_ERR = 0;
  132. ERR_STREAM_NOT_ASSIGNED = 1;
  133. ERR_NOT_SUPPORTED_VERSION = 2;
  134. ERR_NOT_VALID_FILE = 3;
  135. ERR_UNKNOWN_ERROR = 10;
  136. function ChmErrorToStr(Error: Integer): String;
  137. implementation
  138. function ChmErrorToStr(Error: Integer): String;
  139. begin
  140. Result := '';
  141. case Error of
  142. ERR_STREAM_NOT_ASSIGNED : Result := 'ERR_STREAM_NOT_ASSIGNED';
  143. ERR_NOT_SUPPORTED_VERSION : Result := 'ERR_NOT_SUPPORTED_VERSION';
  144. ERR_NOT_VALID_FILE : Result := 'ERR_NOT_VALID_FILE';
  145. ERR_UNKNOWN_ERROR : Result := 'ERR_UNKNOWN_ERROR';
  146. end;
  147. end;
  148. function ChunkType(Stream: TMemoryStream): TPMGchunktype;
  149. var
  150. ChunkID: array[0..3] of char;
  151. begin
  152. Result := ctUnknown;
  153. if Stream.Size< 4 then exit;
  154. Move(Stream.Memory^, ChunkId[0], 4);
  155. if ChunkID = 'PMGL' then Result := ctPMGL
  156. else if ChunkID = 'PMGI' then Result := ctPMGI;
  157. end;
  158. { TITSFReader }
  159. procedure TITSFReader.ReadHeader;
  160. var
  161. fHeaderEntries: array [0..1] of TITSFHeaderEntry;
  162. begin
  163. fStream.Position := 0;
  164. fStream.Read(fChmHeader,SizeOf(fChmHeader));
  165. // Fix endian issues
  166. {$IFDEF ENDIAN_BIG}
  167. fChmHeader.Version := LEtoN(fChmHeader.Version);
  168. fChmHeader.HeaderLength := LEtoN(fChmHeader.HeaderLength);
  169. //Unknown_1
  170. fChmHeader.TimeStamp := BEtoN(fChmHeader.TimeStamp);//bigendian
  171. fChmHeader.LanguageID := LEtoN(fChmHeader.LanguageID);
  172. //Guid1
  173. //Guid2
  174. {$ENDIF}
  175. if not IsValidFile then Exit;
  176. // Copy EntryData into memory
  177. fStream.Read(fHeaderEntries[0], SizeOf(fHeaderEntries));
  178. if fChmHeader.Version > 2 then
  179. fStream.Read(fHeaderSuffix.Offset, SizeOf(QWord));
  180. fHeaderSuffix.Offset := LEtoN(fHeaderSuffix.Offset);
  181. // otherwise this is set in fill directory entries
  182. fStream.Position := LEtoN(fHeaderEntries[1].PosFromZero);
  183. fDirectoryHeaderPos := LEtoN(fHeaderEntries[1].PosFromZero);
  184. fStream.Read(fDirectoryHeader, SizeOf(fDirectoryHeader));
  185. {$IFDEF ENDIAN_BIG}
  186. with fDirectoryHeader do begin
  187. Version := LEtoN(Version);
  188. DirHeaderLength := LEtoN(DirHeaderLength);
  189. //Unknown1
  190. ChunkSize := LEtoN(ChunkSize);
  191. Density := LEtoN(Density);
  192. IndexTreeDepth := LEtoN(IndexTreeDepth);
  193. IndexOfRootChunk := LEtoN(IndexOfRootChunk);
  194. FirstPMGLChunkIndex := LEtoN(FirstPMGLChunkIndex);
  195. LastPMGLChunkIndex := LEtoN(LastPMGLChunkIndex);
  196. //Unknown2
  197. DirectoryChunkCount := LEtoN(DirectoryChunkCount);
  198. LanguageID := LEtoN(LanguageID);
  199. //GUID: TGuid;
  200. LengthAgain := LEtoN(LengthAgain);
  201. end;
  202. {$ENDIF}
  203. {$IFDEF CHM_DEBUG}
  204. WriteLn('PMGI depth = ', fDirectoryHeader.IndexTreeDepth);
  205. WriteLn('PMGI Root = ', fDirectoryHeader.IndexOfRootChunk);
  206. {$ENDIF}
  207. fDirectoryEntriesStartPos := fStream.Position;
  208. fDirectoryHeaderLength := LEtoN(fHeaderEntries[1].Length);
  209. end;
  210. procedure TChmReader.ReadCommonData;
  211. // A little helper proc to make reading a null terminated string easier
  212. function ReadString(const Stream: TStream): String;
  213. var
  214. buf: array[0..49] of char;
  215. begin
  216. Result := '';
  217. repeat
  218. Stream.Read(buf, 50);
  219. Result := Result + buf;
  220. until Pos(#0, buf) > -1;
  221. end;
  222. procedure ReadFromSystem;
  223. var
  224. //Version: DWord;
  225. EntryType: Word;
  226. EntryLength: Word;
  227. Data: array[0..511] of char;
  228. fSystem: TMemoryStream;
  229. Tmp: String;
  230. begin
  231. fSystem := TMemoryStream(GetObject('/#SYSTEM'));
  232. if fSystem = nil then begin
  233. exit;
  234. end;
  235. fSystem.Position := 0;
  236. if fSystem.Size < SizeOf(DWord) then begin
  237. fSystem.Free;
  238. Exit;
  239. end;
  240. {Version := }LEtoN(fSystem.ReadDWord);
  241. while fSystem.Position < fSystem.Size do begin
  242. EntryType := LEtoN(fSystem.ReadWord);
  243. EntryLength := LEtoN(fSystem.ReadWord);
  244. case EntryType of
  245. 0: // Table of contents
  246. begin
  247. if EntryLength > 511 then EntryLength := 511;
  248. fSystem.Read(Data[0], EntryLength);
  249. Data[EntryLength] := #0;
  250. fTOCFile := '/'+Data;
  251. end;
  252. 1: // Index File
  253. begin
  254. if EntryLength > 511 then EntryLength := 511;
  255. fSystem.Read(Data[0], EntryLength);
  256. Data[EntryLength] := #0;
  257. fIndexFile := '/'+Data;
  258. end;
  259. 2: // DefaultPage
  260. begin
  261. if EntryLength > 511 then EntryLength := 511;
  262. fSystem.Read(Data[0], EntryLength);
  263. Data[EntryLength] := #0;
  264. fDefaultPage := '/'+Data;
  265. end;
  266. 3: // Title of chm
  267. begin
  268. if EntryLength > 511 then EntryLength := 511;
  269. fSystem.Read(Data[0], EntryLength);
  270. Data[EntryLength] := #0;
  271. fTitle := Data;
  272. end;
  273. 4: // Locale ID
  274. begin
  275. fLocaleID := LEtoN(fSystem.ReadDWord);
  276. fSystem.Position := (fSystem.Position + EntryLength) - SizeOf(DWord);
  277. end;
  278. 6: // chm file name. use this to get the index and toc name
  279. begin
  280. if EntryLength > 511 then EntryLength := 511;
  281. fSystem.Read(Data[0], EntryLength);
  282. Data[EntryLength] := #0;
  283. if (fIndexFile = '') then begin
  284. Tmp := '/'+Data+'.hhk';
  285. if (ObjectExists(Tmp) > 0) then begin
  286. fIndexFile := Tmp;
  287. end
  288. end;
  289. if (fTOCFile = '') then begin
  290. Tmp := '/'+Data+'.hhc';
  291. if (ObjectExists(Tmp) > 0) then begin
  292. fTOCFile := Tmp;
  293. end;
  294. end;
  295. end;
  296. 16: // Prefered font
  297. begin
  298. if EntryLength > 511 then EntryLength := 511;
  299. fSystem.Read(Data[0], EntryLength);
  300. Data[EntryLength] := #0;
  301. fPreferedFont := Data;
  302. end;
  303. else
  304. // Skip entries we are not interested in
  305. fSystem.Position := fSystem.Position + EntryLength;
  306. end;
  307. end;
  308. fSystem.Free;
  309. end;
  310. procedure ReadFromWindows;
  311. var
  312. fWindows,
  313. fStrings: TMemoryStream;
  314. EntryCount,
  315. EntrySize: DWord;
  316. EntryStart: Int64;
  317. StrPosition: DWord;
  318. X: Integer;
  319. OffSet: Int64;
  320. begin
  321. fWindows := TMemoryStream(GetObject('/#WINDOWS'));
  322. if fWindows = nil then begin
  323. exit;
  324. end;
  325. fStrings := TMemoryStream(GetObject('/#STRINGS'));
  326. if fStrings = nil then begin
  327. if fWindows <> nil then fWindows.Free;
  328. Exit;
  329. end;
  330. fWindows.Position := 0;
  331. if (fWindows.Size = 0) or (fStrings.Size = 0) then begin
  332. fWindows.Free;
  333. fStrings.Free;
  334. Exit;
  335. end;
  336. EntryCount := LEtoN(fWindows.ReadDWord);
  337. EntrySize := LEtoN(fWindows.ReadDWord);
  338. OffSet := fWindows.Position;
  339. for X := 0 to EntryCount -1 do begin
  340. EntryStart := OffSet + (X*EntrySize);
  341. if fTitle = '' then begin
  342. fWindows.Position := EntryStart + $14;
  343. StrPosition := LEtoN(fWindows.ReadDWord);
  344. fStrings.Position := StrPosition;
  345. fTitle := '/'+ReadString(fStrings);
  346. end;
  347. if fTOCFile = '' then begin
  348. fWindows.Position := EntryStart + $60;
  349. StrPosition := LEtoN(fWindows.ReadDWord);
  350. fStrings.Position := StrPosition;
  351. fTOCFile := '/'+ReadString(fStrings);
  352. end;
  353. if fIndexFile = '' then begin
  354. fWindows.Position := EntryStart + $64;
  355. StrPosition := LEtoN(fWindows.ReadDWord);
  356. fStrings.Position := StrPosition;
  357. fIndexFile := '/'+ReadString(fStrings);
  358. end;
  359. if fDefaultPage = '' then begin
  360. fWindows.Position := EntryStart + $68;
  361. StrPosition := LEtoN(fWindows.ReadDWord);
  362. fStrings.Position := StrPosition;
  363. fDefaultPage := '/'+ReadString(fStrings);
  364. end;
  365. end;
  366. end;
  367. procedure ReadContextIds;
  368. var
  369. fIVB,
  370. fStrings: TStream;
  371. Str: String;
  372. Value: DWord;
  373. OffSet: DWord;
  374. //TotalSize: DWord;
  375. begin
  376. fIVB := GetObject('/#IBV');
  377. if fIVB = nil then Exit;
  378. fStrings := GetObject('/#STRINGS');
  379. if fStrings = nil then begin
  380. fIVB.Free;
  381. Exit;
  382. end;
  383. fIVB.Position := 0;
  384. {TotalSize := }LEtoN(fIVB.ReadDWord);
  385. while fIVB.Position < fIVB.Size do begin
  386. Value := LEtoN(fIVB.ReadDWord);
  387. OffSet := LEtoN(fIVB.ReadDWord);
  388. fStrings.Position := Offset;
  389. Str := '/'+ReadString(fStrings);
  390. fContextList.AddContext(Value, Str);
  391. end;
  392. end;
  393. begin
  394. ReadFromSystem;
  395. ReadFromWindows;
  396. ReadContextIds;
  397. {$IFDEF CHM_DEBUG}
  398. WriteLn('TOC=',fTocfile);
  399. WriteLn('DefaultPage=',fDefaultPage);
  400. {$ENDIF}
  401. end;
  402. constructor TChmReader.Create(AStream: TStream; FreeStreamOnDestroy: Boolean);
  403. begin
  404. inherited Create(AStream, FreeStreamOnDestroy);
  405. if not IsValidFile then exit;
  406. fContextList := TContextList.Create;
  407. ReadCommonData;
  408. end;
  409. destructor TChmReader.Destroy;
  410. begin
  411. fContextList.Free;
  412. inherited Destroy;
  413. end;
  414. function TITSFReader.GetChunkType(Stream: TMemoryStream; ChunkIndex: LongInt): TPMGchunktype;
  415. var
  416. Sig: array[0..3] of char;
  417. begin
  418. Result := ctUnknown;
  419. Stream.Position := fDirectoryEntriesStartPos + (fDirectoryHeader.ChunkSize * ChunkIndex);
  420. Stream.Read(Sig, 4);
  421. if Sig = 'PMGL' then Result := ctPMGL
  422. else if Sig = 'PMGI' then Result := ctPMGI;
  423. end;
  424. function TITSFReader.GetDirectoryChunk(Index: Integer; OutStream: TStream): Integer;
  425. begin
  426. Result := Index;
  427. fStream.Position := fDirectoryEntriesStartPos + (fDirectoryHeader.ChunkSize * Index);
  428. OutStream.Position := 0;
  429. OutStream.Size := fDirectoryHeader.ChunkSize;
  430. OutStream.CopyFrom(fStream, fDirectoryHeader.ChunkSize);
  431. OutStream.Position := 0;
  432. end;
  433. procedure TITSFReader.LookupPMGLchunk(Stream: TMemoryStream; out PMGLChunk: TPMGListChunk);
  434. begin
  435. //Stream.Position := fDirectoryEntriesStartPos + (fDirectoryHeader.ChunkSize * ChunkIndex);
  436. Stream.Read(PMGLChunk, SizeOf(PMGLChunk));
  437. {$IFDEF ENDIAN_BIG}
  438. with PMGLChunk do begin
  439. UnusedSpace := LEtoN(UnusedSpace);
  440. //Unknown1
  441. PreviousChunkIndex := LEtoN(PreviousChunkIndex);
  442. NextChunkIndex := LEtoN(NextChunkIndex);
  443. end;
  444. {$ENDIF}
  445. end;
  446. function TITSFReader.ReadPMGLchunkEntryFromStream(Stream: TMemoryStream; var PMGLEntry: TPMGListChunkEntry): Boolean;
  447. var
  448. Buf: array [0..1023] of char;
  449. NameLength: LongInt;
  450. begin
  451. Result := False;
  452. //Stream.Position := fDirectoryEntriesStartPos + (fDirectoryHeader.ChunkSize * ChunkIndex);
  453. NameLength := LongInt(GetCompressedInteger(Stream));
  454. if NameLength > 1022 then NameLength := 1022;
  455. Stream.Read(buf[0], NameLength);
  456. buf[NameLength] := #0;
  457. PMGLEntry.Name := buf;
  458. PMGLEntry.ContentSection := LongWord(GetCompressedInteger(Stream));
  459. PMGLEntry.ContentOffset := GetCompressedInteger(Stream);
  460. PMGLEntry.DecompressedLength := GetCompressedInteger(Stream);
  461. if NameLength = 0 then Exit; // failed GetCompressedInteger sanity check
  462. Result := True;
  463. end;
  464. procedure TITSFReader.LookupPMGIchunk(Stream: TMemoryStream; out PMGIChunk: TPMGIIndexChunk);
  465. begin
  466. //Stream.Position := fDirectoryEntriesStartPos + (fDirectoryHeader.ChunkSize * ChunkIndex);
  467. Stream.Read(PMGIChunk, SizeOf(PMGIChunk));
  468. {$IFDEF ENDIAN_BIG}
  469. with PMGIChunk do begin
  470. UnusedSpace := LEtoN(UnusedSpace);
  471. end;
  472. {$ENDIF}
  473. end;
  474. function TITSFReader.ReadPMGIchunkEntryFromStream(Stream: TMemoryStream;
  475. var PMGIEntry: TPMGIIndexChunkEntry): Boolean;
  476. var
  477. Buf: array [0..1023] of char;
  478. NameLength: LongInt;
  479. begin
  480. Result := False;
  481. //Stream.Position := fDirectoryEntriesStartPos + (fDirectoryHeader.ChunkSize * ChunkIndex);
  482. NameLength := LongInt(GetCompressedInteger(Stream));
  483. if NameLength > 1023 then NameLength := 1023;
  484. Stream.Read(buf, NameLength);
  485. buf[NameLength] := #0;
  486. PMGIEntry.Name := buf;
  487. PMGIEntry.ListingChunk := GetCompressedInteger(Stream);
  488. if NameLength = 0 then Exit; // failed GetCompressedInteger sanity check
  489. Result := True;
  490. end;
  491. constructor TITSFReader.Create(AStream: TStream; FreeStreamOnDestroy: Boolean);
  492. begin
  493. fStream := AStream;
  494. fFreeStreamOnDestroy := FreeStreamOnDestroy;
  495. ReadHeader;
  496. if not IsValidFile then Exit;
  497. end;
  498. destructor TITSFReader.Destroy;
  499. begin
  500. SetLength(fDirectoryEntries, 0);
  501. if fFreeStreamOnDestroy then FreeAndNil(fStream);
  502. inherited Destroy;
  503. end;
  504. function TITSFReader.IsValidFile: Boolean;
  505. begin
  506. if (fStream = nil) then ChmLastError := ERR_STREAM_NOT_ASSIGNED
  507. else if (fChmHeader.ITSFsig <> 'ITSF') then ChmLastError := ERR_NOT_VALID_FILE
  508. else if (fChmHeader.Version <> 2) and (fChmHeader.Version <> 3) then
  509. ChmLastError := ERR_NOT_SUPPORTED_VERSION;
  510. Result := ChmLastError = ERR_NO_ERR;
  511. end;
  512. procedure TITSFReader.GetCompleteFileList(ForEach: TFileEntryForEach);
  513. var
  514. ChunkStream: TMemoryStream;
  515. I : Integer;
  516. Entry: TPMGListChunkEntry;
  517. PMGLChunk: TPMGListChunk;
  518. CutOffPoint: Integer;
  519. NameLength: Integer;
  520. {$IFDEF CHM_DEBUG_CHUNKS}
  521. PMGIChunk: TPMGIIndexChunk;
  522. PMGIndex: Integer;
  523. {$ENDIF}
  524. begin
  525. if ForEach = nil then Exit;
  526. ChunkStream := TMemoryStream.Create;
  527. {$IFDEF CHM_DEBUG_CHUNKS}
  528. WriteLn('ChunkCount = ',fDirectoryHeader.DirectoryChunkCount);
  529. {$ENDIF}
  530. for I := 0 to fDirectoryHeader.DirectoryChunkCount-1 do begin
  531. GetDirectoryChunk(I, ChunkStream);
  532. case ChunkType(ChunkStream) of
  533. ctPMGL:
  534. begin
  535. LookupPMGLchunk(ChunkStream, PMGLChunk);
  536. {$IFDEF CHM_DEBUG_CHUNKS}
  537. WriteLn('PMGL: ', I, ' Prev PMGL: ', PMGLChunk.PreviousChunkIndex, ' Next PMGL: ', PMGLChunk.NextChunkIndex);
  538. {$ENDIF}
  539. CutOffPoint := ChunkStream.Size - PMGLChunk.UnusedSpace;
  540. while ChunkStream.Position < CutOffPoint do begin
  541. NameLength := GetCompressedInteger(ChunkStream);
  542. if (ChunkStream.Position > CutOffPoint) then Continue; // we have entered the quickref section
  543. SetLength(Entry.Name, NameLength);
  544. ChunkStream.ReadBuffer(Entry.Name[1], NameLength);
  545. if (Entry.Name = '') or (ChunkStream.Position > CutOffPoint) then Break; // we have entered the quickref section
  546. Entry.ContentSection := GetCompressedInteger(ChunkStream);
  547. if ChunkStream.Position > CutOffPoint then Break; // we have entered the quickref section
  548. Entry.ContentOffset := GetCompressedInteger(ChunkStream);
  549. if ChunkStream.Position > CutOffPoint then Break; // we have entered the quickref section
  550. Entry.DecompressedLength := GetCompressedInteger(ChunkStream);
  551. if ChunkStream.Position > CutOffPoint then Break; // we have entered the quickref section
  552. fCachedEntry := Entry; // if the caller trys to get this data we already know where it is :)
  553. ForEach(Entry.Name, Entry.ContentOffset, Entry.DecompressedLength, Entry.ContentSection);
  554. end;
  555. end;
  556. {$IFDEF CHM_DEBUG_CHUNKS}
  557. ctPMGI:
  558. begin
  559. WriteLn('PMGI: ', I);
  560. LookupPMGIchunk(ChunkStream, PMGIChunk);
  561. CutOffPoint := ChunkStream.Size - PMGIChunk.UnusedSpace - 10;
  562. while ChunkStream.Position < CutOffPoint do begin
  563. NameLength := GetCompressedInteger(ChunkStream);
  564. SetLength(Entry.Name, NameLength);
  565. ChunkStream.ReadBuffer(Entry.Name[1], NameLength);
  566. PMGIndex := GetCompressedInteger(ChunkStream);
  567. WriteLn(Entry.Name, ' ', PMGIndex);
  568. end;
  569. end;
  570. ctUnknown: WriteLn('UNKNOWN CHUNKTYPE!' , I);
  571. {$ENDIF}
  572. end;
  573. end;
  574. end;
  575. function TITSFReader.ObjectExists(Name: String): QWord;
  576. var
  577. ChunkStream: TMemoryStream;
  578. QuickRefCount: Word;
  579. QuickRefIndex: array of Word;
  580. ItemCount: Integer;
  581. procedure ReadQuickRefSection;
  582. var
  583. OldPosn: Int64;
  584. Posn: Integer;
  585. I: Integer;
  586. begin
  587. OldPosn := ChunkStream.Position;
  588. Posn := ChunkStream.Size-1-SizeOf(Word);
  589. ChunkStream.Position := Posn;
  590. ItemCount := LEToN(ChunkStream.ReadWord);
  591. //WriteLn('Max ITems for next block = ', ItemCount-1);
  592. QuickRefCount := ItemCount div (1 + (1 shl fDirectoryHeader.Density));
  593. //WriteLn('QuickRefCount = ' , QuickRefCount);
  594. SetLength(QuickRefIndex, QuickRefCount+1);
  595. for I := 1 to QuickRefCount do begin
  596. Dec(Posn, SizeOf(Word));
  597. ChunkStream.Position := Posn;
  598. QuickRefIndex[I] := LEToN(ChunkStream.ReadWord);
  599. end;
  600. Inc(QuickRefCount);
  601. ChunkStream.Position := OldPosn;
  602. end;
  603. function ReadString(StreamPosition: Integer = -1): String;
  604. var
  605. NameLength: Integer;
  606. begin
  607. if StreamPosition > -1 then ChunkStream.Position := StreamPosition;
  608. NameLength := GetCompressedInteger(ChunkStream);
  609. SetLength(Result, NameLength);
  610. ChunkStream.Read(Result[1], NameLength);
  611. end;
  612. var
  613. PMGLChunk: TPMGListChunk;
  614. PMGIChunk: TPMGIIndexChunk;
  615. //ChunkStream: TMemoryStream; declared above
  616. Entry: TPMGListChunkEntry;
  617. NextIndex: Integer;
  618. EntryName: String;
  619. CRes: Integer;
  620. I: Integer;
  621. begin
  622. Result := 0;
  623. if Name = '' then Exit;
  624. if fDirectoryHeader.DirectoryChunkCount = 0 then exit;
  625. //WriteLn('Looking for ', Name);
  626. if Name = fCachedEntry.Name then
  627. Exit(fCachedEntry.DecompressedLength); // we've already looked it up
  628. ChunkStream := TMemoryStream.Create;
  629. try
  630. NextIndex := fDirectoryHeader.IndexOfRootChunk;
  631. if NextIndex < 0 then NextIndex := 0; // no PMGI chunks
  632. while NextIndex > -1 do begin
  633. GetDirectoryChunk(NextIndex, ChunkStream);
  634. NextIndex := -1;
  635. ReadQuickRefSection;
  636. //WriteLn('In Block ', ChunkIndex);
  637. case ChunkType(ChunkStream) of
  638. ctUnknown: // something is wrong
  639. begin
  640. {$IFDEF CHM_DEBUG}WriteLn(ChunkIndex, ' << Unknown BlockType!');{$ENDIF}
  641. Break;
  642. end;
  643. ctPMGI: // we must follow the PMGI tree until we reach a PMGL block
  644. begin
  645. LookupPMGIchunk(ChunkStream, PMGIChunk);
  646. //QuickRefIndex[0] := ChunkStream.Position;
  647. I := 0;
  648. while ChunkStream.Position <= ChunkStream.Size - PMGIChunk.UnusedSpace do begin;
  649. EntryName := ReadString;
  650. if EntryName = '' then break;
  651. if ChunkStream.Position >= ChunkStream.Size - PMGIChunk.UnusedSpace then break;
  652. CRes := ChmCompareText(Name, EntryName);
  653. if CRes = 0 then begin
  654. // no more need of this block. onto the next!
  655. NextIndex := GetCompressedInteger(ChunkStream);
  656. Break;
  657. end;
  658. if CRes < 0 then begin
  659. if I = 0 then Break; // File doesn't exist
  660. // file is in previous entry
  661. Break;
  662. end;
  663. NextIndex := GetCompressedInteger(ChunkStream);
  664. Inc(I);
  665. end;
  666. end;
  667. ctPMGL:
  668. begin
  669. LookupPMGLchunk(ChunkStream, PMGLChunk);
  670. QuickRefIndex[0] := ChunkStream.Position;
  671. I := 0;
  672. while ChunkStream.Position <= ChunkStream.Size - PMGLChunk.UnusedSpace do begin
  673. // we consume the entry by reading it
  674. Entry.Name := ReadString;
  675. if Entry.Name = '' then break;
  676. if ChunkStream.Position >= ChunkStream.Size - PMGLChunk.UnusedSpace then break;
  677. Entry.ContentSection := GetCompressedInteger(ChunkStream);
  678. Entry.ContentOffset := GetCompressedInteger(ChunkStream);
  679. Entry.DecompressedLength := GetCompressedInteger(ChunkStream);
  680. CRes := ChmCompareText(Name, Entry.Name);
  681. if CRes = 0 then begin
  682. fCachedEntry := Entry;
  683. Result := Entry.DecompressedLength;
  684. Break;
  685. end;
  686. Inc(I);
  687. end;
  688. end; // case
  689. end;
  690. end;
  691. finally
  692. ChunkStream.Free;
  693. end;
  694. end;
  695. function TITSFReader.GetObject(Name: String): TMemoryStream;
  696. var
  697. SectionNames: TStringList;
  698. Entry: TPMGListChunkEntry;
  699. SectionName: String;
  700. begin
  701. Result := nil;
  702. if ObjectExists(Name) = 0 then begin
  703. //WriteLn('Object ', name,' Doesn''t exist or is zero sized.');
  704. Exit;
  705. end;
  706. Entry := fCachedEntry;
  707. if Entry.ContentSection = 0 then begin
  708. Result := TMemoryStream.Create;
  709. fStream.Position := fHeaderSuffix.Offset+ Entry.ContentOffset;
  710. Result.CopyFrom(fStream, fCachedEntry.DecompressedLength);
  711. end
  712. else begin // we have to get it from ::DataSpace/Storage/[MSCompressed,Uncompressed]/ControlData
  713. GetSections(SectionNames);
  714. FmtStr(SectionName, '::DataSpace/Storage/%s/',[SectionNames[Entry.ContentSection-1]]);
  715. Result := GetBlockFromSection(SectionName, Entry.ContentOffset, Entry.DecompressedLength);
  716. SectionNames.Free;
  717. end;
  718. if Result <> nil then Result.Position := 0;
  719. end;
  720. function TChmReader.GetContextUrl(Context: THelpContext): String;
  721. begin
  722. // will get '' if context not found
  723. Result := fContextList.GetURL(Context);
  724. end;
  725. function TChmReader.HasContextList: Boolean;
  726. begin
  727. Result := fContextList.Count > 0;
  728. end;
  729. procedure TITSFReader.GetSections(out Sections: TStringList);
  730. var
  731. Stream: TStream;
  732. EntryCount: Word;
  733. X: Integer;
  734. {$IFDEF ENDIAN_BIG}
  735. I: Integer;
  736. {$ENDIF}
  737. WString: array [0..31] of WideChar;
  738. StrLength: Word;
  739. begin
  740. Sections := TStringList.Create;
  741. //WriteLn('::DataSpace/NameList Size = ', ObjectExists('::DataSpace/NameList'));
  742. Stream := GetObject('::DataSpace/NameList');
  743. if Stream = nil then begin
  744. //WriteLn('Failed to get ::DataSpace/NameList!');
  745. exit;
  746. end;
  747. Stream.Position := 2;
  748. EntryCount := LEtoN(Stream.ReadWord);
  749. for X := 0 to EntryCount -1 do begin
  750. StrLength := LEtoN(Stream.ReadWord);
  751. if StrLength > 31 then StrLength := 31;
  752. Stream.Read(WString, SizeOf(WideChar)*(StrLength+1)); // the strings are stored null terminated
  753. {$IFDEF ENDIAN_BIG}
  754. for I := 0 to StrLength-1 do
  755. WString[I] := WideChar(LEtoN(Ord(WString[I])));
  756. {$ENDIF}
  757. Sections.Add(WString);
  758. end;
  759. // the sections are sorted alphabetically, this way section indexes will jive
  760. Sections.Sort;
  761. Stream.Free;
  762. end;
  763. function TITSFReader.GetBlockFromSection(SectionPrefix: String; StartPos: QWord;
  764. BlockLength: QWord): TMemoryStream;
  765. var
  766. Compressed: Boolean;
  767. Sig: Array [0..3] of char;
  768. CompressionVersion: LongWord;
  769. CompressedSize: Int64;
  770. UnCompressedSize: Int64;
  771. //LZXResetInterval: LongWord;
  772. //LZXWindowSize: LongWord;
  773. //LZXCacheSize: LongWord;
  774. ResetTableEntry: TPMGListChunkEntry;
  775. ResetTable: TLZXResetTableArr;
  776. WriteCount: QWord;
  777. BlockWriteLength: QWord;
  778. WriteStart: LongWord;
  779. ReadCount:LongInt;
  780. LZXState: PLZXState;
  781. InBuf: array of Byte;
  782. OutBuf: PByte;
  783. BlockSize: QWord;
  784. X: Integer;
  785. FirstBlock, LastBlock: LongInt;
  786. ResultCode: LongInt;
  787. procedure ReadBlock;
  788. begin
  789. if ReadCount > Length(InBuf) then
  790. SetLength(InBuf, ReadCount);
  791. fStream.Read(InBuf[0], ReadCount);
  792. end;
  793. begin
  794. // okay now the fun stuff ;)
  795. Result := nil;
  796. Compressed := ObjectExists(SectionPrefix+'Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable')>0;
  797. // the easy method
  798. if Not(Compressed) then begin
  799. if ObjectExists(SectionPrefix+'Content') > 0 then begin
  800. Result := TMemoryStream.Create;
  801. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + StartPos;
  802. Result.CopyFrom(fStream, BlockLength);
  803. end;
  804. Exit;
  805. end
  806. else
  807. ResetTableEntry := fCachedEntry;
  808. // First make sure that it is a compression we can read
  809. if ObjectExists(SectionPrefix+'ControlData') > 0 then begin
  810. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + 4;
  811. fStream.Read(Sig, 4);
  812. if Sig <> 'LZXC' then Exit;
  813. CompressionVersion := LEtoN(fStream.ReadDWord);
  814. if CompressionVersion > 2 then exit;
  815. {LZXResetInterval := }LEtoN(fStream.ReadDWord);
  816. {LZXWindowSize := }LEtoN(fStream.ReadDWord);
  817. {LZXCacheSize := }LEtoN(fStream.ReadDWord);
  818. BlockSize := FindBlocksFromUnCompressedAddr(ResetTableEntry, CompressedSize, UnCompressedSize, ResetTable);
  819. if UncompressedSize > 0 then ; // to avoid a compiler note
  820. if StartPos > 0 then
  821. FirstBlock := StartPos div BlockSize
  822. else
  823. FirstBlock := 0;
  824. LastBlock := (StartPos+BlockLength) div BlockSize;
  825. if ObjectExists(SectionPrefix+'Content') = 0 then exit;
  826. //WriteLn('Compressed Data start''s at: ', fHeaderSuffix.Offset + fCachedEntry.ContentOffset,' Size is: ', fCachedEntry.DecompressedLength);
  827. Result := TMemoryStream.Create;
  828. Result.Size := BlockLength;
  829. SetLength(InBuf,BlockSize);
  830. OutBuf := GetMem(BlockSize);
  831. // First Init a PLZXState
  832. LZXState := LZXinit(16);
  833. if LZXState = nil then begin
  834. Exit;
  835. end;
  836. // if FirstBlock is odd (1,3,5,7 etc) we have to read the even block before it first.
  837. if (FirstBlock <> 0) and (FirstBlock mod 2 > 0) then begin
  838. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + (ResetTable[FirstBLock-1]);
  839. ReadCount := ResetTable[FirstBlock] - ResetTable[FirstBlock-1];
  840. BlockWriteLength:=BlockSize;
  841. ReadBlock;
  842. ResultCode := LZXdecompress(LZXState, @InBuf[0], OutBuf, ReadCount, LongInt(BlockWriteLength));
  843. end;
  844. // now start the actual decompression loop
  845. for X := FirstBlock to LastBlock do begin
  846. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + (ResetTable[X]);
  847. if X = FirstBLock then
  848. WriteStart := StartPos - (X*BlockSize)
  849. else
  850. WriteStart := 0;
  851. if X = High(ResetTable) then
  852. ReadCount := CompressedSize - ResetTable[X]
  853. else
  854. ReadCount := ResetTable[X+1] - ResetTable[X];
  855. BlockWriteLength := BlockSize;
  856. if FirstBlock = LastBlock then begin
  857. WriteCount := BlockLength;
  858. end
  859. else if X = LastBlock then
  860. WriteCount := (StartPos+BlockLength) - (X*BlockSize)
  861. else WriteCount := BlockSize - WriteStart;
  862. ReadBlock;
  863. ResultCode := LZXdecompress(LZXState, @InBuf[0], OutBuf, ReadCount, LongInt(BlockWriteLength));
  864. //now write the decompressed data to the stream
  865. if ResultCode = DECR_OK then begin
  866. Result.Write(OutBuf[WriteStart], Int64(WriteCount));
  867. end
  868. else begin
  869. {$IFDEF CHM_DEBUG} // windows gui program will cause an exception with writeln's
  870. WriteLn('Decompress FAILED with error code: ', ResultCode);
  871. {$ENDIF}
  872. Result.Free;
  873. Result := Nil;
  874. FreeMem(OutBuf);
  875. SetLength(ResetTable,0);
  876. LZXteardown(LZXState);
  877. Exit;
  878. end;
  879. // if the next block is an even numbered block we have to reset the decompressor state
  880. if (X < LastBlock) and (X mod 2 > 0) then LZXreset(LZXState);
  881. end;
  882. FreeMem(OutBuf);
  883. SetLength(ResetTable,0);
  884. LZXteardown(LZXState);
  885. end;
  886. end;
  887. function TITSFReader.FindBlocksFromUnCompressedAddr(var ResetTableEntry: TPMGListChunkEntry;
  888. out CompressedSize: Int64; out UnCompressedSize: Int64; out LZXResetTable: TLZXResetTableArr): QWord;
  889. var
  890. BlockCount: LongWord;
  891. {$IFDEF ENDIAN_BIG}
  892. I: Integer;
  893. {$ENDIF}
  894. begin
  895. Result := 0;
  896. fStream.Position := fHeaderSuffix.Offset + ResetTableEntry.ContentOffset;
  897. fStream.ReadDWord;
  898. BlockCount := LEtoN(fStream.ReadDWord);
  899. fStream.ReadDWord;
  900. fStream.ReadDWord; // TableHeaderSize;
  901. fStream.Read(UnCompressedSize, SizeOf(QWord));
  902. UnCompressedSize := LEtoN(UnCompressedSize);
  903. fStream.Read(CompressedSize, SizeOf(QWord));
  904. CompressedSize := LEtoN(CompressedSize);
  905. fStream.Read(Result, SizeOf(QWord)); // block size
  906. Result := LEtoN(Result);
  907. // now we are located at the first block index
  908. SetLength(LZXResetTable, BlockCount);
  909. fStream.Read(LZXResetTable[0], SizeOf(QWord)*BlockCount);
  910. {$IFDEF ENDIAN_BIG}
  911. for I := 0 to High(LZXResetTable) do
  912. LZXResetTable[I] := LEtoN(LZXResetTable[I]);
  913. {$ENDIF}
  914. end;
  915. { TContextList }
  916. procedure TContextList.AddContext(Context: THelpContext; Url: String);
  917. var
  918. ContextItem: PContextItem;
  919. begin
  920. New(ContextItem);
  921. Add(ContextItem);
  922. ContextItem^.Context := Context;
  923. ContextItem^.Url := Url;
  924. end;
  925. function TContextList.GetURL(Context: THelpContext): String;
  926. var
  927. X: Integer;
  928. begin
  929. Result := '';
  930. for X := 0 to Count-1 do begin
  931. if PContextItem(Get(X))^.Context = Context then begin
  932. Result := PContextItem(Get(X))^.Url;
  933. Exit;
  934. end;
  935. end;
  936. end;
  937. procedure TContextList.Clear;
  938. var
  939. X: Integer;
  940. begin
  941. for X := Count-1 downto 0 do begin
  942. Dispose(PContextItem(Get(X)));
  943. Delete(X);
  944. end;
  945. end;
  946. { TChmFileList }
  947. procedure TChmFileList.Delete(Index: Integer);
  948. begin
  949. Chm[Index].Free;
  950. inherited Delete(Index);
  951. end;
  952. function TChmFileList.GetChm(AIndex: Integer): TChmReader;
  953. begin
  954. Result := TChmReader(Objects[AIndex]);
  955. end;
  956. function TChmFileList.GetFileName(AIndex: Integer): String;
  957. begin
  958. Result := Strings[AIndex];
  959. end;
  960. procedure TChmFileList.OpenNewFile(AFileName: String);
  961. var
  962. AStream: TFileStream;
  963. AChm: TChmReader;
  964. AIndex: Integer;
  965. begin
  966. if not FileExists(AFileName) then exit;
  967. AStream := TFileStream.Create(AFileName, fmOpenRead);
  968. AChm := TChmReader.Create(AStream, True);
  969. AIndex := AddObject(AFileName, AChm);
  970. fLastChm := AChm;
  971. if Assigned(fOnOpenNewFile) then fOnOpenNewFile(Self, AIndex)
  972. else fUnNotifiedFiles.Add(AChm);
  973. end;
  974. function TChmFileList.CheckOpenFile(AFileName: String): Boolean;
  975. var
  976. X: Integer;
  977. begin
  978. Result := False;
  979. for X := 0 to Count-1 do begin
  980. if ExtractFileName(FileName[X]) = AFileName then begin
  981. fLastChm := Chm[X];
  982. Result := True;
  983. Exit;
  984. end;
  985. end;
  986. if not Result then begin
  987. AFileName := ExtractFilePath(FileName[0])+AFileName;
  988. if FileExists(AFileName) and (ExtractFileExt(AFileName) = '.chm') then OpenNewFile(AFileName);
  989. Result := True;
  990. end;
  991. end;
  992. function TChmFileList.MetaObjectExists(var Name: String): QWord;
  993. var
  994. AFileName: String;
  995. URL: String;
  996. fStart, fEnd: Integer;
  997. Found: Boolean;
  998. begin
  999. Found := False;
  1000. Result := 0;
  1001. //Known META file link types
  1002. // ms-its:name.chm::/topic.htm
  1003. //mk:@MSITStore:name.chm::/topic.htm
  1004. if Pos('ms-its:', Name) > 0 then begin
  1005. fStart := Pos('ms-its:', Name)+Length('ms-its:');
  1006. fEnd := Pos('::', Name)-fStart;
  1007. AFileName := Copy(Name, fStart, fEnd);
  1008. fStart := fEnd+fStart+2;
  1009. fEnd := Length(Name) - (fStart-1);
  1010. URL := Copy(Name, fStart, fEnd);
  1011. Found := True;
  1012. end
  1013. else if Pos('mk:@MSITStore:', Name) > 0 then begin
  1014. fStart := Pos('mk:@MSITStore:', Name)+Length('mk:@MSITStore:');
  1015. fEnd := Pos('::', Name)-fStart;
  1016. AFileName := Copy(Name, fStart, fEnd);
  1017. fStart := fEnd+fStart+2;
  1018. fEnd := Length(Name) - (fStart-1);
  1019. URL := Copy(Name, fStart, fEnd);
  1020. Found := True;
  1021. end;
  1022. if not Found then exit;
  1023. if CheckOpenFile(AFileName) then
  1024. Result := fLastChm.ObjectExists(URL);
  1025. if Result > 0 then NAme := Url;
  1026. end;
  1027. function TChmFileList.MetaGetObject(Name: String): TMemoryStream;
  1028. begin
  1029. Result := nil;
  1030. if MetaObjectExists(Name) > 0 then Result := fLastChm.GetObject(Name);
  1031. end;
  1032. constructor TChmFileList.Create(PrimaryFileName: String);
  1033. begin
  1034. inherited Create;
  1035. fUnNotifiedFiles := TList.Create;
  1036. OpenNewFile(PrimaryFileName);
  1037. end;
  1038. destructor TChmFileList.Destroy;
  1039. begin
  1040. fUnNotifiedFiles.Free;
  1041. end;
  1042. procedure TChmFileList.SetOnOpenNewFile(AValue: TChmFileOpenEvent);
  1043. var
  1044. X: Integer;
  1045. begin
  1046. fOnOpenNewFile := AValue;
  1047. if AValue = nil then exit;
  1048. for X := 0 to fUnNotifiedFiles.Count-1 do
  1049. AValue(Self, X);
  1050. fUnNotifiedFiles.Clear;
  1051. end;
  1052. function TChmFileList.ObjectExists(Name: String; fChm: TChmReader = nil): QWord;
  1053. begin
  1054. Result := 0;
  1055. if Count = 0 then exit;
  1056. if fChm <> nil then fLastChm := fChm;
  1057. Result := fLastChm.ObjectExists(Name);
  1058. if Result = 0 then begin
  1059. Result := Chm[0].ObjectExists(Name);
  1060. if Result > 0 then fLastChm := Chm[0];
  1061. end;
  1062. if Result = 0 then begin
  1063. Result := MetaObjectExists(Name);
  1064. end;
  1065. end;
  1066. function TChmFileList.GetObject(Name: String): TMemoryStream;
  1067. begin
  1068. Result := nil;
  1069. if Count = 0 then exit;
  1070. Result := fLastChm.GetObject(Name);
  1071. if Result = nil then Result := MetaGetObject(Name);
  1072. end;
  1073. function TChmFileList.IsAnOpenFile(AFileName: String): Boolean;
  1074. var
  1075. X: Integer;
  1076. begin
  1077. Result := False;
  1078. for X := 0 to Count-1 do begin
  1079. if AFileName = FileName[X] then Exit(True);
  1080. end;
  1081. end;
  1082. end.