csum-xxhash.c 2.32 KB
Newer Older
1
/*
Mark Fasheh's avatar
Mark Fasheh committed
2
 * csum-xxhash.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Copyright (C) 2013 SUSE.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */


#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include "csum.h"
#include "debug.h"
#include "xxhash.h"

27
#define		HASH_TYPE_XXHASH	"XXHASH  "
28

29
static int xxhash_init_hash(unsigned int *ret_digest_len)
30
{
31
	*ret_digest_len = DIGEST_LEN_MAX;
32 33 34
	return 0;
}

35
static void xxhash_checksum_block(char *buf, int len, unsigned char *digest) {
Mark Fasheh's avatar
Mark Fasheh committed
36
	unsigned long long *hash = (unsigned long long*)digest;
37 38 39 40 41
	/*
	 * For xxhash one use only first 64 bit from 256 bit hash field
	 * Zeroing empty 192 bits with offset
	 */
	memset(&hash[1], 0, DIGEST_LEN_MAX-sizeof(*hash));
42
	*hash = XXH64(buf, len, 0);
43 44
}

45
struct xxhash_running_checksum {
46
	XXH64_state_t	td64;
47
};
48
DECLARE_RUNNING_CSUM_CAST_FUNCS(xxhash_running_checksum);
49

50
static struct running_checksum *xxhash_start_running_checksum(void)
51
{
52 53
	struct xxhash_running_checksum *c =
		calloc(1, sizeof(struct xxhash_running_checksum));
54
	XXH64_reset(&c->td64, 0);
55
	return priv_to_rc(c);
56 57
}

58 59
static void xxhash_add_to_running_checksum(struct running_checksum *_c,
					   unsigned int len, unsigned char *buf)
60
{
61
	struct xxhash_running_checksum *c = rc_to_priv(_c);
62
	XXH64_update(&c->td64, buf, len);
63 64
}

65 66
static void xxhash_finish_running_checksum(struct running_checksum *_c,
					   unsigned char *digest)
67
{
68
	struct xxhash_running_checksum *c = rc_to_priv(_c);
Mark Fasheh's avatar
Mark Fasheh committed
69
	unsigned long long *hash = (unsigned long long*)digest;
70 71

	*hash = XXH64_digest(&c->td64);
72 73
	free(c);
}
74

75
static struct csum_module_ops ops_xxhash = {
76 77 78 79 80 81 82 83 84 85 86 87
	.init			= xxhash_init_hash,
	.checksum_block		= xxhash_checksum_block,
	.start_running_checksum	= xxhash_start_running_checksum,
	.add_to_running_checksum	= xxhash_add_to_running_checksum,
	.finish_running_checksum	= xxhash_finish_running_checksum,
};

struct csum_module csum_module_xxhash =	{
	.name = "xxhash",
	.hash_type = HASH_TYPE_XXHASH,
	.ops = &ops_xxhash,
};