diff options
Diffstat (limited to 'src/xxhash.h')
-rw-r--r-- | src/xxhash.h | 139 |
1 files changed, 70 insertions, 69 deletions
diff --git a/src/xxhash.h b/src/xxhash.h index b89238883e23c..49b0239071e38 100644 --- a/src/xxhash.h +++ b/src/xxhash.h @@ -1,20 +1,20 @@ /* - xxHash - Fast Hash algorithm + xxHash - Extremely Fast Hash algorithm Header File - Copyright (C) 2012-2013, Yann Collet. + Copyright (C) 2012-2014, Yann Collet. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -52,113 +52,114 @@ CRC32 0.43 GB/s 9 MD5-32 0.33 GB/s 10 Ronald L. Rivest SHA1-32 0.28 GB/s 10 -Q.Score is a measure of quality of the hash function. -It depends on successfully passing SMHasher test set. +Q.Score is a measure of quality of the hash function. +It depends on successfully passing SMHasher test set. 10 is a perfect score. */ - -#pragma once +#ifndef LIBUCL_XXHASH_H +#define LIBUCL_XXHASH_H #if defined (__cplusplus) extern "C" { #endif -//**************************** -// Type -//**************************** +/***************************** + Includes +*****************************/ +#include <stddef.h> /* size_t */ +#include <stdint.h> + + +/***************************** + Type +*****************************/ typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; -//**************************** -// Simple Hash Functions -//**************************** +/***************************** + Simple Hash Functions +*****************************/ -unsigned int XXH32 (const void* input, int len, unsigned int seed); +unsigned int XXH32 (const void* input, size_t length, unsigned seed); +uint64_t XXH64 (const void* input, size_t length, uint64_t seed); /* XXH32() : - Calculate the 32-bits hash of sequence of length "len" stored at memory address "input". - The memory between input & input+len must be valid (allocated and read-accessible). + Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". + The memory between input & input+length must be valid (allocated and read-accessible). "seed" can be used to alter the result predictably. This function successfully passes all SMHasher tests. Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s - Note that "len" is type "int", which means it is limited to 2^31-1. - If your data is larger, use the advanced functions below. +XXH64() : + Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". */ -//**************************** -// Advanced Hash Functions -//**************************** - -void* XXH32_init (unsigned int seed); -XXH_errorcode XXH32_update (void* state, const void* input, int len); -unsigned int XXH32_digest (void* state); +/***************************** + Advanced Hash Functions +*****************************/ +typedef struct { int64_t ll[ 6]; } XXH32_state_t; +typedef struct { int64_t ll[11]; } XXH64_state_t; /* -These functions calculate the xxhash of an input provided in several small packets, -as opposed to an input provided as a single block. - -It must be started with : -void* XXH32_init() -The function returns a pointer which holds the state of calculation. +These structures allow static allocation of XXH states. +States must then be initialized using XXHnn_reset() before first use. -This pointer must be provided as "void* state" parameter for XXH32_update(). -XXH32_update() can be called as many times as necessary. -The user must provide a valid (allocated) input. -The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. -Note that "len" is type "int", which means it is limited to 2^31-1. -If your data is larger, it is recommended to chunk your data into blocks -of size for example 2^30 (1GB) to avoid any "int" overflow issue. - -Finally, you can end the calculation anytime, by using XXH32_digest(). -This function returns the final 32-bits hash. -You must provide the same "void* state" parameter created by XXH32_init(). -Memory will be freed by XXH32_digest(). +If you prefer dynamic allocation, please refer to functions below. */ +/* + * !!! + * Rspamd specific: we use the legacy method to free state when digest is obtained + * !!! + */ +void * XXH32_init (unsigned seed); +XXH32_state_t* XXH32_createState(void); +XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); -int XXH32_sizeofState(void); -XXH_errorcode XXH32_resetState(void* state, unsigned int seed); +XXH64_state_t* XXH64_createState(void); +XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); -#define XXH32_SIZEOFSTATE 48 -typedef struct { long long ll[(XXH32_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH32_stateSpace_t; /* -These functions allow user application to make its own allocation for state. +These functions create and release memory for XXH state. +States must then be initialized using XXHnn_reset() before first use. +*/ -XXH32_sizeofState() is used to know how much space must be allocated for the xxHash 32-bits state. -Note that the state must be aligned to access 'long long' fields. Memory must be allocated and referenced by a pointer. -This pointer must then be provided as 'state' into XXH32_resetState(), which initializes the state. -For static allocation purposes (such as allocation on stack, or freestanding systems without malloc()), -use the structure XXH32_stateSpace_t, which will ensure that memory space is large enough and correctly aligned to access 'long long' fields. -*/ +XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned seed); +XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length); +unsigned int XXH32_digest (const XXH32_state_t* statePtr); +XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, uint64_t seed); +XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); +uint64_t XXH64_digest (const XXH64_state_t* statePtr); -unsigned int XXH32_intermediateDigest (void* state); /* -This function does the same as XXH32_digest(), generating a 32-bit hash, -but preserve memory context. -This way, it becomes possible to generate intermediate hashes, and then continue feeding data with XXH32_update(). -To free memory context, use XXH32_digest(), or free(). -*/ +These functions calculate the xxHash of an input provided in multiple smaller packets, +as opposed to an input provided as a single block. +XXH state space must first be allocated, using either static or dynamic method provided above. +Start a new hash by initializing state with a seed, using XXHnn_reset(). -//**************************** -// Deprecated function names -//**************************** -// The following translations are provided to ease code transition -// You are encouraged to no longer this function names -#define XXH32_feed XXH32_update -#define XXH32_result XXH32_digest -#define XXH32_getIntermediateResult XXH32_intermediateDigest +Then, feed the hash state by calling XXHnn_update() as many times as necessary. +Obviously, input must be valid, meaning allocated and read accessible. +The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. +Finally, you can produce a hash anytime, by using XXHnn_digest(). +This function returns the final nn-bits hash. +You can nonetheless continue feeding the hash state with more input, +and therefore get some new hashes, by calling again XXHnn_digest(). + +When you are done, don't forget to free XXH state space, using typically XXHnn_freeState(). +*/ #if defined (__cplusplus) } #endif + +#endif |