示例#1
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());
        }
 public virtual void end()
 {
     if (compress)
     {
         z.deflateEnd();
     }
     else
     {
         z.inflateEnd();
     }
     z.free();
     z = null;
 }
示例#3
0
        protected void EndCompression()
        {
            if (this is NetworkAardwolf)
            {
                throw new Exception("Trying to end compression on Aardwolf connection!");
            }

            if (zStream == null)
            {
                return;
            }

            byte[] d = new byte[0];
            Compress(d, zlibConst.Z_FINISH);
            if (zStream_Length > 0)
            {
                Socket.Send(zStream_Out, zStream_Length, SocketFlags.None);
            }

            zStream.deflateEnd();
            zStream.free();
            zStream = null;
        }
示例#4
0
        /// <summary>
        /// Closes any open connections between the Tibia client and game server, and stops listening for any
        /// new incoming HTTP or TCP connection requests.
        /// </summary>
        internal void Stop()
        {
            if (!_isStarted)
            {
                return;
            }

            try
            {
                _zStream.deflateEnd();
                _zStream.inflateEnd();
                _httpListener.Close();

                if (_tcpListener != null)
                {
                    _tcpListener.Stop();
                }

                if (_clientSocket != null)
                {
                    _clientSocket.Close();
                }

                if (_serverSocket != null)
                {
                    _serverSocket.Close();
                }
            }
            catch (Exception ex)
            {
                _client.Logger.Error(ex.ToString());
            }

            _isStarted      = false;
            _xteaKey        = null;
            ConnectionState = ConnectionState.Disconnected;
        }
示例#5
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));
            }
        }
示例#6
0
 protected override int EndZlibOperation(ZStream zs)
 {
     return(zs.deflateEnd());
 }
示例#7
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);
        }
示例#8
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!");
                        }
                    }
            }
示例#9
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");
            }
        }
        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));
        }
示例#11
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));
        }
        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");
            }
        }