Exemple #1
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 #2
0
 internal int inflateEnd(ZStream z)
 {
     if (this.blocks != null)
     {
         this.blocks.free(z);
     }
     this.blocks = null;
     return(0);
 }
Exemple #3
0
 internal int inflateEnd(ZStream z)
 {
     if (blocks != null)
     {
         blocks.free(z);
     }
     blocks = null;
     //    ZFREE(z, z->state);
     return(Z_OK);
 }
Exemple #4
0
 internal int inflateInit(ZStream z, int w)
 {
     z.msg       = null;
     this.blocks = null;
     this.nowrap = 0;
     if (w < 0)
     {
         w           = -w;
         this.nowrap = 1;
     }
     if ((w < 8) || (w > 15))
     {
         this.inflateEnd(z);
         return(-2);
     }
     this.wbits      = w;
     z.istate.blocks = new InfBlocks(z, (z.istate.nowrap != 0) ? null : this, ((int)1) << w);
     this.inflateReset(z);
     return(0);
 }
Exemple #5
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

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

                                // copy all or what's left
                                if (q - r > 0 && c > (q - r))
                                {
                                    do
                                    {
                                        s.window[q++] = s.window[r++];
                                    }while (--c != 0);
                                }
                                else
                                {
                                    Array.Copy(s.window, r, s.window, q, c);
                                    q += c; r += c; c = 0;
                                }
                                break;
                            }
                            else if ((e & 64) == 0)
                            {
                                t += tp[(tp_index + t) * 3 + 2];
                                t += (b & inflate_mask[e]);
                                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]);
                        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 #6
