Пример #1
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]);
            }
        }
        public static void CompressData(byte[] inData, out byte[] outData)
        {
            ZStream stream = new ZStream();

            byte[] outZData = new byte[CHUNK];
            stream.next_in   = inData;
            stream.avail_in  = bufferIndex;
            stream.next_out  = outZData;
            stream.avail_out = CHUNK;

            // deflateInit2(strm, level, Z_DEFLATED, bits, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
            // CALL_ZLIB(deflateInit2(strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY));
            // CALL_ZLIB(deflate(&strm, Z_FINISH));
            const int BITS = 15; // full 32k=15 outbound buffer

            stream.deflateInit(zlibConst.Z_DEFAULT_COMPRESSION, BITS);

            stream.deflate(zlibConst.Z_FINISH);
            stream.deflateEnd();

            byte[] output = new byte[stream.total_out + 4];
            Buffer.BlockCopy(stream.next_out, 0, output, 4, (int)stream.total_out);
            byte[] tmpbyte4 = new byte[4];
            tmpbyte4 = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(bufferIndex));
            Array.Copy(tmpbyte4, output, 4);
            outData       = output;
            deflated_size = CHUNK - stream.avail_out;
        }
Пример #3
0
    public static byte[] Compress(byte[] buff)
    {
        //using (MemoryStream ms = new MemoryStream())
        //{
        //    Stream s = new ZOutputStream(ms, level);
        //    s.Write(src, 0, (int)ms.Length);
        //    s.Close();
        //    return ms.ToArray();
        //}
        ZStream zs = new ZStream();

        zs.next_in   = buff;
        zs.avail_in  = buff.Length;
        zs.next_out  = _buffer;
        zs.avail_out = _buffer.Length;
        if (zlibConst.Z_OK != zs.deflateInit(zlibConst.Z_BEST_COMPRESSION))
        {
            return(null);
        }
        if (zlibConst.Z_STREAM_END != zs.deflate(zlibConst.Z_FINISH))
        {
            return(null);
        }
        zs.deflateEnd();
        byte[] result = new byte[zs.total_out];
        System.Array.Copy(_buffer, result, zs.total_out);
        return(result);
    }
Пример #4
0
        /// <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());
        }
Пример #5
0
        public override void  Write(System.Byte[] b1, int off, int len)
        {
            if (len == 0)
            {
                return;
            }
            int err;

            byte[] b = new byte[b1.Length];
            System.Array.Copy(b1, 0, b, 0, b1.Length);
            z.next_in       = b;
            z.next_in_index = off;
            z.avail_in      = len;
            do
            {
                z.next_out       = buf;
                z.next_out_index = 0;
                z.avail_out      = bufsize;
                if (compress)
                {
                    err = z.deflate(flush_Renamed_Field);
                }
                else
                {
                    err = z.inflate(flush_Renamed_Field);
                }

                if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
                {
                    if (err == zlibConst.Z_NEED_DICT & !this.compress)
                    {
                        z.inflateSetDictionary(this.dictionary, this.dictionary.Length);
                        err = z.inflate(flush_Renamed_Field);
                        if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
                        {
                            throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
                        }
                    }
                    else
                    {
                        throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
                    }
                }

                out_Renamed.Write(buf, 0, bufsize - z.avail_out);
            }while (z.avail_in > 0 || z.avail_out == 0);
        }
Пример #6
0
        public void Write(byte[] data, int pos, int len, bool finish)
        {
            byte[] srcData = Util.ExtractByteArray(data, pos, len);
            byte[] dstData = new byte[srcData.Length * 2 + 100];

            if (this.finished)
            {
                throw new ApplicationException("already finished");
            }

            zs.next_in       = srcData;
            zs.avail_in      = srcData.Length;
            zs.next_in_index = 0;

            zs.next_out       = dstData;
            zs.avail_out      = dstData.Length;
            zs.next_out_index = 0;

            if (finish)
            {
                zs.deflate(zlibConst.Z_FINISH);
            }
            else
            {
                zs.deflate(zlibConst.Z_SYNC_FLUSH);
            }

            fifo.Write(dstData, 0, dstData.Length - zs.avail_out);

            currentSize += len;

            this.crc32 = ZipUtil.Crc32Next(data, pos, len, this.crc32);

            if (finish)
            {
                this.finished = true;
                this.crc32    = ~this.crc32;

                GZipFooter f = new GZipFooter();
                f.CRC32 = this.crc32;
                f.ISIZE = (uint)(this.currentSize % 0x100000000);

                fifo.Write(Util.StructToByte(f));
            }
        }
