Esempio n. 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());
        }
Esempio n. 2
0
 void ClearContext(bool @in, bool @out)
 {
     if (@in && inbound != null)
     {
         inbound.free(); inbound = null;
     }
     if (@out && outbound != null)
     {
         outbound.free(); outbound = null;
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="CompressedStream"/> and
        /// optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
        /// <c>false</c> to release only the unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && !disposed)
            {
                InnerStream.Dispose();
                disposed = true;
                zOut.free();
                zIn.free();
            }

            base.Dispose(disposing);
        }
 public virtual void end()
 {
     if (compress)
     {
         z.deflateEnd();
     }
     else
     {
         z.inflateEnd();
     }
     z.free();
     z = null;
 }
Esempio n. 5
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;
        }
Esempio n. 6
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();
            }
        }