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/__main__.py | 135 +++++++++-------- python/examples/kaitai/kshelpers.py | 289 +++++++++++++++++++++++++++++++++++- 2 files changed, 356 insertions(+), 68 deletions(-) (limited to 'python') diff --git a/python/examples/kaitai/__main__.py b/python/examples/kaitai/__main__.py index 2accd2e5..33e8e549 100755 --- a/python/examples/kaitai/__main__.py +++ b/python/examples/kaitai/__main__.py @@ -9,7 +9,6 @@ import sys import types import importlib - if sys.version_info[0] == 2: import kaitaistruct import kshelpers @@ -37,70 +36,62 @@ LCYAN = '\033[1;36m' LGRAY = '\033[1;37m' def dump(obj, depth=0): - dump_exceptions = ['_root', '_parent', '_io', 'SEQ_FIELDS'] - indent = ' '*depth - if isinstance(obj, kaitaistruct.KaitaiStruct): - fieldNames = [] - for candidate in dir(obj): - if candidate != '_debug' and candidate.startswith('_'): - continue - if candidate in dump_exceptions: - continue - try: - if getattr(obj, candidate, False): - fieldNames.append(candidate) - except Exception: - pass + print(('%s'+PURPLE+'%s'+NORMAL) % (indent, repr(obj))) - for fieldName in fieldNames: + kshelpers.exercise(obj) + for fieldName in kshelpers.getFieldNamesPrint(obj): + subObj = None + try: subObj = getattr(obj, fieldName) - - if type(subObj) == types.MethodType: - pass - #elif type(subObj) == types.TypeType: - elif isinstance(subObj, type): - pass - elif fieldName == '_debug': - print(('%s._debug:'+RED+' %s'+NORMAL) % (indent, repr(subObj))) - #elif type(subObj) == types.ListType: - elif isinstance(subObj, list): - if len(subObj)>0 and isinstance(subObj[0], kaitaistruct.KaitaiStruct): - for i in range(len(subObj)): - print('%s.%s[%d]:' % (indent, fieldName, i)) - dump(subObj[i], depth+1) - else: - print('%s.%s: %s' % (indent, fieldName, str(subObj))) - #elif type(subObj) == types.DictionaryType: - elif isinstance(subObj, dict): - print('%s.%s: %s' % (indent, fieldName, subObj)) - - #elif type(subObj) == types.StringType: - elif isinstance(subObj, str): - if len(subObj) <= 16: - print(('%s.%s: '+CYAN+'%s'+NORMAL) % (indent, fieldName, repr(subObj))) - else: - print(('%s.%s: '+CYAN+'%s...'+NORMAL+' 0x%X (%d) bytes total') % \ - (indent, fieldName, repr(subObj[0:16]), len(subObj), len(subObj)) - ) - - elif type(subObj) == int: - print(('%s.%s: '+YELLOW+'0x%X '+NORMAL+'('+YELLOW+'%d'+NORMAL+')') % (indent, fieldName, subObj, subObj)) - - elif str(type(subObj)).startswith(' 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