Пример #7
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);
        }
Пример #8
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 Finish()
 {
     do
     {
         z.next_out       = buf;
         z.next_out_index = 0;
         z.avail_out      = buf.Length;
         int num = compress ? z.deflate(4) : z.inflate(4);
         if (num != 1 && num != 0)
         {
             throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
         }
         int num2 = buf.Length - z.avail_out;
         if (num2 > 0)
         {
             output.Write(buf, 0, num2);
         }
     }while (z.avail_in > 0 || z.avail_out == 0);
     Flush();
 }
Пример #10
0
        public static Bytes compress([NotNone] IBufferProtocol data,
                                     int level = Z_DEFAULT_COMPRESSION)
        {
            using var buffer = data.GetBuffer();
            byte[] input  = buffer.AsUnsafeArray() ?? buffer.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)
            {
                var res = new byte[(int)zst.total_out];
                Array.Copy(output, res, res.Length);
                return(Bytes.Make(res));
            }

            throw zlib_error(zst, err, "while finishing compression");
        }
Пример #11
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));
            }
        }
Пример #12
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");
            }
        }
Пример #13
0
        public byte[] Construct(IStreamCipher encryptor, ZStream dStream)
        {
            Log.Write(LogLevel.Client, "Constructing Packet [{0}]", GetType().ToString());

            byte[] cData = _stream.ToArray();

            //Log.Write(LogLevel.Error, "{0}", cData.ToHEX());

            if (_def)
            {
                MemoryStream lStream = new MemoryStream();

                dStream.avail_in      = cData.Length;
                dStream.next_in       = cData;
                dStream.next_in_index = 0;

                byte[] dData = new byte[cData.Length * 2];

                dStream.avail_out      = cData.Length * 2;
                dStream.next_out       = dData;
                dStream.next_out_index = 0;

                dStream.deflate(2);

                lStream.Write(dData, 0, (cData.Length * 2) - dStream.avail_out);
                cData = lStream.ToArray();
                lStream.Dispose();
            }

            MemoryStream       oStream = new MemoryStream();
            EndianBinaryWriter oWriter = new EndianBinaryWriter(MiscUtil.Conversion.EndianBitConverter.Little, oStream);

            oWriter.Write((byte)GetModule());
            oWriter.Write((UInt32)cData.Length + 2);
            oWriter.Write((byte)GenerateChecksum(oStream.ToArray()));
            oWriter.Write(cData, 0, cData.Length - 4);

            byte[] fData = oStream.ToArray();

            if (_enc)
            {
                byte[] eData = new byte[fData.Length];
                encryptor.ProcessBytes(fData, 0, fData.Length, eData, 0);
                fData = eData;
            }

            return(fData);
        }
Пример #14
0
        public int read(byte[] b, int off, int len)
        {
            if (len == 0)
            {
                return(0);
            }
            int err;

            z.next_out       = b;
            z.next_out_index = off;
            z.avail_out      = len;
            do
            {
                if ((z.avail_in == 0) && (!nomoreinput))
                {
                    // if buffer is empty and more input is avaiable, refill it
                    z.next_in_index = 0;
                    z.avail_in      = SupportClass.ReadInput(in_Renamed, buf, 0, bufsize); //(bufsize<z.avail_out ? bufsize : z.avail_out));
                    if (z.avail_in == -1)
                    {
                        z.avail_in  = 0;
                        nomoreinput = true;
                    }
                }
                if (compress)
                {
                    err = z.deflate(flush);
                }
                else
                {
                    err = z.inflate(flush);
                }
                if (nomoreinput && (err == zlibConst.Z_BUF_ERROR))
                {
                    return(-1);
                }
                if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
                {
                    throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
                }
                if (nomoreinput && (z.avail_out == len))
                {
                    return(-1);
                }
            }while (z.avail_out == len && err == zlibConst.Z_OK);
            //System.err.print("("+(len-z.avail_out)+")");
            return(len - z.avail_out);
        }
Пример #15
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);
            }
        }
