Added xxHash support.

This commit is contained in:
hyzboy 2024-07-25 02:02:24 +08:00
parent 09965c1492
commit 6afb401d7d
6 changed files with 7394 additions and 6 deletions

View File

@ -21,7 +21,13 @@ namespace hgl
SHA256, SHA256,
SHA512, SHA512,
ENUM_CLASS_RANGE(Adler32,SHA512) xxH32,
xxH64,
XXH3_64,
XXH3_128,
ENUM_CLASS_RANGE(Adler32,XXH3_128)
};//enum HASH };//enum HASH
/** /**
@ -100,8 +106,12 @@ namespace hgl
using HashCodeSHA1LE =HashCode<20> ; using HashCodeSHA1LE =HashCode<20> ;
using HashCodeSHA256 =HashCode<32> ; using HashCodeSHA256 =HashCode<32> ;
using HashCodeSHA512 =HashCode<64> ; using HashCodeSHA512 =HashCode<64> ;
using HashCodexxH32 =HashCode<4> ;
using HashCodexxH64 =HashCode<8> ;
using HashCodeXXH3_64 =HashCode<8> ;
using HashCodeXXH3_128 =HashCode<16> ;
const int hash_code_bytes[]={4,4,16,16,20,20,32,64}; //hash码长度 const int hash_code_bytes[]={4,4,16,16,20,20,32,64,4,8,16}; //hash码长度
/** /**
* *
@ -146,6 +156,10 @@ namespace hgl
HGL_CREATE_HASH_FUNC(SHA1LE) HGL_CREATE_HASH_FUNC(SHA1LE)
HGL_CREATE_HASH_FUNC(SHA256) HGL_CREATE_HASH_FUNC(SHA256)
HGL_CREATE_HASH_FUNC(SHA512) HGL_CREATE_HASH_FUNC(SHA512)
HGL_CREATE_HASH_FUNC(xxH32)
HGL_CREATE_HASH_FUNC(xxH64)
HGL_CREATE_HASH_FUNC(XXH3_64)
HGL_CREATE_HASH_FUNC(XXH3_128)
#undef HGL_CREATE_HASH_FUNC #undef HGL_CREATE_HASH_FUNC
@ -164,7 +178,11 @@ namespace hgl
CreateSHA1Hash, CreateSHA1Hash,
CreateSHA1LEHash, CreateSHA1LEHash,
CreateSHA256Hash, CreateSHA256Hash,
CreateSHA512Hash CreateSHA512Hash,
CreatexxH32Hash,
CreatexxH64Hash,
CreateXXH3_64Hash,
CreateXXH3_128Hash,
}; };
return func[(size_t)ha](); return func[(size_t)ha]();
@ -218,7 +236,11 @@ namespace hgl
CountHash<HASH::SHA1 >, CountHash<HASH::SHA1 >,
CountHash<HASH::SHA1LE >, CountHash<HASH::SHA1LE >,
CountHash<HASH::SHA256 >, CountHash<HASH::SHA256 >,
CountHash<HASH::SHA512 > CountHash<HASH::SHA512 >,
CountHash<HASH::xxH32 >,
CountHash<HASH::xxH64 >,
CountHash<HASH::XXH3_64 >,
CountHash<HASH::XXH3_128>
}; };
return func[(size_t)ha](data,size,hash_code); return func[(size_t)ha](data,size,hash_code);
@ -285,7 +307,11 @@ namespace hgl
CountHashStr<HASH::SHA1 >, CountHashStr<HASH::SHA1 >,
CountHashStr<HASH::SHA1LE >, CountHashStr<HASH::SHA1LE >,
CountHashStr<HASH::SHA256 >, CountHashStr<HASH::SHA256 >,
CountHashStr<HASH::SHA512 > CountHashStr<HASH::SHA512 >,
CountHashStr<HASH::xxH32 >,
CountHashStr<HASH::xxH64 >,
CountHashStr<HASH::XXH3_64 >,
CountHashStr<HASH::XXH3_128 >
}; };
return func[(size_t)ha](data,size,hash_str,litter); return func[(size_t)ha](data,size,hash_str,litter);
@ -303,6 +329,10 @@ namespace hgl
HGL_COUNT_HASH_FUNC(SHA1LE) HGL_COUNT_HASH_FUNC(SHA1LE)
HGL_COUNT_HASH_FUNC(SHA256) HGL_COUNT_HASH_FUNC(SHA256)
HGL_COUNT_HASH_FUNC(SHA512) HGL_COUNT_HASH_FUNC(SHA512)
HGL_COUNT_HASH_FUNC(xxH32)
HGL_COUNT_HASH_FUNC(xxH64)
HGL_COUNT_HASH_FUNC(XXH3_64)
HGL_COUNT_HASH_FUNC(XXH3_128)
#undef HGL_COUNT_HASH_FUNC #undef HGL_COUNT_HASH_FUNC

View File

@ -50,6 +50,13 @@ ENDIF(CM_UTIL_SUPPORT_JSON)
option(CM_UTIL_SUPPORT_HASH "Build HASH module" ON) option(CM_UTIL_SUPPORT_HASH "Build HASH module" ON)
IF(CM_UTIL_SUPPORT_HASH) IF(CM_UTIL_SUPPORT_HASH)
SET(XXHASH_FILES hash/xxHash/xxh3.h
hash/xxHash/xxhash.h
hash/xxHash/xxhash.c)
SOURCE_GROUP("HASH\\xxHash" FILES ${XXHASH_FILES})
SET(HASH_HEADER_FILES ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/hash/Hash.h SET(HASH_HEADER_FILES ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/hash/Hash.h
${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/hash/SHA1LE.h) ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/hash/SHA1LE.h)
@ -61,7 +68,8 @@ IF(CM_UTIL_SUPPORT_HASH)
hash/sha1.cpp hash/sha1.cpp
hash/sha1le.cpp hash/sha1le.cpp
hash/sha256.cpp hash/sha256.cpp
hash/sha512.cpp) hash/sha512.cpp
hash/xxHash3.cpp)
SOURCE_GROUP("HASH" FILES ${HASH_HEADER_FILES} ${HASH_SOURCE_FILES}) SOURCE_GROUP("HASH" FILES ${HASH_HEADER_FILES} ${HASH_SOURCE_FILES})
ENDIF(CM_UTIL_SUPPORT_HASH) ENDIF(CM_UTIL_SUPPORT_HASH)
@ -111,6 +119,7 @@ add_cm_library(CMUtil "CM" ${CMD_SOURCE}
${JSON_TOOL_SOURCE} ${JSON_TOOL_SOURCE}
${CSV_SOURCE} ${CSV_SOURCE}
${XXHASH_FILES}
${HASH_HEADER_FILES} ${HASH_HEADER_FILES}
${HASH_SOURCE_FILES} ${HASH_SOURCE_FILES}

55
src/hash/xxHash/xxh3.h Normal file
View File

@ -0,0 +1,55 @@
/*
* xxHash - Extremely Fast Hash algorithm
* Development source file for `xxh3`
* Copyright (C) 2019-2021 Yann Collet
*
* BSD 2-Clause License (https://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
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at:
* - xxHash homepage: https://www.xxhash.com
* - xxHash source repository: https://github.com/Cyan4973/xxHash
*/
/*
* Note: This file used to host the source code of XXH3_* variants.
* during the development period.
* The source code is now properly integrated within xxhash.h.
*
* xxh3.h is no longer useful,
* but it is still provided for compatibility with source code
* which used to include it directly.
*
* Programs are now highly discouraged to include xxh3.h.
* Include `xxhash.h` instead, which is the officially supported interface.
*
* In the future, xxh3.h will start to generate warnings, then errors,
* then it will be removed from source package and from include directory.
*/
/* Simulate the same impact as including the old xxh3.h source file */
#define XXH_INLINE_ALL
#include "xxhash.h"

