Ejemplo n.º 1
0
        List <MixEntry> ParseRaHeader(VirtualFile reader, out long dataStart)
        {
            //BinaryReader reader = new BinaryReader(s);
            byte[] keyblock    = reader.Read(80);
            byte[] blowfishKey = new BlowfishKeyProvider().DecryptKey(keyblock);

            uint[] h = ReadUints(reader, 2);

            var          fish    = new Blowfish(blowfishKey);
            MemoryStream ms      = Decrypt(h, fish);
            var          reader2 = new BinaryReader(ms);

            ushort numFiles = reader2.ReadUInt16();

            reader2.ReadUInt32();             /*datasize*/

            reader.Position = headerStart;

            int byteCount = 6 + numFiles * MixEntry.Size;

            h = ReadUints(reader, (byteCount + 3) / 4);

            ms = Decrypt(h, fish);

            dataStart = headerStart + byteCount + ((~byteCount + 1) & 7);

            long ds;

            return(ParseTdHeader(new VirtualFile(ms), out ds));
        }
Ejemplo n.º 2
0
        public bool IsValid()
        {
            Position = 0;
            uint signature = ReadUInt32();

            if ((signature & ~(uint)(MixFileFlags.Encrypted | MixFileFlags.Checksum)) != 0)
            {
                return(false);
            }
            if ((signature & (uint)MixFileFlags.Encrypted) != 0)
            {
                byte[] keyblock    = Read(80);
                byte[] blowfishKey = new BlowfishKeyProvider().DecryptKey(keyblock);

                uint[]       h       = ReadUints(this, 2);
                var          fish    = new Blowfish(blowfishKey);
                MemoryStream ms      = Decrypt(h, fish);
                var          reader2 = new BinaryReader(ms);

                ushort numFiles = reader2.ReadUInt16();
                uint   dataSize = reader2.ReadUInt32();               /*datasize*/
                return(numFiles > 0 && 84 + (6 + numFiles * 12 + 7 & ~7) + dataSize + ((signature & (uint)MixFileFlags.Checksum) != 0 ? 20 : 0) == Length);
            }
            else
            {
                ushort numFiles = ReadUInt16();
                uint   dataSize = ReadUInt32();
                return(numFiles > 0 && 4 + 6 + numFiles * 12 + dataSize + ((signature & (uint)MixFileFlags.Checksum) != 0 ? 20 : 0) == Length);
            }
        }
Ejemplo n.º 3
0
        private IndexEntry[] ReadHeader(Stream stream, out int body_offset)
        {
            BinaryReader reader = new BinaryReader(stream);
            uint         flags  = (this.version == MixFileVersion.CNC) ? 0 : reader.ReadUInt32();
            int          nfiles = 0;

            IndexEntry[] index;

            // The mix is encrypted.
            if ((flags & FlagEncrypted) != 0)
            {
                // Read the Mix key and convert it to a Blowfish key.
                byte[]   key_source = reader.ReadBytes(80);
                byte[]   key        = new BlowfishKeyProvider().DecryptKey(key_source);
                Blowfish bf         = new Blowfish(key);

                // Parse the header.
                byte[] header_buf = stream.ReadBytes(8);
                bf.Decipher(header_buf, 8);
                this.ParseHeader(header_buf, out nfiles);

                // Parse the index.
                int    index_size = (12 * nfiles + 5) & ~7;
                byte[] index_buf  = stream.ReadBytes(index_size);
                bf.Decipher(index_buf, index_size);
                Array.Copy(index_buf, 0, index_buf, 2, index_size - 2);
                Array.Copy(header_buf, 6, index_buf, 0, 2);
                this.ParseIndex(index_buf, nfiles, out index);
            }
            else
            {
                byte[] header_buf = new byte[6];
                stream.Read(header_buf, 0, 6);
                this.ParseHeader(header_buf, out nfiles);

                byte[] index_buf = new byte[12 * nfiles];
                stream.Read(index_buf, 0, 12 * nfiles);
                this.ParseIndex(index_buf, nfiles, out index);
            }

            body_offset = (int)stream.Position;
            return(index);
        }
Ejemplo n.º 4
0
        static MemoryStream DecryptHeader(Stream s, long offset, out long headerEnd)
        {
            s.Seek(offset, SeekOrigin.Begin);

            // Decrypt blowfish key
            var keyblock    = s.ReadBytes(80);
            var blowfishKey = new BlowfishKeyProvider().DecryptKey(keyblock);
            var fish        = new Blowfish(blowfishKey);

            // Decrypt first block to work out the header length
            var ms       = Decrypt(ReadBlocks(s, offset + 80, 1), fish);
            var numFiles = ms.ReadUInt16();

            // Decrypt the full header - round bytes up to a full block
            var blockCount = (13 + numFiles * PackageEntry.Size) / 8;

            headerEnd = offset + 80 + blockCount * 8;

            return(Decrypt(ReadBlocks(s, offset + 80, blockCount), fish));
        }
Ejemplo n.º 5
0
        MemoryStream DecryptHeader(VirtualFile reader)
        {
            byte[] keyblock    = reader.Read(80);
            byte[] blowfishKey = new BlowfishKeyProvider().DecryptKey(keyblock);

            // Decrypt just the 1st block to determine the number of items, and thereby the length of the header
            var fish = new Blowfish(blowfishKey);

            uint[] h = fish.Decrypt(ReadUints(reader, 2));

            // First 2 decrypted bytes indicate number of files defined in header
            ushort numFiles = (ushort)(h[0] & 0xFFFF);
            // now we can determine the actual header length, rounded up to full number of blocks
            const int blockSize    = 8;
            int       headerLength = (6 + numFiles * MixEntry.Size + (blockSize - 1)) & ~(blockSize - 1);

            // Decrypt full header
            reader.Position = 84;
            return(Decrypt(ReadUints(reader, headerLength / 4), fish));
        }
Ejemplo n.º 6
0
        private MemoryStream DecryptHeader(long offset, out long headerEnd)
        {
            rawStream.Seek(offset, SeekOrigin.Begin);

            // Decrypt blowfish key
            var keyblock    = rawStream.ReadBytes(80);
            var blowfishKey = new BlowfishKeyProvider().DecryptKey(keyblock);
            var fish        = new Blowfish(blowfishKey);

            // Decrypt first block to work out the header length
            var ms       = new BinaryReader(Decrypt(ReadBlocks(offset + 80, 1), fish));
            var numFiles = ms.ReadUInt16();

            // Decrypt the full header - round bytes up to a full block
            var blockCount = (13 + numFiles * 12) / 8;

            headerEnd = offset + 80 + blockCount * 8;

            return(Decrypt(ReadBlocks(offset + 80, blockCount), fish));
        }