Esempio n. 1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (m_Compressed)
            {
                long pos = m_Uncomp.Position;
                m_Uncomp.Seek(0, SeekOrigin.End);

                while (pos + count > m_Uncomp.Length && RawStream.Position + 8 < RawStream.Length)
                {
                    int block = Raw.ReadInt32();
                    int ucLen = Raw.ReadInt32();

                    if (block > 0x10000000 || block <= 0 || ucLen > 0x10000000 || ucLen <= 0)
                    {
                        break;
                    }

                    if (RawStream.Position + block > RawStream.Length)
                    {
                        break;
                    }

                    if (m_ReadBuff == null || m_ReadBuff.Length < block)
                    {
                        m_ReadBuff = new byte[block];
                    }

                    if (m_CompBuff == null || m_CompBuff.Length < ucLen)
                    {
                        m_CompBuff = new byte[ucLen];
                    }
                    else
                    {
                        ucLen = m_CompBuff.Length;
                    }

                    Raw.Read(m_ReadBuff, 0, block);

                    ZLibError error = ZLib.uncompress(m_CompBuff, ref ucLen, m_ReadBuff, block);
                    if (error != ZLibError.Z_OK)
                    {
                        throw new Exception("ZLib error uncompressing: " + error.ToString());
                    }

                    m_Uncomp.Write(m_CompBuff, 0, ucLen);
                }

                m_Uncomp.Position = pos;
                return(m_Uncomp.Read(buffer, offset, count));
            }
            else
            {
                return(RawStream.Read(buffer, offset, count));
            }
        }
Esempio n. 2
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            if (m_Compressed)
            {
                long absPos = offset;
                if (origin == SeekOrigin.Current)
                {
                    absPos += m_Uncomp.Position;
                }

                if (absPos < 0)
                {
                    throw new Exception("Cannot seek past the begining of the stream.");
                }

                long pos = m_Uncomp.Position;
                m_Uncomp.Seek(0, SeekOrigin.End);

                while ((origin == SeekOrigin.End || absPos >= m_Uncomp.Length) && RawStream.Position < RawStream.Length)
                {
                    int block = Raw.ReadInt32();
                    int ucLen = Raw.ReadInt32();
                    if (m_ReadBuff == null || m_ReadBuff.Length < block)
                    {
                        m_ReadBuff = new byte[block];
                    }

                    if (m_CompBuff == null || m_CompBuff.Length < ucLen)
                    {
                        m_CompBuff = new byte[ucLen];
                    }
                    else
                    {
                        ucLen = m_CompBuff.Length;
                    }

                    Raw.Read(m_ReadBuff, 0, block);

                    ZLibError error = ZLib.uncompress(m_CompBuff, ref ucLen, m_ReadBuff, block);
                    if (error != ZLibError.Z_OK)
                    {
                        throw new Exception("ZLib error uncompressing: " + error.ToString());
                    }

                    m_Uncomp.Write(m_CompBuff, 0, ucLen);
                }

                m_Uncomp.Position = pos;
                return(m_Uncomp.Seek(offset, origin));
            }
            else
            {
                return(RawStream.Seek(offset, origin));
            }
        }
Esempio n. 3
0
        public PacketReader GetCompressedReader()
        {
            int fullLen = ReadInt32();
            int destLen = 0;

            byte[] buff;

            if (fullLen >= 4)
            {
                int packLen = ReadInt32();
                destLen = packLen;

                if (destLen < 0)
                {
                    destLen = 0;
                }

                buff = new byte[destLen];

                if (fullLen > 4 && destLen > 0)
                {
                    if (ZLib.uncompress(buff, ref destLen, CopyBytes(this.Position, fullLen - 4), fullLen - 4) !=
                        ZLibError.Z_OK)
                    {
                        destLen = 0;
                        buff    = new byte[1];
                    }
                }
                else
                {
                    destLen = 0;
                    buff    = new byte[1];
                }
            }
            else
            {
                buff = new byte[1];
            }

            return(new PacketReader(buff, false));
        }