using System; using System.IO; using System.Collections; namespace HtmlHelp.ChmDecoding { /// /// The class CHMTocidx implements functions to decode the #TOCIDX internal file. /// internal sealed class CHMTocidx : IDisposable { /// /// Constant specifying the size of the data blocks /// private const int BLOCK_SIZE = 0x1000; /// /// Internal flag specifying if the object is going to be disposed /// private bool disposed = false; /// /// Internal member storing the binary file data /// private byte[] _binaryFileData = null; /// /// Internal memebr storing the offset to the 20/28 byte structs /// private int _offset2028 = 0; /// /// Internal member storing the offset to the 16 byte structs /// private int _offset16structs = 0; /// /// Internal member storing the number of 16 byte structs /// private int _numberOf16structs = 0; /// /// Internal member storing the offset to the topic list /// private int _offsetOftopics = 0; /// /// Internal member storing the toc /// private ArrayList _toc = new ArrayList(); /// /// Internal member for offset seeking /// private Hashtable _offsetTable = new Hashtable(); /// /// Internal member storing the associated chmfile object /// private CHMFile _associatedFile = null; /// /// Constructor of the class /// /// binary file data of the #TOCIDX file /// associated chm file public CHMTocidx(byte[] binaryFileData, CHMFile associatedFile) { _binaryFileData = binaryFileData; _associatedFile = associatedFile; DecodeData(); // clear internal binary data after extraction _binaryFileData = null; } /// /// Decodes the binary file data and fills the internal properties /// /// true if succeeded private bool DecodeData() { _toc = new ArrayList(); _offsetTable = new Hashtable(); bool bRet = true; MemoryStream memStream = new MemoryStream(_binaryFileData); BinaryReader binReader = new BinaryReader(memStream); int nCurOffset = 0; _offset2028 = binReader.ReadInt32(); _offset16structs = binReader.ReadInt32(); _numberOf16structs = binReader.ReadInt32(); _offsetOftopics = binReader.ReadInt32(); binReader.BaseStream.Seek( _offset2028, SeekOrigin.Begin ); if( RecursivelyBuildTree(ref binReader, _offset2028, _toc, null) ) { binReader.BaseStream.Seek( _offset16structs, SeekOrigin.Begin ); nCurOffset = (int)binReader.BaseStream.Position; for(int i=0; i < _numberOf16structs; i++) { int tocOffset = binReader.ReadInt32(); int sqNr = binReader.ReadInt32(); int topOffset = binReader.ReadInt32(); int hhctopicIdx = binReader.ReadInt32(); nCurOffset = (int)binReader.BaseStream.Position; int topicIdx = -1; // if the topic offset is within the range of the stream // and is >= the offset of the first topic dword if((topOffset < (binReader.BaseStream.Length - 4)) && (topOffset >= _offsetOftopics)) { // read the index of the topic for this item binReader.BaseStream.Seek( topOffset, SeekOrigin.Begin); topicIdx = binReader.ReadInt32(); binReader.BaseStream.Seek( nCurOffset, SeekOrigin.Begin); TOCItem item = (TOCItem)_offsetTable[tocOffset.ToString()]; if( item != null) { if(( topicIdx < _associatedFile.TopicsFile.TopicTable.Count)&&(topicIdx>=0)) { TopicEntry te = (TopicEntry) (_associatedFile.TopicsFile.TopicTable[topicIdx]); if( (te != null) && (item.TopicOffset < 0) ) { item.TopicOffset = te.EntryOffset; } } } } } } return bRet; } /// /// Recursively reads the binary toc tree from the file /// /// reference to binary reader /// offset of the first node in the current level /// arraylist of TOCItems for the current level /// parent item for the item /// Returns true if succeeded private bool RecursivelyBuildTree(ref BinaryReader binReader, int NodeOffset, ArrayList level, TOCItem parentItem) { bool bRet = true; int nextOffset=0; int nReadOffset = (int)binReader.BaseStream.Position; binReader.BaseStream.Seek(NodeOffset, SeekOrigin.Begin); do { int nCurOffset = (int)binReader.BaseStream.Position; int unkn1 = binReader.ReadInt16(); // unknown int unkn2 = binReader.ReadInt16(); // unknown int flag = binReader.ReadInt32(); int nFolderAdd = 0; if((_associatedFile != null) && (_associatedFile.ImageTypeFolder)) { // get the value which should be added, to display folders instead of books if(HtmlHelpSystem.UseHH2TreePics) nFolderAdd = 8; else nFolderAdd = 4; } int nFolderImgIdx = (HtmlHelpSystem.UseHH2TreePics ? (TOCItem.STD_FOLDER_HH2+nFolderAdd) : (TOCItem.STD_FOLDER_HH1+nFolderAdd)); int nFileImgIdx = (HtmlHelpSystem.UseHH2TreePics ? TOCItem.STD_FILE_HH2 : TOCItem.STD_FILE_HH1); int stdImage = ((flag & 0x4)!=0) ? nFolderImgIdx : nFileImgIdx; int stringOffset = binReader.ReadInt32(); int ParentOffset = binReader.ReadInt32(); nextOffset = binReader.ReadInt32(); int firstChildOffset = 0; int unkn3=0; if( (flag&0x4)!=0 ) { firstChildOffset = binReader.ReadInt32(); unkn3 = binReader.ReadInt32(); // unknown } TOCItem newItem = new TOCItem(); newItem.ImageIndex = stdImage; newItem.Offset = nCurOffset; newItem.OffsetNext = nextOffset; newItem.AssociatedFile = _associatedFile; newItem.TocMode = DataMode.Binary; newItem.Parent = parentItem; if( (flag&0x08) == 0) { // toc item doesn't have a local value (=> stringOffset = offset of strings file) newItem.Name = _associatedFile.StringsFile[stringOffset]; } else { // this item has a topic entry (=> stringOffset = index of topic entry) if((stringOffset < _associatedFile.TopicsFile.TopicTable.Count) && (stringOffset >= 0)) { TopicEntry te = (TopicEntry) (_associatedFile.TopicsFile.TopicTable[stringOffset]); if(te != null) { newItem.TopicOffset = te.EntryOffset; } } } _offsetTable[nCurOffset.ToString()] = newItem; // if this item has children (firstChildOffset > 0) if( firstChildOffset > 0) { bRet &= RecursivelyBuildTree(ref binReader, firstChildOffset, newItem.Children, newItem); } level.Add( newItem ); if(nCurOffset != nextOffset) binReader.BaseStream.Seek(nextOffset, SeekOrigin.Begin); }while(nextOffset != 0); binReader.BaseStream.Seek(nReadOffset, SeekOrigin.Begin); return bRet; } /// /// Gets the internal read toc /// internal ArrayList TOC { get { return _toc; } } /// /// Implement IDisposable. /// public void Dispose() { Dispose(true); // This object will be cleaned up by the Dispose method. // Therefore, you should call GC.SupressFinalize to // take this object off the finalization queue // and prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(this); } /// /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// /// disposing flag private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if(!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if(disposing) { // Dispose managed resources. _binaryFileData = null; _toc = null; _offsetTable = null; } } disposed = true; } } }