Commit 54bfff03 by devttys0

Initial move of hexdiff.py/plotter.py to modules.

parent ae122ee8
...@@ -212,7 +212,7 @@ class BlockFile(io.FileIO): ...@@ -212,7 +212,7 @@ class BlockFile(io.FileIO):
# limit disk I/O, but small enough to limit the size of processed data blocks. # limit disk I/O, but small enough to limit the size of processed data blocks.
READ_BLOCK_SIZE = 1 * 1024 * 1024 READ_BLOCK_SIZE = 1 * 1024 * 1024
def __init__(self, fname, mode='r', length=0, offset=0): def __init__(self, fname, mode='r', length=0, offset=0, block=READ_BLOCK_SIZE):
''' '''
Class constructor. Class constructor.
...@@ -255,6 +255,9 @@ class BlockFile(io.FileIO): ...@@ -255,6 +255,9 @@ class BlockFile(io.FileIO):
elif self.length > self.size: elif self.length > self.size:
self.length = self.size self.length = self.size
if block > 0:
self.READ_BLOCK_SIZE = block
io.FileIO.__init__(self, fname, mode) io.FileIO.__init__(self, fname, mode)
# Work around for python 2.6 where FileIO._name is not defined # Work around for python 2.6 where FileIO._name is not defined
......
...@@ -57,7 +57,6 @@ class Plotter(object): ...@@ -57,7 +57,6 @@ class Plotter(object):
binwalk.module.process_kwargs(self, kwargs) binwalk.module.process_kwargs(self, kwargs)
self.verbose = self.config.verbose self.verbose = self.config.verbose
self.files = self.config.target_files
self.offset = self.config.offset self.offset = self.config.offset
self.length = self.config.length self.length = self.config.length
self.plane_count = -1 self.plane_count = -1
...@@ -79,8 +78,8 @@ class Plotter(object): ...@@ -79,8 +78,8 @@ class Plotter(object):
self.window = gl.GLViewWidget() self.window = gl.GLViewWidget()
self.window.opts['distance'] = self.VIEW_DISTANCE self.window.opts['distance'] = self.VIEW_DISTANCE
if len(self.files) == 1: if len(self.target_files) == 1:
self.window.setWindowTitle(self.files[0]) self.window.setWindowTitle(self.target_files[0].name)
def _print(self, message): def _print(self, message):
''' '''
...@@ -159,20 +158,20 @@ class Plotter(object): ...@@ -159,20 +158,20 @@ class Plotter(object):
''' '''
return (0,0,0) return (0,0,0)
def _generate_data_points(self, file_name): def _generate_data_points(self, fp):
''' '''
Generates a dictionary of data points and their frequency of occurrance. Generates a dictionary of data points and their frequency of occurrance.
@file_name - The file to generate data points from. @fp - The BlockFile object to generate data points from.
Returns a dictionary. Returns a dictionary.
''' '''
i = 0 i = 0
data_points = {} data_points = {}
self._print("Generating data points for %s" % file_name) self._print("Generating data points for %s" % fp.name)
with BlockFile(file_name, 'r', offset=self.offset, length=self.length) as fp: # We don't need any extra data from BlockFile
fp.MAX_TRAILING_SIZE = 0 fp.MAX_TRAILING_SIZE = 0
while True: while True:
...@@ -255,8 +254,8 @@ class Plotter(object): ...@@ -255,8 +254,8 @@ class Plotter(object):
ygrid.scale(12.8, 12.8, 12.8) ygrid.scale(12.8, 12.8, 12.8)
zgrid.scale(12.8, 12.8, 12.8) zgrid.scale(12.8, 12.8, 12.8)
for file_name in self.files: for fd in self.target_files:
data_points = self._generate_data_points(file_name) data_points = self._generate_data_points(fd)
self._print("Generating plot points from %d data points" % len(data_points)) self._print("Generating plot points from %d data points" % len(data_points))
......
...@@ -102,7 +102,7 @@ class Configuration(object): ...@@ -102,7 +102,7 @@ class Configuration(object):
Update(self.verbose).update() Update(self.verbose).update()
sys.exit(0) sys.exit(0)
self._test_target_files() self._open_target_files()
self._set_verbosity() self._set_verbosity()
self.display = binwalk.display.Display(log=self.log_file, self.display = binwalk.display.Display(log=self.log_file,
...@@ -128,7 +128,7 @@ class Configuration(object): ...@@ -128,7 +128,7 @@ class Configuration(object):
Checks if the target files can be opened. Checks if the target files can be opened.
Any files that cannot be opened are removed from the self.target_files list. Any files that cannot be opened are removed from the self.target_files list.
''' '''
failed_open_count = 0 open_files = []
# Validate the target files listed in target_files # Validate the target files listed in target_files
for tfile in self.target_files: for tfile in self.target_files:
...@@ -136,20 +136,20 @@ class Configuration(object): ...@@ -136,20 +136,20 @@ class Configuration(object):
if not os.path.isdir(tfile): if not os.path.isdir(tfile):
# Make sure we can open the target files # Make sure we can open the target files
try: try:
fd = open(tfile, "rb") open_files.append(BlockFile(tfile, length=self.length, offset=self.offset))
fd.close()
except Exception as e: except Exception as e:
sys.stderr.write("Cannot open file : %s\n" % str(e)) sys.stderr.write("Cannot open file : %s\n" % str(e))
self.target_files.pop(self.target_files.index(tfile))
failed_open_count += 1
# Unless -O was specified, don't run the scan unless we are able to scan all specified files # Unless -O was specified, don't run the scan unless we are able to scan all specified files
if failed_open_count > 0 and not self.skip_unopened: if len(open_files) != len(self.target_files) and not self.skip_unopened:
failed_open_count = len(self.target_files) - len(open_files)
if failed_open_count > 1: if failed_open_count > 1:
plural = 's' plural = 's'
else: else:
plural = '' plural = ''
raise Exception("Failed to open %d file%s for scanning" % (failed_open_count, plural)) raise Exception("Failed to open %d file%s for scanning" % (failed_open_count, plural))
else:
self.target_files = open_files
class Update(object): class Update(object):
''' '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment