mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge pull request #406 from chatn0ir07/master
Implementet CRC-32 Checksum Algorithm
This commit is contained in:
commit
eee804a896
@ -4,4 +4,5 @@ Overview files **hash.h** and **hash.c**
|
||||
* sdbm
|
||||
* djb2
|
||||
* xor8 (8 bit)
|
||||
* adler_32 (32 bit)
|
||||
* adler_32 (32 bit)
|
||||
* crc32 (32 bit)
|
26
hash/hash.c
26
hash/hash.c
@ -60,4 +60,28 @@ int adler_32(char s[])
|
||||
i++;
|
||||
}
|
||||
return (b << 16) | a;
|
||||
}
|
||||
}
|
||||
|
||||
/* crc32 Hash-Algorithm*/
|
||||
|
||||
int crc32(char string[])
|
||||
{
|
||||
int i = 0;
|
||||
unsigned int cur_crc, masking;
|
||||
|
||||
cur_crc = 0xFFFFFFFF;
|
||||
|
||||
while(string[i] != '\0')
|
||||
{
|
||||
unsigned int byte = string[i];
|
||||
cur_crc = cur_crc ^ byte;
|
||||
for(int j = 8; j > 0; --j)
|
||||
{
|
||||
masking = -(cur_crc & 1);
|
||||
cur_crc = (cur_crc >> 1) ^ (0xEDB88320 & masking);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return -cur_crc;
|
||||
}
|
||||
|
@ -40,4 +40,11 @@ char xor8(char[]);
|
||||
*/
|
||||
int adler_32(char[]);
|
||||
|
||||
/*
|
||||
crc32: implements the crc-32 hash-algorithm
|
||||
returns the checksum byte for the passed byte
|
||||
*/
|
||||
int crc32(char[]);
|
||||
|
||||
|
||||
#endif
|
@ -15,6 +15,7 @@ int main(void)
|
||||
printf("djb2: %s --> %lld\n", s, djb2(s));
|
||||
printf("xor8: %s --> %i\n", s, xor8(s)); /* 8 bit */
|
||||
printf("adler_32: %s --> %i\n", s, adler_32(s)); /* 32 bit */
|
||||
printf("crc32: %s --> %i\n", s, crc32(s));
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user