public override byte ReadByte()
        {
            byte b = main.ReadByte();

            digest.Update(b);
            return(b);
        }
        void SetupRandPartA()
        {
            if (i2 <= last)
            {
                chPrev = ch2;
                ch2    = ll8[tPos];
                tPos   = tt[tPos];
                if (rNToGo == 0)
                {
                    rNToGo = BZip2Constants.RandomNumbers[rTPos];
                    rTPos++;
                    if (rTPos == 512)
                    {
                        rTPos = 0;
                    }
                }
                rNToGo--;
                ch2 ^= (int)((rNToGo == 1) ? 1 : 0);
                i2++;

                currentChar  = ch2;
                currentState = RAND_PART_B_STATE;
                mCrc.Update(ch2);
            }
            else
            {
                EndBlock();
                InitBlock();
                SetupBlock();
            }
        }
Exemple #3
0
        protected int[] ComputeHash(IChecksum checksum, byte[] data, int m, int k)
        {
            if (checksum == null)
            {
                throw new ArgumentNullException(nameof(checksum));
            }

            int[] positions = new int[k];
            int   hashes    = 0;
            int   salt      = 0;

            byte[] output = new byte[64];

            const int seed32 = 89478583;

            while (hashes < k)
            {
                checksum.Reset();
                checksum.Update(data);
                checksum.Update(hashes + salt++ + seed32);
                int hash = Rejection((int)checksum.Value, m);
                if (hash != -1)
                {
                    positions[hashes++] = hash;
                }
            }
            return(positions);
        }
