Example #1
0
        private void init(Stream innerStream)
        {
            m_stream = innerStream;
            if (m_stream.CanRead)
            {
                m_in = new ZStream();
                int ret = m_in.inflateInit();
                if (ret != zlibConst.Z_OK)
                {
                    throw new CompressionFailedException("Unable to initialize zlib for deflate: " + ret);
                }
                m_inbuf            = new byte[bufsize];
                m_in.avail_in      = 0;
                m_in.next_in       = m_inbuf;
                m_in.next_in_index = 0;
            }

            if (m_stream.CanWrite)
            {
                m_out = new ZStream();
                int ret = m_out.deflateInit(zlibConst.Z_DEFAULT_COMPRESSION);
                if (ret != zlibConst.Z_OK)
                {
                    throw new CompressionFailedException("Unable to initialize zlib for inflate: " + ret);
                }
                m_outbuf       = new byte[bufsize];
                m_out.next_out = m_outbuf;
            }
        }
 public TlsDeflateCompression(int level)
 {
     zIn = new ZStream();
     zIn.inflateInit();
     zOut = new ZStream();
     zOut.deflateInit(level);
 }
Example #3
0
 public ZOutputStream(System.IO.Stream out_Renamed, int level)
 {
     InitBlock();
     this.out_Renamed = out_Renamed;
     z.deflateInit(level);
     compress = true;
 }
Example #4
0
        // Do we really need these? SWGEmu doesn't seem to compress...
        public void Compress()
        {
            byte[] numArray = new byte[this.data.Count];
            this.data.CopyTo(0, numArray, 0, this.data.Count);
            byte[]  numArray1 = new byte[800];
            ZStream zStream   = new ZStream()
            {
                avail_in = 0
            };

            zStream.deflateInit(6);
            zStream.next_in       = numArray;
            zStream.next_in_index = 2;
            zStream.avail_in      = (int)numArray.Length - 4;
            zStream.next_out      = numArray1;
            zStream.avail_out     = 800;
            if (zStream.deflate(4) != -3)
            {
                long totalOut = zStream.total_out;
                zStream.deflateEnd();
                zStream = null;
                this.data.Clear();
                this.data.Add(numArray[0]);
                this.data.Add(numArray[1]);
                for (int i = 0; (long)i < totalOut; i++)
                {
                    this.data.Add(numArray1[i]);
                }
                this.data.Add(numArray[(int)numArray.Length - 3]);
                this.data.Add(numArray[(int)numArray.Length - 2]);
                this.data.Add(numArray[(int)numArray.Length - 1]);
            }
        }
        /// <summary>
        /// Reimplements function from zlib (compress2) that is not present in ZLIB.NET.
        /// </summary>
        /// <param name="dest">Destination array of bytes. May be trimmed if necessary.</param>
        /// <param name="source">Source array of bytes.</param>
        /// <param name="level">Level of compression.</param>
        /// <returns>Zlib status code.</returns>
        static int ZlibCompress(ref byte[] dest, byte[] source, int level)
        {
            ZStream stream = new ZStream();
            int     err;

            stream.next_in   = source;
            stream.avail_in  = source.Length;
            stream.next_out  = dest;
            stream.avail_out = dest.Length;

            err = stream.deflateInit(level);
            if (err != zlibConst.Z_OK)
            {
                return(err);
            }

            err = stream.deflate(zlibConst.Z_FINISH);
            if (err != zlibConst.Z_STREAM_END)
            {
                stream.deflateEnd();
                return(err == zlibConst.Z_OK ? zlibConst.Z_BUF_ERROR : err);
            }

            if (stream.total_out != dest.Length)
            {
                byte[] output = new byte[stream.total_out];
                Buffer.BlockCopy(stream.next_out, 0, output, 0, (int)stream.total_out);
                dest = output;
            }

            return(stream.deflateEnd());
        }
Example #6
0
 private void InitZStream()
 {
     zStream = new ZStream();
     zStream.deflateInit(zlib.zlibConst.Z_DEFAULT_COMPRESSION);
     zBuf              = new byte[8192];
     zStream.next_out  = zBuf;
     zStream.avail_out = zBuf.Length;
 }
 public ZInputStream(Stream input, int level)
 {
     this.input = input;
     z.deflateInit(level);
     compress        = true;
     z.next_in       = buf;
     z.next_in_index = 0;
     z.avail_in      = 0;
 }
Example #8
0
        private int compress(Stream sin, Stream sout, int level)
        {
            ZStream zstream = new ZStream();
            int     length  = (int)sin.Length;

            byte[] buffer1 = new byte[length];
            byte[] buffer2 = new byte[32768];
            int    num1    = 0;
            int    num2    = zstream.deflateInit(level);

            if (num2 != 0)
            {
                return(num2);
            }
            zstream.avail_in  = 0;
            zstream.avail_out = 32768;
            zstream.next_out  = buffer2;
            int flush = 0;
            int num3;

            do
            {
                if (zstream.avail_in == 0)
                {
                    int num4 = sin.Read(buffer1, 0, length);
                    zstream.next_in       = buffer1;
                    zstream.avail_in      = num4;
                    zstream.next_in_index = 0;
                    if (num4 < length)
                    {
                        flush = 4;
                    }
                }
                num3 = zstream.deflate(flush);
                if (num3 < 0)
                {
                    return(num3);
                }
                if (zstream.avail_out == 0 || num3 == 1)
                {
                    int count = 32768 - zstream.avail_out;
                    sout.Write(buffer2, 0, count);
                    num1                  += count;
                    zstream.next_out       = buffer2;
                    zstream.avail_out      = 32768;
                    zstream.next_out_index = 0;
                }
            }while (num3 != 1);
            int num5 = zstream.deflateEnd();

            if (num5 != 0)
            {
                return(num5);
            }
            zstream.free();
            return(num1);
        }
 public ZInputStream(System.IO.Stream in_Renamed, int level) : base(in_Renamed)
 {
     InitBlock();
     this.in_Renamed = in_Renamed;
     z.deflateInit(level);
     compress        = true;
     z.next_in       = buf;
     z.next_in_index = 0;
     z.avail_in      = 0;
 }
