chmreader.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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: QWord;
  48. fDirectoryHeaderLength: QWord;
  49. fDirectoryEntriesStartPos: QWord;
  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: QWord; out UnCompressedSize: QWord; 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: QWord;
  317. StrPosition: DWord;
  318. X: Integer;
  319. OffSet: QWord;
  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: QWord;
  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. //WriteLn('Looking for URL : ', Name);
  624. if Name = '' then Exit;
  625. if fDirectoryHeader.DirectoryChunkCount = 0 then exit;
  626. //WriteLn('Looking for ', Name);
  627. if Name = fCachedEntry.Name then
  628. Exit(fCachedEntry.DecompressedLength); // we've already looked it up
  629. ChunkStream := TMemoryStream.Create;
  630. try
  631. NextIndex := fDirectoryHeader.IndexOfRootChunk;
  632. if NextIndex < 0 then NextIndex := 0; // no PMGI chunks
  633. while NextIndex > -1 do begin
  634. GetDirectoryChunk(NextIndex, ChunkStream);
  635. NextIndex := -1;
  636. ReadQuickRefSection;
  637. //WriteLn('In Block ', ChunkIndex);
  638. case ChunkType(ChunkStream) of
  639. ctUnknown: // something is wrong
  640. begin
  641. {$IFDEF CHM_DEBUG}WriteLn(ChunkIndex, ' << Unknown BlockType!');{$ENDIF}
  642. Break;
  643. end;
  644. ctPMGI: // we must follow the PMGI tree until we reach a PMGL block
  645. begin
  646. LookupPMGIchunk(ChunkStream, PMGIChunk);
  647. //QuickRefIndex[0] := ChunkStream.Position;
  648. I := 0;
  649. while ChunkStream.Position <= ChunkStream.Size - PMGIChunk.UnusedSpace do begin;
  650. EntryName := ReadString;
  651. if EntryName = '' then break;
  652. if ChunkStream.Position >= ChunkStream.Size - PMGIChunk.UnusedSpace then break;
  653. CRes := ChmCompareText(Name, EntryName);
  654. if CRes = 0 then begin
  655. // no more need of this block. onto the next!
  656. NextIndex := GetCompressedInteger(ChunkStream);
  657. Break;
  658. end;
  659. if CRes < 0 then begin
  660. if I = 0 then Break; // File doesn't exist
  661. // file is in previous entry
  662. Break;
  663. end;
  664. NextIndex := GetCompressedInteger(ChunkStream);
  665. Inc(I);
  666. end;
  667. end;
  668. ctPMGL:
  669. begin
  670. LookupPMGLchunk(ChunkStream, PMGLChunk);
  671. QuickRefIndex[0] := ChunkStream.Position;
  672. I := 0;
  673. while ChunkStream.Position <= ChunkStream.Size - PMGLChunk.UnusedSpace do begin
  674. // we consume the entry by reading it
  675. Entry.Name := ReadString;
  676. if Entry.Name = '' then break;
  677. if ChunkStream.Position >= ChunkStream.Size - PMGLChunk.UnusedSpace then break;
  678. Entry.ContentSection := GetCompressedInteger(ChunkStream);
  679. Entry.ContentOffset := GetCompressedInteger(ChunkStream);
  680. Entry.DecompressedLength := GetCompressedInteger(ChunkStream);
  681. CRes := ChmCompareText(Name, Entry.Name);
  682. if CRes = 0 then begin
  683. fCachedEntry := Entry;
  684. Result := Entry.DecompressedLength;
  685. Break;
  686. end;
  687. Inc(I);
  688. end;
  689. end; // case
  690. end;
  691. end;
  692. finally
  693. ChunkStream.Free;
  694. end;
  695. end;
  696. function TITSFReader.GetObject(Name: String): TMemoryStream;
  697. var
  698. SectionNames: TStringList;
  699. Entry: TPMGListChunkEntry;
  700. SectionName: String;
  701. begin
  702. Result := nil;
  703. if ObjectExists(Name) = 0 then begin
  704. //WriteLn('Object ', name,' Doesn''t exist or is zero sized.');
  705. Exit;
  706. end;
  707. Entry := fCachedEntry;
  708. if Entry.ContentSection = 0 then begin
  709. Result := TMemoryStream.Create;
  710. fStream.Position := fHeaderSuffix.Offset+ Entry.ContentOffset;
  711. Result.CopyFrom(fStream, fCachedEntry.DecompressedLength);
  712. end
  713. else begin // we have to get it from ::DataSpace/Storage/[MSCompressed,Uncompressed]/ControlData
  714. GetSections(SectionNames);
  715. FmtStr(SectionName, '::DataSpace/Storage/%s/',[SectionNames[Entry.ContentSection-1]]);
  716. Result := GetBlockFromSection(SectionName, Entry.ContentOffset, Entry.DecompressedLength);
  717. SectionNames.Free;
  718. end;
  719. if Result <> nil then Result.Position := 0;
  720. end;
  721. function TChmReader.GetContextUrl(Context: THelpContext): String;
  722. begin
  723. // will get '' if context not found
  724. Result := fContextList.GetURL(Context);
  725. end;
  726. function TChmReader.HasContextList: Boolean;
  727. begin
  728. Result := fContextList.Count > 0;
  729. end;
  730. procedure TITSFReader.GetSections(out Sections: TStringList);
  731. var
  732. Stream: TStream;
  733. EntryCount: Word;
  734. X: Integer;
  735. {$IFDEF ENDIAN_BIG}
  736. I: Integer;
  737. {$ENDIF}
  738. WString: array [0..31] of WideChar;
  739. StrLength: Word;
  740. begin
  741. Sections := TStringList.Create;
  742. //WriteLn('::DataSpace/NameList Size = ', ObjectExists('::DataSpace/NameList'));
  743. Stream := GetObject('::DataSpace/NameList');
  744. if Stream = nil then begin
  745. //WriteLn('Failed to get ::DataSpace/NameList!');
  746. exit;
  747. end;
  748. Stream.Position := 2;
  749. EntryCount := LEtoN(Stream.ReadWord);
  750. for X := 0 to EntryCount -1 do begin
  751. StrLength := LEtoN(Stream.ReadWord);
  752. if StrLength > 31 then StrLength := 31;
  753. Stream.Read(WString, SizeOf(WideChar)*(StrLength+1)); // the strings are stored null terminated
  754. {$IFDEF ENDIAN_BIG}
  755. for I := 0 to StrLength-1 do
  756. WString[I] := WideChar(LEtoN(Ord(WString[I])));
  757. {$ENDIF}
  758. Sections.Add(WString);
  759. end;
  760. // the sections are sorted alphabetically, this way section indexes will jive
  761. Sections.Sort;
  762. Stream.Free;
  763. end;
  764. function TITSFReader.GetBlockFromSection(SectionPrefix: String; StartPos: QWord;
  765. BlockLength: QWord): TMemoryStream;
  766. var
  767. Compressed: Boolean;
  768. Sig: Array [0..3] of char;
  769. CompressionVersion: LongWord;
  770. CompressedSize: QWord;
  771. UnCompressedSize: QWord;
  772. //LZXResetInterval: LongWord;
  773. //LZXWindowSize: LongWord;
  774. //LZXCacheSize: LongWord;
  775. ResetTableEntry: TPMGListChunkEntry;
  776. ResetTable: TLZXResetTableArr;
  777. WriteCount: QWord;
  778. BlockWriteLength: QWord;
  779. WriteStart: LongWord;
  780. ReadCount:LongInt;
  781. LZXState: PLZXState;
  782. InBuf: array of Byte;
  783. OutBuf: PByte;
  784. BlockSize: QWord;
  785. X: Integer;
  786. FirstBlock, LastBlock: LongInt;
  787. ResultCode: LongInt;
  788. procedure ReadBlock;
  789. begin
  790. if ReadCount > Length(InBuf) then
  791. SetLength(InBuf, ReadCount);
  792. fStream.Read(InBuf[0], ReadCount);
  793. end;
  794. begin
  795. // okay now the fun stuff ;)
  796. Result := nil;
  797. Compressed := ObjectExists(SectionPrefix+'Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable')>0;
  798. // the easy method
  799. if Not(Compressed) then begin
  800. if ObjectExists(SectionPrefix+'Content') > 0 then begin
  801. Result := TMemoryStream.Create;
  802. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + StartPos;
  803. Result.CopyFrom(fStream, BlockLength);
  804. end;
  805. Exit;
  806. end
  807. else
  808. ResetTableEntry := fCachedEntry;
  809. // First make sure that it is a compression we can read
  810. if ObjectExists(SectionPrefix+'ControlData') > 0 then begin
  811. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + 4;
  812. fStream.Read(Sig, 4);
  813. if Sig <> 'LZXC' then Exit;
  814. CompressionVersion := LEtoN(fStream.ReadDWord);
  815. if CompressionVersion > 2 then exit;
  816. {LZXResetInterval := }LEtoN(fStream.ReadDWord);
  817. {LZXWindowSize := }LEtoN(fStream.ReadDWord);
  818. {LZXCacheSize := }LEtoN(fStream.ReadDWord);
  819. BlockSize := FindBlocksFromUnCompressedAddr(ResetTableEntry, CompressedSize, UnCompressedSize, ResetTable);
  820. if UncompressedSize > 0 then ; // to avoid a compiler note
  821. if StartPos > 0 then
  822. FirstBlock := StartPos div BlockSize
  823. else
  824. FirstBlock := 0;
  825. LastBlock := (StartPos+BlockLength) div BlockSize;
  826. if ObjectExists(SectionPrefix+'Content') = 0 then exit;
  827. //WriteLn('Compressed Data start''s at: ', fHeaderSuffix.Offset + fCachedEntry.ContentOffset,' Size is: ', fCachedEntry.DecompressedLength);
  828. Result := TMemoryStream.Create;
  829. Result.Size := BlockLength;
  830. SetLength(InBuf,BlockSize);
  831. OutBuf := GetMem(BlockSize);
  832. // First Init a PLZXState
  833. LZXState := LZXinit(16);
  834. if LZXState = nil then begin
  835. Exit;
  836. end;
  837. // if FirstBlock is odd (1,3,5,7 etc) we have to read the even block before it first.
  838. if (FirstBlock <> 0) and (FirstBlock mod 2 > 0) then begin
  839. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + (ResetTable[FirstBLock-1]);
  840. ReadCount := ResetTable[FirstBlock] - ResetTable[FirstBlock-1];
  841. BlockWriteLength:=BlockSize;
  842. ReadBlock;
  843. ResultCode := LZXdecompress(LZXState, @InBuf[0], OutBuf, ReadCount, LongInt(BlockWriteLength));
  844. end;
  845. // now start the actual decompression loop
  846. for X := FirstBlock to LastBlock do begin
  847. fStream.Position := fHeaderSuffix.Offset + fCachedEntry.ContentOffset + (ResetTable[X]);
  848. if X = FirstBLock then
  849. WriteStart := StartPos - (X*BlockSize)
  850. else
  851. WriteStart := 0;
  852. if X = High(ResetTable) then
  853. ReadCount := CompressedSize - ResetTable[X]
  854. else
  855. ReadCount := ResetTable[X+1] - ResetTable[X];
  856. BlockWriteLength := BlockSize;
  857. if FirstBlock = LastBlock then begin
  858. WriteCount := BlockLength;
  859. end
  860. else if X = LastBlock then
  861. WriteCount := (StartPos+BlockLength) - (X*BlockSize)
  862. else WriteCount := BlockSize - WriteStart;
  863. ReadBlock;
  864. ResultCode := LZXdecompress(LZXState, @InBuf[0], OutBuf, ReadCount, LongInt(BlockWriteLength));
  865. //now write the decompressed data to the stream
  866. if ResultCode = DECR_OK then begin
  867. Result.Write(OutBuf[WriteStart], QWord(WriteCount));
  868. end
  869. else begin
  870. {$IFDEF CHM_DEBUG} // windows gui program will cause an exception with writeln's
  871. WriteLn('Decompress FAILED with error code: ', ResultCode);
  872. {$ENDIF}
  873. Result.Free;
  874. Result := Nil;
  875. FreeMem(OutBuf);
  876. SetLength(ResetTable,0);
  877. LZXteardown(LZXState);
  878. Exit;
  879. end;
  880. // if the next block is an even numbered block we have to reset the decompressor state
  881. if (X < LastBlock) and (X mod 2 > 0) then LZXreset(LZXState);
  882. end;
  883. FreeMem(OutBuf);
  884. SetLength(ResetTable,0);
  885. LZXteardown(LZXState);
  886. end;
  887. end;
  888. function TITSFReader.FindBlocksFromUnCompressedAddr(var ResetTableEntry: TPMGListChunkEntry;
  889. out CompressedSize: QWord; out UnCompressedSize: QWord; out LZXResetTable: TLZXResetTableArr): QWord;
  890. var
  891. BlockCount: LongWord;
  892. {$IFDEF ENDIAN_BIG}
  893. I: Integer;
  894. {$ENDIF}
  895. begin
  896. Result := 0;
  897. fStream.Position := fHeaderSuffix.Offset + ResetTableEntry.ContentOffset;
  898. fStream.ReadDWord;
  899. BlockCount := LEtoN(fStream.ReadDWord);
  900. fStream.ReadDWord;
  901. fStream.ReadDWord; // TableHeaderSize;
  902. fStream.Read(UnCompressedSize, SizeOf(QWord));
  903. UnCompressedSize := LEtoN(UnCompressedSize);
  904. fStream.Read(CompressedSize, SizeOf(QWord));
  905. CompressedSize := LEtoN(CompressedSize);
  906. fStream.Read(Result, SizeOf(QWord)); // block size
  907. Result := LEtoN(Result);
  908. // now we are located at the first block index
  909. SetLength(LZXResetTable, BlockCount);
  910. fStream.Read(LZXResetTable[0], SizeOf(QWord)*BlockCount);
  911. {$IFDEF ENDIAN_BIG}
  912. for I := 0 to High(LZXResetTable) do
  913. LZXResetTable[I] := LEtoN(LZXResetTable[I]);
  914. {$ENDIF}
  915. end;
  916. { TContextList }
  917. procedure TContextList.AddContext(Context: THelpContext; Url: String);
  918. var
  919. ContextItem: PContextItem;
  920. begin
  921. New(ContextItem);
  922. Add(ContextItem);
  923. ContextItem^.Context := Context;
  924. ContextItem^.Url := Url;
  925. end;
  926. function TContextList.GetURL(Context: THelpContext): String;
  927. var
  928. X: Integer;
  929. begin
  930. Result := '';
  931. for X := 0 to Count-1 do begin
  932. if PContextItem(Get(X))^.Context = Context then begin
  933. Result := PContextItem(Get(X))^.Url;
  934. Exit;
  935. end;
  936. end;
  937. end;
  938. procedure TContextList.Clear;
  939. var
  940. X: Integer;
  941. begin
  942. for X := Count-1 downto 0 do begin
  943. Dispose(PContextItem(Get(X)));
  944. Delete(X);
  945. end;
  946. end;
  947. { TChmFileList }
  948. procedure TChmFileList.Delete(Index: Integer);
  949. begin
  950. Chm[Index].Free;
  951. inherited Delete(Index);
  952. end;
  953. function TChmFileList.GetChm(AIndex: Integer): TChmReader;
  954. begin
  955. Result := TChmReader(Objects[AIndex]);
  956. end;
  957. function TChmFileList.GetFileName(AIndex: Integer): String;
  958. begin
  959. Result := Strings[AIndex];
  960. end;
  961. procedure TChmFileList.OpenNewFile(AFileName: String);
  962. var
  963. AStream: TFileStream;
  964. AChm: TChmReader;
  965. AIndex: Integer;
  966. begin
  967. if not FileExists(AFileName) then exit;
  968. AStream := TFileStream.Create(AFileName, fmOpenRead);
  969. AChm := TChmReader.Create(AStream, True);
  970. AIndex := AddObject(AFileName, AChm);
  971. fLastChm := AChm;
  972. if Assigned(fOnOpenNewFile) then fOnOpenNewFile(Self, AIndex)
  973. else fUnNotifiedFiles.Add(AChm);
  974. end;
  975. function TChmFileList.CheckOpenFile(AFileName: String): Boolean;
  976. var
  977. X: Integer;
  978. begin
  979. Result := False;
  980. for X := 0 to Count-1 do begin
  981. if ExtractFileName(FileName[X]) = AFileName then begin
  982. fLastChm := Chm[X];
  983. Result := True;
  984. Exit;
  985. end;
  986. end;
  987. if not Result then begin
  988. AFileName := ExtractFilePath(FileName[0])+AFileName;
  989. if FileExists(AFileName) and (ExtractFileExt(AFileName) = '.chm') then OpenNewFile(AFileName);
  990. Result := True;
  991. end;
  992. end;
  993. function TChmFileList.MetaObjectExists(var Name: String): QWord;
  994. var
  995. AFileName: String;
  996. URL: String;
  997. fStart, fEnd: Integer;
  998. Found: Boolean;
  999. begin
  1000. Found := False;
  1001. Result := 0;
  1002. //Known META file link types
  1003. // ms-its:name.chm::/topic.htm
  1004. //mk:@MSITStore:name.chm::/topic.htm
  1005. if Pos('ms-its:', Name) > 0 then begin
  1006. fStart := Pos('ms-its:', Name)+Length('ms-its:');
  1007. fEnd := Pos('::', Name)-fStart;
  1008. AFileName := Copy(Name, fStart, fEnd);
  1009. fStart := fEnd+fStart+2;
  1010. fEnd := Length(Name) - (fStart-1);
  1011. URL := Copy(Name, fStart, fEnd);
  1012. Found := True;
  1013. end
  1014. else if Pos('mk:@MSITStore:', Name) > 0 then begin
  1015. fStart := Pos('mk:@MSITStore:', Name)+Length('mk:@MSITStore:');
  1016. fEnd := Pos('::', Name)-fStart;
  1017. AFileName := Copy(Name, fStart, fEnd);
  1018. fStart := fEnd+fStart+2;
  1019. fEnd := Length(Name) - (fStart-1);
  1020. URL := Copy(Name, fStart, fEnd);
  1021. Found := True;
  1022. end;
  1023. if not Found then exit;
  1024. //WriteLn('Looking for URL ', URL, ' in ', AFileName);
  1025. if CheckOpenFile(AFileName) then
  1026. Result := fLastChm.ObjectExists(URL);
  1027. if Result > 0 then NAme := Url;
  1028. end;
  1029. function TChmFileList.MetaGetObject(Name: String): TMemoryStream;
  1030. begin
  1031. Result := nil;
  1032. if MetaObjectExists(Name) > 0 then Result := fLastChm.GetObject(Name);
  1033. end;
  1034. constructor TChmFileList.Create(PrimaryFileName: String);
  1035. begin
  1036. inherited Create;
  1037. fUnNotifiedFiles := TList.Create;
  1038. OpenNewFile(PrimaryFileName);
  1039. end;
  1040. destructor TChmFileList.Destroy;
  1041. begin
  1042. fUnNotifiedFiles.Free;
  1043. end;
  1044. procedure TChmFileList.SetOnOpenNewFile(AValue: TChmFileOpenEvent);
  1045. var
  1046. X: Integer;
  1047. begin
  1048. fOnOpenNewFile := AValue;
  1049. if AValue = nil then exit;
  1050. for X := 0 to fUnNotifiedFiles.Count-1 do
  1051. AValue(Self, X);
  1052. fUnNotifiedFiles.Clear;
  1053. end;
  1054. function TChmFileList.ObjectExists(Name: String; fChm: TChmReader = nil): QWord;
  1055. begin
  1056. Result := 0;
  1057. if Count = 0 then exit;
  1058. if fChm <> nil then fLastChm := fChm;
  1059. Result := fLastChm.ObjectExists(Name);
  1060. if Result = 0 then begin
  1061. Result := Chm[0].ObjectExists(Name);
  1062. if Result > 0 then fLastChm := Chm[0];
  1063. end;
  1064. if Result = 0 then begin
  1065. Result := MetaObjectExists(Name);
  1066. end;
  1067. end;
  1068. function TChmFileList.GetObject(Name: String): TMemoryStream;
  1069. begin
  1070. Result := nil;
  1071. if Count = 0 then exit;
  1072. Result := fLastChm.GetObject(Name);
  1073. if Result = nil then Result := MetaGetObject(Name);
  1074. end;
  1075. function TChmFileList.IsAnOpenFile(AFileName: String): Boolean;
  1076. var
  1077. X: Integer;
  1078. begin
  1079. Result := False;
  1080. for X := 0 to Count-1 do begin
  1081. if AFileName = FileName[X] then Exit(True);
  1082. end;
  1083. end;
  1084. end.