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

includeScript("archive");
sName="Microsoft Cabinet File";

function detect_Cab(nOffset,nSize,bShowOptions)
{
    if(nSize>48)
    {
        if(File.compare("'MSCF'00000000",nOffset))
        {
            bDetected=1;
            sVersion=File.readByte(nOffset+0x19)+".0"+File.readByte(nOffset+0x18);
            var nFilesOffset=nOffset+File.readDword(nOffset+0x10);
            var nFiles=File.readWord(nOffset+0x1C);
            var nPacked=File.readDword(nOffset+8);
            if(File.readByte(nOffset+0x1E)&4)
            {
                nOffset+=File.readDword(nOffset+0x24)+4;
            }
            switch(File.readByte(nOffset+0x2A)&15)
            {
            case 1: sOptions=sOptions.append("MSZip");   break;
            case 2: sOptions=sOptions.append("Quantum"); break;
            case 3: sOptions=sOptions.append("LZX");     break;
            }
            if(bShowOptions)
            {
                // Packed size is not stored directly, so assume the first
                // folder's data is first and just skip the names.
                nPacked-=File.readDword(nOffset+0x24);
                while(nFiles--)
                {
                    Archive.add(File.readDword(nFilesOffset),nPacked,
                                File.readByte(nFilesOffset+14)&16);
                    nFilesOffset=File.findByte(nFilesOffset+16,256,0)+1;
                    nPacked=0;
                }
                sOptions=sOptions.append(Archive.contents());
            }
            return 1;
        }
    }

    return 0;
}
