45#include "utilities/qhash.h"
67bool qhashmd5(
const void *data,
size_t nbytes,
void *retbuf) {
68 if (data == NULL || retbuf == NULL) {
75 MD5Update(&context, (
unsigned char *) data, (
unsigned int) nbytes);
76 MD5Final(retbuf, &context);
99 if (filepath == NULL || offset < 0 || nbytes < 0 || retbuf == NULL) {
104 int fd = open(filepath, O_RDONLY, 0);
109 if (fstat(fd, &st) < 0)
111 size_t size = st.st_size;
114 if (size < offset + nbytes) {
122 nbytes = size - offset;
127 if (lseek(fd, offset, SEEK_SET) != offset) {
135 ssize_t toread, nread;
136 unsigned char buf[32 * 1024];
137 for (toread = nbytes; toread > 0; toread -= nread) {
138 if (toread >
sizeof(buf))
139 nread = read(fd, buf,
sizeof(buf));
141 nread = read(fd, buf, toread);
144 MD5Update(&context, buf, nread);
149 MD5Final(retbuf, &context);
193 if (data == NULL || nbytes == 0)
197 uint32_t h = 0x811C9DC5;
199 for (dp = (
unsigned char *) data; *dp && nbytes > 0; dp++, nbytes--) {
201 h += (h<<1) + (h<<4) + (h<<7) + (h<<8) + (h<<24);
224 if (data == NULL || nbytes == 0)
228 uint64_t h = 0xCBF29CE484222325ULL;
230 for (dp = (
unsigned char *) data; *dp && nbytes > 0; dp++, nbytes--) {
232 h += (h << 1) + (h << 4) + (h << 5) +
233 (h << 7) + (h << 8) + (h << 40);
235 h *= 0x100000001B3ULL;
264 if (data == NULL || nbytes == 0)
267 const uint32_t c1 = 0xcc9e2d51;
268 const uint32_t c2 = 0x1b873593;
270 const int nblocks = nbytes / 4;
271 const uint32_t *blocks = (
const uint32_t *) (data);
272 const uint8_t *tail = (
const uint8_t *) (data + (nblocks * 4));
278 for (i = 0; i < nblocks; i++) {
282 k = (k << 15) | (k >> (32 - 15));
286 h = (h << 13) | (h >> (32 - 13));
287 h = (h * 5) + 0xe6546b64;
291 switch (nbytes & 3) {
299 k = (k << 15) | (k >> (32 - 15));
336 if (data == NULL || nbytes == 0)
339 const uint64_t c1 = 0x87c37b91114253d5ULL;
340 const uint64_t c2 = 0x4cf5ad432745937fULL;
342 const int nblocks = nbytes / 16;
343 const uint64_t *blocks = (
const uint64_t *) (data);
344 const uint8_t *tail = (
const uint8_t *) (data + (nblocks * 16));
351 for (i = 0; i < nblocks; i++) {
352 k1 = blocks[i * 2 + 0];
353 k2 = blocks[i * 2 + 1];
356 k1 = (k1 << 31) | (k1 >> (64 - 31));
360 h1 = (h1 << 27) | (h1 >> (64 - 27));
362 h1 = h1 * 5 + 0x52dce729;
365 k2 = (k2 << 33) | (k2 >> (64 - 33));
369 h2 = (h2 << 31) | (h2 >> (64 - 31));
371 h2 = h2 * 5 + 0x38495ab5;
375 switch (nbytes & 15) {
377 k2 ^= (uint64_t)(tail[14]) << 48;
379 k2 ^= (uint64_t)(tail[13]) << 40;
381 k2 ^= (uint64_t)(tail[12]) << 32;
383 k2 ^= (uint64_t)(tail[11]) << 24;
385 k2 ^= (uint64_t)(tail[10]) << 16;
387 k2 ^= (uint64_t)(tail[9]) << 8;
389 k2 ^= (uint64_t)(tail[8]) << 0;
391 k2 = (k2 << 33) | (k2 >> (64 - 33));
396 k1 ^= (uint64_t)(tail[7]) << 56;
398 k1 ^= (uint64_t)(tail[6]) << 48;
400 k1 ^= (uint64_t)(tail[5]) << 40;
402 k1 ^= (uint64_t)(tail[4]) << 32;
404 k1 ^= (uint64_t)(tail[3]) << 24;
406 k1 ^= (uint64_t)(tail[2]) << 16;
408 k1 ^= (uint64_t)(tail[1]) << 8;
410 k1 ^= (uint64_t)(tail[0]) << 0;
412 k1 = (k1 << 31) | (k1 >> (64 - 31));
427 h1 *= 0xff51afd7ed558ccdULL;
429 h1 *= 0xc4ceb9fe1a85ec53ULL;
433 h2 *= 0xff51afd7ed558ccdULL;
435 h2 *= 0xc4ceb9fe1a85ec53ULL;
441 ((uint64_t *) retbuf)[0] = h1;
442 ((uint64_t *) retbuf)[1] = h2;
bool qhashmd5(const void *data, size_t nbytes, void *retbuf)
Calculate 128-bit(16-bytes) MD5 hash.
uint32_t qhashmurmur3_32(const void *data, size_t nbytes)
Get 32-bit Murmur3 hash.
uint64_t qhashfnv1_64(const void *data, size_t nbytes)
Get 64-bit FNV1 hash integer.
bool qhashmurmur3_128(const void *data, size_t nbytes, void *retbuf)
Get 128-bit Murmur3 hash.
uint32_t qhashfnv1_32(const void *data, size_t nbytes)
Get 32-bit FNV1 hash.
bool qhashmd5_file(const char *filepath, off_t offset, ssize_t nbytes, void *retbuf)
Get 128-bit MD5 hash of a file contents.