// Detect a RAR archive.
// Author: Jason Hood <jadoxa@yahoo.com.au>

includeScript("archive-file");
sName = "RAR";

function detect_RAR(bFile, bShowOptions) {
    var nOffset, nSize;

    if (bFile) {
        nOffset = 0;
        nSize = File.getSize();
    } else {
        nOffset = File.getOverlayOffset();
        nSize = File.getOverlaySize();
    }

    if (nSize >= 64) {
        if (File.compare("'Rar!'1A07", nOffset)) {
            switch (File.readByte(nOffset + 6)) {
                case 0:
                    sVersion = "4";
                    break;
                case 1:
                    sVersion = "5";
                    break;
            }

            bDetected = true;
        } else if (File.compare("'RE~^'", nOffset)) {
            sVersion = "1.4";
            bDetected = true;
        }

        if (sVersion == "4") {

            var nOptions = File.readByte(nOffset + 10);

            if (nOptions & 8) {
                sOptions = "solid";
            }

            if (nOptions & 0x80) {
                sOptions += (sOptions !== String() ? ", " : "") + "encrypted";
            } else if (bShowOptions) {
                var nDelta = 0;

                nOffset += 7 + File.readWord(nOffset + 12);
                while (File.readByte(nOffset + 2) != 0x74) {
                    nDelta = File.readWord(nOffset + 5) + File.readDword(nOffset + 7);

                    if (!nDelta) {
                        break;
                    }

                    nOffset += nDelta;
                }

                while (File.readByte(nOffset + 2) == 0x74) {
                    var nLen = File.readDword(nOffset + 7);
                    Archive.add(File.readDword(nOffset + 11), nLen,
                        (File.readByte(nOffset + 3) & 0xE0) == 0xE0);
                    nDelta = File.readWord(nOffset + 5) + nLen;
                    if (!nDelta) {
                        break;
                    }

                    nOffset += nDelta;
                }

                sOptions = sOptions.append(Archive.contents());
            }
        }
    }
}