From 1c03ac08aa94f5bedf21ec8f48ee1ec998e0e50c Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Tue, 29 May 2018 14:32:08 -0400 Subject: addition 3 compatibility changes --- python/function.py | 100 ++++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 48 deletions(-) (limited to 'python/function.py') diff --git a/python/function.py b/python/function.py index a0269e75..6392d7f2 100644 --- a/python/function.py +++ b/python/function.py @@ -35,6 +35,10 @@ from binaryninja import types from binaryninja import highlight from binaryninja import log +# 2-3 compatibility +from six.moves import range + + class LookupTableEntry(object): def __init__(self, from_values, to_value): self.from_values = from_values @@ -173,7 +177,7 @@ class PossibleValueSet(object): elif value.state == RegisterValueType.SignedRangeValue: self.offset = value.value self.ranges = [] - for i in xrange(0, value.count): + for i in range(0, value.count): start = value.ranges[i].start end = value.ranges[i].end step = value.ranges[i].step @@ -185,7 +189,7 @@ class PossibleValueSet(object): elif value.state == RegisterValueType.UnsignedRangeValue: self.offset = value.value self.ranges = [] - for i in xrange(0, value.count): + for i in range(0, value.count): start = value.ranges[i].start end = value.ranges[i].end step = value.ranges[i].step @@ -193,15 +197,15 @@ class PossibleValueSet(object): elif value.state == RegisterValueType.LookupTableValue: self.table = [] self.mapping = {} - for i in xrange(0, value.count): + for i in range(0, value.count): from_list = [] - for j in xrange(0, value.table[i].fromCount): + for j in range(0, value.table[i].fromCount): from_list.append(value.table[i].fromValues[j]) self.mapping[value.table[i].fromValues[j]] = value.table[i].toValue self.table.append(LookupTableEntry(from_list, value.table[i].toValue)) elif (value.state == RegisterValueType.InSetOfValues) or (value.state == RegisterValueType.NotInSetOfValues): self.values = set() - for i in xrange(0, value.count): + for i in range(0, value.count): self.values.add(value.valueSet[i]) def __repr__(self): @@ -479,7 +483,7 @@ class Function(object): count = ctypes.c_ulonglong() blocks = core.BNGetFunctionBasicBlockList(self.handle, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(binaryninja.basicblock.BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i]))) core.BNFreeBasicBlockList(blocks, count.value) return result @@ -490,7 +494,7 @@ class Function(object): count = ctypes.c_ulonglong() addrs = core.BNGetCommentedAddresses(self.handle, count) result = {} - for i in xrange(0, count.value): + for i in range(0, count.value): result[addrs[i]] = self.get_comment_at(addrs[i]) core.BNFreeAddressList(addrs) return result @@ -525,7 +529,7 @@ class Function(object): count = ctypes.c_ulonglong() v = core.BNGetStackLayout(self.handle, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(Variable(self, v[i].var.type, v[i].var.index, v[i].var.storage, v[i].name, types.Type(handle = core.BNNewTypeReference(v[i].type), platform = self.platform, confidence = v[i].typeConfidence))) result.sort(key = lambda x: x.identifier) @@ -538,7 +542,7 @@ class Function(object): count = ctypes.c_ulonglong() v = core.BNGetFunctionVariables(self.handle, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(Variable(self, v[i].var.type, v[i].var.index, v[i].var.storage, v[i].name, types.Type(handle = core.BNNewTypeReference(v[i].type), platform = self.platform, confidence = v[i].typeConfidence))) result.sort(key = lambda x: x.identifier) @@ -551,7 +555,7 @@ class Function(object): count = ctypes.c_ulonglong() branches = core.BNGetIndirectBranches(self.handle, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(IndirectBranchInfo(binaryninja.architecture.CoreArchitecture._from_cache(branches[i].sourceArch), branches[i].sourceAddr, binaryninja.architecture.CoreArchitecture._from_cache(branches[i].destArch), branches[i].destAddr, branches[i].autoDefined)) core.BNFreeIndirectBranchList(branches) return result @@ -572,7 +576,7 @@ class Function(object): count = ctypes.c_ulonglong() info = core.BNGetFunctionAnalysisPerformanceInfo(self.handle, count) result = {} - for i in xrange(0, count.value): + for i in range(0, count.value): result[info[i].name] = info[i].seconds core.BNFreeAnalysisPerformanceInfo(info, count.value) return result @@ -606,7 +610,7 @@ class Function(object): """Registers that are used for the return value""" result = core.BNGetFunctionReturnRegisters(self.handle) reg_set = [] - for i in xrange(0, result.count): + for i in range(0, result.count): reg_set.append(self.arch.get_reg_name(result.regs[i])) regs = types.RegisterSet(reg_set, confidence = result.confidence) core.BNFreeRegisterSet(result) @@ -617,7 +621,7 @@ class Function(object): regs = core.BNRegisterSetWithConfidence() regs.regs = (ctypes.c_uint * len(value))() regs.count = len(value) - for i in xrange(0, len(value)): + for i in range(0, len(value)): regs.regs[i] = self.arch.get_reg_index(value[i]) if hasattr(value, 'confidence'): regs.confidence = value.confidence @@ -649,7 +653,7 @@ class Function(object): """List of variables for the incoming function parameters""" result = core.BNGetFunctionParameterVariables(self.handle) var_list = [] - for i in xrange(0, result.count): + for i in range(0, result.count): var_list.append(Variable(self, result.vars[i].type, result.vars[i].index, result.vars[i].storage)) confidence = result.confidence core.BNFreeParameterVariables(result) @@ -664,7 +668,7 @@ class Function(object): var_conf = core.BNParameterVariablesWithConfidence() var_conf.vars = (core.BNVariable * len(var_list))() var_conf.count = len(var_list) - for i in xrange(0, len(var_list)): + for i in range(0, len(var_list)): var_conf.vars[i].type = var_list[i].source_type var_conf.vars[i].index = var_list[i].index var_conf.vars[i].storage = var_list[i].storage @@ -714,7 +718,7 @@ class Function(object): count = ctypes.c_ulonglong() adjust = core.BNGetFunctionRegisterStackAdjustments(self.handle, count) result = {} - for i in xrange(0, count.value): + for i in range(0, count.value): name = self.arch.get_reg_stack_name(adjust[i].regStack) value = types.RegisterStackAdjustmentWithConfidence(adjust[i].adjustment, confidence = adjust[i].confidence) @@ -742,7 +746,7 @@ class Function(object): """Registers that are modified by this function""" result = core.BNGetFunctionClobberedRegisters(self.handle) reg_set = [] - for i in xrange(0, result.count): + for i in range(0, result.count): reg_set.append(self.arch.get_reg_name(result.regs[i])) regs = types.RegisterSet(reg_set, confidence = result.confidence) core.BNFreeRegisterSet(result) @@ -753,7 +757,7 @@ class Function(object): regs = core.BNRegisterSetWithConfidence() regs.regs = (ctypes.c_uint * len(value))() regs.count = len(value) - for i in xrange(0, len(value)): + for i in range(0, len(value)): regs.regs[i] = self.arch.get_reg_index(value[i]) if hasattr(value, 'confidence'): regs.confidence = value.confidence @@ -847,7 +851,7 @@ class Function(object): count = ctypes.c_ulonglong() blocks = core.BNGetFunctionBasicBlockList(self.handle, count) try: - for i in xrange(0, count.value): + for i in range(0, count.value): yield binaryninja.basicblock.BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i])) finally: core.BNFreeBasicBlockList(blocks, count.value) @@ -918,7 +922,7 @@ class Function(object): count = ctypes.c_ulonglong() exits = core.BNGetLowLevelILExitsForInstruction(self.handle, arch.handle, addr, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(exits[i]) core.BNFreeILInstructionList(exits) return result @@ -1017,7 +1021,7 @@ class Function(object): count = ctypes.c_ulonglong() regs = core.BNGetRegistersReadByInstruction(self.handle, arch.handle, addr, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(arch.get_reg_name(regs[i])) core.BNFreeRegisterList(regs) return result @@ -1028,7 +1032,7 @@ class Function(object): count = ctypes.c_ulonglong() regs = core.BNGetRegistersWrittenByInstruction(self.handle, arch.handle, addr, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(arch.get_reg_name(regs[i])) core.BNFreeRegisterList(regs) return result @@ -1039,7 +1043,7 @@ class Function(object): count = ctypes.c_ulonglong() refs = core.BNGetStackVariablesReferencedByInstruction(self.handle, arch.handle, addr, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): var_type = types.Type(core.BNNewTypeReference(refs[i].type), platform = self.platform, confidence = refs[i].typeConfidence) result.append(StackVariableReference(refs[i].sourceOperand, var_type, refs[i].name, Variable.from_identifier(self, refs[i].varIdentifier, refs[i].name, var_type), @@ -1053,7 +1057,7 @@ class Function(object): count = ctypes.c_ulonglong() refs = core.BNGetConstantsReferencedByInstruction(self.handle, arch.handle, addr, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(ConstantReference(refs[i].value, refs[i].size, refs[i].pointer, refs[i].intermediate)) core.BNFreeConstantReferenceList(refs) return result @@ -1074,7 +1078,7 @@ class Function(object): count = ctypes.c_ulonglong() instrs = core.BNGetLiftedILFlagUsesForDefinition(self.handle, i, flag, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(instrs[i]) core.BNFreeILInstructionList(instrs) return result @@ -1084,7 +1088,7 @@ class Function(object): count = ctypes.c_ulonglong() instrs = core.BNGetLiftedILFlagDefinitionsForUse(self.handle, i, flag, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(instrs[i]) core.BNFreeILInstructionList(instrs) return result @@ -1093,7 +1097,7 @@ class Function(object): count = ctypes.c_ulonglong() flags = core.BNGetFlagsReadByLiftedILInstruction(self.handle, i, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(self.arch._flags_by_index[flags[i]]) core.BNFreeRegisterList(flags) return result @@ -1102,7 +1106,7 @@ class Function(object): count = ctypes.c_ulonglong() flags = core.BNGetFlagsWrittenByLiftedILInstruction(self.handle, i, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(self.arch._flags_by_index[flags[i]]) core.BNFreeRegisterList(flags) return result @@ -1120,7 +1124,7 @@ class Function(object): if source_arch is None: source_arch = self.arch branch_list = (core.BNArchitectureAndAddress * len(branches))() - for i in xrange(len(branches)): + for i in range(len(branches)): branch_list[i].arch = branches[i][0].handle branch_list[i].address = branches[i][1] core.BNSetAutoIndirectBranches(self.handle, source_arch.handle, source, branch_list, len(branches)) @@ -1129,7 +1133,7 @@ class Function(object): if source_arch is None: source_arch = self.arch branch_list = (core.BNArchitectureAndAddress * len(branches))() - for i in xrange(len(branches)): + for i in range(len(branches)): branch_list[i].arch = branches[i][0].handle branch_list[i].address = branches[i][1] core.BNSetUserIndirectBranches(self.handle, source_arch.handle, source, branch_list, len(branches)) @@ -1140,7 +1144,7 @@ class Function(object): count = ctypes.c_ulonglong() branches = core.BNGetIndirectBranchesAt(self.handle, arch.handle, addr, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(IndirectBranchInfo(binaryninja.architecture.CoreArchitecture._from_cache(branches[i].sourceArch), branches[i].sourceAddr, binaryninja.architecture.CoreArchitecture._from_cache(branches[i].destArch), branches[i].destAddr, branches[i].autoDefined)) core.BNFreeIndirectBranchList(branches) return result @@ -1151,9 +1155,9 @@ class Function(object): count = ctypes.c_ulonglong(0) lines = core.BNGetFunctionBlockAnnotations(self.handle, arch.handle, addr, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): tokens = [] - for j in xrange(0, lines[i].count): + for j in range(0, lines[i].count): token_type = InstructionTextTokenType(lines[i].tokens[j].type) text = lines[i].tokens[j].text value = lines[i].tokens[j].value @@ -1187,7 +1191,7 @@ class Function(object): regs = core.BNRegisterSetWithConfidence() regs.regs = (ctypes.c_uint * len(value))() regs.count = len(value) - for i in xrange(0, len(value)): + for i in range(0, len(value)): regs.regs[i] = self.arch.get_reg_index(value[i]) if hasattr(value, 'confidence'): regs.confidence = value.confidence @@ -1213,7 +1217,7 @@ class Function(object): var_conf = core.BNParameterVariablesWithConfidence() var_conf.vars = (core.BNVariable * len(var_list))() var_conf.count = len(var_list) - for i in xrange(0, len(var_list)): + for i in range(0, len(var_list)): var_conf.vars[i].type = var_list[i].source_type var_conf.vars[i].index = var_list[i].index var_conf.vars[i].storage = var_list[i].storage @@ -1270,7 +1274,7 @@ class Function(object): regs = core.BNRegisterSetWithConfidence() regs.regs = (ctypes.c_uint * len(value))() regs.count = len(value) - for i in xrange(0, len(value)): + for i in range(0, len(value)): regs.regs[i] = self.arch.get_reg_index(value[i]) if hasattr(value, 'confidence'): regs.confidence = value.confidence @@ -1458,10 +1462,10 @@ class Function(object): count = ctypes.c_ulonglong() lines = core.BNGetFunctionTypeTokens(self.handle, settings, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): addr = lines[i].addr tokens = [] - for j in xrange(0, lines[i].count): + for j in range(0, lines[i].count): token_type = InstructionTextTokenType(lines[i].tokens[j].type) text = lines[i].tokens[j].text value = lines[i].tokens[j].value @@ -1553,7 +1557,7 @@ class Function(object): count = ctypes.c_ulonglong() adjust = core.BNGetCallRegisterStackAdjustment(self.handle, arch.handle, addr, count) result = {} - for i in xrange(0, count.value): + for i in range(0, count.value): result[arch.get_reg_stack_name(adjust[i].regStack)] = types.RegisterStackAdjustmentWithConfidence( adjust[i].adjustment, confidence = adjust[i].confidence) core.BNFreeRegisterStackAdjustments(adjust) @@ -1709,14 +1713,14 @@ class FunctionGraphBlock(object): lines = core.BNGetFunctionGraphBlockLines(self.handle, count) block = self.basic_block result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): addr = lines[i].addr if (lines[i].instrIndex != 0xffffffffffffffff) and hasattr(block, 'il_function'): il_instr = block.il_function[lines[i].instrIndex] else: il_instr = None tokens = [] - for j in xrange(0, lines[i].count): + for j in range(0, lines[i].count): token_type = InstructionTextTokenType(lines[i].tokens[j].type) text = lines[i].tokens[j].text value = lines[i].tokens[j].value @@ -1736,7 +1740,7 @@ class FunctionGraphBlock(object): count = ctypes.c_ulonglong() edges = core.BNGetFunctionGraphBlockOutgoingEdges(self.handle, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): branch_type = BranchType(edges[i].type) target = edges[i].target if target: @@ -1749,7 +1753,7 @@ class FunctionGraphBlock(object): core.BNNewBasicBlockReference(target)) core.BNFreeFunction(func) points = [] - for j in xrange(0, edges[i].pointCount): + for j in range(0, edges[i].pointCount): points.append((edges[i].points[j].x, edges[i].points[j].y)) result.append(FunctionGraphEdge(branch_type, self, target, points, edges[i].backEdge)) core.BNFreeFunctionGraphBlockOutgoingEdgeList(edges, count.value) @@ -1773,14 +1777,14 @@ class FunctionGraphBlock(object): lines = core.BNGetFunctionGraphBlockLines(self.handle, count) block = self.basic_block try: - for i in xrange(0, count.value): + for i in range(0, count.value): addr = lines[i].addr if (lines[i].instrIndex != 0xffffffffffffffff) and hasattr(block, 'il_function'): il_instr = block.il_function[lines[i].instrIndex] else: il_instr = None tokens = [] - for j in xrange(0, lines[i].count): + for j in range(0, lines[i].count): token_type = InstructionTextTokenType(lines[i].tokens[j].type) text = lines[i].tokens[j].text value = lines[i].tokens[j].value @@ -1877,7 +1881,7 @@ class FunctionGraph(object): count = ctypes.c_ulonglong() blocks = core.BNGetFunctionGraphBlocks(self.handle, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(FunctionGraphBlock(core.BNNewFunctionGraphBlockReference(blocks[i]), self)) core.BNFreeFunctionGraphBlockList(blocks, count.value) return result @@ -1956,7 +1960,7 @@ class FunctionGraph(object): count = ctypes.c_ulonglong() blocks = core.BNGetFunctionGraphBlocks(self.handle, count) try: - for i in xrange(0, count.value): + for i in range(0, count.value): yield FunctionGraphBlock(core.BNNewFunctionGraphBlockReference(blocks[i]), self) finally: core.BNFreeFunctionGraphBlockList(blocks, count.value) @@ -1999,7 +2003,7 @@ class FunctionGraph(object): count = ctypes.c_ulonglong() blocks = core.BNGetFunctionGraphBlocksInRegion(self.handle, left, top, right, bottom, count) result = [] - for i in xrange(0, count.value): + for i in range(0, count.value): result.append(FunctionGraphBlock(core.BNNewFunctionGraphBlockReference(blocks[i]), self)) core.BNFreeFunctionGraphBlockList(blocks, count.value) return result -- cgit v1.3.1