deflate() public method

public deflate ( int flush ) : int
flush int
return int
コード例 #1
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 (zlibConst.Z_OK != err)
                {
                    if (zlibConst.Z_STREAM_END == err)
                    {
                        out_Renamed.Write(buf, 0, bufsize - z.avail_out);
                        return;
                    }
                    else
                    {
                        throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
                    }
                }
                else
                {
                    out_Renamed.Write(buf, 0, bufsize - z.avail_out);
                }

                /*if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
                 *      throw new ZStreamException((compress?"de":"in") + "flating: " + z.msg);*/
            }while (z.avail_in > 0 || z.avail_out == 0);
        }
コード例 #2
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);
        }
コード例 #3
0
 public override void Write(byte[] b1, int off, int len)
 {
     if (len == 0)
     {
         return;
     }
     byte[] array = new byte[b1.Length];
     Array.Copy(b1, 0, array, 0, b1.Length);
     z.next_in       = array;
     z.next_in_index = off;
     z.avail_in      = len;
     do
     {
         z.next_out       = buf;
         z.next_out_index = 0;
         z.avail_out      = bufsize;
         int num = ((!compress) ? z.inflate(flush_Renamed_Field) : z.deflate(flush_Renamed_Field));
         if (num != 0 && num != 1)
         {
             throw new ZStreamException(((!compress) ? "in" : "de") + "flating: " + z.msg);
         }
         out_Renamed.Write(buf, 0, bufsize - z.avail_out);
     }while (z.avail_in > 0 || z.avail_out == 0);
 }
コード例 #4
0
        public 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      = SupportClass.ReadInput(in_Renamed, buf, 0, bufsize);
                    if (z.avail_in == -1)
                    {
                        z.avail_in  = 0;
                        nomoreinput = true;
                    }
                }
                num = ((!compress) ? z.inflate(flush) : z.deflate(flush));
                if (nomoreinput && num == -5)
                {
                    return(-1);
                }
                if (num != 0 && num != 1)
                {
                    throw new ZStreamException(((!compress) ? "in" : "de") + "flating: " + z.msg);
                }
                if (nomoreinput && z.avail_out == len)
                {
                    return(-1);
                }
            }while (z.avail_out == len && num == 0);
            return(len - z.avail_out);
        }
コード例 #5
0
ファイル: Deflate.cs プロジェクト: radinamatic/subtitleedit
		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;
		}
コード例 #6
0
ファイル: ZlibFilter.cs プロジェクト: Ashod/Phalanger
 protected override int PerformZlibOperation(ZStream zs, int flush)
 {
     return zs.deflate(flush);
 }
コード例 #7
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]);
     }
 }
コード例 #8
0
ファイル: PhpZlib.cs プロジェクト: proff/Phalanger
        public static PhpBytes GzEncode(PhpBytes data, int level, int encoding_mode)
        {
            if ((level < -1) || (level > 9))
            {
                PhpException.Throw(PhpError.Warning, String.Format("compression level ({0}) must be within -1..9", level));
                return null;
            }

            ZStream zs = new ZStream();
            int status = zlibConst.Z_OK;

            zs.next_in = data.ReadonlyData;
            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 null;
			        }
                    break;
                case (int)ForceConstants.FORCE_DEFLATE:
                    if ((status = zs.deflateInit(level)) != zlibConst.Z_OK)
                    {
                        PhpException.Throw(PhpError.Warning, zError(status));
                        return null;
                    }
                    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 PHP.Library.CRC32();
                    byte[] crc = crc_algo.ComputeHash(data.ReadonlyData, 0, data.Length);
                    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 PhpBytes(output);
            }
            else
            {
                PhpException.Throw(PhpError.Warning, zError(status));
                return null;
            }
        }
コード例 #9
0
ファイル: PhpZlib.cs プロジェクト: proff/Phalanger
        public static PhpBytes GzDeflate(PhpBytes data, int level)
        {
            if ((level < -1) || (level > 9))
            {
                PhpException.Throw(PhpError.Warning, String.Format("compression level ({0}) must be within -1..9", level));
                return null;
            }

            ZStream zs = new ZStream();

            zs.next_in = data.ReadonlyData;
            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 PhpBytes(result);
            }
            else
            {
                PhpException.Throw(PhpError.Warning, zError(status));
                return null;
            }
        }
コード例 #10
0
ファイル: PhpZlib.cs プロジェクト: proff/Phalanger
        /// <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>
        private 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();
        }