Beispiel #1
0
def add_enums(function):
    """ Add standard enums from parsed MSDN documentation for all imported
    library calls and their arguments.

    Arguments:
    function -- function object
    """
    enum_count = 0
    for argument in function.arguments:
        # Add standard enums
        if not argument.enums:
            g_logger.debug(' No standard constants available for %s' %
                           argument.name)
        else:
            for enum in argument.enums:
                g_logger.debug('  Importing enum %s for argument %s' %
                               (enum, argument.name))
                if idc.Til2Idb(-1, enum) != idaapi.BADADDR:
                    g_logger.debug('  ' + enum + ' ' + hex(idc.GetEnum(enum)) +
                                   ' added successfully')
                    enum_count = enum_count + 1
                else:
                    g_logger.debug('  Could not add ' + enum)

        if not argument.constants:
            # No constants for this argument
            continue

        argument.name = argument.name.encode('utf-8')
        function.name = function.name.encode('utf-8')

        # Add constant descriptions
        for constant in argument.constants:
            constant.name = constant.name.encode('utf-8')

            if constant.name == 'NULL':
                # Create unique name, so we can add descriptive comment to it
                constant.name = 'NULL_{}_{}'.format(argument.name,
                                                    function.name)
                # Add custom enum for NULL values if it does not exist yet
                enumid = idc.GetEnum(NULL_ENUM_NAME)
                if enumid == idaapi.BADADDR:
                    enumid = idc.AddEnum(-1, NULL_ENUM_NAME, idaapi.hexflag())
                idc.AddConstEx(enumid, constant.name, 0, -1)
                constid = idc.GetConstByName(constant.name)
                idc.SetConstCmt(constid, format_comment(constant.description),
                                False)
            else:
                constid = idc.GetConstByName(constant.name)
                if constid:
                    if idc.SetConstCmt(constid,
                                       format_comment(constant.description),
                                       False):
                        g_logger.debug('    Description added for %s' %
                                       constant.name)
                    else:
                        g_logger.debug('    No description added for %s' %
                                       constant.name)
    return enum_count
Beispiel #2
0
 def yatest_enums(self):
     values = []
     for flag in flags:
         for prefix, enum_width, is_bitfield, num_fields in tests:
             name = '%s_%x_%d_%x' % (prefix, enum_width, is_bitfield, flag)
             ea = None
             eid = idc.AddEnum(-1, name, flag)
             self.assertNotEqual(eid, idaapi.BADADDR)
             if enum_width != 0:
                 idc.SetEnumWidth(eid, enum_width)
             if is_bitfield:
                 self.assertTrue(idc.SetEnumBf(eid, True))
             idc.SetEnumCmt(eid, prefix + 'cmt', False)
             idc.SetEnumCmt(eid, prefix + 'rpt', True)
             for n in range(0, num_fields):
                 field = '%s_%d' % (name , n)
                 cid = None
                 if is_bitfield:
                     self.assertEqual(idc.AddConstEx(eid, field, 1 << n, 1 << n), 0)
                 else:
                     self.assertEqual(idc.AddConst(eid, field, n), 0)
                 if n == 0:
                     ea = get_ea()
                     self.assertNotEqual(idaapi.op_enum(ea, 1, eid, 0), idaapi.BADADDR)
                 cid = idc.GetConstByName(field)
                 self.assertTrue(idc.SetConstCmt(cid, field + 'cmt', False))
                 #self.assertTrue(idc.SetConstCmt(cid, field + 'rpt', True))
             values.append((name, ea))
     yaunit.save('enums', values)
Beispiel #3
0
 def setMember(self, name, val, maskVal=None):
     const = idc.GetConstByName(name)
     if const and const != idc.BADADDR:
         # remove constant
         self.deleteConst(const)
     idc.AddConstEx(self.id, name, val, maskVal if maskVal else idc.BADADDR)