Пример #16
0
        internal void Compress(byte[] Data, int type)
        {
            if (this is NetworkAardwolf)
            {
                throw new Exception("Trying to compress data on Aardwolf connection!");
            }

            zStream_Length = 0;

            zStream.avail_in      = Data.Length;
            zStream.next_in       = Data;
            zStream.next_in_index = 0;

            zStream.next_out       = zStream_Out;
            zStream.next_out_index = 0;
            zStream.avail_out      = zStream_Out.Length;

            switch (zStream.deflate(type))
            {
            case zlibConst.Z_OK:
                //inIndex = zStream.next_in_index;
                zStream_Length    = (int)zStream.total_out;
                zStream.total_out = 0;
                zStream.next_in   = null;
                break;

            case zlibConst.Z_STREAM_END:
                //inIndex = zStream.next_in_index;
                zStream_Length = (int)zStream.total_out;
                zStream.deflateEnd();
                zStream.free();
                zStream = null;
                break;

            default:
                //case zlibConst.Z_STREAM_ERROR:
                throw new Exception("Error while compressing: " + (zStream.msg ?? "unknown error") + "!");
            }
        }
Пример #17
0
    public override int Read(byte[] b, int off, int len)
    {
        if (len == 0)
        {
            return(0);
        }
        z.next_out       = b;
        z.next_out_index = off;
        z.avail_out      = len;
        int num;

        do
        {
            if (z.avail_in == 0 && !nomoreinput)
            {
                z.next_in_index = 0;
                z.avail_in      = input.Read(buf, 0, buf.Length);
                if (z.avail_in <= 0)
                {
                    z.avail_in  = 0;
                    nomoreinput = true;
                }
            }
            num = (compress ? z.deflate(flushLevel) : z.inflate(flushLevel));
            if (nomoreinput && num == -5)
            {
                return(0);
            }
            if (num != 0 && num != 1)
            {
                throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
            }
            if ((nomoreinput || num == 1) && z.avail_out == len)
            {
                return(0);
            }
        }while (z.avail_out == len && num == 0);
        return(len - z.avail_out);
    }
Пример #18
0
        public void Construct(ZStream dStream)
        {
            Log.Write(LogLevel.Client, "Constructing Packet [{0}]", this.PacketID.ToString());

            // Compress buffer
            byte[] cData = this.Stream.ToArray();

            MemoryStream lStream = new MemoryStream();

            dStream.avail_in      = cData.Length;
            dStream.next_in       = cData;
            dStream.next_in_index = 0;

            byte[] dData = new byte[cData.Length * 2];

            dStream.avail_out      = cData.Length * 2;
            dStream.next_out       = dData;
            dStream.next_out_index = 0;

            dStream.deflate(2);

            lStream.Write(dData, 0, (cData.Length * 2) - dStream.avail_out);
            cData = lStream.ToArray();
            lStream.Dispose();

            //Log.Write(LogLevel.Error, "SHIEEEEEEEEEE\n{0}", cData.ToHEX(cData.Length));


            MemoryStream       oStream = new MemoryStream();
            EndianBinaryWriter oWriter = new EndianBinaryWriter(MiscUtil.Conversion.EndianBitConverter.Little, oStream);

            oWriter.Write((byte)this.Module);
            oWriter.Write((UInt32)cData.Length + 2);
            oWriter.Write((byte)GenerateChecksum(oStream.ToArray()));
            oWriter.Write(cData, 0, cData.Length - 4);

            this.Data = oStream.ToArray();
        }
Пример #19
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);
        }
Пример #20
0
        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));
        }
Пример #21
0
        internal int deflateParams(ZStream strm, int _level, int _strategy)
        {
            int err=Z_OK;

            if(_level == Z_DEFAULT_COMPRESSION){
                _level = 6;
            }
            if(_level < 0 || _level > 9 ||
                _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
                return Z_STREAM_ERROR;
            }

            if(config_table[level].func!=config_table[_level].func &&
                strm.total_in != 0) {
                // Flush the last buffer:
                err = strm.deflate(Z_PARTIAL_FLUSH);
            }

            if(level != _level) {
                level = _level;
                max_lazy_match   = config_table[level].max_lazy;
                good_match       = config_table[level].good_length;
                nice_match       = config_table[level].nice_length;
                max_chain_length = config_table[level].max_chain;
            }
            strategy = _strategy;
            return err;
        }
Пример #22
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");
            }
        }
Пример #23
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!");
                        }
                    }
            }
Пример #24
0
 protected override int PerformZlibOperation(ZStream zs, int flush)
 {
     return(zs.deflate(flush));
 }
Пример #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];
            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));
        }
Пример #26
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);
        }
Пример #27
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();
            }
        }
Пример #28
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");
            }
        }