Esempio n. 1
0
        protected Stream GetLocalDataStreamInternal(IndexEntry idxInfo, MD5Hash key)
        {
            if (idxInfo == null)
            {
                throw new Exception("local index missing");
            }

            Stream dataStream = GetDataStream(idxInfo.Index);

            dataStream.Position = idxInfo.Offset;

            using (BinaryReader reader = new BinaryReader(dataStream, Encoding.ASCII, true))
            {
                byte[] md5 = reader.ReadBytes(16);
                Array.Reverse(md5);

                if (!key.EqualsTo(md5))
                {
                    throw new Exception("local data corrupted");
                }

                int size = reader.ReadInt32();

                if (size != idxInfo.Size)
                {
                    throw new Exception("local data corrupted");
                }

                //byte[] unkData1 = reader.ReadBytes(2);
                //byte[] unkData2 = reader.ReadBytes(8);
                dataStream.Position += 10;

                byte[] data = reader.ReadBytes(idxInfo.Size - 30);

                return(new MemoryStream(data));
            }
        }
Esempio n. 2
0
        private void Parse(MD5Hash md5)
        {
            int size = (int)_reader.BaseStream.Length;

            if (size < 8)
            {
                throw new BLTEDecoderException("not enough data: {0}", 8);
            }

            int magic = _reader.ReadInt32();

            if (magic != BLTE_MAGIC)
            {
                throw new BLTEDecoderException("frame header mismatch (bad BLTE file)");
            }

            int headerSize = _reader.ReadInt32BE();

            if (CASCConfig.ValidateData)
            {
                long oldPos = _reader.BaseStream.Position;

                _reader.BaseStream.Position = 0;

                byte[] newHash = _md5.ComputeHash(_reader.ReadBytes(headerSize > 0 ? headerSize : size));

                if (!md5.EqualsTo(newHash))
                {
                    throw new BLTEDecoderException("data corrupted");
                }

                _reader.BaseStream.Position = oldPos;
            }

            int numBlocks = 1;

            if (headerSize > 0)
            {
                if (size < 12)
                {
                    throw new BLTEDecoderException("not enough data: {0}", 12);
                }

                byte[] fcbytes = _reader.ReadBytes(4);

                numBlocks = fcbytes[1] << 16 | fcbytes[2] << 8 | fcbytes[3] << 0;

                if (fcbytes[0] != 0x0F || numBlocks == 0)
                {
                    throw new BLTEDecoderException("bad table format 0x{0:x2}, numBlocks {1}", fcbytes[0], numBlocks);
                }

                int frameHeaderSize = 24 * numBlocks + 12;

                if (headerSize != frameHeaderSize)
                {
                    throw new BLTEDecoderException("header size mismatch");
                }

                if (size < frameHeaderSize)
                {
                    throw new BLTEDecoderException("not enough data: {0}", frameHeaderSize);
                }
            }

            DataBlock[] blocks = new DataBlock[numBlocks];

            for (int i = 0; i < numBlocks; i++)
            {
                DataBlock block = new DataBlock();

                if (headerSize != 0)
                {
                    block.CompSize   = _reader.ReadInt32BE();
                    block.DecompSize = _reader.ReadInt32BE();
                    block.Hash       = _reader.Read <MD5Hash>();
                }
                else
                {
                    block.CompSize   = size - 8;
                    block.DecompSize = size - 8 - 1;
                    block.Hash       = default(MD5Hash);
                }

                blocks[i] = block;
            }

            _memStream = new MemoryStream(blocks.Sum(b => b.DecompSize));

            for (int i = 0; i < blocks.Length; i++)
            {
                DataBlock block = blocks[i];

                block.Data = _reader.ReadBytes(block.CompSize);

                if (!block.Hash.IsZeroed() && CASCConfig.ValidateData)
                {
                    byte[] blockHash = _md5.ComputeHash(block.Data);

                    if (!block.Hash.EqualsTo(blockHash))
                    {
                        throw new BLTEDecoderException("MD5 mismatch");
                    }
                }

                HandleDataBlock(block.Data, i);
            }
        }
