CRC.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "CommonCorePch.h"
  6. #include "Core/CRC.h"
  7. unsigned int CalculateCRC32(unsigned int bufferCRC, size_t data)
  8. {
  9. /* update running CRC calculation with contents of a buffer */
  10. bufferCRC = bufferCRC ^ 0xffffffffL;
  11. bufferCRC = crc_32_tab[(bufferCRC ^ data) & 0xFF] ^ (bufferCRC >> 8);
  12. return (bufferCRC ^ 0xffffffffL);
  13. }
  14. unsigned int CalculateCRC32(const char* in)
  15. {
  16. unsigned int crc = (unsigned int)-1;
  17. while (*in != '\0')
  18. {
  19. crc = (crc >> 8) ^ crc_32_tab[(crc ^ *in) & 0xFF];
  20. in++;
  21. }
  22. return crc ^ (unsigned int)-1;
  23. }
  24. uint CalculateCRC(uint bufferCRC, size_t data)
  25. {
  26. #if defined(_WIN32) || defined(__SSE4_2__)
  27. #if defined(_M_IX86)
  28. if (AutoSystemInfo::Data.SSE4_2Available())
  29. {
  30. return _mm_crc32_u32(bufferCRC, data);
  31. }
  32. #elif defined(_M_X64)
  33. if (AutoSystemInfo::Data.SSE4_2Available())
  34. {
  35. //CRC32 always returns a 32-bit result
  36. return (uint)_mm_crc32_u64(bufferCRC, data);
  37. }
  38. #endif
  39. #endif
  40. return CalculateCRC32(bufferCRC, data);
  41. }
  42. uint CalculateCRC(uint bufferCRC, size_t count, _In_reads_bytes_(count) void * buffer)
  43. {
  44. for (uint index = 0; index < count; index++)
  45. {
  46. bufferCRC = CalculateCRC(bufferCRC, *((BYTE*)buffer + index));
  47. }
  48. return bufferCRC;
  49. }