From d19ff934e6b2e84b461370fa7edc8799a461991c Mon Sep 17 00:00:00 2001
From: devttys0 <heffnercj@gmail.com>
Date: Sun, 22 Dec 2013 07:51:21 -0500
Subject: [PATCH] Added verbose output to preceed scans.

---
 setup.py                         |  5 ++++-
 src/binwalk/core/display.py      | 45 ++++++++++++++++++++++++++++++++++++++-------
 src/binwalk/core/module.py       | 17 ++++++++++++++---
 src/binwalk/modules/signature.py |  4 ++++
 src/binwalk/plugins/zlibvalid.py |  3 ++-
 5 files changed, 62 insertions(+), 12 deletions(-)

diff --git a/setup.py b/setup.py
index 4913949..d3d1a36 100755
--- a/setup.py
+++ b/setup.py
@@ -30,9 +30,12 @@ def cleanup_module_directory():
 					remove_tree(path + os.path.sep + "*")
 				except OSError as e:
 					pass
-		except ImportError:
+		except KeyboardInterrupt as e:
+			raise e
+		except Exception:
 			pass
 
+
 # Change to the binwalk src directory
 def warning(lines, terminate=True, prompt=True):
 	WIDTH = 115
diff --git a/src/binwalk/core/display.py b/src/binwalk/core/display.py
index e06d081..a7cba36 100644
--- a/src/binwalk/core/display.py
+++ b/src/binwalk/core/display.py
@@ -1,5 +1,8 @@
 import sys
 import csv as pycsv
+import datetime
+import binwalk.core.common
+from binwalk.core.compat import *
 
 class Display(object):
 
@@ -15,6 +18,8 @@ class Display(object):
 		self.fp = None
 		self.csv = None
 		self.num_columns = 0
+		self.custom_verbose_format = ""
+		self.custom_verbose_args = []
 
 		self._configure_formatting()
 
@@ -37,8 +42,33 @@ class Display(object):
 			else:
 				self.fp.write(fmt % tuple(columns))
 
-	def header(self, *args):
+	def add_custom_header(self, fmt, args):
+		self.custom_verbose_format = fmt
+		self.custom_verbose_args = args
+
+	def header(self, *args, **kwargs):
+		file_name = None
 		self.num_columns = len(args)
+
+		if has_key(kwargs, 'file_name'):
+			file_name = kwargs['file_name']
+
+		if self.verbose and file_name:
+			md5sum = binwalk.core.common.file_md5(file_name)
+			timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+
+			if self.csv:
+				self.log("", ["FILE", "MD5SUM", "TIMESTAMP"])
+				self.log("", [file_name, md5sum, timestamp])
+
+			self._fprint("%s", "\n", csv=False)
+			self._fprint("Scan Time:     %s\n", [timestamp], csv=False)
+			self._fprint("Target File:   %s\n", [file_name], csv=False)
+			self._fprint("MD5 Checksum:  %s\n", [md5sum], csv=False)
+			if self.custom_verbose_format and self.custom_verbose_args:
+				#self._pprint("Signatures:    %d\n" % self.binwalk.parser.signature_count, nolog=nolog)
+				self._fprint(self.custom_verbose_format, self.custom_verbose_args, csv=False)
+
 		self._fprint("%s", "\n", csv=False)
 		self._fprint(self.header_format, args)
 		self._fprint("%s", ["-" * self.HEADER_WIDTH + "\n"], csv=False)
@@ -59,14 +89,15 @@ class Display(object):
 	def footer(self):
 		self._fprint("%s", "\n", csv=False)
 
-	def _fprint(self, fmt, columns, csv=True):
-		if not self.quiet:
-			line = fmt % tuple(columns)
-			if filter and self.filter.valid_result(line):
+	def _fprint(self, fmt, columns, csv=True, stdout=True):
+		line = fmt % tuple(columns)
+		
+		if filter and self.filter.valid_result(line):
+			if not self.quiet and stdout:
 				sys.stdout.write(self._format_line(line.strip()) + "\n")
 
