Example #1
0
 public virtual void Finish()
 {
     deflater_.Finish();
     while (!deflater_.IsFinished)
     {
         int num = deflater_.Deflate(buffer_, 0, buffer_.Length);
         if (num <= 0)
         {
             break;
         }
         if (cryptoTransform_ != null)
         {
             EncryptBlock(buffer_, 0, num);
         }
         baseOutputStream_.Write(buffer_, 0, num);
     }
     if (!deflater_.IsFinished)
     {
         throw new SharpZipBaseException("Can't deflate all input?");
     }
     baseOutputStream_.Flush();
     if (cryptoTransform_ != null)
     {
         ((global::System.IDisposable)cryptoTransform_).Dispose();
         cryptoTransform_ = null;
     }
 }
 public virtual void Finish()
 {
     deflater_.Finish();
     while (!deflater_.IsFinished)
     {
         int num = deflater_.Deflate(buffer_, 0, buffer_.Length);
         if (num <= 0)
         {
             break;
         }
         if (cryptoTransform_ != null)
         {
             EncryptBlock(buffer_, 0, num);
         }
         baseOutputStream_.Write(buffer_, 0, num);
     }
     if (!deflater_.IsFinished)
     {
         throw new SharpZipBaseException("Can't deflate all input?");
     }
     baseOutputStream_.Flush();
     if (cryptoTransform_ != null)
     {
         if (cryptoTransform_ is ZipAESTransform)
         {
             AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode();
         }
         cryptoTransform_.Dispose();
         cryptoTransform_ = null;
     }
 }
