static int GetTypeOf(ea_t ea)
{
	flags_t flags = getFlags(ea);

	if (isTail(flags)) {
		// check if it's a struct ptr
		ea_t head = get_item_head(ea);
		if (head == ea) return T_UNK;
		flags = getFlags(head);
		if (!isData(flags) || !isStruct(flags)) return T_UNK;
		return GetTypeOfStructInstance(ea, head, flags);
	}

	if (!isData(flags)) return T_UNK;
	
	if (isStruct(flags)) return GetTypeOfStructInstance(ea, ea, flags);

	if (isByte(flags)) {
		char s;
		if (has_cmt(flags) && (get_cmt(ea, false, &s, 1) != -1) && s == 'S') return T_I8;
		return T_U8;
	}
	if (isWord(flags)) {
		char s;
		if (has_cmt(flags) && (get_cmt(ea, false, &s, 1) != -1) && s == 'S') return T_I16;
		return T_U16;
	}
	if (isDwrd(flags))  return T_I32;

	return T_UNK;
}
Ejemplo n.º 2
0
void idaapi run(int arg)
{
    char buf[MAXSTR];
    char cmt[MAXSTR];
    char *valid_cmt = NULL;
    char ext[0x20];
    FILE *f = NULL;
    short checkboxes = OPTION_NAMES | OPTION_COMMENTS;
    sval_t bank = 1; // default
    bool first = true;
    bool hasName = false;
    flags_t flags;
    ea_t ea = 0x0;


    if( AskUsingForm_c( madnes_options, &checkboxes, &bank ) != 1 || checkboxes == 0 )
        return;

    // prepare filename for namelist (.nl) file
    get_input_file_path( buf, sizeof( buf ) );
    qsnprintf( ext, sizeof( ext ),".%X.nl",--bank );
    qstrncat( buf, ext, sizeof( buf )-strlen( buf ) );

    // (always) create file
    f = qfopen( buf, "w" );
    if( f == NULL )
    {
        warning( "%s could not be created!", buf );
        return;
    }

    msg( "Writing to file %s..", buf );

    while( ea <= 0xFFFF )
    {
        hasName = false;

        // get flags
        if( isCode( getFlags( ea ) ) )
            flags = getFlags( ea );
        else
            flags = getFlags( get_item_head( ea ) );

        // if user either chose to export names or anynames
        if( ( ( checkboxes & OPTION_NAMES ) && has_name( flags ) ) || ( ( checkboxes & OPTION_ANYNAME ) && has_any_name( flags ) ) )
        {
            // if current item is code or if current item is head of item
            if( isCode( flags ) || ea==get_item_head( ea ) )
            {
                // get name
                get_name( ea, ea, buf, sizeof( buf ) );
                // write to file
                qfprintf( f, "$%04X#%s#", ea, buf );
            }
            else // if not code or not head of item (must be an array)
            {
                // get name of item start
                get_name( get_item_head( ea ), get_item_head( ea ), buf, sizeof( buf ) );
                // calc displacement, write to file (example: "password+$04")
                qfprintf( f, "$%04X#%s+$%X#", ea, buf, ea-get_item_head( ea ) );
            }
            hasName = true;
        }

        // if user chose to export cross references
        if( checkboxes & OPTION_XREFS )
        {
            xrefblk_t xb;

            first = true;
            // cycle through all xrefs except ordinary flow xrefs
            for ( bool ok=xb.first_to( ea, XREF_FAR/*XREF_ALL*/); ok; ok=xb.next_to() )
            {
                if( first ) // if first xref
                {
                    if( !hasName ) // if this location hasn't a name yet, add symbol stub
                    {
                        qfprintf( f, "$%04X##", ea );
                        hasName = true;
                    }
                    qfprintf( f, "XREFS:\n\\"); // append XREFS
                    first = false;
                }
                qfprintf( f, "  $%04X\n\\", xb.from );
            }
        }

        // if user chose to export comments
        if( checkboxes & OPTION_COMMENTS )
        {
            if( has_cmt( flags ) ) // if current item has comment
            {
                // get comment
                // workaround for get_any_indeted_cmt()
                // -> unresolved external symbol "char * __stdcall get_any_indented_cmt(unsigned long,unsigned char *)" (?get_any_indented_cmt@@YGPADKPAE@Z)
                if( get_cmt( ea, false, cmt, sizeof( cmt ) ) == -1 )
                    get_cmt( ea, true, cmt, sizeof( cmt ) );

                // validate comment (replace invalid chars, add room for additional chars)
                valid_cmt = validate_comment( cmt );

                if( valid_cmt != NULL )
                {
                    if( !hasName )
                    {
                        qfprintf( f, "$%04X##", ea ); // add symbol stub if no name yet
                        hasName = true;
                    }
                    qfprintf( f, "%s", valid_cmt ); // write comment to file
                    qfree( valid_cmt );
                }
            }
        }

        if( hasName) qfprintf( f, "\n" );
        ea++; // get name of each byte
    }
    qfclose( f );
    msg( "done.\n" );
}