Exemplo n.º 1
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            mLength += count;
            for (int i = 0; i < count; i++)
            {
                mBytes[buffer[offset + i]]++;
            }

            mCurrentCRC = CRC.Update(mCurrentCRC, buffer, offset, count);
        }
Exemplo n.º 2
0
        public uint Finish()
        {
            if (!mFinished)
            {
                mFinished = true;
                mCRC      = CRC.Finish(mCRC);
            }

            return(mCRC);
        }
Exemplo n.º 3
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (mFinished)
            {
                throw new InvalidOperationException("CRC calculation has been finished.");
            }

            mProcessed += count;
            mCRC        = CRC.Update(mCRC, buffer, offset, count);
            mTarget.Write(buffer, offset, count);
        }
Exemplo n.º 4
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (count > 0 && !mFinished)
            {
                int read = mSource.Read(buffer, offset, count);
                if (read > 0)
                {
                    mProcessed += read;
                    mCRC        = CRC.Update(mCRC, buffer, offset, read);
                    return(read);
                }

                Finish();
            }

            return(0);
        }
Exemplo n.º 5
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing && !mClosed)
                {
                    mClosed     = true;
                    mCurrentCRC = CRC.Finish(mCurrentCRC);
#if DEBUG
                    if (mCurrentCRC == mExpectedCRC)
                    {
                        System.Diagnostics.Debug.WriteLine("CRC ok: " + mExpectedCRC.ToString("x8"));
                    }
                    else
                    {
                        System.Diagnostics.Debugger.Break();
                        System.Diagnostics.Debug.WriteLine("bad CRC");
                    }

                    double lengthInv = 1.0 / mLength;
                    double entropy   = 0;
                    for (int i = 0; i < 256; i++)
                    {
                        if (mBytes[i] != 0)
                        {
                            double p = lengthInv * mBytes[i];
                            entropy -= p * Math.Log(p, 256);
                        }
                    }
                    System.Diagnostics.Debug.WriteLine("entropy: " + (int)(entropy * 100) + "%");
#endif
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }