From 3bf96e91b8c25d83676501b631a2dabbbc7ce664 Mon Sep 17 00:00:00 2001 From: Andrew Lamoureux Date: Sat, 23 Mar 2019 23:48:07 -0400 Subject: kaitai: two-pass on struct fields to entice out "_m_XXX" fields this should fix issues where program_headers[X] and section_headers[X] was not getting their start/end range populated correctly in elf files --- python/examples/kaitai/kshelpers.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'python') diff --git a/python/examples/kaitai/kshelpers.py b/python/examples/kaitai/kshelpers.py index 1deeef59..283c90d7 100644 --- a/python/examples/kaitai/kshelpers.py +++ b/python/examples/kaitai/kshelpers.py @@ -304,7 +304,10 @@ def buildQtree(ksobj): qwi = KaitaiTreeWidgetItem() qwi.setKaitaiObject(ksobj) - fieldNames = [] + fieldNames = set() + + # first pass: access fields that may be properties, which could compute + # internal results (often '_m_XXX' fields) for candidate in dir(ksobj): if candidate.startswith('_') and (not candidate.startswith('_m_')): continue @@ -312,15 +315,25 @@ def buildQtree(ksobj): continue try: if getattr(ksobj, candidate, False): - fieldNames.append(candidate) + fieldNames.add(candidate) except Exception: pass - for fieldName in fieldNames: - if ('_m_'+fieldName) in fieldNames: - # favor the '_m_' version which seems to get the debug info - continue + # second pass: collect the '_m_XXX' fields which have the debug start/end + # marks, remove the 'XXX' counterpart, if it exists + for candidate in filter(lambda x: x.startswith('_m_'), dir(ksobj)): + try: + if not getattr(ksobj, candidate, False): + continue + fieldNames.add(candidate) + if candidate[3:] in fieldNames: + fieldNames.remove(candidate[3:]) + + except Exception: + pass + + for fieldName in fieldNames: subObj = getattr(ksobj, fieldName) child = None -- cgit v1.3.1