Commit 99b8048f by heffnercj

Basic scan runs in python3, but no signatures are loaded

parent 60b3873b
......@@ -183,7 +183,7 @@ class Binwalk(object):
# Parse the magic file(s) and initialize libmagic
self.mfile = self.parser.parse(self.magic_files)
self.magic = magic.open(self.flags)
self.magic.load(self.mfile)
self.magic.load(str2bytes(self.mfile))
# Once the temporary magic file is loaded into libmagic, we don't need it anymore; delete the temp file
self.parser.rm_magic_file()
......@@ -610,7 +610,7 @@ class Binwalk(object):
callback(results_offset, results)
# If a relative jump offset was specified, update the absolute jump_offset variable
if smart.has_key('jump') and smart['jump'] > 0:
if has_key(smart, 'jump') and smart['jump'] > 0:
jump_offset = results_offset + smart['jump']
# Track the total number of bytes scanned
......@@ -621,7 +621,7 @@ class Binwalk(object):
offset = 0
# Sort the results before returning them
scan_items = scan_results.items()
scan_items = list(scan_results.items())
scan_items.sort()
# Do delayed extraction, if specified.
......@@ -654,7 +654,7 @@ class Binwalk(object):
Returns None.
'''
for (new_file_name, new_data) in iterator(new):
if not results.has_key(new_file_name):
if not has_key(results, new_file_name):
results[new_file_name] = new_data
else:
for i in range(0, len(new_data)):
......
......@@ -10,11 +10,39 @@ if sys.version_info.major > 2:
else:
import urllib2
def iterator(obj):
def iterator(dictionary):
'''
For cross compatibility between Python 2 and Python 3 dictionaries.
'''
if sys.version_info.major > 2:
return obj.items()
return dictionary.items()
else:
return obj.iteritems()
return dictionary.iteritems()
def has_key(dictionary, key):
'''
For cross compatibility between Python 2 and Python 3 dictionaries.
'''
if sys.version_info.major > 2:
return key in dictionary
else:
return dictionary.has_key(key)
def str2bytes(string):
'''
For cross compatibility between Python 2 and Python 3 strings.
'''
if sys.version_info.major > 2:
return bytes(string, 'utf-8')
else:
return string
def string_decode(string):
'''
For cross compatibility between Python 2 and Python 3 strings.
'''
if sys.version_info.major > 2:
return bytes(string, 'utf-8').decode('unicode_escape')
else:
return string.decode('string_escape')
......@@ -52,7 +52,7 @@ class PlotEntropy(object):
label = None
description = results[0]['description'].split(',')[0]
if not color_mappings.has_key(description):
if not has_key(color_mappings, description):
if show_legend:
label = description
......
import io
import re
import os.path
import tempfile
......@@ -139,7 +140,7 @@ class MagicParser:
line_count = 0
try:
for line in open(file_name).readlines():
for line in io.FileIO(file_name).readlines():
line_count += 1
# Check if this is the first line of a signature entry
......@@ -152,7 +153,7 @@ class MagicParser:
include = True
self.signature_count += 1
if not self.signatures.has_key(entry['offset']):
if not has_key(self.signatures, entry['offset']):
self.signatures[entry['offset']] = []
if entry['condition'] not in self.signatures[entry['offset']]:
......@@ -190,7 +191,7 @@ 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 line[:1] < '0' or line[:1] > '9':
if str(line[:1]) < '0' or str(line[:1]) > '9':
return None
try:
......@@ -201,7 +202,7 @@ class MagicParser:
entry['offset'] = line_parts[0]
entry['type'] = line_parts[1]
# The condition line may contain escaped sequences, so be sure to decode it properly.
entry['condition'] = line_parts[2].decode('string_escape')
entry['condition'] = string_decode(line_parts[2])
entry['description'] = ' '.join(line_parts[3:])
except Exception as e:
raise Exception("%s :: %s", (str(e), line))
......
......@@ -170,7 +170,7 @@ class SmartSignature:
'''
arg = ''
if self.KEYWORDS.has_key(keyword) and self.KEYWORDS[keyword] in data:
if has_key(self.KEYWORDS, keyword) and self.KEYWORDS[keyword] in data:
arg = data.split(self.KEYWORDS[keyword])[1].split(self.KEYWORD_DELIM_END)[0]
return arg
......
......@@ -109,7 +109,7 @@ class FileStrings(object):
offset = self.total_read + self.start
# Ignore blocks which have a higher than average or higher than MAX_ENTROPY entropy
while self.entropy.has_key(offset):
while has_key(self.entropy, offset):
# Don't ignore blocks that border on an entropy rising/falling edge
try:
if self.entropy[offset-self.block] <= self.MAX_ENTROPY:
......@@ -179,7 +179,7 @@ class FileStrings(object):
Returns True if bracketed, False if not.
'''
return self.BRACKETED.has_key(data[0]) and data.endswith(self.BRACKETED[data[0]])
return has_key(self.BRACKETED, data[0]) and data.endswith(self.BRACKETED[data[0]])
def _non_alpha_count(self, data):
'''
......
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