Пример #1
0
 def name(cls, enum, member, name, *suffix):
     '''Rename the enumeration `member` belonging to `enum` to `name`.'''
     eid = by(enum)
     mid = members.by(eid, member)
     res = (name, ) + suffix
     return idaapi.set_enum_member_name(
         mid, utils.string.to(interface.tuplename(*res)))
Пример #2
0
    def add(cls, enum, name, value, **bitmask):
        """Add an enumeration member `name` with the specified `value` to the enumeration `enum`.

        If the int, `bitmask`, is specified then used it as the bitmask for the enumeration.
        """
        eid = by(enum)
        bmask = bitmask.get('bitmask', idaapi.BADADDR & mask(eid))

        res = interface.tuplename(name) if isinstance(name, tuple) else name
        ok = idaapi.add_enum_member(eid, utils.string.to(res), value, bmask)

        err = {
            getattr(idaapi, item): item
            for item in [
                'ENUM_MEMBER_ERROR_NAME', 'ENUM_MEMBER_ERROR_VALUE',
                'ENUM_MEMBER_ERROR_ENUM', 'ENUM_MEMBER_ERROR_MASK',
                'ENUM_MEMBER_ERROR_ILLV'
            ]
        }
        if ok in err.keys():
            raise E.DisassemblerError(
                u"{:s}.add({:#x}, {!r}, {:#x}{:s}) : Unable to add member to enumeration due to error {:s}({:d})."
                .format(
                    '.'.join([__name__, cls.__name__]), eid, name, value,
                    u", {:s}".format(utils.string.kwargs(bitmask))
                    if bitmask else '', err[ok], ok))
        return eid
Пример #3
0
def name(id, string, *suffix):
    '''Set the name of the structure identified by ``id`` to ``string``.'''
    res = (string, ) + suffix
    string = interface.tuplename(*res)

    res = idaapi.validate_name2(buffer(string)[:])
    if string and string != res:
        logging.warn(
            "{:s}.name : Stripping invalid chars from structure name \"{:s}\". : {!r}"
            .format(__name__, string, res))
        string = res
    return idaapi.set_struc_name(id, string)
Пример #4
0
    def name(self, string):
        '''Set the name for the structure to ``string``.'''
        if isinstance(string, tuple):
            string = interface.tuplename(*string)

        res = idaapi.validate_name2(buffer(string)[:])
        if string and string != res:
            logging.warn(
                "{:s}.name : Stripping invalid chars from structure name {!r}. : {!r}"
                .format('.'.join((__name__, self.__class__.__name__)), string,
                        res))
            string = res
        return idaapi.set_struc_name(self.id, string)
Пример #5
0
    def add(cls, enum, name, value, **bitmask):
        """Add an enumeration member `name` with the specified `value` to the enumeration `enum`.

        If the int, `bitmask`, is specified then used it as the bitmask for the enumeration.
        """
        eid = by(enum)
        bmask = bitmask.get('bitmask', idaapi.BADADDR & mask(eid))

        res = interface.tuplename(name) if isinstance(name, tuple) else name
        ok = idaapi.add_enum_member(eid, utils.string.to(res), value, bmask)

        err = {getattr(idaapi, n) : n for n in ('ENUM_MEMBER_ERROR_NAME', 'ENUM_MEMBER_ERROR_VALUE', 'ENUM_MEMBER_ERROR_ENUM', 'ENUM_MEMBER_ERROR_MASK', 'ENUM_MEMBER_ERROR_ILLV')}
        if ok in err.viewkeys():
            raise E.DisassemblerError(u"{:s}.add({:#x}, {!r}, {:#x}{:s}) : Unable to add member to enumeration due to error {:s}({:d}).".format('.'.join((__name__, cls.__name__)), eid, name, value, u", {:s}".format(utils.string.kwargs(bitmask)) if bitmask else '', err[ok], ok))
        return eid
