99 if (filepath == NULL || offset < 0 || nbytes < 0 || retbuf == NULL) {
104 int fd = open(filepath, O_RDONLY, 0);
109 if (fstat(fd, &st) < 0) {
113 size_t size = st.st_size;
116 if (size < offset + nbytes) {
124 nbytes = size - offset;
129 if (lseek(fd, offset, SEEK_SET) != offset) {
137 ssize_t toread, nread;
138 unsigned char buf[32 * 1024];
139 for (toread = nbytes; toread > 0; toread -= nread) {
140 if (toread >
sizeof(buf))
141 nread = read(fd, buf,
sizeof(buf));
143 nread = read(fd, buf, toread);
146 MD5Update(&context, buf, nread);
151 MD5Final(retbuf, &context);
226 if (data == NULL || nbytes == 0)
230 uint64_t h = 0xCBF29CE484222325ULL;
232 for (dp = (
unsigned char *) data; *dp && nbytes > 0; dp++, nbytes--) {
234 h += (h << 1) + (h << 4) + (h << 5) +
235 (h << 7) + (h << 8) + (h << 40);
237 h *= 0x100000001B3ULL;
266 if (data == NULL || nbytes == 0)
269 const uint32_t c1 = 0xcc9e2d51;
270 const uint32_t c2 = 0x1b873593;
272 const int nblocks = nbytes / 4;
273 const uint32_t *blocks = (
const uint32_t *) (data);
274 const uint8_t *tail = (
const uint8_t *) (data + (nblocks * 4));
280 for (i = 0; i < nblocks; i++) {
284 k = (k << 15) | (k >> (32 - 15));
288 h = (h << 13) | (h >> (32 - 13));
289 h = (h * 5) + 0xe6546b64;
293 switch (nbytes & 3) {
301 k = (k << 15) | (k >> (32 - 15));
338 if (data == NULL || nbytes == 0)
341 const uint64_t c1 = 0x87c37b91114253d5ULL;
342 const uint64_t c2 = 0x4cf5ad432745937fULL;
344 const int nblocks = nbytes / 16;
345 const uint64_t *blocks = (
const uint64_t *) (data);
346 const uint8_t *tail = (
const uint8_t *) (data + (nblocks * 16));
353 for (i = 0; i < nblocks; i++) {
354 k1 = blocks[i * 2 + 0];
355 k2 = blocks[i * 2 + 1];
358 k1 = (k1 << 31) | (k1 >> (64 - 31));
362 h1 = (h1 << 27) | (h1 >> (64 - 27));
364 h1 = h1 * 5 + 0x52dce729;
367 k2 = (k2 << 33) | (k2 >> (64 - 33));
371 h2 = (h2 << 31) | (h2 >> (64 - 31));
373 h2 = h2 * 5 + 0x38495ab5;
377 switch (nbytes & 15) {
379 k2 ^= (uint64_t)(tail[14]) << 48;
381 k2 ^= (uint64_t)(tail[13]) << 40;
383 k2 ^= (uint64_t)(tail[12]) << 32;
385 k2 ^= (uint64_t)(tail[11]) << 24;
387 k2 ^= (uint64_t)(tail[10]) << 16;
389 k2 ^= (uint64_t)(tail[9]) << 8;
391 k2 ^= (uint64_t)(tail[8]) << 0;
393 k2 = (k2 << 33) | (k2 >> (64 - 33));
398 k1 ^= (uint64_t)(tail[7]) << 56;
400 k1 ^= (uint64_t)(tail[6]) << 48;
402 k1 ^= (uint64_t)(tail[5]) << 40;
404 k1 ^= (uint64_t)(tail[4]) << 32;
406 k1 ^= (uint64_t)(tail[3]) << 24;
408 k1 ^= (uint64_t)(tail[2]) << 16;
410 k1 ^= (uint64_t)(tail[1]) << 8;
412 k1 ^= (uint64_t)(tail[0]) << 0;
414 k1 = (k1 << 31) | (k1 >> (64 - 31));
429 h1 *= 0xff51afd7ed558ccdULL;
431 h1 *= 0xc4ceb9fe1a85ec53ULL;
435 h2 *= 0xff51afd7ed558ccdULL;
437 h2 *= 0xc4ceb9fe1a85ec53ULL;
443 ((uint64_t *) retbuf)[0] = h1;
444 ((uint64_t *) retbuf)[1] = h2;
bool qhashmd5(const void *data, size_t nbytes, void *retbuf)
Calculate 128-bit(16-bytes) MD5 hash.
bool qhashmd5_file(const char *filepath, off_t offset, ssize_t nbytes, void *retbuf)
Get 128-bit MD5 hash of a file contents.