Commit 54bfff03 by devttys0

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

parent ae122ee8
......@@ -212,7 +212,7 @@ class BlockFile(io.FileIO):
# limit disk I/O, but small enough to limit the size of processed data blocks.
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.
......@@ -255,6 +255,9 @@ class BlockFile(io.FileIO):
elif self.length > self.size:
self.length = self.size
if block > 0:
self.READ_BLOCK_SIZE = block
io.FileIO.__init__(self, fname, mode)
# Work around for python 2.6 where FileIO._name is not defined
......
......@@ -57,7 +57,6 @@ class Plotter(object):
binwalk.module.process_kwargs(self, kwargs)
self.verbose = self.config.verbose
self.files = self.config.target_files
self.offset = self.config.offset
self.length = self.config.length
self.plane_count = -1
......@@ -79,8 +78,8 @@ class Plotter(object):
self.window = gl.GLViewWidget()
self.window.opts['distance'] = self.VIEW_DISTANCE
if len(self.files) == 1:
self.window.setWindowTitle(self.files[0])
if len(self.target_files) == 1:
self.window.setWindowTitle(self.target_files[0].name)
def _print(self, message):
'''
......@@ -159,35 +158,35 @@ class Plotter(object):
'''
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.
@file_name - The file to generate data points from.
@fp - The BlockFile object to generate data points from.
Returns a dictionary.
'''
i = 0
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:
fp.MAX_TRAILING_SIZE = 0
# We don't need any extra data from BlockFile
fp.MAX_TRAILING_SIZE = 0
while True:
(data, dlen) = fp.read_block()
if not data or not dlen:
break
while True:
(data, dlen) = fp.read_block()
if not data or not dlen:
break
i = 0
while (i+(self.axis-1)) < dlen:
point = self._generate_data_point(data[i:i+self.axis])
if has_key(data_points, point):
data_points[point] += 1
else:
data_points[point] = 1
i += 3
i = 0
while (i+(self.axis-1)) < dlen:
point = self._generate_data_point(data[i:i+self.axis])
if has_key(data_points, point):
data_points[point] += 1
else:
data_points[point] = 1
i += 3
return data_points
......@@ -255,8 +254,8 @@ class Plotter(object):
ygrid.scale(12.8, 12.8, 12.8)
zgrid.scale(12.8, 12.8, 12.8)
for file_name in self.files:
data_points = self._generate_data_points(file_name)
for fd in self.target_files:
data_points = self._generate_data_points(fd)
self._print("Generating plot points from %d data points" % len(data_points))
......
......@@ -102,7 +102,7 @@ class Configuration(object):
Update(self.verbose).update()
sys.exit(0)
self._test_target_files()
self._open_target_files()
self._set_verbosity()
self.display = binwalk.display.Display(log=self.log_file,
......@@ -128,7 +128,7 @@ class Configuration(object):
Checks if the target files can be opened.
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
for tfile in self.target_files:
......@@ -136,20 +136,20 @@ class Configuration(object):
if not os.path.isdir(tfile):
# Make sure we can open the target files
try:
fd = open(tfile, "rb")
fd.close()
open_files.append(BlockFile(tfile, length=self.length, offset=self.offset))
except Exception as 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
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:
plural = 's'
else:
plural = ''
raise Exception("Failed to open %d file%s for scanning" % (failed_open_count, plural))
else:
self.target_files = open_files
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