コード例 #1
0
 /// <summary>
 /// Initializes the CRC-32 calculations.
 /// </summary>
 public override void Initialize()
 {
     if (_definition.ReverseDataBytes)
     {
         _remainder = HackersDelight.Reverse(_definition.Initializer);
     }
     else
     {
         _remainder = _definition.Initializer;
     }
 }
コード例 #2
0
        /// <summary>
        /// Generates a lookup table for a CRC-32 algorithm definition. Both <see cref="Definition.TruncatedPolynomial"/> and <see cref="Definition.ReverseDataBytes"/> are used in the calculations.
        /// </summary>
        /// <param name="definition">The CRC-32 algorithm definition.</param>
        /// <returns>The lookup table.</returns>
        public static uint[] GenerateLookupTable(Definition definition)
        {
            _ = definition ?? throw new ArgumentNullException(nameof(definition));

            unchecked
            {
                uint[] ret = new uint[256];

                byte dividend = 0;
                do
                {
                    uint remainder = 0;

                    for (byte mask = 0x80; mask != 0; mask >>= 1)
                    {
                        if ((dividend & mask) != 0)
                        {
                            remainder ^= 0x80000000;
                        }

                        if ((remainder & 0x80000000) != 0)
                        {
                            remainder <<= 1;
                            remainder  ^= definition.TruncatedPolynomial;
                        }
                        else
                        {
                            remainder <<= 1;
                        }
                    }

                    if (definition.ReverseDataBytes)
                    {
                        var index = HackersDelight.Reverse(dividend);
                        ret[index] = HackersDelight.Reverse(remainder);
                    }
                    else
                    {
                        ret[dividend] = remainder;
                    }

                    ++dividend;
                }while (dividend != 0);

                return(ret);
            }
        }