From 5896a6acc3ac7bfb33915781263e2adf5ee62d5a Mon Sep 17 00:00:00 2001 From: Andrew Lamoureux Date: Sat, 11 May 2019 13:54:01 -0400 Subject: kaitai: fix fields with values 0 being hidden --- python/examples/kaitai/kshelpers.py | 289 +++++++++++++++++++++++++++++++++++- 1 file changed, 286 insertions(+), 3 deletions(-) (limited to 'python/examples/kaitai/kshelpers.py') diff --git a/python/examples/kaitai/kshelpers.py b/python/examples/kaitai/kshelpers.py index 283c90d7..2d465346 100644 --- a/python/examples/kaitai/kshelpers.py +++ b/python/examples/kaitai/kshelpers.py @@ -4,10 +4,13 @@ import traceback import io import os +import re import sys -import struct import types +import struct +import binascii import importlib +import collections from binaryninja import log @@ -148,6 +151,286 @@ def parseIo(ioObj, ksModuleName=None): return parsed +#------------------------------------------------------------------------------ +# misc +#------------------------------------------------------------------------------ + +def objToStr(obj): + objType = type(obj) + + # blacklist: functions, types, callables + # + if isinstance(obj, type): + #print('reject %s because its a type' % fieldName) + return '(type)' + elif hasattr(obj, '__call__'): + #print('reject %s because its a callable' % fieldName) + return '(callable)' + + result = None + + # whitelist: strings, unicodes, bytes, ints, bools, enums + # + if obj == None: + return 'None' + elif isinstance(obj, str): + if len(obj) > 8: + result = '%s...%s (0x%X==%d chars total)' % \ + (repr(obj[0:8]), repr(obj[-1]), len(obj), len(obj)) + else: + result = repr(obj) + elif isinstance(obj, bytes): + if len(obj) > 8: + result = binascii.hexlify(obj[0:8]).decode('utf-8') + '...' + \ + ('%02X' % obj[-1]) + ' (0x%X==%d bytes total)' % (len(obj), len(obj)) + else: + result = binascii.hexlify(obj).decode('utf-8') + # note: bool needs to appear before int (else int determination will dominate) + elif isinstance(obj, bool): + result = '%s' % (obj) + elif isinstance(obj, int): + result = '0x%X (%d)' % (obj, obj) + elif str(objType).startswith('0 and isinstance(subobj[0], kaitaistruct.KaitaiStruct): + result += [fieldName] + except Exception: + pass + + return result + +# compute all kaitai objects linked to from the given object +# +# IN: kaitai object +# OUT: [obj0, obj1, obj2, ...] +# +def getLinkedKaitaiObjects(ksobj): + result = set() + + for fieldName in getFieldNamesDescend(ksobj): + subobj = getattr(ksobj, fieldName, False) + if isinstance(subobj, list): + for tmp in subobj: + result.add(tmp) + else: + result.add(subobj) + + return result + +# compute all kaitai objects linked to from the given object, and from its +# descendents, and so on... +def getLinkedKaitaiObjectsAll(ksobj, depth=0): + #if depth > 2: + # return [] + + exercise(ksobj) + + result = set([ksobj]) + + linkedObjects = getLinkedKaitaiObjects(ksobj) + for subobj in linkedObjects: + subResult = getLinkedKaitaiObjectsAll(subobj, depth+1) + result = result.union(subResult) + + return result + +def getDepth(ksobj, depth=0): + result = depth + + exercise(ksobj) + for subObj in getLinkedKaitaiObjects(ksobj): + result = max(result, getDepth(subObj, depth+1)) + + return result + #------------------------------------------------------------------------------ # Kaitai IO Wrapper #------------------------------------------------------------------------------ @@ -314,8 +597,8 @@ def buildQtree(ksobj): if candidate in exceptions: continue try: - if getattr(ksobj, candidate, False): - fieldNames.add(candidate) + getattr(ksobj, candidate) + fieldNames.add(candidate) except Exception: pass -- cgit v1.3.1