Example #10
0
        private static byte[] CompressZ(byte[] inbuf, int level = 6) //CompLevel= 0-9
        {
            var z      = new ZStream();
            var ms     = new MemoryStream();
            var outbuf = new byte[1024];

            if (z.deflateInit(level) != zlibConst.Z_OK)
            {
                throw new InvalidOperationException("zlib.deflateInit");
            }

            z.next_in       = inbuf;
            z.avail_in      = inbuf.Length;
            z.next_in_index = 0;

            z.next_out       = outbuf;
            z.avail_out      = outbuf.Length;
            z.next_out_index = 0;

            while (true)
            {
                int status = z.deflate(zlibConst.Z_FINISH); /* 圧縮する */
                if (status == zlibConst.Z_STREAM_END)
                {
                    break;
                }

                if (status != zlibConst.Z_OK)
                {
                    throw new InvalidOperationException("zlib.deflate");
                }

                if (z.avail_out == 0)
                {
                    ms.Write(outbuf, 0, outbuf.Length);
                    z.next_out       = outbuf;
                    z.avail_out      = outbuf.Length;
                    z.next_out_index = 0;
                }
            }

            if ((outbuf.Length - z.avail_out) != 0)
            {
                ms.Write(outbuf, 0, outbuf.Length - z.avail_out);
            }

            if (z.deflateEnd() != zlibConst.Z_OK)
            {
                throw new InvalidOperationException("zlib.deflateEnd");
            }

            z.free();

            return(ms.ToArray());
        }
Example #11
0
        public CompressedStream(Stream innerStream)
        {
            InnerStream = innerStream;

            zOut = new ZStream();
            zOut.deflateInit(5, true);
            zOut.next_out = new byte[4096];

            zIn = new ZStream();
            zIn.inflateInit(true);
            zIn.next_in = new byte[4096];
        }
Example #12
0
        static void compress2(ref byte[] dest, byte[] src, int level, bool noHeader)
        {
            ZStream stream = new ZStream();

            stream.next_in  = src;
            stream.avail_in = src.Length;

            stream.next_out  = dest;
            stream.avail_out = dest.Length;

            if (noHeader == false)
            {
                stream.deflateInit(level);
            }
            else
            {
                stream.deflateInit(level, -15);
            }

            stream.deflate(zlibConst.Z_FINISH);

            Array.Resize <byte>(ref dest, (int)stream.total_out);
        }
Example #13
0
        public void InitCrypto()
        {
            _dec = new Salsa20Engine();
            _dec.Init(false, new ParametersWithIV(new KeyParameter(_salsaKey02), _salsaIV02));

            _enc = new Salsa20Engine();
            _enc.Init(true, new ParametersWithIV(new KeyParameter(_salsaKey01), _salsaIV01));

            _dStream = new ZStream();
            _dStream.deflateInit(-1, -15);

            _iStream = new ZStream();
            _iStream.inflateInit(-15);
        }
Example #14
0
        public static PhpString gzdeflate(byte[] data, int level = -1)
        {
            if (level < -1 || level > 9)
            {
                PhpException.Throw(PhpError.Warning, String.Format("compression level ({0}) must be within -1..9", level));
                return(default(PhpString));
            }

            var zs = new ZStream();

            zs.next_in  = data;
            zs.avail_in = data.Length;

            // heuristic for max data length
            zs.avail_out = data.Length + data.Length / PHP_ZLIB_MODIFIER + 15 + 1;
            zs.next_out  = new byte[zs.avail_out];

            // -15 omits the header (undocumented feature of zlib)
            int status = zs.deflateInit(level, -MAX_WBITS);

            if (status == zlibConst.Z_OK)
            {
                status = zs.deflate(zlibConst.Z_FINISH);
                if (status != zlibConst.Z_STREAM_END)
                {
                    zs.deflateEnd();
                    if (status == zlibConst.Z_OK)
                    {
                        status = zlibConst.Z_BUF_ERROR;
                    }
                }
                else
                {
                    status = zs.deflateEnd();
                }
            }

            if (status == zlibConst.Z_OK)
            {
                byte[] result = new byte[zs.total_out];
                Buffer.BlockCopy(zs.next_out, 0, result, 0, (int)zs.total_out);
                return(new PhpString(result));
            }
            else
            {
                PhpException.Throw(PhpError.Warning, zError(status));
                return(default(PhpString));
            }
        }
Example #15
0
        public static string compress([BytesConversion] IList <byte> data,
                                      int level = Z_DEFAULT_COMPRESSION)
        {
            byte[] input  = data.ToArray();
            byte[] output = new byte[input.Length + input.Length / 1000 + 12 + 1];

            ZStream zst = new ZStream();

            zst.next_in   = input;
            zst.avail_in  = input.Length;
            zst.next_out  = output;
            zst.avail_out = output.Length;

            int err = zst.deflateInit(level);

            switch (err)
            {
            case (Z_OK):
                break;

            case (Z_STREAM_ERROR):
                throw PythonOps.CreateThrowable(error,
                                                "Bad compression level");

            default:
                zst.deflateEnd();
                zlib_error(zst, err, "while compressing data");
                return(null);
            }

            err = zst.deflate(FlushStrategy.Z_FINISH);

            if (err != Z_STREAM_END)
            {
                zst.deflateEnd();
                throw zlib_error(zst, err, "while compressing data");
            }

            err = zst.deflateEnd();

            if (err == Z_OK)
            {
                return(PythonAsciiEncoding.Instance.GetString(output, 0, (int)zst.total_out));
            }
            else
            {
                throw zlib_error(zst, err, "while finishing compression");
            }
        }
