Skip to content

Commit

Permalink
Merge pull request #10 from ACRIOS-Systems/feauture/Use_existing_macro
Browse files Browse the repository at this point in the history
Use existing macro
  • Loading branch information
Hadatko authored Nov 14, 2023
2 parents 3d01810 + 6e0924c commit bc202c6
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions dissect/cstruct/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def getStructTypes(self):
return self.getTypes(StructureType)

def getMacroTypes(self):
return self.getTypes(MacroType)
return self.consts


class Parser(object):
Expand Down Expand Up @@ -367,14 +367,13 @@ def clearVersionDetails(self):
self.structVersionDetails = {"name":"", "valB":2**16-1, "valL":0}

def parse(self, data):
self._macros(data)
self._constants(data)
self._enums(data)
self._structs(data)
self._lookups(data)

def _constants(self, data):
r = re.finditer(r'#define\s+(?P<name>[^\s]+)\s+(?P<value>[^\r\n]+)\s*\n', data)
r = re.finditer(COMMENT_REGEX_START+r'#define\s+(?P<name>[^\s\r\n]+)\s+(?P<value>[^\s\r\n]+)\s*\n'+COMMENT_REGEX_END, data)
for t in r:
d = t.groupdict()
v = d['value'].rsplit('//')[0]
Expand Down Expand Up @@ -497,10 +496,11 @@ def getNumber(sign:str, number:int):
s, re.MULTILINE
)
r:list[StructureFieldType] = []
# print(regex)
# print(s)

for f in fields:
d = f.groupdict()
# print(regex)
# print(d)
if d.get("if",None) != None:
macroNameA = d.get('macroNameA',None)
Expand Down Expand Up @@ -529,10 +529,8 @@ def getNumber(sign:str, number:int):
self.structVersionDetails["valB"] = numB
elif dirB == ">":
self.structVersionDetails["valL"] = numB
continue
if d.get("endif",None) != None:
self.clearVersionDetails()
continue
if d['type'].startswith('//'):
continue

Expand Down Expand Up @@ -586,19 +584,6 @@ def parse_comment_block(self,s):

return commentAttributes

def _macros(self, s):
r = re.finditer(
COMMENT_REGEX_START+r'#define\s*(?P<name>[A-Za-z0-9_]+)\s+(?P<value>[A-Za-z0-9_"()])'+COMMENT_REGEX_END,
s, re.MULTILINE
)

for t in r:
d = t.groupdict()

mt = MacroType(self.cstruct, d["name"], d["value"])

self.cstruct.addtype(d['name'], mt, True)

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

Expand Down Expand Up @@ -1539,13 +1524,6 @@ def __repr__(self):
return '<EnumType {}>'.format(self.name)


class MacroType(RawType):
def __init__(self, cstruct, name:str, value:typing.Any):
self.name = name
self.value = value
super(MacroType, self).__init__(cstruct, name, 0)


class EnumInstance(object):
"""Implements a value instance of an EnumType"""

Expand Down

0 comments on commit bc202c6

Please sign in to comment.