public DeflateOutputStream(Stream output, ZStream z, bool compress)
				: base(output)
			{
				this.z = z;
				this.compress = compress;
				this.FlushMode = JZlib.Z_PARTIAL_FLUSH;
			}
        public TlsDeflateCompression(int level)
        {
            this.zIn = new ZStream();
            this.zIn.inflateInit();

            this.zOut = new ZStream();
            this.zOut.deflateInit(level);
        }
 public DeflateOutputStream(Stream output, ZStream z, bool compress)
     : base(output)
 {
     this.z = z;
     this.compress = compress;
     // TODO http://www.bolet.org/~pornin/deflate-flush.html says we should use Z_SYNC_FLUSH
     this.FlushMode = JZlib.Z_PARTIAL_FLUSH;
 }
		public TlsDeflateCompression()
		{
			this.zIn = new ZStream();
			this.zIn.inflateInit();

			this.zOut = new ZStream();
			// TODO Allow custom setting
			this.zOut.deflateInit(JZlib.Z_DEFAULT_COMPRESSION);
		}
            public DeflateOutputStream(Stream output, ZStream z, bool compress)
                : base(output, z)
            {
                this.compress = compress;

                /*
                 * See discussion at http://www.bolet.org/~pornin/deflate-flush.html .
                 */
                this.FlushMode = JZlib.Z_SYNC_FLUSH;
            }
Exemple #6
0
		public CompressedStream (Stream baseStream)
		{
			BaseStream = baseStream;

			zOut = new ZStream ();
			zOut.deflateInit (5, true);
			zOut.next_out = new byte[4096];

			zIn = new ZStream ();
			zIn.inflateInit (true);
			zIn.next_in = new byte[4096];
		}
        public ZOutputStream(Stream output, ZStream z)
			: base()
		{
			Debug.Assert(output.CanWrite);

            if (z == null)
            {
                z = new ZStream();
                z.inflateInit();
            }

            this.output = output;
            this.z = z;
			this.compress = false;
		}
Exemple #8
0
        public ZOutputStream(Stream output, ZStream z)
			: base()
		{
			Debug.Assert(output.CanWrite);

            if (z == null)
            {
                z = new ZStream();
            }

            if (z.istate == null && z.dstate == null)
            {
                z.inflateInit();
            }

            this.output = output;
            this.compress = (z.istate == null);
            this.z = z;
		}
Exemple #9
0
        public ZInputStream(Stream input, ZStream z)
			: base()
		{
			Debug.Assert(input.CanRead);

            if (z == null)
            {
                z = new ZStream();
            }

            if (z.istate == null && z.dstate == null)
            {
                z.inflateInit();
            }

            this.input = input;
            this.compress = (z.istate == null);
            this.z = z;
            this.z.next_in = buf;
			this.z.next_in_index = 0;
			this.z.avail_in = 0;
		}
Exemple #10
0
        internal int deflate(ZStream strm, int flush)
        {
            if (flush > 4 || flush < 0)
            {
                return(-2);
            }
            if (strm.next_out == null || (strm.next_in == null && strm.avail_in != 0) || (status == 666 && flush != 4))
            {
                strm.msg = z_errmsg[4];
                return(-2);
            }
            if (strm.avail_out == 0)
            {
                strm.msg = z_errmsg[7];
                return(-5);
            }
            this.strm = strm;
            int num = last_flush;

            last_flush = flush;
            if (status == 42)
            {
                int num2 = 8 + (w_bits - 8 << 4) << 8;
                int num3 = ((level - 1) & 0xFF) >> 1;
                if (num3 > 3)
                {
                    num3 = 3;
                }
                num2 |= num3 << 6;
                if (strstart != 0)
                {
                    num2 |= 0x20;
                }
                num2  += 31 - num2 % 31;
                status = 113;
                putShortMSB(num2);
                if (strstart != 0)
                {
                    putShortMSB((int)(strm.adler >> 16));
                    putShortMSB((int)(strm.adler & 0xFFFF));
                }
                strm.adler = strm._adler.adler32(0L, null, 0, 0);
            }
            if (pending != 0)
            {
                strm.flush_pending();
                if (strm.avail_out == 0)
                {
                    last_flush = -1;
                    return(0);
                }
            }
            else if (strm.avail_in == 0 && flush <= num && flush != 4)
            {
                strm.msg = z_errmsg[7];
                return(-5);
            }
            if (status == 666 && strm.avail_in != 0)
            {
                strm.msg = z_errmsg[7];
                return(-5);
            }
            if (strm.avail_in != 0 || lookahead != 0 || (flush != 0 && status != 666))
            {
                int num4 = -1;
                switch (config_table[level].func)
                {
                case 0:
                    num4 = deflate_stored(flush);
                    break;

                case 1:
                    num4 = deflate_fast(flush);
                    break;

                case 2:
                    num4 = deflate_slow(flush);
                    break;
                }
                if (num4 == 2 || num4 == 3)
                {
                    status = 666;
                }
                switch (num4)
                {
                case 0:
                case 2:
                    if (strm.avail_out == 0)
                    {
                        last_flush = -1;
                    }
                    return(0);

                case 1:
                    if (flush == 1)
                    {
                        _tr_align();
                    }
                    else
                    {
                        _tr_stored_block(0, 0, eof: false);
                        if (flush == 3)
                        {
                            for (int i = 0; i < hash_size; i++)
                            {
                                head[i] = 0;
                            }
                        }
                    }
                    strm.flush_pending();
                    if (strm.avail_out == 0)
                    {
                        last_flush = -1;
                        return(0);
                    }
                    break;
                }
            }
            if (flush != 4)
            {
                return(0);
            }
            if (noheader != 0)
            {
                return(1);
            }
            putShortMSB((int)(strm.adler >> 16));
            putShortMSB((int)(strm.adler & 0xFFFF));
            strm.flush_pending();
            noheader = -1;
            if (pending == 0)
            {
                return(1);
            }
            return(0);
        }
Exemple #11
0
        internal int inflateReset(ZStream z)
        {
            if(z == null || z.istate == null) return Z_STREAM_ERROR;

            z.total_in = z.total_out = 0;
            z.msg = null;
            z.istate.mode = z.istate.nowrap!=0 ? BLOCKS : METHOD;
            z.istate.blocks.reset(z, null);
            return Z_OK;
        }
Exemple #12
0
 internal int inflateEnd(ZStream z)
 {
     if(blocks != null)
         blocks.free(z);
     blocks=null;
     //    ZFREE(z, z->state);
     return Z_OK;
 }
Exemple #13
0
        public ZLibStatus InflateSync(ZStream 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 (this._mode != InflateMode.BAD)
            {
                this._mode = InflateMode.BAD;
                this._marker = 0;
            }
            if ((n = base.avail_in) == 0)
                return ZLibStatus.Z_BUF_ERROR;
            p = base.next_in_index;
            m = this._marker;

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

            // restore
            base.total_in += p - base.next_in_index;
            base.next_in_index = p;
            base.avail_in = n;
            this._marker = m;

            // return no joy or set up to restart on a new block
            if (m != 4)
            {
                return ZLibStatus.Z_DATA_ERROR;
            }
            r = base.total_in; w = base.total_out;
            InflateReset();
            base.total_in = r; base.total_out = w;
            this._mode = InflateMode.BLOCKS;
            return ZLibStatus.Z_OK;
        }
Exemple #14
0
 public ZLibStatus InflateEnd(ZStream z)
 {
     if (_blocks != null)
         _blocks.free(z);
     _blocks = null;
     //    ZFREE(z, z->state);
     return ZLibStatus.Z_OK;
 }
Exemple #15
0
        internal int deflateSetDictionary (ZStream strm, byte[] dictionary, int dictLength){
            int length = dictLength;
            int index=0;

            if(dictionary == null || status != INIT_STATE)
                return Z_STREAM_ERROR;

            strm.adler=strm._adler.adler32(strm.adler, dictionary, 0, dictLength);

            if(length < MIN_MATCH) return Z_OK;
            if(length > w_size-MIN_LOOKAHEAD){
                length = w_size-MIN_LOOKAHEAD;
                index=dictLength-length; // use the tail of the dictionary
            }
            System.Array.Copy(dictionary, index, window, 0, length);
            strstart = length;
            block_start = length;

            // Insert all strings in the hash table (except for the last two bytes).
            // s->lookahead stays null, so s->ins_h will be recomputed at the next
            // call of fill_window.

            ins_h = window[0]&0xff;
            ins_h=(((ins_h)<<hash_shift)^(window[1]&0xff))&hash_mask;

            for(int n=0; n<=length-MIN_MATCH; n++){
                ins_h=(((ins_h)<<hash_shift)^(window[(n)+(MIN_MATCH-1)]&0xff))&hash_mask;
                prev[n&w_mask]=head[ins_h];
                head[ins_h]=(short)n;
            }
            return Z_OK;
        }
Exemple #16
0
 internal int deflateInit(ZStream strm, int level, int bits)
 {
     return(deflateInit2(strm, level, 8, bits, 8, 0));
 }
