Flush() private method

private Flush ( int r ) : int
r int
return int
        internal int Process(InflateBlocks blocks, int r)
        {
            int j;      // temporary storage
            int tindex; // temporary pointer
            int e;      // extra bits or operation
            int b = 0;  // bit buffer
            int k = 0;  // bits in bit buffer
            int p = 0;  // input data pointer
            int n;      // bytes available there
            int q;      // output window write pointer
            int m;      // bytes to end of window or read pointer
            int f;      // pointer to copy strings from

            ZlibCodec z = blocks._codec;

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

            // process input and output based on current state
            while (true)
            {
                switch (mode)
                {
                    // waiting for "i:"=input, "o:"=output, "x:"=nothing
                    case START:  // x: set up for LEN
                        if (m >= 258 && n >= 10)
                        {
                            blocks.bitb = b; blocks.bitk = k;
                            z.AvailableBytesIn = n;
                            z.TotalBytesIn += p - z.NextIn;
                            z.NextIn = p;
                            blocks.writeAt = q;
                            r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);

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

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

                        mode = LEN;
                        goto case LEN;

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

                        while (k < j)
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {
                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n;
                                z.TotalBytesIn += p - z.NextIn;
                                z.NextIn = p;
                                blocks.writeAt = q;
                                return blocks.Flush(r);
                            }
                            n--;
                            b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;

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

                        e = tree[tindex];

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

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


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

                        while (k < j)
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {
                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                blocks.writeAt = q;
                                return blocks.Flush(r);
                            }
                            n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        len += (b & InternalInflateConstants.InflateMask[j]);

                        b >>= j;
                        k -= j;

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

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

                        while (k < j)
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {
                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                blocks.writeAt = q;
                                return blocks.Flush(r);
                            }
                            n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;

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

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

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


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

                        while (k < j)
                        {
                            if (n != 0)
                                r = ZlibConstants.Z_OK;
                            else
                            {
                                blocks.bitb = b; blocks.bitk = k;
                                z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                                blocks.writeAt = q;
                                return blocks.Flush(r);
                            }
                            n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                            k += 8;
                        }

                        dist += (b & InternalInflateConstants.InflateMask[j]);

                        b >>= j;
                        k -= j;

                        mode = COPY;
                        goto case COPY;

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

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

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

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

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

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

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

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

                        mode = START;
                        break;

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

                        blocks.writeAt = q; r = blocks.Flush(r);
                        q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

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

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

                    case BADCODE:  // x: got error

                        r = ZlibConstants.Z_DATA_ERROR;

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

                    default:
                        r = ZlibConstants.Z_STREAM_ERROR;

                        blocks.bitb = b; blocks.bitk = k;
                        z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                        blocks.writeAt = q;
                        return blocks.Flush(r);
                }
            }
        }
Ejemplo n.º 2
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       num   = 0;
            int       num2  = 0;
            int       num3  = 0;
            ZlibCodec codec = blocks._codec;

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

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

            while (true)
            {
                switch (mode)
                {
                case 0:
                    if (num6 >= 258 && num4 >= 10)
                    {
                        blocks.bitb            = num;
                        blocks.bitk            = num2;
                        codec.AvailableBytesIn = num4;
                        codec.TotalBytesIn    += num3 - codec.NextIn;
                        codec.NextIn           = num3;
                        blocks.writeAt         = num5;
                        r    = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, codec);
                        num3 = codec.NextIn;
                        num4 = codec.AvailableBytesIn;
                        num  = blocks.bitb;
                        num2 = blocks.bitk;
                        num5 = blocks.writeAt;
                        num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - 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 num8;
                    for (num8 = need; num2 < num8; num2 += 8)
                    {
                        if (num4 == 0)
                        {
                            blocks.bitb            = num;
                            blocks.bitk            = num2;
                            codec.AvailableBytesIn = num4;
                            codec.TotalBytesIn    += num3 - codec.NextIn;
                            codec.NextIn           = num3;
                            blocks.writeAt         = num5;
                            return(blocks.Flush(r));
                        }
                        r = 0;
                        num4--;
                        num |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                    }
                    int num11 = (tree_index + (num & InternalInflateConstants.InflateMask[num8])) * 3;
                    num >>= tree[num11 + 1];
                    num2 -= tree[num11 + 1];
                    int num12 = tree[num11];
                    if (num12 == 0)
                    {
                        lit  = tree[num11 + 2];
                        mode = 6;
                    }
                    else if ((num12 & 0x10) != 0)
                    {
                        bitsToGet = (num12 & 0xF);
                        len       = tree[num11 + 2];
                        mode      = 2;
                    }
                    else if ((num12 & 0x40) == 0)
                    {
                        need       = num12;
                        tree_index = num11 / 3 + tree[num11 + 2];
                    }
                    else
                    {
                        if ((num12 & 0x20) == 0)
                        {
                            mode                   = 9;
                            codec.Message          = "invalid literal/length code";
                            r                      = -3;
                            blocks.bitb            = num;
                            blocks.bitk            = num2;
                            codec.AvailableBytesIn = num4;
                            codec.TotalBytesIn    += num3 - codec.NextIn;
                            codec.NextIn           = num3;
                            blocks.writeAt         = num5;
                            return(blocks.Flush(r));
                        }
                        mode = 7;
                    }
                    break;
                }

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

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

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

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

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

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

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

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

                default:
                    r                      = -2;
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }
            }
        }
