示例#1
0
        internal int SetDictionary(byte[] dictionary)
        {
            var index  = 0;
            var length = dictionary.Length;

            if (mode != InflateManagerMode.DICT0)
            {
                throw new ZlibException("Stream error.");
            }

            if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
            {
                return(ZlibConstants.Z_DATA_ERROR);
            }

            _codec._Adler32 = Adler.Adler32(0, null, 0, 0);

            if (length >= 1 << wbits)
            {
                length = (1 << wbits) - 1;
                index  = dictionary.Length - length;
            }

            blocks.SetDictionary(dictionary, index, length);
            mode = InflateManagerMode.BLOCKS;
            return(ZlibConstants.Z_OK);
        }
示例#2
0
        internal uint Reset()
        {
            var oldCheck = check;

            mode   = InflateBlockMode.TYPE;
            bitk   = 0;
            bitb   = 0;
            readAt = writeAt = 0;

            if (checkfn != null)
            {
                _codec._Adler32 = check = Adler.Adler32(0, null, 0, 0);
            }
            return(oldCheck);
        }
示例#3
0
        // Read a new buffer from the current input stream, update the adler32
        // and total number of bytes read.  All deflate() input goes through
        // this function so some applications may wish to modify it to avoid
        // allocating a large strm->next_in buffer and copying from it.
        // (See also flush_pending()).
        internal int ReadNewBuffer(byte[] buf, int start, int size)
        {
            var len = AvailableBytesIn;

            if (len > size)
            {
                len = size;
            }
            if (len == 0)
            {
                return(0);
            }

            AvailableBytesIn -= len;

            if (dstate.WantRfc1950HeaderBytes)
            {
                _Adler32 = Adler.Adler32(_Adler32, InputBuffer, NextIn, len);
            }
            Array.Copy(InputBuffer, NextIn, buf, start, len);
            NextIn       += len;
            TotalBytesIn += len;
            return(len);
        }
示例#4
0
        // copy as much as possible from the sliding window to the output area
        internal int Flush(int r)
        {
            int nBytes;

            for (var pass = 0; pass < 2; pass++)
            {
                if (pass == 0)
                {
                    // compute number of bytes to copy as far as end of window
                    nBytes = (readAt <= writeAt ? writeAt : end) - readAt;
                }
                else
                {
                    // compute bytes to copy
                    nBytes = writeAt - readAt;
                }

                // workitem 8870
                if (nBytes == 0)
                {
                    if (r == ZlibConstants.Z_BUF_ERROR)
                    {
                        r = ZlibConstants.Z_OK;
                    }
                    return(r);
                }

                if (nBytes > _codec.AvailableBytesOut)
                {
                    nBytes = _codec.AvailableBytesOut;
                }

                if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR)
                {
                    r = ZlibConstants.Z_OK;
                }

                // update counters
                _codec.AvailableBytesOut -= nBytes;
                _codec.TotalBytesOut     += nBytes;

                // update check information
                if (checkfn != null)
                {
                    _codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
                }

                // copy as far as end of window
                Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
                _codec.NextOut += nBytes;
                readAt         += nBytes;

                // see if more to copy at beginning of window
                if (readAt == end && pass == 0)
                {
                    // wrap pointers
                    readAt = 0;
                    if (writeAt == end)
                    {
                        writeAt = 0;
                    }
                }
                else
                {
                    pass++;
                }
            }

            // done
            return(r);
        }