Пример #6
0
    def name(self, string):
        '''Set the member's name to ``string``.'''
        if isinstance(string, tuple):
            string = interface.tuplename(*string)

        res = idaapi.validate_name2(buffer(string)[:])
        if string and string != res:
            logging.warn(
                "{:s}.name : Stripping invalid chars from structure \"{:s}\" member {:d} name {!r}. : {!r}"
                .format('.'.join((__name__, self.__class__.__name__)),
                        self.__owner.name, self.__index, string, res))
            string = res
        return idaapi.set_member_name(
            self.__owner.ptr, self.offset - self.__owner.members.baseoffset,
            string)
Пример #7
0
    def add(self, name, type, offset):
        """Add a member at ``offset`` with the given ``name`` and ``type``.
        To specify a particular size, ``type`` can be a tuple with the second element referring to the size.
        """
        flag, typeid, nbytes = interface.typemap.resolve(type)

        # FIXME: handle .strtype (strings), .ec (enums), .cd (custom)
        opinfo = idaapi.opinfo_t()
        opinfo.tid = typeid
        realoffset = offset - self.baseoffset

        if name is None:
            logging.warn(
                "{:s}.instance({:s}).members.add : name is undefined, defaulting to offset {:+#x}"
                .format(__name__, self.owner.name, realoffset))
            name = 'v', realoffset
        if isinstance(name, tuple):
            name = interface.tuplename(*name)

        res = idaapi.add_struc_member(self.owner.ptr, name, realoffset, flag,
                                      opinfo, nbytes)
        if res == idaapi.STRUC_ERROR_MEMBER_OK:
            logging.info(
                "{:s}.instance({:s}).members.add : idaapi.add_struc_member(sptr={!r}, fieldname={:s}, offset={:+#x}, flag={:#x}, mt={:#x}, nbytes={:#x}) : Success"
                .format(__name__, self.owner.name, self.owner.name, name,
                        realoffset, flag, typeid, nbytes))
        else:
            error = {
                idaapi.STRUC_ERROR_MEMBER_NAME: 'Duplicate field name',
                idaapi.STRUC_ERROR_MEMBER_OFFSET: 'Invalid offset',
                idaapi.STRUC_ERROR_MEMBER_SIZE: 'Invalid size',
            }
            callee = "idaapi.add_struc_member(sptr={!r}, fieldname={:s}, offset={:+#x}, flag={:#x}, mt={:#x}, nbytes={:#x})".format(
                self.owner.name, name, realoffset, flag, typeid, nbytes)
            logging.fatal(' : '.join(
                ('members_t.add', callee,
                 error.get(res, "Error code {:#x}".format(res)))))
            return None

        res = idaapi.get_member(self.owner.ptr, realoffset)
        if res is None:
            logging.fatal(
                "{:s}.instance({:s}.members.add : Failed creating member {!r} {:s}:{:+#x}"
                .format(__name__, self.owner.name, name, realoffset, nbytes))

        # sloppily figure out what the correct index is
        idx = self.index(idaapi.get_member(self.owner.ptr, realoffset))
        return member_t(self.owner, idx)
Пример #8
0
 def name(cls, mid, name):
     '''Rename the enumeration member `mid` to `name`.'''
     res = interface.tuplename(*name) if isinstance(name, tuple) else name
     return idaapi.set_enum_member_name(mid, utils.string.to(res))
Пример #9
0
 def name(cls, enum, member, name, *suffix):
     '''Rename the enumeration `member` belonging to `enum` to `name`.'''
     eid = by(enum)
     mid = members.by(eid, member)
     res = (name,) + suffix
     return idaapi.set_enum_member_name(mid, utils.string.to(interface.tuplename(*res)))
Пример #10
0
 def name(cls, mid, name):
     '''Rename the enumeration member `mid` to `name`.'''
     res = interface.tuplename(*name) if isinstance(name, tuple) else name
     return idaapi.set_enum_member_name(mid, utils.string.to(res))