Esempio n. 3
0
        private void Parse(MD5Hash hash)
        {
            int size = (int)reader.BaseStream.Length;

            if (size < 8)
                throw new BLTEDecoderException("Invalid data length: {0}", 8);

            int magic = reader.ReadInt32();
            if (magic != BLTE_MAGIC)
                throw new BLTEDecoderException("Mis-matched header (magic)");

            int headerSize = reader.ReadInt32BE();
            if (CASCConfig.ValidateData)
            {
                long oldPos = reader.BaseStream.Position;
                reader.BaseStream.Position = 0;

                byte[] newHash = this.hash.ComputeHash(reader.ReadBytes(headerSize > 0 ? headerSize : size));
                if (!hash.EqualsTo(newHash))
                    throw new BLTEDecoderException("Corrupted data?");

                reader.BaseStream.Position = oldPos;
            }

            int numBlocks = 1;
            if (headerSize > 0)
            {
                if (size < 12)
                    throw new BLTEDecoderException("Not enough data: {0}", 12);

                byte[] bytes = reader.ReadBytes(4);
                numBlocks = bytes[1] << 16 | bytes[2] << 8 | bytes[3] << 0;

                if (bytes[0] != 0x0F || numBlocks == 0)
                    throw new BLTEDecoderException("Invalid table format 0x{0:x2}, numBlocks {1}", bytes[0], numBlocks);

                int frameHeaderSize = 24 * numBlocks + 12;
                if (headerSize != frameHeaderSize)
                    throw new BLTEDecoderException("Header size mis-match");

                if (size < frameHeaderSize)
                    throw new BLTEDecoderException("Not enough data: {0}", frameHeaderSize);
            }

            dataBlocks = new DataBlock[numBlocks];
            for (int i = 0; i < numBlocks; i++)
            {
                DataBlock block = new DataBlock();

                if (headerSize != 0)
                {
                    block.CompSize = reader.ReadInt32BE();
                    block.DecompSize = reader.ReadInt32BE();
                    block.Hash = reader.Read<MD5Hash>();
                }
                else
                {
                    block.CompSize = size - 8;
                    block.DecompSize = size - 8 - 1;
                    block.Hash = default(MD5Hash);
                }

                dataBlocks[i] = block;
            }

            streamMemory = new MemoryStream(dataBlocks.Sum(b => b.DecompSize));
            ProcessNextBlock();

            length = headerSize == 0 ? streamMemory.Length : streamMemory.Capacity;
        }
Esempio n. 4
0
        private Stream GetLocalDataStreamInternal(IndexEntry idxInfo, MD5Hash key)
        {
            if (idxInfo == null)
                throw new Exception("Missing local index.");

            Stream dataStream = GetDataStream(idxInfo.Index);
            dataStream.Position = idxInfo.Offset;

            using (BinaryReader reader = new BinaryReader(dataStream, System.Text.Encoding.ASCII, true))
            {
                byte[] md5 = reader.ReadBytes(16);
                Array.Reverse(md5);

                if (!key.EqualsTo(md5))
                    throw new Exception("local data corrupted");

                int size = reader.ReadInt32();

                if (size != idxInfo.Size)
                    throw new Exception("local data corrupted");

                //byte[] unkData1 = reader.ReadBytes(2);
                //byte[] unkData2 = reader.ReadBytes(8);
                dataStream.Position += 10;

                byte[] data = reader.ReadBytes(idxInfo.Size - 30);

                return new MemoryStream(data);
            }
        }
