Computes an Adler-32 checksum.
The Adler checksum is similar to a CRC checksum, but faster to compute, though less reliable. It is used in producing RFC1950 compressed streams. The Adler checksum is a required part of the "ZLIB" standard. Applications will almost never need to use this class directly.
예제 #1
0
        /*
         * /// <summary>Reset a codec for another deflation session.</summary>
         * /// <remarks>Call this to reset the deflation state.  For example if a thread is deflatingnon-consecutive blocks, you can call Reset() after the Deflate(Sync) of the first block and before the nextDeflate(None) of the second block.</remarks>
         * public void ResetDeflate()
         * {
         *  if (this.Dstate == null)
         *  {
         *      throw new ZlibException("No Deflate State!");
         *  }
         *
         *  this.Dstate.Reset();
         * }
         */

        /*
         * /// <summary>Set the CompressionStrategy and CompressionLevel for a deflation session.</summary>
         * /// <param name="level">the level of compression to use.</param>
         * /// <param name="strategy">the strategy to use for compression.</param>
         * /// <returns>Z_OK if all goes well.</returns>
         * public int SetDeflateParams(CompressionLevel level, CompressionStrategy strategy)
         * {
         *  if (this.Dstate == null)
         *  {
         *      throw new ZlibException("No Deflate State!");
         *  }
         *
         *  return this.Dstate.SetParams(level, strategy);
         * }
         */

        /*
         * /// <summary>Set the dictionary to be used for either Inflation or Deflation.</summary>
         * /// <param name="dictionary">The dictionary bytes to use.</param>
         * /// <returns>Z_OK if all goes well.</returns>
         * public int SetDictionary(byte[] dictionary)
         * {
         *  if (this.Istate != null)
         *  {
         *      return this.Istate.SetDictionary(dictionary);
         *  }
         *
         *  if (this.Dstate != null)
         *  {
         *      return this.Dstate.SetDictionary(dictionary);
         *  }
         *
         *  throw new ZlibException("No Inflate or Deflate state!");
         * }
         */

        // 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()).

        /// <param name="buf"></param><param name="start"></param> <param name="size"></param><returns></returns>
        internal int ReadBuf(byte[] buf, int start, int size)
        {
            int len = this.AvailableBytesIn;

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

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

            this.AvailableBytesIn -= len;

            if (this.Dstate.WantRfc1950HeaderBytes)
            {
                this.Adler32 = Adler.Adler32(this.Adler32, this.InputBuffer, this.NextIn, len);
            }

            Array.Copy(this.InputBuffer, this.NextIn, buf, start, len);
            this.NextIn       += len;
            this.TotalBytesIn += len;
            return(len);
        }
예제 #2
0
        /// <returns>
        /// </returns>
        internal uint Reset()
        {
            uint oldCheck = this.check;

            this.mode   = InflateBlockMode.Type;
            this.Bitk   = 0;
            this.Bitb   = 0;
            this.ReadAt = this.WriteAt = 0;

            if (this.checkfn != null)
            {
                this.Codec.Adler32 = this.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 read_buf(byte[] buf, int start, int size)
        {
            int 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
        /// <param name="r">
        /// </param><returns></returns>
        internal int Flush(int r)
        {
            for (int pass = 0; pass < 2; pass++)
            {
                int bytes;
                if (pass == 0)
                {
                    // compute number of bytes to copy as far as end of window
                    bytes = (this.ReadAt <= this.WriteAt ? this.WriteAt : this.End) - this.ReadAt;
                }
                else
                {
                    // compute bytes to copy
                    bytes = this.WriteAt - this.ReadAt;
                }

                // workitem 8870
                if (bytes == 0)
                {
                    if (r == ZlibConstants.ZBufError)
                    {
                        r = ZlibConstants.Zok;
                    }

                    return(r);
                }

                if (bytes > this.Codec.AvailableBytesOut)
                {
                    bytes = this.Codec.AvailableBytesOut;
                }

                if (bytes != 0 && r == ZlibConstants.ZBufError)
                {
                    r = ZlibConstants.Zok;
                }

                // update counters
                this.Codec.AvailableBytesOut -= bytes;
                this.Codec.TotalBytesOut     += bytes;

                // update check information
                if (this.checkfn != null)
                {
                    this.Codec.Adler32 = this.check = Adler.Adler32(this.check, this.Window, this.ReadAt, bytes);
                }

                // copy as far as end of window
                Array.Copy(this.Window, this.ReadAt, this.Codec.OutputBuffer, this.Codec.NextOut, bytes);
                this.Codec.NextOut += bytes;
                this.ReadAt        += bytes;

                // see if more to copy at beginning of window
                if (this.ReadAt == this.End && pass == 0)
                {
                    // wrap pointers
                    this.ReadAt = 0;
                    if (this.WriteAt == this.End)
                    {
                        this.WriteAt = 0;
                    }
                }
                else
                {
                    pass++;
                }
            }

            // done
            return(r);
        }