Skip to content

Commit

Permalink
Added support for simple macros
Browse files Browse the repository at this point in the history
Signed-off-by: Cervenka Dusan <cervenka@acrios.com>
  • Loading branch information
Hadatko committed Apr 6, 2022
1 parent edb3276 commit b68caf0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 12 additions & 3 deletions dissect/cstruct/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def parse(self, data):
self._constants(data)
self._enums(data)
self._structs(data)
self._lookups(data, self.cstruct.consts)
self._lookups(data)

def _constants(self, data):
r = re.finditer(r'#define\s+(?P<name>[^\s]+)\s+(?P<value>[^\r\n]+)\s*\n', data)
Expand Down Expand Up @@ -375,7 +375,7 @@ def _parse_fields_enums(self, s):
commentAttributes = self.parse_comment_block(d['commentBlock'])

field = re.finditer(
r'(?P<key>[a-zA-z][^ =]*)[ ]*=?[ ]*(?P<value>[^ ]+)?',
r'(?P<key>[a-zA-z][^ =]*)[ ]*=?[ ]*(?P<value>[^,/]+)?',
d["value"],
)

Expand Down Expand Up @@ -484,7 +484,7 @@ def parse_comment_block(self,s):

return commentAttributes

def _lookups(self, data, consts):
def _lookups(self, data):
r = re.finditer(r'\$(?P<name>[^\s]+) = ({[^}]+})\w*\n', data)

for t in r:
Expand All @@ -493,6 +493,15 @@ def _lookups(self, data, consts):
[(self.cstruct.consts[k], v) for k, v in d.items()]
)

#support simple defines
r = re.finditer(r'^[ \t]*#define (?P<name>[^\s]+)[ \t]+(?P<value>\(?-?[0-9]+U?\)?)\n?', data)

for t in r:
d = ast.literal_eval(t.group(2))
self.cstruct.lookups[t.group(1)] = dict(
[(self.cstruct.consts[k], v) for k, v in d.items()]
)


class Instance(object):
"""Holds parsed structure data."""
Expand Down
4 changes: 3 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ def test_bytes_integer_signed_be():

def test_enum():
d = """
#define MyEnumVal 4
enum Test16 : uint16 {
A = 0x1,
B = 0x2
B = 0x6 - MyEnumVal
};
enum Test24 : uint24 {
Expand Down

0 comments on commit b68caf0

Please sign in to comment.