From ca18351173b1b0f40676f4b4373138f2202cd2e1 Mon Sep 17 00:00:00 2001 From: ChatN0ir Date: Mon, 14 Oct 2019 17:49:03 +0200 Subject: [PATCH] Fixed CRC-32 Error --- hash/hash.c | 27 ++++++++++----------------- hash/hash.h | 5 +++-- hash/test_program.c | 1 + 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/hash/hash.c b/hash/hash.c index 8f202d4bb..b8a4b37f1 100644 --- a/hash/hash.c +++ b/hash/hash.c @@ -63,25 +63,18 @@ int adler_32(char s[]) } /* crc32 Hash-Algorithm*/ +#include -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; +} \ No newline at end of file diff --git a/hash/hash.h b/hash/hash.h index 1f0722ff0..8d2e80919 100644 --- a/hash/hash.h +++ b/hash/hash.h @@ -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 \ No newline at end of file diff --git a/hash/test_program.c b/hash/test_program.c index 54439e8e4..b799135b6 100644 --- a/hash/test_program.c +++ b/hash/test_program.c @@ -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; } \ No newline at end of file