Ejemplo n.º 3
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       b = 0;
            int       j = 0;
            int       p = 0;
            ZlibCodec z = blocks._codec;

            p = z.NextIn;
            int l = z.AvailableBytesIn;

            b = blocks.bitb;
            j = blocks.bitk;
            int q = blocks.writeAt;
            int k = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));

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

                case 1:
                {
                    int i;
                    for (i = need; j < i; j += 8)
                    {
                        if (l != 0)
                        {
                            r = 0;
                            l--;
                            b |= (z.InputBuffer[p++] & 0xFF) << j;
                            continue;
                        }
                        blocks.bitb        = b;
                        blocks.bitk        = j;
                        z.AvailableBytesIn = l;
                        z.TotalBytesIn    += p - z.NextIn;
                        z.NextIn           = p;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    int tindex = (tree_index + (b & InternalInflateConstants.InflateMask[i])) * 3;
                    b >>= tree[tindex + 1];
                    j  -= tree[tindex + 1];
                    int e = tree[tindex];
                    if (e == 0)
                    {
                        lit  = tree[tindex + 2];
                        mode = 6;
                        break;
                    }
                    if (((uint)e & 0x10u) != 0)
                    {
                        bitsToGet = e & 0xF;
                        len       = tree[tindex + 2];
                        mode      = 2;
                        break;
                    }
                    if ((e & 0x40) == 0)
                    {
                        need       = e;
                        tree_index = tindex / 3 + tree[tindex + 2];
                        break;
                    }
                    if (((uint)e & 0x20u) != 0)
                    {
                        mode = 7;
                        break;
                    }
                    mode               = 9;
                    z.Message          = "invalid literal/length code";
                    r                  = -3;
                    blocks.bitb        = b;
                    blocks.bitk        = j;
                    z.AvailableBytesIn = l;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));
                }

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

                case 3:
                {
                    int i;
                    for (i = need; j < i; j += 8)
                    {
                        if (l != 0)
                        {
                            r = 0;
                            l--;
                            b |= (z.InputBuffer[p++] & 0xFF) << j;
                            continue;
                        }
                        blocks.bitb        = b;
                        blocks.bitk        = j;
                        z.AvailableBytesIn = l;
                        z.TotalBytesIn    += p - z.NextIn;
                        z.NextIn           = p;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    int tindex = (tree_index + (b & InternalInflateConstants.InflateMask[i])) * 3;
                    b >>= tree[tindex + 1];
                    j  -= tree[tindex + 1];
                    int e = tree[tindex];
                    if (((uint)e & 0x10u) != 0)
                    {
                        bitsToGet = e & 0xF;
                        dist      = tree[tindex + 2];
                        mode      = 4;
                        break;
                    }
                    if ((e & 0x40) == 0)
                    {
                        need       = e;
                        tree_index = tindex / 3 + tree[tindex + 2];
                        break;
                    }
                    mode               = 9;
                    z.Message          = "invalid distance code";
                    r                  = -3;
                    blocks.bitb        = b;
                    blocks.bitk        = j;
                    z.AvailableBytesIn = l;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));
                }

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

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

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

                case 7:
                    if (j > 7)
                    {
                        j -= 8;
                        l++;
                        p--;
                    }
                    blocks.writeAt = q;
                    r = blocks.Flush(r);
                    q = blocks.writeAt;
                    k = ((q < blocks.readAt) ? (blocks.readAt - q - 1) : (blocks.end - q));
                    if (blocks.readAt != blocks.writeAt)
                    {
                        blocks.bitb        = b;
                        blocks.bitk        = j;
                        z.AvailableBytesIn = l;
                        z.TotalBytesIn    += p - z.NextIn;
                        z.NextIn           = p;
                        blocks.writeAt     = q;
                        return(blocks.Flush(r));
                    }
                    mode = 8;
                    goto case 8;

                case 8:
                    r                  = 1;
                    blocks.bitb        = b;
                    blocks.bitk        = j;
                    z.AvailableBytesIn = l;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));

                case 9:
                    r                  = -3;
                    blocks.bitb        = b;
                    blocks.bitk        = j;
                    z.AvailableBytesIn = l;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));

                default:
                    r                  = -2;
                    blocks.bitb        = b;
                    blocks.bitk        = j;
                    z.AvailableBytesIn = l;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));
                }
            }
        }
Ejemplo n.º 4
0
        internal int Process(InflateBlocks blocks, int r)
        {
            ZlibCodec codec = blocks._codec;
            int       num   = codec.NextIn;
            int       num2  = codec.AvailableBytesIn;
            int       num3  = blocks.bitb;
            int       i     = blocks.bitk;
            int       num4  = blocks.writeAt;
            int       num5  = (num4 >= blocks.readAt) ? (blocks.end - num4) : (blocks.readAt - num4 - 1);

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

                case 1:
                    goto IL_1C4;

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

                case 3:
                    goto IL_471;

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

                case 5:
                    goto IL_6C3;

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

                case 7:
                    goto IL_9B8;

                case 8:
                    goto IL_A7A;

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

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

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

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

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

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

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

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

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

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

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

IL_AC7:
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(r));
        }
