def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name : Stripping invalid chars from function name {!r} at {:x}. : {!r}'.format(__name__, string, ea, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise AssertionError('{:s}.set_name : Unable to set function name for {:x} : {!r}'.format(__name__, ea, string))
return res
python类set()的实例源码
def set_color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, n = cls.get_color(bb), by_address(bb.startEA), idaapi.node_info_t()
# specify the bg color
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb.id, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the colors of each item too.
for ea in block.iterate(bb):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def set_color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, n = cls.get_color(bb), by_address(bb.startEA), idaapi.node_info_t()
# specify the bg color
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb.id, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the colors of each item too.
for ea in block.iterate(bb):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def set_color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, n = cls.get_color(bb), by_address(bb.startEA), idaapi.node_info_t()
# specify the bg color
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb.id, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the colors of each item too.
for ea in block.iterate(bb):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def set_color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, n = cls.get_color(bb), by_address(bb.startEA), idaapi.node_info_t()
# specify the bg color
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb.id, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the colors of each item too.
for ea in block.iterate(bb):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def __getarray(cls, ea):
try:
c, = xref.data_up(ea)
sidata,each = type.array(ea),__builtin__.set(xref.code_down(c))
# check to see if first element is the correct dataref
lastea, = xref.data_down(c)
if ea != lastea: raise TypeError
# then copy the first element since it's been decoded already
each.add(sidata[0])
# ensure that each element matches
if config.bits() == sidata.itemsize*8 and all(x in each for x in sidata):
r, = xref.data_up(ea)
return cls.__getinsn(r)
except (IndexError,TypeError,KeyError,ValueError): pass
raise TypeError("{:s}({:x}) : Unable to instantiate a switch_info_ex_t at switch array.".format('.'.join((__name__, 'type', cls.__name__)), ea))
def set_color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, n = cls.get_color(bb), by_address(bb.startEA), idaapi.node_info_t()
# specify the bg color
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb.id, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the colors of each item too.
for ea in block.iterate(bb):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def set_color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, n = cls.get_color(bb), by_address(bb.startEA), idaapi.node_info_t()
# specify the bg color
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb.id, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the colors of each item too.
for ea in block.iterate(bb):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name : Stripping invalid chars from function name {!r} at {:x}. : {!r}'.format(__name__, string, ea, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise AssertionError('{:s}.set_name : Unable to set function name for {:x} : {!r}'.format(__name__, ea, string))
return res
def set_color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, n = cls.get_color(bb), by_address(bb.startEA), idaapi.node_info_t()
# specify the bg color
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb.id, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the colors of each item too.
for ea in block.iterate(bb):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def openscope(self, customlocals=None):
'''Opens a new (embedded) scope.
Args:
customlocals (dict): By default, the locals of the embedding scope
are visible in the new one. When this is not the desired
behaviour a dictionary of customized locals can be passed,
and those locals will become the only visible ones.
'''
self._locals_stack.append(self._locals)
self._globalrefs_stack.append(self._globalrefs)
if customlocals is not None:
self._locals = customlocals.copy()
elif self._locals is not None:
self._locals = self._locals.copy()
else:
self._locals = {}
self._globalrefs = set()
self._scope = self._globals.copy()
self._scope.update(self._locals)
def set_color(cls, ea, rgb, **frame):
"""Sets the color of the basic-block at address ``ea`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, bb = cls.get_color(ea), by_address(ea), cls.id(ea)
n = idaapi.node_info_t()
# specify the bgcolor
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the color of each item too
for ea in block.iterate(ea):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def color(cls, ea, rgb, **frame):
"""Sets the color of the basic-block at the address ``ea`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
return cls.set_color(ea, rgb, **frame)
def color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
return cls.set_color(bb, rgb, **frame)
def tag_write(func, key, none):
'''Removes the tag identified by ``key`` from the function ``func``.'''
#fn = by(func)
# Check to see if function tag is being applied to an import
try:
rt,ea = __addressOfRtOrSt(func)
except LookupError:
# If we're not even in a function, then use a database tag.
logging.warn('{:s}.tag_write : Attempted to clear tag for a non-function. Falling back to a database tag. : {:x}'.format(__name__, func))
return database.tag_write(func, key, none)
# If so, then write the tag to the import
if rt:
logging.warn('{:s}.tag_write : Attempted to set tag for a runtime-linked symbol. Falling back to a database tag. : {:x}'.format(__name__, ea))
return database.tag_write(ea, key, none)
# Otherwise, it's a function.
fn = by_address(ea)
# if the user wants to remove the '__name__' tag then remove the name from the function.
if key == '__name__':
return set_name(fn, None)
state = internal.comment.decode(comment(fn, repeatable=1))
res = state.pop(key)
comment(fn, internal.comment.encode(state), repeatable=1)
internal.comment.globals.dec(fn.startEA, key)
return res
#FIXME: define tag_erase
def select(tag, *tags, **boolean):
tags = (tag,) + tags
boolean['And'] = tuple(set(boolean.get('And',set())).union(tags))
return select(ui.current.function(), **boolean)
def select(func, tag, *tags, **boolean):
tags = (tag,) + tags
boolean['And'] = tuple(set(boolean.get('And',set())).union(tags))
return select(func, **boolean)
def select(func, tag, *tags, **boolean):
tags = set(__builtin__.list(tag) + __builtin__.list(tags))
boolean['And'] = tuple(set(boolean.get('And',set())).union(tags))
return select(func, **boolean)
def select(func, **boolean):
'''Fetch a list of addresses within the function that contain the specified tags.'''
fn = by(func)
boolean = dict((k,set(v if isinstance(v, (__builtin__.tuple,__builtin__.set,__builtin__.list)) else (v,))) for k,v in boolean.viewitems())
if not boolean:
for ea in internal.comment.contents.address(fn.startEA):
res = database.tag(ea)
if res: yield ea, res
return
for ea in internal.comment.contents.address(fn.startEA):
res,d = {},database.tag(ea)
Or = boolean.get('Or', set())
res.update((k,v) for k,v in d.iteritems() if k in Or)
And = boolean.get('And', set())
if And:
if And.intersection(d.viewkeys()) == And:
res.update((k,v) for k,v in d.iteritems() if k in And)
else: continue
if res: yield ea,res
return
## referencing
def set_color(cls, ea, rgb, **frame):
"""Sets the color of the basic-block at address ``ea`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, bb = cls.get_color(ea), by_address(ea), cls.id(ea)
n = idaapi.node_info_t()
# specify the bgcolor
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the color of each item too
for ea in block.iterate(ea):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def color(cls, ea, rgb, **frame):
"""Sets the color of the basic-block at the address ``ea`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
return cls.set_color(ea, rgb, **frame)
def color(cls, bb, rgb, **frame):
"""Sets the color of the basic-block ``bb`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
return cls.set_color(bb, rgb, **frame)
def tag_write(func, key, value):
'''Set the tag ``key`` to ``value`` for the function ``func``.'''
if value is None:
raise ValueError("{:s}.tag_write({!r}) : Tried to set tag {!r} to an invalid value.".format(__name__, ea, key))
# Check to see if function tag is being applied to an import
try:
rt,ea = __addressOfRtOrSt(func)
except LookupError:
# If we're not even in a function, then use a database tag.
logging.warn('{:s}.tag_write({!r}, {!r}, ...) : Attempted to set tag for a non-function. Falling back to a database tag.'.format(__name__, func, key))
return database.tag_write(func, key, value)
# If so, then write the tag to the import
if rt:
logging.warn('{:s}.tag_write({:x}, {!r}, ...) : Attempted to set tag for a runtime-linked symbol. Falling back to a database tag.'.format(__name__, ea, key))
return database.tag_write(ea, key, value)
# Otherwise, it's a function.
fn = by_address(ea)
# if the user wants to change the '__name__' tag then update the function's name.
if key == '__name__':
return set_name(fn, value)
state = internal.comment.decode(comment(fn, repeatable=1))
res,state[key] = state.get(key,None),value
comment(fn, internal.comment.encode(state), repeatable=1)
if res is None:
internal.comment.globals.inc(fn.startEA, key)
return res
def select(tag, *tags, **boolean):
tags = (tag,) + tags
boolean['And'] = tuple(set(boolean.get('And',set())).union(tags))
return select(ui.current.function(), **boolean)
def select(func, tag, *tags, **boolean):
tags = (tag,) + tags
boolean['And'] = tuple(set(boolean.get('And',set())).union(tags))
return select(func, **boolean)
def select(func, tag, *tags, **boolean):
tags = set(__builtin__.list(tag) + __builtin__.list(tags))
boolean['And'] = tuple(set(boolean.get('And',set())).union(tags))
return select(func, **boolean)
def select(func, **boolean):
'''Fetch a list of addresses within the function that contain the specified tags.'''
fn = by(func)
boolean = dict((k,set(v if isinstance(v, (__builtin__.tuple,__builtin__.set,__builtin__.list)) else (v,))) for k,v in boolean.viewitems())
if not boolean:
for ea in internal.comment.contents.address(fn.startEA):
res = database.tag(ea)
if res: yield ea, res
return
for ea in internal.comment.contents.address(fn.startEA):
res,d = {},database.tag(ea)
Or = boolean.get('Or', set())
res.update((k,v) for k,v in d.iteritems() if k in Or)
And = boolean.get('And', set())
if And:
if And.intersection(d.viewkeys()) == And:
res.update((k,v) for k,v in d.iteritems() if k in And)
else: continue
if res: yield ea,res
return
## referencing
def down(func):
'''Return all the functions that are called by the function ``func``.'''
def codeRefs(fn):
resultData,resultCode = [],[]
for ea in iterate(fn):
if len(database.down(ea)) == 0:
if database.type.is_code(ea) and _instruction.is_call(ea):
logging.warn('{:s}.down({:x}) : Discovered a dynamically resolved call that is unable to be resolved. : {:s}'.format(__name__, fn.startEA, database.disasm(ea)))
#resultCode.append((ea, 0))
continue
resultData.extend( (ea,x) for x in database.dxdown(ea) )
resultCode.extend( (ea,x) for x in database.cxdown(ea) if fn.startEA == x or not contains(fn,x) )
return resultData,resultCode
fn = by(func)
return sorted(set(d for x,d in codeRefs(fn)[1]))
def set_color(cls, ea, rgb, **frame):
"""Sets the color of the basic-block at address ``ea`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
res, fn, bb = cls.get_color(ea), by_address(ea), cls.id(ea)
n = idaapi.node_info_t()
# specify the bgcolor
r,b = (rgb&0xff0000) >> 16, rgb&0x0000ff
n.bg_color = n.frame_color = (b<<16)|(rgb&0x00ff00)|r
# now the frame color
frgb = frame.get('frame', 0x000000)
fr, fb = (frgb & 0xff0000) >> 16, frgb&0x0000ff
n.frame_color = (fb<<16)|(frgb&0x00ff00)|fr
# set the node
f = (idaapi.NIF_BG_COLOR|idaapi.NIF_FRAME_COLOR) if frame else idaapi.NIF_BG_COLOR
try: idaapi.set_node_info2(fn.startEA, bb, n, f)
finally: idaapi.refresh_idaview_anyway()
# update the color of each item too
for ea in block.iterate(ea):
database.set_color(ea, rgb)
#internal.netnode.alt.set(ea, 0x14, n.bg_color)
return res
def color(cls, ea, rgb, **frame):
"""Sets the color of the basic-block at the address ``ea`` to ``rgb``.
If the color ``frame`` is specified, set the frame to the specified color.
"""
return cls.set_color(ea, rgb, **frame)