mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
Fixed CRC-32 Error
This commit is contained in:
parent
71738b36a9
commit
ca18351173
27
hash/hash.c
27
hash/hash.c
@ -63,25 +63,18 @@ int adler_32(char s[])
|
||||
}
|
||||
|
||||
/* crc32 Hash-Algorithm*/
|
||||
#include <inttypes.h>
|
||||
|
||||
int crc32(char string[])
|
||||
{
|
||||
uint32_t crc32(char* data){
|
||||
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;
|
||||
uint32_t crc = 0xffffffff;
|
||||
while(data[i] != '\0'){
|
||||
uint8_t byte = data[i];
|
||||
crc = crc ^ byte;
|
||||
for(int j = 8; j > 0; --j)
|
||||
{
|
||||
masking = -(cur_crc & 1);
|
||||
cur_crc = (cur_crc >> 1) ^ (0xEDB88320 & masking);
|
||||
}
|
||||
crc = (crc >> 1) ^ (0xEDB88320 & ( -(crc & 1)));
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return -cur_crc;
|
||||
}
|
||||
return crc ^ 0xffffffff;
|
||||
}
|
@ -41,10 +41,11 @@ char xor8(char[]);
|
||||
int adler_32(char[]);
|
||||
|
||||
/*
|
||||
crc32: implements the crc-32 hash-algorithm
|
||||
returns the checksum byte for the passed byte
|
||||
crc32: implements the crc-32 checksum-algorithm
|
||||
returns the crc-32 checksum
|
||||
*/
|
||||
int crc32(char[]);
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -16,6 +16,7 @@ int main(void)
|
||||
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