Skip to content

Commit

Permalink
Code formatting, removal of unused code.
Browse files Browse the repository at this point in the history
  • Loading branch information
devttys0 committed Sep 14, 2017
1 parent e7043ba commit 51f2dc4
Show file tree
Hide file tree
Showing 29 changed files with 141 additions and 1,356 deletions.
173 changes: 0 additions & 173 deletions src/binwalk/core/C.py

This file was deleted.

15 changes: 7 additions & 8 deletions src/binwalk/core/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ def header(self, *args, **kwargs):
self._fprint(
"MD5 Checksum: %s\n", [md5sum], csv=False, filter=False)
if self.custom_verbose_format and self.custom_verbose_args:
self._fprint(
self.custom_verbose_format, self.custom_verbose_args, csv=False, filter=False)
self._fprint(self.custom_verbose_format,
self.custom_verbose_args,
csv=False,
filter=False)

self._fprint("%s", "\n", csv=False, filter=False)
self._fprint(self.header_format, args, filter=False)
self._fprint(
"%s", ["-" * self.HEADER_WIDTH + "\n"], csv=False, filter=False)
self._fprint("%s", ["-" * self.HEADER_WIDTH + "\n"], csv=False, filter=False)

def result(self, *args):
# Convert to list for item assignment
Expand Down Expand Up @@ -218,8 +219,7 @@ def _format_line(self, line):

# Add any remaining data (guarunteed to be max_line_wrap_length
# long or shorter) to self.string_parts
self._append_to_data_parts(
line, offset, offset + len(line[offset:]))
self._append_to_data_parts(line, offset, offset + len(line[offset:]))

# Append self.string_parts to formatted_line; each part seperated
# by delim
Expand All @@ -244,8 +244,7 @@ def _configure_formatting(self):
import termios