Esempio n. 5
0
        private void Parse(MD5Hash hash)
        {
            int size = (int)reader.BaseStream.Length;

            if (size < 8)
            {
                throw new BLTEDecoderException("Invalid data length: {0}", 8);
            }

            int magic = reader.ReadInt32();

            if (magic != BLTE_MAGIC)
            {
                throw new BLTEDecoderException("Mis-matched header (magic)");
            }

            int headerSize = reader.ReadInt32BE();

            if (CASCConfig.ValidateData)
            {
                long oldPos = reader.BaseStream.Position;
                reader.BaseStream.Position = 0;

                byte[] newHash = this.hash.ComputeHash(reader.ReadBytes(headerSize > 0 ? headerSize : size));
                if (!hash.EqualsTo(newHash))
                {
                    throw new BLTEDecoderException("Corrupted data?");
                }

                reader.BaseStream.Position = oldPos;
            }

            int numBlocks = 1;

            if (headerSize > 0)
            {
                if (size < 12)
                {
                    throw new BLTEDecoderException("Not enough data: {0}", 12);
                }

                byte[] bytes = reader.ReadBytes(4);
                numBlocks = bytes[1] << 16 | bytes[2] << 8 | bytes[3] << 0;

                if (bytes[0] != 0x0F || numBlocks == 0)
                {
                    throw new BLTEDecoderException("Invalid table format 0x{0:x2}, numBlocks {1}", bytes[0], numBlocks);
                }

                int frameHeaderSize = 24 * numBlocks + 12;
                if (headerSize != frameHeaderSize)
                {
                    throw new BLTEDecoderException("Header size mis-match");
                }

                if (size < frameHeaderSize)
                {
                    throw new BLTEDecoderException("Not enough data: {0}", frameHeaderSize);
                }
            }

            dataBlocks = new DataBlock[numBlocks];
            for (int i = 0; i < numBlocks; i++)
            {
                DataBlock block = new DataBlock();

                if (headerSize != 0)
                {
                    block.CompSize   = reader.ReadInt32BE();
                    block.DecompSize = reader.ReadInt32BE();
                    block.Hash       = reader.Read <MD5Hash>();
                }
                else
                {
                    block.CompSize   = size - 8;
                    block.DecompSize = size - 8 - 1;
                    block.Hash       = default(MD5Hash);
                }

                dataBlocks[i] = block;
            }

            streamMemory = new MemoryStream(dataBlocks.Sum(b => b.DecompSize));
            ProcessNextBlock();

            length = headerSize == 0 ? streamMemory.Length : streamMemory.Capacity;
        }
Esempio n. 6
0
        private void Parse(MD5Hash md5)
        {
            int size = (int)_reader.BaseStream.Length;

            if (size < 8)
                throw new BLTEDecoderException("not enough data: {0}", 8);

            int magic = _reader.ReadInt32();

            if (magic != BLTE_MAGIC)
                throw new BLTEDecoderException("frame header mismatch (bad BLTE file)");

            int headerSize = _reader.ReadInt32BE();

            if (CASCConfig.ValidateData)
            {
                long oldPos = _reader.BaseStream.Position;

                _reader.BaseStream.Position = 0;

                byte[] newHash = _md5.ComputeHash(_reader.ReadBytes(headerSize > 0 ? headerSize : size));

                if (!md5.EqualsTo(newHash))
                    throw new BLTEDecoderException("data corrupted");

                _reader.BaseStream.Position = oldPos;
            }

            int numBlocks = 1;

            if (headerSize > 0)
            {
                if (size < 12)
                    throw new BLTEDecoderException("not enough data: {0}", 12);

                byte[] fcbytes = _reader.ReadBytes(4);

                numBlocks = fcbytes[1] << 16 | fcbytes[2] << 8 | fcbytes[3] << 0;

                if (fcbytes[0] != 0x0F || numBlocks == 0)
                    throw new BLTEDecoderException("bad table format 0x{0:x2}, numBlocks {1}", fcbytes[0], numBlocks);

                int frameHeaderSize = 24 * numBlocks + 12;

                if (headerSize != frameHeaderSize)
                    throw new BLTEDecoderException("header size mismatch");

                if (size < frameHeaderSize)
                    throw new BLTEDecoderException("not enough data: {0}", frameHeaderSize);
            }

            _dataBlocks = new DataBlock[numBlocks];

            for (int i = 0; i < numBlocks; i++)
            {
                DataBlock block = new DataBlock();

                if (headerSize != 0)
                {
                    block.CompSize = _reader.ReadInt32BE();
                    block.DecompSize = _reader.ReadInt32BE();
                    block.Hash = _reader.Read<MD5Hash>();
                }
                else
                {
                    block.CompSize = size - 8;
                    block.DecompSize = size - 8 - 1;
                    block.Hash = default(MD5Hash);
                }

                _dataBlocks[i] = block;
            }

            _memStream = new MemoryStream(_dataBlocks.Sum(b => b.DecompSize));

            ProcessNextBlock();

            _length = headerSize == 0 ? _memStream.Length : _memStream.Capacity;

            //for (int i = 0; i < _dataBlocks.Length; i++)
            //{
            //    ProcessNextBlock();
            //}
        }