// DIE's signature file

init("compiler","MASM");

function isMASM32()
{
    bResult=0;

    var nOffset=PE.nEP;
    var nTemp;
    var nSize=128;

    while(nSize>0)
    {
        nTemp=PE.findByte(nOffset,nSize,0xE8)
        if(nTemp==-1)
        {
            break;
        }

        if(PE.compare("E8$$$$$$$$FF25",nTemp))
        {
            return 1;
        }

        nSize-=(nTemp-nOffset-1);
        nOffset=nTemp+1;
    }

    return bResult;
}

function getMASMVersion()
{
    var sResult="";

    // Original MASM32 from http://masm32.com...
    if(PE.isRichVersionPresent(8078)    // link.exe 5.12.8078
     &&PE.isRichVersionPresent(8444))   // ml.exe 6.14.8444
    {
        sName="MASM32";
        sResult="8-11";
    }
    else if(PE.isRichVersionPresent(8444))
    {
        sResult="6.14.8444";
    }
    else if(PE.isRichVersionPresent(50727))
    {
        sResult="8.00.50727";
    }
    else if(PE.isRichVersionPresent(21022))
    {
        sResult="9.00.21022";
    }
    else if(PE.isRichVersionPresent(30729))
    {
        sResult="9.00.30729"; // SP
    }
    /*else if(PE.isRichVersionPresent(30311)) // pre?
    {
        sResult="10.00.30311";
    }*/
    else if(PE.isRichVersionPresent(30319))
    {
        sResult="10.00.30319";
    }
    else if(PE.isRichVersionPresent(40219))
    {
        sResult="10.00.40219"; // SP
    }
    else if(PE.isRichVersionPresent(20827))
    {
        sResult="12.00.20827";
    }

    return sResult;
}

function detect(bShowType,bShowVersion,bShowOptions)
{
    var nNumberOfRichIDs=PE.getNumberOfRichIDs();

    // Usually MASM-compiled GUI exes have 3 IDs: link.exe, ml.exe and cvtres.exe;
    // DLLs and consoles have 2 IDs: link.exe and ml.exe.
    if((nNumberOfRichIDs>1)&&(nNumberOfRichIDs<=3)&&(!PE.isRichVersionPresent(0)))
    {
        bDetected=1;
        sVersion=getMASMVersion();
    }
    /* else // some packers/protectors have MASM code on entrypoint
    {
        if(isMASM32())
        {
            sOptions="EP stub";
            bDetected=1;
        }
    } */ // False positives :(
    if(bDetected&&PE.isLibraryPresent("MSVBVM60.DLL")) // VB
    {
        bDetected=0;
    }

    return result(bShowType,bShowVersion,bShowOptions);
}