Example #16
0
        internal Compress(int level, int method, int wbits, int memlevel, int strategy)
        {
            zst = new ZStream();
            int err = zst.deflateInit(level, wbits);

            switch (err)
            {
            case ZlibModule.Z_OK:
                break;

            case ZlibModule.Z_STREAM_ERROR:
                throw PythonOps.ValueError("Invalid initialization option");

            default:
                throw ZlibModule.zlib_error(this.zst, err, "while creating compression object");
            }
        }
Example #17
0
        public static byte[] Compress(byte[] source, int start, int size)
        {
            ZStream stream = new ZStream();

            stream.next_in       = source;
            stream.next_in_index = start;
            stream.avail_in      = size;

            int maxCompSize = (int)((long)size * 1001 / 1000 + 12);

            byte[] compressed = new byte[maxCompSize];
            stream.next_out       = compressed;
            stream.next_out_index = 0;
            stream.avail_out      = maxCompSize;

            int err = stream.deflateInit(zlibConst.Z_DEFAULT_COMPRESSION);

            if (err != zlibConst.Z_OK)
            {
                return(null);
            }

            err = stream.deflate(zlibConst.Z_FINISH);
            if (err != zlibConst.Z_STREAM_END)
            {
                stream.deflateEnd();
                return(null);
            }

            int realSize = (int)stream.total_out;

            stream.deflateEnd();

            if (realSize < compressed.Length)
            {
                byte[] temp = new byte[realSize];
                Array.Copy(compressed, temp, realSize);
                return(temp);
            }
            else
            {
                return(compressed);
            }
        }
Example #18
0
        /// <summary>
        /// Closes any open connections between the Tibia client and game server, and clears
        /// any pending packets to be sent.
        /// </summary>
        private void ResetConnection()
        {
            if (_isResettingConnection)
            {
                return;
            }

            _isResettingConnection = true;

            lock (_clientSendLock)
            {
                _isSendingToClient = false;
                _clientSendQueue.Clear();
            }
            if (_clientSocket != null)
            {
                _clientSocket.Close();
            }

            lock (_serverSendLock)
            {
                _isSendingToServer = false;
                _serverSendQueue.Clear();
            }
            if (_serverSocket != null)
            {
                _serverSocket.Close();
            }

            _zStream.deflateEnd();
            _zStream.inflateEnd();
            _zStream = new ZStream();
            _zStream.deflateInit(zlibConst.Z_DEFAULT_COMPRESSION, -15);
            _zStream.inflateInit(-15);

            _clientSequenceNumber = 1;
            _serverSequenceNumber = 1;
            _xteaKey = null;
            _isResettingConnection = false;
            ConnectionState        = ConnectionState.ConnectingStage1;
        }
Example #19
0
        /// <summary>
        /// Starts the <see cref="HttpListener"/> and <see cref="TcpListener"/> objects that listen for incoming
        /// connection requests from the Tibia client.
        /// </summary>
        /// <returns>Returns true on success, or if already started. Returns false if an exception is thrown.</returns>
        internal bool Start(int httpPort = 7171, string loginWebService = "")
        {
            if (_isStarted)
            {
                return(true);
            }

            try
            {
                if (_tcpListener == null)
                {
                    _tcpListener = new TcpListener(IPAddress.Loopback, 0);
                }

                var uriPrefix = $"http://127.0.0.1:{httpPort}/";
                if (!_httpListener.Prefixes.Contains(uriPrefix))
                {
                    _httpListener.Prefixes.Add(uriPrefix);
                }

                _zStream.deflateInit(zlibConst.Z_DEFAULT_COMPRESSION, -15);
                _zStream.inflateInit(-15);

                _httpListener.Start();
                _httpListener.BeginGetContext(new AsyncCallback(BeginGetContextCallback), _httpListener);

                _tcpListener.Start();
                _tcpListener.BeginAcceptSocket(new AsyncCallback(BeginAcceptTcpClientCallback), _tcpListener);

                _isStarted       = true;
                _loginWebService = loginWebService;
                ConnectionState  = ConnectionState.ConnectingStage1;
            }
            catch (Exception ex)
            {
                _isStarted = false;
                _client.Logger.Error(ex.ToString());
            }

            return(_isStarted);
        }
Example #20
0
        public GZipPacker()
        {
            fifo = new Fifo();

            zs = new ZStream();
            zs.deflateInit(-1, -15);

            this.currentSize = 0;
            this.crc32       = 0xffffffff;
            this.finished    = false;

            GZipHeader h = new GZipHeader();

            h.ID1   = 0x1f;
            h.ID2   = 0x8b;
            h.FLG   = 0;
            h.MTIME = Util.DateTimeToUnixTime(DateTime.Now.ToUniversalTime());
            h.XFL   = 0;
            h.OS    = 3;
            h.CM    = 8;

            fifo.Write(Util.StructToByte(h));
        }
