inflate_trees_dynamic() приватный Метод

private inflate_trees_dynamic ( int nl, int nd, int c, int bl, int bd, int tl, int td, int hp, ZlibCodec z ) : int
nl int
nd int
c int
bl int
bd int
tl int
td int
hp int
z ZlibCodec
Результат int
Пример #1
0
        internal int Process(int r)
        {
            int p = _codec.NextIn;
            int m = _codec.AvailableBytesIn;
            int b = bitb;
            int k = bitk;
            int q = writeAt;
            int l = ((q < readAt) ? (readAt - q - 1) : (end - q));

            while (true)
            {
                switch (mode)
                {
                case InflateBlockMode.TYPE:
                {
                    for (; k < 3; k += 8)
                    {
                        if (m != 0)
                        {
                            r = 0;
                            m--;
                            b |= (_codec.InputBuffer[p++] & 0xFF) << k;
                            continue;
                        }
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    int t = b & 7;
                    last = t & 1;
                    switch ((uint)t >> 1)
                    {
                    case 0u:
                        b  >>= 3;
                        k   -= 3;
                        t    = k & 7;
                        b  >>= t;
                        k   -= t;
                        mode = InflateBlockMode.LENS;
                        break;

                    case 1u:
                    {
                        int[]   bl = new int[1];
                        int[]   bd = new int[1];
                        int[][] tl = new int[1][];
                        int[][] td = new int[1][];
                        InfTree.inflate_trees_fixed(bl, bd, tl, td, _codec);
                        codes.Init(bl[0], bd[0], tl[0], 0, td[0], 0);
                        b  >>= 3;
                        k   -= 3;
                        mode = InflateBlockMode.CODES;
                        break;
                    }

                    case 2u:
                        b  >>= 3;
                        k   -= 3;
                        mode = InflateBlockMode.TABLE;
                        break;

                    case 3u:
                        b                     >>= 3;
                        k                      -= 3;
                        mode                    = InflateBlockMode.BAD;
                        _codec.Message          = "invalid block type";
                        r                       = -3;
                        bitb                    = b;
                        bitk                    = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt                 = q;
                        return(Flush(r));
                    }
                    break;
                }

                case InflateBlockMode.LENS:
                    for (; k < 32; k += 8)
                    {
                        if (m != 0)
                        {
                            r = 0;
                            m--;
                            b |= (_codec.InputBuffer[p++] & 0xFF) << k;
                            continue;
                        }
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    if (((~b >> 16) & 0xFFFF) != (b & 0xFFFF))
                    {
                        mode           = InflateBlockMode.BAD;
                        _codec.Message = "invalid stored block lengths";
                        r    = -3;
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    left = b & 0xFFFF;
                    b    = (k = 0);
                    mode = ((left != 0) ? InflateBlockMode.STORED : ((last != 0) ? InflateBlockMode.DRY : InflateBlockMode.TYPE));
                    break;

                case InflateBlockMode.STORED:
                {
                    if (m == 0)
                    {
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    if (l == 0)
                    {
                        if (q == end && readAt != 0)
                        {
                            q = 0;
                            l = ((q < readAt) ? (readAt - q - 1) : (end - q));
                        }
                        if (l == 0)
                        {
                            writeAt = q;
                            r       = Flush(r);
                            q       = writeAt;
                            l       = ((q < readAt) ? (readAt - q - 1) : (end - q));
                            if (q == end && readAt != 0)
                            {
                                q = 0;
                                l = ((q < readAt) ? (readAt - q - 1) : (end - q));
                            }
                            if (l == 0)
                            {
                                bitb = b;
                                bitk = k;
                                _codec.AvailableBytesIn = m;
                                _codec.TotalBytesIn    += p - _codec.NextIn;
                                _codec.NextIn           = p;
                                writeAt = q;
                                return(Flush(r));
                            }
                        }
                    }
                    r = 0;
                    int t = left;
                    if (t > m)
                    {
                        t = m;
                    }
                    if (t > l)
                    {
                        t = l;
                    }
                    Array.Copy(_codec.InputBuffer, p, window, q, t);
                    p += t;
                    m -= t;
                    q += t;
                    l -= t;
                    if ((left -= t) == 0)
                    {
                        mode = ((last != 0) ? InflateBlockMode.DRY : InflateBlockMode.TYPE);
                    }
                    break;
                }

                case InflateBlockMode.TABLE:
                {
                    for (; k < 14; k += 8)
                    {
                        if (m != 0)
                        {
                            r = 0;
                            m--;
                            b |= (_codec.InputBuffer[p++] & 0xFF) << k;
                            continue;
                        }
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    int t = (table = b & 0x3FFF);
                    if ((t & 0x1F) > 29 || ((t >> 5) & 0x1F) > 29)
                    {
                        mode           = InflateBlockMode.BAD;
                        _codec.Message = "too many length or distance symbols";
                        r    = -3;
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    t = 258 + (t & 0x1F) + ((t >> 5) & 0x1F);
                    if (blens == null || blens.Length < t)
                    {
                        blens = new int[t];
                    }
                    else
                    {
                        Array.Clear(blens, 0, t);
                    }
                    b   >>= 14;
                    k    -= 14;
                    index = 0;
                    mode  = InflateBlockMode.BTREE;
                    goto case InflateBlockMode.BTREE;
                }

                case InflateBlockMode.BTREE:
                {
                    while (index < 4 + (table >> 10))
                    {
                        for (; k < 3; k += 8)
                        {
                            if (m != 0)
                            {
                                r = 0;
                                m--;
                                b |= (_codec.InputBuffer[p++] & 0xFF) << k;
                                continue;
                            }
                            bitb = b;
                            bitk = k;
                            _codec.AvailableBytesIn = m;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        blens[border[index++]] = b & 7;
                        b >>= 3;
                        k  -= 3;
                    }
                    while (index < 19)
                    {
                        blens[border[index++]] = 0;
                    }
                    bb[0] = 7;
                    int t = inftree.inflate_trees_bits(blens, bb, tb, hufts, _codec);
                    if (t != 0)
                    {
                        r = t;
                        if (r == -3)
                        {
                            blens = null;
                            mode  = InflateBlockMode.BAD;
                        }
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    index = 0;
                    mode  = InflateBlockMode.DTREE;
                    goto case InflateBlockMode.DTREE;
                }

                case InflateBlockMode.DTREE:
                {
                    int t;
                    while (true)
                    {
                        t = table;
                        if (index >= 258 + (t & 0x1F) + ((t >> 5) & 0x1F))
                        {
                            break;
                        }
                        for (t = bb[0]; k < t; k += 8)
                        {
                            if (m != 0)
                            {
                                r = 0;
                                m--;
                                b |= (_codec.InputBuffer[p++] & 0xFF) << k;
                                continue;
                            }
                            bitb = b;
                            bitk = k;
                            _codec.AvailableBytesIn = m;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        t = hufts[(tb[0] + (b & InternalInflateConstants.InflateMask[t])) * 3 + 1];
                        int c = hufts[(tb[0] + (b & InternalInflateConstants.InflateMask[t])) * 3 + 2];
                        if (c < 16)
                        {
                            b >>= t;
                            k  -= t;
                            blens[index++] = c;
                            continue;
                        }
                        int i = ((c == 18) ? 7 : (c - 14));
                        int j = ((c == 18) ? 11 : 3);
                        for (; k < t + i; k += 8)
                        {
                            if (m != 0)
                            {
                                r = 0;
                                m--;
                                b |= (_codec.InputBuffer[p++] & 0xFF) << k;
                                continue;
                            }
                            bitb = b;
                            bitk = k;
                            _codec.AvailableBytesIn = m;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        b >>= t;
                        k  -= t;
                        j  += b & InternalInflateConstants.InflateMask[i];
                        b >>= i;
                        k  -= i;
                        i   = index;
                        t   = table;
                        if (i + j > 258 + (t & 0x1F) + ((t >> 5) & 0x1F) || (c == 16 && i < 1))
                        {
                            blens          = null;
                            mode           = InflateBlockMode.BAD;
                            _codec.Message = "invalid bit length repeat";
                            r    = -3;
                            bitb = b;
                            bitk = k;
                            _codec.AvailableBytesIn = m;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        c = ((c == 16) ? blens[i - 1] : 0);
                        do
                        {
                            blens[i++] = c;
                        }while (--j != 0);
                        index = i;
                    }
                    tb[0] = -1;
                    int[] bl2 = new int[1]
                    {
                        9
                    };
                    int[] bd2 = new int[1]
                    {
                        6
                    };
                    int[] tl2 = new int[1];
                    int[] td2 = new int[1];
                    t = table;
                    t = inftree.inflate_trees_dynamic(257 + (t & 0x1F), 1 + ((t >> 5) & 0x1F), blens, bl2, bd2, tl2, td2, hufts, _codec);
                    if (t != 0)
                    {
                        if (t == -3)
                        {
                            blens = null;
                            mode  = InflateBlockMode.BAD;
                        }
                        r    = t;
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    codes.Init(bl2[0], bd2[0], hufts, tl2[0], hufts, td2[0]);
                    mode = InflateBlockMode.CODES;
                    goto case InflateBlockMode.CODES;
                }

                case InflateBlockMode.CODES:
                    bitb = b;
                    bitk = k;
                    _codec.AvailableBytesIn = m;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    r       = codes.Process(this, r);
                    if (r != 1)
                    {
                        return(Flush(r));
                    }
                    r = 0;
                    p = _codec.NextIn;
                    m = _codec.AvailableBytesIn;
                    b = bitb;
                    k = bitk;
                    q = writeAt;
                    l = ((q < readAt) ? (readAt - q - 1) : (end - q));
                    if (last == 0)
                    {
                        mode = InflateBlockMode.TYPE;
                        break;
                    }
                    mode = InflateBlockMode.DRY;
                    goto case InflateBlockMode.DRY;

                case InflateBlockMode.DRY:
                    writeAt = q;
                    r       = Flush(r);
                    q       = writeAt;
                    l       = ((q < readAt) ? (readAt - q - 1) : (end - q));
                    if (readAt != writeAt)
                    {
                        bitb = b;
                        bitk = k;
                        _codec.AvailableBytesIn = m;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    mode = InflateBlockMode.DONE;
                    goto case InflateBlockMode.DONE;

                case InflateBlockMode.DONE:
                    r    = 1;
                    bitb = b;
                    bitk = k;
                    _codec.AvailableBytesIn = m;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    return(Flush(r));

                case InflateBlockMode.BAD:
                    r    = -3;
                    bitb = b;
                    bitk = k;
                    _codec.AvailableBytesIn = m;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    return(Flush(r));

                default:
                    r    = -2;
                    bitb = b;
                    bitk = k;
                    _codec.AvailableBytesIn = m;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    return(Flush(r));
                }
            }
        }
Пример #2
0
        internal int Process(int r)
        {
            int num  = _codec.NextIn;
            int num2 = _codec.AvailableBytesIn;
            int num3 = bitb;
            int i    = bitk;
            int num4 = writeAt;
            int num5 = (num4 < readAt) ? (readAt - num4 - 1) : (end - num4);

            while (true)
            {
                switch (mode)
                {
                case InflateBlockMode.TYPE:
                {
                    for (; i < 3; i += 8)
                    {
                        if (num2 != 0)
                        {
                            r = 0;
                            num2--;
                            num3 |= (_codec.InputBuffer[num++] & 0xFF) << i;
                            continue;
                        }
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    int num6 = num3 & 7;
                    last = (num6 & 1);
                    switch ((uint)num6 >> 1)
                    {
                    case 0u:
                        num3 >>= 3;
                        i     -= 3;
                        num6   = (i & 7);
                        num3 >>= num6;
                        i     -= num6;
                        mode   = InflateBlockMode.LENS;
                        break;

                    case 1u:
                    {
                        int[]   array  = new int[1];
                        int[]   array2 = new int[1];
                        int[][] array3 = new int[1][];
                        int[][] array4 = new int[1][];
                        InfTree.inflate_trees_fixed(array, array2, array3, array4, _codec);
                        codes.Init(array[0], array2[0], array3[0], 0, array4[0], 0);
                        num3 >>= 3;
                        i     -= 3;
                        mode   = InflateBlockMode.CODES;
                        break;
                    }

                    case 2u:
                        num3 >>= 3;
                        i     -= 3;
                        mode   = InflateBlockMode.TABLE;
                        break;

                    case 3u:
                        num3                  >>= 3;
                        i                      -= 3;
                        mode                    = InflateBlockMode.BAD;
                        _codec.Message          = "invalid block type";
                        r                       = -3;
                        bitb                    = num3;
                        bitk                    = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt                 = num4;
                        return(Flush(r));
                    }
                    break;
                }

                case InflateBlockMode.LENS:
                    for (; i < 32; i += 8)
                    {
                        if (num2 != 0)
                        {
                            r = 0;
                            num2--;
                            num3 |= (_codec.InputBuffer[num++] & 0xFF) << i;
                            continue;
                        }
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    if (((~num3 >> 16) & 0xFFFF) != (num3 & 0xFFFF))
                    {
                        mode           = InflateBlockMode.BAD;
                        _codec.Message = "invalid stored block lengths";
                        r    = -3;
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    left = (num3 & 0xFFFF);
                    num3 = (i = 0);
                    mode = ((left != 0) ? InflateBlockMode.STORED : ((last != 0) ? InflateBlockMode.DRY : InflateBlockMode.TYPE));
                    break;

                case InflateBlockMode.STORED:
                {
                    if (num2 == 0)
                    {
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    if (num5 == 0)
                    {
                        if (num4 == end && readAt != 0)
                        {
                            num4 = 0;
                            num5 = ((num4 < readAt) ? (readAt - num4 - 1) : (end - num4));
                        }
                        if (num5 == 0)
                        {
                            writeAt = num4;
                            r       = Flush(r);
                            num4    = writeAt;
                            num5    = ((num4 < readAt) ? (readAt - num4 - 1) : (end - num4));
                            if (num4 == end && readAt != 0)
                            {
                                num4 = 0;
                                num5 = ((num4 < readAt) ? (readAt - num4 - 1) : (end - num4));
                            }
                            if (num5 == 0)
                            {
                                bitb = num3;
                                bitk = i;
                                _codec.AvailableBytesIn = num2;
                                _codec.TotalBytesIn    += num - _codec.NextIn;
                                _codec.NextIn           = num;
                                writeAt = num4;
                                return(Flush(r));
                            }
                        }
                    }
                    r = 0;
                    int num6 = left;
                    if (num6 > num2)
                    {
                        num6 = num2;
                    }
                    if (num6 > num5)
                    {
                        num6 = num5;
                    }
                    Array.Copy(_codec.InputBuffer, num, window, num4, num6);
                    num  += num6;
                    num2 -= num6;
                    num4 += num6;
                    num5 -= num6;
                    if ((left -= num6) == 0)
                    {
                        mode = ((last != 0) ? InflateBlockMode.DRY : InflateBlockMode.TYPE);
                    }
                    break;
                }

                case InflateBlockMode.TABLE:
                {
                    for (; i < 14; i += 8)
                    {
                        if (num2 != 0)
                        {
                            r = 0;
                            num2--;
                            num3 |= (_codec.InputBuffer[num++] & 0xFF) << i;
                            continue;
                        }
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    int num6 = table = (num3 & 0x3FFF);
                    if ((num6 & 0x1F) > 29 || ((num6 >> 5) & 0x1F) > 29)
                    {
                        mode           = InflateBlockMode.BAD;
                        _codec.Message = "too many length or distance symbols";
                        r    = -3;
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    num6 = 258 + (num6 & 0x1F) + ((num6 >> 5) & 0x1F);
                    if (blens == null || blens.Length < num6)
                    {
                        blens = new int[num6];
                    }
                    else
                    {
                        Array.Clear(blens, 0, num6);
                    }
                    num3 >>= 14;
                    i     -= 14;
                    index  = 0;
                    mode   = InflateBlockMode.BTREE;
                    goto case InflateBlockMode.BTREE;
                }

                case InflateBlockMode.BTREE:
                {
                    while (index < 4 + (table >> 10))
                    {
                        for (; i < 3; i += 8)
                        {
                            if (num2 != 0)
                            {
                                r = 0;
                                num2--;
                                num3 |= (_codec.InputBuffer[num++] & 0xFF) << i;
                                continue;
                            }
                            bitb = num3;
                            bitk = i;
                            _codec.AvailableBytesIn = num2;
                            _codec.TotalBytesIn    += num - _codec.NextIn;
                            _codec.NextIn           = num;
                            writeAt = num4;
                            return(Flush(r));
                        }
                        blens[border[index++]] = (num3 & 7);
                        num3 >>= 3;
                        i     -= 3;
                    }
                    while (index < 19)
                    {
                        blens[border[index++]] = 0;
                    }
                    bb[0] = 7;
                    int num6 = inftree.inflate_trees_bits(blens, bb, tb, hufts, _codec);
                    if (num6 != 0)
                    {
                        r = num6;
                        if (r == -3)
                        {
                            blens = null;
                            mode  = InflateBlockMode.BAD;
                        }
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    index = 0;
                    mode  = InflateBlockMode.DTREE;
                    goto case InflateBlockMode.DTREE;
                }

                case InflateBlockMode.DTREE:
                {
                    int num6;
                    while (true)
                    {
                        num6 = table;
                        if (index >= 258 + (num6 & 0x1F) + ((num6 >> 5) & 0x1F))
                        {
                            break;
                        }
                        for (num6 = bb[0]; i < num6; i += 8)
                        {
                            if (num2 != 0)
                            {
                                r = 0;
                                num2--;
                                num3 |= (_codec.InputBuffer[num++] & 0xFF) << i;
                                continue;
                            }
                            bitb = num3;
                            bitk = i;
                            _codec.AvailableBytesIn = num2;
                            _codec.TotalBytesIn    += num - _codec.NextIn;
                            _codec.NextIn           = num;
                            writeAt = num4;
                            return(Flush(r));
                        }
                        num6 = hufts[(tb[0] + (num3 & InternalInflateConstants.InflateMask[num6])) * 3 + 1];
                        int num7 = hufts[(tb[0] + (num3 & InternalInflateConstants.InflateMask[num6])) * 3 + 2];
                        if (num7 < 16)
                        {
                            num3         >>= num6;
                            i             -= num6;
                            blens[index++] = num7;
                            continue;
                        }
                        int num8 = (num7 == 18) ? 7 : (num7 - 14);
                        int num9 = (num7 == 18) ? 11 : 3;
                        for (; i < num6 + num8; i += 8)
                        {
                            if (num2 != 0)
                            {
                                r = 0;
                                num2--;
                                num3 |= (_codec.InputBuffer[num++] & 0xFF) << i;
                                continue;
                            }
                            bitb = num3;
                            bitk = i;
                            _codec.AvailableBytesIn = num2;
                            _codec.TotalBytesIn    += num - _codec.NextIn;
                            _codec.NextIn           = num;
                            writeAt = num4;
                            return(Flush(r));
                        }
                        num3 >>= num6;
                        i     -= num6;
                        num9  += (num3 & InternalInflateConstants.InflateMask[num8]);
                        num3 >>= num8;
                        i     -= num8;
                        num8   = index;
                        num6   = table;
                        if (num8 + num9 > 258 + (num6 & 0x1F) + ((num6 >> 5) & 0x1F) || (num7 == 16 && num8 < 1))
                        {
                            blens          = null;
                            mode           = InflateBlockMode.BAD;
                            _codec.Message = "invalid bit length repeat";
                            r    = -3;
                            bitb = num3;
                            bitk = i;
                            _codec.AvailableBytesIn = num2;
                            _codec.TotalBytesIn    += num - _codec.NextIn;
                            _codec.NextIn           = num;
                            writeAt = num4;
                            return(Flush(r));
                        }
                        num7 = ((num7 == 16) ? blens[num8 - 1] : 0);
                        do
                        {
                            blens[num8++] = num7;
                        }while (--num9 != 0);
                        index = num8;
                    }
                    tb[0] = -1;
                    int[] array5 = new int[1]
                    {
                        9
                    };
                    int[] array6 = new int[1]
                    {
                        6
                    };
                    int[] array7 = new int[1];
                    int[] array8 = new int[1];
                    num6 = table;
                    num6 = inftree.inflate_trees_dynamic(257 + (num6 & 0x1F), 1 + ((num6 >> 5) & 0x1F), blens, array5, array6, array7, array8, hufts, _codec);
                    if (num6 != 0)
                    {
                        if (num6 == -3)
                        {
                            blens = null;
                            mode  = InflateBlockMode.BAD;
                        }
                        r    = num6;
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    codes.Init(array5[0], array6[0], hufts, array7[0], hufts, array8[0]);
                    mode = InflateBlockMode.CODES;
                    goto case InflateBlockMode.CODES;
                }

                case InflateBlockMode.CODES:
                    bitb = num3;
                    bitk = i;
                    _codec.AvailableBytesIn = num2;
                    _codec.TotalBytesIn    += num - _codec.NextIn;
                    _codec.NextIn           = num;
                    writeAt = num4;
                    r       = codes.Process(this, r);
                    if (r != 1)
                    {
                        return(Flush(r));
                    }
                    r    = 0;
                    num  = _codec.NextIn;
                    num2 = _codec.AvailableBytesIn;
                    num3 = bitb;
                    i    = bitk;
                    num4 = writeAt;
                    num5 = ((num4 < readAt) ? (readAt - num4 - 1) : (end - num4));
                    if (last == 0)
                    {
                        mode = InflateBlockMode.TYPE;
                        break;
                    }
                    mode = InflateBlockMode.DRY;
                    goto case InflateBlockMode.DRY;

                case InflateBlockMode.DRY:
                    writeAt = num4;
                    r       = Flush(r);
                    num4    = writeAt;
                    num5    = ((num4 < readAt) ? (readAt - num4 - 1) : (end - num4));
                    if (readAt != writeAt)
                    {
                        bitb = num3;
                        bitk = i;
                        _codec.AvailableBytesIn = num2;
                        _codec.TotalBytesIn    += num - _codec.NextIn;
                        _codec.NextIn           = num;
                        writeAt = num4;
                        return(Flush(r));
                    }
                    mode = InflateBlockMode.DONE;
                    goto case InflateBlockMode.DONE;

                case InflateBlockMode.DONE:
                    r    = 1;
                    bitb = num3;
                    bitk = i;
                    _codec.AvailableBytesIn = num2;
                    _codec.TotalBytesIn    += num - _codec.NextIn;
                    _codec.NextIn           = num;
                    writeAt = num4;
                    return(Flush(r));

                case InflateBlockMode.BAD:
                    r    = -3;
                    bitb = num3;
                    bitk = i;
                    _codec.AvailableBytesIn = num2;
                    _codec.TotalBytesIn    += num - _codec.NextIn;
                    _codec.NextIn           = num;
                    writeAt = num4;
                    return(Flush(r));

                default:
                    r    = -2;
                    bitb = num3;
                    bitk = i;
                    _codec.AvailableBytesIn = num2;
                    _codec.TotalBytesIn    += num - _codec.NextIn;
                    _codec.NextIn           = num;
                    writeAt = num4;
                    return(Flush(r));
                }
            }
        }
Пример #3
0
        internal int Process(int r)
        {
            int p  = _codec.NextIn;
            int n2 = _codec.AvailableBytesIn;
            int b4 = bitb;
            int k2 = bitk;
            int q  = writeAt;
            int m2 = (q < readAt) ? (readAt - q - 1) : (end - q);

            while (true)
            {
                switch (mode)
                {
                case InflateBlockMode.TYPE:
                {
                    for (; k2 < 3; k2 += 8)
                    {
                        if (n2 != 0)
                        {
                            r = 0;
                            n2--;
                            b4 |= (_codec.InputBuffer[p++] & 0xFF) << k2;
                            continue;
                        }
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    int t12 = b4 & 7;
                    last = (t12 & 1);
                    switch ((uint)t12 >> 1)
                    {
                    case 0u:
                        b4 >>= 3;
                        k2  -= 3;
                        t12  = (k2 & 7);
                        b4 >>= t12;
                        k2  -= t12;
                        mode = InflateBlockMode.LENS;
                        break;

                    case 1u:
                    {
                        int[]   bl = new int[1];
                        int[]   bd = new int[1];
                        int[][] tl = new int[1][];
                        int[][] td = new int[1][];
                        InfTree.inflate_trees_fixed(bl, bd, tl, td, _codec);
                        codes.Init(bl[0], bd[0], tl[0], 0, td[0], 0);
                        b4 >>= 3;
                        k2  -= 3;
                        mode = InflateBlockMode.CODES;
                        break;
                    }

                    case 2u:
                        b4 >>= 3;
                        k2  -= 3;
                        mode = InflateBlockMode.TABLE;
                        break;

                    case 3u:
                        b4           >>= 3;
                        k2            -= 3;
                        mode           = InflateBlockMode.BAD;
                        _codec.Message = "invalid block type";
                        r    = -3;
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    break;
                }

                case InflateBlockMode.LENS:
                    for (; k2 < 32; k2 += 8)
                    {
                        if (n2 != 0)
                        {
                            r = 0;
                            n2--;
                            b4 |= (_codec.InputBuffer[p++] & 0xFF) << k2;
                            continue;
                        }
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    if (((~b4 >> 16) & 0xFFFF) != (b4 & 0xFFFF))
                    {
                        mode           = InflateBlockMode.BAD;
                        _codec.Message = "invalid stored block lengths";
                        r    = -3;
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    left = (b4 & 0xFFFF);
                    b4   = (k2 = 0);
                    mode = ((left != 0) ? InflateBlockMode.STORED : ((last != 0) ? InflateBlockMode.DRY : InflateBlockMode.TYPE));
                    break;

                case InflateBlockMode.STORED:
                {
                    if (n2 == 0)
                    {
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    if (m2 == 0)
                    {
                        if (q == end && readAt != 0)
                        {
                            q  = 0;
                            m2 = ((q < readAt) ? (readAt - q - 1) : (end - q));
                        }
                        if (m2 == 0)
                        {
                            writeAt = q;
                            r       = Flush(r);
                            q       = writeAt;
                            m2      = ((q < readAt) ? (readAt - q - 1) : (end - q));
                            if (q == end && readAt != 0)
                            {
                                q  = 0;
                                m2 = ((q < readAt) ? (readAt - q - 1) : (end - q));
                            }
                            if (m2 == 0)
                            {
                                bitb = b4;
                                bitk = k2;
                                _codec.AvailableBytesIn = n2;
                                _codec.TotalBytesIn    += p - _codec.NextIn;
                                _codec.NextIn           = p;
                                writeAt = q;
                                return(Flush(r));
                            }
                        }
                    }
                    r = 0;
                    int t12 = left;
                    if (t12 > n2)
                    {
                        t12 = n2;
                    }
                    if (t12 > m2)
                    {
                        t12 = m2;
                    }
                    Array.Copy(_codec.InputBuffer, p, window, q, t12);
                    p  += t12;
                    n2 -= t12;
                    q  += t12;
                    m2 -= t12;
                    if ((left -= t12) == 0)
                    {
                        mode = ((last != 0) ? InflateBlockMode.DRY : InflateBlockMode.TYPE);
                    }
                    break;
                }

                case InflateBlockMode.TABLE:
                {
                    for (; k2 < 14; k2 += 8)
                    {
                        if (n2 != 0)
                        {
                            r = 0;
                            n2--;
                            b4 |= (_codec.InputBuffer[p++] & 0xFF) << k2;
                            continue;
                        }
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    int t12 = table = (b4 & 0x3FFF);
                    if ((t12 & 0x1F) > 29 || ((t12 >> 5) & 0x1F) > 29)
                    {
                        mode           = InflateBlockMode.BAD;
                        _codec.Message = "too many length or distance symbols";
                        r    = -3;
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    t12 = 258 + (t12 & 0x1F) + ((t12 >> 5) & 0x1F);
                    if (blens == null || blens.Length < t12)
                    {
                        blens = new int[t12];
                    }
                    else
                    {
                        Array.Clear(blens, 0, t12);
                    }
                    b4  >>= 14;
                    k2   -= 14;
                    index = 0;
                    mode  = InflateBlockMode.BTREE;
                    goto case InflateBlockMode.BTREE;
                }

                case InflateBlockMode.BTREE:
                {
                    while (index < 4 + (table >> 10))
                    {
                        for (; k2 < 3; k2 += 8)
                        {
                            if (n2 != 0)
                            {
                                r = 0;
                                n2--;
                                b4 |= (_codec.InputBuffer[p++] & 0xFF) << k2;
                                continue;
                            }
                            bitb = b4;
                            bitk = k2;
                            _codec.AvailableBytesIn = n2;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        blens[border[index++]] = (b4 & 7);
                        b4 >>= 3;
                        k2  -= 3;
                    }
                    while (index < 19)
                    {
                        blens[border[index++]] = 0;
                    }
                    bb[0] = 7;
                    int t12 = inftree.inflate_trees_bits(blens, bb, tb, hufts, _codec);
                    if (t12 != 0)
                    {
                        r = t12;
                        if (r == -3)
                        {
                            blens = null;
                            mode  = InflateBlockMode.BAD;
                        }
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    index = 0;
                    mode  = InflateBlockMode.DTREE;
                    goto case InflateBlockMode.DTREE;
                }

                case InflateBlockMode.DTREE:
                {
                    int t12;
                    while (true)
                    {
                        t12 = table;
                        if (index >= 258 + (t12 & 0x1F) + ((t12 >> 5) & 0x1F))
                        {
                            break;
                        }
                        for (t12 = bb[0]; k2 < t12; k2 += 8)
                        {
                            if (n2 != 0)
                            {
                                r = 0;
                                n2--;
                                b4 |= (_codec.InputBuffer[p++] & 0xFF) << k2;
                                continue;
                            }
                            bitb = b4;
                            bitk = k2;
                            _codec.AvailableBytesIn = n2;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        t12 = hufts[(tb[0] + (b4 & InternalInflateConstants.InflateMask[t12])) * 3 + 1];
                        int c2 = hufts[(tb[0] + (b4 & InternalInflateConstants.InflateMask[t12])) * 3 + 2];
                        if (c2 < 16)
                        {
                            b4           >>= t12;
                            k2            -= t12;
                            blens[index++] = c2;
                            continue;
                        }
                        int i2 = (c2 == 18) ? 7 : (c2 - 14);
                        int j2 = (c2 == 18) ? 11 : 3;
                        for (; k2 < t12 + i2; k2 += 8)
                        {
                            if (n2 != 0)
                            {
                                r = 0;
                                n2--;
                                b4 |= (_codec.InputBuffer[p++] & 0xFF) << k2;
                                continue;
                            }
                            bitb = b4;
                            bitk = k2;
                            _codec.AvailableBytesIn = n2;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        b4 >>= t12;
                        k2  -= t12;
                        j2  += (b4 & InternalInflateConstants.InflateMask[i2]);
                        b4 >>= i2;
                        k2  -= i2;
                        i2   = index;
                        t12  = table;
                        if (i2 + j2 > 258 + (t12 & 0x1F) + ((t12 >> 5) & 0x1F) || (c2 == 16 && i2 < 1))
                        {
                            blens          = null;
                            mode           = InflateBlockMode.BAD;
                            _codec.Message = "invalid bit length repeat";
                            r    = -3;
                            bitb = b4;
                            bitk = k2;
                            _codec.AvailableBytesIn = n2;
                            _codec.TotalBytesIn    += p - _codec.NextIn;
                            _codec.NextIn           = p;
                            writeAt = q;
                            return(Flush(r));
                        }
                        c2 = ((c2 == 16) ? blens[i2 - 1] : 0);
                        do
                        {
                            blens[i2++] = c2;
                        }while (--j2 != 0);
                        index = i2;
                    }
                    tb[0] = -1;
                    int[] bl2 = new int[1]
                    {
                        9
                    };
                    int[] bd2 = new int[1]
                    {
                        6
                    };
                    int[] tl2 = new int[1];
                    int[] td2 = new int[1];
                    t12 = table;
                    t12 = inftree.inflate_trees_dynamic(257 + (t12 & 0x1F), 1 + ((t12 >> 5) & 0x1F), blens, bl2, bd2, tl2, td2, hufts, _codec);
                    if (t12 != 0)
                    {
                        if (t12 == -3)
                        {
                            blens = null;
                            mode  = InflateBlockMode.BAD;
                        }
                        r    = t12;
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    codes.Init(bl2[0], bd2[0], hufts, tl2[0], hufts, td2[0]);
                    mode = InflateBlockMode.CODES;
                    goto case InflateBlockMode.CODES;
                }

                case InflateBlockMode.CODES:
                    bitb = b4;
                    bitk = k2;
                    _codec.AvailableBytesIn = n2;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    r       = codes.Process(this, r);
                    if (r != 1)
                    {
                        return(Flush(r));
                    }
                    r  = 0;
                    p  = _codec.NextIn;
                    n2 = _codec.AvailableBytesIn;
                    b4 = bitb;
                    k2 = bitk;
                    q  = writeAt;
                    m2 = ((q < readAt) ? (readAt - q - 1) : (end - q));
                    if (last == 0)
                    {
                        mode = InflateBlockMode.TYPE;
                        break;
                    }
                    mode = InflateBlockMode.DRY;
                    goto case InflateBlockMode.DRY;

                case InflateBlockMode.DRY:
                    writeAt = q;
                    r       = Flush(r);
                    q       = writeAt;
                    m2      = ((q < readAt) ? (readAt - q - 1) : (end - q));
                    if (readAt != writeAt)
                    {
                        bitb = b4;
                        bitk = k2;
                        _codec.AvailableBytesIn = n2;
                        _codec.TotalBytesIn    += p - _codec.NextIn;
                        _codec.NextIn           = p;
                        writeAt = q;
                        return(Flush(r));
                    }
                    mode = InflateBlockMode.DONE;
                    goto case InflateBlockMode.DONE;

                case InflateBlockMode.DONE:
                    r    = 1;
                    bitb = b4;
                    bitk = k2;
                    _codec.AvailableBytesIn = n2;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    return(Flush(r));

                case InflateBlockMode.BAD:
                    r    = -3;
                    bitb = b4;
                    bitk = k2;
                    _codec.AvailableBytesIn = n2;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    return(Flush(r));

                default:
                    r    = -2;
                    bitb = b4;
                    bitk = k2;
                    _codec.AvailableBytesIn = n2;
                    _codec.TotalBytesIn    += p - _codec.NextIn;
                    _codec.NextIn           = p;
                    writeAt = q;
                    return(Flush(r));
                }
            }
        }