From 6cf71c2d8655dc6a69b1843e93e3bfe844d32882 Mon Sep 17 00:00:00 2001
From: heffnercj <heffnercj@gmail.com>
Date: Sun, 17 Nov 2013 10:28:18 -0500
Subject: [PATCH] Signatures now loaded, but filters / validators are broken (bytes decoding issues)

---
 src/binwalk/compat.py | 15 ++++++++++++---
 src/binwalk/parser.py |  7 ++++---
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/binwalk/compat.py b/src/binwalk/compat.py
index 1819b7d..e81e322 100644
--- a/src/binwalk/compat.py
+++ b/src/binwalk/compat.py
@@ -32,11 +32,20 @@ def str2bytes(string):
 	'''
 	For cross compatibility between Python 2 and Python 3 strings.
 	'''
-	if sys.version_info.major > 2:
-		return bytes(string, 'utf-8')
+	if isinstance(string, type('')) and sys.version_info.major > 2:
+		return bytes(string, 'ascii')
 	else:
 		return string
-	
+
+def bytes2str(bs):
+	'''
+	For cross compatibility between Python 2 and Python 3 strings.
+	'''
+	if isinstance(bs, type(b'')) and sys.version_info.major > 2:
+		return bs.decode('ascii')
+	else:
+		return bs
+
 def string_decode(string):
 	'''
 	For cross compatibility between Python 2 and Python 3 strings.
diff --git a/src/binwalk/parser.py b/src/binwalk/parser.py
index a3e8c78..1a4468a 100644
--- a/src/binwalk/parser.py
+++ b/src/binwalk/parser.py
@@ -169,7 +169,7 @@ class MagicParser:
 			self.build_signature_set()			
 		except Exception as e:
 			raise Exception("Error parsing magic file '%s' on line %d: %s" % (file_name, line_count, str(e)))
-
+		
 	def _parse_line(self, line):
 		'''
 		Parses a signature line into its four parts (offset, type, condition and description),
@@ -191,14 +191,14 @@ class MagicParser:
 		# Quick and dirty pre-filter. We are only concerned with the first line of a
 		# signature, which will always start with a number. Make sure the first byte of
 		# the line is a number; if not, don't process.
-		if str(line[:1]) < '0' or str(line[:1]) > '9':
+		if bytes2str(line[:1]) < '0' or bytes2str(line[:1]) > '9':
 			return None
 
 		try:
 			# Split the line into white-space separated parts.
 			# For this to work properly, replace escaped spaces ('\ ') with '\x20'.
 			# This means the same thing, but doesn't confuse split().
-			line_parts = line.replace('\\ ', '\\x20').split()
+			line_parts = bytes2str(line).replace('\\ ', '\\x20').split()
 			entry['offset'] = line_parts[0]
 			entry['type'] = line_parts[1]
 			# The condition line may contain escaped sequences, so be sure to decode it properly.
@@ -281,6 +281,7 @@ class MagicParser:
 		Returns an ordered list of offsets inside of data at which candidate offsets were found.
 		'''
 		candidate_offsets = []
+		data = bytes2str(data)
 
 		for regex in self.signature_set:
 			candidate_offsets += [match.start() for match in regex.finditer(data) if match.start() < end]
--
libgit2 0.26.0