Encoder and Decoder for ZLIB (IETF RFC1950 and RFC1951).
This class compresses and decompresses data according to the Deflate algorithm documented in RFC1950 and RFC1951.
Exemplo n.º 1
0
        internal int Initialize(ZlibCodec codec, int w)
        {
            _codec = codec;
            _codec.Message = null;
            blocks = null;

            // handle undocumented nowrap option (no zlib header or check)
            //nowrap = 0;
            //if (w < 0)
            //{
            //    w = - w;
            //    nowrap = 1;
            //}

            // set window size
            if (w < 8 || w > 15)
            {
                End();
                throw new ZlibException("Bad window size.");

                //return ZlibConstants.Z_STREAM_ERROR;
            }
            wbits = w;

            blocks = new InflateBlocks(codec,
                                       HandleRfc1950HeaderBytes ? this : null,
                                       1 << w);

            // reset state
            Reset();
            return ZlibConstants.Z_OK;
        }
Exemplo n.º 2
0
        public ZlibStream(Stream innerStream)
        {
            _inner = innerStream;

            if (_inner.CanRead)
            {
                _in = new ZlibCodec(CompressionMode.Decompress);
                //int ret = _in.InitializeInflate();
                //if (ret != ZlibConstants.Z_OK)
                //    throw new ZlibException("Unable to initialize Inflate");
                _inBuff = new byte[_bufferSize];
                _in.AvailableBytesIn = 0;
                _in.InputBuffer = _inBuff;
                _in.NextIn = 0;
            }

            if (_inner.CanWrite)
            {
                _out = new ZlibCodec(CompressionMode.Compress);
                int ret = _out.InitializeDeflate(CompressionLevel.Default);
                if (ret != ZlibConstants.Z_OK)
                    throw new ZlibException("Unable to initialize Deflate");
                _outBuff = new byte[_bufferSize];
                _out.OutputBuffer = _outBuff;
            }
        }
Exemplo n.º 3
0
        internal void InflateTreesDynamic(int nl, int nd, int[] c, ref int bl, ref int bd, 
		                                  ref int tl, ref int td, int[] hp, ZlibCodec z)
        {
            // build literal/length tree
            ResetWorkArea( 288 );
            hn = 0;
            int result = BuildTree(c, 0, nl, 257, cplens, cplext, ref tl, ref bl, hp);
            if (result != RCode.Okay || bl == 0) {
                string message = null;
                if (result == RCode.DataError) {
                    message = "oversubscribed literal/length tree";
                } else {
                    message = "incomplete literal/length tree";
                }
                throw new InvalidOperationException( "Unable to inflate dynamic tree: " + message );
            }

            // build distance tree
            ResetWorkArea( 288 );
            result = BuildTree(c, nl, nd, 0, cpdist, cpdext, ref td, ref bd, hp);

            if (result != RCode.Okay || (bd == 0 && nl > 257)) {
                string message = null;
                if (result == RCode.DataError) {
                    message = "oversubscribed distance tree";
                } else if (result == RCode.BufferError) {
                    message = "incomplete distance tree";
                } else {
                    message = "empty distance tree with lengths";
                }
                throw new InvalidOperationException( "Unable to inflate dynamic tree: " + message );
            }
        }
Exemplo n.º 4
0
 public DeflateStream( Stream stream, bool leaveOpen )
 {
     _stream = stream;
     _leaveOpen = leaveOpen;
     workBuffer = new byte[16384]; // TODO: 1024 bytes?
     z = new ZlibCodec();
 }
Exemplo n.º 5
0
        internal void Initialize( ZlibCodec codec, int w )
        {
            _codec = codec;
            blocks = null;

            wbits = w;
            blocks = new InflateBlocks( codec, 1 << w );
            Reset();
        }
Exemplo n.º 6
0
        internal void InflateTreeBits(int[] c, ref int bb, ref int tb, int[] hp, ZlibCodec z)
        {
            ResetWorkArea( 19 );
            hn = 0;
            int result = BuildTree(c, 0, 19, 19, null, null, ref tb, ref bb, hp);

            if (result == RCode.DataError) {
                throw new InvalidOperationException( "oversubscribed dynamic bit lengths tree" );
            } else if (result == RCode.BufferError || bb == 0) {
                throw new InvalidOperationException( "incomplete dynamic bit lengths tree" );
            }
        }
            public WorkItem(int size, Ionic.Zlib.CompressionLevel compressLevel, CompressionStrategy strategy)
            {
                buffer= new byte[size];
                // alloc 5 bytes overhead for every block (margin of safety= 2)
                int n = size + ((size / 32768)+1) * 5 * 2;
                compressed= new byte[n];

                status = (int)Status.None;
                compressor = new ZlibCodec();
                compressor.InitializeDeflate(compressLevel, false);
                compressor.OutputBuffer = compressed;
                compressor.InputBuffer = buffer;
            }
Exemplo n.º 8
0
 /// <summary>
 /// Decompress the specified inputData.
 /// </summary>
 /// <param name="inputData">Input data.</param>
 /// <returns>decompressed byte array</returns>
 public static byte[] Decompress(byte[] inputData)
 {
     var zlib = new ZlibCodec(Ionic.Zlib.CompressionMode.Decompress);
     zlib.InputBuffer = inputData;
     zlib.OutputBuffer = new byte[MAX_PACKET_SIZE];
     zlib.NextIn = 0;
     zlib.AvailableBytesIn = inputData.Length;
     zlib.NextOut = 0;
     zlib.AvailableBytesOut = MAX_PACKET_SIZE;
     zlib.Inflate(FlushType.Finish);
     var output = new byte[zlib.TotalBytesOut];
     Array.Copy(zlib.OutputBuffer, output, (int)zlib.TotalBytesOut);
     return output;
 }
 public WorkItem(int size,
                 CompressionLevel compressLevel,
                 CompressionStrategy strategy,
                 int ix)
 {
     this.buffer= new byte[size];
     // alloc 5 bytes overhead for every block (margin of safety= 2)
     int n = size + ((size / 32768)+1) * 5 * 2;
     this.compressed = new byte[n];
     this.compressor = new ZlibCodec();
     this.compressor.InitializeDeflate(compressLevel, false);
     this.compressor.OutputBuffer = this.compressed;
     this.compressor.InputBuffer = this.buffer;
     this.index = ix;
 }
Exemplo n.º 10
0
        public override void Flush()
        {
            CheckDisposed();

            var ms = (MemoryStream)_outputWriter.BaseStream;

            var buffer = ms.ToArraySegment();

            try
            {
                if (_compression)
                {
                    if (_deflate == null)
                    {
                        _deflate = new Ionic.Zlib.ZlibCodec(Ionic.Zlib.CompressionMode.Compress);
                    }
                    if (_compressionBuffer == null)
                    {
                        _compressionBuffer = new byte[1024 * 1024];
                    }

                    _deflate.OutputBuffer      = _compressionBuffer;
                    _deflate.AvailableBytesOut = _compressionBuffer.Length;
                    _deflate.NextOut           = 0;
                    _deflate.InputBuffer       = buffer.Array;
                    _deflate.AvailableBytesIn  = buffer.Count;
                    _deflate.NextIn            = 0;
                    var rc = _deflate.Deflate(Ionic.Zlib.FlushType.Sync);
                    if (rc != Ionic.Zlib.ZlibConstants.Z_OK)
                    {
                        throw new IOException($"Error '{rc}' while compressing the data.");
                    }
                    if (_deflate.AvailableBytesIn != 0)
                    {
                        throw new IOException("Compression buffer too small.");
                    }
                    buffer = new ArraySegment <byte>(_compressionBuffer, 0, _deflate.NextOut);
                }
                _innerStream.Write(buffer.Array, buffer.Offset, buffer.Count);
                _innerStream.Flush();
                _inputReader.BaseStream.Flush();
            }
            finally
            {
                _outputWriter.BaseStream.SetLength(0);
            }
        }
Exemplo n.º 11
0
        private static byte[] ZlibCodecCompress(string textToCompress)
        {
            int outputSize = 2048;
            byte[] output = new Byte[outputSize];
            byte[] uncompressed = UTF8Encoding.UTF8.GetBytes(textToCompress);
            int lengthToCompress = uncompressed.Length;

            // If you want a ZLIB stream, set this to true.  If you want
            // a bare DEFLATE stream, set this to false.
            bool wantRfc1950Header = false;

            using (MemoryStream ms = new MemoryStream())
            {
                ZlibCodec compressor = new ZlibCodec();
                compressor.InitializeDeflate(CompressionLevel.BestCompression, wantRfc1950Header);

                compressor.InputBuffer = uncompressed;
                compressor.AvailableBytesIn = lengthToCompress;
                compressor.NextIn = 0;
                compressor.OutputBuffer = output;

                foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish })
                {
                    int bytesToWrite = 0;
                    do
                    {
                        compressor.AvailableBytesOut = outputSize;
                        compressor.NextOut = 0;
                        compressor.Deflate(f);

                        bytesToWrite = outputSize - compressor.AvailableBytesOut;
                        if (bytesToWrite > 0)
                            ms.Write(output, 0, bytesToWrite);
                    }
                    while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
                           (f == FlushType.Finish && bytesToWrite != 0));
                }

                compressor.EndDeflate();

                ms.Flush();
                return ms.ToArray();
            }
        }
Exemplo n.º 12
0
        public static byte[] ZLibDecompress(byte[] compressed, bool mode, int outputSize)
        {
            byte[] output = new Byte[outputSize];

            bool expectRfc1950Header = mode;

            using (MemoryStream ms = new MemoryStream())
            {
                ZlibCodec compressor = new ZlibCodec();
                compressor.InitializeInflate(expectRfc1950Header);

                compressor.InputBuffer = compressed;
                compressor.AvailableBytesIn = compressed.Length;
                compressor.NextIn = 0;
                compressor.OutputBuffer = output;

                foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish })
                {
                    int bytesToWrite = 0;
                    do
                    {
                        compressor.AvailableBytesOut = outputSize;
                        compressor.NextOut = 0;
                        compressor.Inflate(f);

                        bytesToWrite = outputSize - compressor.AvailableBytesOut;
                        if (bytesToWrite > 0)
                            ms.Write(output, 0, bytesToWrite);
                    }
                    while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
                           (f == FlushType.Finish && bytesToWrite != 0));
                }

                compressor.EndInflate();

                return (ms.ToArray());
            }
        }
Exemplo n.º 13
0
        public Packet Inflate(int inflatedSize, bool keepStream = true)
        {
            var arr = ReadToEnd();
            var newarr = new byte[inflatedSize];

            if (ClientVersion.RemovedInVersion(ClientVersionBuild.V4_3_0_15005))
                keepStream = false;

            if (keepStream)
            {
                if (!SessionHandler.z_streams.ContainsKey(ConnectionIndex))
                    SessionHandler.z_streams[ConnectionIndex] = new ZlibCodec(CompressionMode.Decompress);
                SessionHandler.z_streams[ConnectionIndex].InputBuffer = arr;
                SessionHandler.z_streams[ConnectionIndex].NextIn = 0;
                SessionHandler.z_streams[ConnectionIndex].AvailableBytesIn = arr.Length;
                SessionHandler.z_streams[ConnectionIndex].OutputBuffer = newarr;
                SessionHandler.z_streams[ConnectionIndex].NextOut = 0;
                SessionHandler.z_streams[ConnectionIndex].AvailableBytesOut = inflatedSize;
                SessionHandler.z_streams[ConnectionIndex].Inflate(FlushType.Sync);
            }
            else
            {
                /*try
                {
                    var inflater = new Inflater(true);
                    inflater.SetInput(arr, 0, arr.Length);
                    inflater.Inflate(newarr, 0, inflatedSize);
                }
                catch (ICSharpCode.SharpZipLib.SharpZipBaseException)
                {
                    var inflater = new Inflater(true);
                    inflater.SetInput(arr, 0, arr.Length);
                    inflater.Inflate(newarr, 0, inflatedSize);
                }*/
                ZlibCodec stream = new ZlibCodec(CompressionMode.Decompress);
                stream.InputBuffer = arr;
                stream.NextIn = 0;
                stream.AvailableBytesIn = arr.Length;
                stream.OutputBuffer = newarr;
                stream.NextOut = 0;
                stream.AvailableBytesOut = inflatedSize;
                stream.Inflate(FlushType.None);
                stream.Inflate(FlushType.Finish);
                stream.EndInflate();
            }

            // Cannot use "using" here
            var pkt = new Packet(newarr, Opcode, Time, Direction, Number, Writer, FileName);
            pkt.ConnectionIndex = ConnectionIndex;
            return pkt;
        }
Exemplo n.º 14
0
		internal int Initialize(ZlibCodec codec, CompressionLevel level, int bits, CompressionStrategy compressionStrategy)
		{
			return Initialize(codec, level, bits, MEM_LEVEL_DEFAULT, compressionStrategy);
		}
 internal InflateBlocks(ZlibCodec z, System.Object checkfn, int w)
 {
     hufts = new int[MANY * 3];
     window = new byte[w];
     end = w;
     this.checkfn = checkfn;
     mode = TYPE;
     Reset(z, null);
 }
 internal void Init(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, ZlibCodec z)
 {
     mode = START;
     lbits = (byte)bl;
     dbits = (byte)bd;
     ltree = tl;
     ltree_index = tl_index;
     dtree = td;
     dtree_index = td_index;
     tree = null;
 }
