HasherCrc32Traits.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. template<uint32_t __Polynomial>
  5. struct HasherCrc32Traits {
  6. static constexpr size_t BlockSize = 0;
  7. static constexpr size_t DigestSize = 32 / 8;
  8. using DigestType = uint32_t;
  9. using LookupTableType = uint32_t[256];
  10. struct ContextType {
  11. uint32_t Value;
  12. ContextType() noexcept :
  13. Value(0) {}
  14. ContextType(uint32_t InitialValue) noexcept :
  15. Value(InitialValue) {}
  16. ContextType(const ContextType& Other) noexcept = default;
  17. ContextType(ContextType&& Other) noexcept : Value(Other.Value) {
  18. Other.Value = 0;
  19. }
  20. ContextType& operator=(const ContextType& Other) noexcept = default;
  21. ContextType& operator=(ContextType&& Other) noexcept {
  22. Value = Other.Value;
  23. Other.Value = 0;
  24. return *this;
  25. }
  26. };
  27. static const LookupTableType& InitializeLookupTable() noexcept {
  28. static LookupTableType LookupTable = {};
  29. if (LookupTable[1] == 0) {
  30. for (unsigned i = 0; i < 256; ++i) {
  31. uint32_t result = i;
  32. for (unsigned j = 0; j < 8; ++j) {
  33. if (result % 2) {
  34. result /= 2;
  35. result ^= __Polynomial;
  36. } else {
  37. result /= 2;
  38. }
  39. }
  40. LookupTable[i] = result;
  41. }
  42. }
  43. return LookupTable;
  44. }
  45. static inline const LookupTableType& LookupTable = InitializeLookupTable();
  46. static inline ContextType ContextCreate() noexcept {
  47. ContextType Ctx;
  48. return Ctx;
  49. }
  50. static inline ContextType ContextCreate(const void* lpBuffer, size_t cbBuffer) noexcept {
  51. ContextType Ctx;
  52. ContextUpdate(Ctx, lpBuffer, cbBuffer);
  53. return Ctx;
  54. }
  55. static inline ContextType ContextCreate(uint32_t InitialValue) noexcept {
  56. ContextType Ctx(InitialValue);
  57. return Ctx;
  58. }
  59. static inline ContextType ContextCreate(uint32_t InitialValue, const void* lpBuffer, size_t cbBuffer) noexcept {
  60. ContextType Ctx;
  61. ContextUpdate(Ctx, lpBuffer, cbBuffer);
  62. return Ctx;
  63. }
  64. static inline ContextType ContextCopy(const ContextType& Ctx) noexcept {
  65. return Ctx;
  66. }
  67. static inline void ContextUpdate(ContextType& Ctx, const void* lpBuffer, size_t cbBuffer) noexcept {
  68. auto pbBuffer = reinterpret_cast<const uint8_t*>(lpBuffer);
  69. Ctx.Value = ~Ctx.Value;
  70. for (size_t i = 0; i < cbBuffer; ++i) {
  71. Ctx.Value = (Ctx.Value >> 8) ^ LookupTable[static_cast<uint8_t>(Ctx.Value) ^ pbBuffer[i]];
  72. }
  73. Ctx.Value = ~Ctx.Value;
  74. }
  75. static inline void ContextEvaluate(const ContextType& Ctx, DigestType& Digest) noexcept {
  76. Digest = Ctx.Value;
  77. }
  78. static inline void ContextDestroy(ContextType& Ctx) noexcept {
  79. Ctx.Value = 0;
  80. }
  81. };
  82. #ifdef _WIN32
  83. #include <windows.h>
  84. #ifdef __cplusplus
  85. extern "C" {
  86. #endif
  87. #pragma comment(lib, "ntdll")
  88. // available on WindowsXP and above
  89. NTSYSAPI
  90. DWORD
  91. NTAPI
  92. RtlComputeCrc32(
  93. _In_ DWORD InitialCrc,
  94. _In_reads_bytes_(Size) const void* Buffer,
  95. _In_ size_t Size
  96. );
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. template<>
  101. struct HasherCrc32Traits<0xEDB88320> {
  102. static constexpr size_t BlockSize = 0;
  103. static constexpr size_t DigestSize = 32 / 8;
  104. using DigestType = uint32_t;
  105. struct ContextType {
  106. uint32_t Value;
  107. ContextType() noexcept :
  108. Value(0) {}
  109. ContextType(uint32_t InitialValue) noexcept :
  110. Value(InitialValue) {}
  111. ContextType(const ContextType& Other) noexcept = default;
  112. ContextType(ContextType&& Other) noexcept : Value(Other.Value) {
  113. Other.Value = 0;
  114. }
  115. ContextType& operator=(const ContextType& Other) noexcept = default;
  116. ContextType& operator=(ContextType&& Other) noexcept {
  117. Value = Other.Value;
  118. Other.Value = 0;
  119. return *this;
  120. }
  121. };
  122. static inline ContextType ContextCreate() noexcept {
  123. ContextType Ctx;
  124. return Ctx;
  125. }
  126. static inline ContextType ContextCreate(const void* lpBuffer, size_t cbBuffer) noexcept {
  127. ContextType Ctx;
  128. ContextUpdate(Ctx, lpBuffer, cbBuffer);
  129. return Ctx;
  130. }
  131. static inline ContextType ContextCreate(uint32_t InitialValue) noexcept {
  132. ContextType Ctx(InitialValue);
  133. return Ctx;
  134. }
  135. static inline ContextType ContextCreate(uint32_t InitialValue, const void* lpBuffer, size_t cbBuffer) noexcept {
  136. ContextType Ctx;
  137. ContextUpdate(Ctx, lpBuffer, cbBuffer);
  138. return Ctx;
  139. }
  140. static inline ContextType ContextCopy(const ContextType& Ctx) noexcept {
  141. return Ctx;
  142. }
  143. static inline void ContextUpdate(ContextType& Ctx, const void* lpBuffer, size_t cbBuffer) noexcept {
  144. Ctx.Value = RtlComputeCrc32(Ctx.Value, lpBuffer, cbBuffer);
  145. }
  146. static inline void ContextEvaluate(const ContextType& Ctx, DigestType& Digest) noexcept {
  147. Digest = Ctx.Value;
  148. }
  149. static inline void ContextDestroy(ContextType& Ctx) noexcept {
  150. Ctx.Value = 0;
  151. }
  152. };
  153. #endif // #ifdef _MSC_VER