42
src/hash/xxHash/xxhash.c Normal file
View File

@ -0,0 +1,42 @@
/*
* xxHash - Extremely Fast Hash algorithm
* Copyright (C) 2012-2023 Yann Collet
*
* BSD 2-Clause License (https://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
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at:
* - xxHash homepage: https://www.xxhash.com
* - xxHash source repository: https://github.com/Cyan4973/xxHash
*/
/*
* xxhash.c instantiates functions defined in xxhash.h
*/
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */
#include "xxhash.h"

7089
src/hash/xxHash/xxhash.h Normal file

File diff suppressed because it is too large Load Diff

163
src/hash/xxHash3.cpp Normal file
View File

@ -0,0 +1,163 @@
#include"xxHash/xxh3.h"
#include<hgl/util/hash/Hash.h>
#include<random>
namespace hgl
{
namespace util
{
namespace
{
static std::random_device rd;
static std::mt19937 mt_rand32(rd());
static std::uniform_int_distribution<uint32> mt_rand32_dist;
static std::mt19937_64 mt_rand64(rd());
static std::uniform_int_distribution<uint64> mt_rand64_dist;
}
class xxHash32:public Hash
{
XXH32_state_t *state=nullptr;
uint32 seed;
public:
void GetName(UTF8String &str)const override{str="XXH32";}
void GetName(UTF16String &str)const override{str=U16_TEXT("XXH32");}
const int GetHashBytes()const override{return 4;}
void Init()override
{
state=XXH32_createState();
seed=mt_rand32_dist(mt_rand32);
XXH32_reset(state,seed);
}
void Update(const void *input,uint inputLen)override
{
XXH32_update(state,input,inputLen);
}
void Final(void *digest)override
{
*(XXH32_hash_t *)digest=XXH32_digest(state);
}
};//class xxHash32
Hash *CreatexxH32Hash()
{
return(new xxHash32);
}
class xxHash64:public Hash
{
XXH64_state_t *state=nullptr;
uint64 seed;
public:
void GetName(UTF8String &str)const override{str="XXH64";}
void GetName(UTF16String &str)const override{str=U16_TEXT("XXH64");}
const int GetHashBytes()const override{return 8;}
void Init()override
{
state=XXH64_createState();
seed=mt_rand64_dist(mt_rand64);
XXH64_reset(state,seed);
}
void Update(const void *input,uint inputLen)override
{
XXH64_update(state,input,inputLen);
}
void Final(void *digest)override
{
*(XXH64_hash_t *)digest=XXH64_digest(state);
}
};//class xxHash64
Hash *CreatexxH64Hash()
{
return(new xxHash64);
}
class xxHash3_64:public Hash
{
XXH3_state_t *state=nullptr;
uint64 seed;
public:
void GetName(UTF8String &str)const override{str="XXH3_64bits";}
void GetName(UTF16String &str)const override{str=U16_TEXT("XXH3_64bits");}
const int GetHashBytes()const override{return 8;}
void Init()override
{
state=XXH3_createState();
seed=mt_rand64_dist(mt_rand64);
XXH3_64bits_reset_withSeed(state,seed);
}
void Update(const void *input,uint inputLen)override
{
XXH3_64bits_update(state,input,inputLen);
}
void Final(void *digest)override
{
*(XXH64_hash_t *)digest=XXH3_64bits_digest(state);
}
};//class xxHash3_64
Hash *CreateXXH3_64Hash()
{
return(new xxHash3_64);
}
class xxHash3_128:public Hash
{
XXH3_state_t *state=nullptr;
XXH64_hash_t seed;
public:
void GetName(UTF8String &str)const override{str="XXH3_128bits";}
void GetName(UTF16String &str)const override{str=U16_TEXT("XXH3_128bits");}
const int GetHashBytes()const override{return 16;}
void Init()override
{
state=XXH3_createState();
seed=mt_rand64_dist(mt_rand64);
XXH3_128bits_reset_withSeed(state,seed);
}
void Update(const void *input,uint inputLen)override
{
XXH3_128bits_update(state,input,inputLen);
}
void Final(void *digest)override
{
*(XXH128_hash_t *)digest=XXH3_128bits_digest(state);
}
};//class xxHash3_128
Hash *CreateXXH3_128Hash()
{
return(new xxHash3_128);
}
}//namespace util
}//namespace hgl