Example #21
0
        public static BufLen BCGZipCompressNew(BufLen buf)
        {
            var crc = LZUtils.CRC32(buf);

            var dest   = new byte[buf.Length + 4096];
            var destix = 0;

            dest[destix++] = 0x1f;
            dest[destix++] = 0x8b;
            dest[destix++] = 0x08;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x02;
            dest[destix++] = 0xFF;

            var z = new ZStream();

            z.deflateInit(6, true);

            z.next_in_index = buf.BaseArrayOffset;
            z.next_in       = buf.BaseArray;
            z.avail_in      = buf.Length;

bigger_dest:

            z.next_out       = dest;
            z.next_out_index = destix;
            z.avail_out      = dest.Length - destix;
            var err = z.deflate(JZlib.Z_FINISH);

            if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
            {
                throw new IOException("deflating: " + z.msg);
            }

            if (z.avail_out == 0)
            {
                var newdest = new byte[dest.Length * 2];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            if (z.avail_out < 8)
            {
                var newdest = new byte[dest.Length + 8];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            var result = new BufLen(dest, 0, 10 + dest.Length - z.avail_out + 8);

            result.Poke32(crc, result.Length - 8);
            result.Poke32((uint)buf.Length, result.Length - 4);

            z.deflateEnd();

            /*
             * using ( var foos = new FileStream( "test.gz", FileMode.Create, FileAccess.Write ) )
             * {
             * foos.Write( msb );
             * }
             */
            return(result);
        }
Example #22
0
 protected override int InitZlibOperation(ZStream zs)
 {
     // -MAX_WBITS stands for absense of Zlib header and trailer (needed for GZIP compression and decompression)
     return(zs.deflateInit(_level, -Zlib.MAX_WBITS));
 }
        public static void main(String[] arg)
        {
            int err;
            int comprLen   = 40000;
            int uncomprLen = comprLen;

            byte[] uncompr = new byte[uncomprLen];
            byte[] compr   = new byte[comprLen];
            long   dictId;

            ZStream c_stream = new ZStream();

            err = c_stream.deflateInit(JZlib.Z_BEST_COMPRESSION);
            CHECK_ERR(c_stream, err, "deflateInit");

            err = c_stream.deflateSetDictionary(dictionary, dictionary.Length);
            CHECK_ERR(c_stream, err, "deflateSetDictionary");

            dictId = c_stream.adler;

            c_stream.next_out       = compr;
            c_stream.next_out_index = 0;
            c_stream.avail_out      = comprLen;

            c_stream.next_in       = hello;
            c_stream.next_in_index = 0;
            c_stream.avail_in      = hello.Length;

            err = c_stream.deflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_STREAM_END)
            {
                java.lang.SystemJ.outJ.println("deflate should report Z_STREAM_END");
                java.lang.SystemJ.exit(1);
            }
            err = c_stream.deflateEnd();
            CHECK_ERR(c_stream, err, "deflateEnd");

            ZStream d_stream = new ZStream();

            d_stream.next_in       = compr;
            d_stream.next_in_index = 0;
            d_stream.avail_in      = comprLen;

            err = d_stream.inflateInit();
            CHECK_ERR(d_stream, err, "inflateInit");
            d_stream.next_out       = uncompr;
            d_stream.next_out_index = 0;
            d_stream.avail_out      = uncomprLen;

            while (true)
            {
                err = d_stream.inflate(JZlib.Z_NO_FLUSH);
                if (err == JZlib.Z_STREAM_END)
                {
                    break;
                }
                if (err == JZlib.Z_NEED_DICT)
                {
                    if ((int)d_stream.adler != (int)dictId)
                    {
                        java.lang.SystemJ.outJ.println("unexpected dictionary");
                        java.lang.SystemJ.exit(1);
                    }
                    err = d_stream.inflateSetDictionary(dictionary, dictionary.Length);
                }
                CHECK_ERR(d_stream, err, "inflate with dict");
            }

            err = d_stream.inflateEnd();
            CHECK_ERR(d_stream, err, "inflateEnd");

            int j = 0;

            for (; j < uncompr.Length; j++)
            {
                if (uncompr[j] == 0)
                {
                    break;
                }
            }

            java.lang.SystemJ.outJ.println("after inflateSync(): hel" + new java.lang.StringJ(uncompr, 0, j));
        }
Example #24
0
            IEnumerable <WebSocketsFrame> IExtension.ApplyOutgoing(NetContext context, WebSocketConnection connection, WebSocketsFrame frame)
            {
                if (!frame.IsControlFrame && frame.PayloadLength > parent.compressMessagesLargerThanBytes)
                {
                    if (frame.Reserved1)
                    {
                        throw new InvalidOperationException("Reserved1 flag is already set; extension conflict?");
                    }

                    int headerBytes = 0;
                    if (outbound == null)
                    {
                        outbound = new ZStream();
                        const int BITS = 12; // 4096 byte outbound buffer (instead of full 32k=15)
                        outbound.deflateInit(zlibConst.Z_BEST_COMPRESSION, BITS);
                        headerBytes = 2;
                    }
                    BufferStream tmp     = null;
                    var          payload = frame.Payload;
                    payload.Position = 0;
                    byte[] inBuffer = null, outBuffer = null;
                    try
                    {
                        inBuffer  = context.GetBuffer();
                        outBuffer = context.GetBuffer();
                        tmp       = new BufferStream(context, 0);

                        outbound.next_out = outBuffer;
                        outbound.next_in  = inBuffer;

                        int remaining = frame.PayloadLength;
                        while (remaining > 0)
                        {
                            int readCount = payload.Read(inBuffer, 0, inBuffer.Length);
                            if (readCount <= 0)
                            {
                                break;
                            }
                            remaining -= readCount;

                            outbound.next_in_index = 0;
                            outbound.avail_in      = readCount;

                            do
                            {
                                outbound.next_out_index = 0;
                                outbound.avail_out      = outBuffer.Length;
                                long priorOut = outbound.total_out;
                                int  err      = outbound.deflate(remaining == 0 ? zlibConst.Z_SYNC_FLUSH : zlibConst.Z_NO_FLUSH);
                                if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
                                {
                                    throw new ZStreamException("deflating: " + outbound.msg);
                                }

                                int outCount = (int)(outbound.total_out - priorOut);
                                if (outCount > 0)
                                {
                                    if (headerBytes == 0)
                                    {
                                        tmp.Write(outBuffer, 0, outCount);
                                    }
                                    else
                                    {
                                        if (outCount < headerBytes)
                                        {
                                            throw new InvalidOperationException("Failed to write entire header");
                                        }
                                        // check the generated header meets our expectations
                                        // CMF is very specific - CM must be 8, and CINFO must be <=7 (for 32k window)
                                        if ((outBuffer[0] & 15) != 8)
                                        {
                                            throw new InvalidOperationException("Zlib CM header was incorrect");
                                        }
                                        if ((outBuffer[0] & 128) != 0) // if msb set, is > 7 - invalid
                                        {
                                            throw new InvalidOperationException("Zlib CINFO header was incorrect");
                                        }

                                        // FLG is less important; FCHECK is irrelevent, FLEVEL doesn't matter; but
                                        // FDICT must be zero, to ensure that we aren't expecting a an initialization dictionary
                                        if ((outBuffer[1] & 32) != 0)
                                        {
                                            throw new InvalidOperationException("Zlib FLG.FDICT header was set (must not be)");
                                        }

                                        // skip the header, and write anything else
                                        outCount -= headerBytes;
                                        if (outCount > 0)
                                        {
                                            tmp.Write(outBuffer, headerBytes, outCount);
                                        }
                                        headerBytes = 0; // all written now
                                    }
                                }
                            } while (outbound.avail_in > 0 || outbound.avail_out == 0);
                        }
                        if (remaining != 0)
                        {
                            throw new EndOfStreamException();
                        }
                        if (headerBytes != 0)
                        {
                            throw new InvalidOperationException("Zlib header was not written");
                        }

                        // verify the last 4 bytes, then drop them
                        tmp.Position = tmp.Length - 4;
                        NetContext.Fill(tmp, outBuffer, 4);
                        if (!(outBuffer[0] == 0x00 && outBuffer[1] == 0x00 && outBuffer[2] == 0xFF && outBuffer[3] == 0xFF))
                        {
                            throw new InvalidOperationException("expectation failed: 0000FFFF in the tail");
                        }

                        if (parent.disableContextTakeover && tmp.Length >= frame.PayloadLength)
                        { // compressing it didn't do anything useful; since we're going to discard
                          // the compression context, we might as well stick with the original data
                            payload.Position = 0;
                            payload          = null; // so that it doesn't get disposed
                        }
                        else
                        {
                            // set our final output
                            tmp.Position = 0;
                            tmp.SetLength(tmp.Length - 4);
                            long bytesSaved = frame.PayloadLength - tmp.Length;
                            frame.Payload       = tmp;
                            frame.PayloadLength = (int)tmp.Length;
                            frame.Reserved1     = true;
                            tmp = payload as BufferStream;
                            parent.RegisterOutboundBytesSaved(bytesSaved);
                        }
                    }
#if DEBUG
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                        throw;
                    }
#endif
                    finally
                    {
                        if (outbound != null)
                        {
                            outbound.next_out = null;
                            outbound.next_in  = null;
                        }
                        if (tmp != null)
                        {
                            tmp.Dispose();
                        }
                        if (inBuffer != null)
                        {
                            context.Recycle(inBuffer);
                        }
                        if (outBuffer != null)
                        {
                            context.Recycle(outBuffer);
                        }
                        if (parent.disableContextTakeover)
                        {
                            ClearContext(false, true);
                        }
                    }
                }
                yield return(frame);
            }
