def pymsiecf_test_multi_open_close_file_object(filename, mode): print( ("Testing multi open close of file-like object of: {0:s} " "with access: {1:s}\t").format( filename, get_mode_string(mode) ) ) result = True try: file_object = open(filename, "rb") msiecf_file = pymsiecf.file() msiecf_file.open_file_object(file_object, mode) msiecf_file.close() msiecf_file.open_file_object(file_object, mode) msiecf_file.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def test_open_close(self): """Tests the open and close functions.""" if not unittest.source: return msiecf_file = pymsiecf.file() # Test open and close. msiecf_file.open(unittest.source) msiecf_file.close() # Test open and close a second time to validate clean up on close. msiecf_file.open(unittest.source) msiecf_file.close() file_object = open(unittest.source, "rb") # Test open_file_object and close. msiecf_file.open_file_object(file_object) msiecf_file.close() # Test open_file_object and close a second time to validate clean up on close. msiecf_file.open_file_object(file_object) msiecf_file.close() # Test open_file_object and close and dereferencing file_object. msiecf_file.open_file_object(file_object) del file_object msiecf_file.close()
def pymsiecf_test_single_open_close_file_object_with_dereference( filename, mode): """Tests single file-like object open and close with dereference.""" description = ( "Testing single open close of file-like object with dereference of: " "{0:s} with access: {1:s}\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: file_object = open(filename, "rb") msiecf_file = pymsiecf.file() msiecf_file.open_file_object(file_object, mode) del file_object msiecf_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def pymsiecf_test_single_open_close_file_object_with_dereference( filename, mode): """Tests single file-like object open and close with dereference.""" description = ( "Testing single open close of file-like object with dereference of: " "{0:s} with access: {1:s}\t").format( get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: file_object = open(filename, "rb") msiecf_file = pymsiecf.file() msiecf_file.open_file_object(file_object, mode) del file_object msiecf_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def pymsiecf_test_multi_open_close_file(filename, mode): """Tests multiple open and close.""" description = ( "Testing multi open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: msiecf_file = pymsiecf.file() msiecf_file.open(filename, mode) msiecf_file.close() msiecf_file.open(filename, mode) msiecf_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def pymsiecf_test_multi_open_close_file(filename, mode): """Tests multiple open and close.""" description = ("Testing multi open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: msiecf_file = pymsiecf.file() msiecf_file.open(filename, mode) msiecf_file.close() msiecf_file.open(filename, mode) msiecf_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def test_close(self): """Tests the close function.""" if not unittest.source: raise unittest.SkipTest("missing source") msiecf_file = pymsiecf.file() with self.assertRaises(IOError): msiecf_file.close()
def test_close(self): """Tests the close function.""" if not unittest.source: return msiecf_file = pymsiecf.file() with self.assertRaises(IOError): msiecf_file.close()
def Parse(self, file_entry): """Extract data from a MSIE Cache File (MSIECF). Args: file_entry: A file entry object. Yields: An event object (instance of MsiecfUrlEvent) that contains the parsed data. """ file_object = file_entry.GetFileObject() msiecf_file = pymsiecf.file() msiecf_file.set_ascii_codepage( getattr(self._pre_obj, 'codepage', 'cp1252')) try: msiecf_file.open_file_object(file_object) self.version = msiecf_file.format_version except IOError as exception: raise errors.UnableToParseFile( u'[{0:s}] unable to parse file {1:s}: {2:s}'.format( self.parser_name, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.url): for event_object in self._ParseUrl(self._pre_obj, msiecf_item): yield event_object # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.warning( u'[{0:s}] unable to parse item: {1:d} in file: {2:s}: {3:s}' .format(self.parser_name, item_index, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_recovered_items): try: msiecf_item = msiecf_file.get_recovered_item(item_index) if isinstance(msiecf_item, pymsiecf.url): for event_object in self._ParseUrl(self._pre_obj, msiecf_item, recovered=True): yield event_object # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.info(( u'[{0:s}] unable to parse recovered item: {1:d} in file: {2:s}: ' u'{3:s}').format(self.parser_name, item_index, file_entry.name, exception)) file_object.close()
def parse_index_dat(filepath): msiecf_file = pymsiecf.file() msiecf_file.open(filepath) #try: # msiecf_file.open_file_object(filepath) #except IOError as exception: # print(u'unable to open file with error: {0:s}'.format(exception)) # return format_version = msiecf_file.format_version cache_directories = [] for cache_directory_name in msiecf_file.cache_directories: cache_directories.append(cache_directory_name) for item_index in range(0, msiecf_file.number_of_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.leak): pass elif isinstance(msiecf_item, pymsiecf.redirected): pass elif isinstance(msiecf_item, pymsiecf.url): timestamps = parse_url(format_version, cache_directories, msiecf_item) print "Type: Url" print "Primary timestamp: " + timestamps[0] print "Secondary timestamp: " + timestamps[1] print "Http headers: " + timestamps[2] print "---------------------------------------------\n" except IOError as exception: print(u'Unable to parse item: {0:d} with error: {1:s}'.format(item_index, exception)) for item_index in range(0, msiecf_file.number_of_recovered_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.leak): pass elif isinstance(msiecf_item, pymsiecf.redirected): pass elif isinstance(msiecf_item, pymsiecf.url): timestamps = parse_url(format_version, cache_directories, msiecf_item, True) print "Type: Url (recovered)" print "Primary timestamp: " + timestamps[0] print "Secondary timestamp: " + timestamps[1] print "Http headers: " + timestamps[2] print "---------------------------------------------\n" except IOError as exception: print(u'Unable to parse item: {0:d} with error: {1:s}'.format(item_index, exception)) msiecf_file.close()
def Parse(self, parser_context, file_entry): """Extract data from a MSIE Cache File (MSIECF). Args: parser_context: A parser context object (instance of ParserContext). file_entry: A file entry object (instance of dfvfs.FileEntry). """ file_object = file_entry.GetFileObject() msiecf_file = pymsiecf.file() msiecf_file.set_ascii_codepage(parser_context.codepage) try: msiecf_file.open_file_object(file_object) self.version = msiecf_file.format_version except IOError as exception: raise errors.UnableToParseFile( u'[{0:s}] unable to parse file {1:s}: {2:s}'.format( self.NAME, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.url): self._ParseUrl(parser_context, msiecf_item, file_entry=file_entry) # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.warning( u'[{0:s}] unable to parse item: {1:d} in file: {2:s}: {3:s}' .format(self.NAME, item_index, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_recovered_items): try: msiecf_item = msiecf_file.get_recovered_item(item_index) if isinstance(msiecf_item, pymsiecf.url): self._ParseUrl(parser_context, msiecf_item, file_entry=file_entry, recovered=True) # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.info(( u'[{0:s}] unable to parse recovered item: {1:d} in file: {2:s}: ' u'{3:s}').format(self.NAME, item_index, file_entry.name, exception)) file_object.close()
def Parse(self, file_entry): """Extract data from a MSIE Cache File (MSIECF). Args: file_entry: A file entry object. Yields: An event object (instance of MsiecfUrlEvent) that contains the parsed data. """ file_object = file_entry.GetFileObject() msiecf_file = pymsiecf.file() msiecf_file.set_ascii_codepage(getattr(self._pre_obj, 'codepage', 'cp1252')) try: msiecf_file.open_file_object(file_object) self.version = msiecf_file.format_version except IOError as exception: raise errors.UnableToParseFile( u'[{0:s}] unable to parse file {1:s}: {2:s}'.format( self.parser_name, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.url): for event_object in self._ParseUrl(self._pre_obj, msiecf_item): yield event_object # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.warning( u'[{0:s}] unable to parse item: {1:d} in file: {2:s}: {3:s}'.format( self.parser_name, item_index, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_recovered_items): try: msiecf_item = msiecf_file.get_recovered_item(item_index) if isinstance(msiecf_item, pymsiecf.url): for event_object in self._ParseUrl( self._pre_obj, msiecf_item, recovered=True): yield event_object # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.info(( u'[{0:s}] unable to parse recovered item: {1:d} in file: {2:s}: ' u'{3:s}').format( self.parser_name, item_index, file_entry.name, exception)) file_object.close()
def Parse(self, parser_context, file_entry): """Extract data from a MSIE Cache File (MSIECF). Args: parser_context: A parser context object (instance of ParserContext). file_entry: A file entry object (instance of dfvfs.FileEntry). """ file_object = file_entry.GetFileObject() msiecf_file = pymsiecf.file() msiecf_file.set_ascii_codepage(parser_context.codepage) try: msiecf_file.open_file_object(file_object) self.version = msiecf_file.format_version except IOError as exception: raise errors.UnableToParseFile( u'[{0:s}] unable to parse file {1:s}: {2:s}'.format( self.NAME, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.url): self._ParseUrl(parser_context, msiecf_item, file_entry=file_entry) # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.warning( u'[{0:s}] unable to parse item: {1:d} in file: {2:s}: {3:s}'.format( self.NAME, item_index, file_entry.name, exception)) for item_index in range(0, msiecf_file.number_of_recovered_items): try: msiecf_item = msiecf_file.get_recovered_item(item_index) if isinstance(msiecf_item, pymsiecf.url): self._ParseUrl( parser_context, msiecf_item, file_entry=file_entry, recovered=True) # TODO: implement support for pymsiecf.leak, pymsiecf.redirected, # pymsiecf.item. except IOError as exception: logging.info(( u'[{0:s}] unable to parse recovered item: {1:d} in file: {2:s}: ' u'{3:s}').format( self.NAME, item_index, file_entry.name, exception)) file_object.close()
def pymsiecf_test_single_open_close_file(filename, mode): """Tests a single open and close.""" description = ( "Testing single open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: msiecf_file = pymsiecf.file() msiecf_file.open(filename, mode) msiecf_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pymsiecf_file_open") if not filename and str(exception) == expected_message: pass else: error_string = str(exception) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format( "pymsiecf_file_open") if mode != "w" or str(exception) != expected_message: error_string = str(exception) result = False except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def test_get_number_of_recovered_items(self): """Tests the get_number_of_recovered_items function and number_of_recovered_items property.""" if not unittest.source: raise unittest.SkipTest("missing source") msiecf_file = pymsiecf.file() msiecf_file.open(unittest.source) number_of_recovered_items = msiecf_file.get_number_of_recovered_items() self.assertIsNotNone(number_of_recovered_items) self.assertIsNotNone(msiecf_file.number_of_recovered_items) msiecf_file.close()
def test_get_ascii_codepage(self): """Tests the get_ascii_codepage function and ascii_codepage property.""" if not unittest.source: raise unittest.SkipTest("missing source") msiecf_file = pymsiecf.file() msiecf_file.open(unittest.source) ascii_codepage = msiecf_file.get_ascii_codepage() self.assertIsNotNone(ascii_codepage) self.assertIsNotNone(msiecf_file.ascii_codepage) msiecf_file.close()
def test_get_size(self): """Tests the get_size function and size property.""" if not unittest.source: raise unittest.SkipTest("missing source") msiecf_file = pymsiecf.file() msiecf_file.open(unittest.source) size = msiecf_file.get_size() self.assertIsNotNone(size) self.assertIsNotNone(msiecf_file.size) msiecf_file.close()
def pymsiecf_test_single_open_close_file(filename, mode): """Tests a single open and close.""" description = ("Testing single open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: msiecf_file = pymsiecf.file() msiecf_file.open(filename, mode) msiecf_file.close() except TypeError as exception: expected_message = ("{0:s}: unsupported string object type." ).format("pymsiecf_file_open") if not filename and str(exception) == expected_message: pass else: error_string = str(exception) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format("pymsiecf_file_open") if mode != "w" or str(exception) != expected_message: error_string = str(exception) result = False except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def pymsiecf_test_single_open_close_file(filename, mode): if not filename: filename_string = "None" else: filename_string = filename print("Testing single open close of: {0:s} with access: {1:s}\t".format( filename_string, get_mode_string(mode))) result = True try: msiecf_file = pymsiecf.file() msiecf_file.open(filename, mode) msiecf_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pymsiecf_file_open") if not filename and str(exception) == expected_message: pass else: print(str(exception)) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format( "pymsiecf_file_open") if mode != "w" or str(exception) != expected_message: print(str(exception)) result = False except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def test_open(self): """Tests the open function.""" if not unittest.source: return msiecf_file = pymsiecf.file() msiecf_file.open(unittest.source) with self.assertRaises(IOError): msiecf_file.open(unittest.source) msiecf_file.close() with self.assertRaises(TypeError): msiecf_file.open(None) with self.assertRaises(ValueError): msiecf_file.open(unittest.source, mode="w")
def test_set_ascii_codepage(self): """Tests the set_ascii_codepage function.""" supported_codepages = ( "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258") msiecf_file = pymsiecf.file() for codepage in supported_codepages: msiecf_file.set_ascii_codepage(codepage) unsupported_codepages = ( "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u") for codepage in unsupported_codepages: with self.assertRaises(RuntimeError): msiecf_file.set_ascii_codepage(codepage)
def ParseFileObject(self, parser_mediator, file_object): """Parses a MSIE Cache File (MSIECF) file-like object. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. file_object (dfvfs.FileIO): file-like object. """ msiecf_file = pymsiecf.file() msiecf_file.set_ascii_codepage(parser_mediator.codepage) try: msiecf_file.open_file_object(file_object) except IOError as exception: parser_mediator.ProduceExtractionWarning( 'unable to open file with error: {0!s}'.format(exception)) return try: self._ParseItems(parser_mediator, msiecf_file) finally: msiecf_file.close()
def test_set_ascii_codepage(self): """Tests the set_ascii_codepage function.""" supported_codepages = ("ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258") msiecf_file = pymsiecf.file() for codepage in supported_codepages: msiecf_file.set_ascii_codepage(codepage) unsupported_codepages = ("iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u") for codepage in unsupported_codepages: with self.assertRaises(RuntimeError): msiecf_file.set_ascii_codepage(codepage)
def test_open_file_object(self): """Tests the open_file_object function.""" if not unittest.source: return file_object = open(unittest.source, "rb") msiecf_file = pymsiecf.file() msiecf_file.open_file_object(file_object) with self.assertRaises(IOError): msiecf_file.open_file_object(file_object) msiecf_file.close() # TODO: change IOError into TypeError with self.assertRaises(IOError): msiecf_file.open_file_object(None) with self.assertRaises(ValueError): msiecf_file.open_file_object(file_object, mode="w")
def test_open_file_object(self): """Tests the open_file_object function.""" if not unittest.source: raise unittest.SkipTest("missing source") if not os.path.isfile(unittest.source): raise unittest.SkipTest("source not a regular file") msiecf_file = pymsiecf.file() with open(unittest.source, "rb") as file_object: msiecf_file.open_file_object(file_object) with self.assertRaises(IOError): msiecf_file.open_file_object(file_object) msiecf_file.close() with self.assertRaises(TypeError): msiecf_file.open_file_object(None) with self.assertRaises(ValueError): msiecf_file.open_file_object(file_object, mode="w")
def ParseFileObject(self, parser_mediator, file_object, **kwargs): """Parses a MSIE Cache File (MSIECF) file-like object. Args: parser_mediator: A parser mediator object (instance of ParserMediator). file_object: A file-like object. """ msiecf_file = pymsiecf.file() msiecf_file.set_ascii_codepage(parser_mediator.codepage) try: msiecf_file.open_file_object(file_object) except IOError as exception: parser_mediator.ProduceParseError( u'unable to open file with error: {0:s}'.format(exception)) return format_version = msiecf_file.format_version cache_directories = [] for cache_directory_name in msiecf_file.cache_directories: cache_directories.append(cache_directory_name) for item_index in range(0, msiecf_file.number_of_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.leak): self._ParseLeak(parser_mediator, cache_directories, msiecf_item) elif isinstance(msiecf_item, pymsiecf.redirected): self._ParseRedirected(parser_mediator, msiecf_item) elif isinstance(msiecf_item, pymsiecf.url): self._ParseUrl( parser_mediator, format_version, cache_directories, msiecf_item) except IOError as exception: parser_mediator.ProduceParseError( u'Unable to parse item: {0:d} with error: {1:s}'.format( item_index, exception)) for item_index in range(0, msiecf_file.number_of_recovered_items): try: msiecf_item = msiecf_file.get_recovered_item(item_index) if isinstance(msiecf_item, pymsiecf.leak): self._ParseLeak( parser_mediator, cache_directories, msiecf_item, recovered=True) elif isinstance(msiecf_item, pymsiecf.redirected): self._ParseRedirected(parser_mediator, msiecf_item, recovered=True) elif isinstance(msiecf_item, pymsiecf.url): self._ParseUrl( parser_mediator, format_version, cache_directories, msiecf_item, recovered=True) except IOError as exception: parser_mediator.ProduceParseError( u'Unable to parse recovered item: {0:d} with error: {1:s}'.format( item_index, exception)) msiecf_file.close()
def test_signal_abort(self): """Tests the signal_abort function.""" msiecf_file = pymsiecf.file() msiecf_file.signal_abort()
def main(): supported_codepages = [ "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258"] unsupported_codepages = [ "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u"] msiecf_file = pymsiecf.file() result = True for codepage in supported_codepages: print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: msiecf_file.ascii_codepage = codepage result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: msiecf_file.set_ascii_codepage(codepage) result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") for codepage in unsupported_codepages: print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: msiecf_file.ascii_codepage = codepage except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pymsiecf_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: msiecf_file.set_ascii_codepage(codepage) except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pymsiecf_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") return True
def main(): supported_codepages = [ "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258" ] unsupported_codepages = [ "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u" ] msiecf_file = pymsiecf.file() result = True for codepage in supported_codepages: print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: msiecf_file.ascii_codepage = codepage result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: msiecf_file.set_ascii_codepage(codepage) result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") for codepage in unsupported_codepages: print("Testing setting unsupported ASCII codepage of: {0:s}:\t". format(codepage)), result = False try: msiecf_file.ascii_codepage = codepage except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage." ).format("pymsiecf_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting unsupported ASCII codepage of: {0:s}:\t". format(codepage)), result = False try: msiecf_file.set_ascii_codepage(codepage) except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage." ).format("pymsiecf_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") return True
def ParseFileObject(self, parser_mediator, file_object, **kwargs): """Parses a MSIE Cache File (MSIECF) file-like object. Args: parser_mediator: A parser mediator object (instance of ParserMediator). file_object: A file-like object. """ msiecf_file = pymsiecf.file() msiecf_file.set_ascii_codepage(parser_mediator.codepage) try: msiecf_file.open_file_object(file_object) except IOError as exception: parser_mediator.ProduceParseError( u'unable to open file with error: {0:s}'.format(exception)) return format_version = msiecf_file.format_version cache_directories = [] for cache_directory_name in iter(msiecf_file.cache_directories): cache_directories.append(cache_directory_name) for item_index in range(0, msiecf_file.number_of_items): try: msiecf_item = msiecf_file.get_item(item_index) if isinstance(msiecf_item, pymsiecf.leak): self._ParseLeak(parser_mediator, cache_directories, msiecf_item) elif isinstance(msiecf_item, pymsiecf.redirected): self._ParseRedirected(parser_mediator, msiecf_item) elif isinstance(msiecf_item, pymsiecf.url): self._ParseUrl(parser_mediator, format_version, cache_directories, msiecf_item) except IOError as exception: parser_mediator.ProduceParseError( u'Unable to parse item: {0:d} with error: {1:s}'.format( item_index, exception)) for item_index in range(0, msiecf_file.number_of_recovered_items): try: msiecf_item = msiecf_file.get_recovered_item(item_index) if isinstance(msiecf_item, pymsiecf.leak): self._ParseLeak(parser_mediator, cache_directories, msiecf_item, recovered=True) elif isinstance(msiecf_item, pymsiecf.redirected): self._ParseRedirected(parser_mediator, msiecf_item, recovered=True) elif isinstance(msiecf_item, pymsiecf.url): self._ParseUrl(parser_mediator, format_version, cache_directories, msiecf_item, recovered=True) except IOError as exception: parser_mediator.ProduceParseError( u'Unable to parse recovered item: {0:d} with error: {1:s}'. format(item_index, exception)) msiecf_file.close()