/// <summary> /// Compute a CRC value for a block of data. /// </summary> /// <param name="data">The data from which to compute an updated running CRC.</param> /// <param name="polynomial">The polynomial to use.</param> /// <param name="numBytesToProcess">Number of bytes in the buffer to process (always start at first element).</param> /// <param name="runningValue">Running value of the CRC.</param> /// <returns>Running CRC, updated with the contents of the given data block.</returns> private static uint OfBlock(byte[] data, Crc32Polynomial polynomial, int numBytesToProcess, uint runningValue) { uint[] lookupTable = null; switch (polynomial) { case Crc32Polynomial.Zip: lookupTable = Crc32Table; break; case Crc32Polynomial.Castagnoli: lookupTable = Crc32CTable; break; default: throw new InvalidOperationException(); } var crc = runningValue; for (var i = 0; i < numBytesToProcess; ++i) { crc = Update(crc, data[i], lookupTable); } return(crc); }
/// <summary> /// Compute a CRC value for a block of data using a specific polynomial. /// </summary> /// <param name="data">The data from which to compute the CRC.</param> /// <param name="polynomial">The polynomial to use.</param> /// <returns>The CRC of the data block.</returns> public static uint OfBlock(byte[] data, Crc32Polynomial polynomial) { var initialValue = InitialValue; switch (polynomial) { case Crc32Polynomial.Castagnoli: initialValue = InitialValueZero; break; default: break; } uint crc = OfBlock(data, polynomial, data.Length, initialValue) ^ initialValue; return(crc); }