Skip to content

Commit

Permalink
crc32 implementation is not correct but is compatible with libmemcach…
Browse files Browse the repository at this point in the history
…ed; use crc32a for the correct crc32 implementation
manjuraj committed Apr 15, 2013
1 parent 8f3f5f8 commit 653b05f
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -77,7 +77,8 @@ nutcracker can be configured through a YAML file specified by the -c or --conf-f
+ one_at_a_time
+ md5
+ crc16
+ crc32
+ crc32 (crc32 implementation compatible with [libmemcached](http://libmemcached.org/)
+ crc32a (correct crc32 implementation as per the spec)
+ fnv1_64
+ fnv1a_64
+ fnv1_32
20 changes: 19 additions & 1 deletion src/hashkit/nc_crc32.c
Original file line number Diff line number Diff line change
@@ -91,15 +91,33 @@ static const uint32_t crc32tab[256] = {
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};

/*
* CRC-32 implementation compatible with libmemcached library. Unfortunately
* this implementation does not return CRC-32 as per spec.
*/
uint32_t
hash_crc32(const char *key, size_t key_length)
{
uint64_t x;
uint32_t crc = UINT32_MAX;

for (x= 0; x < key_length; x++) {
for (x = 0; x < key_length; x++) {
crc = (crc >> 8) ^ crc32tab[(crc ^ (uint64_t)key[x]) & 0xff];
}

return ((~crc) >> 16) & 0x7fff;
}

uint32_t
hash_crc32a(const char *key, size_t key_length)
{
const uint8_t *p = key;
uint32_t crc;

crc = ~0U;
while (key_length--) {
crc = crc32tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
}

return crc ^ ~0U;
}
4 changes: 3 additions & 1 deletion src/hashkit/nc_hashkit.h
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
ACTION( HASH_MD5, md5 ) \
ACTION( HASH_CRC16, crc16 ) \
ACTION( HASH_CRC32, crc32 ) \
ACTION( HASH_CRC32A, crc32a ) \
ACTION( HASH_FNV1_64, fnv1_64 ) \
ACTION( HASH_FNV1A_64, fnv1a_64 ) \
ACTION( HASH_FNV1_32, fnv1_32 ) \
@@ -56,8 +57,9 @@ typedef enum dist_type {
uint32_t hash_one_at_a_time(const char *key, size_t key_length);
void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
uint32_t hash_md5(const char *key, size_t key_length);
uint32_t hash_crc32(const char *key, size_t key_length);
uint32_t hash_crc16(const char *key, size_t key_length);
uint32_t hash_crc32(const char *key, size_t key_length);
uint32_t hash_crc32a(const char *key, size_t key_length);
uint32_t hash_fnv1_64(const char *key, size_t key_length);
uint32_t hash_fnv1a_64(const char *key, size_t key_length);
uint32_t hash_fnv1_32(const char *key, size_t key_length);

0 comments on commit 653b05f

Please sign in to comment.