Exemple #4
0
        private void exceptionTesting(IChecksum crcUnderTest)
        {
            bool exception = false;

            try {
                crcUnderTest.Update(null);
            } catch (ArgumentNullException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a null buffer should cause an ArgumentNullException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(null, 0, 0);
            } catch (ArgumentNullException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a null buffer should cause an ArgumentNullException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(check, -1, 9);
            } catch (ArgumentOutOfRangeException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a negative offset should cause an ArgumentOutOfRangeException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(check, 9, 0);
            } catch (ArgumentOutOfRangeException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing an offset greater than or equal to buffer.Length should cause an ArgumentOutOfRangeException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(check, 0, -1);
            } catch (ArgumentOutOfRangeException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a negative count should cause an ArgumentOutOfRangeException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(check, 0, 10);
            } catch (ArgumentOutOfRangeException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a count + offset greater than buffer.Length should cause an ArgumentOutOfRangeException");
        }
 public virtual void Update(byte[] b, int off, int len)
 {
     if (len >= buffer.Length)
     {
         Flush();
         @in.Update(b, off, len);
     }
     else
     {
         if (upto + len > buffer.Length)
         {
             Flush();
         }
         System.Buffer.BlockCopy(b, off, buffer, upto, len);
         upto += len;
     }
 }
Exemple #6
0
 public void Update(byte[] b, int off, int len)
 {
     if (len >= Buffer.Length)
     {
         Flush();
         @in.Update((byte[])(Array)b, off, len);
     }
     else
     {
         if (Upto + len > Buffer.Length)
         {
             Flush();
         }
         System.Buffer.BlockCopy(b, off, Buffer, Upto, len);
         Upto += len;
     }
 }
        public override void WriteByte(byte b)
        {
            _buffer ??= new byte[_bufferSize];

            FlushIfFull();

            _buffer[_bufferPosition++] = b;

            _checksum.Update(b);
        }
Exemple #8
0
 private void SetupNoRandPartA()
 {
     if (_i2 <= _last)
     {
         _chPrev = _ch2;
         _ch2    = _ll8[_tPos];
         _tPos   = _tt[_tPos];
         _i2++;
         _currentChar  = _ch2;
         _currentState = 6;
         _mCrc.Update(_ch2);
     }
     else
     {
         EndBlock();
         InitBlock();
         SetupBlock();
     }
 }
Exemple #9
0
        private void WriteRun()
        {
            if (last < allowableBlockSize)
            {
                inUse[currentChar] = true;
                for (int i = 0; i < runLength; i++)
                {
                    mCrc.Update(currentChar);
                }

                switch (runLength)
                {
                case 1:
                    last++;
                    block[last + 1] = (byte)currentChar;
                    break;

                case 2:
                    last++;
                    block[last + 1] = (byte)currentChar;
                    last++;
                    block[last + 1] = (byte)currentChar;
                    break;

                case 3:
                    last++;
                    block[last + 1] = (byte)currentChar;
                    last++;
                    block[last + 1] = (byte)currentChar;
                    last++;
                    block[last + 1] = (byte)currentChar;
                    break;

                default:
                    inUse[runLength - 4] = true;
                    last++;
                    block[last + 1] = (byte)currentChar;
                    last++;
                    block[last + 1] = (byte)currentChar;
                    last++;
                    block[last + 1] = (byte)currentChar;
                    last++;
                    block[last + 1] = (byte)currentChar;
                    last++;
                    block[last + 1] = (byte)(runLength - 4);
                    break;
                }
            }
            else
            {
                EndBlock();
                InitBlock();
                WriteRun();
            }
        }
Exemple #10
0
        private void exceptionTesting(IChecksum crcUnderTest)
        {
            bool exception = false;

            try {
                crcUnderTest.Update(null);
            } catch (ArgumentNullException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a null buffer should cause an ArgumentNullException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(new ArraySegment <byte>(null, 0, 0));
            } catch (ArgumentNullException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a null buffer should cause an ArgumentNullException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(new ArraySegment <byte>(check, -1, 9));
            } catch (ArgumentOutOfRangeException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a negative offset should cause an ArgumentOutOfRangeException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(new ArraySegment <byte>(check, 10, 0));
            } catch (ArgumentException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing an offset greater than buffer.Length should cause an ArgumentException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(new ArraySegment <byte>(check, 0, -1));
            } catch (ArgumentOutOfRangeException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a negative count should cause an ArgumentOutOfRangeException");

            // reset exception
            exception = false;
            try {
                crcUnderTest.Update(new ArraySegment <byte>(check, 0, 10));
            } catch (ArgumentException) {
                exception = true;
            }
            Assert.IsTrue(exception, "Passing a count + offset greater than buffer.Length should cause an ArgumentException");
        }
        // 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 = avail_in;

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

            avail_in -= len;

            if (dstate.wrap != 0)
            {
                adler.Update(next_in, next_in_index, len);
            }
            Array.Copy(next_in, next_in_index, buf, start, len);
            next_in_index += len;
            total_in      += len;
            return(len);
        }
        public static long GenerateHash(string file, IChecksum crc)
        {
            FileStream reader = new FileStream (file, FileMode.Open);
            byte[] buffer = new byte[_bufferSize];
            long total = 0;
            long count = 0;

            do {
                count = reader.Read (buffer, 0, buffer.Length);
                crc.Update (buffer, 0, (int)count);
                total += count;
            } while (count > 0);

            reader.Close ();
            return total;
        }
 static void Check(IChecksum <ulong> crc, byte[] data, ulong value)
 {
     crc.Reset();
     crc.Update(data);
     Assert.AreEqual(value, crc.Value, "Test crc {0} invalid result {1:x2} expected {2:x2}!", crc, crc.Value, value);
 }
Exemple #14
0
 public override void  WriteByte(byte b)
 {
     digest.Update(b);
     main.WriteByte(b);
 }
        /// <summary>Copia una parte de un fichero final de otro y realiza
        /// el checksum de comprobación de los datos transferidos.</summary>
        /// <param name="desde">El fichero origen de datos.</param>
        /// <param name="hacia">El fichero destido de los datos.</param>
        /// <param name="from">El número de bytes que no se leerán del principio de origen</param>
        /// <param count="count">El tamaño del fragmento a copiar.</param>
        /// <param name="crc">El checsum a aplicar a los datos.</param>
        /// <returns>El número de bytes copiados</returns>
        public static long CopiarIntervalo(String desde, String hacia, long from, long count, IChecksum crc)
        {
            long totales = 0;
            long leidos = 0;
            FileStream writer = new FileStream (hacia, FileMode.Append, FileAccess.Write);
            writer.Seek (writer.Length, SeekOrigin.Begin);
            FileStream reader = new FileStream (desde, FileMode.Open, FileAccess.Read);
            reader.Seek (from, SeekOrigin.Begin);

            byte[] buffer = new byte[_bufferSize];

            long limite = 0;
            do{
                limite = (buffer.Length > (count - totales)) ? (count - totales) : buffer.Length;
                leidos = reader.Read (buffer, 0, (int)limite);
                crc.Update (buffer, 0, (int) leidos);
                totales += leidos;
                writer.Write (buffer, 0, (int)leidos);
            }while ((leidos != 0) && ((totales + from) < reader.Length));

            writer.Close();
            reader.Close();

            return totales;
        }
 public static void CalcularCRC(string file, int offset, int count ,IChecksum crc)
 {
     FileStream reader = new FileStream (file, FileMode.Open);
     reader.Seek (offset, SeekOrigin.Begin);
     for (int i=0; i < count; i++){
         crc.Update (reader.ReadByte());
     }
     reader.Close();
 }
Exemple #17
0
 void Check(IChecksum <uint> crc, byte[] data, uint value)
 {
     crc.Reset();
     crc.Update(data);
     Assert.AreEqual(value, crc.Value);
 }
Exemple #18
0
 /// <summary>
 /// 将字节数组添加到数据校验和
 /// </summary>
 /// <param name="buffer">字节数组</param>
 /// <param name="offset">起始偏移量</param>
 /// <param name="count">多少长度会被添加到数据校验</param>
 /// <param name="checksum">使用效验类类型</param>
 /// <returns>效验值</returns>
 private long Checksum(byte[] buffer, int offset, int count, IChecksum checksum)
 {
     HashingGuard.BufferOffsetCount(buffer, offset, count);
     checksum.Update(buffer, offset, count);
     return(checksum.Value);
 }
 public static void Append(String fichero, byte[] data, IChecksum crc)
 {
     if (crc != null){
         crc.Update (data);
     }
     FileStream writer = new FileStream (fichero, FileMode.Append, FileAccess.Write);
     writer.Seek (writer.Length, SeekOrigin.Begin);
     writer.Write(data, 0, data.Length);
     writer.Close();
 }
 public override void WriteByte(byte b)
 {
     _digest.Update(b);
     _indexOutput.WriteByte(b);
 }