Exemplo n.º 17
0
        internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
        {
            int num12;
            int nextIn           = z.NextIn;
            int availableBytesIn = z.AvailableBytesIn;
            int bitb             = s.bitb;
            int bitk             = s.bitk;
            int write            = s.write;
            int num9             = (write < s.read) ? ((s.read - write) - 1) : (s.end - write);
            int num10            = inflate_mask[bl];
            int num11            = inflate_mask[bd];

Label_0096:
            while (bitk < 20)
            {
                availableBytesIn--;
                bitb |= (z.InputBuffer[nextIn++] & 0xff) << bitk;
                bitk += 8;
            }
            int num = bitb & num10;

            int[] numArray = tl;
            int   num2     = tl_index;
            int   index    = (num2 + num) * 3;
            int   num3     = numArray[index];

            if (num3 == 0)
            {
                bitb  = bitb >> numArray[index + 1];
                bitk -= numArray[index + 1];
                s.window[write++] = (byte)numArray[index + 2];
                num9--;
            }
            else
            {
                while (true)
                {
                    bool flag;
                    bitb  = bitb >> numArray[index + 1];
                    bitk -= numArray[index + 1];
                    if ((num3 & 0x10) != 0)
                    {
                        num3 &= 15;
                        num12 = numArray[index + 2] + (bitb & inflate_mask[num3]);
                        bitb  = bitb >> num3;
                        bitk -= num3;
                        while (bitk < 15)
                        {
                            availableBytesIn--;
                            bitb |= (z.InputBuffer[nextIn++] & 0xff) << bitk;
                            bitk += 8;
                        }
                        num      = bitb & num11;
                        numArray = td;
                        num2     = td_index;
                        index    = (num2 + num) * 3;
                        num3     = numArray[index];
                        while (true)
                        {
                            bitb  = bitb >> numArray[index + 1];
                            bitk -= numArray[index + 1];
                            if ((num3 & 0x10) != 0)
                            {
                                int num14;
                                num3 &= 15;
                                while (bitk < num3)
                                {
                                    availableBytesIn--;
                                    bitb |= (z.InputBuffer[nextIn++] & 0xff) << bitk;
                                    bitk += 8;
                                }
                                int num13 = numArray[index + 2] + (bitb & inflate_mask[num3]);
                                bitb  = bitb >> num3;
                                bitk -= num3;
                                num9 -= num12;
                                if (write >= num13)
                                {
                                    num14 = write - num13;
                                    if (((write - num14) > 0) && (2 > (write - num14)))
                                    {
                                        s.window[write++] = s.window[num14++];
                                        s.window[write++] = s.window[num14++];
                                        num12            -= 2;
                                    }
                                    else
                                    {
                                        Array.Copy(s.window, num14, s.window, write, 2);
                                        write += 2;
                                        num14 += 2;
                                        num12 -= 2;
                                    }
                                }
                                else
                                {
                                    num14 = write - num13;
                                    do
                                    {
                                        num14 += s.end;
                                    }while (num14 < 0);
                                    num3 = s.end - num14;
                                    if (num12 > num3)
                                    {
                                        num12 -= num3;
                                        if (((write - num14) > 0) && (num3 > (write - num14)))
                                        {
                                            do
                                            {
                                                s.window[write++] = s.window[num14++];
                                            }while (--num3 != 0);
                                        }
                                        else
                                        {
                                            Array.Copy(s.window, num14, s.window, write, num3);
                                            write += num3;
                                            num14 += num3;
                                            num3   = 0;
                                        }
                                        num14 = 0;
                                    }
                                }
                                if (((write - num14) > 0) && (num12 > (write - num14)))
                                {
                                    do
                                    {
                                        s.window[write++] = s.window[num14++];
                                    }while (--num12 != 0);
                                }
                                else
                                {
                                    Array.Copy(s.window, num14, s.window, write, num12);
                                    write += num12;
                                    num14 += num12;
                                    num12  = 0;
                                }
                                goto Label_06B8;
                            }
                            if ((num3 & 0x40) == 0)
                            {
                                num  += numArray[index + 2];
                                num  += bitb & inflate_mask[num3];
                                index = (num2 + num) * 3;
                                num3  = numArray[index];
                            }
                            else
                            {
                                z.Message          = "invalid distance code";
                                num12              = z.AvailableBytesIn - availableBytesIn;
                                num12              = ((bitk >> 3) < num12) ? (bitk >> 3) : num12;
                                availableBytesIn  += num12;
                                nextIn            -= num12;
                                bitk              -= num12 << 3;
                                s.bitb             = bitb;
                                s.bitk             = bitk;
                                z.AvailableBytesIn = availableBytesIn;
                                z.TotalBytesIn    += nextIn - z.NextIn;
                                z.NextIn           = nextIn;
                                s.write            = write;
                                return(-3);
                            }
                            flag = true;
                        }
                    }
                    if ((num3 & 0x40) == 0)
                    {
                        num  += numArray[index + 2];
                        num  += bitb & inflate_mask[num3];
                        index = (num2 + num) * 3;
                        num3  = numArray[index];
                        if (num3 == 0)
                        {
                            bitb  = bitb >> numArray[index + 1];
                            bitk -= numArray[index + 1];
                            s.window[write++] = (byte)numArray[index + 2];
                            num9--;
                            break;
                        }
                    }
                    else
                    {
                        if ((num3 & 0x20) != 0)
                        {
                            num12              = z.AvailableBytesIn - availableBytesIn;
                            num12              = ((bitk >> 3) < num12) ? (bitk >> 3) : num12;
                            availableBytesIn  += num12;
                            nextIn            -= num12;
                            bitk              -= num12 << 3;
                            s.bitb             = bitb;
                            s.bitk             = bitk;
                            z.AvailableBytesIn = availableBytesIn;
                            z.TotalBytesIn    += nextIn - z.NextIn;
                            z.NextIn           = nextIn;
                            s.write            = write;
                            return(1);
                        }
                        z.Message          = "invalid literal/length code";
                        num12              = z.AvailableBytesIn - availableBytesIn;
                        num12              = ((bitk >> 3) < num12) ? (bitk >> 3) : num12;
                        availableBytesIn  += num12;
                        nextIn            -= num12;
                        bitk              -= num12 << 3;
                        s.bitb             = bitb;
                        s.bitk             = bitk;
                        z.AvailableBytesIn = availableBytesIn;
                        z.TotalBytesIn    += nextIn - z.NextIn;
                        z.NextIn           = nextIn;
                        s.write            = write;
                        return(-3);
                    }
                    flag = true;
                }
            }
Label_06B8:
            if ((num9 >= 0x102) && (availableBytesIn >= 10))
            {
                goto Label_0096;
            }
            num12              = z.AvailableBytesIn - availableBytesIn;
            num12              = ((bitk >> 3) < num12) ? (bitk >> 3) : num12;
            availableBytesIn  += num12;
            nextIn            -= num12;
            bitk              -= num12 << 3;
            s.bitb             = bitb;
            s.bitk             = bitk;
            z.AvailableBytesIn = availableBytesIn;
            z.TotalBytesIn    += nextIn - z.NextIn;
            z.NextIn           = nextIn;
            s.write            = write;
            return(0);
        }
Exemplo n.º 18
0
        // Token: 0x0600079C RID: 1948 RVA: 0x0004379C File Offset: 0x0004199C
        internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
        {
            int num  = z.NextIn;
            int num2 = z.AvailableBytesIn;
            int num3 = s.bitb;
            int i    = s.bitk;
            int num4 = s.writeAt;
            int num5 = (num4 < s.readAt) ? (s.readAt - num4 - 1) : (s.end - num4);
            int num6 = InternalInflateConstants.InflateMask[bl];
            int num7 = InternalInflateConstants.InflateMask[bd];
            int num10;
            int num11;

            for (;;)
            {
                if (i >= 20)
                {
                    int num8 = num3 & num6;
                    int num9 = (tl_index + num8) * 3;
                    if ((num10 = tl[num9]) == 0)
                    {
                        num3           >>= tl[num9 + 1];
                        i               -= tl[num9 + 1];
                        s.window[num4++] = (byte)tl[num9 + 2];
                        num5--;
                    }
                    else
                    {
                        for (;;)
                        {
                            num3 >>= tl[num9 + 1];
                            i     -= tl[num9 + 1];
                            if ((num10 & 16) != 0)
                            {
                                break;
                            }
                            if ((num10 & 64) != 0)
                            {
                                goto IL_531;
                            }
                            num8 += tl[num9 + 2];
                            num8 += (num3 & InternalInflateConstants.InflateMask[num10]);
                            num9  = (tl_index + num8) * 3;
                            if ((num10 = tl[num9]) == 0)
                            {
                                goto IL_3F1;
                            }
                        }
                        num10 &= 15;
                        num11  = tl[num9 + 2] + (num3 & InternalInflateConstants.InflateMask[num10]);
                        num3 >>= num10;
                        for (i -= num10; i < 15; i += 8)
                        {
                            num2--;
                            num3 |= (int)(z.InputBuffer[num++] & byte.MaxValue) << i;
                        }
                        num8  = (num3 & num7);
                        num9  = (td_index + num8) * 3;
                        num10 = td[num9];
                        for (;;)
                        {
                            num3 >>= td[num9 + 1];
                            i     -= td[num9 + 1];
                            if ((num10 & 16) != 0)
                            {
                                break;
                            }
                            if ((num10 & 64) != 0)
                            {
                                goto IL_446;
                            }
                            num8 += td[num9 + 2];
                            num8 += (num3 & InternalInflateConstants.InflateMask[num10]);
                            num9  = (td_index + num8) * 3;
                            num10 = td[num9];
                        }
                        num10 &= 15;
                        while (i < num10)
                        {
                            num2--;
                            num3 |= (int)(z.InputBuffer[num++] & byte.MaxValue) << i;
                            i    += 8;
                        }
                        int num12 = td[num9 + 2] + (num3 & InternalInflateConstants.InflateMask[num10]);
                        num3 >>= num10;
                        i     -= num10;
                        num5  -= num11;
                        int num13;
                        if (num4 >= num12)
                        {
                            num13 = num4 - num12;
                            if (num4 - num13 > 0 && 2 > num4 - num13)
                            {
                                s.window[num4++] = s.window[num13++];
                                s.window[num4++] = s.window[num13++];
                                num11           -= 2;
                            }
                            else
                            {
                                Array.Copy(s.window, num13, s.window, num4, 2);
                                num4  += 2;
                                num13 += 2;
                                num11 -= 2;
                            }
                        }
                        else
                        {
                            num13 = num4 - num12;
                            do
                            {
                                num13 += s.end;
                            }while (num13 < 0);
                            num10 = s.end - num13;
                            if (num11 > num10)
                            {
                                num11 -= num10;
                                if (num4 - num13 > 0 && num10 > num4 - num13)
                                {
                                    do
                                    {
                                        s.window[num4++] = s.window[num13++];
                                    }while (--num10 != 0);
                                }
                                else
                                {
                                    Array.Copy(s.window, num13, s.window, num4, num10);
                                    num4  += num10;
                                    num13 += num10;
                                }
                                num13 = 0;
                            }
                        }
                        if (num4 - num13 > 0 && num11 > num4 - num13)
                        {
                            do
                            {
                                s.window[num4++] = s.window[num13++];
                            }while (--num11 != 0);
                            goto IL_425;
                        }
                        Array.Copy(s.window, num13, s.window, num4, num11);
                        num4  += num11;
                        num13 += num11;
                        goto IL_425;
IL_3F1:
                        num3           >>= tl[num9 + 1];
                        i               -= tl[num9 + 1];
                        s.window[num4++] = (byte)tl[num9 + 2];
                        num5--;
                    }
IL_425:
                    if (num5 < 258 || num2 < 10)
                    {
                        goto IL_4C2;
                    }
                }
                else
                {
                    num2--;
                    num3 |= (int)(z.InputBuffer[num++] & byte.MaxValue) << i;
                    i    += 8;
                }
            }
IL_446:
            z.Message          = "invalid distance code";
            num11              = z.AvailableBytesIn - num2;
            num11              = ((i >> 3 < num11) ? (i >> 3) : num11);
            num2              += num11;
            num               -= num11;
            i                 -= num11 << 3;
            s.bitb             = num3;
            s.bitk             = i;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num - z.NextIn);
            z.NextIn           = num;
            s.writeAt          = num4;
            return(-3);

IL_4C2:
            num11              = z.AvailableBytesIn - num2;
            num11              = ((i >> 3 < num11) ? (i >> 3) : num11);
            num2              += num11;
            num               -= num11;
            i                 -= num11 << 3;
            s.bitb             = num3;
            s.bitk             = i;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num - z.NextIn);
            z.NextIn           = num;
            s.writeAt          = num4;
            return(0);

IL_531:
            if ((num10 & 32) != 0)
            {
                num11              = z.AvailableBytesIn - num2;
                num11              = ((i >> 3 < num11) ? (i >> 3) : num11);
                num2              += num11;
                num               -= num11;
                i                 -= num11 << 3;
                s.bitb             = num3;
                s.bitk             = i;
                z.AvailableBytesIn = num2;
                z.TotalBytesIn    += (long)(num - z.NextIn);
                z.NextIn           = num;
                s.writeAt          = num4;
                return(1);
            }
            z.Message          = "invalid literal/length code";
            num11              = z.AvailableBytesIn - num2;
            num11              = ((i >> 3 < num11) ? (i >> 3) : num11);
            num2              += num11;
            num               -= num11;
            i                 -= num11 << 3;
            s.bitb             = num3;
            s.bitk             = i;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num - z.NextIn);
            z.NextIn           = num;
            s.writeAt          = num4;
            return(-3);
        }
Exemplo n.º 19
0
        internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
        {
            int nextIn = z.NextIn;
            int num    = z.AvailableBytesIn;
            int num2   = s.bitb;
            int num3   = s.bitk;
            int num4   = s.writeAt;
            int num5   = (num4 < s.readAt) ? (s.readAt - num4 - 1) : (s.end - num4);
            int num6   = InternalInflateConstants.InflateMask[bl];
            int num7   = InternalInflateConstants.InflateMask[bd];
            int num12;

            while (true)
            {
                if (num3 < 20)
                {
                    num--;
                    num2 |= (z.InputBuffer[nextIn++] & 0xFF) << num3;
                    num3 += 8;
                    continue;
                }
                int   num8  = num2 & num6;
                int[] array = tl;
                int   num9  = tl_index;
                int   num10 = (num9 + num8) * 3;
                int   num11;
                if ((num11 = array[num10]) == 0)
                {
                    num2           >>= array[num10 + 1];
                    num3            -= array[num10 + 1];
                    s.window[num4++] = (byte)array[num10 + 2];
                    num5--;
                }
                else
                {
                    while (true)
                    {
                        num2 >>= array[num10 + 1];
                        num3  -= array[num10 + 1];
                        if ((num11 & 0x10) != 0)
                        {
                            num11 &= 0xF;
                            num12  = array[num10 + 2] + (num2 & InternalInflateConstants.InflateMask[num11]);
                            num2 >>= num11;
                            for (num3 -= num11; num3 < 15; num3 += 8)
                            {
                                num--;
                                num2 |= (z.InputBuffer[nextIn++] & 0xFF) << num3;
                            }
                            num8  = (num2 & num7);
                            array = td;
                            num9  = td_index;
                            num10 = (num9 + num8) * 3;
                            num11 = array[num10];
                            while (true)
                            {
                                num2 >>= array[num10 + 1];
                                num3  -= array[num10 + 1];
                                if ((num11 & 0x10) != 0)
                                {
                                    break;
                                }
                                if ((num11 & 0x40) == 0)
                                {
                                    num8 += array[num10 + 2];
                                    num8 += (num2 & InternalInflateConstants.InflateMask[num11]);
                                    num10 = (num9 + num8) * 3;
                                    num11 = array[num10];
                                    continue;
                                }
                                z.Message          = "invalid distance code";
                                num12              = z.AvailableBytesIn - num;
                                num12              = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
                                num               += num12;
                                nextIn            -= num12;
                                num3              -= num12 << 3;
                                s.bitb             = num2;
                                s.bitk             = num3;
                                z.AvailableBytesIn = num;
                                z.TotalBytesIn    += nextIn - z.NextIn;
                                z.NextIn           = nextIn;
                                s.writeAt          = num4;
                                return(-3);
                            }
                            for (num11 &= 0xF; num3 < num11; num3 += 8)
                            {
                                num--;
                                num2 |= (z.InputBuffer[nextIn++] & 0xFF) << num3;
                            }
                            int num13 = array[num10 + 2] + (num2 & InternalInflateConstants.InflateMask[num11]);
                            num2 >>= num11;
                            num3  -= num11;
                            num5  -= num12;
                            int num14;
                            if (num4 >= num13)
                            {
                                num14 = num4 - num13;
                                if (num4 - num14 > 0 && 2 > num4 - num14)
                                {
                                    s.window[num4++] = s.window[num14++];
                                    s.window[num4++] = s.window[num14++];
                                    num12           -= 2;
                                }
                                else
                                {
                                    Array.Copy(s.window, num14, s.window, num4, 2);
                                    num4  += 2;
                                    num14 += 2;
                                    num12 -= 2;
                                }
                            }
                            else
                            {
                                num14 = num4 - num13;
                                do
                                {
                                    num14 += s.end;
                                }while (num14 < 0);
                                num11 = s.end - num14;
                                if (num12 > num11)
                                {
                                    num12 -= num11;
                                    if (num4 - num14 > 0 && num11 > num4 - num14)
                                    {
                                        do
                                        {
                                            s.window[num4++] = s.window[num14++];
                                        }while (--num11 != 0);
                                    }
                                    else
                                    {
                                        Array.Copy(s.window, num14, s.window, num4, num11);
                                        num4  += num11;
                                        num14 += num11;
                                        num11  = 0;
                                    }
                                    num14 = 0;
                                }
                            }
                            if (num4 - num14 > 0 && num12 > num4 - num14)
                            {
                                do
                                {
                                    s.window[num4++] = s.window[num14++];
                                }while (--num12 != 0);
                            }
                            else
                            {
                                Array.Copy(s.window, num14, s.window, num4, num12);
                                num4  += num12;
                                num14 += num12;
                                num12  = 0;
                            }
                            break;
                        }
                        if ((num11 & 0x40) == 0)
                        {
                            num8 += array[num10 + 2];
                            num8 += (num2 & InternalInflateConstants.InflateMask[num11]);
                            num10 = (num9 + num8) * 3;
                            if ((num11 = array[num10]) == 0)
                            {
                                num2           >>= array[num10 + 1];
                                num3            -= array[num10 + 1];
                                s.window[num4++] = (byte)array[num10 + 2];
                                num5--;
                                break;
                            }
                            continue;
                        }
                        if ((num11 & 0x20) != 0)
                        {
                            num12              = z.AvailableBytesIn - num;
                            num12              = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
                            num               += num12;
                            nextIn            -= num12;
                            num3              -= num12 << 3;
                            s.bitb             = num2;
                            s.bitk             = num3;
                            z.AvailableBytesIn = num;
                            z.TotalBytesIn    += nextIn - z.NextIn;
                            z.NextIn           = nextIn;
                            s.writeAt          = num4;
                            return(1);
                        }
                        z.Message          = "invalid literal/length code";
                        num12              = z.AvailableBytesIn - num;
                        num12              = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
                        num               += num12;
                        nextIn            -= num12;
                        num3              -= num12 << 3;
                        s.bitb             = num2;
                        s.bitk             = num3;
                        z.AvailableBytesIn = num;
                        z.TotalBytesIn    += nextIn - z.NextIn;
                        z.NextIn           = nextIn;
                        s.writeAt          = num4;
                        return(-3);
                    }
                }
                if (num5 < 258 || num < 10)
                {
                    break;
                }
            }
            num12              = z.AvailableBytesIn - num;
            num12              = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
            num               += num12;
            nextIn            -= num12;
            num3              -= num12 << 3;
            s.bitb             = num2;
            s.bitk             = num3;
            z.AvailableBytesIn = num;
            z.TotalBytesIn    += nextIn - z.NextIn;
            z.NextIn           = nextIn;
            s.writeAt          = num4;
            return(0);
        }