Example #25
0
        public static void main(String[] arg)
        {
            int err;
            int comprLen   = 40000;
            int uncomprLen = comprLen;

            byte[] compr   = new byte[comprLen];
            byte[] uncompr = new byte[uncomprLen];

            ZStream c_stream = new ZStream();

            err = c_stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION);
            CHECK_ERR(c_stream, err, "deflateInit");

            c_stream.next_in       = hello;
            c_stream.next_in_index = 0;

            c_stream.next_out       = compr;
            c_stream.next_out_index = 0;

            while (c_stream.total_in != hello.Length &&
                   c_stream.total_out < comprLen)
            {
                c_stream.avail_in = c_stream.avail_out = 1; // force small buffers
                err = c_stream.deflate(JZlib.Z_NO_FLUSH);
                CHECK_ERR(c_stream, err, "deflate");
            }

            while (true)
            {
                c_stream.avail_out = 1;
                err = c_stream.deflate(JZlib.Z_FINISH);
                if (err == JZlib.Z_STREAM_END)
                {
                    break;
                }
                CHECK_ERR(c_stream, err, "deflate");
            }

            err = c_stream.deflateEnd();
            CHECK_ERR(c_stream, err, "deflateEnd");

            ZStream d_stream = new ZStream();

            d_stream.next_in        = compr;
            d_stream.next_in_index  = 0;
            d_stream.next_out       = uncompr;
            d_stream.next_out_index = 0;

            err = d_stream.inflateInit();
            CHECK_ERR(d_stream, err, "inflateInit");

            while (d_stream.total_out < uncomprLen &&
                   d_stream.total_in < comprLen)
            {
                d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
                err = d_stream.inflate(JZlib.Z_NO_FLUSH);
                if (err == JZlib.Z_STREAM_END)
                {
                    break;
                }
                CHECK_ERR(d_stream, err, "inflate");
            }

            err = d_stream.inflateEnd();
            CHECK_ERR(d_stream, err, "inflateEnd");

            int i = 0;

            for (; i < hello.Length; i++)
            {
                if (hello[i] == 0)
                {
                    break;
                }
            }
            int j = 0;

            for (; j < uncompr.Length; j++)
            {
                if (uncompr[j] == 0)
                {
                    break;
                }
            }

            if (i == j)
            {
                for (i = 0; i < j; i++)
                {
                    if (hello[i] != uncompr[i])
                    {
                        break;
                    }
                }
                if (i == j)
                {
                    java.lang.SystemJ.outJ.println("inflate(): " + new java.lang.StringJ(uncompr, 0, j).ToString());
                    return;
                }
            }
            else
            {
                java.lang.SystemJ.outJ.println("bad inflate");
            }
        }
