Commit b8d6b8d9 by devttys0

Added support for complex operators in type fields

parent 5a6a8ad0
......@@ -60,7 +60,10 @@ class SignatureLine(object):
if operator in parts[1]:
(self.type, self.opvalue) = parts[1].split(operator, 1)
self.operator = operator
try:
self.opvalue = int(self.opvalue, 0)
except ValueError as e:
pass
break
if parts[2][0] in ['=', '!', '>', '<', '&', '|']:
......@@ -241,20 +244,10 @@ class Magic(object):
return filtered
def parse(self, signature, offset):
description = []
tag_strlen = None
max_line_level = 0
tags = {'id' : signature.id, 'offset' : offset, 'invalid' : False}
for line in signature.lines:
if line.level <= max_line_level:
if isinstance(line.offset, int):
line_offset = line.offset
else:
def do_math(self, offset, expression):
# (4.l+12)
if '.' in line.offset:
(o, t) = line.offset.split('.', 1)
if '.' in expression:
(o, t) = expression.split('.', 1)
o = offset + int(o.split('(', 1)[1], 0)
t = t[0]
......@@ -272,13 +265,26 @@ class Magic(object):
except struct.error as e:
v = 0
v = "(%d%s" % (v, line.offset.split(t, 1)[1])
v = "(%d%s" % (v, expression.split(t, 1)[1])
# (32+0x20)
else:
v = line.offset
v = expression
#print ("Converted offset '%s' to '%s'" % (line.offset, v))
line_offset = binwalk.core.common.MathExpression(v).value
#print ("Converted offset '%s' to '%s'" % (expression, v))
return binwalk.core.common.MathExpression(v).value
def parse(self, signature, offset):
description = []
tag_strlen = None
max_line_level = 0
tags = {'id' : signature.id, 'offset' : offset, 'invalid' : False}
for line in signature.lines:
if line.level <= max_line_level:
if isinstance(line.offset, int):
line_offset = line.offset
else:
line_offset = self.do_math(offset, line.offset)
start = offset + line_offset
end = start + line.size
......@@ -299,18 +305,23 @@ class Magic(object):
dvalue = self.data[start:end]
if isinstance(dvalue, int) and line.operator:
if isinstance(line.opvalue, int):
opval = line.opvalue
else:
opval = self.do_math(offset, line.opvalue)
if line.operator == '&':
dvalue &= line.opvalue
dvalue &= opval
elif line.operator == '|':
dvalue |= line.opvalue
dvalue |= opval
elif line.operator == '*':
dvalue *= line.opvalue
dvalue *= opval
elif line.operator == '+':
dvalue += line.opvalue
dvalue += opval
elif line.operator == '-':
dvalue -= line.opvalue
dvalue -= opval
elif line.operator == '/':
dvalue /= line.opvalue
dvalue /= opval
if ((line.value is None) or
(line.condition == '=' and dvalue == line.value) or
......
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