private void end()
 {
     if (z == null)
         return;
     if (_wantCompress)
     {
         _z.EndDeflate();
     }
     else
     {
         _z.EndInflate();
     }
     _z = null;
 }
 internal int Initialize(ZlibCodec codec, CompressionLevel level)
 {
     return Initialize(codec, level, ZlibConstants.WindowBitsMax);
 }
 internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
 {
         int result;
         
         // build literal/length tree
         initWorkArea(288);
         hn[0] = 0;
         result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
         if (result != Z_OK || bl[0] == 0)
         {
                 if (result == Z_DATA_ERROR)
                 {
                         z.Message = "oversubscribed literal/length tree";
                 }
                 else if (result != Z_MEM_ERROR)
                 {
                         z.Message = "incomplete literal/length tree";
                         result = Z_DATA_ERROR;
                 }
                 return result;
         }
         
         // build distance tree
         initWorkArea(288);
         result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
         
         if (result != Z_OK || (bd[0] == 0 && nl > 257))
         {
                 if (result == Z_DATA_ERROR)
                 {
                         z.Message = "oversubscribed distance tree";
                 }
                 else if (result == Z_BUF_ERROR)
                 {
                         z.Message = "incomplete distance tree";
                         result = Z_DATA_ERROR;
                 }
                 else if (result != Z_MEM_ERROR)
                 {
                         z.Message = "empty distance tree with lengths";
                         result = Z_DATA_ERROR;
                 }
                 return result;
         }
         
         return Z_OK;
 }
 internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
 {
         bl[0] = fixed_bl;
         bd[0] = fixed_bd;
         tl[0] = fixed_tl;
         td[0] = fixed_td;
         return Z_OK;
 }
 // Returns true if inflate is currently at the end of a block generated
 // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
 // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
 // but removes the length bytes of the resulting empty stored block. When
 // decompressing, PPP checks that at the end of input packet, inflate is
 // waiting for these length bytes.
 internal int SyncPoint(ZlibCodec z)
 {
     return blocks.SyncPoint();
 }
 internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
 {
         int result;
         initWorkArea(19);
         hn[0] = 0;
         result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
         
         if (result == Z_DATA_ERROR)
         {
                 z.Message = "oversubscribed dynamic bit lengths tree";
         }
         else if (result == Z_BUF_ERROR || bb[0] == 0)
         {
                 z.Message = "incomplete dynamic bit lengths tree";
                 result = Z_DATA_ERROR;
         }
         return result;
 }
 internal InflateBlocks(ZlibCodec codec, System.Object checkfn, int w)
 {
     _codec = codec;
     hufts = new int[MANY * 3];
     window = new byte[w];
     end = w;
     this.checkfn = checkfn;
     mode = InflateBlockMode.TYPE;
     Reset();
 }
Example #8
0
 internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
 {
     bl[0] = 9;
     bd[0] = 5;
     tl[0] = fixed_tl;
     td[0] = fixed_td;
     return(0);
 }
Example #9
0
 internal int Initialize(ZlibCodec codec, CompressionLevel level, int bits, CompressionStrategy compressionStrategy) =>
 this.Initialize(codec, level, bits, MEM_LEVEL_DEFAULT, compressionStrategy);
        // Called with number of bytes left to write in window at least 258
        // (the maximum string length) and number of input bytes available
        // at least ten.  The ten bytes are six bytes for the longest length/
        // distance pair plus four bytes for overloading the bit buffer.

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

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

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

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

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

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

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

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

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

                        b >>= e; k -= e;

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

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

                        do
                        {

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

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

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

                                b >>= e; k -= e;

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

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

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

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

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

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

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

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

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

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

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

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

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

            return ZlibConstants.Z_OK;
        }
Example #11
0
 internal int Initialize(ZlibCodec codec, CompressionLevel level) =>
 this.Initialize(codec, level, 15);
Example #12
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));
                }
            }
        }
