Flush() private method

private Flush ( int r ) : int
r int ///
return int
Exemplo n.º 1
0
        internal int Process(InflateBlocks blocks, int r)
        {
            ZlibCodec z = blocks.Codec;

            // copy input/output information to locals (UPDATE macro restores)
            int p = z.NextIn;    // input data pointer
            int n = z.AvailableBytesIn;
            int b = blocks.Bitb; // bit buffer
            int k = blocks.Bitk; // bits in bit buffer
            int q = blocks.WriteAt;
            int m = q < blocks.ReadAt ? blocks.ReadAt - q - 1 : blocks.End - q;

            // process input and output based on current state
            while (true)
            {
                int tindex; // temporary pointer
                int j;      // temporary storage
                int e;      // extra bits or operation
                switch (this.mode)
                {
                // waiting for "i:"=input, "o:"=output, "x:"=nothing
                case Start:     // x: set up for LEN
                    if (m >= 258 && n >= 10)
                    {
                        blocks.Bitb        = b;
                        blocks.Bitk        = k;
                        z.AvailableBytesIn = n;
                        z.TotalBytesIn    += p - z.NextIn;
                        z.NextIn           = p;
                        blocks.WriteAt     = q;
                        r = InflateFast(
                            this.lbits,
                            this.dbits,
                            this.ltree,
                            this.ltreeIndex,
                            this.dtree,
                            this.dtreeIndex,
                            blocks,
                            z);

                        p = z.NextIn;
                        n = z.AvailableBytesIn;
                        b = blocks.Bitb;
                        k = blocks.Bitk;
                        q = blocks.WriteAt;
                        m = q < blocks.ReadAt ? blocks.ReadAt - q - 1 : blocks.End - q;

                        if (r != ZlibConstants.Zok)
                        {
                            this.mode = (r == ZlibConstants.ZStreamEnd) ? Wash : Badcode;
                            break;
                        }
                    }

                    this.need      = this.lbits;
                    this.tree      = this.ltree;
                    this.treeIndex = this.ltreeIndex;

                    this.mode = Len;
                    goto case Len;

                case Len:     // i: get length/literal/eob next
                    j = this.need;

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Zok;
                        }
                        else
                        {
                            blocks.Bitb        = b;
                            blocks.Bitk        = k;
                            z.AvailableBytesIn = n;
                            z.TotalBytesIn    += p - z.NextIn;
                            z.NextIn           = p;
                            blocks.WriteAt     = q;
                            return(blocks.Flush(r));
                        }

                        n--;
                        b |= (z.InputBuffer[p++] & 0xff) << k;
                        k += 8;
                    }

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

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

                    e = this.tree[tindex];

                    if (e == 0)
                    {
                        // literal
                        this.lit  = this.tree[tindex + 2];
                        this.mode = Lit;
                        break;
                    }

                    if ((e & 16) != 0)
                    {
                        // length
                        this.bitsToGet = e & 15;
                        this.len       = this.tree[tindex + 2];
                        this.mode      = Lenext;
                        break;
                    }

                    if ((e & 64) == 0)
                    {
                        // next table
                        this.need      = e;
                        this.treeIndex = tindex / 3 + this.tree[tindex + 2];
                        break;
                    }

                    if ((e & 32) != 0)
                    {
                        // end of block
                        this.mode = Wash;
                        break;
                    }

                    this.mode = Badcode;     // invalid code
                    z.Message = "invalid literal/length code";
                    r         = ZlibConstants.ZDataError;

                    blocks.Bitb        = b;
                    blocks.Bitk        = k;
                    z.AvailableBytesIn = n;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.WriteAt     = q;
                    return(blocks.Flush(r));

                case Lenext:     // i: getting length extra (have base)
                    j = this.bitsToGet;

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Zok;
                        }
                        else
                        {
                            blocks.Bitb        = b;
                            blocks.Bitk        = k;
                            z.AvailableBytesIn = n;
                            z.TotalBytesIn    += p - z.NextIn;
                            z.NextIn           = p;
                            blocks.WriteAt     = q;
                            return(blocks.Flush(r));
                        }

                        n--;
                        b |= (z.InputBuffer[p++] & 0xff) << k;
                        k += 8;
                    }

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

                    b >>= j;
                    k  -= j;

                    this.need      = this.dbits;
                    this.tree      = this.dtree;
                    this.treeIndex = this.dtreeIndex;
                    this.mode      = Dist;
                    goto case Dist;

                case Dist:     // i: get distance next
                    j = this.need;

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Zok;
                        }
                        else
                        {
                            blocks.Bitb        = b;
                            blocks.Bitk        = k;
                            z.AvailableBytesIn = n;
                            z.TotalBytesIn    += p - z.NextIn;
                            z.NextIn           = p;
                            blocks.WriteAt     = q;
                            return(blocks.Flush(r));
                        }

                        n--;
                        b |= (z.InputBuffer[p++] & 0xff) << k;
                        k += 8;
                    }

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

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

                    e = this.tree[tindex];
                    if ((e & 0x10) != 0)
                    {
                        // distance
                        this.bitsToGet = e & 15;
                        this.dist      = this.tree[tindex + 2];
                        this.mode      = Distext;
                        break;
                    }

                    if ((e & 64) == 0)
                    {
                        // next table
                        this.need      = e;
                        this.treeIndex = tindex / 3 + this.tree[tindex + 2];
                        break;
                    }

                    this.mode = Badcode;     // invalid code
                    z.Message = "invalid distance code";
                    r         = ZlibConstants.ZDataError;

                    blocks.Bitb        = b;
                    blocks.Bitk        = k;
                    z.AvailableBytesIn = n;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.WriteAt     = q;
                    return(blocks.Flush(r));

                case Distext:     // i: getting distance extra
                    j = this.bitsToGet;

                    while (k < j)
                    {
                        if (n != 0)
                        {
                            r = ZlibConstants.Zok;
                        }
                        else
                        {
                            blocks.Bitb        = b;
                            blocks.Bitk        = k;
                            z.AvailableBytesIn = n;
                            z.TotalBytesIn    += p - z.NextIn;
                            z.NextIn           = p;
                            blocks.WriteAt     = q;
                            return(blocks.Flush(r));
                        }

                        n--;
                        b |= (z.InputBuffer[p++] & 0xff) << k;
                        k += 8;
                    }

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

                    b >>= j;
                    k  -= j;

                    this.mode = Copy;
                    goto case Copy;

                case Copy:                 // o: copying bytes in window, waiting for space
                    int f = q - this.dist; // pointer to copy strings from
                    while (f < 0)
                    {
                        // modulo window size-"while" instead
                        f += blocks.End;     // of "if" handles invalid distances
                    }

                    while (this.len != 0)
                    {
                        if (m == 0)
                        {
                            if (q == blocks.End && blocks.ReadAt != 0)
                            {
                                q = 0;
                                m = q < blocks.ReadAt ? blocks.ReadAt - q - 1 : blocks.End - q;
                            }

                            if (m == 0)
                            {
                                blocks.WriteAt = q;
                                r = blocks.Flush(r);
                                q = blocks.WriteAt;
                                m = q < blocks.ReadAt ? blocks.ReadAt - q - 1 : blocks.End - q;

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

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

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

                        if (f == blocks.End)
                        {
                            f = 0;
                        }

                        this.len--;
                    }

                    this.mode = Start;
                    break;

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

                        if (m == 0)
                        {
                            blocks.WriteAt = q;
                            r = blocks.Flush(r);
                            q = blocks.WriteAt;
                            m = q < blocks.ReadAt ? blocks.ReadAt - q - 1 : blocks.End - q;

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

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

                    r = ZlibConstants.Zok;

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

                    this.mode = Start;
                    break;

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

                    blocks.WriteAt = q;
                    r = blocks.Flush(r);
                    q = blocks.WriteAt;
                    m = q < blocks.ReadAt ? blocks.ReadAt - q - 1 : blocks.End - q;

                    if (blocks.ReadAt != blocks.WriteAt)
                    {
                        blocks.Bitb        = b;
                        blocks.Bitk        = k;
                        z.AvailableBytesIn = n;
                        z.TotalBytesIn    += p - z.NextIn;
                        z.NextIn           = p;
                        blocks.WriteAt     = q;
                        return(blocks.Flush(r));
                    }

                    this.mode = End;
                    goto case End;

                case End:
                    r                  = ZlibConstants.ZStreamEnd;
                    blocks.Bitb        = b;
                    blocks.Bitk        = k;
                    z.AvailableBytesIn = n;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.WriteAt     = q;
                    return(blocks.Flush(r));

                case Badcode:     // x: got error

                    r = ZlibConstants.ZDataError;

                    blocks.Bitb        = b;
                    blocks.Bitk        = k;
                    z.AvailableBytesIn = n;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.WriteAt     = q;
                    return(blocks.Flush(r));

                default:
                    r = ZlibConstants.ZStreamError;

                    blocks.Bitb        = b;
                    blocks.Bitk        = k;
                    z.AvailableBytesIn = n;
                    z.TotalBytesIn    += p - z.NextIn;
                    z.NextIn           = p;
                    blocks.WriteAt     = q;
                    return(blocks.Flush(r));
                }
            }
        }