// DIE's signature file

init("compiler", "MinGW");

includeScript("FPC");

function getMinGWVersion() {
    var sResult = "";

    var rdataSection = PE.section[".rdata"];

    if (rdataSection) {
        var nOffset = rdataSection.FileOffset,
            nSize = rdataSection.FileSize,
            nVersionOffset = PE.findString(nOffset, nSize, "GCC: ");

        if (nVersionOffset != -1) {
            sResult = PE.getString(nVersionOffset, 128);
        }

        if (sResult == "") {
            var nVersionOffset = PE.findString(nOffset, nSize, "gcc-");
            if (nVersionOffset != -1) {
                sResult = PE.getString(nVersionOffset);
                sResult = sResult.substring(0, sResult.indexOf('/'));
            }
        }
    }

    return sResult;
}

function detect(bShowType, bShowVersion, bShowOptions) {
    /*if(PE.compareEP("5589E583EC08C7042401000000FF15........E8C8FEFFFF908DB426000000005589E583EC08C7042402000000FF15"))
    {
        bDetected=1;
    }
    else if(PE.compareEP("5589E583EC0883C4F46A..A1........FFD0E8..FFFFFF"))
    {
        bDetected=1;
    }
    else if(PE.compareEP("5589E583EC146A..FF15....................................00000000"))
    {
        bDetected=1;
    }
    else */

    if (PE.getMajorLinkerVersion() == 2) {
        if (!bFPC && (PE.getMinorLinkerVersion() <= 30 || PE.getMinorLinkerVersion() == 36 || PE.getMinorLinkerVersion() == 56)) {
            if (PE.compare("'MZ'90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21'This program cannot be run in DOS mode.\r\r\n$'00000000000000'PE'0000")) {
                if (!PE.section[".rsrc"]) {
                    bDetected = true;
                } else {
                    var nOffset = PE.section[".rsrc"].FileOffset;
                    var nSize = PE.section[".rsrc"].VirtualSize;
                    if (!PE.isSignaturePresent(nOffset + nSize - 512, 512, "'Microsoft Corp.'")) {
                        bDetected = true;
                    }
                }
            }
        }
    }

    if (bDetected) {
        sVersion = getMinGWVersion();
    } else {
        var buildidSection = PE.section[".buildid"],
            rdataSection = PE.section[".rdata"];

        if (buildidSection) {
            bDetected = PE.isSignaturePresent(rdataSection.FileOffset, rdataSection.FileSize, "'Mingw'");
        }
    }

    _setLang("C/C++", bDetected);
    return result(bShowType, bShowVersion, bShowOptions);
}