0
        internal int proc(InfBlocks s, ZStream z, int r)
        {
            int j;             // temporary storage
            //int[] t; // temporary pointer
            int tindex;        // temporary pointer
            int e;             // extra bits or operation
            var b = 0;         // bit buffer
            var k = 0;         // bits in bit buffer
            var 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  = SupportClass.URShift(b, (tree[tindex + 1]));
                    k -= (tree[tindex + 1]);

                    e = tree[tindex];

                    if (e == 0)
                    {
                        // literal
                        lit  = tree[tindex + 2];
                        mode = LIT;
                        break;
                    }
                    if ((e & 16) != 0)
                    {
                        // length
                        get_Renamed = e & 15;
                        len         = tree[tindex + 2];
                        mode        = LENEXT;
                        break;
                    }
                    if ((e & 64) == 0)
                    {
                        // next table
                        need       = e;
                        tree_index = tindex / 3 + tree[tindex + 2];
                        break;
                    }
                    if ((e & 32) != 0)
                    {
                        // end of block
                        mode = WASH;
                        break;
                    }
                    mode  = BADCODE;                            // invalid code
                    z.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_Renamed;

                    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_Renamed = e & 15;
                        dist        = tree[tindex + 2];
                        mode        = DISTEXT;
                        break;
                    }
                    if ((e & 64) == 0)
                    {
                        // next table
                        need       = e;
                        tree_index = tindex / 3 + tree[tindex + 2];
                        break;
                    }
                    mode  = BADCODE;                            // invalid code
                    z.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_Renamed;

                    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 #7
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

            // 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;
                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;
                        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++]; c--; // minimum count is three,
                                        s.window[q++] = s.window[r++]; c--; // so unroll loop a little
                                    }
                                    else
                                    {
                                        Buffer.BlockCopy(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
                                        {
                                            Buffer.BlockCopy(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
                                {
                                    Buffer.BlockCopy(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]);
                                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]);
                        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 #8
0
        internal int proc(InfBlocks s, ZStream z, int r)
        {
            int j; // temporary storage
            //int[] t; // temporary pointer
            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 = SupportClass.URShift(b, (tree[tindex + 1]));
                        k -= (tree[tindex + 1]);

                        e = tree[tindex];

                        if (e == 0)
                        {
                            // literal
                            lit = tree[tindex + 2];
                            mode = LIT;
                            break;
                        }
                        if ((e & 16) != 0)
                        {
                            // length
                            get_Renamed = e & 15;
                            len = tree[tindex + 2];
                            mode = LENEXT;
                            break;
                        }
                        if ((e & 64) == 0)
                        {
                            // next table
                            need = e;
                            tree_index = tindex / 3 + tree[tindex + 2];
                            break;
                        }
                        if ((e & 32) != 0)
                        {
                            // end of block
                            mode = WASH;
                            break;
                        }
                        mode = BADCODE; // invalid code
                        z.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_Renamed;

                        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_Renamed = e & 15;
                            dist = tree[tindex + 2];
                            mode = DISTEXT;
                            break;
                        }
                        if ((e & 64) == 0)
                        {
                            // next table
                            need = e;
                            tree_index = tindex / 3 + tree[tindex + 2];
                            break;
                        }
                        mode = BADCODE; // invalid code
                        z.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_Renamed;

                        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 #9
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 #10
0
 internal int inflateEnd(ZStream z)
 {
     if (blocks != null)
         blocks.free(z);
     blocks = null;
     //    ZFREE(z, z->state);
     return Z_OK;
 }
Exemple #11
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.end - num4) : (s.read - num4 - 1));
            int num6          = inflate_mask[bl];
            int num7          = inflate_mask[bd];
            int num11;

            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;
                if ((num10 = array[(num9 + num8) * 3]) == 0)
                {
                    num2           >>= array[(num9 + num8) * 3 + 1];
                    num3            -= array[(num9 + num8) * 3 + 1];
                    s.window[num4++] = (byte)array[(num9 + num8) * 3 + 2];
                    num5--;
                }
                else
                {
                    while (true)
                    {
                        num2 >>= array[(num9 + num8) * 3 + 1];
                        num3  -= array[(num9 + num8) * 3 + 1];
                        if (((uint)num10 & 0x10u) != 0)
                        {
                            num10 &= 0xF;
                            num11  = array[(num9 + num8) * 3 + 2] + (num2 & inflate_mask[num10]);
                            num2 >>= num10;
                            for (num3 -= num10; num3 < 15; num3 += 8)
                            {
                                num--;
                                num2 |= (z.next_in[next_in_index++] & 0xFF) << num3;
                            }
                            num8  = num2 & num7;
                            array = td;
                            num9  = td_index;
                            num10 = array[(num9 + num8) * 3];
                            while (true)
                            {
                                num2 >>= array[(num9 + num8) * 3 + 1];
                                num3  -= array[(num9 + num8) * 3 + 1];
                                if (((uint)num10 & 0x10u) != 0)
                                {
                                    break;
                                }
                                if ((num10 & 0x40) == 0)
                                {
                                    num8 += array[(num9 + num8) * 3 + 2];
                                    num8 += num2 & inflate_mask[num10];
                                    num10 = array[(num9 + num8) * 3];
                                    continue;
                                }
                                z.msg           = "invalid distance code";
                                num11           = z.avail_in - num;
                                num11           = ((num3 >> 3 >= num11) ? num11 : (num3 >> 3));
                                num            += num11;
                                next_in_index  -= num11;
                                num3           -= num11 << 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 (num10 &= 0xF; num3 < num10; num3 += 8)
                            {
                                num--;
                                num2 |= (z.next_in[next_in_index++] & 0xFF) << num3;
                            }
                            int num12 = array[(num9 + num8) * 3 + 2] + (num2 & inflate_mask[num10]);
                            num2 >>= num10;
                            num3  -= num10;
                            num5  -= num11;
                            int num13;
                            if (num4 >= num12)
                            {
                                num13 = num4 - num12;
                                if (num4 - num13 > 0 && 2 > num4 - num13)
                                {
                                    s.window[num4++] = s.window[num13++];
                                    num11--;
                                    s.window[num4++] = s.window[num13++];
                                    num11--;
                                }
                                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;
                                        num10  = 0;
                                    }
                                    num13 = 0;
                                }
                            }
                            if (num4 - num13 > 0 && num11 > num4 - num13)
                            {
                                do
                                {
                                    s.window[num4++] = s.window[num13++];
                                }while (--num11 != 0);
                                break;
                            }
                            Array.Copy(s.window, num13, s.window, num4, num11);
                            num4  += num11;
                            num13 += num11;
                            num11  = 0;
                            break;
                        }
                        if ((num10 & 0x40) == 0)
                        {
                            num8 += array[(num9 + num8) * 3 + 2];
                            num8 += num2 & inflate_mask[num10];
                            if ((num10 = array[(num9 + num8) * 3]) == 0)
                            {
                                num2           >>= array[(num9 + num8) * 3 + 1];
                                num3            -= array[(num9 + num8) * 3 + 1];
                                s.window[num4++] = (byte)array[(num9 + num8) * 3 + 2];
                                num5--;
                                break;
                            }
                            continue;
                        }
                        if (((uint)num10 & 0x20u) != 0)
                        {
                            num11           = z.avail_in - num;
                            num11           = ((num3 >> 3 >= num11) ? num11 : (num3 >> 3));
                            num            += num11;
                            next_in_index  -= num11;
                            num3           -= num11 << 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";
                        num11           = z.avail_in - num;
                        num11           = ((num3 >> 3 >= num11) ? num11 : (num3 >> 3));
                        num            += num11;
                        next_in_index  -= num11;
                        num3           -= num11 << 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;
                }
            }
            num11           = z.avail_in - num;
            num11           = ((num3 >> 3 >= num11) ? num11 : (num3 >> 3));
            num            += num11;
            next_in_index  -= num11;
            num3           -= num11 << 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);
        }