Example #26
0
            /// <summary>
            /// Pack the folder pointed by the path (source) in a package pointed by the other path (destination).
            /// </summary>
            public static void Pack(String Source, String Destination)
            {
                if (!Destination.EndsWith(".tpi"))
                {
                    Destination += ".tpi";
                }

                DirectoryInfo DI = new DirectoryInfo(Source);

                FileInfo[] Files = DI.GetFiles("*.*", SearchOption.AllDirectories);

                Header *pHeader = stackalloc Header[1];

                TPI_IDENTIFIER.ToPointer(pHeader->Identifier);
                pHeader->Number   = (UInt32)Files.Length;
                pHeader->Version  = TPI_VERSION;
                pHeader->Unknown1 = TPI_UNKNOWN_1;
                pHeader->Unknown2 = TPI_UNKNOWN_2;
                pHeader->Unknown3 = TPI_UNKNOWN_3;
                pHeader->Reserved = 0x00;

                Encoding Encoding = Encoding.GetEncoding("UTF-8");

                Byte[] Buffer = new Byte[Kernel.MAX_BUFFER_SIZE];
                Byte[] Tmp    = new Byte[Kernel.MAX_BUFFER_SIZE];

                using (FileStream TpiStream = new FileStream(Destination, FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (FileStream TpdStream = new FileStream(Destination.Replace(".tpi", ".tpd"), FileMode.Create, FileAccess.Write, FileShare.Read))
                    {
                        using (BinaryWriter Writer = new BinaryWriter(TpiStream, Encoding))
                        {
                            Console.Write("Writing header... ");
                            Kernel.memcpy(Buffer, pHeader, sizeof(TPI.Header));
                            TpiStream.Write(Buffer, 0, sizeof(TPI.Header));
                            TpdStream.Write(Buffer, 0, sizeof(TPD.Header));
                            Console.WriteLine("Ok!");

                            Console.Write("Writing data... ");
                            UInt32[] CompressedSizes = new UInt32[Files.Length];
                            UInt32[] Offsets         = new UInt32[Files.Length];
                            UInt32   Offset          = (UInt32)sizeof(TPD.Header);

                            for (Int32 i = 0; i < pHeader->Number; i++)
                            {
                                Console.Write("\rWriting data... {0}%", i * 100 / pHeader->Number);

                                using (FileStream Input = new FileStream(Files[i].FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                                {
                                    ZStream Stream = new ZStream();
                                    Stream.deflateInit(9); //TQ use lvl 3

                                    Int32 Param  = zlibConst.Z_NO_FLUSH;
                                    Int32 Length = 0;
                                    do
                                    {
                                        Length = Input.Read(Buffer, 0, Kernel.MAX_BUFFER_SIZE);

                                        Param                = Length == 0 ? zlibConst.Z_FINISH : zlibConst.Z_NO_FLUSH;
                                        Stream.avail_in      = Length;
                                        Stream.next_in       = Buffer;
                                        Stream.next_in_index = 0;
                                        do
                                        {
                                            Stream.avail_out      = Kernel.MAX_BUFFER_SIZE;
                                            Stream.next_out       = Tmp;
                                            Stream.next_out_index = 0;

                                            Int32 Result = Stream.deflate(Param);

                                            Int32 Len = Kernel.MAX_BUFFER_SIZE - Stream.avail_out;
                                            TpdStream.Write(Tmp, 0, Len);
                                        }while (Stream.avail_out == 0);
                                    }while (Param != zlibConst.Z_FINISH);

                                    Stream.deflateEnd();

                                    CompressedSizes[i] = (UInt32)Stream.total_out;
                                    Offsets[i]         = Offset;
                                    Offset            += CompressedSizes[i];
                                }
                            }
                            Console.WriteLine("\b\b\bOk!");

                            Console.Write("Writing entries... ");
                            UInt32 LastOffset = 0;

                            for (Int32 i = 0; i < pHeader->Number; i++)
                            {
                                Console.Write("\rWriting entries... {0}%", i * 100 / pHeader->Number);

                                String RelativePath = Files[i].FullName.Replace(DI.Parent.FullName + "\\", "");
                                RelativePath = RelativePath.ToLowerInvariant();
                                RelativePath = RelativePath.Replace('\\', '/');

                                LastOffset = (UInt32)Writer.BaseStream.Position;

                                Writer.Write((Byte)RelativePath.Length);
                                Writer.Write(Encoding.GetBytes(RelativePath.ToCharArray(), 0, (Byte)RelativePath.Length));
                                Writer.Write((Int16)TPI_UNKNOWN_1);
                                Writer.Write((UInt32)Files[i].Length);
                                Writer.Write((UInt32)CompressedSizes[i]);
                                Writer.Write((UInt32)CompressedSizes[i]);
                                Writer.Write((UInt32)Files[i].Length);
                                Writer.Write((UInt32)Offsets[i]);
                            }

                            Console.WriteLine("\b\b\bOk!");

                            Console.Write("Updating header... ");
                            TpiStream.Seek(0, SeekOrigin.Begin);
                            pHeader->Offset = LastOffset;

                            Kernel.memcpy(Buffer, pHeader, sizeof(TPI.Header));
                            TpiStream.Write(Buffer, 0, sizeof(TPI.Header));
                            Console.WriteLine("Ok!");
                        }
                    }
            }
Example #27
0
        public static void main(String[] arg)
        {
            int err;
            int comprLen   = 40000;
            int uncomprLen = comprLen;

            byte[] compr   = new byte[comprLen];
            byte[] uncompr = new byte[uncomprLen];
            int    len     = hello.Length;

            ZStream c_stream = new ZStream();

            err = c_stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION);
            CHECK_ERR(c_stream, err, "deflate");

            c_stream.next_in        = hello;
            c_stream.next_in_index  = 0;
            c_stream.next_out       = compr;
            c_stream.next_out_index = 0;
            c_stream.avail_in       = 3;
            c_stream.avail_out      = comprLen;

            err = c_stream.deflate(JZlib.Z_FULL_FLUSH);
            CHECK_ERR(c_stream, err, "deflate");

            compr[3]++;              // force an error in first compressed block
            c_stream.avail_in = len - 3;

            err = c_stream.deflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_STREAM_END)
            {
                CHECK_ERR(c_stream, err, "deflate");
            }
            err = c_stream.deflateEnd();
            CHECK_ERR(c_stream, err, "deflateEnd");
            comprLen = (int)(c_stream.total_out);

            ZStream d_stream = new ZStream();

            d_stream.next_in       = compr;
            d_stream.next_in_index = 0;
            d_stream.avail_in      = 2;

            err = d_stream.inflateInit();
            CHECK_ERR(d_stream, err, "inflateInit");
            d_stream.next_out       = uncompr;
            d_stream.next_out_index = 0;
            d_stream.avail_out      = uncomprLen;

            err = d_stream.inflate(JZlib.Z_NO_FLUSH);
            CHECK_ERR(d_stream, err, "inflate");

            d_stream.avail_in = comprLen - 2;

            err = d_stream.inflateSync();
            CHECK_ERR(d_stream, err, "inflateSync");

            err = d_stream.inflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_DATA_ERROR)
            {
                java.lang.SystemJ.outJ.println("inflate should report DATA_ERROR");
                /* Because of incorrect adler32 */
                java.lang.SystemJ.exit(1);
            }

            err = d_stream.inflateEnd();
            CHECK_ERR(d_stream, err, "inflateEnd");

            int j = 0;

            for (; j < uncompr.Length; j++)
            {
                if (uncompr[j] == 0)
                {
                    break;
                }
            }

            java.lang.SystemJ.outJ.println("after inflateSync(): hel" + new java.lang.StringJ(uncompr, 0, j));
        }
Example #28
0
        public static Tuple <byte[], uint> Deflate(byte[] input)
        {
            // ReSharper disable InconsistentNaming
            const int Z_OK           = 0;
            const int Z_MEM_ERROR    = -4;
            const int Z_STREAM_ERROR = -2;
            const int Z_FINISH       = 4;
            const int Z_STREAM_END   = 1;
            // ReSharper restore InconsistentNaming

            var crc32 = Crc32Algorithm.Compute(input);

            var result = new MemoryStream();

            var outputBuf = new byte[65536];
            var zstream   = new ZStream();

            try
            {
                switch (zstream.deflateInit(9, true))
                {
                case Z_OK:
                    break;

                case Z_MEM_ERROR:
                    throw new OutOfMemoryException("zlib ran out of memory");

                case Z_STREAM_ERROR:
                    throw new ArgumentException();

                default:
                    throw new InvalidOperationException(zstream.msg);
                }

                zstream.next_in       = input;
                zstream.next_in_index = 0;
                zstream.avail_in      = input.Length;

                do
                {
                    zstream.next_out       = outputBuf;
                    zstream.next_out_index = 0;
                    zstream.avail_out      = outputBuf.Length;
                    switch (zstream.deflate(Z_FINISH))
                    {
                    case Z_OK:
                    case Z_STREAM_END:
                        var count = outputBuf.Length - zstream.avail_out;
                        if (count > 0)
                        {
                            result.Write(outputBuf, 0, count);
                        }
                        continue;

                    default:
                        throw new IOException("deflating: " + zstream.msg);
                    }
                }while (zstream.avail_in > 0 || zstream.avail_out == 0);

                return(Tuple.Create(result.ToArray(), crc32));
            }
            finally
            {
                zstream.free();
            }
        }
Example #29
0
        public static PhpString gzencode(byte[] data, int level = -1, int encoding_mode = FORCE_GZIP)
        {
            if ((level < -1) || (level > 9))
            {
                PhpException.Throw(PhpError.Warning, "compression level ({0}) must be within -1..9", level.ToString());
                return(default(PhpString));
            }

            ZStream zs = new ZStream();

            int status = zlibConst.Z_OK;

            zs.next_in  = data;
            zs.avail_in = data.Length;

            // heuristic for max data length
            zs.avail_out = data.Length + data.Length / Zlib.PHP_ZLIB_MODIFIER + 15 + 1;
            zs.next_out  = new byte[zs.avail_out];

            switch (encoding_mode)
            {
            case (int)ForceConstants.FORCE_GZIP:
                if ((status = zs.deflateInit(level, -MAX_WBITS)) != zlibConst.Z_OK)
                {
                    PhpException.Throw(PhpError.Warning, zError(status));
                    return(default(PhpString));
                }
                break;

            case (int)ForceConstants.FORCE_DEFLATE:
                if ((status = zs.deflateInit(level)) != zlibConst.Z_OK)
                {
                    PhpException.Throw(PhpError.Warning, zError(status));
                    return(default(PhpString));
                }
                break;
            }

            status = zs.deflate(zlibConst.Z_FINISH);

            if (status != zlibConst.Z_STREAM_END)
            {
                zs.deflateEnd();

                if (status == zlibConst.Z_OK)
                {
                    status = zlibConst.Z_STREAM_ERROR;
                }
            }
            else
            {
                status = zs.deflateEnd();
            }

            if (status == zlibConst.Z_OK)
            {
                long output_length = zs.total_out + (encoding_mode == (int)ForceConstants.FORCE_GZIP ? GZIP_HEADER_LENGTH + GZIP_FOOTER_LENGTH : GZIP_HEADER_LENGTH);
                long output_offset = GZIP_HEADER_LENGTH;

                byte[] output = new byte[output_length];
                Buffer.BlockCopy(zs.next_out, 0, output, (int)output_offset, (int)zs.total_out);

                // fill the header
                output[0] = GZIP_HEADER[0];
                output[1] = GZIP_HEADER[1];
                output[2] = Z_DEFLATED; // zlib constant (private in ZLIB.NET)
                output[3] = 0;          // reserved flag bits (this function puts invalid flags in here)
                // 4-8 represent time and are set to zero
                output[9] = OS_CODE;    // php constant

                if (encoding_mode == (int)ForceConstants.FORCE_GZIP)
                {
                    var    crc_algo = new PhpHash.HashPhpResource.CRC32();
                    byte[] crc      = crc_algo.ComputeHash(data);
                    crc_algo.Dispose();

                    output[output_length - 8] = crc[0];
                    output[output_length - 7] = crc[1];
                    output[output_length - 6] = crc[2];
                    output[output_length - 5] = crc[3];
                    output[output_length - 4] = (byte)(zs.total_in & 0xFF);
                    output[output_length - 3] = (byte)((zs.total_in >> 8) & 0xFF);
                    output[output_length - 2] = (byte)((zs.total_in >> 16) & 0xFF);
                    output[output_length - 1] = (byte)((zs.total_in >> 24) & 0xFF);
                }

                return(new PhpString(output));
            }
            else
            {
                PhpException.Throw(PhpError.Warning, zError(status) ?? zs.msg);
                return(default(PhpString));
            }
        }
        public static void main(String[] arg)
        {
            int err;
            int comprLen   = 40000;
            int uncomprLen = comprLen;

            byte[] compr   = new byte[comprLen];
            byte[] uncompr = new byte[uncomprLen];

            ZStream c_stream = new ZStream();

            err = c_stream.deflateInit(JZlib.Z_BEST_SPEED);
            CHECK_ERR(c_stream, err, "deflateInit");

            c_stream.next_out       = compr;
            c_stream.next_out_index = 0;
            c_stream.avail_out      = comprLen;

            // At this point, uncompr is still mostly zeroes, so it should compress
            // very well:
            c_stream.next_in  = uncompr;
            c_stream.avail_in = uncomprLen;
            err = c_stream.deflate(JZlib.Z_NO_FLUSH);
            CHECK_ERR(c_stream, err, "deflate");
            if (c_stream.avail_in != 0)
            {
                java.lang.SystemJ.outJ.println("deflate not greedy");
                java.lang.SystemJ.exit(1);
            }

            // Feed in already compressed data and switch to no compression:
            c_stream.deflateParams(JZlib.Z_NO_COMPRESSION, JZlib.Z_DEFAULT_STRATEGY);
            c_stream.next_in       = compr;
            c_stream.next_in_index = 0;
            c_stream.avail_in      = comprLen / 2;
            err = c_stream.deflate(JZlib.Z_NO_FLUSH);
            CHECK_ERR(c_stream, err, "deflate");

            // Switch back to compressing mode:
            c_stream.deflateParams(JZlib.Z_BEST_COMPRESSION, JZlib.Z_FILTERED);
            c_stream.next_in       = uncompr;
            c_stream.next_in_index = 0;
            c_stream.avail_in      = uncomprLen;
            err = c_stream.deflate(JZlib.Z_NO_FLUSH);
            CHECK_ERR(c_stream, err, "deflate");

            err = c_stream.deflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_STREAM_END)
            {
                java.lang.SystemJ.outJ.println("deflate should report Z_STREAM_END");
                java.lang.SystemJ.exit(1);
            }
            err = c_stream.deflateEnd();
            CHECK_ERR(c_stream, err, "deflateEnd");

            ZStream d_stream = new ZStream();

            d_stream.next_in       = compr;
            d_stream.next_in_index = 0;
            d_stream.avail_in      = comprLen;

            err = d_stream.inflateInit();
            CHECK_ERR(d_stream, err, "inflateInit");

            while (true)
            {
                d_stream.next_out       = uncompr;
                d_stream.next_out_index = 0;
                d_stream.avail_out      = uncomprLen;
                err = d_stream.inflate(JZlib.Z_NO_FLUSH);
                if (err == JZlib.Z_STREAM_END)
                {
                    break;
                }
                CHECK_ERR(d_stream, err, "inflate large");
            }

            err = d_stream.inflateEnd();
            CHECK_ERR(d_stream, err, "inflateEnd");

            if (d_stream.total_out != 2 * uncomprLen + comprLen / 2)
            {
                java.lang.SystemJ.outJ.println("bad large inflate: " + d_stream.total_out);
                java.lang.SystemJ.exit(1);
            }
            else
            {
                java.lang.SystemJ.outJ.println("large_inflate(): OK");
            }
        }