Exemplo n.º 20
0
 internal int Initialize(ZlibCodec codec, CompressionLevel level, int bits, CompressionStrategy compressionStrategy)
 {
     return(Initialize(codec, level, bits, MEM_LEVEL_DEFAULT, compressionStrategy));
 }
Exemplo n.º 21
0
 // Token: 0x060006C9 RID: 1737 RVA: 0x000320B8 File Offset: 0x000302B8
 internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
 {
     bl[0] = 9;
     bd[0] = 5;
     tl[0] = InfTree.fixed_tl;
     td[0] = InfTree.fixed_td;
     return(0);
 }
Exemplo n.º 22
0
        // Token: 0x060006C8 RID: 1736 RVA: 0x00031F8C File Offset: 0x0003018C
        internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
        {
            this.initWorkArea(288);
            this.hn[0] = 0;
            int num = this.huft_build(c, 0, nl, 257, InfTree.cplens, InfTree.cplext, tl, bl, hp, this.hn, this.v);

            if (num != 0 || bl[0] == 0)
            {
                if (num == -3)
                {
                    z.Message = "oversubscribed literal/length tree";
                }
                else if (num != -4)
                {
                    z.Message = "incomplete literal/length tree";
                    num       = -3;
                }
                return(num);
            }
            this.initWorkArea(288);
            num = this.huft_build(c, nl, nd, 0, InfTree.cpdist, InfTree.cpdext, td, bd, hp, this.hn, this.v);
            if (num != 0 || (bd[0] == 0 && nl > 257))
            {
                if (num == -3)
                {
                    z.Message = "oversubscribed distance tree";
                }
                else if (num == -5)
                {
                    z.Message = "incomplete distance tree";
                    num       = -3;
                }
                else if (num != -4)
                {
                    z.Message = "empty distance tree with lengths";
                    num       = -3;
                }
                return(num);
            }
            return(0);
        }
Exemplo n.º 23
0
 internal int SyncPoint(ZlibCodec z)
 {
     return(this.blocks.SyncPoint());
 }
Exemplo n.º 24
0
        internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
        {
            this.initWorkArea(288);
            this.hn[0] = 0;
            int num = this.huft_build(c, 0, nl, 257, InfTree.cplens, InfTree.cplext, tl, bl, hp, this.hn, this.v);

            if (num == 0 && bl[0] != 0)
            {
                this.initWorkArea(288);
                num = this.huft_build(c, nl, nd, 0, InfTree.cpdist, InfTree.cpdext, td, bd, hp, this.hn, this.v);
                if (num == 0 && (bd[0] != 0 || nl <= 257))
                {
                    return(0);
                }
                switch (num)
                {
                case -3:
                    z.Message = "oversubscribed distance tree";
                    break;

                case -5:
                    z.Message = "incomplete distance tree";
                    num       = -3;
                    break;

                default:
                    z.Message = "empty distance tree with lengths";
                    num       = -3;
                    break;

                case -4:
                    break;
                }
                return(num);
            }
            switch (num)
            {
            case -3:
                z.Message = "oversubscribed literal/length tree";
                break;

            default:
                z.Message = "incomplete literal/length tree";
                num       = -3;
                break;

            case -4:
                break;
            }
            return(num);
        }
Exemplo n.º 25
0
        private void _PerpetualWriterMethod(object state)
        {
            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod START");

            try
            {
                do
                {
                    // wait for the next session
                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(begin) PWM");
                    _sessionReset.WaitOne();
                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(done)  PWM");

                    if (_isDisposed)
                    {
                        break;
                    }

                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.Reset()        PWM");
                    _sessionReset.Reset();

                    // repeatedly write buffers as they become ready
                    WorkItem         workitem = null;
                    Ionic.Zlib.CRC32 c        = new Ionic.Zlib.CRC32();
                    do
                    {
                        workitem = _pool[_nextToWrite % _pc];
                        lock (workitem)
                        {
                            if (_noMoreInputForThisSegment)
                            {
                                TraceOutput(TraceBits.Write,
                                            "Write    drain    wi({0}) stat({1}) canuse({2})  cba({3})",
                                            workitem.index,
                                            workitem.status,
                                            (workitem.status == (int)WorkItem.Status.Compressed),
                                            workitem.compressedBytesAvailable);
                            }

                            do
                            {
                                if (workitem.status == (int)WorkItem.Status.Compressed)
                                {
                                    TraceOutput(TraceBits.WriteBegin,
                                                "Write    begin    wi({0}) stat({1})              cba({2})",
                                                workitem.index,
                                                workitem.status,
                                                workitem.compressedBytesAvailable);

                                    workitem.status = (int)WorkItem.Status.Writing;
                                    _outStream.Write(workitem.compressed, 0, workitem.compressedBytesAvailable);
                                    c.Combine(workitem.crc, workitem.inputBytesAvailable);
                                    _totalBytesProcessed += workitem.inputBytesAvailable;
                                    _nextToWrite++;
                                    workitem.inputBytesAvailable = 0;
                                    workitem.status = (int)WorkItem.Status.Done;

                                    TraceOutput(TraceBits.WriteDone,
                                                "Write    done     wi({0}) stat({1})              cba({2})",
                                                workitem.index,
                                                workitem.status,
                                                workitem.compressedBytesAvailable);


                                    Monitor.Pulse(workitem);
                                    break;
                                }
                                else
                                {
                                    int wcycles = 0;
                                    // I've locked a workitem I cannot use.
                                    // Therefore, wake someone else up, and then release the lock.
                                    while (workitem.status != (int)WorkItem.Status.Compressed)
                                    {
                                        TraceOutput(TraceBits.WriteWait,
                                                    "Write    waiting  wi({0}) stat({1}) nw({2}) nf({3}) nomore({4})",
                                                    workitem.index,
                                                    workitem.status,
                                                    _nextToWrite, _nextToFill,
                                                    _noMoreInputForThisSegment);

                                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                                        {
                                            break;
                                        }

                                        wcycles++;

                                        // wake up someone else
                                        Monitor.Pulse(workitem);
                                        // release and wait
                                        Monitor.Wait(workitem);

                                        if (workitem.status == (int)WorkItem.Status.Compressed)
                                        {
                                            TraceOutput(TraceBits.WriteWait,
                                                        "Write    A-OK     wi({0}) stat({1}) iba({2}) cba({3}) cyc({4})",
                                                        workitem.index,
                                                        workitem.status,
                                                        workitem.inputBytesAvailable,
                                                        workitem.compressedBytesAvailable,
                                                        wcycles);
                                        }
                                    }

                                    if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                                    {
                                        break;
                                    }
                                }
                            }while (true);
                        }

                        if (_noMoreInputForThisSegment)
                        {
                            TraceOutput(TraceBits.Write,
                                        "Write    nomore  nw({0}) nf({1}) break({2})",
                                        _nextToWrite, _nextToFill, (_nextToWrite == _nextToFill));
                        }

                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                        {
                            break;
                        }
                    } while (true);


                    // Finish:
                    // After writing a series of buffers, closing each one with
                    // Flush.Sync, we now write the final one as Flush.Finish, and
                    // then stop.
                    byte[]    buffer     = new byte[128];
                    ZlibCodec compressor = new ZlibCodec();
                    int       rc         = compressor.InitializeDeflate(_compressLevel, false);
                    compressor.InputBuffer       = null;
                    compressor.NextIn            = 0;
                    compressor.AvailableBytesIn  = 0;
                    compressor.OutputBuffer      = buffer;
                    compressor.NextOut           = 0;
                    compressor.AvailableBytesOut = buffer.Length;
                    rc = compressor.Deflate(FlushType.Finish);

                    if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
                    {
                        throw new Exception("deflating: " + compressor.Message);
                    }

                    if (buffer.Length - compressor.AvailableBytesOut > 0)
                    {
                        TraceOutput(TraceBits.WriteBegin,
                                    "Write    begin    flush bytes({0})",
                                    buffer.Length - compressor.AvailableBytesOut);

                        _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);

                        TraceOutput(TraceBits.WriteBegin,
                                    "Write    done     flush");
                    }

                    compressor.EndDeflate();

                    _Crc32 = c.Crc32Result;

                    // signal that writing is complete:
                    TraceOutput(TraceBits.Synch, "Synch    _writingDone.Set()           PWM");
                    _writingDone.Set();
                }while (true);
            }
            catch (System.Exception exc1)
            {
                lock (_eLock)
                {
                    // expose the exception to the main thread
                    if (_pendingException != null)
                    {
                        _pendingException = exc1;
                    }
                }
            }

            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod FINIS");
        }
 public void StartCompression()
 {
     _compressionBuffer = new byte[PreferredBufferSize];
     _compressor        = new Ionic.Zlib.ZlibCodec(Ionic.Zlib.CompressionMode.Compress);
     _decompressor      = new Ionic.Zlib.ZlibCodec(Ionic.Zlib.CompressionMode.Decompress);
 }
Exemplo n.º 27
0
        // Token: 0x0600079B RID: 1947 RVA: 0x00042D40 File Offset: 0x00040F40
        internal int Process(InflateBlocks blocks, int r)
        {
            ZlibCodec codec = blocks._codec;
            int       num   = codec.NextIn;
            int       num2  = codec.AvailableBytesIn;
            int       num3  = blocks.bitb;
            int       i     = blocks.bitk;
            int       num4  = blocks.writeAt;
            int       num5  = (num4 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4);

            for (;;)
            {
                int num6;
                switch (this.mode)
                {
                case 0:
                    if (num5 >= 258 && num2 >= 10)
                    {
                        blocks.bitb            = num3;
                        blocks.bitk            = i;
                        codec.AvailableBytesIn = num2;
                        codec.TotalBytesIn    += (long)(num - codec.NextIn);
                        codec.NextIn           = num;
                        blocks.writeAt         = num4;
                        r    = this.InflateFast((int)this.lbits, (int)this.dbits, this.ltree, this.ltree_index, this.dtree, this.dtree_index, blocks, codec);
                        num  = codec.NextIn;
                        num2 = codec.AvailableBytesIn;
                        num3 = blocks.bitb;
                        i    = blocks.bitk;
                        num4 = blocks.writeAt;
                        num5 = ((num4 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4));
                        if (r != 0)
                        {
                            this.mode = ((r == 1) ? 7 : 9);
                            continue;
                        }
                    }
                    this.need       = (int)this.lbits;
                    this.tree       = this.ltree;
                    this.tree_index = this.ltree_index;
                    this.mode       = 1;
                    goto IL_441;

                case 1:
                    goto IL_441;

                case 2:
                    num6 = this.bitsToGet;
                    while (i < num6)
                    {
                        if (num2 == 0)
                        {
                            goto IL_773;
                        }
                        r = 0;
                        num2--;
                        num3 |= (int)(codec.InputBuffer[num++] & byte.MaxValue) << i;
                        i    += 8;
                    }
                    this.len       += (num3 & InternalInflateConstants.InflateMask[num6]);
                    num3          >>= num6;
                    i              -= num6;
                    this.need       = (int)this.dbits;
                    this.tree       = this.dtree;
                    this.tree_index = this.dtree_index;
                    this.mode       = 3;
                    goto IL_2D8;

                case 3:
                    goto IL_2D8;

                case 4:
                    num6 = this.bitsToGet;
                    while (i < num6)
                    {
                        if (num2 == 0)
                        {
                            goto IL_854;
                        }
                        r = 0;
                        num2--;
                        num3 |= (int)(codec.InputBuffer[num++] & byte.MaxValue) << i;
                        i    += 8;
                    }
                    this.dist += (num3 & InternalInflateConstants.InflateMask[num6]);
                    num3     >>= num6;
                    i         -= num6;
                    this.mode  = 5;
                    goto IL_13C;

                case 5:
                    goto IL_13C;

                case 6:
                    if (num5 == 0)
                    {
                        if (num4 == blocks.end && blocks.readAt != 0)
                        {
                            num4 = 0;
                            num5 = ((0 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4));
                        }
                        if (num5 == 0)
                        {
                            blocks.writeAt = num4;
                            r    = blocks.Flush(r);
                            num4 = blocks.writeAt;
                            num5 = ((num4 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4));
                            if (num4 == blocks.end && blocks.readAt != 0)
                            {
                                num4 = 0;
                                num5 = ((0 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4));
                            }
                            if (num5 == 0)
                            {
                                goto IL_8DA;
                            }
                        }
                    }
                    r = 0;
                    blocks.window[num4++] = (byte)this.lit;
                    num5--;
                    this.mode = 0;
                    continue;

                case 7:
                    goto IL_91D;

                case 8:
                    goto IL_9C2;

                case 9:
                    goto IL_A08;
                }
                break;
IL_13C:
                int j;
                for (j = num4 - this.dist; j < 0; j += blocks.end)
                {
                }
                while (this.len != 0)
                {
                    if (num5 == 0)
                    {
                        if (num4 == blocks.end && blocks.readAt != 0)
                        {
                            num4 = 0;
                            num5 = ((0 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4));
                        }
                        if (num5 == 0)
                        {
                            blocks.writeAt = num4;
                            r    = blocks.Flush(r);
                            num4 = blocks.writeAt;
                            num5 = ((num4 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4));
                            if (num4 == blocks.end && blocks.readAt != 0)
                            {
                                num4 = 0;
                                num5 = ((0 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4));
                            }
                            if (num5 == 0)
                            {
                                goto IL_897;
                            }
                        }
                    }
                    blocks.window[num4++] = blocks.window[j++];
                    num5--;
                    if (j == blocks.end)
                    {
                        j = 0;
                    }
                    this.len--;
                }
                this.mode = 0;
                continue;
IL_2D8:
                num6 = this.need;
                while (i < num6)
                {
                    if (num2 == 0)
                    {
                        goto IL_811;
                    }
                    r = 0;
                    num2--;
                    num3 |= (int)(codec.InputBuffer[num++] & byte.MaxValue) << i;
                    i    += 8;
                }
                int num7 = (this.tree_index + (num3 & InternalInflateConstants.InflateMask[num6])) * 3;
                num3 >>= this.tree[num7 + 1];
                i     -= this.tree[num7 + 1];
                int num8 = this.tree[num7];
                if ((num8 & 16) != 0)
                {
                    this.bitsToGet = (num8 & 15);
                    this.dist      = this.tree[num7 + 2];
                    this.mode      = 4;
                    continue;
                }
                if ((num8 & 64) == 0)
                {
                    this.need       = num8;
                    this.tree_index = num7 / 3 + this.tree[num7 + 2];
                    continue;
                }
                goto IL_7B6;
IL_441:
                num6 = this.need;
                while (i < num6)
                {
                    if (num2 == 0)
                    {
                        goto IL_730;
                    }
                    r = 0;
                    num2--;
                    num3 |= (int)(codec.InputBuffer[num++] & byte.MaxValue) << i;
                    i    += 8;
                }
                num7   = (this.tree_index + (num3 & InternalInflateConstants.InflateMask[num6])) * 3;
                num3 >>= this.tree[num7 + 1];
                i     -= this.tree[num7 + 1];
                num8   = this.tree[num7];
                if (num8 == 0)
                {
                    this.lit  = this.tree[num7 + 2];
                    this.mode = 6;
                }
                else if ((num8 & 16) != 0)
                {
                    this.bitsToGet = (num8 & 15);
                    this.len       = this.tree[num7 + 2];
                    this.mode      = 2;
                }
                else if ((num8 & 64) == 0)
                {
                    this.need       = num8;
                    this.tree_index = num7 / 3 + this.tree[num7 + 2];
                }
                else
                {
                    if ((num8 & 32) == 0)
                    {
                        goto IL_6D5;
                    }
                    this.mode = 7;
                }
            }
            r                      = -2;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(-2));

