Exemplo n.º 1
0
        private ulong[] CreateTable()
        {
            ulong bit, crc;

            ulong[] crctab = new ulong[256];

            for (ulong i = 0; i < (ulong)crctab.Length; i++)
            {
                crc = i;

                if (this._reflectIn)
                {
                    crc = CrcHelper.Reflect(crc, 8);
                }

                crc <<= this._order - 8;

                for (int j = 0; j < 8; j++)
                {
                    bit   = crc & this._crcHighBit;
                    crc <<= 1;

                    if (bit > 0)
                    {
                        crc ^= this._polynom;
                    }
                }

                if (this._reflectIn)
                {
                    crc = CrcHelper.Reflect(crc, this._order);
                }

                crc      &= this._crcMask;
                crctab[i] = crc;
            }

            return(crctab);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Uses a fast lookup table algorithm without augmented zero bytes to produce a CRC for the provided input.
        /// </summary>
        /// <remarks>
        /// This algorithm is only usable for polynomial orders of 8, 16, 24 or 32.
        /// </remarks>
        /// <param name="input">The target of the CRC.</param>
        /// <returns>A <see cref="UInt64"/> containing the CRC value that was calculated for the provided input.</returns>
        public ulong CalculateCrc(byte[] input)
        {
            ulong crc = this._crcInitialDirect;

            if (_reflectIn)
            {
                crc = CrcHelper.Reflect(crc, this._order);
            }

            int len   = input.Length;
            int index = 0;

            if (!this._reflectIn)
            {
                while (len-- > 0)
                {
                    crc = (crc << 8) ^ this._table[((crc >> (this._order - 8)) & 0xff) ^ input[index++]];
                }
            }
            else
            {
                while (len-- > 0)
                {
                    crc = (crc >> 8) ^ this._table[(crc & 0xff) ^ input[index++]];
                }
            }

            if (this._reflectOut ^ this._reflectIn)
            {
                crc = CrcHelper.Reflect(crc, this._order);
            }

            crc ^= this._crcXor;
            crc &= this._crcMask;

            return(crc);
        }