我试图将两个应用程序组合起来,用于可视化二进制文件,binvis和droidcolors。由于droidcolors在C语言中,我试图让binvis复制droidcolors提供的功能。
以下是我到目前为止编写的代码中的相关摘要。完整的文件位于:https://pastebin.com/0C7fu9W2。
class _Color:
def __init__(self, data, **kwargs):
self.data = data
for key, value in kwargs.items():
setattr(self, key, value)
s = list(set(data))
s.sort()
self.symbol_map = {v : i for (i, v) in enumerate(s)}
def __len__(self):
return len(self.data)
def point(self, x):
if hasattr(self, 'class_defs') and (self.class_defs[0] <= x <= self.class_defs[1]):
return self.return_droidcolors_classdefs(x)
elif hasattr(self, 'dat') and (self.dat[0] <= x <= self.dat[1]):
return self.return_droidcolors_data(x)
elif hasattr(self, 'field_ids') and (self.field_ids[0] <= x <= self.field_ids[1]):
return self.return_droidcolors_field_ids(x)
elif hasattr(self, 'header') and (self.header[0] <= x <= self.header[1]):
return self.return_droidcolors_header(x)
elif hasattr(self, 'links') and (self.links[0] <= x <= self.links[1]):
return self.return_droidcolors_links(x)
elif hasattr(self, 'proto_ids') and (self.proto_ids[0] <= x <= self.proto_ids[1]):
return self.return_droidcolors_proto_ids(x)
elif hasattr(self, 'method_ids') and (self.method_ids[0] <= x <= self.method_ids[1]):
return self.return_droidcolors_method_ids(x)
elif hasattr(self, 'string_ids') and (self.string_ids[0] <= x <= self.string_ids[1]):
return self.return_droidcolors_string_ids(x)
elif hasattr(self, 'type_ids') and (self.type_ids[0] <= x <= self.type_ids[1]):
return self.return_droidcolors_type_ids(x)
elif hasattr(self, 'block') and (self.block[0] <= x <= self.block[1]):
return self.block[2]
else:
return self.getPoint(x)
# --- snip ---
class ColorDroidColors(_Color):
def getPoint(self, x):
return [0, 0, 0]
# --- snip ---
dex_handle = Dexparser(args[0])
dex_header_info = dex_handle.header_info()
dex_class_defs = (dex_header_info['class_defs_off'], dex_header_info['class_defs_off'] + (64 * dex_header_info['class_defs_size']) - 1)
dex_data = (dex_header_info['data_off'], dex_header_info['data_off'] + dex_header_info['data_size'] - 1)
dex_field_ids = (dex_header_info['field_ids_off'], dex_header_info['field_ids_off'] + (8 * dex_header_info['field_ids_size']) - 1)
dex_header = (0x0, dex_header_info['header_size'] - 1)
dex_links = (link_off, link_off + dex_header_info['link_size'] - 1)
dex_proto_ids = (dex_header_info['proto_ids_off'], dex_header_info['proto_ids_off'] + (12 * dex_header_info['proto_ids_size']) - 1)
dex_method_ids = (dex_header_info['method_ids_off'], dex_header_info['method_ids_off'] + (8 * dex_header_info['method_ids_size']) - 1)
dex_string_ids = (dex_header_info['string_ids_off'], dex_header_info['string_ids_off'] + (4 * (dex_header_info['string_ids_size']) - 1))
dex_type_ids = (dex_header_info['type_ids_off'], dex_header_info['type_ids_off'] + (4 * dex_header_info['type_ids_size']) - 1)
kwarg_dict = {
"class_defs": dex_class_defs,
"dat": dex_data,
"field_ids": dex_field_ids,
"header": dex_header,
"links": dex_links,
"proto_ids": dex_proto_ids,
"method_ids": dex_method_ids,
"string_ids": dex_string_ids,
"type_ids": dex_type_ids,
}
for i, item in enumerate(dex_handle.protoids_list()):
if item[2]:
kwarg_dict['dex_proto_params_' + str(i)] = (item[2], (0x0 + item[2]) * 4 + 4)
csource = ColorDroidColors(
d,
**kwarg_dict
)
我基本上将10000+ kwargs传递给一个方法,并且如果存在kwarg并且依赖于相同的值,则需要一种方法在方法内进行测试以返回一些值。所有的kwargs都有一个特定的前缀,后跟从1开始的连续数字。我尝试使用for循环,但这会使程序减慢到爬行速度。
有人可以建议我出路吗?
答案 0 :(得分:0)
使用getattr()
和要测试的属性列表:
attrs = ['class_defs', 'dat', 'field_ids', ..., 'block']
for name in attrs:
value = getattr(self, name, None)
if value is not None and value[0] <= x <= value[1]
if name != 'block':
method_name = 'return_droidcolors_' + name
return getattr(self, method_name)(x)
else:
return value[2]
# none of the attributes matched
return self.getPoint(x)
请注意,在__init__
方法中,您可以使用self.__dict__.update(kwargs)
而不是循环遍历字典并每次调用setattr()
。