IL_6D5:
            this.mode              = 9;
            codec.Message          = "invalid literal/length code";
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(-3));

IL_730:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_773:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_7B6:
            this.mode              = 9;
            codec.Message          = "invalid distance code";
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(-3));

IL_811:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_854:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_897:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_8DA:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_91D:
            if (i > 7)
            {
                i -= 8;
                num2++;
                num--;
            }
            blocks.writeAt = num4;
            r    = blocks.Flush(r);
            num4 = blocks.writeAt;
            int num9 = (num4 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4);

            if (blocks.readAt != blocks.writeAt)
            {
                blocks.bitb            = num3;
                blocks.bitk            = i;
                codec.AvailableBytesIn = num2;
                codec.TotalBytesIn    += (long)(num - codec.NextIn);
                codec.NextIn           = num;
                blocks.writeAt         = num4;
                return(blocks.Flush(r));
            }
            this.mode = 8;
IL_9C2:
            r                      = 1;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(1));

IL_A08:
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(-3));
        }
Exemplo n.º 28
0
        internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
        {
            int p5 = z.NextIn;
            int n3 = z.AvailableBytesIn;
            int b  = s.bitb;
            int k4 = s.bitk;
            int q  = s.writeAt;
            int m2 = (q < s.readAt) ? (s.readAt - q - 1) : (s.end - q);
            int ml = InternalInflateConstants.InflateMask[bl];
            int md = InternalInflateConstants.InflateMask[bd];
            int c10;

            while (true)
            {
                if (k4 < 20)
                {
                    n3--;
                    b  |= (z.InputBuffer[p5++] & 0xFF) << k4;
                    k4 += 8;
                    continue;
                }
                int   t4           = b & ml;
                int[] tp2          = tl;
                int   tp_index2    = tl_index;
                int   tp_index_t_4 = (tp_index2 + t4) * 3;
                int   e6;
                if ((e6 = tp2[tp_index_t_4]) == 0)
                {
                    b           >>= tp2[tp_index_t_4 + 1];
                    k4           -= tp2[tp_index_t_4 + 1];
                    s.window[q++] = (byte)tp2[tp_index_t_4 + 2];
                    m2--;
                }
                else
                {
                    while (true)
                    {
                        b >>= tp2[tp_index_t_4 + 1];
                        k4 -= tp2[tp_index_t_4 + 1];
                        if ((e6 & 0x10) != 0)
                        {
                            e6 &= 0xF;
                            c10 = tp2[tp_index_t_4 + 2] + (b & InternalInflateConstants.InflateMask[e6]);
                            b >>= e6;
                            for (k4 -= e6; k4 < 15; k4 += 8)
                            {
                                n3--;
                                b |= (z.InputBuffer[p5++] & 0xFF) << k4;
                            }
                            t4           = (b & md);
                            tp2          = td;
                            tp_index2    = td_index;
                            tp_index_t_4 = (tp_index2 + t4) * 3;
                            e6           = tp2[tp_index_t_4];
                            while (true)
                            {
                                b >>= tp2[tp_index_t_4 + 1];
                                k4 -= tp2[tp_index_t_4 + 1];
                                if ((e6 & 0x10) != 0)
                                {
                                    break;
                                }
                                if ((e6 & 0x40) == 0)
                                {
                                    t4          += tp2[tp_index_t_4 + 2];
                                    t4          += (b & InternalInflateConstants.InflateMask[e6]);
                                    tp_index_t_4 = (tp_index2 + t4) * 3;
                                    e6           = tp2[tp_index_t_4];
                                    continue;
                                }
                                z.Message          = "invalid distance code";
                                c10                = z.AvailableBytesIn - n3;
                                c10                = ((k4 >> 3 < c10) ? (k4 >> 3) : c10);
                                n3                += c10;
                                p5                -= c10;
                                k4                -= c10 << 3;
                                s.bitb             = b;
                                s.bitk             = k4;
                                z.AvailableBytesIn = n3;
                                z.TotalBytesIn    += p5 - z.NextIn;
                                z.NextIn           = p5;
                                s.writeAt          = q;
                                return(-3);
                            }
                            for (e6 &= 0xF; k4 < e6; k4 += 8)
                            {
                                n3--;
                                b |= (z.InputBuffer[p5++] & 0xFF) << k4;
                            }
                            int d = tp2[tp_index_t_4 + 2] + (b & InternalInflateConstants.InflateMask[e6]);
                            b >>= e6;
                            k4 -= e6;
                            m2 -= c10;
                            int r3;
                            if (q >= d)
                            {
                                r3 = q - d;
                                if (q - r3 > 0 && 2 > q - r3)
                                {
                                    s.window[q++] = s.window[r3++];
                                    s.window[q++] = s.window[r3++];
                                    c10          -= 2;
                                }
                                else
                                {
                                    Array.Copy(s.window, r3, s.window, q, 2);
                                    q   += 2;
                                    r3  += 2;
                                    c10 -= 2;
                                }
                            }
                            else
                            {
                                r3 = q - d;
                                do
                                {
                                    r3 += s.end;
                                }while (r3 < 0);
                                e6 = s.end - r3;
                                if (c10 > e6)
                                {
                                    c10 -= e6;
                                    if (q - r3 > 0 && e6 > q - r3)
                                    {
                                        do
                                        {
                                            s.window[q++] = s.window[r3++];
                                        }while (--e6 != 0);
                                    }
                                    else
                                    {
                                        Array.Copy(s.window, r3, s.window, q, e6);
                                        q  += e6;
                                        r3 += e6;
                                        e6  = 0;
                                    }
                                    r3 = 0;
                                }
                            }
                            if (q - r3 > 0 && c10 > q - r3)
                            {
                                do
                                {
                                    s.window[q++] = s.window[r3++];
                                }while (--c10 != 0);
                            }
                            else
                            {
                                Array.Copy(s.window, r3, s.window, q, c10);
                                q  += c10;
                                r3 += c10;
                                c10 = 0;
                            }
                            break;
                        }
                        if ((e6 & 0x40) == 0)
                        {
                            t4          += tp2[tp_index_t_4 + 2];
                            t4          += (b & InternalInflateConstants.InflateMask[e6]);
                            tp_index_t_4 = (tp_index2 + t4) * 3;
                            if ((e6 = tp2[tp_index_t_4]) == 0)
                            {
                                b           >>= tp2[tp_index_t_4 + 1];
                                k4           -= tp2[tp_index_t_4 + 1];
                                s.window[q++] = (byte)tp2[tp_index_t_4 + 2];
                                m2--;
                                break;
                            }
                            continue;
                        }
                        if ((e6 & 0x20) != 0)
                        {
                            c10                = z.AvailableBytesIn - n3;
                            c10                = ((k4 >> 3 < c10) ? (k4 >> 3) : c10);
                            n3                += c10;
                            p5                -= c10;
                            k4                -= c10 << 3;
                            s.bitb             = b;
                            s.bitk             = k4;
                            z.AvailableBytesIn = n3;
                            z.TotalBytesIn    += p5 - z.NextIn;
                            z.NextIn           = p5;
                            s.writeAt          = q;
                            return(1);
                        }
                        z.Message          = "invalid literal/length code";
                        c10                = z.AvailableBytesIn - n3;
                        c10                = ((k4 >> 3 < c10) ? (k4 >> 3) : c10);
                        n3                += c10;
                        p5                -= c10;
                        k4                -= c10 << 3;
                        s.bitb             = b;
                        s.bitk             = k4;
                        z.AvailableBytesIn = n3;
                        z.TotalBytesIn    += p5 - z.NextIn;
                        z.NextIn           = p5;
                        s.writeAt          = q;
                        return(-3);
                    }
                }
                if (m2 < 258 || n3 < 10)
                {
                    break;
                }
            }
            c10                = z.AvailableBytesIn - n3;
            c10                = ((k4 >> 3 < c10) ? (k4 >> 3) : c10);
            n3                += c10;
            p5                -= c10;
            k4                -= c10 << 3;
            s.bitb             = b;
            s.bitk             = k4;
            z.AvailableBytesIn = n3;
            z.TotalBytesIn    += p5 - z.NextIn;
            z.NextIn           = p5;
            s.writeAt          = q;
            return(0);
        }
 internal void Free(ZlibCodec z)
 {
     Reset(z, null);
     window = null;
     hufts = null;
     //ZFREE(z, s);
 }
