fix murmur3 hash unaligned memory access
- do not do unaligned accesses on non-x86 hardware - clean up the code a little bitcute-signatures
parent
6d11ec36e6
commit
3694dc5fa2
68
src/blob.c
68
src/blob.c
|
@ -199,49 +199,59 @@ apk_blob_t apk_blob_pushed(apk_blob_t buffer, apk_blob_t left)
|
||||||
return APK_BLOB_PTR_LEN(buffer.ptr, left.ptr - buffer.ptr);
|
return APK_BLOB_PTR_LEN(buffer.ptr, left.ptr - buffer.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t murmur3_32(const char *key, uint32_t len, uint32_t seed)
|
static inline uint32_t rotl32(uint32_t x, int8_t r)
|
||||||
|
{
|
||||||
|
return (x << r) | (x >> (32 - r));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint32_t get_unaligned32(const void *ptr)
|
||||||
|
{
|
||||||
|
#if defined(__x86_64__) || defined(__i386__)
|
||||||
|
return *(const uint32_t *)ptr;
|
||||||
|
#else
|
||||||
|
const uint8_t *p = ptr;
|
||||||
|
return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t murmur3_32(const void *pkey, uint32_t len, uint32_t seed)
|
||||||
{
|
{
|
||||||
static const uint32_t c1 = 0xcc9e2d51;
|
static const uint32_t c1 = 0xcc9e2d51;
|
||||||
static const uint32_t c2 = 0x1b873593;
|
static const uint32_t c2 = 0x1b873593;
|
||||||
static const uint32_t r1 = 15;
|
const uint8_t *key = pkey;
|
||||||
static const uint32_t r2 = 13;
|
|
||||||
static const uint32_t m = 5;
|
|
||||||
static const uint32_t n = 0xe6546b64;
|
|
||||||
uint32_t hash = seed;
|
|
||||||
const int nblocks = len / 4;
|
const int nblocks = len / 4;
|
||||||
const uint32_t *blocks = (const uint32_t *) key;
|
uint32_t k, h = seed;
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < nblocks; i++) {
|
|
||||||
uint32_t k = blocks[i];
|
for (i = 0; i < nblocks; i++, key += 4) {
|
||||||
|
k = get_unaligned32(key);
|
||||||
k *= c1;
|
k *= c1;
|
||||||
k = (k << r1) | (k >> (32 - r1));
|
k = rotl32(k, 15);
|
||||||
k *= c2;
|
k *= c2;
|
||||||
hash ^= k;
|
h ^= k;
|
||||||
hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
|
h = rotl32(h, 13) * 5 + 0xe6546b64;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t *tail = (const uint8_t *) (key + nblocks * 4);
|
k = 0;
|
||||||
uint32_t k1 = 0;
|
|
||||||
|
|
||||||
switch (len & 3) {
|
switch (len & 3) {
|
||||||
case 3:
|
case 3:
|
||||||
k1 ^= tail[2] << 16;
|
k ^= key[2] << 16;
|
||||||
case 2:
|
case 2:
|
||||||
k1 ^= tail[1] << 8;
|
k ^= key[1] << 8;
|
||||||
case 1:
|
case 1:
|
||||||
k1 ^= tail[0];
|
k ^= key[0];
|
||||||
k1 *= c1;
|
k *= c1;
|
||||||
k1 = (k1 << r1) | (k1 >> (32 - r1));
|
k = rotl32(k, 15);
|
||||||
k1 *= c2;
|
k *= c2;
|
||||||
hash ^= k1;
|
h ^= k;
|
||||||
}
|
}
|
||||||
hash ^= len;
|
h ^= len;
|
||||||
hash ^= (hash >> 16);
|
h ^= (h >> 16);
|
||||||
hash *= 0x85ebca6b;
|
h *= 0x85ebca6b;
|
||||||
hash ^= (hash >> 13);
|
h ^= (h >> 13);
|
||||||
hash *= 0xc2b2ae35;
|
h *= 0xc2b2ae35;
|
||||||
hash ^= (hash >> 16);
|
h ^= (h >> 16);
|
||||||
return hash;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long apk_blob_hash_seed(apk_blob_t blob, unsigned long seed)
|
unsigned long apk_blob_hash_seed(apk_blob_t blob, unsigned long seed)
|
||||||
|
|
Loading…
Reference in New Issue