def ksObjToNode(ksobj, id_): global filterLevel kshelp.exercise(ksobj) result = '' result += '\t"%s" [\n' % id_ print(repr(ksobj)) title = re.match(r'<.*\.(.*) object at .*', repr(ksobj)).group(1) fieldDescriptions = ['<title> [%s]' % title] # add non-KaitaiStruct fields for candidate in sorted(kshelp.getFieldNamesPrint(ksobj, filterLevel)): tmp = repr(candidate) tmp = tmp.replace('"', '\\"') if tmp.startswith('\'') and tmp.endswith('\''): tmp = tmp[1:-1] fieldDescriptions.append('<%s> .%s' % (tmp, tmp)) # add KaitaiStruct fields for candidate in sorted(kshelp.getFieldNamesDescend(ksobj, filterLevel)): fieldDescriptions.append('<%s> .%s' % (candidate, candidate)) result += '\t\tlabel = "%s"\n' % ('|'.join(fieldDescriptions)) result += '\t\tshape = "record"\n' result += '\t];\n' return result
def dump(obj, depth=0): global filterLevel indent = ' '*depth print(('%s'+PURPLE+'%s'+NORMAL) % (indent, repr(obj))) kshelp.exercise(obj) for fieldName in kshelp.getFieldNamesPrint(obj, filterLevel): subObj = None try: subObj = getattr(obj, fieldName) except Exception: continue if subObj == None: continue subObjStr = kshelp.objToStr(subObj) color = '' if type(subObj) == types.MethodType: pass elif isinstance(subObj, type): pass elif fieldName == '_debug': color = RED elif isinstance(subObj, list): pass elif isinstance(subObj, dict): pass elif isinstance(subObj, str): color = CYAN elif isinstance(subObj, bytes): color = CYAN elif type(subObj) == int: color = YELLOW elif str(type(subObj)).startswith('<enum '): color = GREEN pass if color: print('%s.%s: %s%s%s' % (indent, fieldName, color, subObjStr, NORMAL)) else: print('%s.%s: %s' % (indent, fieldName, subObjStr)) for fieldName in kshelp.getFieldNamesDescend(obj, filterLevel): subObj = getattr(obj, fieldName) #print('recurring on: %s' % repr(subObj)) if isinstance(subObj, list): for (i, tmp) in enumerate(subObj): print('%s.%s[%d]:' % (indent, fieldName, i)) dump(subObj[i], depth+1) else: print('%s.%s:' % (indent, fieldName)) #print(dir(subObj)) dump(subObj, depth+1)
def ksObjToDot(ksobj): global filterLevel dot = '' dot += 'digraph g {\n' dot += ' graph [\n' dot += ' rankdir=LR, overlap=false\n' dot += ' ];\n' dot += ' node [\n' dot += ' fontsize = "16"\n' dot += ' shape = "ellipse"\n' dot += ' ];\n' dot += ' edge [\n' dot += ' ];\n' kshelp.exercise(ksobj) ksObjs = kshelp.getLinkedKaitaiObjectsAll(ksobj) ksObjToLabel = {} for (i, obj) in enumerate(ksObjs): ksObjToLabel[obj] = 'ksobj%d' % i # declare nodes for ksobj in ksObjs: label = ksObjToLabel[ksobj] dot += ksObjToNode(ksobj, label) # declare edges id_ = 0 for srcObj in ksObjs: for fieldName in kshelp.getFieldNamesDescend(srcObj, filterLevel): # fieldName can be a KaitaiStruct or a [KaitaiStruct, ...] # normalize to list... dstObjs = None try: dstObjs = getattr(srcObj, fieldName) except AttributeError: continue if not isinstance(dstObjs, list): dstObjs = [dstObjs] # loop over list for dstObj in dstObjs: if not srcObj in ksObjToLabel or not dstObj in ksObjToLabel: continue srcName = ksObjToLabel[srcObj] dstName = ksObjToLabel[dstObj] dot += '\t"%s":%s -> "%s":title [\n' % (srcName, fieldName, dstName) dot += '\t\tid = %d' % id_ id_ += 1 dot += '\t];\n' dot += '}\n' return dot
def dump(obj, depth=0): global filterLevel print('%s{' % ('\t'*depth)) depth += 1 print('%s"_kaitai_type": "%s",' % (('\t'*depth), kshelp.objToStr(obj))) kshelp.exercise(obj) fieldNamesPrint = kshelp.getFieldNamesPrint(obj, filterLevel) fieldNamesDescend = kshelp.getFieldNamesDescend(obj, filterLevel) fieldNamesAll = fieldNamesPrint + fieldNamesDescend for (i,fieldName) in enumerate(fieldNamesAll): subObj = None try: subObj = getattr(obj, fieldName) except Exception: continue if subObj == None: continue # simple fields if fieldName in fieldNamesPrint: subObjStr = kshelp.objToStr(subObj) print('%s"%s": "%s"' % (('\t'*depth), fieldName, subObjStr), end='') # kaitai struct (descend!) else: if isinstance(subObj, list): print('%s"%s":\n%s[' % (('\t'*depth), fieldName, ('\t'*depth))) for (j, tmp) in enumerate(subObj): #print('%s.%s[%d]:' % (('\t'*depth), fieldName, i)) dump(tmp, depth+1) if j!=len(subObj)-1: print(',') else: print('') print('%s]' % ('\t'*depth), end='') else: print('%s"%s":' % (('\t'*depth), fieldName)) dump(subObj, depth) # add comma? if i!=len(fieldNamesAll)-1: print(',') else: print('') depth -= 1 print('%s}' % ('\t'*depth), end='')
def load_child_keys(self): global filterLevel result = [] kshelp.exercise(self._ksobj) for fieldName in ( kshelp.getFieldNamesPrint(self._ksobj, filterLevel) + kshelp.getFieldNamesDescend(self._ksobj, filterLevel)): childObj = getattr(self._ksobj, fieldName) if isinstance(childObj, list): for i in range(len(childObj)): result.append('%s[%d]' % (fieldName, i)) else: result.append(fieldName) return result
def coverage(obj, depth=0, doDraw=False): global filterLevel kshelp.exercise(obj) indent = ' ' * depth queue = [] # printable (non descendable field names) for fieldName in kshelp.getFieldNamesPrint( obj, filterLevel) + kshelp.getFieldNamesDescend(obj, filterLevel): try: subObj = getattr(obj, fieldName) except Exception: continue fieldRange = kshelp.getFieldRange(obj, fieldName, True) if not fieldRange: #print('rejecting %s since it has no range' % fieldName) continue print('%s.%s covers [0x%x, 0x%X)' % (indent, fieldName, fieldRange[0], fieldRange[1])) if doDraw: drawRange(fieldRange[0], fieldRange[1], depth) if isinstance(subObj, kaitaistruct.KaitaiStruct): #print('queuing %s' % fieldName) coverage(subObj, depth + 1, doDraw) if isinstance(subObj, list): for i in range(len(subObj)): fieldRange = kshelp.getFieldRange(obj, '%s[%d]' % (fieldName, i), True) print('%s.%s[%d] covers [0x%x, 0x%X)' % \ (indent, fieldName, i, fieldRange[0], fieldRange[1])) if doDraw: drawRange(fieldRange[0], fieldRange[1], depth) if isinstance(subObj[i], kaitaistruct.KaitaiStruct): coverage(subObj[i], depth + 1, doDraw)