crctool.cs

来自「ecg tool kit for medical image retrieval」· CS 代码 · 共 632 行 · 第 1/2 页

CS
632
字号
			crc^= crcxor;
			crc&= crcmask;

			return(crc);
		}

        public ulong crcbitbybit(byte[] p) 
        {
            // bit by bit algorithm with augmented zero bytes.
            // does not use lookup table, suited for polynom orders between 1...32.
            int i;
            ulong  j, c, bit;
            ulong crc = crcinit_nondirect;

            for (i=0; i<p.Length; i++) 
            {
                c = (ulong)p[i];
                if ( refin != 0 ) 
                {
                    c = reflect(c, 8);
                }

                for (j=0x80; j != 0; j>>=1) 
                {
                    bit = crc & crchighbit;
                    crc<<= 1;
                    if ( (c & j) != 0) 
                    {
                        crc|= 1;
                    }
                    if ( bit  != 0 ) 
                    {
                        crc^= polynom;
                    }
                }
            }	

            for ( i=0; (int)i < order; i++) 
            {

                bit = crc & crchighbit;
                crc<<= 1;
                if ( bit != 0 ) crc^= polynom;
            }

            if ( refout != 0 ) 
            {
                crc=reflect(crc, order);
            }
            crc^= crcxor;
            crc&= crcmask;

            return(crc);
        }

		public ulong crcbitbybit(byte[] p, int offset, int length) 
		{
			// Works like crcbitbybit(byte[] p), except you now calculate a CRC for a specific part of the byte array.

			// bit by bit algorithm with augmented zero bytes.
			// does not use lookup table, suited for polynom orders between 1...32.
			int i = 0;
			ulong  j, c, bit;
			ulong crc = crcinit_nondirect;

			if (offset > 0)
			{
				i = offset;
				length += offset;
			}
			if (length > p.Length)
				length = p.Length;

			for (; i<length; i++) 
			{
				c = (ulong)p[i];
				if ( refin != 0 ) 
				{
					c = reflect(c, 8);
				}

				for (j=0x80; j != 0; j>>=1) 
				{
					bit = crc & crchighbit;
					crc<<= 1;
					if ( (c & j) != 0) 
					{
						crc|= 1;
					}
					if ( bit  != 0 ) 
					{
						crc^= polynom;
					}
				}
			}	

			for ( i=0; (int)i < order; i++) 
			{

				bit = crc & crchighbit;
				crc<<= 1;
				if ( bit != 0 ) crc^= polynom;
			}

			if ( refout != 0 ) 
			{
				crc=reflect(crc, order);
			}
			crc^= crcxor;
			crc&= crcmask;

			return(crc);
		}

        public ulong crcbitbybitfast(byte[] p) 
        {
            // fast bit by bit algorithm without augmented zero bytes.
            // does not use lookup table, suited for polynom orders between 1...32.
            int i;
            ulong j, c, bit;
            ulong crc = crcinit_direct;

            for (i = 0; i < p.Length; i++) 
            {
                c = (ulong)p[i];
                if ( refin != 0) 
                {
                    c = reflect(c, 8);
                }

                for ( j = 0x80; j > 0; j >>= 1 ) 
                {
                    bit = crc & crchighbit;
                    crc <<= 1;
                    if ( (c & j) > 0 ) bit^= crchighbit;
                    if ( bit > 0 ) crc^= polynom;
                }
            }	

            if ( refout > 0) 
            {
                crc=reflect( crc, order );
            }
            crc^= crcxor;
            crc&= crcmask;

            return(crc);
        }


		public ulong crcbitbybitfast(byte[] p, int offset, int length) 
		{
			// Works like crcbitbybitfast(byte[] p), except you now calculate a CRC for a specific part of the byte array.

			// fast bit by bit algorithm without augmented zero bytes.
			// does not use lookup table, suited for polynom orders between 1...32.
			int i = 0;
			ulong j, c, bit;
			ulong crc = crcinit_direct;

			if (offset > 0)
			{
				i = offset;
				length += offset;
			}
			if (length > p.Length)
				length = p.Length;

			for (; i < length; i++) 
			{
				c = (ulong)p[i];
				if ( refin != 0) 
				{
					c = reflect(c, 8);
				}

				for ( j = 0x80; j > 0; j >>= 1 ) 
				{
					bit = crc & crchighbit;
					crc <<= 1;
					if ( (c & j) > 0 ) bit^= crchighbit;
					if ( bit > 0 ) crc^= polynom;
				}
			}	

			if ( refout > 0) 
			{
				crc=reflect( crc, order );
			}
			crc^= crcxor;
			crc&= crcmask;

			return(crc);
		}
    
        /// <summary>
        /// CalcCRCITT is an algorithm found on the web for calculating the CRCITT checksum
        /// It is included to demonstrate that although it looks different it is the same 
        /// routine as the crcbitbybit* functions. But it is optimized and preconfigured for CRCITT.
        /// </summary>
        public ushort CalcCRCITT(byte[] p)
        {
            uint uiCRCITTSum = 0xFFFF;
            uint uiByteValue;

            for (int iBufferIndex = 0; iBufferIndex < p.Length; iBufferIndex++)
            {
                uiByteValue = ( (uint) p[iBufferIndex] << 8);
                for ( int iBitIndex = 0; iBitIndex < 8; iBitIndex++ )
                {
                    if ( ( (uiCRCITTSum^uiByteValue) & 0x8000) != 0 )
                    {
                        uiCRCITTSum = (uiCRCITTSum <<1 ) ^ 0x1021;
                    }
                    else
                    {
                        uiCRCITTSum <<= 1;
                    }
                    uiByteValue <<=1;
                }
            }
            return (ushort)uiCRCITTSum;
        }
		/// <summary>
		/// CalcCRCITT is an algorithm found on the web for calculating the CRCITT checksum
		/// It is included to demonstrate that although it looks different it is the same 
		/// routine as the crcbitbybit* functions. But it is optimized and preconfigured for CRCITT.
		/// </summary>
		public ushort CalcCRCITT(byte[] p, int offset, int length)
		{
			// Works like CalcCRCITT(byte[] p), except you now calculate a CRC for a specific part of the byte array.
			int iBufferIndex = 0;
			uint uiCRCITTSum = 0xFFFF;
			uint uiByteValue;

			if (offset > 0)
			{
				iBufferIndex = offset;
				length += offset;
			}
			if (length > p.Length)
				length = p.Length;

			for (; iBufferIndex < length; iBufferIndex++)
			{
				uiByteValue = ( (uint) p[iBufferIndex] << 8);
				for ( int iBitIndex = 0; iBitIndex < 8; iBitIndex++ )
				{
					if ( ( (uiCRCITTSum^uiByteValue) & 0x8000) != 0 )
					{
						uiCRCITTSum = (uiCRCITTSum <<1 ) ^ 0x1021;
					}
					else
					{
						uiCRCITTSum <<= 1;
					}
					uiByteValue <<=1;
				}
			}
			return (ushort)uiCRCITTSum;
		}

        #region subroutines
        private ulong reflect (ulong crc, int bitnum) 
        {
            // reflects the lower 'bitnum' bits of 'crc'

            ulong i, j=1, crcout = 0;

            for ( i = (ulong)1 <<(bitnum-1); i != 0; i>>=1) 
            {
                if ( ( crc & i ) != 0 ) 
                {
                    crcout |= j;
                }
                j<<= 1;
            }
            return (crcout);
        }

        private void generate_crc_table() 
        {

            // make CRC lookup table used by table algorithms

            int i, j;
            ulong bit, crc;

            for (i=0; i<256; i++) 
            {
                crc=(ulong)i;
                if ( refin !=0 ) 
                {
                    crc=reflect(crc, 8);
                }
                crc<<= order-8;

                for (j=0; j<8; j++) 
                {
                    bit = crc & crchighbit;
                    crc<<= 1;
                    if ( bit !=0 ) crc^= polynom;
                }			

                if (refin != 0) 
                {
                    crc = reflect(crc, order);
                }
                crc&= crcmask;
                crctab[i]= crc;
            }
        }
        #endregion 
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?