Exemplo n.º 30
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       b2 = 0;
            int       k3 = 0;
            int       p2 = 0;
            ZlibCodec z  = blocks._codec;

            p2 = z.NextIn;
            int n2 = z.AvailableBytesIn;

            b2 = blocks.bitb;
            k3 = blocks.bitk;
            int q = blocks.writeAt;
            int n = (q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q);

            while (true)
            {
                switch (mode)
                {
                case 0:
                    if (n >= 258 && n2 >= 10)
                    {
                        blocks.bitb        = b2;
                        blocks.bitk        = k3;
                        z.AvailableBytesIn = n2;
                        z.TotalBytesIn    += p2 - z.NextIn;
                        z.NextIn           = p2;
                        blocks.writeAt     = q;
                        r  = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);
                        p2 = z.NextIn;
                        n2 = z.AvailableBytesIn;
                        b2 = blocks.bitb;
                        k3 = blocks.bitk;
                        q  = blocks.writeAt;
                        n  = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                        if (r != 0)
                        {
                            mode = ((r == 1) ? 7 : 9);
                            break;
                        }
                    }
                    need       = lbits;
                    tree       = ltree;
                    tree_index = ltree_index;
                    mode       = 1;
                    goto case 1;

                case 1:
                {
                    int k;
                    for (k = need; k3 < k; k3 += 8)
                    {
                        if (n2 != 0)
                        {
                            r = 0;
                            n2--;
                            b2 |= (z.InputBuffer[p2++] & 0xFF) << k3;
                            continue;
                        }
                        blocks.bitb        = b2;
                        blocks.bitk        = k3;
                        z.AvailableBytesIn = n2;
                        z.TotalBytesIn    += p2 - z.NextIn;
                        z.NextIn           = p2;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    int tindex2 = (tree_index + (b2 & InternalInflateConstants.InflateMask[k])) * 3;
                    b2 >>= tree[tindex2 + 1];
                    k3  -= tree[tindex2 + 1];
                    int e2 = tree[tindex2];
                    if (e2 == 0)
                    {
                        lit  = tree[tindex2 + 2];
                        mode = 6;
                        break;
                    }
                    if ((e2 & 0x10) != 0)
                    {
                        bitsToGet = (e2 & 0xF);
                        len       = tree[tindex2 + 2];
                        mode      = 2;
                        break;
                    }
                    if ((e2 & 0x40) == 0)
                    {
                        need       = e2;
                        tree_index = tindex2 / 3 + tree[tindex2 + 2];
                        break;
                    }
                    if ((e2 & 0x20) != 0)
                    {
                        mode = 7;
                        break;
                    }
                    mode               = 9;
                    z.Message          = "invalid literal/length code";
                    r                  = -3;
                    blocks.bitb        = b2;
                    blocks.bitk        = k3;
                    z.AvailableBytesIn = n2;
                    z.TotalBytesIn    += p2 - z.NextIn;
                    z.NextIn           = p2;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));
                }

                case 2:
                {
                    int k;
                    for (k = bitsToGet; k3 < k; k3 += 8)
                    {
                        if (n2 != 0)
                        {
                            r = 0;
                            n2--;
                            b2 |= (z.InputBuffer[p2++] & 0xFF) << k3;
                            continue;
                        }
                        blocks.bitb        = b2;
                        blocks.bitk        = k3;
                        z.AvailableBytesIn = n2;
                        z.TotalBytesIn    += p2 - z.NextIn;
                        z.NextIn           = p2;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    len       += (b2 & InternalInflateConstants.InflateMask[k]);
                    b2       >>= k;
                    k3        -= k;
                    need       = dbits;
                    tree       = dtree;
                    tree_index = dtree_index;
                    mode       = 3;
                    goto case 3;
                }

                case 3:
                {
                    int k;
                    for (k = need; k3 < k; k3 += 8)
                    {
                        if (n2 != 0)
                        {
                            r = 0;
                            n2--;
                            b2 |= (z.InputBuffer[p2++] & 0xFF) << k3;
                            continue;
                        }
                        blocks.bitb        = b2;
                        blocks.bitk        = k3;
                        z.AvailableBytesIn = n2;
                        z.TotalBytesIn    += p2 - z.NextIn;
                        z.NextIn           = p2;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    int tindex2 = (tree_index + (b2 & InternalInflateConstants.InflateMask[k])) * 3;
                    b2 >>= tree[tindex2 + 1];
                    k3  -= tree[tindex2 + 1];
                    int e2 = tree[tindex2];
                    if ((e2 & 0x10) != 0)
                    {
                        bitsToGet = (e2 & 0xF);
                        dist      = tree[tindex2 + 2];
                        mode      = 4;
                        break;
                    }
                    if ((e2 & 0x40) == 0)
                    {
                        need       = e2;
                        tree_index = tindex2 / 3 + tree[tindex2 + 2];
                        break;
                    }
                    mode               = 9;
                    z.Message          = "invalid distance code";
                    r                  = -3;
                    blocks.bitb        = b2;
                    blocks.bitk        = k3;
                    z.AvailableBytesIn = n2;
                    z.TotalBytesIn    += p2 - z.NextIn;
                    z.NextIn           = p2;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));
                }

                case 4:
                {
                    int k;
                    for (k = bitsToGet; k3 < k; k3 += 8)
                    {
                        if (n2 != 0)
                        {
                            r = 0;
                            n2--;
                            b2 |= (z.InputBuffer[p2++] & 0xFF) << k3;
                            continue;
                        }
                        blocks.bitb        = b2;
                        blocks.bitk        = k3;
                        z.AvailableBytesIn = n2;
                        z.TotalBytesIn    += p2 - z.NextIn;
                        z.NextIn           = p2;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    dist += (b2 & InternalInflateConstants.InflateMask[k]);
                    b2  >>= k;
                    k3   -= k;
                    mode  = 5;
                    goto case 5;
                }

                case 5:
                {
                    int f;
                    for (f = q - dist; f < 0; f += blocks.end)
                    {
                    }
                    while (len != 0)
                    {
                        if (n == 0)
                        {
                            if (q == blocks.end && blocks.readAt != 0)
                            {
                                q = 0;
                                n = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                            }
                            if (n == 0)
                            {
                                blocks.writeAt = q;
                                r = blocks.Flush(r);
                                q = blocks.writeAt;
                                n = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                                if (q == blocks.end && blocks.readAt != 0)
                                {
                                    q = 0;
                                    n = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                                }
                                if (n == 0)
                                {
                                    blocks.bitb        = b2;
                                    blocks.bitk        = k3;
                                    z.AvailableBytesIn = n2;
                                    z.TotalBytesIn    += p2 - z.NextIn;
                                    z.NextIn           = p2;
                                    blocks.writeAt     = q;
                                    return(blocks.Flush(r));
                                }
                            }
                        }
                        blocks.window[q++] = blocks.window[f++];
                        n--;
                        if (f == blocks.end)
                        {
                            f = 0;
                        }
                        len--;
                    }
                    mode = 0;
                    break;
                }

                case 6:
                    if (n == 0)
                    {
                        if (q == blocks.end && blocks.readAt != 0)
                        {
                            q = 0;
                            n = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                        }
                        if (n == 0)
                        {
                            blocks.writeAt = q;
                            r = blocks.Flush(r);
                            q = blocks.writeAt;
                            n = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                            if (q == blocks.end && blocks.readAt != 0)
                            {
                                q = 0;
                                n = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                            }
                            if (n == 0)
                            {
                                blocks.bitb        = b2;
                                blocks.bitk        = k3;
                                z.AvailableBytesIn = n2;
                                z.TotalBytesIn    += p2 - z.NextIn;
                                z.NextIn           = p2;
                                blocks.writeAt     = q;
                                return(blocks.Flush(r));
                            }
                        }
                    }
                    r = 0;
                    blocks.window[q++] = (byte)lit;
                    n--;
                    mode = 0;
                    break;

                case 7:
                    if (k3 > 7)
                    {
                        k3 -= 8;
                        n2++;
                        p2--;
                    }
                    blocks.writeAt = q;
                    r = blocks.Flush(r);
                    q = blocks.writeAt;
                    n = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                    if (blocks.readAt != blocks.writeAt)
                    {
                        blocks.bitb        = b2;
                        blocks.bitk        = k3;
                        z.AvailableBytesIn = n2;
                        z.TotalBytesIn    += p2 - z.NextIn;
                        z.NextIn           = p2;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    mode = 8;
                    goto case 8;

                case 8:
                    r                  = 1;
                    blocks.bitb        = b2;
                    blocks.bitk        = k3;
                    z.AvailableBytesIn = n2;
                    z.TotalBytesIn    += p2 - z.NextIn;
                    z.NextIn           = p2;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));

                case 9:
                    r                  = -3;
                    blocks.bitb        = b2;
                    blocks.bitk        = k3;
                    z.AvailableBytesIn = n2;
                    z.TotalBytesIn    += p2 - z.NextIn;
                    z.NextIn           = p2;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));

                default:
                    r                  = -2;
                    blocks.bitb        = b2;
                    blocks.bitk        = k3;
                    z.AvailableBytesIn = n2;
                    z.TotalBytesIn    += p2 - z.NextIn;
                    z.NextIn           = p2;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));
                }
            }
        }
Exemplo n.º 31
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       num;
            int       num10;
            bool      flag;
            int       number = 0;
            int       bitk   = 0;
            int       nextIn = 0;
            ZlibCodec z      = blocks._codec;

            nextIn = z.NextIn;
            int availableBytesIn = z.AvailableBytesIn;

            number = blocks.bitb;
            bitk   = blocks.bitk;
            int write = blocks.write;
            int num9  = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);

            goto Label_0C3A;
Label_0196:
            this.need       = this.lbits;
            this.tree       = this.ltree;
            this.tree_index = this.ltree_index;
            this.mode       = 1;
Label_01C3:
            num = this.need;
            while (bitk < num)
            {
                if (availableBytesIn != 0)
                {
                    r = 0;
                }
                else
                {
                    blocks.bitb        = number;
                    blocks.bitk        = bitk;
                    z.AvailableBytesIn = availableBytesIn;
                    z.TotalBytesIn    += nextIn - z.NextIn;
                    z.NextIn           = nextIn;
                    blocks.write       = write;
                    return(blocks.Flush(r));
                }
                availableBytesIn--;
                number |= (z.InputBuffer[nextIn++] & 0xff) << bitk;
                bitk   += 8;
            }
            int index = (this.tree_index + (number & inflate_mask[num])) * 3;

            number = SharedUtils.URShift(number, this.tree[index + 1]);
            bitk  -= this.tree[index + 1];
            int num3 = this.tree[index];

            if (num3 == 0)
            {
                this.lit  = this.tree[index + 2];
                this.mode = 6;
                goto Label_0C3A;
            }
            if ((num3 & 0x10) != 0)
            {
                this.get_Renamed = num3 & 15;
                this.len         = this.tree[index + 2];
                this.mode        = 2;
                goto Label_0C3A;
            }
            if ((num3 & 0x40) == 0)
            {
                this.need       = num3;
                this.tree_index = (index / 3) + this.tree[index + 2];
                goto Label_0C3A;
            }
            if ((num3 & 0x20) != 0)
            {
                this.mode = 7;
                goto Label_0C3A;
            }
            this.mode          = 9;
            z.Message          = "invalid literal/length code";
            r                  = -3;
            blocks.bitb        = number;
            blocks.bitk        = bitk;
            z.AvailableBytesIn = availableBytesIn;
            z.TotalBytesIn    += nextIn - z.NextIn;
            z.NextIn           = nextIn;
            blocks.write       = write;
            return(blocks.Flush(r));

Label_04AE:
            num = this.need;
            while (bitk < num)
            {
                if (availableBytesIn != 0)
                {
                    r = 0;
                }
                else
                {
                    blocks.bitb        = number;
                    blocks.bitk        = bitk;
                    z.AvailableBytesIn = availableBytesIn;
                    z.TotalBytesIn    += nextIn - z.NextIn;
                    z.NextIn           = nextIn;
                    blocks.write       = write;
                    return(blocks.Flush(r));
                }
                availableBytesIn--;
                number |= (z.InputBuffer[nextIn++] & 0xff) << bitk;
                bitk   += 8;
            }
            index  = (this.tree_index + (number & inflate_mask[num])) * 3;
            number = number >> this.tree[index + 1];
            bitk  -= this.tree[index + 1];
            num3   = this.tree[index];
            if ((num3 & 0x10) != 0)
            {
                this.get_Renamed = num3 & 15;
                this.dist        = this.tree[index + 2];
                this.mode        = 4;
                goto Label_0C3A;
            }
            if ((num3 & 0x40) == 0)
            {
                this.need       = num3;
                this.tree_index = (index / 3) + this.tree[index + 2];
                goto Label_0C3A;
            }
            this.mode          = 9;
            z.Message          = "invalid distance code";
            r                  = -3;
            blocks.bitb        = number;
            blocks.bitk        = bitk;
            z.AvailableBytesIn = availableBytesIn;
            z.TotalBytesIn    += nextIn - z.NextIn;
            z.NextIn           = nextIn;
            blocks.write       = write;
            return(blocks.Flush(r));

Label_0730:
            num10 = write - this.dist;
            while (num10 < 0)
            {
                num10 += blocks.end;
            }
            while (this.len != 0)
            {
                if (num9 == 0)
                {
                    if ((write == blocks.end) && (blocks.read != 0))
                    {
                        write = 0;
                        num9  = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                    }
                    if (num9 == 0)
                    {
                        blocks.write = write;
                        r            = blocks.Flush(r);
                        write        = blocks.write;
                        num9         = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                        if ((write == blocks.end) && (blocks.read != 0))
                        {
                            write = 0;
                            num9  = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                        }
                        if (num9 == 0)
                        {
                            blocks.bitb        = number;
                            blocks.bitk        = bitk;
                            z.AvailableBytesIn = availableBytesIn;
                            z.TotalBytesIn    += nextIn - z.NextIn;
                            z.NextIn           = nextIn;
                            blocks.write       = write;
                            return(blocks.Flush(r));
                        }
                    }
                }
                blocks.window[write++] = blocks.window[num10++];
                num9--;
                if (num10 == blocks.end)
                {
                    num10 = 0;
                }
                this.len--;
            }
            this.mode = 0;
            goto Label_0C3A;
Label_0B44:
            r                  = 1;
            blocks.bitb        = number;
            blocks.bitk        = bitk;
            z.AvailableBytesIn = availableBytesIn;
            z.TotalBytesIn    += nextIn - z.NextIn;
            z.NextIn           = nextIn;
            blocks.write       = write;
            return(blocks.Flush(r));