Example #13
0
        internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
        {
            int num  = z.NextIn;
            int num2 = z.AvailableBytesIn;
            int num3 = s.bitb;
            int num4 = s.bitk;
            int num5 = s.writeAt;
            int num6 = (num5 >= s.readAt) ? (s.end - num5) : (s.readAt - num5 - 1);
            int num7 = InternalInflateConstants.InflateMask[bl];
            int num8 = InternalInflateConstants.InflateMask[bd];
            int num13;

            while (true)
            {
                if (num4 < 20)
                {
                    num2--;
                    num3 |= (z.InputBuffer[num++] & 0xFF) << (num4 & 0x1F);
                    num4 += 8;
                }
                else
                {
                    int num10 = num3 & num7;
                    int num11 = (tl_index + num10) * 3;
                    int num12;
                    if ((num12 = tl[num11]) != 0)
                    {
                        while (true)
                        {
                            num3 >>= tl[num11 + 1];
                            num4  -= tl[num11 + 1];
                            if ((num12 & 0x10) != 0)
                            {
                                num12 &= 0xF;
                                num13  = tl[num11 + 2] + (num3 & InternalInflateConstants.InflateMask[num12]);
                                num3 >>= num12;
                                for (num4 -= num12; num4 < 15; num4 += 8)
                                {
                                    num2--;
                                    num3 |= (z.InputBuffer[num++] & 0xFF) << num4;
                                }
                                num10 = (num3 & num8);
                                num11 = (td_index + num10) * 3;
                                num12 = td[num11];
                                while (true)
                                {
                                    num3 >>= td[num11 + 1];
                                    num4  -= td[num11 + 1];
                                    if ((num12 & 0x10) != 0)
                                    {
                                        break;
                                    }
                                    if ((num12 & 0x40) != 0)
                                    {
                                        z.Message          = "invalid distance code";
                                        num13              = z.AvailableBytesIn - num2;
                                        num13              = ((num4 >> 3 >= num13) ? num13 : (num4 >> 3));
                                        num2              += num13;
                                        num               -= num13;
                                        num4              -= num13 << 3;
                                        s.bitb             = num3;
                                        s.bitk             = num4;
                                        z.AvailableBytesIn = num2;
                                        z.TotalBytesIn    += num - z.NextIn;
                                        z.NextIn           = num;
                                        s.writeAt          = num5;
                                        return(-3);
                                    }
                                    num10 += td[num11 + 2];
                                    num10 += (num3 & InternalInflateConstants.InflateMask[num12]);
                                    num11  = (td_index + num10) * 3;
                                    num12  = td[num11];
                                }
                                for (num12 &= 0xF; num4 < num12; num4 += 8)
                                {
                                    num2--;
                                    num3 |= (z.InputBuffer[num++] & 0xFF) << num4;
                                }
                                int num16 = td[num11 + 2] + (num3 & InternalInflateConstants.InflateMask[num12]);
                                num3 >>= num12;
                                num4  -= num12;
                                num6  -= num13;
                                int num17;
                                if (num5 >= num16)
                                {
                                    num17 = num5 - num16;
                                    if (num5 - num17 > 0 && 2 > num5 - num17)
                                    {
                                        s.window[num5++] = s.window[num17++];
                                        s.window[num5++] = s.window[num17++];
                                        num13           -= 2;
                                    }
                                    else
                                    {
                                        Array.Copy(s.window, num17, s.window, num5, 2);
                                        num5  += 2;
                                        num17 += 2;
                                        num13 -= 2;
                                    }
                                }
                                else
                                {
                                    num17 = num5 - num16;
                                    do
                                    {
                                        num17 += s.end;
                                    }while (num17 < 0);
                                    num12 = s.end - num17;
                                    if (num13 > num12)
                                    {
                                        num13 -= num12;
                                        if (num5 - num17 > 0 && num12 > num5 - num17)
                                        {
                                            do
                                            {
                                                s.window[num5++] = s.window[num17++];
                                            }while (--num12 != 0);
                                        }
                                        else
                                        {
                                            Array.Copy(s.window, num17, s.window, num5, num12);
                                            num5  += num12;
                                            num17 += num12;
                                            num12  = 0;
                                        }
                                        num17 = 0;
                                    }
                                }
                                if (num5 - num17 > 0 && num13 > num5 - num17)
                                {
                                    do
                                    {
                                        s.window[num5++] = s.window[num17++];
                                    }while (--num13 != 0);
                                }
                                else
                                {
                                    Array.Copy(s.window, num17, s.window, num5, num13);
                                    num5  += num13;
                                    num17 += num13;
                                    num13  = 0;
                                }
                                break;
                            }
                            if ((num12 & 0x40) != 0)
                            {
                                if ((num12 & 0x20) != 0)
                                {
                                    num13              = z.AvailableBytesIn - num2;
                                    num13              = ((num4 >> 3 >= num13) ? num13 : (num4 >> 3));
                                    num2              += num13;
                                    num               -= num13;
                                    num4              -= num13 << 3;
                                    s.bitb             = num3;
                                    s.bitk             = num4;
                                    z.AvailableBytesIn = num2;
                                    z.TotalBytesIn    += num - z.NextIn;
                                    z.NextIn           = num;
                                    s.writeAt          = num5;
                                    return(1);
                                }
                                z.Message          = "invalid literal/length code";
                                num13              = z.AvailableBytesIn - num2;
                                num13              = ((num4 >> 3 >= num13) ? num13 : (num4 >> 3));
                                num2              += num13;
                                num               -= num13;
                                num4              -= num13 << 3;
                                s.bitb             = num3;
                                s.bitk             = num4;
                                z.AvailableBytesIn = num2;
                                z.TotalBytesIn    += num - z.NextIn;
                                z.NextIn           = num;
                                s.writeAt          = num5;
                                return(-3);
                            }
                            num10 += tl[num11 + 2];
                            num10 += (num3 & InternalInflateConstants.InflateMask[num12]);
                            num11  = (tl_index + num10) * 3;
                            if ((num12 = tl[num11]) == 0)
                            {
                                num3           >>= tl[num11 + 1];
                                num4            -= tl[num11 + 1];
                                s.window[num5++] = (byte)tl[num11 + 2];
                                num6--;
                                break;
                            }
                        }
                    }
                    else
                    {
                        num3           >>= tl[num11 + 1];
                        num4            -= tl[num11 + 1];
                        s.window[num5++] = (byte)tl[num11 + 2];
                        num6--;
                    }
                    if (num6 < 258 || num2 < 10)
                    {
                        break;
                    }
                }
            }
            num13              = z.AvailableBytesIn - num2;
            num13              = ((num4 >> 3 >= num13) ? num13 : (num4 >> 3));
            num2              += num13;
            num               -= num13;
            num4              -= num13 << 3;
            s.bitb             = num3;
            s.bitk             = num4;
            z.AvailableBytesIn = num2;
            z.TotalBytesIn    += num - z.NextIn;
            z.NextIn           = num;
            s.writeAt          = num5;
            return(0);
        }
 internal int Initialize(ZlibCodec codec, CompressionLevel level, int bits, CompressionStrategy compressionStrategy)
 {
     return Initialize(codec, level, bits, MEM_LEVEL_DEFAULT, compressionStrategy);
 }
        internal int Initialize(ZlibCodec codec, int w)
        {
            _codec = codec;
            _codec.Message = null;
            blocks = null;

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

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

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

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

            // reset state
            Reset();
            return ZlibConstants.Z_OK;
        }
        internal int Initialize(ZlibCodec codec, CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
        {
            _codec = codec;
            _codec.Message = null;

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

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

            _codec.dstate = this;

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

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

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

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

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

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

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

            Reset();
            return ZlibConstants.Z_OK;
        }
Example #17
0
        internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
        {
            initWorkArea(288);
            hn[0] = 0;
            int num = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);

            if (num != 0 || bl[0] == 0)
            {
                switch (num)
                {
                case -3:
                    z.Message = "oversubscribed literal/length tree";
                    break;

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

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

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

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

                case -4:
                    break;
                }
                return(num);
            }
            return(0);
        }