Exemple #12
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.end - num5) : (s.read - num5 - 1));

            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.end - num5) : (s.read - num5 - 1));
                        if (r != 0)
                        {
                            mode = ((r != 1) ? 9 : 7);
                            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   = SupportClass.URShift(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_Renamed = 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_Renamed; 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_Renamed = 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_Renamed; 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.end - num5) : (s.read - num5 - 1));
                            }
                            if (num6 == 0)
                            {
                                s.write = num5;
                                r       = s.inflate_flush(z, r);
                                num5    = s.write;
                                num6    = ((num5 >= s.read) ? (s.end - num5) : (s.read - num5 - 1));
                                if (num5 == s.end && s.read != 0)
                                {
                                    num5 = 0;
                                    num6 = ((num5 >= s.read) ? (s.end - num5) : (s.read - num5 - 1));
                                }
                                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.end - num5) : (s.read - num5 - 1));
                        }
                        if (num6 == 0)
                        {
                            s.write = num5;
                            r       = s.inflate_flush(z, r);
                            num5    = s.write;
                            num6    = ((num5 >= s.read) ? (s.end - num5) : (s.read - num5 - 1));
                            if (num5 == s.end && s.read != 0)
                            {
                                num5 = 0;
                                num6 = ((num5 >= s.read) ? (s.end - num5) : (s.read - num5 - 1));
                            }
                            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.end - num5) : (s.read - num5 - 1));
                    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 #13
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.read - write) - 1) : (s.end - write);
            int num10 = inflate_mask[bl];
            int num11 = inflate_mask[bd];

Label_0092:
            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    = numArray[(num2 + num) * 3];

            if (index == 0)
            {
                bitb  = bitb >> numArray[((num2 + num) * 3) + 1];
                bitk -= numArray[((num2 + num) * 3) + 1];
                s.window[write++] = (byte)numArray[((num2 + num) * 3) + 2];
                num9--;
                goto Label_05E0;
            }
