From 530c17aaa1e8b1048a12b4d8f5d7bb4350a9fde2 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 7 Jun 2018 20:43:32 +0800 Subject: [PATCH] correct CRC32 --- HISTORY | 3 ++- src/hash.c | 9 ++++----- src/hash.h | 6 +++--- src/tests/Makefile | 2 +- src/tests/test_crc32.c | 46 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 src/tests/test_crc32.c diff --git a/HISTORY b/HISTORY index 39e8cd4..53518a4 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.38 2018-06-01 +Version 1.38 2018-06-07 * connection_pool.c: set err_no to 0 when success * shared_func.h: add functions float2buff / buff2float, double2buff / buff2double * logger.h: add function log_get_level_caption @@ -7,6 +7,7 @@ Version 1.38 2018-06-01 * add files: multi_socket_client.[hc] * ioevent.[hc]: remove care_events in FreeBSD or MacOS * add skiplist_set.[hc] and skiplist bug fixed + * correct CRC32 Version 1.37 2018-02-24 * ini_file_reader.c function annotations LOCAL_IP_GET support index, such as: diff --git a/src/hash.c b/src/hash.c index a1e3c7f..d5458c6 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1370,7 +1370,7 @@ static unsigned int crc_table[256] = { #define CRC32_BODY(init_value) \ unsigned char *pKey; \ unsigned char *pEnd; \ - int crc; \ + int64_t crc; \ \ crc = init_value; \ pEnd = (unsigned char *)key + key_len; \ @@ -1383,14 +1383,13 @@ int CRC32(const void *key, const int key_len) { CRC32_BODY(CRC32_XINIT) - return crc ^ CRC32_XOROT; + return (int)(crc ^ CRC32_XOROT); } -int CRC32_ex(const void *key, const int key_len, \ - const int init_value) +int64_t CRC32_ex(const void *key, const int key_len, \ + const int64_t init_value) { CRC32_BODY(init_value) return crc; } - diff --git a/src/hash.h b/src/hash.h index 6be86cd..5681f64 100644 --- a/src/hash.h +++ b/src/hash.h @@ -17,7 +17,7 @@ extern "C" { #endif -#define CRC32_XINIT 0xFFFFFFFF /* initial value */ +#define CRC32_XINIT 0xFFFFFFFF /* initial value */ #define CRC32_XOROT 0xFFFFFFFF /* final xor value */ typedef int (*HashFunc) (const void *key, const int key_len); @@ -351,8 +351,8 @@ int simple_hash_ex(const void* key, const int key_len, \ const int init_value); int CRC32(const void *key, const int key_len); -int CRC32_ex(const void *key, const int key_len, \ - const int init_value); +int64_t CRC32_ex(const void *key, const int key_len, \ + const int64_t init_value); #define CRC32_FINAL(crc) (crc ^ CRC32_XOROT) diff --git a/src/tests/Makefile b/src/tests/Makefile index e2e76a4..fc71f50 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -6,7 +6,7 @@ LIB_PATH = -lfastcommon -lpthread ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blocked_queue \ test_id_generator test_ini_parser test_char_convert test_char_convert_loader \ - test_logger test_skiplist_set + test_logger test_skiplist_set test_crc32 all: $(ALL_PRGS) .c: diff --git a/src/tests/test_crc32.c b/src/tests/test_crc32.c new file mode 100644 index 0000000..116dfad --- /dev/null +++ b/src/tests/test_crc32.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include +#include "fastcommon/logger.h" +#include "fastcommon/shared_func.h" + +int main(int argc, char *argv[]) +{ + int result; + char *filename; + char *content; + int64_t file_size; + int64_t crc32; + int byte1, byte2; + + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return EINVAL; + } + filename = argv[1]; + + log_init(); + result = getFileContent(filename, &content, &file_size); + if (result != 0) { + return result; + } + + printf("file_size: %"PRId64"\n", file_size); + + crc32 = CRC32(content, (int)file_size); + printf("crc32: %x\n", (int)crc32); + + byte1 = (int)(file_size / 2); + byte2 = (int)(file_size - byte1); + crc32 = CRC32_XINIT; + crc32 = CRC32_ex(content, byte1, crc32); + crc32 = CRC32_ex(content + byte1, byte2, crc32); + crc32 = CRC32_FINAL(crc32); + printf("crc32: %x\n", (int)crc32); + + return 0; +}