def test_invalid_dir_indexes(): with warnings.catch_warnings(record=True) as w: # Same file as example.dat with the root siblings corrupted, and # Stream 1 child corrupted; library continues as normal with cf.CompoundFileReader('tests/invalid_dir_indexes1.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileDirIndexWarning) assert issubclass(w[1].category, cf.CompoundFileDirIndexWarning) assert issubclass(w[2].category, cf.CompoundFileDirIndexWarning) assert len(w) == 3 verify_example(doc) with warnings.catch_warnings(record=True) as w: # Same file as example.dat with the Stream 1 siblings corrupted; # library continues as normal with cf.CompoundFileReader('tests/invalid_dir_indexes2.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileDirIndexWarning) assert issubclass(w[1].category, cf.CompoundFileDirIndexWarning) assert len(w) == 2 verify_example(doc) with warnings.catch_warnings(record=True) as w: # Same file as example.dat with the root child index corrupted; library # continues but file is effectively empty with cf.CompoundFileReader('tests/invalid_dir_indexes3.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileDirIndexWarning) assert len(w) == 1 verify_example(doc, ())
def test_invalid_bom(): with pytest.raises(cf.CompoundFileInvalidBomError): # Same as example.dat with big endian BOM cf.CompoundFileReader('tests/invalid_big_endian_bom.dat') with pytest.raises(cf.CompoundFileInvalidBomError): # Same as example.dat with invalid BOM cf.CompoundFileReader('tests/invalid_bom.dat')
def test_invalid_master_ext(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with master FAT extension pointer set to # FREE_SECTOR (should be END_OF_CHAIN for no extension); reader assumes # END_OF_CHAIN with cf.CompoundFileReader('tests/invalid_master_ext_free.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert len(w) == 1 verify_example(doc) with warnings.catch_warnings(record=True) as w: # Same as example.dat with master FAT extension pointer set to 6 # (beyond EOF) and count set to 0 with cf.CompoundFileReader('tests/invalid_master_ext_eof.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert len(w) == 1 verify_example(doc) with warnings.catch_warnings(record=True) as w: # Same as example.dat with master FAT extension counter set to 2, # but extension pointer set to END_OF_CHAIN with cf.CompoundFileReader( 'tests/invalid_master_ext_count.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert issubclass(w[1].category, cf.CompoundFileMasterFatWarning) assert len(w) == 2 verify_example(doc)
def test_invalid_name(): # Same file as example.dat with corrupted names (no NULL terminator in # name1, incorrect length in name2); library continues as normal in these # cases with warnings.catch_warnings(record=True) as w: with cf.CompoundFileReader('tests/invalid_name1.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileDirNameWarning) assert len(w) == 1 verify_example(doc) with warnings.catch_warnings(record=True) as w: with cf.CompoundFileReader('tests/invalid_name2.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileDirNameWarning) assert len(w) == 1 verify_example(doc)
def test_invalid_master_sectors(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with second master FAT block pointing beyond EOF; # reader ignores it and all further blocks with cf.CompoundFileReader('tests/invalid_master_eof.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert len(w) == 1 verify_example(doc) with warnings.catch_warnings(record=True) as w: # Same as example.dat with second master FAT block set to # MASTER_FAT_SECTOR; reader ignores it and all further blocks with cf.CompoundFileReader('tests/invalid_master_special.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert len(w) == 1 verify_example(doc)
def check_roi_areas_visually(self, heatmap_save_path): # ROIをヒートマップに表示して視覚的に確認する cxd_file = cf.CompoundFileReader(self.cxd_path) bitmap = cxd_file.open(cxd_file.root['Field Data']['Field 1']['i_Image1']['Bitmap 1']) data = bitmap.read() intensity = np.zeros(self.data_pixel_height*self.data_pixel_width*2) j = 0 for i in range(0, int(len(data) / 2)): intensity[i] = (int.from_bytes([data[j], data[j + 1]], 'little')) j += 2 tmp_intensity = (np.array(intensity) + 0) * 5 for _center in range(0, len(self.roi)): for j in range(0, len(self.roi[0])): tmp_intensity[int(self.roi[_center, j])] = 4095 cnt = 0 map_roi = np.zeros((self.data_pixel_height, self.data_pixel_width)) for i in range(0, self.data_pixel_height): for j in range(0, self.data_pixel_width): map_roi[i][j] = tmp_intensity[cnt] cnt += 1 plt.figure(figsize=(12, 9)) sns.heatmap(map_roi, square=True, vmax=4095, vmin=0) plt.savefig(heatmap_save_path + '/' + self.data_name + '_ROI.png', dpi=350) plt.close() # plt.show() print('ROI check heatmap is saved as :') print(heatmap_save_path + '/' + self.data_name + '_ROI.png') print()
def test_stream_seek(): with cf.CompoundFileReader('tests/example.dat') as doc: with doc.open('Storage 1/Stream 1') as f: assert f.seek(0, io.SEEK_END) == 544 assert f.seek(0, io.SEEK_CUR) == 544 with pytest.raises(ValueError): f.seek(-1)
def test_invalid_master_len(): with warnings.catch_warnings(record=True) as w: # Same as strange_master_ext.dat with master extension count set to 2 # (should be 1); reader ignores DIFAT count with cf.CompoundFileReader('tests/invalid_master_underrun.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert len(w) == 1 verify_example(doc) with warnings.catch_warnings(record=True) as w: # Same as strange_master_ext.dat with master extension count set to 0 # (should be 1); reader ignores DIFAT count with cf.CompoundFileReader('tests/invalid_master_overrun.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert issubclass(w[1].category, cf.CompoundFileMasterFatWarning) assert len(w) == 2 verify_example(doc)
def test_sample_from_fake_mmap(sample): filename, contents = sample stream = io.BytesIO() with io.open(filename, 'rb') as source: stream.write(source.read()) with cf.CompoundFileReader(stream) as doc: verify_contents(doc, contents)
def __init__(self, filePath): warnings.filterwarnings("ignore") #if os.path.exists(filePath): try: self.fp = compoundfiles.CompoundFileReader(filePath) self.parseFileHeader() self.isDamaged = False except: self.fp = open(filePath, 'rb') self.isDamaged = True #else: # self.fp = None if self.fp is not None: self.fileName = os.path.basename(filePath) self.filePath = filePath self.fileSize = os.path.getsize(filePath) self.fileType = os.path.splitext(filePath)[1][1:] self.has_metadata = False self.metaList = {'Title': "", 'Subject': "", 'Author': "", 'Tags':"", 'Explanation': "", 'LastSavedBy': "",'Version': "", 'Date': "",\ 'LastPrintedTime': "", 'CreatedTime': "",'LastSavedTime': "", 'Comment': "", 'RevisionNumber': "", 'Category': "", 'Manager': "",\ 'Company': "", 'ProgramName': "", 'TotalTime': "", 'Creator': "", 'Trapped': ""} self.has_content = False self.content = "" self.has_multimedia = False if self.isDamaged: self.isCompressed = None self.isEncrypted = None
def test_reader_open_filename(): with patch('io.open') as m: try: compoundfiles.CompoundFileReader('foo.doc') except ValueError: pass assert m.mock_calls[:2] == [call('foo.doc', 'rb'), call().fileno()]
def test_reader_master_invalid_special(): with patch('mmap.mmap') as mmap: master_fat = [0, compoundfiles.const.MASTER_FAT_SECTOR] + [0] * 107 mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*V3_HEADER) + struct.pack('<109L', *master_fat)) with pytest.raises(compoundfiles.CompoundFileMasterFatWarning): compoundfiles.CompoundFileReader(MagicMock())
def test_invalid_fat_len(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with length of FAT in header tweaked to 2 (should # be 1); reader ignores erroneous length with cf.CompoundFileReader('tests/invalid_fat_len.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMasterFatWarning) assert len(w) == 1 verify_example(doc)
def test_reader_invalid_dll_version(): with patch('mmap.mmap') as mmap: header = list(V4_HEADER) header[3] = 5 mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*header)) with pytest.raises(compoundfiles.CompoundFileVersionError): compoundfiles.CompoundFileReader(MagicMock())
def test_reader_silly_mini_sector_size(): with patch('mmap.mmap') as mmap: header = list(V3_HEADER) header[6] = 9 mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*header)) with pytest.raises(compoundfiles.CompoundFileSectorSizeWarning): compoundfiles.CompoundFileReader(MagicMock())
def test_reader_invalid_txn_signature(): with patch('mmap.mmap') as mmap: header = list(V4_HEADER) header[11] = 1 mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*header)) with pytest.raises(compoundfiles.CompoundFileHeaderWarning): compoundfiles.CompoundFileReader(MagicMock())
def test_reader_non_empty_unused(): with patch('mmap.mmap') as mmap: header = list(V4_HEADER) header[7] = b'\1' mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*header)) with pytest.raises(compoundfiles.CompoundFileHeaderWarning): compoundfiles.CompoundFileReader(MagicMock())
def test_invalid_root_type(): with warnings.catch_warnings(record=True) as w: # Same file as example.dat with root dir-entry type corrupted; in this # case the library corrects the dir-entry type and continues with cf.CompoundFileReader('tests/invalid_root_type.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileDirTypeWarning) assert len(w) == 1 verify_example(doc)
def test_reader_master_terminated_with_free(): with patch('mmap.mmap') as mmap: master_fat = [0, compoundfiles.const.FREE_SECTOR] + [0] * 107 mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*V3_HEADER) + struct.pack('<109L', *master_fat)) with pytest.raises(compoundfiles.CompoundFileMasterFatWarning): compoundfiles.CompoundFileReader(MagicMock())
def test_strange_dll_version(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with DLL version set to 5; reader warns about # this but ignores it with cf.CompoundFileReader('tests/strange_dll_version.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileVersionWarning) assert len(w) == 1 verify_example(doc)
def test_invalid_mini_fat(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with the mini-FAT start sector corrupted to be # FREE_SECTOR; reader assumes no mini-FAT in this case with cf.CompoundFileReader('tests/invalid_mini_free.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMiniFatWarning) assert len(w) == 1 verify_example(doc, test_contents=False) with pytest.raises(cf.CompoundFileNoMiniFatError): doc.open('Storage 1/Stream 1') with warnings.catch_warnings(record=True) as w: # Same as example.dat with the mini-FAT start sector corrupted to be # beyond EOF; reader assumes no mini-FAT in this case with cf.CompoundFileReader('tests/invalid_mini_eof.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileMiniFatWarning) assert len(w) == 1 verify_example(doc, test_contents=False)
def test_reader_header_bom(): with patch('mmap.mmap') as mmap: header = list(V3_HEADER) header[4] = 0xFEFF mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*header)) with pytest.raises(compoundfiles.CompoundFileInvalidBomError): compoundfiles.CompoundFileReader(MagicMock())
def test_reader_master_sector_beyond_eof(): with patch('mmap.mmap') as mmap: master_fat = [20] + [0] * 108 mmap.return_value = mmap_mock( compoundfiles.const.COMPOUND_HEADER.pack(*V3_HEADER) + struct.pack('<109L', *master_fat)) with pytest.raises(compoundfiles.CompoundFileMasterFatWarning): compoundfiles.CompoundFileReader(MagicMock())
def test_function_sample1_xls(): with compoundfiles.CompoundFileReader('tests/sample1.xls') as doc: contents = ( DirEntry('Workbook', True, 11073), DirEntry('\x05SummaryInformation', True, 4096), DirEntry('\x05DocumentSummaryInformation', True, 4096), ) verify_contents(doc, contents)
def parseStickNotes(self): output = self.volumeInfo result = [] try: bias = datetime.timedelta(hours=-self.bias) except TypeError: pass if "FAT" or "NTFS" in output.split(" ")[0]: os.chdir("%s/%s/" % (self.mountDir, output.split(" ")[2])) logger.info( "Loading every user info!") # TODO:It should be per user! try: os.chdir("Users/") except FileNotFoundError: logger.error("Couldn't find Users folder!") return None for userDir in os.listdir("."): if os.access( "{0}/AppData/Roaming/Microsoft/Sticky Notes/StickyNotes.snt" .format(userDir), os.F_OK | os.R_OK): pass else: logger.warning("Couldn't find StickNotes file on %s" % userDir) continue doc = compoundfiles.CompoundFileReader( "{0}/AppData/Roaming/Microsoft/Sticky Notes/StickyNotes.snt" .format(userDir)) for item in doc: if item.isdir: logger.info("Directory name: {0}.".format(item.name)) logger.info( "Directory last modified time: {0}.".format( item.modified)) for sub_item in item: content = doc.open(sub_item).read() logger.info("Entry name: {0}.".format( sub_item.name)) if "Rich Text" in magic.from_buffer(content): logger.debug( "This is an RTF file.Stripping to normal text." ) logger.info("Entry content: {0}.".format( rtf_to_text(content.decode()))) else: logger.info("Entry type: {0}.".format( magic.from_buffer( doc.open(sub_item).read()))) logger.info( "Entry content: {0}.".format(content)) elif item.isfile: logger.info("Entry name: {0}.".format(item.name)) logger.info("Entry content: {0}.".format( doc.open(item).read())) logger.info("Entry type: {0}.".format( magic.from_buffer(doc.open(item).read()))) else: continue
def test_invalid_dir_sector_count(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with dir sector count filled in (not allowed in # v3 file); reader ignores the value in both v3 and v4 anyway with cf.CompoundFileReader( 'tests/invalid_dir_sector_count.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileHeaderWarning) assert len(w) == 1 verify_example(doc)
def test_strange_sector_size_v3(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with sector size re-written to 1024 and file # content padded out accordingly; reader warns out this but carries # on anyway with cf.CompoundFileReader('tests/strange_sector_size_v3.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileSectorSizeWarning) assert len(w) == 1 verify_example(doc)
def test_strange_sector_size_v4(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with DLL version set to 4 and dir sector count # filled in (required in v4 files); reader warns about unusual # sector size by carries on anyway with cf.CompoundFileReader('tests/strange_sector_size_v4.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileSectorSizeWarning) assert len(w) == 1 verify_example(doc)
def test_strange_master_ext(): # Same as example.dat with FAT extended to include enough blank sectors to # necessitate a DIFAT extension (e.g. a compound document where lots of # contents have been deleted and the FAT hasn't been compressed but simply # overwritten) with warnings.catch_warnings(record=True) as w: with cf.CompoundFileReader('tests/strange_master_ext.dat') as doc: assert len(w) == 0 verify_example(doc)
def test_strange_mini_sector_size(): with warnings.catch_warnings(record=True) as w: # Same as example.dat with mini sector size set to 128 and mini FAT # re-written accordingly with cf.CompoundFileReader( 'tests/strange_mini_sector_size.dat') as doc: assert issubclass(w[0].category, cf.CompoundFileSectorSizeWarning) assert len(w) == 1 verify_example(doc)