Example #3
0
            // Write data to this compressor.
            public void Write(byte[] buffer, int offset, int count)
            {
                int len;

                // Set the input for the deflater.
                if (count == 0)
                {
                    // Nothing to do if no data was supplied.
                    return;
                }
                deflater.SetInput(buffer, offset, count);

                // Deflate data until the deflater asks for more input.
                for (;;)
                {
                    len = deflater.Deflate
                              (outBuffer, outLen, outBuffer.Length - outLen);
                    if (len > 0)
                    {
                        outLen += len;
                        if (outLen >= outBuffer.Length)
                        {
                            writer.Write
                                (PngReader.IDAT, outBuffer, 0, outLen);
                            outLen     = 0;
                            wroteBlock = true;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
Example #4
0
        /// <summary>
        /// Finishes the stream by calling finish() on the deflater.
        /// </summary>
        /// <exception cref="SharpZipBaseException">
        /// Not all input is deflated
        /// </exception>
        public virtual void Finish()
        {
            deflater_.Finish();
            while (!deflater_.IsFinished)
            {
                int len = deflater_.Deflate(buffer_, 0, buffer_.Length);
                if (len <= 0)
                {
                    break;
                }
                if (keys != null)
                {
                    EncryptBlock(buffer_, 0, len);
                }

                baseOutputStream_.Write(buffer_, 0, len);
            }

            if (!deflater_.IsFinished)
            {
                throw new SharpZipBaseException("Can't deflate all input?");
            }

            baseOutputStream_.Flush();

            if (keys != null)
            {
                keys = null;
            }
        }
        /// <summary>
        /// Writes next block of compressed data to the output stream. </summary>
        /// <exception cref="IOException"> if an I/O error has occurred </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void deflate() throws java.io.IOException
        protected internal virtual void Deflate()
        {
            int len = Def.Deflate(Buf, 0, Buf.Length);

            if (len > 0)
            {
                @out.Write(Buf, 0, len);
            }
        }
Example #6
0
        public static byte[] Compress(byte[] input, int level = Deflater.BEST_COMPRESSION)
        {
            if (input == null || input.Length == 0)
            {
                Debug.LogError("Compress error inputBytes Len = 0");
                return(input);
            }

            // Create the compressor with highest level of compression
            Deflater compressor = new Deflater(level);

            // Give the compressor the data to compress
            compressor.SetInput(input);
            compressor.Finish();

            /*
             * Create an expandable byte array to hold the compressed data.
             * You cannot use an array that's the same size as the orginal because
             * there is no guarantee that the compressed data will be smaller than
             * the uncompressed data.
             */
            MemoryStream result = new MemoryStream(input.Length);

            // Compress the data
            byte[] buffer = new byte[1024];
            while (!compressor.IsFinished)
            {
                int count = compressor.Deflate(buffer);
                result.Write(buffer, 0, count);
            }

            // Get the compressed data
            return(result.ToArray());
        }
Example #7
0
        /// <summary>
        /// Create a writeable byte array to persist data back into the PES data system.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private byte[] CreateWriteableBytes(byte[] input)
        {
            uint fsize = (uint)input.Length;

            Deflater deflater = new Deflater(9);

            deflater.SetInput(input);
            deflater.Finish();
            using (var ms = new MemoryStream())
            {
                var outputBuffer = new byte[65536 * 32];
                while (deflater.IsNeedingInput == false)
                {
                    var read = deflater.Deflate(outputBuffer);
                    ms.Write(outputBuffer, 0, read);

                    if (deflater.IsFinished == true)
                    {
                        break;
                    }
                }

                deflater.Reset();

                uint   zsize  = (uint)ms.Length;
                byte[] header = { 0x04, 0x10, 0x01, 0x57, 0x45, 0x53, 0x59, 0x53 };
                byte[] b1, b2;
                b1 = BitConverter.GetBytes(zsize);
                b2 = BitConverter.GetBytes(fsize);

                byte[] zlib = ms.ToArray();

                return(header.Concat(b1).Concat(b2).Concat(zlib).ToArray());
            }
        }
Example #8
0
        public static byte[] Compress(byte[] input, int offset, int len)
        {
            byte[]   output     = new byte[1024];
            int      outputused = 0;
            Deflater deflater   = new Deflater();

            deflater.SetInput(input, offset, len);
            deflater.Finish();
            while (deflater.IsFinished == false)
            {
                //				if(deflater.IsNeedingInput && deflater.IsFinished == false)
                //					throw(new Exception("deflateData: input incomplete!"));
                if (outputused == output.Length)
                {
                    byte[] newOutput = new byte[output.Length * 2];
                    Array.Copy(output, newOutput, output.Length);
                    output = newOutput;
                }
                try
                {
                    outputused += deflater.Deflate(output, outputused, output.Length - outputused);
                }
                catch (FormatException e)
                {
                    throw(new IOException(e.ToString()));
                }
            }
            deflater.Reset();
            byte[] realOutput = new byte[outputused];
            Array.Copy(output, realOutput, outputused);
            return(realOutput);
        }
Example #9
0
    public static byte[] Deflate(ReadOnlySpan <int> ints)
    {
        if (ints == null)
        {
            throw new ArgumentNullException(nameof(ints));
        }
        byte[] input   = new byte[ints.Length * sizeof(int)];
        var    asBytes = MemoryMarshal.Cast <int, byte>(ints);

        asBytes.CopyTo(input.AsSpan());

        var deflater = new Deflater();

        deflater.SetLevel(Deflater.DEFAULT_COMPRESSION);
        deflater.SetInput(input);
        deflater.Finish();

        using var ms = new MemoryStream(input.Length);
        byte[] buf = new byte[1024];
        while (!deflater.IsFinished)
        {
            int count = deflater.Deflate(buf);
            ms.Write(buf, 0, count);
        }

        return(ms.ToArray());
    }
Example #10
0
        private void Deflate()
        {
            var buffer = sink.Buffer();

            while (true)
            {
                Segment s        = buffer.WritableSegment(1);
                int     deflated = deflater.Deflate(s.Data, s.Limit, Segment.SIZE - s.Limit);
                if (deflated > 0)
                {
                    s.Limit     += deflated;
                    buffer.Size += deflated;
                    sink.EmitCompleteSegments();
                }
                else if (deflater.IsNeedingInput)
                {
                    if (s.Pos == s.Limit)
                    {
                        // We allocated a tail segment, but didn't end up needing it. Recycle!
                        buffer.Head = s.Pop();
                        SegmentPool.Recycle(s);
                    }
                    return;
                }
            }
        }
Example #11
0
        protected override void Encode(IChannelHandlerContext ctx, IByteBuffer msg, List <object> output)
        {
            var buffer = Unpooled.Buffer();

            if (msg.ReadableBytes >= CompressionThreshold)
            {
                var data = msg.ToArray(out var offset, out var count);

                buffer.WriteVarInt32(count);

                _deflater.SetInput(data, offset, count);
                _deflater.Finish();

                while (!_deflater.IsFinished)
                {
                    var read = _deflater.Deflate(_buffer);
                    buffer.WriteBytes(_buffer, 0, read);
                }

                _deflater.Reset();
            }
            else
            {
                buffer.WriteVarInt32(0);
                buffer.WriteBytes(msg);
            }

            output.Add(buffer);
        }
Example #12
0
        private static MemoryStream Compress(MemoryStream stream)
        {
            stream.Position = 0;
            byte[]   input      = stream.ToArray();
            byte[]   size       = BitConverter.GetBytes(Convert.ToUInt32(input.Length));
            Deflater compressor = new Deflater();

            compressor.SetLevel(Deflater.BEST_SPEED);
            compressor.SetInput(input);
            compressor.Finish();
            MemoryStream bos = new MemoryStream(input.Length);

            byte[] buf = new byte[1024];
            while (!compressor.IsFinished)
            {
                int count = compressor.Deflate(buf);
                bos.Write(buf, 0, count);
            }
            MemoryStream result      = new MemoryStream();
            BinaryWriter writeBinary = new BinaryWriter(result);

            writeBinary.Write(size);
            writeBinary.Write(bos.ToArray());
            writeBinary.Close();
            return(result);
        }
Example #13
0
        void IPreviewCallback.OnPreviewFrame(byte[] b, Camera c)
        {
            Android.Graphics.YuvImage yuvImage = new Android.Graphics.YuvImage(b, Android.Graphics.ImageFormat.Nv21, previewSize.Width, previewSize.Height, null);
            byte[]       msg = new byte[65000];
            MemoryStream ms  = new MemoryStream();

            yuvImage.CompressToJpeg(new Android.Graphics.Rect(0, 0, previewSize.Width, previewSize.Height), 100, ms);

            Deflater compresser = new Deflater();

            compresser.SetInput(ms.ToArray());
            compresser.Finish();
            compresser.Deflate(msg);             //int length = compresser.Deflate(msg);
            compresser.End();

            var current = Connectivity.NetworkAccess;

            if (current == Xamarin.Essentials.NetworkAccess.Internet)
            {
                try
                {
                    Sock.SendTo(msg, EndP);
                }
                catch
                {
                }
            }
            Thread.Sleep(300);
        }
Example #14
0
        public static byte[] Deflate(byte[] inputData)
        {
            var deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, false);

            deflater.SetInput(inputData);
            deflater.Finish();

            using (var ms = new MemoryStream())
            {
                var outputBuffer = new byte[65536 * 4];
                while (deflater.IsNeedingInput == false)
                {
                    var read = deflater.Deflate(outputBuffer);
                    ms.Write(outputBuffer, 0, read);

                    if (deflater.IsFinished == true)
                    {
                        break;
                    }
                }
                deflater.Reset();

                return(ms.ToArray());
            }
        }
        public static String Compress(string str)
        {
            byte[] bytesToCompress = ASCIIEncoding.UTF8.GetBytes(str);
            // Compressor with highest level of compression.
            Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);

            compressor.SetInput(bytesToCompress); // Give the compressor the data to
            // compress.
            compressor.Finish();

            // Create an expandable byte array to hold the compressed data.
            // It is not necessary that the compressed data will be smaller than
            // the uncompressed data.
            byte[] ret = null;


            using (MemoryStream memStream = new MemoryStream(bytesToCompress.Length))
            {
                // Compress the data
                byte[] buf = new byte[bytesToCompress.Length + 100];
                while (!compressor.IsFinished)
                {
                    memStream.Write(buf, 0, compressor.Deflate(buf));
                }
                memStream.Close();
                ret = memStream.ToArray();
            }

            // Get the compressed data
            return(Convert.ToBase64String(ret));
        }
Example #16
0
        /// <summary>
        /// Deflates the decompressed bytes.
        /// </summary>
        /// <param name="decompressedData"> Data. </param>
        /// <param name="offset"> Offset in the data. </param>
        /// <param name="count"> Number of the written bytes. </param>
        /// <param name="compressedSize"> Compressed size. Optional parameter, but if it is known, then passing of the parameter may increasing performance. </param>
        /// <returns> Array of the compressed bytes. </returns>
        public static byte[] Deflate(byte[] decompressedData, int offset, int count, int?compressedSize = null)
        {
            IByteArrayWriter writer   = new ByteArrayWriter(true);
            Deflater         deflater = new Deflater(Deflater.DEFLATED);

            deflater.SetInput(decompressedData, offset, count);
            deflater.Finish();

            int length = (compressedSize != null) ? compressedSize.Value : count;

            byte[] compressedData;
            int    processedBytes;

            while (true)
            {
                compressedData = new byte[length];
                processedBytes = deflater.Deflate(compressedData, 0, length);

                if (processedBytes != 0)
                {
                    writer.WriteBytes(compressedData, 0, processedBytes);
                }
                else
                {
                    if (deflater.IsFinished)
                    {
                        break;
                    }
                }
            }

            return(writer.GetBytes());
        }
Example #17
0
        /// <summary>
        /// Encode a RIF byte array to zRIF string.
        /// </summary>
        /// <param name="zrif">The zRIF string.</param>
        /// <returns>The zRIF string or null on error.</returns>
        public static string Encode(byte[] rif)
        {
            byte[] output = new byte[128 + 2];

            if ((rif.Length != 512) && (rif.Length != 1024))
            {
                Console.Error.WriteLine("[ERROR] invalid RIF length");
                return(null);
            }
            var deflater = new Deflater();

            deflater.SetDictionary(zrif_dict);
            deflater.SetInput(rif);
            deflater.SetLevel(Deflater.BEST_COMPRESSION);
            deflater.SetStrategy(DeflateStrategy.Default);
            deflater.Finish();
            int size = deflater.Deflate(output, 0, output.Length - 2);

            if (!deflater.IsFinished)
            {
                Console.Error.WriteLine("[ERROR] Deflate error");
                return(null);
            }
            // Don't have that much control over Window size so our header needs to be adjusted.
            if ((output[0] == 0x78) && (output[1] == 0xF9))
            {
                output[0] = 0x28;
                output[1] = 0xEE;
            }
            // Adjust the size to be a multiple of 3 so that we don't get padding
            return(Convert.ToBase64String(output, 0, ((size + 2) / 3) * 3));
        }
Example #18
0
        private void WriteWhole(Deflater def, int typeCode, byte[] data)
        {
            int sz     = data.Length;
            int hdrlen = 0;

            _buffer[hdrlen++] = (byte)((typeCode << 4) | sz & 15);
            sz = (int)(((uint)sz) >> 4);

            while (sz > 0)
            {
                _buffer[hdrlen - 1] |= 0x80;
                _buffer[hdrlen++]    = (byte)(sz & 0x7f);
                sz = (int)(((uint)sz) >> 7);
            }

            _packDigest.Update(_buffer, 0, hdrlen);
            _crc.Update(_buffer, 0, hdrlen);
            _packOut.Write(_buffer, 0, hdrlen);
            def.Reset();
            def.SetInput(data);
            def.Finish();

            while (!def.IsFinished)
            {
                int datlen = def.Deflate(_buffer);
                _packDigest.Update(_buffer, 0, datlen);
                _crc.Update(_buffer, 0, datlen);
                _packOut.Write(_buffer, 0, datlen);
            }
        }
Example #19
0
            public override void Compress(byte[] bytes, int off, int len, DataOutput @out)
            {
                Compressor.Reset();
                Compressor.SetInput((byte[])(Array)bytes, off, len);
                Compressor.Finish();

                if (Compressor.NeedsInput)
                {
                    // no output
                    Debug.Assert(len == 0, len.ToString());
                    @out.WriteVInt(0);
                    return;
                }

                int totalCount = 0;

                for (; ;)
                {
                    int count = Compressor.Deflate(Compressed, totalCount, Compressed.Length - totalCount);
                    totalCount += count;
                    Debug.Assert(totalCount <= Compressed.Length);
                    if (Compressor.IsFinished)
                    {
                        break;
                    }
                    else
                    {
                        Compressed = ArrayUtil.Grow(Compressed);
                    }
                }

                @out.WriteVInt(totalCount);
                @out.WriteBytes(Compressed, totalCount);
            }
        public byte[] Compress(byte[] input, int offset, int length)
        {
            // Create the compressor with highest level of compression
            Deflater compressor = new Deflater();

            compressor.SetLevel(Deflater.BEST_COMPRESSION);

            // Give the compressor the data to compress
            compressor.SetInput(input, offset, length);
            compressor.Finish();

            /*
             * Create an expandable byte array to hold the compressed data.
             * You cannot use an array that's the same size as the orginal because
             * there is no guarantee that the compressed data will be smaller than
             * the uncompressed data.
             */
            MemoryStream bos = new MemoryStream(input.Length);

            // Compress the data
            byte[] buf = new byte[1024];
            while (!compressor.IsFinished)
            {
                int count = compressor.Deflate(buf);
                bos.Write(buf, 0, count);
            }

            // Get the compressed data
            return(bos.ToArray());
        }
Example #21
0
        /// <summary>
        /// Compresses this instance.
        /// </summary>
        public void Compress()
        {
            var deflater = new Deflater();

            byte[] packet = PacketData;
            deflater.SetInput(packet, 0, packet.Length);
            deflater.Finish();

            var compBuffer = new byte[1024];
            var ret        = new List <byte>();

            while (!deflater.IsFinished)
            {
                try
                {
                    deflater.Deflate(compBuffer);
                    ret.AddRange(compBuffer);
                    Array.Clear(compBuffer, 0, compBuffer.Length);
                }
                catch (Exception ex)
                {
                    Logging.WriteException(ex);
                    return;
                }
            }
            deflater.Reset();

            Seek((byte)_headerType, SeekOrigin.Begin);
            // Write the compressed bytes over whatever is there.
            Write(ret.ToArray());
            // Set the stream length to the end of the actual packet data.
            // This makes sure we don't have any 'junk' packets at the end.
            OutStream.SetLength(BaseStream.Position);
        }
Example #22
0
        public byte[] GetCompressedOutPacket(int offset, int length)
        {
            var deflater = new Deflater();

            deflater.SetInput(Data, offset, length);
            deflater.Finish();

            var compBuffer = new byte[1024];
            var ret        = new List <byte>();

            while (!deflater.IsFinished)
            {
                try
                {
                    deflater.Deflate(compBuffer);
                    ret.AddRange(compBuffer);
                    Array.Clear(compBuffer, 0, compBuffer.Length);
                }
                catch (Exception ex)
                {
                    Logging.WriteException(ex);
                    return(null);
                }
            }
            deflater.Reset();
            return(ret.ToArray());
        }
Example #23
0
        public static byte[] ZlibCompress(byte[] input)
        {
            using var ms = new MemoryStream(input.Length);
            using var bs = new BinaryStream(ms);
            bs.WriteUInt32(Constants.ZLIB_MAGIC);
            bs.WriteInt32(-input.Length);

            /* For some reason System.IO.Compression has issues making the games load these files?
             * using var ds = new DeflateStream(ms, CompressionLevel.Optimal);
             * ds.Write(input, 0, input.Length);
             * ds.Flush();
             */

            // Fall back to ICSharpCode
            var d = new Deflater(Deflater.DEFAULT_COMPRESSION, true);

            d.SetInput(input);
            d.Finish();

            int count = d.Deflate(input);

            bs.Write(input, 0, count);

            return(ms.ToArray());
        }
Example #24
0
        public static byte[] Compress(string str)
        {
            byte[]   bytes    = Encoding.Unicode.GetBytes(str);
            Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);

            deflater.SetInput(bytes);
            deflater.Finish();
            MemoryStream stream = new MemoryStream(bytes.Length);

            try
            {
                byte[] output = new byte[0x400];
                while (!deflater.IsFinished)
                {
                    int count = deflater.Deflate(output);
                    stream.Write(output, 0, count);
                }
            }
            finally
            {
                stream.Close();
            }
            byte[] buffer3 = stream.ToArray();
            if ((buffer3.Length % 2) == 0)
            {
                return(buffer3);
            }
            byte[] buffer4 = new byte[buffer3.Length + 1];
            for (int i = 0; i < buffer3.Length; i++)
            {
                buffer4[i] = buffer3[i];
            }
            buffer4[buffer3.Length] = 0;
            return(buffer4);
        }
Example #25
0
        /// <summary>
        /// Finishes the stream by calling finish() on the deflater.
        /// </summary>
        /// <exception cref="InvalidDataException">
        /// Not all input is deflated.
        /// </exception>
        public virtual void Finish()
        {
            Deflater.Finish();
            while (!Deflater.IsFinished)
            {
                var len = Deflater.Deflate(buffer, 0, buffer.Length);
                if (len <= 0)
                {
                    break;
                }

                if (CryptoTransform != null)
                {
                    EncryptBlock(buffer, 0, len);
                }

                BaseOutputStream.Write(buffer, 0, len);
            }

            if (!Deflater.IsFinished)
            {
                throw new InvalidDataException("Can't deflate all input?");
            }

            BaseOutputStream.Flush();
        }
Example #26
0
        private const int CopyBufferSize = 32 * 1024;    // 32kb

        public void Compress(Stream source, Stream destination)
        {
            /*
             * var deflater = new DeflaterOutputStream(destination, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
             *
             * var dataBuffer = new byte[CopyBufferSize];
             * StreamUtils.Copy(source, deflater, dataBuffer);
             */

            var def = new Deflater(Deflater.DEFAULT_COMPRESSION, true);

            var inputData = new byte[source.Length - source.Position];

            source.Read(inputData, 0, inputData.Length);

            var buffer = new byte[CopyBufferSize];

            def.SetInput(inputData, 0, inputData.Length);
            def.Finish();

            while (!def.IsFinished)
            {
                int outputLen = def.Deflate(buffer, 0, buffer.Length);
                destination.Write(buffer, 0, outputLen);
            }

            def.Reset();
        }
Example #27
0
        /// <summary>
        /// 字节数组压缩
        /// </summary>
        /// <param name="data">待压缩的字节数组</param>
        /// <param name="isClearData">压缩完成后,是否清除待压缩字节数组里面的内容</param>
        /// <returns>已压缩的字节数组</returns>
        public byte[] ZipCompress(byte[] data, bool isClearData = true)
        {
            byte[]   bytes = null;
            Deflater f     = new Deflater(Deflater.BEST_COMPRESSION);

            f.SetInput(data);
            f.Finish();
            int count = 0;

            using (MemoryStream o = new MemoryStream(data.Length))
            {
                byte[] buffer = new byte[BUFFER_LENGTH];
                while (!f.IsFinished)
                {
                    count = f.Deflate(buffer);
                    o.Write(buffer, 0, count);
                }
                bytes = o.ToArray();
            }
            if (isClearData)
            {
                Array.Clear(data, 0, data.Length);
            }
            return(bytes);
        }
Example #28
0
        /// <summary>
        /// Compresses the specified byte range using the
        ///  specified compressionLevel (constants are defined in
        ///  java.util.zip.Deflater).
        /// </summary>
        public static byte[] Compress(sbyte[] value, int offset, int length, int compressionLevel)
        {
            /* Create an expandable byte array to hold the compressed data.
             * You cannot use an array that's the same size as the orginal because
             * there is no guarantee that the compressed data will be smaller than
             * the uncompressed data. */
            ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

            Deflater compressor = SharpZipLib.CreateDeflater();

            try
            {
                compressor.SetLevel(compressionLevel);
                compressor.SetInput((byte[])(Array)value, offset, length);
                compressor.Finish();

                // Compress the data
                var buf = new byte[1024];
                while (!compressor.IsFinished)
                {
                    int count = compressor.Deflate(buf);
                    bos.Write(buf, 0, count);
                }
            }
            finally
            {
            }

            return(bos.ToArray());
        }
Example #29
0
        public static byte[] Compress(byte[] input, int offset, int len)
        {
            byte[]   sourceArray = new byte[0x400];
            int      num         = 0;
            Deflater deflater    = new Deflater();

            deflater.SetInput(input, offset, len);
            deflater.Finish();
            while (!deflater.IsFinished)
            {
                if (num == sourceArray.Length)
                {
                    byte[] buffer2 = new byte[sourceArray.Length * 2];
                    Array.Copy(sourceArray, buffer2, sourceArray.Length);
                    sourceArray = buffer2;
                }
                try
                {
                    num += deflater.Deflate(sourceArray, num, sourceArray.Length - num);
                }
                catch (FormatException exception)
                {
                    throw new IOException(exception.ToString());
                }
            }
            deflater.Reset();
            byte[] destinationArray = new byte[num];
            Array.Copy(sourceArray, destinationArray, num);
            return(destinationArray);
        }
Example #30
0
        /// <summary>Performs deflate compression on the given data.</summary>
        /// <param name="input">the data to compress</param>
        /// <param name="output">the compressed data</param>
        public static void CompressZLib(byte[] input, byte[] output, int compressionLevel, out int deflatedLength)
        {
            Deflater deflater = new Deflater(compressionLevel);

            deflater.SetInput(input, 0, input.Length);
            deflater.Finish();
            deflatedLength = deflater.Deflate(output, 0, output.Length);
        }
        public static String Compress(string str)
        {
            byte[] bytesToCompress = ASCIIEncoding.UTF8.GetBytes(str);
            // Compressor with highest level of compression.
            Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
            compressor.SetInput(bytesToCompress); // Give the compressor the data to
            // compress.
            compressor.Finish();

            // Create an expandable byte array to hold the compressed data.
            // It is not necessary that the compressed data will be smaller than
            // the uncompressed data.
            byte[] ret = null;

            using (MemoryStream memStream = new MemoryStream(bytesToCompress.Length))
            {
                // Compress the data
                byte[] buf = new byte[bytesToCompress.Length + 100];
                while (!compressor.IsFinished)
                {
                    memStream.Write(buf, 0, compressor.Deflate(buf));
                }
                memStream.Close();
                ret = memStream.ToArray();
            }

            // Get the compressed data
            return Convert.ToBase64String(ret);
        }
Example #32
0
 private static byte[] Zip(byte[] buffer, int version, byte[] key, byte[] iv)
 {
     byte[] buffer12;
     try
     {
         ZipStream stream = new ZipStream();
         if (version == 0)
         {
             Deflater deflater = new Deflater();
             DateTime now = DateTime.Now;
             long num = (long) ((ulong) ((((((((now.Year - 0x7bc) & 0x7f) << 0x19) | (now.Month << 0x15)) | (now.Day << 0x10)) | (now.Hour << 11)) | (now.Minute << 5)) | (now.Second >> 1)));
             uint[] numArray = new uint[] { 
                 0, 0x77073096, 0xee0e612c, 0x990951ba, 0x76dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0xedb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x9b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 
                 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 
                 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 
                 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 
                 0x76dc4190, 0x1db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x6b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0xf00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x86d3d2d, 0x91646c97, 0xe6635c01, 
                 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 
                 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 
                 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 
                 0xedb88320, 0x9abfb3b6, 0x3b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x4db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0xd6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0xa00ae27, 0x7d079eb1, 
                 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 
                 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 
                 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 
                 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x26d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x5005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0xcb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0xbdbdf21, 
                 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 
                 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 
                 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
              };
             uint maxValue = uint.MaxValue;
             uint num3 = maxValue;
             int num4 = 0;
             int length = buffer.Length;
             while (--length >= 0)
             {
                 num3 = numArray[(int) ((IntPtr) ((num3 ^ buffer[num4++]) & 0xff))] ^ (num3 >> 8);
             }
             num3 ^= maxValue;
             stream.WriteInt(0x4034b50);
             stream.WriteShort(20);
             stream.WriteShort(0);
             stream.WriteShort(8);
             stream.WriteInt((int) num);
             stream.WriteInt((int) num3);
             long position = stream.Position;
             stream.WriteInt(0);
             stream.WriteInt(buffer.Length);
             byte[] bytes = Encoding.UTF8.GetBytes("{data}");
             stream.WriteShort(bytes.Length);
             stream.WriteShort(0);
             stream.Write(bytes, 0, bytes.Length);
             deflater.SetInput(buffer);
             while (!deflater.IsNeedingInput)
             {
                 byte[] output = new byte[0x200];
                 int count = deflater.Deflate(output);
                 if (count <= 0)
                 {
                     break;
                 }
                 stream.Write(output, 0, count);
             }
             deflater.Finish();
             while (!deflater.IsFinished)
             {
                 byte[] buffer4 = new byte[0x200];
                 int num8 = deflater.Deflate(buffer4);
                 if (num8 <= 0)
                 {
                     break;
                 }
                 stream.Write(buffer4, 0, num8);
             }
             long totalOut = deflater.TotalOut;
             stream.WriteInt(0x2014b50);
             stream.WriteShort(20);
             stream.WriteShort(20);
             stream.WriteShort(0);
             stream.WriteShort(8);
             stream.WriteInt((int) num);
             stream.WriteInt((int) num3);
             stream.WriteInt((int) totalOut);
             stream.WriteInt(buffer.Length);
             stream.WriteShort(bytes.Length);
             stream.WriteShort(0);
             stream.WriteShort(0);
             stream.WriteShort(0);
             stream.WriteShort(0);
             stream.WriteInt(0);
             stream.WriteInt(0);
             stream.Write(bytes, 0, bytes.Length);
             stream.WriteInt(0x6054b50);
             stream.WriteShort(0);
             stream.WriteShort(0);
             stream.WriteShort(1);
             stream.WriteShort(1);
             stream.WriteInt(0x2e + bytes.Length);
             stream.WriteInt((30 + bytes.Length) + ((int) totalOut));
             stream.WriteShort(0);
             stream.Seek(position, SeekOrigin.Begin);
             stream.WriteInt((int) totalOut);
         }
         else if (version == 1)
         {
             byte[] buffer5;
             stream.WriteInt(0x17d7a7b);
             stream.WriteInt(buffer.Length);
             for (int i = 0; i < buffer.Length; i += buffer5.Length)
             {
                 buffer5 = new byte[Math.Min(0x1fffff, buffer.Length - i)];
                 Buffer.BlockCopy(buffer, i, buffer5, 0, buffer5.Length);
                 long num11 = stream.Position;
                 stream.WriteInt(0);
                 stream.WriteInt(buffer5.Length);
                 Deflater deflater2 = new Deflater();
                 deflater2.SetInput(buffer5);
                 while (!deflater2.IsNeedingInput)
                 {
                     byte[] buffer6 = new byte[0x200];
                     int num12 = deflater2.Deflate(buffer6);
                     if (num12 <= 0)
                     {
                         break;
                     }
                     stream.Write(buffer6, 0, num12);
                 }
                 deflater2.Finish();
                 while (!deflater2.IsFinished)
                 {
                     byte[] buffer7 = new byte[0x200];
                     int num13 = deflater2.Deflate(buffer7);
                     if (num13 <= 0)
                     {
                         break;
                     }
                     stream.Write(buffer7, 0, num13);
                 }
                 long num14 = stream.Position;
                 stream.Position = num11;
                 stream.WriteInt((int) deflater2.TotalOut);
                 stream.Position = num14;
             }
         }
         else
         {
             if (version == 2)
             {
                 stream.WriteInt(0x27d7a7b);
                 byte[] inputBuffer = Zip(buffer, 1, null, null);
                 using (ICryptoTransform transform = GetDesTransform(key, iv, false))
                 {
                     byte[] buffer9 = transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
                     stream.Write(buffer9, 0, buffer9.Length);
                     goto Label_044F;
                 }
             }
             if (version == 3)
             {
                 stream.WriteInt(0x37d7a7b);
                 byte[] buffer10 = Zip(buffer, 1, null, null);
                 using (ICryptoTransform transform2 = GetAesTransform(key, iv, false))
                 {
                     byte[] buffer11 = transform2.TransformFinalBlock(buffer10, 0, buffer10.Length);
                     stream.Write(buffer11, 0, buffer11.Length);
                 }
             }
         }
     Label_044F:
         stream.Flush();
         stream.Close();
         buffer12 = stream.ToArray();
     }
     catch (Exception exception)
     {
         ExceptionMessage = "ERR 2003: " + exception.Message;
         throw;
     }
     return buffer12;
 }
Example #33
0
    static int Deflate(Deflater def, byte [] src, byte [] dest)
    {
        bool count;
        int offset, length, remain;

        if (dest == null) {
            dest = new byte [BlockSize];
            count = true;
        } else
            count = false;

        def.Reset ();
        def.SetInput (src);

        offset = 0;
        while (!def.IsFinished) {
            if (def.IsNeedingInput)
                def.Finish ();

            remain = Math.Min (dest.Length - offset, BlockSize);
            if (remain == 0)
                break;

            length = def.Deflate (dest, offset, remain);
            if (!count)
                offset += length;
        }

        return def.TotalOut;
    }