Commit beb34975 by devttys0

Fixed relative offset support in magic.py

parent 60ec1f43
...@@ -466,11 +466,13 @@ class Magic(object): ...@@ -466,11 +466,13 @@ class Magic(object):
description = [] description = []
tag_strlen = None tag_strlen = None
max_line_level = 0 max_line_level = 0
last_indirect_offset = 0 previous_line_end = 0
tags = {'id' : signature.id, 'offset' : offset, 'invalid' : False} tags = {'id' : signature.id, 'offset' : offset, 'invalid' : False}
# Apply each line of the signature to self.data, starting at the specified offset # Apply each line of the signature to self.data, starting at the specified offset
for line in signature.lines: for n in range(0, len(signature.lines)):
line = signature.lines[n]
# Ignore indentation levels above the current max indent level # Ignore indentation levels above the current max indent level
if line.level <= max_line_level: if line.level <= max_line_level:
# If the relative offset of this signature line is just an integer value, use it # If the relative offset of this signature line is just an integer value, use it
...@@ -480,10 +482,14 @@ class Magic(object): ...@@ -480,10 +482,14 @@ class Magic(object):
else: else:
line_offset = self._do_math(offset, line.offset) line_offset = self._do_math(offset, line.offset)
# If the uplevel delimiter was set in the signature line, add the last indirect # Sanity check
# offset to the line's specified offset. if not isinstance(line_offset, int):
raise ParserException("Failed to convert offset '%s' to a number" % line.offset)
# If the uplevel delimiter was set in the signature line, then the specified offset
# is relative to the end of the last line's data (the '>>&0' offset syntax).
if line.uplevel: if line.uplevel:
line_offset += last_indirect_offset line_offset += previous_line_end
# The start of the data needed by this line is at offset + line_offset. # 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. # The end of the data will be line.size bytes later.
...@@ -600,9 +606,19 @@ class Magic(object): ...@@ -600,9 +606,19 @@ class Magic(object):
if not self.show_invalid and tags['invalid']: if not self.show_invalid and tags['invalid']:
break break
# Track the last indirect offset value # Look ahead to the next line in the signature; if its indent level is greater than
if line.is_indirect_offset: # that of the current line, then track the end of data for the current line. This is
last_indirect_offset = line_offset # so that subsequent lines can use the '>>&0' offset syntax to specify relative offsets
# from previous lines.
try:
next_line = signature.lines[n+1]
if next_line.level > line.level:
if line.type == 'string':
previous_line_end = line_offset + len(dvalue)
else:
previous_line_end = line_offset + line.size
except IndexError as e:
pass
# If this line satisfied its comparison, +1 the max indentation level # If this line satisfied its comparison, +1 the max indentation level
max_line_level = line.level + 1 max_line_level = line.level + 1
......
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