Label_0C3A:
            flag = true;
            switch (this.mode)
            {
            case 0:
                if ((num9 < 0x102) || (availableBytesIn < 10))
                {
                    goto Label_0196;
                }
                blocks.bitb        = number;
                blocks.bitk        = bitk;
                z.AvailableBytesIn = availableBytesIn;
                z.TotalBytesIn    += nextIn - z.NextIn;
                z.NextIn           = nextIn;
                blocks.write       = write;
                r                = this.InflateFast(this.lbits, this.dbits, this.ltree, this.ltree_index, this.dtree, this.dtree_index, blocks, z);
                nextIn           = z.NextIn;
                availableBytesIn = z.AvailableBytesIn;
                number           = blocks.bitb;
                bitk             = blocks.bitk;
                write            = blocks.write;
                num9             = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                if (r == 0)
                {
                    goto Label_0196;
                }
                this.mode = (r == 1) ? 7 : 9;
                goto Label_0C3A;

            case 1:
                goto Label_01C3;

            case 2:
                num = this.get_Renamed;
                while (bitk < num)
                {
                    if (availableBytesIn != 0)
                    {
                        r = 0;
                    }
                    else
                    {
                        blocks.bitb        = number;
                        blocks.bitk        = bitk;
                        z.AvailableBytesIn = availableBytesIn;
                        z.TotalBytesIn    += nextIn - z.NextIn;
                        z.NextIn           = nextIn;
                        blocks.write       = write;
                        return(blocks.Flush(r));
                    }
                    availableBytesIn--;
                    number |= (z.InputBuffer[nextIn++] & 0xff) << bitk;
                    bitk   += 8;
                }
                this.len       += number & inflate_mask[num];
                number          = number >> num;
                bitk           -= num;
                this.need       = this.dbits;
                this.tree       = this.dtree;
                this.tree_index = this.dtree_index;
                this.mode       = 3;
                goto Label_04AE;

            case 3:
                goto Label_04AE;

            case 4:
                num = this.get_Renamed;
                while (bitk < num)
                {
                    if (availableBytesIn != 0)
                    {
                        r = 0;
                    }
                    else
                    {
                        blocks.bitb        = number;
                        blocks.bitk        = bitk;
                        z.AvailableBytesIn = availableBytesIn;
                        z.TotalBytesIn    += nextIn - z.NextIn;
                        z.NextIn           = nextIn;
                        blocks.write       = write;
                        return(blocks.Flush(r));
                    }
                    availableBytesIn--;
                    number |= (z.InputBuffer[nextIn++] & 0xff) << bitk;
                    bitk   += 8;
                }
                this.dist += number & inflate_mask[num];
                number     = number >> num;
                bitk      -= num;
                this.mode  = 5;
                goto Label_0730;

            case 5:
                goto Label_0730;

            case 6:
                if (num9 == 0)
                {
                    if ((write == blocks.end) && (blocks.read != 0))
                    {
                        write = 0;
                        num9  = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                    }
                    if (num9 == 0)
                    {
                        blocks.write = write;
                        r            = blocks.Flush(r);
                        write        = blocks.write;
                        num9         = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                        if ((write == blocks.end) && (blocks.read != 0))
                        {
                            write = 0;
                            num9  = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                        }
                        if (num9 == 0)
                        {
                            blocks.bitb        = number;
                            blocks.bitk        = bitk;
                            z.AvailableBytesIn = availableBytesIn;
                            z.TotalBytesIn    += nextIn - z.NextIn;
                            z.NextIn           = nextIn;
                            blocks.write       = write;
                            return(blocks.Flush(r));
                        }
                    }
                }
                r = 0;
                blocks.window[write++] = (byte)this.lit;
                num9--;
                this.mode = 0;
                goto Label_0C3A;

            case 7:
                if (bitk > 7)
                {
                    bitk -= 8;
                    availableBytesIn++;
                    nextIn--;
                }
                blocks.write = write;
                r            = blocks.Flush(r);
                write        = blocks.write;
                num9         = (write < blocks.read) ? ((blocks.read - write) - 1) : (blocks.end - write);
                if (blocks.read != blocks.write)
                {
                    blocks.bitb        = number;
                    blocks.bitk        = bitk;
                    z.AvailableBytesIn = availableBytesIn;
                    z.TotalBytesIn    += nextIn - z.NextIn;
                    z.NextIn           = nextIn;
                    blocks.write       = write;
                    return(blocks.Flush(r));
                }
                this.mode = 8;
                goto Label_0B44;

            case 8:
                goto Label_0B44;

            case 9:
                r                  = -3;
                blocks.bitb        = number;
                blocks.bitk        = bitk;
                z.AvailableBytesIn = availableBytesIn;
                z.TotalBytesIn    += nextIn - z.NextIn;
                z.NextIn           = nextIn;
                blocks.write       = write;
                return(blocks.Flush(r));
            }
            r                  = -2;
            blocks.bitb        = number;
            blocks.bitk        = bitk;
            z.AvailableBytesIn = availableBytesIn;
            z.TotalBytesIn    += nextIn - z.NextIn;
            z.NextIn           = nextIn;
            blocks.write       = write;
            return(blocks.Flush(r));
        }
        internal int Inflate(ZlibCodec z, int f)
        {
            int r;
            int b;

            if (z == null)
                throw new ZlibException("Codec is null. ");
            if (z.istate == null)
                throw new ZlibException("InflateManager is null. ");
            if (z.InputBuffer == null)
                throw new ZlibException("InputBuffer is null. ");

            //return ZlibConstants.Z_STREAM_ERROR;

            f = (f == ZlibConstants.Z_FINISH)
                ? ZlibConstants.Z_BUF_ERROR
                : ZlibConstants.Z_OK;
            r = ZlibConstants.Z_BUF_ERROR;
            while (true)
            {
                switch (z.istate.mode)
                {
                    case METHOD:
                        if (z.AvailableBytesIn == 0)
                            return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;

                        if (((z.istate.method = z.InputBuffer[z.NextIn++]) & 0xf) != Z_DEFLATED)
                        {
                            z.istate.mode = BAD;
                            z.Message = String.Format("unknown compression method (0x{0:X2})", z.istate.method);
                            z.istate.marker = 5; // can't try inflateSync
                            break;
                        }
                        if ((z.istate.method >> 4) + 8 > z.istate.wbits)
                        {
                            z.istate.mode = BAD;
                            z.Message = String.Format("invalid window size ({0})", (z.istate.method >> 4) + 8);
                            z.istate.marker = 5; // can't try inflateSync
                            break;
                        }
                        z.istate.mode = FLAG;
                        goto case FLAG;

                    case FLAG:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        b = (z.InputBuffer[z.NextIn++]) & 0xff;

                        if ((((z.istate.method << 8) + b) % 31) != 0)
                        {
                            z.istate.mode = BAD;
                            z.Message = "incorrect header check";
                            z.istate.marker = 5; // can't try inflateSync
                            break;
                        }

                        if ((b & PRESET_DICT) == 0)
                        {
                            z.istate.mode = BLOCKS;
                            break;
                        }
                        z.istate.mode = DICT4;
                        goto case DICT4;

                    case DICT4:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need = ((z.InputBuffer[z.NextIn++] & 0xff) << 24) & unchecked((int)0xff000000L);
                        z.istate.mode = DICT3;
                        goto case DICT3;

                    case DICT3:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need += (((z.InputBuffer[z.NextIn++] & 0xff) << 16) & 0xff0000L);
                        z.istate.mode = DICT2;
                        goto case DICT2;

                    case DICT2:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need += (((z.InputBuffer[z.NextIn++] & 0xff) << 8) & 0xff00L);
                        z.istate.mode = DICT1;
                        goto case DICT1;

                    case DICT1:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need += (z.InputBuffer[z.NextIn++] & 0xffL);
                        z._Adler32 = z.istate.need;
                        z.istate.mode = DICT0;
                        return ZlibConstants.Z_NEED_DICT;

                    case DICT0:
                        z.istate.mode = BAD;
                        z.Message = "need dictionary";
                        z.istate.marker = 0; // can try inflateSync
                        return ZlibConstants.Z_STREAM_ERROR;

                    case BLOCKS:

                        r = z.istate.blocks.Process(z, r);
                        if (r == ZlibConstants.Z_DATA_ERROR)
                        {
                            z.istate.mode = BAD;
                            z.istate.marker = 0; // can try inflateSync
                            break;
                        }
                        if (r == ZlibConstants.Z_OK) r = f;

                        if (r != ZlibConstants.Z_STREAM_END) return r;

                        r = f;
                        z.istate.blocks.Reset(z, z.istate.was);
                        if (!z.istate.HandleRfc1950HeaderBytes)
                        {
                            z.istate.mode = DONE;
                            break;
                        }
                        z.istate.mode = CHECK4;
                        goto case CHECK4;

                    case CHECK4:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need = ((z.InputBuffer[z.NextIn++] & 0xff) << 24) & unchecked((int)0xff000000L);
                        z.istate.mode = CHECK3;
                        goto case CHECK3;

                    case CHECK3:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need += (((z.InputBuffer[z.NextIn++] & 0xff) << 16) & 0xff0000L);
                        z.istate.mode = CHECK2;
                        goto case CHECK2;

                    case CHECK2:

                        if (z.AvailableBytesIn == 0) return r;
                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need += (((z.InputBuffer[z.NextIn++] & 0xff) << 8) & 0xff00L);
                        z.istate.mode = CHECK1;
                        goto case CHECK1;

                    case CHECK1:

                        if (z.AvailableBytesIn == 0) return r;

                        r = f;

                        z.AvailableBytesIn--; z.TotalBytesIn++;
                        z.istate.need += (z.InputBuffer[z.NextIn++] & 0xffL);

                        if (((int)(z.istate.was[0])) != ((int)(z.istate.need)))
                        {
                            z.istate.mode = BAD;
                            z.Message = "incorrect data check";
                            z.istate.marker = 5; // can't try inflateSync
                            break;
                        }

                        z.istate.mode = DONE;
                        goto case DONE;

                    case DONE:
                        return ZlibConstants.Z_STREAM_END;

                    case BAD:
                        throw new ZlibException(String.Format("Bad state ({0})", z.Message));
                    //return ZlibConstants.Z_DATA_ERROR;

                    default:
                        throw new ZlibException("Stream error.");
                    //return ZlibConstants.Z_STREAM_ERROR;

                }
            }
        }
        internal int Sync(ZlibCodec z)
        {
            int n; // number of bytes to look at
            int p; // pointer to bytes
            int m; // number of marker bytes found in a row
            long r, w; // temporaries to save total_in and total_out

            // set up
            if (z == null || z.istate == null)
                return ZlibConstants.Z_STREAM_ERROR;
            if (z.istate.mode != BAD)
            {
                z.istate.mode = BAD;
                z.istate.marker = 0;
            }
            if ((n = z.AvailableBytesIn) == 0)
                return ZlibConstants.Z_BUF_ERROR;
            p = z.NextIn;
            m = z.istate.marker;

            // search
            while (n != 0 && m < 4)
            {
                if (z.InputBuffer[p] == mark[m])
                {
                    m++;
                }
                else if (z.InputBuffer[p] != 0)
                {
                    m = 0;
                }
                else
                {
                    m = 4 - m;
                }
                p++; n--;
            }

            // restore
            z.TotalBytesIn += p - z.NextIn;
            z.NextIn = p;
            z.AvailableBytesIn = n;
            z.istate.marker = m;

            // return no joy or set up to restart on a new block
            if (m != 4)
            {
                return ZlibConstants.Z_DATA_ERROR;
            }
            r = z.TotalBytesIn; w = z.TotalBytesOut;
            Reset(z);
            z.TotalBytesIn = r; z.TotalBytesOut = w;
            z.istate.mode = BLOCKS;
            return ZlibConstants.Z_OK;
        }
 internal int End(ZlibCodec z)
 {
     if (blocks != null)
         blocks.Free(z);
     blocks = null;
     //    ZFREE(z, z->state);
     return ZlibConstants.Z_OK;
 }