Ejemplo n.º 5
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int tindex;                                                               // temporary pointer
            int e;                                                                    // extra bits or operation

            int nextIn  = blocks._NextIn;                                             // input data pointer
            int availIn = blocks._AvailIn;                                            // bytes available there
            int bits    = blocks.bitb;                                                // bit buffer
            int bitsNum = blocks.bitk;                                                // bits in bit buffer
            int q       = blocks.writeAt;                                             // output window write pointer
            int m       = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q; // bytes to end of window or read pointer

            // 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 && availIn >= 10)
                    {
                        blocks.UpdateState(bits, bitsNum, availIn, nextIn, q);
                        r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks);

                        nextIn  = blocks._NextIn;
                        availIn = blocks._AvailIn;
                        bits    = blocks.bitb;
                        bitsNum = blocks.bitk;
                        q       = blocks.writeAt;
                        m       = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

                        if (r != RCode.Okay)
                        {
                            mode = (r == RCode.StreamEnd) ? WASH : BADCODE;
                            break;
                        }
                    }
                    need       = lbits;
                    tree       = ltree;
                    tree_index = ltree_index;

                    mode = LEN;
                    goto case LEN;

                case LEN:                          // i: get length/literal/eob next
                    while (bitsNum < need)
                    {
                        if (availIn != 0)
                        {
                            r = RCode.Okay;
                        }
                        else
                        {
                            return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));
                        }
                        availIn--;
                        bits    |= blocks.InputBuffer[nextIn++] << bitsNum;
                        bitsNum += 8;
                    }

                    tindex = (tree_index + (bits & Constants.InflateMask[need])) * 3;

                    bits   >>= (tree[tindex + 1]);
                    bitsNum -= (tree[tindex + 1]);

                    e = tree[tindex];

                    if (e == 0)
                    {
                        // literal
                        lit  = tree[tindex + 2];
                        mode = LIT;
                        break;
                    }
                    if ((e & 16) != 0)
                    {
                        // length
                        bitsToGet = 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;
                    }
                    throw new InvalidDataException("invalid literal/length code");


                case LENEXT:                          // i: getting length extra (have base)
                    while (bitsNum < bitsToGet)
                    {
                        if (availIn != 0)
                        {
                            r = RCode.Okay;
                        }
                        else
                        {
                            return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));
                        }
                        availIn--;
                        bits    |= blocks.InputBuffer[nextIn++] << bitsNum;
                        bitsNum += 8;
                    }

                    len += (bits & Constants.InflateMask[bitsToGet]);

                    bits   >>= bitsToGet;
                    bitsNum -= bitsToGet;

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

                case DIST:                          // i: get distance next
                    while (bitsNum < need)
                    {
                        if (availIn != 0)
                        {
                            r = RCode.Okay;
                        }
                        else
                        {
                            return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));
                        }
                        availIn--;
                        bits    |= blocks.InputBuffer[nextIn++] << bitsNum;
                        bitsNum += 8;
                    }

                    tindex = (tree_index + (bits & Constants.InflateMask[need])) * 3;

                    bits   >>= tree[tindex + 1];
                    bitsNum -= tree[tindex + 1];

                    e = tree[tindex];
                    if ((e & 0x10) != 0)
                    {
                        // distance
                        bitsToGet = 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;
                    }
                    throw new InvalidDataException("invalid distance code");


                case DISTEXT:                          // i: getting distance extra
                    while (bitsNum < bitsToGet)
                    {
                        if (availIn != 0)
                        {
                            r = RCode.Okay;
                        }
                        else
                        {
                            return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));
                        }
                        availIn--;
                        bits    |= blocks.InputBuffer[nextIn++] << bitsNum;
                        bitsNum += 8;
                    }

                    dist += (bits & Constants.InflateMask[bitsToGet]);

                    bits   >>= bitsToGet;
                    bitsNum -= bitsToGet;

                    mode = COPY;
                    goto case COPY;

                case COPY:                          // o: copying bytes in window, waiting for space
                    int f = q - dist;               // pointer to copy strings from
                    while (f < 0)
                    {
                        // modulo window size-"while" instead
                        f += blocks.end;                                 // of "if" handles invalid distances
                    }
                    while (len != 0)
                    {
                        if (m == 0)
                        {
                            if (q == blocks.end && blocks.readAt != 0)
                            {
                                q = 0;
                                m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
                            }

                            if (m == 0)
                            {
                                blocks.writeAt = q;
                                r = blocks.Flush(r);
                                q = blocks.writeAt;
                                m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

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

                                if (m == 0)
                                {
                                    return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));
                                }
                            }
                        }

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

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

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

                        if (m == 0)
                        {
                            blocks.writeAt = q;
                            r = blocks.Flush(r);
                            q = blocks.writeAt;
                            m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

                            if (q == blocks.end && blocks.readAt != 0)
                            {
                                q = 0;
                                m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
                            }
                            if (m == 0)
                            {
                                return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));
                            }
                        }
                    }

                    r = RCode.Okay;
                    blocks.window[q++] = (byte)lit;
                    m--;
                    mode = START;
                    break;

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

                    blocks.writeAt = q;
                    r = blocks.Flush(r);
                    q = blocks.writeAt;
                    m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

                    if (blocks.readAt != blocks.writeAt)
                    {
                        return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));
                    }
                    mode = END;
                    goto case END;

                case END:
                    r = RCode.StreamEnd;
                    return(blocks.RanOutOfInput(bits, bitsNum, availIn, nextIn, q, r));

                default:
                    throw new InvalidDataException("Encountered error: " + mode);
                }
            }
        }
