mirror of
https://github.com/TheAlgorithms/C
synced 2024-12-01 01:26:58 +03:00
60 lines
1.4 KiB
C
60 lines
1.4 KiB
C
/**
|
|
* @addtogroup hash Hash algorithms
|
|
* @{
|
|
* @file hash_crc32.c
|
|
* @author [Christian Bender](https://github.com/christianbender)
|
|
* @brief 32-bit [CRC
|
|
* hash](https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-32_algorithm)
|
|
* algorithm
|
|
*/
|
|
#include <assert.h>
|
|
#include <inttypes.h>
|
|
#include <stdio.h>
|
|
|
|
/**
|
|
* @brief 32-bit CRC algorithm implementation
|
|
*
|
|
* @param s NULL terminated ASCII string to hash
|
|
* @return 32-bit hash result
|
|
*/
|
|
uint32_t crc32(const char* s)
|
|
{
|
|
uint32_t crc = 0xffffffff;
|
|
size_t i = 0;
|
|
while (s[i] != '\0')
|
|
{
|
|
uint8_t byte = s[i];
|
|
crc = crc ^ byte;
|
|
for (uint8_t j = 8; j > 0; --j)
|
|
crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1)));
|
|
|
|
i++;
|
|
}
|
|
return crc ^ 0xffffffff;
|
|
}
|
|
|
|
/**
|
|
* @brief Test function for ::crc32
|
|
*/
|
|
void test_crc32()
|
|
{
|
|
assert(crc32("Hello World") == (const uint32_t)1243066710);
|
|
assert(crc32("Hello World!") == (const uint32_t)472456355);
|
|
assert(crc32("Hello world") == (const uint32_t)2346098258);
|
|
assert(crc32("Hello world!") == (const uint32_t)461707669);
|
|
// printf("%" PRIu32 "\n", crc32("Hello World"));
|
|
// printf("%" PRIu32 "\n", crc32("Hello World!"));
|
|
// printf("%" PRIu32 "\n", crc32("Hello world"));
|
|
// printf("%" PRIX32 "\n", crc32("Hello world!"));
|
|
printf("Tests passed\n");
|
|
}
|
|
|
|
/** @} */
|
|
|
|
/** Main function */
|
|
int main()
|
|
{
|
|
test_crc32();
|
|
return 0;
|
|
}
|