Exemplo n.º 35
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       num   = 0;
            int       num2  = 0;
            int       num3  = 0;
            ZlibCodec codec = blocks._codec;

            num3 = codec.NextIn;
            int num4 = codec.AvailableBytesIn;

            num  = blocks.bitb;
            num2 = blocks.bitk;
            int num5 = blocks.writeAt;
            int num6 = (num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5);

            while (true)
            {
                switch (mode)
                {
                case 0:
                    if (num6 >= 258 && num4 >= 10)
                    {
                        blocks.bitb            = num;
                        blocks.bitk            = num2;
                        codec.AvailableBytesIn = num4;
                        codec.TotalBytesIn    += num3 - codec.NextIn;
                        codec.NextIn           = num3;
                        blocks.writeAt         = num5;
                        r    = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, codec);
                        num3 = codec.NextIn;
                        num4 = codec.AvailableBytesIn;
                        num  = blocks.bitb;
                        num2 = blocks.bitk;
                        num5 = blocks.writeAt;
                        num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                        if (r != 0)
                        {
                            mode = ((r == 1) ? 7 : 9);
                            break;
                        }
                    }
                    need       = lbits;
                    tree       = ltree;
                    tree_index = ltree_index;
                    mode       = 1;
                    goto case 1;

                case 1:
                {
                    int num7;
                    for (num7 = need; num2 < num7; num2 += 8)
                    {
                        if (num4 != 0)
                        {
                            r = 0;
                            num4--;
                            num |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                            continue;
                        }
                        blocks.bitb            = num;
                        blocks.bitk            = num2;
                        codec.AvailableBytesIn = num4;
                        codec.TotalBytesIn    += num3 - codec.NextIn;
                        codec.NextIn           = num3;
                        blocks.writeAt         = num5;
                        return(blocks.Flush(r));
                    }
                    int num8 = (tree_index + (num & InternalInflateConstants.InflateMask[num7])) * 3;
                    num >>= tree[num8 + 1];
                    num2 -= tree[num8 + 1];
                    int num9 = tree[num8];
                    if (num9 == 0)
                    {
                        lit  = tree[num8 + 2];
                        mode = 6;
                        break;
                    }
                    if ((num9 & 0x10) != 0)
                    {
                        bitsToGet = (num9 & 0xF);
                        len       = tree[num8 + 2];
                        mode      = 2;
                        break;
                    }
                    if ((num9 & 0x40) == 0)
                    {
                        need       = num9;
                        tree_index = num8 / 3 + tree[num8 + 2];
                        break;
                    }
                    if ((num9 & 0x20) != 0)
                    {
                        mode = 7;
                        break;
                    }
                    mode                   = 9;
                    codec.Message          = "invalid literal/length code";
                    r                      = -3;
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }

                case 2:
                {
                    int num7;
                    for (num7 = bitsToGet; num2 < num7; num2 += 8)
                    {
                        if (num4 != 0)
                        {
                            r = 0;
                            num4--;
                            num |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                            continue;
                        }
                        blocks.bitb            = num;
                        blocks.bitk            = num2;
                        codec.AvailableBytesIn = num4;
                        codec.TotalBytesIn    += num3 - codec.NextIn;
                        codec.NextIn           = num3;
                        blocks.writeAt         = num5;
                        return(blocks.Flush(r));
                    }
                    len       += (num & InternalInflateConstants.InflateMask[num7]);
                    num      >>= num7;
                    num2      -= num7;
                    need       = dbits;
                    tree       = dtree;
                    tree_index = dtree_index;
                    mode       = 3;
                    goto case 3;
                }

                case 3:
                {
                    int num7;
                    for (num7 = need; num2 < num7; num2 += 8)
                    {
                        if (num4 != 0)
                        {
                            r = 0;
                            num4--;
                            num |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                            continue;
                        }
                        blocks.bitb            = num;
                        blocks.bitk            = num2;
                        codec.AvailableBytesIn = num4;
                        codec.TotalBytesIn    += num3 - codec.NextIn;
                        codec.NextIn           = num3;
                        blocks.writeAt         = num5;
                        return(blocks.Flush(r));
                    }
                    int num8 = (tree_index + (num & InternalInflateConstants.InflateMask[num7])) * 3;
                    num >>= tree[num8 + 1];
                    num2 -= tree[num8 + 1];
                    int num9 = tree[num8];
                    if ((num9 & 0x10) != 0)
                    {
                        bitsToGet = (num9 & 0xF);
                        dist      = tree[num8 + 2];
                        mode      = 4;
                        break;
                    }
                    if ((num9 & 0x40) == 0)
                    {
                        need       = num9;
                        tree_index = num8 / 3 + tree[num8 + 2];
                        break;
                    }
                    mode                   = 9;
                    codec.Message          = "invalid distance code";
                    r                      = -3;
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }

                case 4:
                {
                    int num7;
                    for (num7 = bitsToGet; num2 < num7; num2 += 8)
                    {
                        if (num4 != 0)
                        {
                            r = 0;
                            num4--;
                            num |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                            continue;
                        }
                        blocks.bitb            = num;
                        blocks.bitk            = num2;
                        codec.AvailableBytesIn = num4;
                        codec.TotalBytesIn    += num3 - codec.NextIn;
                        codec.NextIn           = num3;
                        blocks.writeAt         = num5;
                        return(blocks.Flush(r));
                    }
                    dist += (num & InternalInflateConstants.InflateMask[num7]);
                    num >>= num7;
                    num2 -= num7;
                    mode  = 5;
                    goto case 5;
                }

                case 5:
                {
                    int i;
                    for (i = num5 - dist; i < 0; i += blocks.end)
                    {
                    }
                    while (len != 0)
                    {
                        if (num6 == 0)
                        {
                            if (num5 == blocks.end && blocks.readAt != 0)
                            {
                                num5 = 0;
                                num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                            }
                            if (num6 == 0)
                            {
                                blocks.writeAt = num5;
                                r    = blocks.Flush(r);
                                num5 = blocks.writeAt;
                                num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                                if (num5 == blocks.end && blocks.readAt != 0)
                                {
                                    num5 = 0;
                                    num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                                }
                                if (num6 == 0)
                                {
                                    blocks.bitb            = num;
                                    blocks.bitk            = num2;
                                    codec.AvailableBytesIn = num4;
                                    codec.TotalBytesIn    += num3 - codec.NextIn;
                                    codec.NextIn           = num3;
                                    blocks.writeAt         = num5;
                                    return(blocks.Flush(r));
                                }
                            }
                        }
                        blocks.window[num5++] = blocks.window[i++];
                        num6--;
                        if (i == blocks.end)
                        {
                            i = 0;
                        }
                        len--;
                    }
                    mode = 0;
                    break;
                }

                case 6:
                    if (num6 == 0)
                    {
                        if (num5 == blocks.end && blocks.readAt != 0)
                        {
                            num5 = 0;
                            num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                        }
                        if (num6 == 0)
                        {
                            blocks.writeAt = num5;
                            r    = blocks.Flush(r);
                            num5 = blocks.writeAt;
                            num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                            if (num5 == blocks.end && blocks.readAt != 0)
                            {
                                num5 = 0;
                                num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                            }
                            if (num6 == 0)
                            {
                                blocks.bitb            = num;
                                blocks.bitk            = num2;
                                codec.AvailableBytesIn = num4;
                                codec.TotalBytesIn    += num3 - codec.NextIn;
                                codec.NextIn           = num3;
                                blocks.writeAt         = num5;
                                return(blocks.Flush(r));
                            }
                        }
                    }
                    r = 0;
                    blocks.window[num5++] = (byte)lit;
                    num6--;
                    mode = 0;
                    break;

                case 7:
                    if (num2 > 7)
                    {
                        num2 -= 8;
                        num4++;
                        num3--;
                    }
                    blocks.writeAt = num5;
                    r    = blocks.Flush(r);
                    num5 = blocks.writeAt;
                    num6 = ((num5 < blocks.readAt) ? (blocks.readAt - num5 - 1) : (blocks.end - num5));
                    if (blocks.readAt != blocks.writeAt)
                    {
                        blocks.bitb            = num;
                        blocks.bitk            = num2;
                        codec.AvailableBytesIn = num4;
                        codec.TotalBytesIn    += num3 - codec.NextIn;
                        codec.NextIn           = num3;
                        blocks.writeAt         = num5;
                        return(blocks.Flush(r));
                    }
                    mode = 8;
                    goto case 8;

                case 8:
                    r                      = 1;
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));

                case 9:
                    r                      = -3;
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));

                default:
                    r                      = -2;
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }
            }
        }
        internal int SetDictionary(ZlibCodec z, byte[] dictionary)
        {
            int index = 0;
            int length = dictionary.Length;
            if (z == null || z.istate == null || z.istate.mode != DICT0)
                throw new ZlibException("Stream error.");

            if (Adler.Adler32(1L, dictionary, 0, dictionary.Length) != z._Adler32)
            {
                return ZlibConstants.Z_DATA_ERROR;
            }

            z._Adler32 = Adler.Adler32(0, null, 0, 0);

            if (length >= (1 << z.istate.wbits))
            {
                length = (1 << z.istate.wbits) - 1;
                index = dictionary.Length - length;
            }
            z.istate.blocks.SetDictionary(dictionary, index, length);
            z.istate.mode = BLOCKS;
            return ZlibConstants.Z_OK;
        }
 internal int Initialize(ZlibCodec codec, CompressionLevel level)
 {
     return(this.Initialize(codec, level, 15));
 }
 // Returns true if inflate is currently at the end of a block generated
 // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
 // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
 // but removes the length bytes of the resulting empty stored block. When
 // decompressing, PPP checks that at the end of input packet, inflate is
 // waiting for these length bytes.
 internal int SyncPoint(ZlibCodec z)
 {
     if (z == null || z.istate == null || z.istate.blocks == null)
         return ZlibConstants.Z_STREAM_ERROR;
     return z.istate.blocks.SyncPoint();
 }
 internal int Initialize(ZlibCodec codec, CompressionLevel level, int bits, CompressionStrategy compressionStrategy)
 {
     return(this.Initialize(codec, level, bits, DeflateManager.MEM_LEVEL_DEFAULT, compressionStrategy));
 }
        // copy as much as possible from the sliding window to the output area
        internal int Flush(ZlibCodec z, int r)
        {
            int n;
            int p;
            int q;

            // local copies of source and destination pointers
            p = z.NextOut;
            q = read;

            // compute number of bytes to copy as far as end of window
            n = (int)((q <= write ? write : end) - q);
            if (n > z.AvailableBytesOut)
                n = z.AvailableBytesOut;
            if (n != 0 && r == ZlibConstants.Z_BUF_ERROR)
                r = ZlibConstants.Z_OK;

            // update counters
            z.AvailableBytesOut -= n;
            z.TotalBytesOut += n;

            // update check information
            if (checkfn != null)
                z._Adler32 = check = Adler.Adler32(check, window, q, n);

            // copy as far as end of window
            Array.Copy(window, q, z.OutputBuffer, p, n);
            p += n;
            q += n;

            // see if more to copy at beginning of window
            if (q == end)
            {
                // wrap pointers
                q = 0;
                if (write == end)
                    write = 0;

                // compute bytes to copy
                n = write - q;
                if (n > z.AvailableBytesOut)
                    n = z.AvailableBytesOut;
                if (n != 0 && r == ZlibConstants.Z_BUF_ERROR)
                    r = ZlibConstants.Z_OK;

                // update counters
                z.AvailableBytesOut -= n;
                z.TotalBytesOut += n;

                // update check information
                if (checkfn != null)
                    z._Adler32 = check = Adler.Adler32(check, window, q, n);

                // copy
                Array.Copy(window, q, z.OutputBuffer, p, n);
                p += n;
                q += n;
            }

            // update pointers
            z.NextOut = p;
            read = q;

            // done
            return r;
        }
 private void _PerpetualWriterMethod(object state)
 {
     try
     {
         ZlibCodec zlibCodec;
         while (true)
         {
             _sessionReset.WaitOne();
             if (_isDisposed)
             {
                 return;
             }
             _sessionReset.Reset();
             WorkItem workItem = null;
             CRC32    cRC      = new CRC32();
             bool     flag;
             while (true)
             {
                 workItem = _pool[_nextToWrite % _pc];
                 lock (workItem)
                 {
                     if (_noMoreInputForThisSegment)
                     {
                     }
                     while (true)
                     {
                         if (workItem.status == 4)
                         {
                             workItem.status = 5;
                             _outStream.Write(workItem.compressed, 0, workItem.compressedBytesAvailable);
                             cRC.Combine(workItem.crc, workItem.inputBytesAvailable);
                             _totalBytesProcessed += workItem.inputBytesAvailable;
                             _nextToWrite++;
                             workItem.inputBytesAvailable = 0;
                             workItem.status = 6;
                             Monitor.Pulse(workItem);
                             break;
                         }
                         int num = 0;
                         while (workItem.status != 4 && (!_noMoreInputForThisSegment || _nextToWrite != _nextToFill))
                         {
                             num++;
                             Monitor.Pulse(workItem);
                             Monitor.Wait(workItem);
                             if (workItem.status != 4)
                             {
                             }
                         }
                         if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                         {
                             break;
                         }
                         flag = true;
                     }
                 }
                 if (_noMoreInputForThisSegment)
                 {
                 }
                 if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                 {
                     break;
                 }
                 flag = true;
             }
             byte[] array = new byte[128];
             zlibCodec = new ZlibCodec();
             int num2 = zlibCodec.InitializeDeflate(_compressLevel, wantRfc1950Header: false);
             zlibCodec.InputBuffer       = null;
             zlibCodec.NextIn            = 0;
             zlibCodec.AvailableBytesIn  = 0;
             zlibCodec.OutputBuffer      = array;
             zlibCodec.NextOut           = 0;
             zlibCodec.AvailableBytesOut = array.Length;
             num2 = zlibCodec.Deflate(FlushType.Finish);
             if (num2 != 1 && num2 != 0)
             {
                 break;
             }
             if (array.Length - zlibCodec.AvailableBytesOut > 0)
             {
                 _outStream.Write(array, 0, array.Length - zlibCodec.AvailableBytesOut);
             }
             zlibCodec.EndDeflate();
             _Crc32 = cRC.Crc32Result;
             _writingDone.Set();
             flag = true;
         }
         throw new Exception("deflating: " + zlibCodec.Message);
     }
     catch (Exception pendingException)
     {
         lock (_eLock)
         {
             if (_pendingException != null)
             {
                 _pendingException = pendingException;
             }
         }
     }
 }
        internal int Process(InflateBlocks blocks, ZlibCodec z, int r)
        {
            int j; // temporary storage
            int tindex; // temporary pointer
            int e; // extra bits or operation
            int b = 0; // bit buffer
            int k = 0; // bits in bit buffer
            int p = 0; // input data pointer
            int n; // bytes available there
            int q; // output window write pointer
            int m; // bytes to end of window or read pointer
            int f; // pointer to copy strings from

            // copy input/output information to locals (UPDATE macro restores)
            p = z.NextIn; n = z.AvailableBytesIn; b = blocks.bitb; k = blocks.bitk;
            q = blocks.write; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;

            // process input and output based on current state
            while (true)
            {
                switch (mode)
                {
                    // waiting for "i:"=input, "o:"=output, "x:"=nothing
                    case START:  // x: set up for LEN
                        if (m >= 258 && n >= 10)
                        {

                            blocks.bitb = b; blocks.bitk = k;
                            z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                            blocks.write = q;
                            r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);

                            p = z.NextIn; n = z.AvailableBytesIn; b = blocks.bitb; k = blocks.bitk;
                            q = blocks.write; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;

                            if (r != ZlibConstants.Z_OK)
                            {
                                mode = (r == ZlibConstants.Z_STREAM_END) ? WASH : BADCODE;
                                break;
                            }
                        }
                        need = lbits;
                        tree = ltree;
                        tree_index = ltree_index;

                        mode = LEN;
                        goto case LEN;

                    case LEN:  // i: get length/literal/eob next
                        j = need;

                        while (k < (j))
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {

                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                blocks.write = q;
                                return blocks.Flush(z, r);
                            }
                            n--;
                            b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        tindex = (tree_index + (b & inflate_mask[j])) * 3;

                        b = SharedUtils.URShift(b, (tree[tindex + 1]));
                        k -= (tree[tindex + 1]);

                        e = tree[tindex];

                        if (e == 0)
                        {
                            // literal
                            lit = tree[tindex + 2];
                            mode = LIT;
                            break;
                        }
                        if ((e & 16) != 0)
                        {
                            // length
                            get_Renamed = e & 15;
                            len = tree[tindex + 2];
                            mode = LENEXT;
                            break;
                        }
                        if ((e & 64) == 0)
                        {
                            // next table
                            need = e;
                            tree_index = tindex / 3 + tree[tindex + 2];
                            break;
                        }
                        if ((e & 32) != 0)
                        {
                            // end of block
                            mode = WASH;
                            break;
                        }
                        mode = BADCODE; // invalid code
                        z.Message = "invalid literal/length code";
                        r = ZlibConstants.Z_DATA_ERROR;

                        blocks.bitb = b; blocks.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        blocks.write = q;
                        return blocks.Flush(z, r);


                    case LENEXT:  // i: getting length extra (have base)
                        j = get_Renamed;

                        while (k < (j))
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {

                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                blocks.write = q;
                                return blocks.Flush(z, r);
                            }
                            n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        len += (b & inflate_mask[j]);

                        b >>= j;
                        k -= j;

                        need = dbits;
                        tree = dtree;
                        tree_index = dtree_index;
                        mode = DIST;
                        goto case DIST;

                    case DIST:  // i: get distance next
                        j = need;

                        while (k < (j))
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {

                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                blocks.write = q;
                                return blocks.Flush(z, r);
                            }
                            n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        tindex = (tree_index + (b & inflate_mask[j])) * 3;

                        b >>= tree[tindex + 1];
                        k -= tree[tindex + 1];

                        e = (tree[tindex]);
                        if ((e & 16) != 0)
                        {
                            // distance
                            get_Renamed = e & 15;
                            dist = tree[tindex + 2];
                            mode = DISTEXT;
                            break;
                        }
                        if ((e & 64) == 0)
                        {
                            // next table
                            need = e;
                            tree_index = tindex / 3 + tree[tindex + 2];
                            break;
                        }
                        mode = BADCODE; // invalid code
                        z.Message = "invalid distance code";
                        r = ZlibConstants.Z_DATA_ERROR;

                        blocks.bitb = b; blocks.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        blocks.write = q;
                        return blocks.Flush(z, r);


                    case DISTEXT:  // i: getting distance extra
                        j = get_Renamed;

                        while (k < (j))
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {

                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                blocks.write = q;
                                return blocks.Flush(z, r);
                            }
                            n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        dist += (b & inflate_mask[j]);

                        b >>= j;
                        k -= j;

                        mode = COPY;
                        goto case COPY;

                    case COPY:  // o: copying bytes in window, waiting for space
                        f = q - dist;
                        while (f < 0)
                        {
                            // modulo window size-"while" instead
                            f += blocks.end; // of "if" handles invalid distances
                        }
                        while (len != 0)
                        {

                            if (m == 0)
                            {
                                if (q == blocks.end && blocks.read != 0)
                                {
                                    q = 0; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;
                                }
                                if (m == 0)
                                {
                                    blocks.write = q; r = blocks.Flush(z, r);
                                    q = blocks.write; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;

                                    if (q == blocks.end && blocks.read != 0)
                                    {
                                        q = 0; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;
                                    }

                                    if (m == 0)
                                    {
                                        blocks.bitb = b; blocks.bitk = k;
                                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                        blocks.write = q;
                                        return blocks.Flush(z, r);
                                    }
                                }
                            }

                            blocks.window[q++] = blocks.window[f++]; m--;

                            if (f == blocks.end)
                                f = 0;
                            len--;
                        }
                        mode = START;
                        break;

                    case LIT:  // o: got literal, waiting for output space
                        if (m == 0)
                        {
                            if (q == blocks.end && blocks.read != 0)
                            {
                                q = 0; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;
                            }
                            if (m == 0)
                            {
                                blocks.write = q; r = blocks.Flush(z, r);
                                q = blocks.write; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;

                                if (q == blocks.end && blocks.read != 0)
                                {
                                    q = 0; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;
                                }
                                if (m == 0)
                                {
                                    blocks.bitb = b; blocks.bitk = k;
                                    z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                    blocks.write = q;
                                    return blocks.Flush(z, r);
                                }
                            }
                        }
                        r = ZlibConstants.Z_OK;

                        blocks.window[q++] = (byte)lit; m--;

                        mode = START;
                        break;

                    case WASH:  // o: got eob, possibly more output
                        if (k > 7)
                        {
                            // return unused byte, if any
                            k -= 8;
                            n++;
                            p--; // can always return one
                        }

                        blocks.write = q; r = blocks.Flush(z, r);
                        q = blocks.write; m = q < blocks.read ? blocks.read - q - 1 : blocks.end - q;

                        if (blocks.read != blocks.write)
                        {
                            blocks.bitb = b; blocks.bitk = k;
                            z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                            blocks.write = q;
                            return blocks.Flush(z, r);
                        }
                        mode = END;
                        goto case END;

                    case END:
                        r = ZlibConstants.Z_STREAM_END;
                        blocks.bitb = b; blocks.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        blocks.write = q;
                        return blocks.Flush(z, r);


                    case BADCODE:  // x: got error

                        r = ZlibConstants.Z_DATA_ERROR;

                        blocks.bitb = b; blocks.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        blocks.write = q;
                        return blocks.Flush(z, r);


                    default:
                        r = ZlibConstants.Z_STREAM_ERROR;

                        blocks.bitb = b; blocks.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        blocks.write = q;
                        return blocks.Flush(z, r);

                }
            }
        }
Exemplo n.º 43
0
 internal static int inflate_trees_fixed(ref int bl, ref int bd, out int[] tl, out int[] td, ZlibCodec z)
 {
     bl = fixed_bl;
     bd = fixed_bd;
     tl = fixed_tl;
     td = fixed_td;
     return(Z_OK);
 }
Exemplo n.º 44
0
		internal int Initialize(ZlibCodec codec, CompressionLevel level)
		{
			return Initialize(codec, level, ZlibConstants.WindowBitsMax);
		}
Exemplo n.º 45
0
        internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
        {
            int result;

            // build literal/length tree
            initWorkArea(288);
            hn[0]  = 0;
            result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
            if (result != Z_OK || bl[0] == 0)
            {
                if (result == Z_DATA_ERROR)
                {
                    z.Message = "oversubscribed literal/length tree";
                }
                else if (result != Z_MEM_ERROR)
                {
                    z.Message = "incomplete literal/length tree";
                    result    = Z_DATA_ERROR;
                }
                return(result);
            }

            // build distance tree
            initWorkArea(288);
            result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);

            if (result != Z_OK || (bd[0] == 0 && nl > 257))
            {
                if (result == Z_DATA_ERROR)
                {
                    z.Message = "oversubscribed distance tree";
                }
                else if (result == Z_BUF_ERROR)
                {
                    z.Message = "incomplete distance tree";
                    result    = Z_DATA_ERROR;
                }
                else if (result != Z_MEM_ERROR)
                {
                    z.Message = "empty distance tree with lengths";
                    result    = Z_DATA_ERROR;
                }
                return(result);
            }

            return(Z_OK);
        }