# Get the terminal window width
hw = struct.unpack(
'hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))
hw = struct.unpack('hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))
self.SCREEN_WIDTH = self.HEADER_WIDTH = hw[1]
except KeyboardInterrupt as e:
raise e
Expand Down
63 changes: 25 additions & 38 deletions src/binwalk/core/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,11 @@ def __init__(self, line):
except KeyboardInterrupt as e:
raise e
except Exception as e:
raise ParserException(
"Failed to expand string '%s' with integer '%s' in line '%s'" % (self.value, n, line))
raise ParserException("Failed to expand string '%s' with integer '%s' in line '%s'" % (self.value, n, line))
try:
self.value = binwalk.core.compat.string_decode(self.value)
except ValueError as e:
raise ParserException(
"Failed to decode string value '%s' in line '%s'" % (self.value, line))
raise ParserException("Failed to decode string value '%s' in line '%s'" % (self.value, line))
# If a regex was specified, compile it
elif self.type == 'regex':
self.regex = True
Expand All @@ -180,21 +178,18 @@ def __init__(self, line):
except KeyboardInterrupt as e:
raise e
except Exception as e:
raise ParserException(
"Invalid regular expression '%s': %s" % (self.value, str(e)))
raise ParserException("Invalid regular expression '%s': %s" % (self.value, str(e)))
# Non-string types are integer values
else:
try:
self.value = int(self.value, 0)
except ValueError as e:
raise ParserException(
"Failed to convert value '%s' to an integer on line '%s'" % (self.value, line))
raise ParserException("Failed to convert value '%s' to an integer on line '%s'" % (self.value, line))

# Sanity check to make sure the first line of a signature has an
# explicit value
if self.level == 0 and self.value is None:
raise ParserException(
"First element of a signature must specify a non-wildcard value: '%s'" % (line))
raise ParserException("First element of a signature must specify a non-wildcard value: '%s'" % (line))

# Set the size and struct format value for the specified data type.
# This must be done, obviously, after the value has been parsed out
Expand Down Expand Up @@ -232,8 +227,7 @@ def __init__(self, line):
self.fmt = 'i'
self.size = 4
else:
raise ParserException(
"Unknown data type '%s' in line '%s'" % (self.type, line))
raise ParserException("Unknown data type '%s' in line '%s'" % (self.type, line))

# The struct module uses the same characters for specifying signed and unsigned data types,
# except that signed data types are upper case. The above if-else code sets self.fmt to the
Expand Down Expand Up @@ -374,8 +368,7 @@ def _generate_regex(self, line):
if not binwalk.core.compat.has_key(line.tags, 'overlap'):
for i in range(1, line.size):
if restr[i:] == restr[0:(line.size - i)]:
binwalk.core.common.warning(
"Signature '%s' is a self-overlapping signature!" % line.text)
binwalk.core.common.warning("Signature '%s' is a self-overlapping signature!" % line.text)
break

return re.compile(re.escape(restr))
Expand Down Expand Up @@ -492,8 +485,7 @@ def _do_math(self, offset, expression):
s = expression[:period].rfind('(') + 1
# The offset address may be an evaluatable expression, such as '(4+0.L)', typically the result
# of the original offset being something like '(&0.L)'.
o = binwalk.core.common.MathExpression(
expression[s:period]).value
o = binwalk.core.common.MathExpression(expression[s:period]).value
t = expression[period + 1]

# Re-build just the parsed offset portion of the expression
Expand All @@ -512,24 +504,19 @@ def _do_math(self, offset, expression):
try:
# Big and little endian byte format
if t in ['b', 'B']:
v = struct.unpack(
'b', binwalk.core.compat.str2bytes(self.data[o:o + 1]))[0]
v = struct.unpack('b', binwalk.core.compat.str2bytes(self.data[o:o + 1]))[0]
# Little endian short format
elif t == 's':
v = struct.unpack(
'<h', binwalk.core.compat.str2bytes(self.data[o:o + 2]))[0]
v = struct.unpack('<h', binwalk.core.compat.str2bytes(self.data[o:o + 2]))[0]
# Little endian long format
elif t == 'l':
v = struct.unpack(
'<i', binwalk.core.compat.str2bytes(self.data[o:o + 4]))[0]
v = struct.unpack('<i', binwalk.core.compat.str2bytes(self.data[o:o + 4]))[0]
# Big endian short format
elif t == 'S':
v = struct.unpack(
'>h', binwalk.core.compat.str2bytes(self.data[o:o + 2]))[0]
v = struct.unpack('>h', binwalk.core.compat.str2bytes(self.data[o:o + 2]))[0]
# Bit endian long format
elif t == 'L':
v = struct.unpack(
'>i', binwalk.core.compat.str2bytes(self.data[o:o + 4]))[0]
v = struct.unpack('>i', binwalk.core.compat.str2bytes(self.data[o:o + 4]))[0]
# struct.error is thrown if there is not enough bytes in
# self.data for the specified format type
except struct.error as e:
Expand Down Expand Up @@ -589,15 +576,13 @@ def _analyze(self, signature, offset):
ple = '%d+' % previous_line_end
# Allow users to use either the '&0' (libmagic) or '&+0' (explcit addition) sytaxes;
# replace both with the ple text.
line_offset_text = line.offset.replace(
'&+', ple).replace('&', ple)
line_offset_text = line.offset.replace('&+', ple).replace('&', ple)
# Evaluate the expression
line_offset = self._do_math(offset, line_offset_text)

# Sanity check
if not isinstance(line_offset, int):
raise ParserException(
"Failed to convert offset '%s' to a number: '%s'" % (line.offset, line.text))
raise ParserException("Failed to convert offset '%s' to a number: '%s'" % (line.offset, line.text))

# The start of the data needed by this line is at offset + line_offset.
# The end of the data will be line.size bytes later.
Expand All @@ -607,8 +592,7 @@ def _analyze(self, signature, offset):
# If the line has a packed format string, unpack it
if line.pkfmt:
try:
dvalue = struct.unpack(
line.pkfmt, binwalk.core.compat.str2bytes(self.data[start:end]))[0]
dvalue = struct.unpack(line.pkfmt, binwalk.core.compat.str2bytes(self.data[start:end]))[0]
# Not enough bytes left in self.data for the specified
# format size
except struct.error as e:
Expand All @@ -624,8 +608,7 @@ def _analyze(self, signature, offset):
# Else, just terminate the string at the first newline,
# carriage return, or NULL byte
else:
dvalue = self.data[start:end].split(
'\x00')[0].split('\r')[0].split('\n')[0]
dvalue = self.data[start:end].split('\x00')[0].split('\r')[0].split('\n')[0]
# Non-wildcard strings have a known length, specified in
# the signature line
else:
Expand Down Expand Up @@ -665,8 +648,13 @@ def _analyze(self, signature, offset):
except KeyboardInterrupt as e:
raise e
except Exception as e:
raise ParserException("Operation '" + str(dvalue) + " " + str(
line.operator) + "= " + str(line.opvalue) + "' failed: " + str(e))
raise ParserException("Operation '" +
str(dvalue) +
" " +
str(line.operator) +
"= " +
str(line.opvalue) +
"' failed: " + str(e))

# Does the data (dvalue) match the specified comparison?
if ((line.value is None) or
Expand Down Expand Up @@ -896,8 +884,7 @@ def parse(self, lines):
# existing signature entry, something is very wrong with the
# signature file.
else:
raise ParserException(
"Invalid signature line: '%s'" % line)
raise ParserException("Invalid signature line: '%s'" % line)

# Add the final signature to the signature list
if signature:
Expand Down
Loading

0 comments on commit 51f2dc4

Please sign in to comment.