def GetWindowsInfo(self): '''Gets information from the #WINDOWS file. Checks the #WINDOWS file to see if it has any info that was not found in #SYSTEM (topics, index or default page. ''' result, ui = chmlib.chm_resolve_object(self.file, '/#WINDOWS') if (result != chmlib.CHM_RESOLVE_SUCCESS): return -1 size, text = chmlib.chm_retrieve_object(self.file, ui, 0l, 8) if (size < 8): return -2 buff = array.array('B', text) num_entries = self.GetDWORD(buff, 0) entry_size = self.GetDWORD(buff, 4) if num_entries < 1: return -3 size, text = chmlib.chm_retrieve_object(self.file, ui, 8l, entry_size) if (size < entry_size): return -4 buff = array.array('B', text) toc_index = self.GetDWORD(buff, 0x60) idx_index = self.GetDWORD(buff, 0x64) dft_index = self.GetDWORD(buff, 0x68) result, ui = chmlib.chm_resolve_object(self.file, '/#STRINGS') if (result != chmlib.CHM_RESOLVE_SUCCESS): return -5 size, text = chmlib.chm_retrieve_object(self.file, ui, 0l, ui.length) if (size == 0): return -6 if (not self.topics): self.topics = self.GetString(text, toc_index) if not self.topics.startswith("/"): self.topics = "/" + self.topics if (not self.index): self.index = self.GetString(text, idx_index) if not self.index.startswith("/"): self.index = "/" + self.index if (dft_index != 0): self.home = self.GetString(text, dft_index) if not self.home.startswith("/"): self.home = "/" + self.home
def ResolveObject(self, document): '''Tries to locate a document in the archive. This function tries to locate the document inside the archive. It returns a tuple where the first element is zero if the function was successful, and the second is the UnitInfo for that document. The UnitInfo is used to retrieve the document contents ''' if self.file: path = os.path.abspath(document) return chmlib.chm_resolve_object(self.file, path) else: return (1, None)
def GetIndex(self): '''Reads and returns the index tree. This auxiliary function reads and returns the index tree file contents for the CHM archive. ''' if self.index is None: return None if self.index: res, ui = chmlib.chm_resolve_object(self.file, self.index) if (res != chmlib.CHM_RESOLVE_SUCCESS): return None size, text = chmlib.chm_retrieve_object(self.file, ui, 0l, ui.length) if (size == 0): sys.stderr.write('GetIndex: file size = 0\n') return None return text
def GetArchiveInfo(self): '''Obtains information on CHM archive. This function checks the /#SYSTEM file inside the CHM archive to obtain the index, home page, topics, encoding and title. It is called from LoadCHM. ''' self.searchable = extra.is_searchable(self.file) self.lcid = None result, ui = chmlib.chm_resolve_object(self.file, '/#SYSTEM') if (result != chmlib.CHM_RESOLVE_SUCCESS): sys.stderr.write('GetArchiveInfo: #SYSTEM does not exist\n') return 0 size, text = chmlib.chm_retrieve_object(self.file, ui, 4l, ui.length) if (size == 0): sys.stderr.write('GetArchiveInfo: file size = 0\n') return 0 buff = array.array('B', text) index = 0 while (index < size): cursor = buff[index] + (buff[index + 1] * 256) if (cursor == 0): index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 self.topics = '/' + text[index:index + cursor - 1] elif (cursor == 1): index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 self.index = '/' + text[index:index + cursor - 1] elif (cursor == 2): index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 self.home = '/' + text[index:index + cursor - 1] elif (cursor == 3): index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 self.title = text[index:index + cursor - 1] elif (cursor == 4): index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 self.lcid = buff[index] + (buff[index + 1] * 256) elif (cursor == 6): index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 tmp = text[index:index + cursor - 1] if not self.topics: tmp1 = '/' + tmp + '.hhc' tmp2 = '/' + tmp + '.hhk' res1, ui1 = chmlib.chm_resolve_object(self.file, tmp1) res2, ui2 = chmlib.chm_resolve_object(self.file, tmp2) if not self.topics and res1 == chmlib.CHM_RESOLVE_SUCCESS: self.topics = '/' + tmp + '.hhc' if not self.index and res2 == chmlib.CHM_RESOLVE_SUCCESS: self.index = '/' + tmp + '.hhk' elif (cursor == 16): index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 self.encoding = text[index:index + cursor - 1] else: index += 2 cursor = buff[index] + (buff[index + 1] * 256) index += 2 index += cursor self.GetWindowsInfo() if not self.lcid: self.lcid = extra.get_lcid(self.file) return 1
def GetArchiveInfo(self): '''Obtains information on CHM archive. This function checks the /#SYSTEM file inside the CHM archive to obtain the index, home page, topics, encoding and title. It is called from LoadCHM. ''' self.searchable = extra.is_searchable(self.file) self.lcid = None result, ui = chmlib.chm_resolve_object(self.file, '/#SYSTEM') if (result != chmlib.CHM_RESOLVE_SUCCESS): sys.stderr.write('GetArchiveInfo: #SYSTEM does not exist\n') return 0 size, text = chmlib.chm_retrieve_object(self.file, ui, 4l, ui.length) if (size == 0): sys.stderr.write('GetArchiveInfo: file size = 0\n') return 0 buff = array.array('B', text) index = 0 while (index < size): cursor = buff[index] + (buff[index+1] * 256) if (cursor == 0): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.topics = '/' + text[index:index+cursor-1] elif (cursor == 1): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.index = '/' + text[index:index+cursor-1] elif (cursor == 2): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.home = '/' + text[index:index+cursor-1] elif (cursor == 3): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.title = text[index:index+cursor-1] elif (cursor == 4): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.lcid = buff[index] + (buff[index+1] * 256) elif (cursor == 6): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 tmp = text[index:index+cursor-1] if not self.topics: tmp1 = '/' + tmp + '.hhc' tmp2 = '/' + tmp + '.hhk' res1, ui1 = chmlib.chm_resolve_object(self.file, tmp1) res2, ui2 = chmlib.chm_resolve_object(self.file, tmp2) if not self.topics and res1 == chmlib.CHM_RESOLVE_SUCCESS: self.topics = '/' + tmp + '.hhc' if not self.index and res2 == chmlib.CHM_RESOLVE_SUCCESS: self.index = '/' + tmp + '.hhk' elif (cursor == 16): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.encoding = text[index:index+cursor-1] else: index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 index += cursor self.GetWindowsInfo() if not self.lcid: self.lcid = extra.get_lcid(self.file) return 1