Ejemplo n.º 6
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int tindex; // temporary pointer
            int e; // extra bits or operation

            ZlibCodec z = blocks.codec;
            int nextIn = z.NextIn;// input data pointer
            int availIn = z.AvailableBytesIn; // bytes available there
            int bits = blocks.bitb; // bit buffer
            int bitsNum = blocks.bitk; // bits in bit buffer
            int q = blocks.writeAt;  // output window write pointer
            int m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q; // bytes to end of window or read pointer

            // 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 && availIn >= 10) {
                            blocks.UpdateState( bits, bitsNum, availIn, nextIn, q );
                            r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);

                            nextIn = z.NextIn;
                            availIn = z.AvailableBytesIn;
                            bits = blocks.bitb;
                            bitsNum = blocks.bitk;
                            q = blocks.writeAt;
                            m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

                            if (r != RCode.Okay)
                            {
                                mode = (r == RCode.StreamEnd) ? WASH : BADCODE;
                                break;
                            }
                        }
                        need = lbits;
                        tree = ltree;
                        tree_index = ltree_index;

                        mode = LEN;
                        goto case LEN;

                    case LEN:  // i: get length/literal/eob next
                        while (bitsNum < need) {
                            if (availIn != 0) {
                                r = RCode.Okay;
                            } else {
                                return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );
                            }
                            availIn--;
                            bits |= z.InputBuffer[nextIn++] << bitsNum;
                            bitsNum += 8;
                        }

                        tindex = (tree_index + (bits & Constants.InflateMask[need])) * 3;

                        bits >>= (tree[tindex + 1]);
                        bitsNum -= (tree[tindex + 1]);

                        e = tree[tindex];

                        if (e == 0) {
                            // literal
                            lit = tree[tindex + 2];
                            mode = LIT;
                            break;
                        }
                        if ((e & 16) != 0) {
                            // length
                            bitsToGet = 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;
                        }
                        throw new InvalidDataException( "invalid literal/length code" );

                    case LENEXT:  // i: getting length extra (have base)
                        while (bitsNum < bitsToGet) {
                            if (availIn != 0) {
                                r = RCode.Okay;
                            } else {
                                return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );
                            }
                            availIn--;
                            bits |= z.InputBuffer[nextIn++] << bitsNum;
                            bitsNum += 8;
                        }

                        len += (bits & Constants.InflateMask[bitsToGet]);

                        bits >>= bitsToGet;
                        bitsNum -= bitsToGet;

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

                    case DIST:  // i: get distance next
                        while (bitsNum < need) {
                            if (availIn != 0) {
                                r = RCode.Okay;
                            } else {
                                return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );
                            }
                            availIn--;
                            bits |= z.InputBuffer[nextIn++] << bitsNum;
                            bitsNum += 8;
                        }

                        tindex = (tree_index + (bits & Constants.InflateMask[need])) * 3;

                        bits >>= tree[tindex + 1];
                        bitsNum -= tree[tindex + 1];

                        e = tree[tindex];
                        if ((e & 0x10) != 0) {
                            // distance
                            bitsToGet = 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;
                        }
                        throw new InvalidDataException( "invalid distance code" );

                    case DISTEXT:  // i: getting distance extra
                        while (bitsNum < bitsToGet) {
                            if (availIn != 0) {
                                r = RCode.Okay;
                            } else {
                                return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );
                            }
                            availIn--;
                            bits |= z.InputBuffer[nextIn++] << bitsNum;
                            bitsNum += 8;
                        }

                        dist += (bits & Constants.InflateMask[bitsToGet]);

                        bits >>= bitsToGet;
                        bitsNum -= bitsToGet;

                        mode = COPY;
                        goto case COPY;

                    case COPY:  // o: copying bytes in window, waiting for space
                        int f = q - dist; // pointer to copy strings from
                        while (f < 0) {
                            // modulo window size-"while" instead
                            f += blocks.end; // of "if" handles invalid distances
                        }
                        while (len != 0) {
                            if (m == 0) {
                                if (q == blocks.end && blocks.readAt != 0) {
                                    q = 0;
                                    m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
                                }

                                if (m == 0) {
                                    blocks.writeAt = q;
                                    r = blocks.Flush(r);
                                    q = blocks.writeAt;
                                    m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

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

                                    if (m == 0) {
                                        return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );
                                    }
                                }
                            }

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

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

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

                            if (m == 0) {
                                blocks.writeAt = q;
                                r = blocks.Flush(r);
                                q = blocks.writeAt;
                                m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

                                if (q == blocks.end && blocks.readAt != 0)
                                {
                                    q = 0;
                                    m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
                                }
                                if (m == 0) {
                                    return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );
                                }
                            }
                        }

                        r = RCode.Okay;
                        blocks.window[q++] = (byte)lit;
                        m--;
                        mode = START;
                        break;

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

                        blocks.writeAt = q;
                        r = blocks.Flush(r);
                        q = blocks.writeAt;
                        m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

                        if (blocks.readAt != blocks.writeAt) {
                            return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );
                        }
                        mode = END;
                        goto case END;

                    case END:
                        r = RCode.StreamEnd;
                        return blocks.RanOutOfInput( bits, bitsNum, availIn, nextIn, q, r );

                    default:
                        throw new InvalidDataException( "Encountered error: " + mode );
                }
            }
        }