-		if self.fp and not (self.csv and not csv):
-			self.log(fmt, columns)
+			if self.fp and not (self.csv and not csv):
+				self.log(fmt, columns)
 
 	def _append_to_data_parts(self, data, start, end):
 		'''
diff --git a/src/binwalk/core/module.py b/src/binwalk/core/module.py
index 36c986a..bed8c51 100644
--- a/src/binwalk/core/module.py
+++ b/src/binwalk/core/module.py
@@ -165,6 +165,9 @@ class Module(object):
 	#RESULT = ['offset', 'description']
 	RESULT = ["offset", "offset", "description"]
 
+	VERBOSE_HEADER_FORMAT = ""
+	VERBOSE_HEADER_ARGS = []
+
 	# If set to True, the progress status will be automatically updated for each result
 	# containing a valid file attribute.
 	AUTO_UPDATE_STATUS = True
@@ -184,6 +187,7 @@ class Module(object):
 		self.target_file_list = []
 		self.status = None
 		self.enabled = False
+		self.current_target_file_name = None
 		self.name = self.__class__.__name__
 		self.plugins = binwalk.core.plugin.Plugins(self)
 
@@ -303,7 +307,12 @@ class Module(object):
 			fp = self.target_file_list.pop(0)
 			self.status.clear()
 			self.status.total = fp.length
-		
+
+		if fp is not None:
+			self.current_target_file_name = fp.name	
+		else:
+			self.current_target_file_name = None
+
 		return fp
 
 	def clear(self, results=True, errors=True):
@@ -382,10 +391,12 @@ class Module(object):
 
 	def header(self):
 		self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)
+		self.config.display.add_custom_header(self.VERBOSE_HEADER_FORMAT, self.VERBOSE_HEADER_ARGS)
+
 		if type(self.HEADER) == type([]):
-			self.config.display.header(*self.HEADER)
+			self.config.display.header(*self.HEADER, file_name=self.current_target_file_name)
 		elif self.HEADER:
-			self.config.display.header(self.HEADER)
+			self.config.display.header(self.HEADER, file_name=self.current_target_file_name)
 	
 	def footer(self):
 		self.config.display.footer()
diff --git a/src/binwalk/modules/signature.py b/src/binwalk/modules/signature.py
index 6c2a96e..8280251 100644
--- a/src/binwalk/modules/signature.py
+++ b/src/binwalk/modules/signature.py
@@ -45,6 +45,8 @@ class Signature(Module):
 
 	MAGIC_FLAGS = magic.MAGIC_NO_CHECK_TEXT | magic.MAGIC_NO_CHECK_ENCODING | magic.MAGIC_NO_CHECK_APPTYPE | magic.MAGIC_NO_CHECK_TOKENS
 
+	VERBOSE_HEADER_FORMAT = "%s    %d"
+
 	def init(self):
 		# Create SmartSignature and MagicParser class instances. These are mostly for internal use.
 		self.smart = binwalk.core.smart.SmartSignature(self.config.filter, ignore_smart_signatures=self.dumb_scan)
@@ -76,6 +78,8 @@ class Signature(Module):
 		# Once the temporary magic files are loaded into libmagic, we don't need them anymore; delete the temp files
 		self.parser.rm_magic_files()
 
+		self.VERBOSE_HEADER_ARGS = ["Signatures:", self.parser.signature_count]
+
 	def validate(self, r):
 		'''
 		Called automatically by self.result.
diff --git a/src/binwalk/plugins/zlibvalid.py b/src/binwalk/plugins/zlibvalid.py
index bea70b9..e80b3c6 100644
--- a/src/binwalk/plugins/zlibvalid.py
+++ b/src/binwalk/plugins/zlibvalid.py
@@ -23,7 +23,8 @@ class Plugin(object):
 		# If this result is a zlib signature match, try to decompress the data
 		if self.tinfl and result.file and result.description.lower().startswith('zlib'):
 			# Seek to and read the suspected zlib data
-			fd = BlockFile(result.file.name, offset=result.offset, swap=self.module.config.swap_size)
+			fd = self.module.config.open_file(result.file.name, offset=result.offset)
+			#BlockFile(result.file.name, offset=result.offset, swap=self.module.config.swap_size)
 			data = fd.read(self.MAX_DATA_SIZE)
 			fd.close()
 
--
libgit2 0.26.0