Label_00F1:
            bitb  = bitb >> numArray[((num2 + num) * 3) + 1];
            bitk -= numArray[((num2 + num) * 3) + 1];
            if ((index & 0x10) == 0)
            {
                if ((index & 0x40) == 0)
                {
                    num  += numArray[((num2 + num) * 3) + 2];
                    num  += bitb & inflate_mask[index];
                    index = numArray[(num2 + num) * 3];
                    if (index != 0)
                    {
                        goto Label_00F1;
                    }
                    bitb  = bitb >> numArray[((num2 + num) * 3) + 1];
                    bitk -= numArray[((num2 + num) * 3) + 1];
                    s.window[write++] = (byte)numArray[((num2 + num) * 3) + 2];
                    num9--;
                }
                else
                {
                    if ((index & 0x20) != 0)
                    {
                        num12           = z.avail_in - num7;
                        num12           = ((bitk >> 3) < num12) ? (bitk >> 3) : num12;
                        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) ? (bitk >> 3) : num12;
                    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_05E0;
            }
            index &= 15;
            num12  = numArray[((num2 + num) * 3) + 2] + (bitb & inflate_mask[index]);
            bitb   = bitb >> index;
            bitk  -= index;
            while (bitk < 15)
            {
                num7--;
                bitb |= (z.next_in[num6++] & 0xff) << bitk;
                bitk += 8;
            }
            num      = bitb & num11;
            numArray = td;
            num2     = td_index;
            index    = numArray[(num2 + num) * 3];
Label_018B:
            bitb  = bitb >> numArray[((num2 + num) * 3) + 1];
            bitk -= numArray[((num2 + num) * 3) + 1];
            if ((index & 0x10) != 0)
            {
                int num14;
                index &= 15;
                while (bitk < index)
                {
                    num7--;
                    bitb |= (z.next_in[num6++] & 0xff) << bitk;
                    bitk += 8;
                }
                int num13 = numArray[((num2 + num) * 3) + 2] + (bitb & inflate_mask[index]);
                bitb  = bitb >> index;
                bitk -= index;
                num9 -= num12;
                if (write >= num13)
                {
                    num14 = write - num13;
                    if (((write - num14) > 0) && (2 > (write - num14)))
                    {
                        s.window[write++] = s.window[num14++];
                        num12--;
                        s.window[write++] = s.window[num14++];
                        num12--;
                    }
                    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);
                    index = s.end - num14;
                    if (num12 > index)
                    {
                        num12 -= index;
                        if (((write - num14) > 0) && (index > (write - num14)))
                        {
                            do
                            {
                                s.window[write++] = s.window[num14++];
                            }while (--index != 0);
                        }
                        else
                        {
                            Array.Copy(s.window, num14, s.window, write, index);
                            write += index;
                            num14 += index;
                            index  = 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 ((index & 0x40) == 0)
                {
                    num  += numArray[((num2 + num) * 3) + 2];
                    num  += bitb & inflate_mask[index];
                    index = numArray[(num2 + num) * 3];
                    goto Label_018B;
                }
                z.msg           = "invalid distance code";
                num12           = z.avail_in - num7;
                num12           = ((bitk >> 3) < num12) ? (bitk >> 3) : num12;
                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_05E0:
            if ((num9 >= 0x102) && (num7 >= 10))
            {
                goto Label_0092;
            }
            num12           = z.avail_in - num7;
            num12           = ((bitk >> 3) < num12) ? (bitk >> 3) : num12;
            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 #14
0
        internal int proc(InfBlocks s, ZStream z, int r)
        {
            int num;
            int num10;
            int number = 0;
            int bitk   = 0;
            int num6   = 0;

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

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

Label_0051:
            switch (this.mode)
            {
            case 0:
                if ((num9 < 0x102) || (num7 < 10))
                {
                    break;
                }
                s.bitb          = number;
                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;
                number = s.bitb;
                bitk   = s.bitk;
                write  = s.write;
                num9   = (write < s.read) ? ((s.read - write) - 1) : (s.end - write);
                if (r == 0)
                {
                    break;
                }
                this.mode = (r == 1) ? 7 : 9;
                goto Label_0051;

            case 1:
                goto Label_0199;

            case 2:
                num = this.get_Renamed;
                while (bitk < num)
                {
                    if (num7 != 0)
                    {
                        r = 0;
                    }
                    else
                    {
                        s.bitb          = number;
                        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--;
                    number |= (z.next_in[num6++] & 0xff) << bitk;
                    bitk   += 8;
                }
                this.len       += number & inflate_mask[num];
                number          = number >> num;
                bitk           -= num;
                this.need       = this.dbits;
                this.tree       = this.dtree;
                this.tree_index = this.dtree_index;
                this.mode       = 3;
                goto Label_0412;

            case 3:
                goto Label_0412;

            case 4:
                num = this.get_Renamed;
                while (bitk < num)
                {
                    if (num7 != 0)
                    {
                        r = 0;
                    }
                    else
                    {
                        s.bitb          = number;
                        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--;
                    number |= (z.next_in[num6++] & 0xff) << bitk;
                    bitk   += 8;
                }
                this.dist += number & inflate_mask[num];
                number     = number >> num;
                bitk      -= num;
                this.mode  = 5;
                goto Label_0635;

            case 5:
                goto Label_0635;

            case 6:
                if (num9 == 0)
                {
                    if ((write == s.end) && (s.read != 0))
                    {
                        write = 0;
                        num9  = (write < s.read) ? ((s.read - write) - 1) : (s.end - write);
                    }
                    if (num9 == 0)
                    {
                        s.write = write;
                        r       = s.inflate_flush(z, r);
                        write   = s.write;
                        num9    = (write < s.read) ? ((s.read - write) - 1) : (s.end - write);
                        if ((write == s.end) && (s.read != 0))
                        {
                            write = 0;
                            num9  = (write < s.read) ? ((s.read - write) - 1) : (s.end - write);
                        }
                        if (num9 == 0)
                        {
                            s.bitb          = number;
                            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_0051;

            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.read - write) - 1) : (s.end - write);
                if (s.read != s.write)
                {
                    s.bitb          = number;
                    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_098A;

            case 8:
                goto Label_098A;

            case 9:
                r               = -3;
                s.bitb          = number;
                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          = number;
                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_0199:
            num = this.need;
            while (bitk < num)
            {
                if (num7 != 0)
                {
                    r = 0;
                }
                else
                {
                    s.bitb          = number;
                    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--;
                number |= (z.next_in[num6++] & 0xff) << bitk;
                bitk   += 8;
            }
            int index = (this.tree_index + (number & inflate_mask[num])) * 3;

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

            if (num3 == 0)
            {
                this.lit  = this.tree[index + 2];
                this.mode = 6;
                goto Label_0051;
            }
            if ((num3 & 0x10) != 0)
            {
                this.get_Renamed = num3 & 15;
                this.len         = this.tree[index + 2];
                this.mode        = 2;
                goto Label_0051;
            }
            if ((num3 & 0x40) == 0)
            {
                this.need       = num3;
                this.tree_index = (index / 3) + this.tree[index + 2];
                goto Label_0051;
            }
            if ((num3 & 0x20) != 0)
            {
                this.mode = 7;
                goto Label_0051;
            }
            this.mode       = 9;
            z.msg           = "invalid literal/length code";
            r               = -3;
            s.bitb          = number;
            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_0412:
            num = this.need;
            while (bitk < num)
            {
                if (num7 != 0)
                {
                    r = 0;
                }
                else
                {
                    s.bitb          = number;
                    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--;
                number |= (z.next_in[num6++] & 0xff) << bitk;
                bitk   += 8;
            }
            index  = (this.tree_index + (number & inflate_mask[num])) * 3;
            number = number >> this.tree[index + 1];
            bitk  -= this.tree[index + 1];
            num3   = this.tree[index];
            if ((num3 & 0x10) != 0)
            {
                this.get_Renamed = num3 & 15;
                this.dist        = this.tree[index + 2];
                this.mode        = 4;
                goto Label_0051;
            }
            if ((num3 & 0x40) == 0)
            {
                this.need       = num3;
                this.tree_index = (index / 3) + this.tree[index + 2];
                goto Label_0051;
            }
            this.mode       = 9;
            z.msg           = "invalid distance code";
            r               = -3;
            s.bitb          = number;
            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_0635:
            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.read - write) - 1) : (s.end - write);
                    }
                    if (num9 == 0)
                    {
                        s.write = write;
                        r       = s.inflate_flush(z, r);
                        write   = s.write;
                        num9    = (write < s.read) ? ((s.read - write) - 1) : (s.end - write);
                        if ((write == s.end) && (s.read != 0))
                        {
                            write = 0;
                            num9  = (write < s.read) ? ((s.read - write) - 1) : (s.end - write);
                        }
                        if (num9 == 0)
                        {
                            s.bitb          = number;
                            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_0051;
Label_098A:
            r               = 1;
            s.bitb          = number;
            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));
        }