Ejemplo n.º 7
0
        internal int Process(InflateBlocks blocks, int r)
        {
            ZlibCodec z    = blocks._codec;
            int       num1 = z.NextIn;
            int       num2 = z.AvailableBytesIn;
            int       num3 = blocks.bitb;
            int       num4 = blocks.bitk;
            int       num5 = blocks.writeAt;
            int       num6 = num5 < blocks.readAt ? blocks.readAt - num5 - 1 : blocks.end - num5;

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

                case 1:
                    int index1 = this.need;
                    while (num4 < index1)
                    {
                        if (num2 != 0)
                        {
                            r = 0;
                            --num2;
                            num3 |= ((int)z.InputBuffer[num1++] & (int)byte.MaxValue) << num4;
                            num4 += 8;
                        }
                        else
                        {
                            blocks.bitb        = num3;
                            blocks.bitk        = num4;
                            z.AvailableBytesIn = num2;
                            z.TotalBytesIn    += (long)(num1 - z.NextIn);
                            z.NextIn           = num1;
                            blocks.writeAt     = num5;
                            return(blocks.Flush(r));
                        }
                    }
                    int index2 = (this.tree_index + (num3 & InternalInflateConstants.InflateMask[index1])) * 3;
                    num3 >>= this.tree[index2 + 1];
                    num4  -= this.tree[index2 + 1];
                    int num7 = this.tree[index2];
                    if (num7 == 0)
                    {
                        this.lit  = this.tree[index2 + 2];
                        this.mode = 6;
                        break;
                    }
                    if ((num7 & 16) != 0)
                    {
                        this.bitsToGet = num7 & 15;
                        this.len       = this.tree[index2 + 2];
                        this.mode      = 2;
                        break;
                    }
                    if ((num7 & 64) == 0)
                    {
                        this.need       = num7;
                        this.tree_index = index2 / 3 + this.tree[index2 + 2];
                        break;
                    }
                    if ((num7 & 32) != 0)
                    {
                        this.mode = 7;
                        break;
                    }
                    goto label_18;

                case 2:
                    int index3 = this.bitsToGet;
                    while (num4 < index3)
                    {
                        if (num2 != 0)
                        {
                            r = 0;
                            --num2;
                            num3 |= ((int)z.InputBuffer[num1++] & (int)byte.MaxValue) << num4;
                            num4 += 8;
                        }
                        else
                        {
                            blocks.bitb        = num3;
                            blocks.bitk        = num4;
                            z.AvailableBytesIn = num2;
                            z.TotalBytesIn    += (long)(num1 - z.NextIn);
                            z.NextIn           = num1;
                            blocks.writeAt     = num5;
                            return(blocks.Flush(r));
                        }
                    }
                    this.len       += num3 & InternalInflateConstants.InflateMask[index3];
                    num3          >>= index3;
                    num4           -= index3;
                    this.need       = (int)this.dbits;
                    this.tree       = this.dtree;
                    this.tree_index = this.dtree_index;
                    this.mode       = 3;
                    goto case 3;

                case 3:
                    int index4 = this.need;
                    while (num4 < index4)
                    {
                        if (num2 != 0)
                        {
                            r = 0;
                            --num2;
                            num3 |= ((int)z.InputBuffer[num1++] & (int)byte.MaxValue) << num4;
                            num4 += 8;
                        }
                        else
                        {
                            blocks.bitb        = num3;
                            blocks.bitk        = num4;
                            z.AvailableBytesIn = num2;
                            z.TotalBytesIn    += (long)(num1 - z.NextIn);
                            z.NextIn           = num1;
                            blocks.writeAt     = num5;
                            return(blocks.Flush(r));
                        }
                    }
                    int index5 = (this.tree_index + (num3 & InternalInflateConstants.InflateMask[index4])) * 3;
                    num3 >>= this.tree[index5 + 1];
                    num4  -= this.tree[index5 + 1];
                    int num8 = this.tree[index5];
                    if ((num8 & 16) != 0)
                    {
                        this.bitsToGet = num8 & 15;
                        this.dist      = this.tree[index5 + 2];
                        this.mode      = 4;
                        break;
                    }
                    if ((num8 & 64) == 0)
                    {
                        this.need       = num8;
                        this.tree_index = index5 / 3 + this.tree[index5 + 2];
                        break;
                    }
                    goto label_34;

                case 4:
                    int index6 = this.bitsToGet;
                    while (num4 < index6)
                    {
                        if (num2 != 0)
                        {
                            r = 0;
                            --num2;
                            num3 |= ((int)z.InputBuffer[num1++] & (int)byte.MaxValue) << num4;
                            num4 += 8;
                        }
                        else
                        {
                            blocks.bitb        = num3;
                            blocks.bitk        = num4;
                            z.AvailableBytesIn = num2;
                            z.TotalBytesIn    += (long)(num1 - z.NextIn);
                            z.NextIn           = num1;
                            blocks.writeAt     = num5;
                            return(blocks.Flush(r));
                        }
                    }
                    this.dist += num3 & InternalInflateConstants.InflateMask[index6];
                    num3     >>= index6;
                    num4      -= index6;
                    this.mode  = 5;
                    goto case 5;

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

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

                case 7:
                    goto label_68;

                case 8:
                    goto label_73;

                case 9:
                    goto label_74;

                default:
                    goto label_75;
                }
            }