Exemple #17
0
        internal int proc(InfBlocks s, ZStream 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.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
            q = s.write; m = q < s.read?s.read - q - 1:s.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)
                    {
                        s.bitb     = b; s.bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        s.write    = q;
                        r          = inflate_fast(lbits, dbits,
                                                  ltree, ltree_index,
                                                  dtree, dtree_index,
                                                  s, z);

                        p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
                        q = s.write; m = q < s.read?s.read - q - 1:s.end - q;

                        if (r != Z_OK)
                        {
                            mode = r == 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 = Z_OK;
                        }
                        else
                        {
                            s.bitb     = b; s.bitk = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            s.write    = q;
                            return(s.inflate_flush(z, r));
                        }
                        n--;
                        b |= (z.next_in[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 == 0)                   // literal
                    {
                        lit  = tree[tindex + 2];
                        mode = LIT;
                        break;
                    }
                    if ((e & 16) != 0)             // length
                    {
                        get  = 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.msg = "invalid literal/length code";
                    r     = Z_DATA_ERROR;

                    s.bitb     = b; s.bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    s.write    = q;
                    return(s.inflate_flush(z, r));

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

                    while (k < (j))
                    {
                        if (n != 0)
                        {
                            r = Z_OK;
                        }
                        else
                        {
                            s.bitb     = b; s.bitk = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            s.write    = q;
                            return(s.inflate_flush(z, r));
                        }
                        n--; b |= (z.next_in[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 = Z_OK;
                        }
                        else
                        {
                            s.bitb     = b; s.bitk = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            s.write    = q;
                            return(s.inflate_flush(z, r));
                        }
                        n--; b |= (z.next_in[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  = 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.msg = "invalid distance code";
                    r     = Z_DATA_ERROR;

                    s.bitb     = b; s.bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    s.write    = q;
                    return(s.inflate_flush(z, r));

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

                    while (k < (j))
                    {
                        if (n != 0)
                        {
                            r = Z_OK;
                        }
                        else
                        {
                            s.bitb     = b; s.bitk = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            s.write    = q;
                            return(s.inflate_flush(z, r));
                        }
                        n--; b |= (z.next_in[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 += s.end;     // of "if" handles invalid distances
                    }
                    while (len != 0)
                    {
                        if (m == 0)
                        {
                            if (q == s.end && s.read != 0)
                            {
                                q = 0; m = q < s.read?s.read - q - 1:s.end - q;
                            }
                            if (m == 0)
                            {
                                s.write = q; r = s.inflate_flush(z, r);
                                q       = s.write; m = q < s.read?s.read - q - 1:s.end - q;

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

                                if (m == 0)
                                {
                                    s.bitb     = b; s.bitk = k;
                                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                    s.write    = q;
                                    return(s.inflate_flush(z, r));
                                }
                            }
                        }

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

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

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

                            if (q == s.end && s.read != 0)
                            {
                                q = 0; m = q < s.read?s.read - q - 1:s.end - q;
                            }
                            if (m == 0)
                            {
                                s.bitb     = b; s.bitk = k;
                                z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                s.write    = q;
                                return(s.inflate_flush(z, r));
                            }
                        }
                    }
                    r = Z_OK;

                    s.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
                    }

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

                    if (s.read != s.write)
                    {
                        s.bitb     = b; s.bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        s.write    = q;
                        return(s.inflate_flush(z, r));
                    }
                    mode = END;
                    goto case END;

                case END:
                    r          = Z_STREAM_END;
                    s.bitb     = b; s.bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    s.write    = q;
                    return(s.inflate_flush(z, r));

                case BADCODE:           // x: got error

                    r = Z_DATA_ERROR;

                    s.bitb     = b; s.bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    s.write    = q;
                    return(s.inflate_flush(z, r));

                default:
                    r = Z_STREAM_ERROR;

                    s.bitb     = b; s.bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    s.write    = q;
                    return(s.inflate_flush(z, r));
                }
            }
        }
Exemple #18
0
        internal int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InfBlocks s, ZStream z)
        {
            int num12;
            int num6  = z.next_in_index;
            int num7  = z.avail_in;
            int bitb  = s.bitb;
            int bitk  = s.bitk;
            int write = s.write;
            int num9  = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
            int num10 = inflate_mask[bl];
            int num11 = inflate_mask[bd];

Label_009B:
            while (bitk < 20)
            {
                num7--;
                bitb |= (z.next_in[num6++] & 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--;
                goto Label_062B;
            }
Label_00FB:
            bitb  = bitb >> numArray[index + 1];
            bitk -= numArray[index + 1];
            if ((num3 & 0x10) == 0)
            {
                if ((num3 & 0x40) == 0)
                {
                    num  += numArray[index + 2];
                    num  += bitb & inflate_mask[num3];
                    index = (num2 + num) * 3;
                    num3  = numArray[index];
                    if (num3 != 0)
                    {
                        goto Label_00FB;
                    }
                    bitb  = bitb >> numArray[index + 1];
                    bitk -= numArray[index + 1];
                    s.window[write++] = (byte)numArray[index + 2];
                    num9--;
                }
                else
                {
                    if ((num3 & 0x20) != 0)
                    {
                        num12           = z.avail_in - num7;
                        num12           = ((bitk >> 3) >= num12) ? num12 : (bitk >> 3);
                        num7           += num12;
                        num6           -= num12;
                        bitk           -= num12 << 3;
                        s.bitb          = bitb;
                        s.bitk          = bitk;
                        z.avail_in      = num7;
                        z.total_in     += num6 - z.next_in_index;
                        z.next_in_index = num6;
                        s.write         = write;
                        return(1);
                    }
                    z.msg           = "invalid literal/length code";
                    num12           = z.avail_in - num7;
                    num12           = ((bitk >> 3) >= num12) ? num12 : (bitk >> 3);
                    num7           += num12;
                    num6           -= num12;
                    bitk           -= num12 << 3;
                    s.bitb          = bitb;
                    s.bitk          = bitk;
                    z.avail_in      = num7;
                    z.total_in     += num6 - z.next_in_index;
                    z.next_in_index = num6;
                    s.write         = write;
                    return(-3);
                }
                goto Label_062B;
            }
            num3 &= 15;
            num12 = numArray[index + 2] + (bitb & inflate_mask[num3]);
            bitb  = bitb >> num3;
            bitk -= num3;
            while (bitk < 15)
            {
                num7--;
                bitb |= (z.next_in[num6++] & 0xff) << bitk;
                bitk += 8;
            }
            num      = bitb & num11;
            numArray = td;
            num2     = td_index;
            index    = (num2 + num) * 3;
            num3     = numArray[index];
Label_0196:
            bitb  = bitb >> numArray[index + 1];
            bitk -= numArray[index + 1];
            if ((num3 & 0x10) != 0)
            {
                int num14;
                num3 &= 15;
                while (bitk < num3)
                {
                    num7--;
                    bitb |= (z.next_in[num6++] & 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;
                }
            }
            else
            {
                if ((num3 & 0x40) == 0)
                {
                    num  += numArray[index + 2];
                    num  += bitb & inflate_mask[num3];
                    index = (num2 + num) * 3;
                    num3  = numArray[index];
                    goto Label_0196;
                }
                z.msg           = "invalid distance code";
                num12           = z.avail_in - num7;
                num12           = ((bitk >> 3) >= num12) ? num12 : (bitk >> 3);
                num7           += num12;
                num6           -= num12;
                bitk           -= num12 << 3;
                s.bitb          = bitb;
                s.bitk          = bitk;
                z.avail_in      = num7;
                z.total_in     += num6 - z.next_in_index;
                z.next_in_index = num6;
                s.write         = write;
                return(-3);
            }
Label_062B:
            if ((num9 >= 0x102) && (num7 >= 10))
            {
                goto Label_009B;
            }
            num12           = z.avail_in - num7;
            num12           = ((bitk >> 3) >= num12) ? num12 : (bitk >> 3);
            num7           += num12;
            num6           -= num12;
            bitk           -= num12 << 3;
            s.bitb          = bitb;
            s.bitk          = bitk;
            z.avail_in      = num7;
            z.total_in     += num6 - z.next_in_index;
            z.next_in_index = num6;
            s.write         = write;
            return(0);
        }
Exemple #19
0
 internal void free(ZStream z)
 {
 }
Exemple #20
0
        internal int proc(InfBlocks s, ZStream z, int r)
        {
            int get;
            int num10;
            int bitb = 0;
            int bitk = 0;
            int num6 = 0;

            num6 = z.next_in_index;
            int num7 = z.avail_in;

            bitb = s.bitb;
            bitk = s.bitk;
            int write = s.write;
            int num9  = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);

Label_0057:
            switch (this.mode)
            {
            case 0:
                if ((num9 < 0x102) || (num7 < 10))
                {
                    break;
                }
                s.bitb          = bitb;
                s.bitk          = bitk;
                z.avail_in      = num7;
                z.total_in     += num6 - z.next_in_index;
                z.next_in_index = num6;
                s.write         = write;
                r     = this.inflate_fast(this.lbits, this.dbits, this.ltree, this.ltree_index, this.dtree, this.dtree_index, s, z);
                num6  = z.next_in_index;
                num7  = z.avail_in;
                bitb  = s.bitb;
                bitk  = s.bitk;
                write = s.write;
                num9  = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                if (r == 0)
                {
                    break;
                }
                this.mode = (r != 1) ? 9 : 7;
                goto Label_0057;

            case 1:
                goto Label_01B3;

            case 2:
                get = this.get;
                while (bitk < get)
                {
                    if (num7 != 0)
                    {
                        r = 0;
                    }
                    else
                    {
                        s.bitb          = bitb;
                        s.bitk          = bitk;
                        z.avail_in      = num7;
                        z.total_in     += num6 - z.next_in_index;
                        z.next_in_index = num6;
                        s.write         = write;
                        return(s.inflate_flush(z, r));
                    }
                    num7--;
                    bitb |= (z.next_in[num6++] & 0xff) << bitk;
                    bitk += 8;
                }
                this.len       += bitb & inflate_mask[get];
                bitb            = bitb >> get;
                bitk           -= get;
                this.need       = this.dbits;
                this.tree       = this.dtree;
                this.tree_index = this.dtree_index;
                this.mode       = 3;
                goto Label_0454;

            case 3:
                goto Label_0454;

            case 4:
                get = this.get;
                while (bitk < get)
                {
                    if (num7 != 0)
                    {
                        r = 0;
                    }
                    else
                    {
                        s.bitb          = bitb;
                        s.bitk          = bitk;
                        z.avail_in      = num7;
                        z.total_in     += num6 - z.next_in_index;
                        z.next_in_index = num6;
                        s.write         = write;
                        return(s.inflate_flush(z, r));
                    }
                    num7--;
                    bitb |= (z.next_in[num6++] & 0xff) << bitk;
                    bitk += 8;
                }
                this.dist += bitb & inflate_mask[get];
                bitb       = bitb >> get;
                bitk      -= get;
                this.mode  = 5;
                goto Label_069A;

            case 5:
                goto Label_069A;

            case 6:
                if (num9 == 0)
                {
                    if ((write == s.end) && (s.read != 0))
                    {
                        write = 0;
                        num9  = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                    }
                    if (num9 == 0)
                    {
                        s.write = write;
                        r       = s.inflate_flush(z, r);
                        write   = s.write;
                        num9    = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                        if ((write == s.end) && (s.read != 0))
                        {
                            write = 0;
                            num9  = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                        }
                        if (num9 == 0)
                        {
                            s.bitb          = bitb;
                            s.bitk          = bitk;
                            z.avail_in      = num7;
                            z.total_in     += num6 - z.next_in_index;
                            z.next_in_index = num6;
                            s.write         = write;
                            return(s.inflate_flush(z, r));
                        }
                    }
                }
                r = 0;
                s.window[write++] = (byte)this.lit;
                num9--;
                this.mode = 0;
                goto Label_0057;

            case 7:
                if (bitk > 7)
                {
                    bitk -= 8;
                    num7++;
                    num6--;
                }
                s.write = write;
                r       = s.inflate_flush(z, r);
                write   = s.write;
                num9    = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                if (s.read != s.write)
                {
                    s.bitb          = bitb;
                    s.bitk          = bitk;
                    z.avail_in      = num7;
                    z.total_in     += num6 - z.next_in_index;
                    z.next_in_index = num6;
                    s.write         = write;
                    return(s.inflate_flush(z, r));
                }
                this.mode = 8;
                goto Label_0A4B;

            case 8:
                goto Label_0A4B;

            case 9:
                r               = -3;
                s.bitb          = bitb;
                s.bitk          = bitk;
                z.avail_in      = num7;
                z.total_in     += num6 - z.next_in_index;
                z.next_in_index = num6;
                s.write         = write;
                return(s.inflate_flush(z, r));

            default:
                r               = -2;
                s.bitb          = bitb;
                s.bitk          = bitk;
                z.avail_in      = num7;
                z.total_in     += num6 - z.next_in_index;
                z.next_in_index = num6;
                s.write         = write;
                return(s.inflate_flush(z, r));
            }
            this.need       = this.lbits;
            this.tree       = this.ltree;
            this.tree_index = this.ltree_index;
            this.mode       = 1;
Label_01B3:
            get = this.need;
            while (bitk < get)
            {
                if (num7 != 0)
                {
                    r = 0;
                }
                else
                {
                    s.bitb          = bitb;
                    s.bitk          = bitk;
                    z.avail_in      = num7;
                    z.total_in     += num6 - z.next_in_index;
                    z.next_in_index = num6;
                    s.write         = write;
                    return(s.inflate_flush(z, r));
                }
                num7--;
                bitb |= (z.next_in[num6++] & 0xff) << bitk;
                bitk += 8;
            }
            int index = (this.tree_index + (bitb & inflate_mask[get])) * 3;

            bitb  = bitb >> 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_0057;
            }
            if ((num3 & 0x10) != 0)
            {
                this.get  = num3 & 15;
                this.len  = this.tree[index + 2];
                this.mode = 2;
                goto Label_0057;
            }
            if ((num3 & 0x40) == 0)
            {
                this.need       = num3;
                this.tree_index = (index / 3) + this.tree[index + 2];
                goto Label_0057;
            }
            if ((num3 & 0x20) != 0)
            {
                this.mode = 7;
                goto Label_0057;
            }
            this.mode       = 9;
            z.msg           = "invalid literal/length code";
            r               = -3;
            s.bitb          = bitb;
            s.bitk          = bitk;
            z.avail_in      = num7;
            z.total_in     += num6 - z.next_in_index;
            z.next_in_index = num6;
            s.write         = write;
            return(s.inflate_flush(z, r));

Label_0454:
            get = this.need;
            while (bitk < get)
            {
                if (num7 != 0)
                {
                    r = 0;
                }
                else
                {
                    s.bitb          = bitb;
                    s.bitk          = bitk;
                    z.avail_in      = num7;
                    z.total_in     += num6 - z.next_in_index;
                    z.next_in_index = num6;
                    s.write         = write;
                    return(s.inflate_flush(z, r));
                }
                num7--;
                bitb |= (z.next_in[num6++] & 0xff) << bitk;
                bitk += 8;
            }
            index = (this.tree_index + (bitb & inflate_mask[get])) * 3;
            bitb  = bitb >> this.tree[index + 1];
            bitk -= this.tree[index + 1];
            num3  = this.tree[index];
            if ((num3 & 0x10) != 0)
            {
                this.get  = num3 & 15;
                this.dist = this.tree[index + 2];
                this.mode = 4;
                goto Label_0057;
            }
            if ((num3 & 0x40) == 0)
            {
                this.need       = num3;
                this.tree_index = (index / 3) + this.tree[index + 2];
                goto Label_0057;
            }
            this.mode       = 9;
            z.msg           = "invalid distance code";
            r               = -3;
            s.bitb          = bitb;
            s.bitk          = bitk;
            z.avail_in      = num7;
            z.total_in     += num6 - z.next_in_index;
            z.next_in_index = num6;
            s.write         = write;
            return(s.inflate_flush(z, r));

Label_069A:
            num10 = write - this.dist;
            while (num10 < 0)
            {
                num10 += s.end;
            }
            while (this.len != 0)
            {
                if (num9 == 0)
                {
                    if ((write == s.end) && (s.read != 0))
                    {
                        write = 0;
                        num9  = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                    }
                    if (num9 == 0)
                    {
                        s.write = write;
                        r       = s.inflate_flush(z, r);
                        write   = s.write;
                        num9    = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                        if ((write == s.end) && (s.read != 0))
                        {
                            write = 0;
                            num9  = (write >= s.read) ? (s.end - write) : ((s.read - write) - 1);
                        }
                        if (num9 == 0)
                        {
                            s.bitb          = bitb;
                            s.bitk          = bitk;
                            z.avail_in      = num7;
                            z.total_in     += num6 - z.next_in_index;
                            z.next_in_index = num6;
                            s.write         = write;
                            return(s.inflate_flush(z, r));
                        }
                    }
                }
                s.window[write++] = s.window[num10++];
                num9--;
                if (num10 == s.end)
                {
                    num10 = 0;
                }
                this.len--;
            }
            this.mode = 0;
            goto Label_0057;
Label_0A4B:
            r               = 1;
            s.bitb          = bitb;
            s.bitk          = bitk;
            z.avail_in      = num7;
            z.total_in     += num6 - z.next_in_index;
            z.next_in_index = num6;
            s.write         = write;
            return(s.inflate_flush(z, r));
        }
Exemple #21
0
 internal void init(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, ZStream z)
 {
     this.mode        = 0;
     this.lbits       = (byte)bl;
     this.dbits       = (byte)bd;
     this.ltree       = tl;
     this.ltree_index = tl_index;
     this.dtree       = td;
     this.dtree_index = td_index;
     this.tree        = null;
 }
Exemple #22
0
        internal int inflate(ZStream z, int f)
        {
            int r;
            int b;

            if (z == null || z.istate == null || z.next_in == null)
            {
                return(Z_STREAM_ERROR);
            }
            f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
            r = Z_BUF_ERROR;
            while (true)
            {
                //System.out.println("mode: "+z.istate.mode);
                switch (z.istate.mode)
                {
                case METHOD:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    if (((z.istate.method = z.next_in[z.next_in_index++]) & 0xf) != Z_DEFLATED)
                    {
                        z.istate.mode   = BAD;
                        z.msg           = "unknown compression method";
                        z.istate.marker = 5;           // can't try inflateSync
                        break;
                    }
                    if ((z.istate.method >> 4) + 8 > z.istate.wbits)
                    {
                        z.istate.mode   = BAD;
                        z.msg           = "invalid window size";
                        z.istate.marker = 5;           // can't try inflateSync
                        break;
                    }
                    z.istate.mode = FLAG;
                    goto case FLAG;

                case FLAG:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    b = (z.next_in[z.next_in_index++]) & 0xff;

                    if ((((z.istate.method << 8) + b) % 31) != 0)
                    {
                        z.istate.mode   = BAD;
                        z.msg           = "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.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need = ((z.next_in[z.next_in_index++] & 0xff) << 24) & 0xff000000L;
                    z.istate.mode = DICT3;
                    goto case DICT3;

                case DICT3:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need += ((z.next_in[z.next_in_index++] & 0xff) << 16) & 0xff0000L;
                    z.istate.mode  = DICT2;
                    goto case DICT2;

                case DICT2:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need += ((z.next_in[z.next_in_index++] & 0xff) << 8) & 0xff00L;
                    z.istate.mode  = DICT1;
                    goto case DICT1;

                case DICT1:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need += (z.next_in[z.next_in_index++] & 0xffL);
                    z.adler        = z.istate.need;
                    z.istate.mode  = DICT0;
                    return(Z_NEED_DICT);

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

                case BLOCKS:

                    r = z.istate.blocks.proc(z, r);
                    if (r == Z_DATA_ERROR)
                    {
                        z.istate.mode   = BAD;
                        z.istate.marker = 0;         // can try inflateSync
                        break;
                    }
                    if (r == Z_OK)
                    {
                        r = f;
                    }
                    if (r != Z_STREAM_END)
                    {
                        return(r);
                    }
                    r = f;
                    z.istate.blocks.reset(z, z.istate.was);
                    if (z.istate.nowrap != 0)
                    {
                        z.istate.mode = DONE;
                        break;
                    }
                    z.istate.mode = CHECK4;
                    goto case CHECK4;

                case CHECK4:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need = ((z.next_in[z.next_in_index++] & 0xff) << 24) & 0xff000000L;
                    z.istate.mode = CHECK3;
                    goto case CHECK3;

                case CHECK3:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need += ((z.next_in[z.next_in_index++] & 0xff) << 16) & 0xff0000L;
                    z.istate.mode  = CHECK2;
                    goto case CHECK2;

                case CHECK2:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need += ((z.next_in[z.next_in_index++] & 0xff) << 8) & 0xff00L;
                    z.istate.mode  = CHECK1;
                    goto case CHECK1;

                case CHECK1:

                    if (z.avail_in == 0)
                    {
                        return(r);
                    }
                    r = f;

                    z.avail_in--; z.total_in++;
                    z.istate.need += (z.next_in[z.next_in_index++] & 0xffL);

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

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

                case DONE:
                    return(Z_STREAM_END);

                case BAD:
                    return(Z_DATA_ERROR);

                default:
                    return(Z_STREAM_ERROR);
                }
            }
        }
Exemple #23
0
        internal void reset(ZStream z, long[] c){
            if(c!=null) c[0]=check;
            if(mode==BTREE || mode==DTREE){
            }
            if(mode==CODES){
                codes.free(z);
            }
            mode=TYPE;
            bitk=0;
            bitb=0;
            read=write=0;

            if(checkfn != null)
                z.adler=check=z._adler.adler32(0L, null, 0, 0);
        }
Exemple #24
0
        internal int proc(InfBlocks s, ZStream z, int r)
        {
            int num  = z.next_in_index;
            int num2 = z.avail_in;
            int num3 = s.bitb;
            int i    = s.bitk;
            int num4 = s.write;
            int num5 = (num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4);

            while (true)
            {
                int num6;
                switch (this.mode)
                {
                case 0:
                    if (num5 >= 258 && num2 >= 10)
                    {
                        s.bitb          = num3;
                        s.bitk          = i;
                        z.avail_in      = num2;
                        z.total_in     += (long)(num - z.next_in_index);
                        z.next_in_index = num;
                        s.write         = num4;
                        r    = this.inflate_fast((int)this.lbits, (int)this.dbits, this.ltree, this.ltree_index, this.dtree, this.dtree_index, s, z);
                        num  = z.next_in_index;
                        num2 = z.avail_in;
                        num3 = s.bitb;
                        i    = s.bitk;
                        num4 = s.write;
                        num5 = ((num4 < s.read) ? (s.read - num4 - 1) : (s.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_199;

                case 1:
                    goto IL_199;

                case 2:
                    num6 = this.get;
                    while (i < num6)
                    {
                        if (num2 == 0)
                        {
                            goto IL_34E;
                        }
                        r = 0;
                        num2--;
                        num3 |= (int)(z.next_in[num++] & 255) << i;
                        i    += 8;
                    }
                    this.len       += (num3 & InfCodes.inflate_mask[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_411;

                case 3:
                    goto IL_411;

                case 4:
                    num6 = this.get;
                    while (i < num6)
                    {
                        if (num2 == 0)
                        {
                            goto IL_595;
                        }
                        r = 0;
                        num2--;
                        num3 |= (int)(z.next_in[num++] & 255) << i;
                        i    += 8;
                    }
                    this.dist += (num3 & InfCodes.inflate_mask[num6]);
                    num3     >>= num6;
                    i         -= num6;
                    this.mode  = 5;
                    goto IL_634;

                case 5:
                    goto IL_634;

                case 6:
                    if (num5 == 0)
                    {
                        if (num4 == s.end && s.read != 0)
                        {
                            num4 = 0;
                            num5 = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
                        }
                        if (num5 == 0)
                        {
                            s.write = num4;
                            r       = s.inflate_flush(z, r);
                            num4    = s.write;
                            num5    = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
                            if (num4 == s.end && s.read != 0)
                            {
                                num4 = 0;
                                num5 = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
                            }
                            if (num5 == 0)
                            {
                                goto Block_44;
                            }
                        }
                    }
                    r = 0;
                    s.window[num4++] = (byte)this.lit;
                    num5--;
                    this.mode = 0;
                    continue;

                case 7:
                    goto IL_8DA;

                case 8:
                    goto IL_989;

                case 9:
                    goto IL_9D3;
                }
                break;
IL_199:
                num6 = this.need;
                while (i < num6)
                {
                    if (num2 == 0)
                    {
                        goto IL_1AB;
                    }
                    r = 0;
                    num2--;
                    num3 |= (int)(z.next_in[num++] & 255) << i;
                    i    += 8;
                }
                int num7 = (this.tree_index + (num3 & InfCodes.inflate_mask[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.get  = (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_2DE;
IL_411:
                num6 = this.need;
                while (i < num6)
                {
                    if (num2 == 0)
                    {
                        goto IL_423;
                    }
                    r = 0;
                    num2--;
                    num3 |= (int)(z.next_in[num++] & 255) << i;
                    i    += 8;
                }
                num7   = (this.tree_index + (num3 & InfCodes.inflate_mask[num6])) * 3;
                num3 >>= this.tree[num7 + 1];
                i     -= this.tree[num7 + 1];
                num8   = this.tree[num7];
                if ((num8 & 16) != 0)
                {
                    this.get  = (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_525;
IL_634:
                int j;
                for (j = num4 - this.dist; j < 0; j += s.end)
                {
                }
                while (this.len != 0)
                {
                    if (num5 == 0)
                    {
                        if (num4 == s.end && s.read != 0)
                        {
                            num4 = 0;
                            num5 = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
                        }
                        if (num5 == 0)
                        {
                            s.write = num4;
                            r       = s.inflate_flush(z, r);
                            num4    = s.write;
                            num5    = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
                            if (num4 == s.end && s.read != 0)
                            {
                                num4 = 0;
                                num5 = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
                            }
                            if (num5 == 0)
                            {
                                goto Block_32;
                            }
                        }
                    }
                    s.window[num4++] = s.window[j++];
                    num5--;
                    if (j == s.end)
                    {
                        j = 0;
                    }
                    this.len--;
                }
                this.mode = 0;
            }
            r               = -2;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_1AB:
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_2DE:
            this.mode       = 9;
            z.msg           = "invalid literal/length code";
            r               = -3;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_34E:
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_423:
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_525:
            this.mode       = 9;
            z.msg           = "invalid distance code";
            r               = -3;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_595:
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

Block_32:
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

Block_44:
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_8DA:
            if (i > 7)
            {
                i -= 8;
                num2++;
                num--;
            }
            s.write = num4;
            r       = s.inflate_flush(z, r);
            num4    = s.write;
            int arg_92B_0 = (num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4);

            if (s.read != s.write)
            {
                s.bitb          = num3;
                s.bitk          = i;
                z.avail_in      = num2;
                z.total_in     += (long)(num - z.next_in_index);
                z.next_in_index = num;
                s.write         = num4;
                return(s.inflate_flush(z, r));
            }
            this.mode = 8;
IL_989:
            r               = 1;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));

IL_9D3:
            r               = -3;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(s.inflate_flush(z, r));
        }
Exemple #25
0
 internal void free(ZStream z)
 {
     //  ZFREE(z, c);
 }
Exemple #26
0
        internal int inflate(ZStream z, int f)
        {
            if (z == null || z.istate == null || z.next_in == null)
            {
                return(-2);
            }
            f = ((f == 4) ? -5 : 0);
            int num = -5;

            while (true)
            {
                switch (z.istate.mode)
                {
                case 0:
                    if (z.avail_in == 0)
                    {
                        return(num);
                    }
                    num = f;
                    z.avail_in--;
                    z.total_in += 1L;
                    if (((z.istate.method = (int)z.next_in[z.next_in_index++]) & 15) != 8)
                    {
                        z.istate.mode   = 13;
                        z.msg           = "unknown compression method";
                        z.istate.marker = 5;
                        continue;
                    }
                    if ((z.istate.method >> 4) + 8 > z.istate.wbits)
                    {
                        z.istate.mode   = 13;
                        z.msg           = "invalid window size";
                        z.istate.marker = 5;
                        continue;
                    }
                    z.istate.mode = 1;
                    goto IL_144;

                case 1:
                    goto IL_144;

                case 2:
                    goto IL_1EE;

                case 3:
                    goto IL_259;

                case 4:
                    goto IL_2CB;

                case 5:
                    goto IL_33C;

                case 6:
                    goto IL_3B8;

                case 7:
                    num = z.istate.blocks.proc(z, num);
                    if (num == -3)
                    {
                        z.istate.mode   = 13;
                        z.istate.marker = 0;
                        continue;
                    }
                    if (num == 0)
                    {
                        num = f;
                    }
                    if (num != 1)
                    {
                        return(num);
                    }
                    num = f;
                    z.istate.blocks.reset(z, z.istate.was);
                    if (z.istate.nowrap != 0)
                    {
                        z.istate.mode = 12;
                        continue;
                    }
                    z.istate.mode = 8;
                    goto IL_469;

                case 8:
                    goto IL_469;

                case 9:
                    goto IL_4D5;

                case 10:
                    goto IL_548;

                case 11:
                    goto IL_5BA;

                case 12:
                    return(1);

                case 13:
                    return(-3);
                }
                break;
IL_144:
                if (z.avail_in == 0)
                {
                    return(num);
                }
                num = f;
                z.avail_in--;
                z.total_in += 1L;
                int num2 = (int)(z.next_in[z.next_in_index++] & 255);
                if (((z.istate.method << 8) + num2) % 31 != 0)
                {
                    z.istate.mode   = 13;
                    z.msg           = "incorrect header check";
                    z.istate.marker = 5;
                    continue;
                }
                if ((num2 & 32) == 0)
                {
                    z.istate.mode = 7;
                    continue;
                }
                goto IL_1E2;
IL_5BA:
                if (z.avail_in == 0)
                {
                    return(num);
                }
                num = f;
                z.avail_in--;
                z.total_in    += 1L;
                z.istate.need += (long)((ulong)z.next_in[z.next_in_index++] & 255uL);
                if ((int)z.istate.was[0] != (int)z.istate.need)
                {
                    z.istate.mode   = 13;
                    z.msg           = "incorrect data check";
                    z.istate.marker = 5;
                    continue;
                }
                goto IL_65C;
IL_548:
                if (z.avail_in == 0)
                {
                    return(num);
                }
                num = f;
                z.avail_in--;
                z.total_in    += 1L;
                z.istate.need += ((long)((long)(z.next_in[z.next_in_index++] & 255) << 8) & 65280L);
                z.istate.mode  = 11;
                goto IL_5BA;
IL_4D5:
                if (z.avail_in == 0)
                {
                    return(num);
                }
                num = f;
                z.avail_in--;
                z.total_in    += 1L;
                z.istate.need += ((long)((long)(z.next_in[z.next_in_index++] & 255) << 16) & 16711680L);
                z.istate.mode  = 10;
                goto IL_548;
IL_469:
                if (z.avail_in == 0)
                {
                    return(num);
                }
                num = f;
                z.avail_in--;
                z.total_in   += 1L;
                z.istate.need = ((long)((long)(z.next_in[z.next_in_index++] & 255) << 24) & (long)((ulong)-16777216));
                z.istate.mode = 9;
                goto IL_4D5;
            }
            return(-2);

IL_1E2:
            z.istate.mode = 2;
IL_1EE:
            if (z.avail_in == 0)
            {
                return(num);
            }
            num = f;
            z.avail_in--;
            z.total_in   += 1L;
            z.istate.need = ((long)((long)(z.next_in[z.next_in_index++] & 255) << 24) & (long)((ulong)-16777216));
            z.istate.mode = 3;
IL_259:
            if (z.avail_in == 0)
            {
                return(num);
            }
            num = f;
            z.avail_in--;
            z.total_in    += 1L;
            z.istate.need += ((long)((long)(z.next_in[z.next_in_index++] & 255) << 16) & 16711680L);
            z.istate.mode  = 4;
IL_2CB:
            if (z.avail_in == 0)
            {
                return(num);
            }
            num = f;
            z.avail_in--;
            z.total_in    += 1L;
            z.istate.need += ((long)((long)(z.next_in[z.next_in_index++] & 255) << 8) & 65280L);
            z.istate.mode  = 5;
IL_33C:
            if (z.avail_in == 0)
            {
                return(num);
            }
            z.avail_in--;
            z.total_in    += 1L;
            z.istate.need += (long)((ulong)z.next_in[z.next_in_index++] & 255uL);
            z.adler        = z.istate.need;
            z.istate.mode  = 6;
            return(2);

IL_3B8:
            z.istate.mode   = 13;
            z.msg           = "need dictionary";
            z.istate.marker = 0;
            return(-2);

IL_65C:
            z.istate.mode = 12;
            return(1);
        }
Exemple #27
0
        // 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 inflate_fast(int bl, int bd,
                                  int[] tl, int tl_index,
                                  int[] td, int td_index,
                                  InfBlocks s, ZStream 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.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
            q = s.write; m = q < s.read?s.read - q - 1:s.end - q;

            // initialize masks
            ml = inflate_mask[bl];
            md = inflate_mask[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.next_in[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 & inflate_mask[e]);

                        b >>= e; k -= e;

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

                                d = tp[tp_index_t_3 + 2] + (b & inflate_mask[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
                                    {
                                        System.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
                                        {
                                            System.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
                                {
                                    System.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 & inflate_mask[e]);
                                tp_index_t_3 = (tp_index + t) * 3;
                                e            = tp[tp_index_t_3];
                            }
                            else
                            {
                                z.msg = "invalid distance code";

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

                                s.bitb     = b; s.bitk = k;
                                z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                s.write    = q;

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

                    if ((e & 64) == 0)
                    {
                        t           += tp[tp_index_t_3 + 2];
                        t           += (b & inflate_mask[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.avail_in - n; c = (k >> 3) < c?k >> 3:c; n += c; p -= c; k -= c << 3;

                        s.bitb     = b; s.bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        s.write    = q;

                        return(Z_STREAM_END);
                    }
                    else
                    {
                        z.msg = "invalid literal/length code";

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

                        s.bitb     = b; s.bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        s.write    = q;

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

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

            s.bitb     = b; s.bitk = k;
            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
            s.write    = q;

            return(Z_OK);
        }
Exemple #28
0
        internal int deflate(ZStream strm, int flush){
            int old_flush;

            if(flush>Z_FINISH || flush<0){
                return Z_STREAM_ERROR;
            }

            if(strm.next_out == null ||
                (strm.next_in == null && strm.avail_in != 0) ||
                (status == FINISH_STATE && flush != Z_FINISH)) {
                strm.msg=z_errmsg[Z_NEED_DICT-(Z_STREAM_ERROR)];
                return Z_STREAM_ERROR;
            }
            if(strm.avail_out == 0){
                strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
                return Z_BUF_ERROR;
            }

            this.strm = strm; // just in case
            old_flush = last_flush;
            last_flush = flush;

            // Write the zlib header
            if(status == INIT_STATE) {
                int header = (Z_DEFLATED+((w_bits-8)<<4))<<8;
                int level_flags=((level-1)&0xff)>>1;

                if(level_flags>3) level_flags=3;
                header |= (level_flags<<6);
                if(strstart!=0) header |= PRESET_DICT;
                header+=31-(header % 31);

                status=BUSY_STATE;
                putShortMSB(header);


                // Save the adler32 of the preset dictionary:
                if(strstart!=0){
                    putShortMSB((int)(strm.adler>>16));
                    putShortMSB((int)(strm.adler&0xffff));
                }
                strm.adler=strm._adler.adler32(0, null, 0, 0);
            }

            // Flush as much pending output as possible
            if(pending != 0) {
                strm.flush_pending();
                if(strm.avail_out == 0) {
                    //System.out.println("  avail_out==0");
                    // Since avail_out is 0, deflate will be called again with
                    // more output space, but possibly with both pending and
                    // avail_in equal to zero. There won't be anything to do,
                    // but this is not an error situation so make sure we
                    // return OK instead of BUF_ERROR at next call of deflate:
                    last_flush = -1;
                    return Z_OK;
                }

                // Make sure there is something to do and avoid duplicate consecutive
                // flushes. For repeated and useless calls with Z_FINISH, we keep
                // returning Z_STREAM_END instead of Z_BUFF_ERROR.
            }
            else if(strm.avail_in==0 && flush <= old_flush &&
                flush != Z_FINISH) {
                strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
                return Z_BUF_ERROR;
            }

            // User must not provide more input after the first FINISH:
            if(status == FINISH_STATE && strm.avail_in != 0) {
                strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
                return Z_BUF_ERROR;
            }

            // Start a new block or continue the current one.
            if(strm.avail_in!=0 || lookahead!=0 ||
                (flush != Z_NO_FLUSH && status != FINISH_STATE)) {
                int bstate=-1;
                switch(config_table[level].func){
                    case STORED: 
                        bstate = deflate_stored(flush);
                        break;
                    case FAST: 
                        bstate = deflate_fast(flush);
                        break;
                    case SLOW: 
                        bstate = deflate_slow(flush);
                        break;
                    default:
                        break;
                }

                if (bstate==FinishStarted || bstate==FinishDone) {
                    status = FINISH_STATE;
                }
                if (bstate==NeedMore || bstate==FinishStarted) {
                    if(strm.avail_out == 0) {
                        last_flush = -1; // avoid BUF_ERROR next call, see above
                    }
                    return Z_OK;
                    // If flush != Z_NO_FLUSH && avail_out == 0, the next call
                    // of deflate should use the same flush parameter to make sure
                    // that the flush is complete. So we don't have to output an
                    // empty block here, this will be done at next call. This also
                    // ensures that for a very small output buffer, we emit at most
                    // one empty block.
                }

                if (bstate==BlockDone) {
                    if(flush == Z_PARTIAL_FLUSH) {
                        _tr_align();
                    } 
                    else { // FULL_FLUSH or SYNC_FLUSH
                        _tr_stored_block(0, 0, false);
                        // For a full flush, this empty block will be recognized
                        // as a special marker by inflate_sync().
                        if(flush == Z_FULL_FLUSH) {
                            //state.head[s.hash_size-1]=0;
                            for(int i=0; i<hash_size/*-1*/; i++)  // forget history
                                head[i]=0;
                        }
                    }
                    strm.flush_pending();
                    if(strm.avail_out == 0) {
                        last_flush = -1; // avoid BUF_ERROR at next call, see above
                        return Z_OK;
                    }
                }
            }

            if(flush!=Z_FINISH) return Z_OK;
            if(noheader!=0) return Z_STREAM_END;

            // Write the zlib trailer (adler32)
            putShortMSB((int)(strm.adler>>16));
            putShortMSB((int)(strm.adler&0xffff));
            strm.flush_pending();

            // If avail_out is zero, the application will call deflate again
            // to flush the rest.
            noheader = -1; // write the trailer only once!
            return pending != 0 ? Z_OK : Z_STREAM_END;
        }
Exemple #29
0
        internal int proc(ZStream z, int r)
        {
            int t;              // temporary storage
            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
                // copy input/output information to locals (UPDATE macro restores)
                p = z.next_in_index; n = z.avail_in; b = bitb; k = bitk;
            } {
                q = write; m = (int)(q < read?read - q - 1:end - q);
            }

            // process input based on current state
            while (true)
            {
                switch (mode)
                {
                case TYPE:

                    while (k < (3))
                    {
                        if (n != 0)
                        {
                            r = Z_OK;
                        }
                        else
                        {
                            bitb        = b; bitk = k;
                            z.avail_in  = n;
                            z.total_in += p - z.next_in_index; z.next_in_index = p;
                            write       = q;
                            return(inflate_flush(z, r));
                        };
                        n--;
                        b |= (z.next_in[p++] & 0xff) << k;
                        k += 8;
                    }
                    t    = (int)(b & 7);
                    last = t & 1;

                    switch (t >> 1)
                    {
                    case 0: {                             // stored
                        b >>= (3); k -= (3);
                    }
                        t = k & 7; {                        // go to byte boundary
                            b >>= (t); k -= (t);
                        }
                        mode = LENS;                      // get length of stored block
                        break;

                    case 1: {                             // fixed
                        int[]   bl = new int[1];
                        int[]   bd = new int[1];
                        int[][] tl = new int[1][];
                        int[][] td = new int[1][];

                        InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
                        codes.init(bl[0], bd[0], tl[0], 0, td[0], 0, z);
                    } {
                            b >>= (3); k -= (3);
                    }

                        mode = CODES;
                        break;

                    case 2: {                             // dynamic
                        b >>= (3); k -= (3);
                    }

                        mode = TABLE;
                        break;

                    case 3: {                             // illegal
                        b >>= (3); k -= (3);
                    }
                        mode  = BAD;
                        z.msg = "invalid block type";
                        r     = Z_DATA_ERROR;

                        bitb       = b; bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        write      = q;
                        return(inflate_flush(z, r));
                    }
                    break;

                case LENS:

                    while (k < (32))
                    {
                        if (n != 0)
                        {
                            r = Z_OK;
                        }
                        else
                        {
                            bitb        = b; bitk = k;
                            z.avail_in  = n;
                            z.total_in += p - z.next_in_index; z.next_in_index = p;
                            write       = q;
                            return(inflate_flush(z, r));
                        };
                        n--;
                        b |= (z.next_in[p++] & 0xff) << k;
                        k += 8;
                    }

                    if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
                    {
                        mode  = BAD;
                        z.msg = "invalid stored block lengths";
                        r     = Z_DATA_ERROR;

                        bitb       = b; bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        write      = q;
                        return(inflate_flush(z, r));
                    }
                    left = (b & 0xffff);
                    b    = k = 0;                        // dump bits
                    mode = left != 0 ? STORED : (last != 0 ? DRY : TYPE);
                    break;

                case STORED:
                    if (n == 0)
                    {
                        bitb       = b; bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        write      = q;
                        return(inflate_flush(z, r));
                    }

                    if (m == 0)
                    {
                        if (q == end && read != 0)
                        {
                            q = 0; m = (int)(q < read?read - q - 1:end - q);
                        }
                        if (m == 0)
                        {
                            write = q;
                            r     = inflate_flush(z, r);
                            q     = write; m = (int)(q < read?read - q - 1:end - q);
                            if (q == end && read != 0)
                            {
                                q = 0; m = (int)(q < read?read - q - 1:end - q);
                            }
                            if (m == 0)
                            {
                                bitb       = b; bitk = k;
                                z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                write      = q;
                                return(inflate_flush(z, r));
                            }
                        }
                    }
                    r = Z_OK;

                    t = left;
                    if (t > n)
                    {
                        t = n;
                    }
                    if (t > m)
                    {
                        t = m;
                    }
                    System.Array.Copy(z.next_in, p, window, q, t);
                    p += t;  n -= t;
                    q += t;  m -= t;
                    if ((left -= t) != 0)
                    {
                        break;
                    }
                    mode = last != 0 ? DRY : TYPE;
                    break;

                case TABLE:

                    while (k < (14))
                    {
                        if (n != 0)
                        {
                            r = Z_OK;
                        }
                        else
                        {
                            bitb        = b; bitk = k;
                            z.avail_in  = n;
                            z.total_in += p - z.next_in_index; z.next_in_index = p;
                            write       = q;
                            return(inflate_flush(z, r));
                        };
                        n--;
                        b |= (z.next_in[p++] & 0xff) << k;
                        k += 8;
                    }

                    table = t = (b & 0x3fff);
                    if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
                    {
                        mode  = BAD;
                        z.msg = "too many length or distance symbols";
                        r     = Z_DATA_ERROR;

                        bitb       = b; bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        write      = q;
                        return(inflate_flush(z, r));
                    }
                    t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
                    if (blens == null || blens.Length < t)
                    {
                        blens = new int[t];
                    }
                    else
                    {
                        for (int i = 0; i < t; i++)
                        {
                            blens[i] = 0;
                        }
                    } {
                        b >>= (14); k -= (14);
                    }

                    index = 0;
                    mode  = BTREE;
                    goto case BTREE;

                case BTREE:
                    while (index < 4 + (table >> 10))
                    {
                        while (k < (3))
                        {
                            if (n != 0)
                            {
                                r = Z_OK;
                            }
                            else
                            {
                                bitb        = b; bitk = k;
                                z.avail_in  = n;
                                z.total_in += p - z.next_in_index; z.next_in_index = p;
                                write       = q;
                                return(inflate_flush(z, r));
                            };
                            n--;
                            b |= (z.next_in[p++] & 0xff) << k;
                            k += 8;
                        }

                        blens[border[index++]] = b & 7; {
                            b >>= (3); k -= (3);
                        }
                    }

                    while (index < 19)
                    {
                        blens[border[index++]] = 0;
                    }

                    bb[0] = 7;
                    t     = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
                    if (t != Z_OK)
                    {
                        r = t;
                        if (r == Z_DATA_ERROR)
                        {
                            blens = null;
                            mode  = BAD;
                        }

                        bitb       = b; bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        write      = q;
                        return(inflate_flush(z, r));
                    }

                    index = 0;
                    mode  = DTREE;
                    goto case DTREE;

                case DTREE:
                    while (true)
                    {
                        t = table;
                        if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)))
                        {
                            break;
                        }

                        int i, j, c;

                        t = bb[0];

                        while (k < (t))
                        {
                            if (n != 0)
                            {
                                r = Z_OK;
                            }
                            else
                            {
                                bitb        = b; bitk = k;
                                z.avail_in  = n;
                                z.total_in += p - z.next_in_index; z.next_in_index = p;
                                write       = q;
                                return(inflate_flush(z, r));
                            };
                            n--;
                            b |= (z.next_in[p++] & 0xff) << k;
                            k += 8;
                        }

                        if (tb[0] == -1)
                        {
                            //System.err.println("null...");
                        }

                        t = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 1];
                        c = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 2];

                        if (c < 16)
                        {
                            b >>= (t); k -= (t);
                            blens[index++] = c;
                        }
                        else       // c == 16..18
                        {
                            i = c == 18 ? 7 : c - 14;
                            j = c == 18 ? 11 : 3;

                            while (k < (t + i))
                            {
                                if (n != 0)
                                {
                                    r = Z_OK;
                                }
                                else
                                {
                                    bitb        = b; bitk = k;
                                    z.avail_in  = n;
                                    z.total_in += p - z.next_in_index; z.next_in_index = p;
                                    write       = q;
                                    return(inflate_flush(z, r));
                                };
                                n--;
                                b |= (z.next_in[p++] & 0xff) << k;
                                k += 8;
                            }

                            b >>= (t); k -= (t);

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

                            b >>= (i); k -= (i);

                            i = index;
                            t = table;
                            if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
                                (c == 16 && i < 1))
                            {
                                blens = null;
                                mode  = BAD;
                                z.msg = "invalid bit length repeat";
                                r     = Z_DATA_ERROR;

                                bitb       = b; bitk = k;
                                z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                                write      = q;
                                return(inflate_flush(z, r));
                            }

                            c = c == 16 ? blens[i - 1] : 0;
                            do
                            {
                                blens[i++] = c;
                            }while (--j != 0);
                            index = i;
                        }
                    }

                    tb[0] = -1; {
                        int[] bl = new int[1];
                        int[] bd = new int[1];
                        int[] tl = new int[1];
                        int[] td = new int[1];
                        bl[0] = 9;         // must be <= 9 for lookahead assumptions
                        bd[0] = 6;         // must be <= 9 for lookahead assumptions

                        t = table;
                        t = inftree.inflate_trees_dynamic(257 + (t & 0x1f),
                                                          1 + ((t >> 5) & 0x1f),
                                                          blens, bl, bd, tl, td, hufts, z);

                        if (t != Z_OK)
                        {
                            if (t == Z_DATA_ERROR)
                            {
                                blens = null;
                                mode  = BAD;
                            }
                            r = t;

                            bitb       = b; bitk = k;
                            z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                            write      = q;
                            return(inflate_flush(z, r));
                        }
                        codes.init(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
                    }
                    mode = CODES;
                    goto case CODES;

                case CODES:
                    bitb       = b; bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    write      = q;

                    if ((r = codes.proc(this, z, r)) != Z_STREAM_END)
                    {
                        return(inflate_flush(z, r));
                    }
                    r = Z_OK;
                    codes.free(z);

                    p = z.next_in_index; n = z.avail_in; b = bitb; k = bitk;
                    q = write; m = (int)(q < read?read - q - 1:end - q);

                    if (last == 0)
                    {
                        mode = TYPE;
                        break;
                    }
                    mode = DRY;
                    goto case DRY;

                case DRY:
                    write = q;
                    r     = inflate_flush(z, r);
                    q     = write; m = (int)(q < read?read - q - 1:end - q);
                    if (read != write)
                    {
                        bitb       = b; bitk = k;
                        z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                        write      = q;
                        return(inflate_flush(z, r));
                    }
                    mode = DONE;
                    goto case DONE;

                case DONE:
                    r = Z_STREAM_END;

                    bitb       = b; bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    write      = q;
                    return(inflate_flush(z, r));

                case BAD:
                    r = Z_DATA_ERROR;

                    bitb       = b; bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    write      = q;
                    return(inflate_flush(z, r));

                default:
                    r = Z_STREAM_ERROR;

                    bitb       = b; bitk = k;
                    z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
                    write      = q;
                    return(inflate_flush(z, r));
                }
            }
        }
Exemple #30
0
        public ZLibStatus InflateInit(ZStream z, int w)
        {
            base.msg = 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)
            {
                InflateEnd(z);
                return ZLibStatus.Z_STREAM_ERROR;
            }
            _wbits = w;

            this._blocks = new InflateBlocks(z,
                this._nowrap != 0 ? null : this,
                1 << w);

            // reset state
            InflateReset();
            return ZLibStatus.Z_OK;
        }
Exemple #31
0
        // copy as much as possible from the sliding window to the output area
        internal int inflate_flush(ZStream z, int r)
        {
            int n;
            int p;
            int q;

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

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

            // update counters
            z.avail_out -= n;
            z.total_out += n;

            // update check information
            if (checkfn != null)
            {
                z.adler = check = z._adler.adler32(check, window, q, n);
            }

            // copy as far as end of window
            System.Array.Copy(window, q, z.next_out, 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.avail_out)
                {
                    n = z.avail_out;
                }
                if (n != 0 && r == Z_BUF_ERROR)
                {
                    r = Z_OK;
                }

                // update counters
                z.avail_out -= n;
                z.total_out += n;

                // update check information
                if (checkfn != null)
                {
                    z.adler = check = z._adler.adler32(check, window, q, n);
                }

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

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

            // done
            return(r);
        }
Exemple #32
0
        internal int inflate(ZStream z, int f)
        {
            int r;
            int b;

            if(z == null || z.istate == null || z.next_in == null)
                return Z_STREAM_ERROR;
            f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
            r = Z_BUF_ERROR;
            while (true){
                //System.out.println("mode: "+z.istate.mode);
                switch (z.istate.mode){
                    case METHOD:

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

                        z.avail_in--; z.total_in++;
                        if(((z.istate.method = z.next_in[z.next_in_index++])&0xf)!=Z_DEFLATED){
                            z.istate.mode = BAD;
                            z.msg="unknown compression method";
                            z.istate.marker = 5;       // can't try inflateSync
                            break;
                        }
                        if((z.istate.method>>4)+8>z.istate.wbits){
                            z.istate.mode = BAD;
                            z.msg="invalid window size";
                            z.istate.marker = 5;       // can't try inflateSync
                            break;
                        }
                        z.istate.mode=FLAG;
                        goto case FLAG;
                    case FLAG:

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

                        z.avail_in--; z.total_in++;
                        b = (z.next_in[z.next_in_index++])&0xff;

                        if((((z.istate.method << 8)+b) % 31)!=0){
                            z.istate.mode = BAD;
                            z.msg = "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.avail_in==0)return r;r=f;

                        z.avail_in--; z.total_in++;
                        z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
                        z.istate.mode=DICT3;
                        goto case DICT3;
                    case DICT3:

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

                        z.avail_in--; z.total_in++;
                        z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
                        z.istate.mode=DICT2;
                        goto case DICT2;
                    case DICT2:

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

                        z.avail_in--; z.total_in++;
                        z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
                        z.istate.mode=DICT1;
                        goto case DICT1;
                    case DICT1:

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

                        z.avail_in--; z.total_in++;
                        z.istate.need += (z.next_in[z.next_in_index++]&0xffL);
                        z.adler = z.istate.need;
                        z.istate.mode = DICT0;
                        return Z_NEED_DICT;
                    case DICT0:
                        z.istate.mode = BAD;
                        z.msg = "need dictionary";
                        z.istate.marker = 0;       // can try inflateSync
                        return Z_STREAM_ERROR;
                    case BLOCKS:

                        r = z.istate.blocks.proc(z, r);
                        if(r == Z_DATA_ERROR){
                            z.istate.mode = BAD;
                            z.istate.marker = 0;     // can try inflateSync
                            break;
                        }
                        if(r == Z_OK){
                            r = f;
                        }
                        if(r != Z_STREAM_END){
                            return r;
                        }
                        r = f;
                        z.istate.blocks.reset(z, z.istate.was);
                        if(z.istate.nowrap!=0){
                            z.istate.mode=DONE;
                            break;
                        }
                        z.istate.mode=CHECK4;
                        goto case CHECK4;
                    case CHECK4:

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

                        z.avail_in--; z.total_in++;
                        z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
                        z.istate.mode=CHECK3;
                        goto case CHECK3;
                    case CHECK3:

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

                        z.avail_in--; z.total_in++;
                        z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
                        z.istate.mode = CHECK2;
                        goto case CHECK2;
                    case CHECK2:

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

                        z.avail_in--; z.total_in++;
                        z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
                        z.istate.mode = CHECK1;
                        goto case CHECK1;
                    case CHECK1:

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

                        z.avail_in--; z.total_in++;
                        z.istate.need+=(z.next_in[z.next_in_index++]&0xffL);

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

                        z.istate.mode = DONE;
                        goto case DONE;
                    case DONE:
                        return Z_STREAM_END;
                    case BAD:
                        return Z_DATA_ERROR;
                    default:
                        return Z_STREAM_ERROR;
                }
            }
        }
Exemple #33
0
        internal int inflateSync(ZStream 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 Z_STREAM_ERROR;
            if(z.istate.mode != BAD){
                z.istate.mode = BAD;
                z.istate.marker = 0;
            }
            if((n=z.avail_in)==0)
                return Z_BUF_ERROR;
            p=z.next_in_index;
            m=z.istate.marker;

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

            // restore
            z.total_in += p-z.next_in_index;
            z.next_in_index = p;
            z.avail_in = n;
            z.istate.marker = m;

            // return no joy or set up to restart on a new block
            if(m != 4){
                return Z_DATA_ERROR;
            }
            r=z.total_in;  w=z.total_out;
            inflateReset(z);
            z.total_in=r;  z.total_out = w;
            z.istate.mode = BLOCKS;
            return Z_OK;
        }
Exemple #34
0
        internal int inflateInit(ZStream z, int w)
        {
            z.msg = 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){
                inflateEnd(z);
                return Z_STREAM_ERROR;
            }
            wbits=w;

            z.istate.blocks=new InfBlocks(z,
                z.istate.nowrap!=0 ? null : this,
                1<<w);

            // reset state
            inflateReset(z);
            return Z_OK;
        }
Exemple #35
0
        public virtual void End()
		{
			if (z == null)
				return;
			if (compress)
				z.deflateEnd();
			else
				z.inflateEnd();
			z.free();
			z = null;
		}
Exemple #36
0
        internal int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength)
        {
            int index=0;
            int length = dictLength;
            if(z==null || z.istate == null|| z.istate.mode != DICT0)
                return Z_STREAM_ERROR;

            if(z._adler.adler32(1L, dictionary, 0, dictLength)!=z.adler){
                return Z_DATA_ERROR;
            }

            z.adler = z._adler.adler32(0, null, 0, 0);

            if(length >= (1<<z.istate.wbits)){
                length = (1<<z.istate.wbits)-1;
                index=dictLength - length;
            }
            z.istate.blocks.set_dictionary(dictionary, index, length);
            z.istate.mode = BLOCKS;
            return Z_OK;
        }
Exemple #37
0
 internal int deflateInit(ZStream strm, int level)
 {
     return(deflateInit(strm, level, 15));
 }
Exemple #38
0
 // 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 inflateSyncPoint(ZStream z)
 {
     if(z == null || z.istate == null || z.istate.blocks == null)
         return Z_STREAM_ERROR;
     return z.istate.blocks.sync_point();
 }
 public void End() {
     if(z==null)
         return;
     z.deflateEnd();
     z.free();
     z=null;
 }
Exemple #40
0
 private static ZStream GetDefaultZStream(bool nowrap)
 {
     ZStream z = new ZStream();
     z.inflateInit(nowrap);
     return z;
 }
Exemple #41
0
        internal int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InfBlocks s, ZStream z)
        {
            int num  = z.next_in_index;
            int num2 = z.avail_in;
            int num3 = s.bitb;
            int i    = s.bitk;
            int num4 = s.write;
            int num5 = (num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4);
            int num6 = InfCodes.inflate_mask[bl];
            int num7 = InfCodes.inflate_mask[bd];
            int num10;
            int num11;

            while (true)
            {
                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
                    {
                        while (true)
                        {
                            num3 >>= tl[num9 + 1];
                            i     -= tl[num9 + 1];
                            if ((num10 & 16) != 0)
                            {
                                break;
                            }
                            if ((num10 & 64) != 0)
                            {
                                goto IL_4B3;
                            }
                            num8 += tl[num9 + 2];
                            num8 += (num3 & InfCodes.inflate_mask[num10]);
                            num9  = (tl_index + num8) * 3;
                            if ((num10 = tl[num9]) == 0)
                            {
                                goto Block_20;
                            }
                        }
                        num10 &= 15;
                        num11  = tl[num9 + 2] + (num3 & InfCodes.inflate_mask[num10]);
                        num3 >>= num10;
                        for (i -= num10; i < 15; i += 8)
                        {
                            num2--;
                            num3 |= (int)(z.next_in[num++] & 255) << i;
                        }
                        num8  = (num3 & num7);
                        num9  = (td_index + num8) * 3;
                        num10 = td[num9];
                        while (true)
                        {
                            num3 >>= td[num9 + 1];
                            i     -= td[num9 + 1];
                            if ((num10 & 16) != 0)
                            {
                                break;
                            }
                            if ((num10 & 64) != 0)
                            {
                                goto IL_3C1;
                            }
                            num8 += td[num9 + 2];
                            num8 += (num3 & InfCodes.inflate_mask[num10]);
                            num9  = (td_index + num8) * 3;
                            num10 = td[num9];
                        }
                        num10 &= 15;
                        while (i < num10)
                        {
                            num2--;
                            num3 |= (int)(z.next_in[num++] & 255) << i;
                            i    += 8;
                        }
                        int num12 = td[num9 + 2] + (num3 & InfCodes.inflate_mask[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_5C0;
                        }
                        Array.Copy(s.window, num13, s.window, num4, num11);
                        num4  += num11;
                        num13 += num11;
                        goto IL_5C0;
Block_20:
                        num3           >>= tl[num9 + 1];
                        i               -= tl[num9 + 1];
                        s.window[num4++] = (byte)tl[num9 + 2];
                        num5--;
                    }
IL_5C0:
                    if (num5 < 258 || num2 < 10)
                    {
                        goto IL_5D2;
                    }
                }
                else
                {
                    num2--;
                    num3 |= (int)(z.next_in[num++] & 255) << i;
                    i    += 8;
                }
            }
IL_3C1:
            z.msg           = "invalid distance code";
            num11           = z.avail_in - num2;
            num11           = ((i >> 3 < num11) ? (i >> 3) : num11);
            num2           += num11;
            num            -= num11;
            i              -= num11 << 3;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(-3);

IL_4B3:
            if ((num10 & 32) != 0)
            {
                num11           = z.avail_in - num2;
                num11           = ((i >> 3 < num11) ? (i >> 3) : num11);
                num2           += num11;
                num            -= num11;
                i              -= num11 << 3;
                s.bitb          = num3;
                s.bitk          = i;
                z.avail_in      = num2;
                z.total_in     += (long)(num - z.next_in_index);
                z.next_in_index = num;
                s.write         = num4;
                return(1);
            }
            z.msg           = "invalid literal/length code";
            num11           = z.avail_in - num2;
            num11           = ((i >> 3 < num11) ? (i >> 3) : num11);
            num2           += num11;
            num            -= num11;
            i              -= num11 << 3;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(-3);

IL_5D2:
            num11           = z.avail_in - num2;
            num11           = ((i >> 3 < num11) ? (i >> 3) : num11);
            num2           += num11;
            num            -= num11;
            i              -= num11 << 3;
            s.bitb          = num3;
            s.bitk          = i;
            z.avail_in      = num2;
            z.total_in     += (long)(num - z.next_in_index);
            z.next_in_index = num;
            s.write         = num4;
            return(0);
        }
Exemple #42
0
		public ZOutputStream(Stream output, int level, bool nowrap)
			: base()
		{
			Debug.Assert(output.CanWrite);

			this.output = output;
            this.compress = true;
            this.z = new ZStream();
			this.z.deflateInit(level, nowrap);
		}
Exemple #43
0
 internal void free(ZStream z){
     reset(z, null);
     window=null;
     hufts=null;
     //ZFREE(z, s);
 }
Exemple #44
0
 internal InfBlocks(ZStream z, Object checkfn, int w){
     hufts=new int[MANY*3];
     window=new byte[w];
     end=w;
     this.checkfn = checkfn;
     mode = TYPE;
     reset(z, null);
 }
Exemple #45
0
 internal void init(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, ZStream z)
 {
     mode        = 0;
     lbits       = (byte)bl;
     dbits       = (byte)bd;
     ltree       = tl;
     ltree_index = tl_index;
     dtree       = td;
     dtree_index = td_index;
     tree        = null;
 }
Exemple #46
0
        internal int proc(ZStream z, int r){
            int t;              // temporary storage
            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

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

            // process input based on current state
            while(true){
                switch (mode){
                    case TYPE:

                        while(k<(3)){
                            if(n!=0){
                                r=Z_OK;
                            }
                            else{
                                bitb=b; bitk=k; 
                                z.avail_in=n;
                                z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                write=q;
                                return inflate_flush(z,r);
                            };
                            n--;
                            b|=(z.next_in[p++]&0xff)<<k;
                            k+=8;
                        }
                        t = (int)(b & 7);
                        last = t & 1;

                    switch (t >> 1){
                        case 0: {                         // stored 
           b>>=(3);k-=(3);}
                            t = k & 7; {                    // go to byte boundary

           b>>=(t);k-=(t);}
                            mode = LENS;                  // get length of stored block
                            break;
                        case 1: {                         // fixed
                            int[] bl=new int[1];
                            int[] bd=new int[1];
                            int[][] tl=new int[1][];
                            int[][] td=new int[1][];

                            InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
                            codes.init(bl[0], bd[0], tl[0], 0, td[0], 0, z);
                        } {

           b>>=(3);k-=(3);}

                            mode = CODES;
                            break;
                        case 2: {                         // dynamic

           b>>=(3);k-=(3);}

                            mode = TABLE;
                            break;
                        case 3: {                         // illegal

           b>>=(3);k-=(3);}
                            mode = BAD;
                            z.msg = "invalid block type";
                            r = Z_DATA_ERROR;

                            bitb=b; bitk=k; 
                            z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                            write=q;
                            return inflate_flush(z,r);
                    }
                        break;
                    case LENS:

                        while(k<(32)){
                            if(n!=0){
                                r=Z_OK;
                            }
                            else{
                                bitb=b; bitk=k; 
                                z.avail_in=n;
                                z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                write=q;
                                return inflate_flush(z,r);
                            };
                            n--;
                            b|=(z.next_in[p++]&0xff)<<k;
                            k+=8;
                        }

                        if ((((~b) >> 16) & 0xffff) != (b & 0xffff)){
                            mode = BAD;
                            z.msg = "invalid stored block lengths";
                            r = Z_DATA_ERROR;

                            bitb=b; bitk=k; 
                            z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                            write=q;
                            return inflate_flush(z,r);
                        }
                        left = (b & 0xffff);
                        b = k = 0;                       // dump bits
                        mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE);
                        break;
                    case STORED:
                        if (n == 0){
                            bitb=b; bitk=k; 
                            z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                            write=q;
                            return inflate_flush(z,r);
                        }

                        if(m==0){
                            if(q==end&&read!=0){
                                q=0; m=(int)(q<read?read-q-1:end-q);
                            }
                            if(m==0){
                                write=q; 
                                r=inflate_flush(z,r);
                                q=write;m=(int)(q<read?read-q-1:end-q);
                                if(q==end&&read!=0){
                                    q=0; m=(int)(q<read?read-q-1:end-q);
                                }
                                if(m==0){
                                    bitb=b; bitk=k; 
                                    z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                    write=q;
                                    return inflate_flush(z,r);
                                }
                            }
                        }
                        r=Z_OK;

                        t = left;
                        if(t>n) t = n;
                        if(t>m) t = m;
                        System.Array.Copy(z.next_in, p, window, q, t);
                        p += t;  n -= t;
                        q += t;  m -= t;
                        if ((left -= t) != 0)
                            break;
                        mode = last!=0 ? DRY : TYPE;
                        break;
                    case TABLE:

                        while(k<(14)){
                            if(n!=0){
                                r=Z_OK;
                            }
                            else{
                                bitb=b; bitk=k; 
                                z.avail_in=n;
                                z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                write=q;
                                return inflate_flush(z,r);
                            };
                            n--;
                            b|=(z.next_in[p++]&0xff)<<k;
                            k+=8;
                        }

                        table = t = (b & 0x3fff);
                        if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
                            mode = BAD;
                            z.msg = "too many length or distance symbols";
                            r = Z_DATA_ERROR;

                            bitb=b; bitk=k; 
                            z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                            write=q;
                            return inflate_flush(z,r);
                        }
                        t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
                        if(blens==null || blens.Length<t){
                            blens=new int[t];
                        }
                        else{
                            for(int i=0; i<t; i++){blens[i]=0;}
                        } {

	 b>>=(14);k-=(14);}

                        index = 0;
                        mode = BTREE;
                        goto case BTREE;
                    case BTREE:
                        while (index < 4 + (table >> 10)){
                            while(k<(3)){
                                if(n!=0){
                                    r=Z_OK;
                                }
                                else{
                                    bitb=b; bitk=k; 
                                    z.avail_in=n;
                                    z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                    write=q;
                                    return inflate_flush(z,r);
                                };
                                n--;
                                b|=(z.next_in[p++]&0xff)<<k;
                                k+=8;
                            }

                            blens[border[index++]] = b&7; {

	   b>>=(3);k-=(3);}
                        }

                        while(index < 19){
                            blens[border[index++]] = 0;
                        }

                        bb[0] = 7;
                        t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
                        if (t != Z_OK){
                            r = t;
                            if (r == Z_DATA_ERROR){
                                blens=null;
                                mode = BAD;
                            }

                            bitb=b; bitk=k; 
                            z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                            write=q;
                            return inflate_flush(z,r);
                        }

                        index = 0;
                        mode = DTREE;
                        goto case DTREE;
                    case DTREE:
                        while (true){
                            t = table;
                            if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){
                                break;
                            }

                            int i, j, c;

                            t = bb[0];

                            while(k<(t)){
                                if(n!=0){
                                    r=Z_OK;
                                }
                                else{
                                    bitb=b; bitk=k; 
                                    z.avail_in=n;
                                    z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                    write=q;
                                    return inflate_flush(z,r);
                                };
                                n--;
                                b|=(z.next_in[p++]&0xff)<<k;
                                k+=8;
                            }

                            if(tb[0]==-1){
                                //System.err.println("null...");
                            }

                            t=hufts[(tb[0]+(b&inflate_mask[t]))*3+1];
                            c=hufts[(tb[0]+(b&inflate_mask[t]))*3+2];

                            if (c < 16){
                                b>>=(t);k-=(t);
                                blens[index++] = c;
                            }
                            else { // c == 16..18
                                i = c == 18 ? 7 : c - 14;
                                j = c == 18 ? 11 : 3;

                                while(k<(t+i)){
                                    if(n!=0){
                                        r=Z_OK;
                                    }
                                    else{
                                        bitb=b; bitk=k; 
                                        z.avail_in=n;
                                        z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                        write=q;
                                        return inflate_flush(z,r);
                                    };
                                    n--;
                                    b|=(z.next_in[p++]&0xff)<<k;
                                    k+=8;
                                }

                                b>>=(t);k-=(t);

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

                                b>>=(i);k-=(i);

                                i = index;
                                t = table;
                                if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
                                    (c == 16 && i < 1)){
                                    blens=null;
                                    mode = BAD;
                                    z.msg = "invalid bit length repeat";
                                    r = Z_DATA_ERROR;

                                    bitb=b; bitk=k; 
                                    z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                                    write=q;
                                    return inflate_flush(z,r);
                                }

                                c = c == 16 ? blens[i-1] : 0;
                                do{
                                    blens[i++] = c;
                                }
                                while (--j!=0);
                                index = i;
                            }
                        }

                        tb[0]=-1; {
                        int[] bl=new int[1];
                        int[] bd=new int[1];
                        int[] tl=new int[1];
                        int[] td=new int[1];
                        bl[0] = 9;         // must be <= 9 for lookahead assumptions
                        bd[0] = 6;         // must be <= 9 for lookahead assumptions

                        t = table;
                        t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), 
                            1 + ((t >> 5) & 0x1f),
                            blens, bl, bd, tl, td, hufts, z);

                        if (t != Z_OK){
                            if (t == Z_DATA_ERROR){
                                blens=null;
                                mode = BAD;
                            }
                            r = t;

                            bitb=b; bitk=k; 
                            z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                            write=q;
                            return inflate_flush(z,r);
                        }
                        codes.init(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
                    }
                        mode = CODES;
                        goto case CODES;
                    case CODES:
                        bitb=b; bitk=k;
                        z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p;
                        write=q;

                        if ((r = codes.proc(this, z, r)) != Z_STREAM_END){
                            return inflate_flush(z, r);
                        }
                        r = Z_OK;
                        codes.free(z);

                        p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk;
                        q=write;m=(int)(q<read?read-q-1:end-q);

                        if (last==0){
                            mode = TYPE;
                            break;
                        }
                        mode = DRY;
                        goto case DRY;
                    case DRY:
                        write=q; 
                        r=inflate_flush(z, r); 
                        q=write; m=(int)(q<read?read-q-1:end-q);
                        if (read != write){
                            bitb=b; bitk=k; 
                            z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                            write=q;
                            return inflate_flush(z, r);
                        }
                        mode = DONE;
                        goto case DONE;
                    case DONE:
                        r = Z_STREAM_END;

                        bitb=b; bitk=k; 
                        z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                        write=q;
                        return inflate_flush(z, r);
                    case BAD:
                        r = Z_DATA_ERROR;

                        bitb=b; bitk=k; 
                        z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                        write=q;
                        return inflate_flush(z, r);

                    default:
                        r = Z_STREAM_ERROR;

                        bitb=b; bitk=k; 
                        z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
                        write=q;
                        return inflate_flush(z, r);
                }
            }
        }
Exemple #47
0
        internal int proc(InfBlocks s, ZStream z, int r)
        {
            int num  = 0;
            int num2 = 0;
            int num3 = 0;

            num3 = z.next_in_index;
            int num4 = z.avail_in;

            num  = s.bitb;
            num2 = s.bitk;
            int num5 = s.write;
            int num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));

            while (true)
            {
                switch (mode)
                {
                case 0:
                    if (num6 >= 258 && num4 >= 10)
                    {
                        s.bitb          = num;
                        s.bitk          = num2;
                        z.avail_in      = num4;
                        z.total_in     += num3 - z.next_in_index;
                        z.next_in_index = num3;
                        s.write         = num5;
                        r    = inflate_fast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, s, z);
                        num3 = z.next_in_index;
                        num4 = z.avail_in;
                        num  = s.bitb;
                        num2 = s.bitk;
                        num5 = s.write;
                        num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.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 |= (z.next_in[num3++] & 0xFF) << num2;
                            continue;
                        }
                        s.bitb          = num;
                        s.bitk          = num2;
                        z.avail_in      = num4;
                        z.total_in     += num3 - z.next_in_index;
                        z.next_in_index = num3;
                        s.write         = num5;
                        return(s.inflate_flush(z, r));
                    }
                    int num8 = (tree_index + (num & inflate_mask[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 (((uint)num9 & 0x10u) != 0)
                    {
                        get  = num9 & 0xF;
                        len  = tree[num8 + 2];
                        mode = 2;
                        break;
                    }
                    if ((num9 & 0x40) == 0)
                    {
                        need       = num9;
                        tree_index = num8 / 3 + tree[num8 + 2];
                        break;
                    }
                    if (((uint)num9 & 0x20u) != 0)
                    {
                        mode = 7;
                        break;
                    }
                    mode            = 9;
                    z.msg           = "invalid literal/length code";
                    r               = -3;
                    s.bitb          = num;
                    s.bitk          = num2;
                    z.avail_in      = num4;
                    z.total_in     += num3 - z.next_in_index;
                    z.next_in_index = num3;
                    s.write         = num5;
                    return(s.inflate_flush(z, r));
                }

                case 2:
                {
                    int num7;
                    for (num7 = get; num2 < num7; num2 += 8)
                    {
                        if (num4 != 0)
                        {
                            r = 0;
                            num4--;
                            num |= (z.next_in[num3++] & 0xFF) << num2;
                            continue;
                        }
                        s.bitb          = num;
                        s.bitk          = num2;
                        z.avail_in      = num4;
                        z.total_in     += num3 - z.next_in_index;
                        z.next_in_index = num3;
                        s.write         = num5;
                        return(s.inflate_flush(z, r));
                    }
                    len       += num & inflate_mask[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 |= (z.next_in[num3++] & 0xFF) << num2;
                            continue;
                        }
                        s.bitb          = num;
                        s.bitk          = num2;
                        z.avail_in      = num4;
                        z.total_in     += num3 - z.next_in_index;
                        z.next_in_index = num3;
                        s.write         = num5;
                        return(s.inflate_flush(z, r));
                    }
                    int num8 = (tree_index + (num & inflate_mask[num7])) * 3;
                    num >>= tree[num8 + 1];
                    num2 -= tree[num8 + 1];
                    int num9 = tree[num8];
                    if (((uint)num9 & 0x10u) != 0)
                    {
                        get  = 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;
                    z.msg           = "invalid distance code";
                    r               = -3;
                    s.bitb          = num;
                    s.bitk          = num2;
                    z.avail_in      = num4;
                    z.total_in     += num3 - z.next_in_index;
                    z.next_in_index = num3;
                    s.write         = num5;
                    return(s.inflate_flush(z, r));
                }

                case 4:
                {
                    int num7;
                    for (num7 = get; num2 < num7; num2 += 8)
                    {
                        if (num4 != 0)
                        {
                            r = 0;
                            num4--;
                            num |= (z.next_in[num3++] & 0xFF) << num2;
                            continue;
                        }
                        s.bitb          = num;
                        s.bitk          = num2;
                        z.avail_in      = num4;
                        z.total_in     += num3 - z.next_in_index;
                        z.next_in_index = num3;
                        s.write         = num5;
                        return(s.inflate_flush(z, r));
                    }
                    dist += num & inflate_mask[num7];
                    num >>= num7;
                    num2 -= num7;
                    mode  = 5;
                    goto case 5;
                }

                case 5:
                {
                    int i;
                    for (i = num5 - dist; i < 0; i += s.end)
                    {
                    }
                    while (len != 0)
                    {
                        if (num6 == 0)
                        {
                            if (num5 == s.end && s.read != 0)
                            {
                                num5 = 0;
                                num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
                            }
                            if (num6 == 0)
                            {
                                s.write = num5;
                                r       = s.inflate_flush(z, r);
                                num5    = s.write;
                                num6    = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
                                if (num5 == s.end && s.read != 0)
                                {
                                    num5 = 0;
                                    num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
                                }
                                if (num6 == 0)
                                {
                                    s.bitb          = num;
                                    s.bitk          = num2;
                                    z.avail_in      = num4;
                                    z.total_in     += num3 - z.next_in_index;
                                    z.next_in_index = num3;
                                    s.write         = num5;
                                    return(s.inflate_flush(z, r));
                                }
                            }
                        }
                        s.window[num5++] = s.window[i++];
                        num6--;
                        if (i == s.end)
                        {
                            i = 0;
                        }
                        len--;
                    }
                    mode = 0;
                    break;
                }

                case 6:
                    if (num6 == 0)
                    {
                        if (num5 == s.end && s.read != 0)
                        {
                            num5 = 0;
                            num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
                        }
                        if (num6 == 0)
                        {
                            s.write = num5;
                            r       = s.inflate_flush(z, r);
                            num5    = s.write;
                            num6    = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
                            if (num5 == s.end && s.read != 0)
                            {
                                num5 = 0;
                                num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
                            }
                            if (num6 == 0)
                            {
                                s.bitb          = num;
                                s.bitk          = num2;
                                z.avail_in      = num4;
                                z.total_in     += num3 - z.next_in_index;
                                z.next_in_index = num3;
                                s.write         = num5;
                                return(s.inflate_flush(z, r));
                            }
                        }
                    }
                    r = 0;
                    s.window[num5++] = (byte)lit;
                    num6--;
                    mode = 0;
                    break;

                case 7:
                    if (num2 > 7)
                    {
                        num2 -= 8;
                        num4++;
                        num3--;
                    }
                    s.write = num5;
                    r       = s.inflate_flush(z, r);
                    num5    = s.write;
                    num6    = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
                    if (s.read != s.write)
                    {
                        s.bitb          = num;
                        s.bitk          = num2;
                        z.avail_in      = num4;
                        z.total_in     += num3 - z.next_in_index;
                        z.next_in_index = num3;
                        s.write         = num5;
                        return(s.inflate_flush(z, r));
                    }
                    mode = 8;
                    goto case 8;

                case 8:
                    r               = 1;
                    s.bitb          = num;
                    s.bitk          = num2;
                    z.avail_in      = num4;
                    z.total_in     += num3 - z.next_in_index;
                    z.next_in_index = num3;
                    s.write         = num5;
                    return(s.inflate_flush(z, r));

                case 9:
                    r               = -3;
                    s.bitb          = num;
                    s.bitk          = num2;
                    z.avail_in      = num4;
                    z.total_in     += num3 - z.next_in_index;
                    z.next_in_index = num3;
                    s.write         = num5;
                    return(s.inflate_flush(z, r));

                default:
                    r               = -2;
                    s.bitb          = num;
                    s.bitk          = num2;
                    z.avail_in      = num4;
                    z.total_in     += num3 - z.next_in_index;
                    z.next_in_index = num3;
                    s.write         = num5;
                    return(s.inflate_flush(z, r));
                }
            }
        }
Exemple #48
0
        // copy as much as possible from the sliding window to the output area
        internal int inflate_flush(ZStream z, int r){
            int n;
            int p;
            int q;

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

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

            // update counters
            z.avail_out -= n;
            z.total_out += n;

            // update check information
            if(checkfn != null)
                z.adler=check=z._adler.adler32(check, window, q, n);

            // copy as far as end of window
            System.Array.Copy(window, q, z.next_out, 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.avail_out) n = z.avail_out;
                if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;

                // update counters
                z.avail_out -= n;
                z.total_out += n;

                // update check information
                if(checkfn != null)
                    z.adler=check=z._adler.adler32(check, window, q, n);

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

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

            // done
            return r;
        }
Exemple #49
0
        internal int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InfBlocks s, ZStream z)
        {
            int next_in_index = z.next_in_index;
            int num           = z.avail_in;
            int num2          = s.bitb;
            int num3          = s.bitk;
            int num4          = s.write;
            int num5          = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
            int num6          = inflate_mask[bl];
            int num7          = inflate_mask[bd];
            int num12;

            while (true)
            {
                if (num3 < 20)
                {
                    num--;
                    num2 |= (z.next_in[next_in_index++] & 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 (((uint)num11 & 0x10u) != 0)
                        {
                            num11 &= 0xF;
                            num12  = array[num10 + 2] + (num2 & inflate_mask[num11]);
                            num2 >>= num11;
                            for (num3 -= num11; num3 < 15; num3 += 8)
                            {
                                num--;
                                num2 |= (z.next_in[next_in_index++] & 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 (((uint)num11 & 0x10u) != 0)
                                {
                                    break;
                                }
                                if ((num11 & 0x40) == 0)
                                {
                                    num8 += array[num10 + 2];
                                    num8 += num2 & inflate_mask[num11];
                                    num10 = (num9 + num8) * 3;
                                    num11 = array[num10];
                                    continue;
                                }
                                z.msg           = "invalid distance code";
                                num12           = z.avail_in - num;
                                num12           = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
                                num            += num12;
                                next_in_index  -= num12;
                                num3           -= num12 << 3;
                                s.bitb          = num2;
                                s.bitk          = num3;
                                z.avail_in      = num;
                                z.total_in     += next_in_index - z.next_in_index;
                                z.next_in_index = next_in_index;
                                s.write         = num4;
                                return(-3);
                            }
                            for (num11 &= 0xF; num3 < num11; num3 += 8)
                            {
                                num--;
                                num2 |= (z.next_in[next_in_index++] & 0xFF) << num3;
                            }
                            int num13 = array[num10 + 2] + (num2 & inflate_mask[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
                                {
                                    global::System.Array.Copy((global::System.Array)s.window, num14, (global::System.Array)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
                                    {
                                        global::System.Array.Copy((global::System.Array)s.window, num14, (global::System.Array)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);
                                break;
                            }
                            global::System.Array.Copy((global::System.Array)s.window, num14, (global::System.Array)s.window, num4, num12);
                            num4  += num12;
                            num14 += num12;
                            num12  = 0;
                            break;
                        }
                        if ((num11 & 0x40) == 0)
                        {
                            num8 += array[num10 + 2];
                            num8 += num2 & inflate_mask[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 (((uint)num11 & 0x20u) != 0)
                        {
                            num12           = z.avail_in - num;
                            num12           = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
                            num            += num12;
                            next_in_index  -= num12;
                            num3           -= num12 << 3;
                            s.bitb          = num2;
                            s.bitk          = num3;
                            z.avail_in      = num;
                            z.total_in     += next_in_index - z.next_in_index;
                            z.next_in_index = next_in_index;
                            s.write         = num4;
                            return(1);
                        }
                        z.msg           = "invalid literal/length code";
                        num12           = z.avail_in - num;
                        num12           = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
                        num            += num12;
                        next_in_index  -= num12;
                        num3           -= num12 << 3;
                        s.bitb          = num2;
                        s.bitk          = num3;
                        z.avail_in      = num;
                        z.total_in     += next_in_index - z.next_in_index;
                        z.next_in_index = next_in_index;
                        s.write         = num4;
                        return(-3);
                    }
                }
                if (num5 < 258 || num < 10)
                {
                    break;
                }
            }
            num12           = z.avail_in - num;
            num12           = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
            num            += num12;
            next_in_index  -= num12;
            num3           -= num12 << 3;
            s.bitb          = num2;
            s.bitk          = num3;
            z.avail_in      = num;
            z.total_in     += next_in_index - z.next_in_index;
            z.next_in_index = next_in_index;
            s.write         = num4;
            return(0);
        }