Exemplo n.º 46
0
		internal int Initialize(ZlibCodec codec, CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
		{
			_codec = codec;
			_codec.Message = null;

			// validation
			if (windowBits < 9 || windowBits > 15)
				throw new ZlibException("windowBits must be in the range 9..15.");

			if (memLevel < 1 || memLevel > MEM_LEVEL_MAX)
				throw new ZlibException(String.Format("memLevel must be in the range 1.. {0}", MEM_LEVEL_MAX));

			_codec.dstate = this;

			w_bits = windowBits;
			w_size = 1 << w_bits;
			w_mask = w_size - 1;

			hash_bits = memLevel + 7;
			hash_size = 1 << hash_bits;
			hash_mask = hash_size - 1;
			hash_shift = ((hash_bits + MIN_MATCH - 1) / MIN_MATCH);

			window = new byte[w_size * 2];
			prev = new short[w_size];
			head = new short[hash_size];

			// for memLevel==8, this will be 16384, 16k
			lit_bufsize = 1 << (memLevel + 6);

			// Use a single array as the buffer for data pending compression,
			// the output distance codes, and the output length codes (aka tree).  
			// orig comment: This works just fine since the average
			// output size for (length,distance) codes is <= 24 bits.
			pending = new byte[lit_bufsize * 4];
			_distanceOffset = lit_bufsize;
			_lengthOffset = (1 + 2) * lit_bufsize;

			// So, for memLevel 8, the length of the pending buffer is 65536. 64k.
			// The first 16k are pending bytes.
			// The middle slice, of 32k, is used for distance codes. 
			// The final 16k are length codes.

			this.compressionLevel = level;
			this.compressionStrategy = strategy;

			Reset();
			return ZlibConstants.Z_OK;
		}
Exemplo n.º 47
0
 internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
 {
     bl[0] = fixed_bl;
     bd[0] = fixed_bd;
     tl[0] = fixed_tl;
     td[0] = fixed_td;
     return(Z_OK);
 }
 // Returns true if inflate is currently at the end of a block generated
 // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
 // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
 // but removes the length bytes of the resulting empty stored block. When
 // decompressing, PPP checks that at the end of input packet, inflate is
 // waiting for these length bytes.
 internal int SyncPoint(ZlibCodec z)
 {
     return blocks.SyncPoint();
 }
Exemplo n.º 49
0
        internal int Process(InflateBlocks blocks, int r)
        {
            ZlibCodec codec = blocks._codec;
            int       num   = codec.NextIn;
            int       num2  = codec.AvailableBytesIn;
            int       num3  = blocks.bitb;
            int       i     = blocks.bitk;
            int       num4  = blocks.writeAt;
            int       num5  = (num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1);

            while (true)
            {
                int num6;
                switch (this.mode)
                {
                case 0:
                    if (num5 >= 258 && num2 >= 10)
                    {
                        blocks.bitb            = num3;
                        blocks.bitk            = i;
                        codec.AvailableBytesIn = num2;
                        codec.TotalBytesIn    += (long)(num - codec.NextIn);
                        codec.NextIn           = num;
                        blocks.writeAt         = num4;
                        r    = this.InflateFast((int)this.lbits, (int)this.dbits, this.ltree, this.ltree_index, this.dtree, this.dtree_index, blocks, codec);
                        num  = codec.NextIn;
                        num2 = codec.AvailableBytesIn;
                        num3 = blocks.bitb;
                        i    = blocks.bitk;
                        num4 = blocks.writeAt;
                        num5 = ((num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1));
                        if (r != 0)
                        {
                            this.mode = ((r != 1) ? 9 : 7);
                            continue;
                        }
                    }
                    this.need       = (int)this.lbits;
                    this.tree       = this.ltree;
                    this.tree_index = this.ltree_index;
                    this.mode       = 1;
                    goto IL_1C4;

                case 1:
                    goto IL_1C4;

                case 2:
                    num6 = this.bitsToGet;
                    while (i < num6)
                    {
                        if (num2 == 0)
                        {
                            goto IL_3A2;
                        }
                        r = 0;
                        num2--;
                        num3 |= (int)(codec.InputBuffer[num++] & 255) << i;
                        i    += 8;
                    }
                    this.len       += (num3 & InternalInflateConstants.InflateMask[num6]);
                    num3          >>= num6;
                    i              -= num6;
                    this.need       = (int)this.dbits;
                    this.tree       = this.dtree;
                    this.tree_index = this.dtree_index;
                    this.mode       = 3;
                    goto IL_471;

                case 3:
                    goto IL_471;

                case 4:
                    num6 = this.bitsToGet;
                    while (i < num6)
                    {
                        if (num2 == 0)
                        {
                            goto IL_618;
                        }
                        r = 0;
                        num2--;
                        num3 |= (int)(codec.InputBuffer[num++] & 255) << i;
                        i    += 8;
                    }
                    this.dist += (num3 & InternalInflateConstants.InflateMask[num6]);
                    num3     >>= num6;
                    i         -= num6;
                    this.mode  = 5;
                    goto IL_6C3;

                case 5:
                    goto IL_6C3;

                case 6:
                    if (num5 == 0)
                    {
                        if (num4 == blocks.end && blocks.readAt != 0)
                        {
                            num4 = 0;
                            num5 = ((num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1));
                        }
                        if (num5 == 0)
                        {
                            blocks.writeAt = num4;
                            r    = blocks.Flush(r);
                            num4 = blocks.writeAt;
                            num5 = ((num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1));
                            if (num4 == blocks.end && blocks.readAt != 0)
                            {
                                num4 = 0;
                                num5 = ((num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1));
                            }
                            if (num5 == 0)
                            {
                                goto Block_44;
                            }
                        }
                    }
                    r = 0;
                    blocks.window[num4++] = (byte)this.lit;
                    num5--;
                    this.mode = 0;
                    continue;

                case 7:
                    goto IL_9B8;

                case 8:
                    goto IL_A7A;

                case 9:
                    goto IL_AC7;
                }
                break;
                continue;
IL_1C4:
                num6 = this.need;
                while (i < num6)
                {
                    if (num2 == 0)
                    {
                        goto IL_1DF;
                    }
                    r = 0;
                    num2--;
                    num3 |= (int)(codec.InputBuffer[num++] & 255) << i;
                    i    += 8;
                }
                int num7 = (this.tree_index + (num3 & InternalInflateConstants.InflateMask[num6])) * 3;
                num3 >>= this.tree[num7 + 1];
                i     -= this.tree[num7 + 1];
                int num8 = this.tree[num7];
                if (num8 == 0)
                {
                    this.lit  = this.tree[num7 + 2];
                    this.mode = 6;
                    continue;
                }
                if ((num8 & 16) != 0)
                {
                    this.bitsToGet = (num8 & 15);
                    this.len       = this.tree[num7 + 2];
                    this.mode      = 2;
                    continue;
                }
                if ((num8 & 64) == 0)
                {
                    this.need       = num8;
                    this.tree_index = num7 / 3 + this.tree[num7 + 2];
                    continue;
                }
                if ((num8 & 32) != 0)
                {
                    this.mode = 7;
                    continue;
                }
                goto IL_325;
IL_471:
                num6 = this.need;
                while (i < num6)
                {
                    if (num2 == 0)
                    {
                        goto IL_48C;
                    }
                    r = 0;
                    num2--;
                    num3 |= (int)(codec.InputBuffer[num++] & 255) << i;
                    i    += 8;
                }
                num7   = (this.tree_index + (num3 & InternalInflateConstants.InflateMask[num6])) * 3;
                num3 >>= this.tree[num7 + 1];
                i     -= this.tree[num7 + 1];
                num8   = this.tree[num7];
                if ((num8 & 16) != 0)
                {
                    this.bitsToGet = (num8 & 15);
                    this.dist      = this.tree[num7 + 2];
                    this.mode      = 4;
                    continue;
                }
                if ((num8 & 64) == 0)
                {
                    this.need       = num8;
                    this.tree_index = num7 / 3 + this.tree[num7 + 2];
                    continue;
                }
                goto IL_59B;
IL_6C3:
                int j;
                for (j = num4 - this.dist; j < 0; j += blocks.end)
                {
                }
                while (this.len != 0)
                {
                    if (num5 == 0)
                    {
                        if (num4 == blocks.end && blocks.readAt != 0)
                        {
                            num4 = 0;
                            num5 = ((num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1));
                        }
                        if (num5 == 0)
                        {
                            blocks.writeAt = num4;
                            r    = blocks.Flush(r);
                            num4 = blocks.writeAt;
                            num5 = ((num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1));
                            if (num4 == blocks.end && blocks.readAt != 0)
                            {
                                num4 = 0;
                                num5 = ((num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1));
                            }
                            if (num5 == 0)
                            {
                                goto Block_32;
                            }
                        }
                    }
                    blocks.window[num4++] = blocks.window[j++];
                    num5--;
                    if (j == blocks.end)
                    {
                        j = 0;
                    }
                    this.len--;
                }
                this.mode = 0;
            }
            r                      = -2;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_1DF:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_325:
            this.mode              = 9;
            codec.Message          = "invalid literal/length code";
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_3A2:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_48C:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_59B:
            this.mode              = 9;
            codec.Message          = "invalid distance code";
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_618:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

Block_32:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

Block_44:
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_9B8:
            if (i > 7)
            {
                i -= 8;
                num2++;
                num--;
            }
            blocks.writeAt = num4;
            r    = blocks.Flush(r);
            num4 = blocks.writeAt;
            int arg_A11_0 = (num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1);

            if (blocks.readAt != blocks.writeAt)
            {
                blocks.bitb            = num3;
                blocks.bitk            = i;
                codec.AvailableBytesIn = num2;
                codec.TotalBytesIn    += (long)(num - codec.NextIn);
                codec.NextIn           = num;
                blocks.writeAt         = num4;
                return(blocks.Flush(r));
            }
            this.mode = 8;
IL_A7A:
            r                      = 1;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));

IL_AC7:
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));
        }
 internal InflateBlocks(ZlibCodec codec, System.Object checkfn, int w)
 {
     _codec = codec;
     hufts = new int[MANY * 3];
     window = new byte[w];
     end = w;
     this.checkfn = checkfn;
     mode = InflateBlockMode.TYPE;
     Reset();
 }
Exemplo n.º 51
0
 private void end()
 {
     if (z == null)
         return;
     if (_wantCompress)
     {
         _z.EndDeflate();
     }
     else
     {
         _z.EndInflate();
     }
     _z = null;
 }
        // Called with number of bytes left to write in window at least 258
        // (the maximum string length) and number of input bytes available
        // at least ten.  The ten bytes are six bytes for the longest length/
        // distance pair plus four bytes for overloading the bit buffer.

        internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
        {
            int t;        // temporary pointer
            int[] tp;     // temporary pointer
            int tp_index; // temporary pointer
            int e;        // extra bits or operation
            int b;        // bit buffer
            int k;        // bits in bit buffer
            int p;        // input data pointer
            int n;        // bytes available there
            int q;        // output window write pointer
            int m;        // bytes to end of window or read pointer
            int ml;       // mask for literal/length tree
            int md;       // mask for distance tree
            int c;        // bytes to copy
            int d;        // distance back to copy from
            int r;        // copy source pointer

            int tp_index_t_3; // (tp_index+t)*3

            // load input, output, bit values
            p = z.NextIn; n = z.AvailableBytesIn; b = s.bitb; k = s.bitk;
            q = s.writeAt; m = q < s.readAt ? s.readAt - q - 1 : s.end - q;

            // initialize masks
            ml = InternalInflateConstants.InflateMask[bl];
            md = InternalInflateConstants.InflateMask[bd];

            // do until not enough input or output space for fast loop
            do
            {
                // assume called with m >= 258 && n >= 10
                // get literal/length code
                while (k < (20))
                {
                    // max bits for literal/length code
                    n--;
                    b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
                }

                t = b & ml;
                tp = tl;
                tp_index = tl_index;
                tp_index_t_3 = (tp_index + t) * 3;
                if ((e = tp[tp_index_t_3]) == 0)
                {
                    b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);

                    s.window[q++] = (byte)tp[tp_index_t_3 + 2];
                    m--;
                    continue;
                }
                do
                {

                    b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);

                    if ((e & 16) != 0)
                    {
                        e &= 15;
                        c = tp[tp_index_t_3 + 2] + ((int)b & InternalInflateConstants.InflateMask[e]);

                        b >>= e; k -= e;

                        // decode distance base of block to copy
                        while (k < 15)
                        {
                            // max bits for distance code
                            n--;
                            b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
                        }

                        t = b & md;
                        tp = td;
                        tp_index = td_index;
                        tp_index_t_3 = (tp_index + t) * 3;
                        e = tp[tp_index_t_3];

                        do
                        {

                            b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);

                            if ((e & 16) != 0)
                            {
                                // get extra bits to add to distance base
                                e &= 15;
                                while (k < e)
                                {
                                    // get extra bits (up to 13)
                                    n--;
                                    b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
                                }

                                d = tp[tp_index_t_3 + 2] + (b & InternalInflateConstants.InflateMask[e]);

                                b >>= e; k -= e;

                                // do the copy
                                m -= c;
                                if (q >= d)
                                {
                                    // offset before dest
                                    //  just copy
                                    r = q - d;
                                    if (q - r > 0 && 2 > (q - r))
                                    {
                                        s.window[q++] = s.window[r++]; // minimum count is three,
                                        s.window[q++] = s.window[r++]; // so unroll loop a little
                                        c -= 2;
                                    }
                                    else
                                    {
                                        Array.Copy(s.window, r, s.window, q, 2);
                                        q += 2; r += 2; c -= 2;
                                    }
                                }
                                else
                                {
                                    // else offset after destination
                                    r = q - d;
                                    do
                                    {
                                        r += s.end; // force pointer in window
                                    }
                                    while (r < 0); // covers invalid distances
                                    e = s.end - r;
                                    if (c > e)
                                    {
                                        // if source crosses,
                                        c -= e; // wrapped copy
                                        if (q - r > 0 && e > (q - r))
                                        {
                                            do
                                            {
                                                s.window[q++] = s.window[r++];
                                            }
                                            while (--e != 0);
                                        }
                                        else
                                        {
                                            Array.Copy(s.window, r, s.window, q, e);
                                            q += e; r += e; e = 0;
                                        }
                                        r = 0; // copy rest from start of window
                                    }
                                }

                                // copy all or what's left
                                if (q - r > 0 && c > (q - r))
                                {
                                    do
                                    {
                                        s.window[q++] = s.window[r++];
                                    }
                                    while (--c != 0);
                                }
                                else
                                {
                                    Array.Copy(s.window, r, s.window, q, c);
                                    q += c; r += c; c = 0;
                                }
                                break;
                            }
                            else if ((e & 64) == 0)
                            {
                                t += tp[tp_index_t_3 + 2];
                                t += (b & InternalInflateConstants.InflateMask[e]);
                                tp_index_t_3 = (tp_index + t) * 3;
                                e = tp[tp_index_t_3];
                            }
                            else
                            {
                                z.Message = "invalid distance code";

                                c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);

                                s.bitb = b; s.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                s.writeAt = q;

                                return ZlibConstants.Z_DATA_ERROR;
                            }
                        }
                        while (true);
                        break;
                    }

                    if ((e & 64) == 0)
                    {
                        t += tp[tp_index_t_3 + 2];
                        t += (b & InternalInflateConstants.InflateMask[e]);
                        tp_index_t_3 = (tp_index + t) * 3;
                        if ((e = tp[tp_index_t_3]) == 0)
                        {
                            b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
                            s.window[q++] = (byte)tp[tp_index_t_3 + 2];
                            m--;
                            break;
                        }
                    }
                    else if ((e & 32) != 0)
                    {
                        c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);

                        s.bitb = b; s.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        s.writeAt = q;

                        return ZlibConstants.Z_STREAM_END;
                    }
                    else
                    {
                        z.Message = "invalid literal/length code";

                        c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);

                        s.bitb = b; s.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        s.writeAt = q;

                        return ZlibConstants.Z_DATA_ERROR;
                    }
                }
                while (true);
            }
            while (m >= 258 && n >= 10);

            // not enough input or output--restore pointers and return
            c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);

            s.bitb = b; s.bitk = k;
            z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
            s.writeAt = q;

            return ZlibConstants.Z_OK;
        }
Exemplo n.º 53
-1
        public void Close()
        {
            z.EndInflate();
            z = null;

            if( !_leaveOpen )
                _stream.Close();
            _stream = null;
        }