label_18:
            this.mode          = 9;
            z.Message          = "invalid literal/length code";
            r                  = -3;
            blocks.bitb        = num3;
            blocks.bitk        = num4;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num1 - z.NextIn);
            z.NextIn           = num1;
            blocks.writeAt     = num5;
            return(blocks.Flush(r));

label_34:
            this.mode          = 9;
            z.Message          = "invalid distance code";
            r                  = -3;
            blocks.bitb        = num3;
            blocks.bitk        = num4;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num1 - z.NextIn);
            z.NextIn           = num1;
            blocks.writeAt     = num5;
            return(blocks.Flush(r));

label_65:
            blocks.bitb        = num3;
            blocks.bitk        = num4;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num1 - z.NextIn);
            z.NextIn           = num1;
            blocks.writeAt     = num5;
            return(blocks.Flush(r));

label_68:
            if (num4 > 7)
            {
                num4 -= 8;
                ++num2;
                --num1;
            }
            blocks.writeAt = num5;
            r    = blocks.Flush(r);
            num5 = blocks.writeAt;
            int num10 = num5 < blocks.readAt ? blocks.readAt - num5 - 1 : blocks.end - num5;

            if (blocks.readAt != blocks.writeAt)
            {
                blocks.bitb        = num3;
                blocks.bitk        = num4;
                z.AvailableBytesIn = num2;
                z.TotalBytesIn    += (long)(num1 - z.NextIn);
                z.NextIn           = num1;
                blocks.writeAt     = num5;
                return(blocks.Flush(r));
            }
            this.mode = 8;
label_73:
            r                  = 1;
            blocks.bitb        = num3;
            blocks.bitk        = num4;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num1 - z.NextIn);
            z.NextIn           = num1;
            blocks.writeAt     = num5;
            return(blocks.Flush(r));

label_74:
            r                  = -3;
            blocks.bitb        = num3;
            blocks.bitk        = num4;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num1 - z.NextIn);
            z.NextIn           = num1;
            blocks.writeAt     = num5;
            return(blocks.Flush(r));

label_75:
            r                  = -2;
            blocks.bitb        = num3;
            blocks.bitk        = num4;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += (long)(num1 - z.NextIn);
            z.NextIn           = num1;
            blocks.writeAt     = num5;
            return(blocks.Flush(r));
        }
Ejemplo n.º 8
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int j;                  // temporary storage
            int tindex;             // temporary pointer
            int e;                  // extra bits or operation
            int b = 0;              // bit buffer
            int k = 0;              // bits in bit buffer
            int p = 0;              // input data pointer
            int n;                  // bytes available there
            int q;                  // output window write pointer
            int m;                  // bytes to end of window or read pointer
            int f;                  // pointer to copy strings from

            ZlibCodec z = blocks._codec;

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

            // process input and output based on current state
            while (true)
            {
                switch (mode)
                {
                // waiting for "i:"=input, "o:"=output, "x:"=nothing
                case START:                          // x: set up for LEN
                    if (m >= 258 && n >= 10)
                    {
                        blocks.bitb        = b; blocks.bitk = k;
                        z.AvailableBytesIn = n;
                        z.TotalBytesIn    += p - z.NextIn;
                        z.NextIn           = p;
                        blocks.writeAt     = q;
                        r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);

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

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

                    mode = LEN;
                    goto case LEN;

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

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Z_OK;
                        }
                        else
                        {
                            blocks.bitb        = b; blocks.bitk = k;
                            z.AvailableBytesIn = n;
                            z.TotalBytesIn    += p - z.NextIn;
                            z.NextIn           = p;
                            blocks.writeAt     = q;
                            return(blocks.Flush(r));
                        }
                        n--;
                        b |= (z.InputBuffer[p++] & 0xff) << k;
                        k += 8;
                    }

                    tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;

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

                    e = tree[tindex];

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

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


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

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Z_OK;
                        }
                        else
                        {
                            blocks.bitb        = b; blocks.bitk = k;
                            z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                            blocks.writeAt     = q;
                            return(blocks.Flush(r));
                        }
                        n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                        k      += 8;
                    }

                    len += (b & InternalInflateConstants.InflateMask[j]);

                    b >>= j;
                    k  -= j;

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

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

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Z_OK;
                        }
                        else
                        {
                            blocks.bitb        = b; blocks.bitk = k;
                            z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                            blocks.writeAt     = q;
                            return(blocks.Flush(r));
                        }
                        n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                        k      += 8;
                    }

                    tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;

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

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

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


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

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Z_OK;
                        }
                        else
                        {
                            blocks.bitb        = b; blocks.bitk = k;
                            z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                            blocks.writeAt     = q;
                            return(blocks.Flush(r));
                        }
                        n--; b |= (z.InputBuffer[p++] & 0xff) << k;
                        k      += 8;
                    }

                    dist += (b & InternalInflateConstants.InflateMask[j]);

                    b >>= j;
                    k  -= j;

                    mode = COPY;
                    goto case COPY;

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

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

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

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

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

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

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

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

                    mode = START;
                    break;

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

                    blocks.writeAt = q; r = blocks.Flush(r);
                    q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;

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

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

                case BADCODE:                          // x: got error

                    r = ZlibConstants.Z_DATA_ERROR;

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

                default:
                    r = ZlibConstants.Z_STREAM_ERROR;

                    blocks.bitb        = b; blocks.bitk = k;
                    z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
                    blocks.writeAt     = q;
                    return(blocks.Flush(r));
                }
            }
        }
