Ejemplo n.º 1
0
        private static MemoryStream BlteGetData(BinaryReader reader, long size, long uncompressedSize)
        {
            if (reader.ReadUInt32() != 0x45544C42)
            {
                throw new InvalidOperationException("Invalid file in archive. Invalid BLTE header");
            }

            var  sizeFrameHeader = reader.ReadUInt32Be();
            uint numChunks;
            var  totalSize = 0L;

            if (sizeFrameHeader == 0)
            {
                numChunks = 1;
                totalSize = size - 8;
            }
            else
            {
                if (reader.ReadByte() != 0x0F)
                {
                    throw new InvalidOperationException("Unknown error in BLTE: unk1 != 0x0F. This is not good im told.");
                }

                var sizes = reader.ReadBytes(3);
                numChunks = (uint)((sizes[0] << 16) | (sizes[1] >> 8) | sizes[2]);
            }

            if (numChunks == 0)
            {
                return(new MemoryStream());
            }

            var chunks = new BlteChunk[numChunks];

            for (var i = 0; i < numChunks; ++i)
            {
                var chunk = new BlteChunk();
                chunks[i] = chunk;
                if (sizeFrameHeader != 0)
                {
                    chunk.SizeCompressed        = reader.ReadUInt32Be();
                    chunk.SizeUncompressed      = reader.ReadUInt32Be();
                    reader.BaseStream.Position += 16;
                }
                else
                {
                    chunk.SizeCompressed   = totalSize;
                    chunk.SizeUncompressed = totalSize - 1;
                }
            }

            var data = new byte[uncompressedSize];
            var idx  = 0;

            for (var i = 0; i < numChunks; ++i)
            {
                var code = reader.ReadByte();
                switch (code)
                {
                case 0x4E:
                {
                    var numRead = 0;
                    while (numRead < (int)chunks[i].SizeCompressed - 1)
                    {
                        var read = reader.Read(data, idx, (int)(chunks[i].SizeCompressed - 1) - numRead);
                        numRead += read;
                        idx     += read;
                    }
                    break;
                }

                case 0x5A:
                {
                    if (sizeFrameHeader != 0)
                    {
                        var curPos = reader.BaseStream.Position;
                        reader.BaseStream.Position += 2;
                        using (var strm = new DeflateStream(reader.BaseStream, CompressionMode.Decompress, true))
                        {
                            var numRead = 0;
                            while (numRead < (int)chunks[i].SizeUncompressed)
                            {
                                var read = strm.Read(data, idx, (int)chunks[i].SizeUncompressed - numRead);
                                numRead += read;
                                idx     += numRead;
                            }
                        }
                        reader.BaseStream.Position = curPos + (chunks[i].SizeCompressed - 1);
                    }
                    else
                    {
                        reader.BaseStream.Position += 2;
                        using (var strm = new DeflateStream(reader.BaseStream, CompressionMode.Decompress))
                        {
                            var numRemain = uncompressedSize - idx;
                            while (numRemain > 0)
                            {
                                var read = strm.Read(data, idx, (int)numRemain);
                                numRemain -= read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                break;
                }
            }

            return(new MemoryStream(data));
        }
Ejemplo n.º 2
0
        private static MemoryStream BlteGetData(BinaryReader reader, long size)
        {
            if (reader.ReadUInt32() != 0x45544C42)
            {
                throw new InvalidOperationException("Invalid file in archive. Invalid BLTE header");
            }

            var  sizeFrameHeader = reader.ReadUInt32Be();
            uint numChunks;
            var  totalSize = 0L;

            if (sizeFrameHeader == 0)
            {
                numChunks = 1;
                totalSize = size - 8;
            }
            else
            {
                if (reader.ReadByte() != 0x0F)
                {
                    throw new InvalidOperationException("Unknown error in BLTE: unk1 != 0x0F. This is not good im told.");
                }

                var sizes = reader.ReadBytes(3);
                numChunks = (uint)((sizes[0] << 16) | (sizes[1] >> 8) | sizes[2]);
            }

            if (numChunks == 0)
            {
                return(new MemoryStream());
            }

            var chunks = new BlteChunk[numChunks];

            for (var i = 0; i < numChunks; ++i)
            {
                var chunk = new BlteChunk();
                chunks[i] = chunk;
                if (sizeFrameHeader != 0)
                {
                    chunk.SizeCompressed        = reader.ReadUInt32Be();
                    chunk.SizeUncompressed      = reader.ReadUInt32Be();
                    reader.BaseStream.Position += 16;
                }
                else
                {
                    chunk.SizeCompressed   = totalSize;
                    chunk.SizeUncompressed = totalSize - 1;
                }
            }

            var data = new List <byte>();

            for (var i = 0; i < numChunks; ++i)
            {
                var code = reader.ReadByte();
                switch (code)
                {
                case 0x4E:
                {
                    data.AddRange(reader.ReadBytes((int)(chunks[i].SizeCompressed - 1)));
                    break;
                }

                case 0x5A:
                {
                    if (sizeFrameHeader != 0)
                    {
                        var curPos = reader.BaseStream.Position;
                        reader.BaseStream.Position += 2;
                        using (var strm = new DeflateStream(reader.BaseStream, CompressionMode.Decompress, true))
                        {
                            var content = new byte[(int)chunks[i].SizeUncompressed];
                            var numRead = 0;
                            while (numRead < content.Length)
                            {
                                numRead += strm.Read(content, numRead, content.Length - numRead);
                            }

                            data.AddRange(content);
                        }
                        reader.BaseStream.Position = curPos + (chunks[i].SizeCompressed - 1);
                    }
                    else
                    {
                        var content = reader.ReadBytes((int)(chunks[i].SizeCompressed - 1));
                        using (var inStrm = new MemoryStream(content))
                        {
                            inStrm.Position = 2;
                            using (var strm = new DeflateStream(inStrm, CompressionMode.Decompress))
                            {
                                using (var outStrm = new MemoryStream())
                                {
                                    strm.CopyTo(outStrm);
                                    data.AddRange(outStrm.ToArray());
                                }
                            }
                        }
                    }
                }
                break;
                }
            }

            return(new MemoryStream(data.ToArray()));
        }
Ejemplo n.º 3
0
        private static MemoryStream BlteGetData(BinaryReader reader, long size)
        {
            if (reader.ReadUInt32() != 0x45544C42)
                throw new InvalidOperationException("Invalid file in archive. Invalid BLTE header");

            var sizeFrameHeader = reader.ReadUInt32Be();
            uint numChunks;
            var totalSize = 0L;
            if(sizeFrameHeader == 0)
            {
                numChunks = 1;
                totalSize = size - 8;
            }
            else
            {
                if (reader.ReadByte() != 0x0F)
                    throw new InvalidOperationException("Unknown error in BLTE: unk1 != 0x0F. This is not good im told.");

                var sizes = reader.ReadBytes(3);
                numChunks = (uint) ((sizes[0] << 16) | (sizes[1] >> 8) | sizes[2]);
            }

            if (numChunks == 0)
                return new MemoryStream();

            var chunks = new BlteChunk[numChunks];
            for(var i = 0; i < numChunks; ++i)
            {
                var chunk = new BlteChunk();
                chunks[i] = chunk;
                if(sizeFrameHeader != 0)
                {
                    chunk.SizeCompressed = reader.ReadUInt32Be();
                    chunk.SizeUncompressed = reader.ReadUInt32Be();
                    reader.BaseStream.Position += 16;
                }
                else
                {
                    chunk.SizeCompressed = totalSize;
                    chunk.SizeUncompressed = totalSize - 1;
                }
            }

            var data = new List<byte>();

            for(var i = 0; i < numChunks; ++i)
            {
                var code = reader.ReadByte();
                switch (code)
                {
                    case 0x4E:
                        data.AddRange(reader.ReadBytes((int)(chunks[i].SizeCompressed - 1)));
                        break;

                    case 0x5A:
                        {
                            if (sizeFrameHeader != 0)
                            {
                                var curPos = reader.BaseStream.Position;
                                reader.BaseStream.Position += 2;
                                using (var strm = new DeflateStream(reader.BaseStream, CompressionMode.Decompress, true))
                                {
                                    var content = new byte[(int)chunks[i].SizeUncompressed];
                                    var numRead = 0;
                                    while (numRead < content.Length)
                                    {
                                        numRead += strm.Read(content, numRead, content.Length - numRead);
                                    }

                                    data.AddRange(content);
                                }
                                reader.BaseStream.Position = curPos + (chunks[i].SizeCompressed - 1);
                            }
                            else
                            {
                                var content = reader.ReadBytes((int)(chunks[i].SizeCompressed - 1));
                                using (var inStrm = new MemoryStream(content))
                                {
                                    inStrm.Position = 2;
                                    using (var strm = new DeflateStream(inStrm, CompressionMode.Decompress))
                                    {
                                        using (var outStrm = new MemoryStream())
                                        {
                                            strm.CopyTo(outStrm);
                                            data.AddRange(outStrm.ToArray());
                                        }
                                    }
                                }
                            }
                        }
                        break;
                }
            }

            return new MemoryStream(data.ToArray());
        }