ext/digest/murmurhash/murmurhash2a.c in digest-murmurhash-0.2.3 vs ext/digest/murmurhash/murmurhash2a.c in digest-murmurhash-0.3.0

- old
+ new

@@ -5,19 +5,19 @@ #include "murmurhash2a.h" #define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } static uint32_t -murmur_hash_process2a(const void *key, uint32_t length) +murmur_hash_process2a(const char *key, uint32_t length, uint32_t seed) { const uint32_t m = MURMURHASH_MAGIC; const uint8_t r = 24; uint32_t h, k, t, l; const unsigned char *data = (const unsigned char *) key; l = length; - h = 0 ^ length; + h = seed; while (4 <= length) { k = *(uint32_t*)data; mmix(h,k); data += 4; @@ -39,56 +39,42 @@ h ^= h >> 15; return h; } +static uint32_t +_murmur2a_finish(VALUE self) +{ + const char *seed = RSTRING_PTR(murmur_seed_get32(self)); + MURMURHASH(self, ptr); + return murmur_hash_process2a(ptr->buffer, ptr->p - ptr->buffer, *(uint32_t*)seed); +} + VALUE murmur2a_finish(VALUE self) { - uint32_t h; uint8_t digest[4]; - MURMURHASH(self, ptr); + uint64_t h; - h = murmur_hash_process2a(ptr->buffer, ptr->p - ptr->buffer); - - digest[0] = h >> 24; - digest[1] = h >> 16; - digest[2] = h >> 8; - digest[3] = h; - + h = _murmur2a_finish(self); + ASSINE_BY_ENDIAN_32(digest, h); return rb_str_new((const char*) digest, 4); } VALUE murmur2a_to_i(VALUE self) { - MURMURHASH(self, ptr); - return UINT2NUM(murmur_hash_process2a(ptr->buffer, ptr->p - ptr->buffer)); + return ULONG2NUM(_murmur2a_finish(self)); } VALUE murmur2a_s_digest(int argc, VALUE *argv, VALUE klass) { - VALUE str; - uint32_t h; uint8_t digest[4]; - - if (argc < 1) - rb_raise(rb_eArgError, "no data given"); - - str = *argv++; - argc--; - - StringValue(str); - - h = murmur_hash_process2a(RSTRING_PTR(str), RSTRING_LEN(str)); - - digest[0] = h >> 24; - digest[1] = h >> 16; - digest[2] = h >> 8; - digest[3] = h; - + uint32_t h; + h = _murmur_s_digest32(argc, argv, klass, murmur_hash_process2a); + ASSINE_BY_ENDIAN_32(digest, h); return rb_str_new((const char*) digest, 4); } VALUE murmur2a_s_hexdigest(int argc, VALUE *argv, VALUE klass) @@ -97,18 +83,8 @@ } VALUE murmur2a_s_rawdigest(int argc, VALUE *argv, VALUE klass) { - VALUE str; - - if (argc < 1) - rb_raise(rb_eArgError, "no data given"); - - str = *argv++; - argc--; - - StringValue(str); - - return UINT2NUM(murmur_hash_process2a(RSTRING_PTR(str), RSTRING_LEN(str))); + return ULL2NUM(_murmur_s_digest32(argc, argv, klass, murmur_hash_process2a)); }