Ejemplo n.º 9
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       num   = 0;
            int       num2  = 0;
            int       num3  = 0;
            ZlibCodec codec = blocks._codec;

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

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

            while (true)
            {
                switch (this.mode)
                {
                case 0:
                    break;

                case 1:
                    goto IL_01c4;

                case 2:
                    goto IL_0387;

                case 3:
                    goto IL_0471;

                case 4:
                    goto IL_05fd;

                case 5:
                    goto IL_06c3;

                case 6:
                    goto IL_0868;

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

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

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

                default:
                    r                      = -2;
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }
                if (num6 >= 258 && num4 >= 10)
                {
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    r    = this.InflateFast(this.lbits, this.dbits, this.ltree, this.ltree_index, this.dtree, this.dtree_index, blocks, codec);
                    num3 = codec.NextIn;
                    num4 = codec.AvailableBytesIn;
                    num  = blocks.bitb;
                    num2 = blocks.bitk;
                    num5 = blocks.writeAt;
                    num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - num5 - 1));
                    if (r != 0)
                    {
                        this.mode = ((r != 1) ? 9 : 7);
                        continue;
                    }
                }
                this.need       = this.lbits;
                this.tree       = this.ltree;
                this.tree_index = this.ltree_index;
                this.mode       = 1;
                goto IL_01c4;
IL_0868:
                if (num6 == 0)
                {
                    if (num5 == blocks.end && blocks.readAt != 0)
                    {
                        num5 = 0;
                        num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - num5 - 1));
                    }
                    if (num6 == 0)
                    {
                        blocks.writeAt = num5;
                        r    = blocks.Flush(r);
                        num5 = blocks.writeAt;
                        num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - num5 - 1));
                        if (num5 == blocks.end && blocks.readAt != 0)
                        {
                            num5 = 0;
                            num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - num5 - 1));
                        }
                        if (num6 == 0)
                        {
                            break;
                        }
                    }
                }
                r = 0;
                blocks.window[num5++] = (byte)this.lit;
                num6--;
                this.mode = 0;
                continue;
IL_0471:
                int num8 = this.need;
                while (num2 < num8)
                {
                    if (num4 != 0)
                    {
                        r = 0;
                        num4--;
                        num  |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                        num2 += 8;
                        continue;
                    }
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }
                int num10 = (this.tree_index + (num & InternalInflateConstants.InflateMask[num8])) * 3;
                num >>= this.tree[num10 + 1];
                num2 -= this.tree[num10 + 1];
                int num11 = this.tree[num10];
                if ((num11 & 0x10) != 0)
                {
                    this.bitsToGet = (num11 & 0xF);
                    this.dist      = this.tree[num10 + 2];
                    this.mode      = 4;
                    continue;
                }
                if ((num11 & 0x40) == 0)
                {
                    this.need       = num11;
                    this.tree_index = num10 / 3 + this.tree[num10 + 2];
                    continue;
                }
                this.mode              = 9;
                codec.Message          = "invalid distance code";
                r                      = -3;
                blocks.bitb            = num;
                blocks.bitk            = num2;
                codec.AvailableBytesIn = num4;
                codec.TotalBytesIn    += num3 - codec.NextIn;
                codec.NextIn           = num3;
                blocks.writeAt         = num5;
                return(blocks.Flush(r));

IL_05fd:
                num8 = this.bitsToGet;
                while (num2 < num8)
                {
                    if (num4 != 0)
                    {
                        r = 0;
                        num4--;
                        num  |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                        num2 += 8;
                        continue;
                    }
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }
                this.dist += (num & InternalInflateConstants.InflateMask[num8]);
                num      >>= num8;
                num2      -= num8;
                this.mode  = 5;
                goto IL_06c3;
IL_06c3:
                int i;
                for (i = num5 - this.dist; i < 0; i += blocks.end)
                {
                }
                while (this.len != 0)
                {
                    if (num6 == 0)
                    {
                        if (num5 == blocks.end && blocks.readAt != 0)
                        {
                            num5 = 0;
                            num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - num5 - 1));
                        }
                        if (num6 == 0)
                        {
                            blocks.writeAt = num5;
                            r    = blocks.Flush(r);
                            num5 = blocks.writeAt;
                            num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - num5 - 1));
                            if (num5 == blocks.end && blocks.readAt != 0)
                            {
                                num5 = 0;
                                num6 = ((num5 >= blocks.readAt) ? (blocks.end - num5) : (blocks.readAt - num5 - 1));
                            }
                            if (num6 == 0)
                            {
                                blocks.bitb            = num;
                                blocks.bitk            = num2;
                                codec.AvailableBytesIn = num4;
                                codec.TotalBytesIn    += num3 - codec.NextIn;
                                codec.NextIn           = num3;
                                blocks.writeAt         = num5;
                                return(blocks.Flush(r));
                            }
                        }
                    }
                    blocks.window[num5++] = blocks.window[i++];
                    num6--;
                    if (i == blocks.end)
                    {
                        i = 0;
                    }
                    this.len--;
                }
                this.mode = 0;
                continue;
