41 #include <sys/types.h>
44 #include "qinternal.h"
45 #include "utilities/qhash.h"
67 bool 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 if (lseek(fd, offset, SEEK_SET) != offset) {
130 ssize_t toread, nread;
131 unsigned char buf[32 * 1024];
132 for (toread = nbytes; toread > 0; toread -= nread) {
133 if (toread >
sizeof(buf))
134 nread = read(fd, buf,
sizeof(buf));
136 nread = read(fd, buf, toread);
139 MD5Update(&context, buf, nread);
144 MD5Final(retbuf, &context);
188 if (data == NULL || nbytes == 0)
192 uint32_t h = 0x811C9DC5;
194 for (dp = (
unsigned char *) data; *dp && nbytes > 0; dp++, nbytes--) {
196 h += (h<<1) + (h<<4) + (h<<7) + (h<<8) + (h<<24);
219 if (data == NULL || nbytes == 0)
223 uint64_t h = 0xCBF29CE484222325ULL;
225 for (dp = (
unsigned char *) data; *dp && nbytes > 0; dp++, nbytes--) {
227 h += (h << 1) + (h << 4) + (h << 5) +
228 (h << 7) + (h << 8) + (h << 40);
230 h *= 0x100000001B3ULL;
259 if (data == NULL || nbytes == 0)
262 const uint32_t c1 = 0xcc9e2d51;
263 const uint32_t c2 = 0x1b873593;
265 const int nblocks = nbytes / 4;
266 const uint32_t *blocks = (
const uint32_t *) (data);
267 const uint8_t *tail = (
const uint8_t *) (data + (nblocks * 4));
273 for (i = 0; i < nblocks; i++) {
277 k = (k << 15) | (k >> (32 - 15));
281 h = (h << 13) | (h >> (32 - 13));
282 h = (h * 5) + 0xe6546b64;
286 switch (nbytes & 3) {
294 k = (k << 15) | (k >> (32 - 15));
331 if (data == NULL || nbytes == 0)
334 const uint64_t c1 = 0x87c37b91114253d5ULL;
335 const uint64_t c2 = 0x4cf5ad432745937fULL;
337 const int nblocks = nbytes / 16;
338 const uint64_t *blocks = (
const uint64_t *) (data);
339 const uint8_t *tail = (
const uint8_t *) (data + (nblocks * 16));
346 for (i = 0; i < nblocks; i++) {
347 k1 = blocks[i * 2 + 0];
348 k2 = blocks[i * 2 + 1];
351 k1 = (k1 << 31) | (k1 >> (64 - 31));
355 h1 = (h1 << 27) | (h1 >> (64 - 27));
357 h1 = h1 * 5 + 0x52dce729;
360 k2 = (k2 << 33) | (k2 >> (64 - 33));
364 h2 = (h2 << 31) | (h2 >> (64 - 31));
366 h2 = h2 * 5 + 0x38495ab5;
370 switch (nbytes & 15) {
372 k2 ^= (uint64_t)(tail[14]) << 48;
374 k2 ^= (uint64_t)(tail[13]) << 40;
376 k2 ^= (uint64_t)(tail[12]) << 32;
378 k2 ^= (uint64_t)(tail[11]) << 24;
380 k2 ^= (uint64_t)(tail[10]) << 16;
382 k2 ^= (uint64_t)(tail[9]) << 8;
384 k2 ^= (uint64_t)(tail[8]) << 0;
386 k2 = (k2 << 33) | (k2 >> (64 - 33));
391 k1 ^= (uint64_t)(tail[7]) << 56;
393 k1 ^= (uint64_t)(tail[6]) << 48;
395 k1 ^= (uint64_t)(tail[5]) << 40;
397 k1 ^= (uint64_t)(tail[4]) << 32;
399 k1 ^= (uint64_t)(tail[3]) << 24;
401 k1 ^= (uint64_t)(tail[2]) << 16;
403 k1 ^= (uint64_t)(tail[1]) << 8;
405 k1 ^= (uint64_t)(tail[0]) << 0;
407 k1 = (k1 << 31) | (k1 >> (64 - 31));
422 h1 *= 0xff51afd7ed558ccdULL;
424 h1 *= 0xc4ceb9fe1a85ec53ULL;
428 h2 *= 0xff51afd7ed558ccdULL;
430 h2 *= 0xc4ceb9fe1a85ec53ULL;
436 ((uint64_t *) retbuf)[0] = h1;
437 ((uint64_t *) retbuf)[1] = h2;
bool qhashmurmur3_128(const void *data, size_t nbytes, void *retbuf)
Get 128-bit Murmur3 hash.
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.
bool qhashmd5_file(const char *filepath, off_t offset, ssize_t nbytes, void *retbuf)
Get 128-bit MD5 hash of a file contents.
uint32_t qhashfnv1_32(const void *data, size_t nbytes)
Get 32-bit FNV1 hash.
uint64_t qhashfnv1_64(const void *data, size_t nbytes)
Get 64-bit FNV1 hash integer.