/// <summary> /// Gets the corresponding CRC table for the given polynomial. /// </summary> /// <param name="polynomial">The polynomial to get a CRC table for.</param> /// <param name="cacheCrcTable">Whether or not to cache the generated CRC table in <see cref="CrcTables"/>. /// If this CRC is used as the starting point for others, the CRC table will be carried over from it. /// The cache is only used if this constructor is called again with the same polynomial.</param> /// <returns></returns> private static CrcTable GetCrcTable(uint polynomial, bool cacheCrcTable) { if (!CrcTables.TryGetValue(polynomial, out var crcTable)) { crcTable = new CrcTable(polynomial); if (cacheCrcTable) { CrcTables[polynomial] = crcTable; } } return(crcTable); }
/// <summary> /// Updates a CRC value directly. /// </summary> /// <param name="crc">The CRC to update.</param> /// <param name="b">The byte to update the CRC with.</param> /// <param name="crcTable">The CRC table to use when updating the CRC.</param> /// <returns></returns> private static uint UpdateCrc(uint crc, byte b, CrcTable crcTable) { return(crc = (crc >> 8) ^ crcTable[(crc ^ b) & 0xFF]); }
/// <summary> /// Instantiates a new <see cref="Crc32"/>. /// </summary> /// <param name="crc">The value of the CRC.</param> /// <param name="correspondingCrcTable">The CRC table that will be used, based on its polynomial.</param> /// <param name="finalXor">The final XOR value to apply to the CRC before returned.</param> private Crc32(uint crc, CrcTable correspondingCrcTable, uint finalXor) { Crc = crc; CorrespondingCrcTable = correspondingCrcTable; FinalXor = finalXor; }