correct CRC32

pull/37/head
yuqing 2018-06-07 20:43:32 +08:00
parent 63978b63ed
commit 530c17aaa1
5 changed files with 56 additions and 10 deletions

View File

@ -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:

View File

@ -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;
}

View File

@ -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)

View File

@ -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:

46
src/tests/test_crc32.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <inttypes.h>
#include <sys/time.h>
#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 <filename>\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;
}