IL_0387:
                num8 = this.bitsToGet;
                while (num2 < num8)
                {
                    if (num4 != 0)
                    {
                        r = 0;
                        num4--;
                        num  |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                        num2 += 8;
                        continue;
                    }
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }
                this.len       += (num & InternalInflateConstants.InflateMask[num8]);
                num           >>= num8;
                num2           -= num8;
                this.need       = this.dbits;
                this.tree       = this.dtree;
                this.tree_index = this.dtree_index;
                this.mode       = 3;
                goto IL_0471;
IL_01c4:
                num8 = this.need;
                while (num2 < num8)
                {
                    if (num4 != 0)
                    {
                        r = 0;
                        num4--;
                        num  |= (codec.InputBuffer[num3++] & 0xFF) << num2;
                        num2 += 8;
                        continue;
                    }
                    blocks.bitb            = num;
                    blocks.bitk            = num2;
                    codec.AvailableBytesIn = num4;
                    codec.TotalBytesIn    += num3 - codec.NextIn;
                    codec.NextIn           = num3;
                    blocks.writeAt         = num5;
                    return(blocks.Flush(r));
                }
                num10 = (this.tree_index + (num & InternalInflateConstants.InflateMask[num8])) * 3;
                num >>= this.tree[num10 + 1];
                num2 -= this.tree[num10 + 1];
                num11 = this.tree[num10];
                if (num11 == 0)
                {
                    this.lit  = this.tree[num10 + 2];
                    this.mode = 6;
                    continue;
                }
                if ((num11 & 0x10) != 0)
                {
                    this.bitsToGet = (num11 & 0xF);
                    this.len       = this.tree[num10 + 2];
                    this.mode      = 2;
                    continue;
                }
                if ((num11 & 0x40) == 0)
                {
                    this.need       = num11;
                    this.tree_index = num10 / 3 + this.tree[num10 + 2];
                    continue;
                }
                if ((num11 & 0x20) != 0)
                {
                    this.mode = 7;
                    continue;
                }
                this.mode              = 9;
                codec.Message          = "invalid literal/length code";
                r                      = -3;
                blocks.bitb            = num;
                blocks.bitk            = num2;
                codec.AvailableBytesIn = num4;
                codec.TotalBytesIn    += num3 - codec.NextIn;
                codec.NextIn           = num3;
                blocks.writeAt         = num5;
                return(blocks.Flush(r));
            }
            blocks.bitb            = num;
            blocks.bitk            = num2;
            codec.AvailableBytesIn = num4;
            codec.TotalBytesIn    += num3 - codec.NextIn;
            codec.NextIn           = num3;
            blocks.writeAt         = num5;
            return(blocks.Flush(r));
        }
Ejemplo n.º 10
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       num;
            int       num10;
            bool      flag;
            int       number = 0;
            int       bitk   = 0;
            int       nextIn = 0;
            ZlibCodec z      = blocks._codec;

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

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

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

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

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

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

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

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

            case 1:
                goto Label_01C3;

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

            case 3:
                goto Label_04AE;

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

            case 5:
                goto Label_0730;

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

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

            case 8:
                goto Label_0B44;

            case 9:
                r                  = -3;
                blocks.bitb        = number;
                blocks.bitk        = bitk;
                z.AvailableBytesIn = availableBytesIn;
                z.TotalBytesIn    += nextIn - z.NextIn;
                z.NextIn           = nextIn;
                blocks.write       = write;
                return(blocks.Flush(r));
            }
            r                  = -2;
            blocks.bitb        = number;
            blocks.bitk        = bitk;
            z.AvailableBytesIn = availableBytesIn;
            z.TotalBytesIn    += nextIn - z.NextIn;
            z.NextIn           = nextIn;
            blocks.write       = write;
            return(blocks.Flush(r));
        }
Ejemplo n.º 11
0
        // Token: 0x0600079B RID: 1947 RVA: 0x00042D40 File Offset: 0x00040F40
        internal int Process(InflateBlocks blocks, int r)
        {
            ZlibCodec codec = blocks._codec;
            int       num   = codec.NextIn;
            int       num2  = codec.AvailableBytesIn;
            int       num3  = blocks.bitb;
            int       i     = blocks.bitk;
            int       num4  = blocks.writeAt;
            int       num5  = (num4 < blocks.readAt) ? (blocks.readAt - num4 - 1) : (blocks.end - num4);

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

                case 1:
                    goto IL_441;

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

                case 3:
                    goto IL_2D8;

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

                case 5:
                    goto IL_13C;

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

                case 7:
                    goto IL_91D;

                case 8:
                    goto IL_9C2;

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

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

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

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

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

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

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

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

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

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

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

IL_A08:
            r                      = -3;
            blocks.bitb            = num3;
            blocks.bitk            = i;
            codec.AvailableBytesIn = num2;
            codec.TotalBytesIn    += (long)(num - codec.NextIn);
            codec.NextIn           = num;
            blocks.writeAt         = num4;
            return(blocks.Flush(-3));
        }
Ejemplo n.º 12
0
        internal int Process(InflateBlocks blocks, int r)
        {
            int       b2 = 0;
            int       k3 = 0;
            int       p2 = 0;
            ZlibCodec z  = blocks._codec;

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

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

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

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

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

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

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

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

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

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

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

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

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