1 /* 2 xxHash - Extremely Fast Hash algorithm 3 Header File 4 Copyright (C) 2012-2016, Yann Collet. 5 6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 7 8 Redistribution and use in source and binary forms, with or without 9 modification, are permitted provided that the following conditions are 10 met: 11 12 * Redistributions of source code must retain the above copyright 13 notice, this list of conditions and the following disclaimer. 14 * Redistributions in binary form must reproduce the above 15 copyright notice, this list of conditions and the following disclaimer 16 in the documentation and/or other materials provided with the 17 distribution. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 You can contact the author at : 32 - xxHash source repository : https://github.com/Cyan4973/xxHash 33 */ 34 35 /* Notice extracted from xxHash homepage : 36 37 xxHash is an extremely fast Hash algorithm, running at RAM speed limits. 38 It also successfully passes all tests from the SMHasher suite. 39 40 Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) 41 42 Name Speed Q.Score Author 43 xxHash 5.4 GB/s 10 44 CrapWow 3.2 GB/s 2 Andrew 45 MumurHash 3a 2.7 GB/s 10 Austin Appleby 46 SpookyHash 2.0 GB/s 10 Bob Jenkins 47 SBox 1.4 GB/s 9 Bret Mulvey 48 Lookup3 1.2 GB/s 9 Bob Jenkins 49 SuperFastHash 1.2 GB/s 1 Paul Hsieh 50 CityHash64 1.05 GB/s 10 Pike & Alakuijala 51 FNV 0.55 GB/s 5 Fowler, Noll, Vo 52 CRC32 0.43 GB/s † 9 53 MD5-32 0.33 GB/s 10 Ronald L. Rivest 54 SHA1-32 0.28 GB/s 10 55 56 Note †: other CRC32 implementations can be over 40x faster than SMHasher's: 57 http://fastcompression.blogspot.com/2019/03/presenting-xxh3.html?showComment=1552696407071#c3490092340461170735 58 59 Q.Score is a measure of quality of the hash function. 60 It depends on successfully passing SMHasher test set. 61 10 is a perfect score. 62 63 A 64-bit version, named XXH64, is available since r35. 64 It offers much better speed, but for 64-bit applications only. 65 Name Speed on 64 bits Speed on 32 bits 66 XXH64 13.8 GB/s 1.9 GB/s 67 XXH32 6.8 GB/s 6.0 GB/s 68 */ 69 70 #ifndef XXHASH_H_5627135585666179 71 #define XXHASH_H_5627135585666179 1 72 73 #if defined (__cplusplus) 74 extern "C" { 75 #endif 76 77 78 /* **************************** 79 * Definitions 80 ******************************/ 81 #include <stddef.h> /* size_t */ 82 typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; 83 84 85 /* **************************** 86 * API modifier 87 ******************************/ 88 /** XXH_INLINE_ALL (and XXH_PRIVATE_API) 89 * This build macro includes xxhash functions in `static` mode 90 * in order to inline them, and remove their symbol from the public list. 91 * Inlining offers great performance improvement on small keys, 92 * and dramatic ones when length is expressed as a compile-time constant. 93 * See https://fastcompression.blogspot.com/2018/03/xxhash-for-small-keys-impressive-power.html . 94 * Methodology : 95 * #define XXH_INLINE_ALL 96 * #include "xxhash.h" 97 * `xxhash.c` is automatically included. 98 * It's not useful to compile and link it as a separate object. 99 */ 100 #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) 101 # ifndef XXH_STATIC_LINKING_ONLY 102 # define XXH_STATIC_LINKING_ONLY 103 # endif 104 # if defined(__GNUC__) 105 # define XXH_PUBLIC_API static __inline __attribute__((unused)) 106 # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 107 # define XXH_PUBLIC_API static inline 108 # elif defined(_MSC_VER) 109 # define XXH_PUBLIC_API static __inline 110 # else 111 /* this version may generate warnings for unused static functions */ 112 # define XXH_PUBLIC_API static 113 # endif 114 #else 115 # if defined(WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT)) 116 # ifdef XXH_EXPORT 117 # define XXH_PUBLIC_API __declspec(dllexport) 118 # elif XXH_IMPORT 119 # define XXH_PUBLIC_API __declspec(dllimport) 120 # endif 121 # else 122 # define XXH_PUBLIC_API /* do nothing */ 123 # endif 124 #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */ 125 126 /*! XXH_NAMESPACE, aka Namespace Emulation : 127 * 128 * If you want to include _and expose_ xxHash functions from within your own library, 129 * but also want to avoid symbol collisions with other libraries which may also include xxHash, 130 * 131 * you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library 132 * with the value of XXH_NAMESPACE (therefore, avoid NULL and numeric values). 133 * 134 * Note that no change is required within the calling program as long as it includes `xxhash.h` : 135 * regular symbol name will be automatically translated by this header. 136 */ 137 #ifdef XXH_NAMESPACE 138 # define XXH_CAT(A,B) A##B 139 # define XXH_NAME2(A,B) XXH_CAT(A,B) 140 # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber) 141 # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32) 142 # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState) 143 # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState) 144 # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset) 145 # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update) 146 # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest) 147 # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState) 148 # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash) 149 # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical) 150 # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64) 151 # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState) 152 # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState) 153 # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset) 154 # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update) 155 # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest) 156 # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState) 157 # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash) 158 # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical) 159 #endif 160 161 162 /* ************************************* 163 * Version 164 ***************************************/ 165 #define XXH_VERSION_MAJOR 0 166 #define XXH_VERSION_MINOR 7 167 #define XXH_VERSION_RELEASE 2 168 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE) 169 XXH_PUBLIC_API unsigned XXH_versionNumber (void); 170 171 172 /*-********************************************************************** 173 * 32-bit hash 174 ************************************************************************/ 175 #if !defined (__VMS) \ 176 && (defined (__cplusplus) \ 177 || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) 178 # include <stdint.h> 179 typedef uint32_t XXH32_hash_t; 180 #else 181 # include <limits.h> 182 # if UINT_MAX == 0xFFFFFFFFUL 183 typedef unsigned int XXH32_hash_t; 184 # else 185 # if ULONG_MAX == 0xFFFFFFFFUL 186 typedef unsigned long XXH32_hash_t; 187 # else 188 # error "unsupported platform : need a 32-bit type" 189 # endif 190 # endif 191 #endif 192 193 /*! XXH32() : 194 Calculate the 32-bit hash of sequence "length" bytes stored at memory address "input". 195 The memory between input & input+length must be valid (allocated and read-accessible). 196 "seed" can be used to alter the result predictably. 197 Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s */ 198 XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, XXH32_hash_t seed); 199 200 /*====== Streaming ======*/ 201 202 /* 203 * Streaming functions generate the xxHash value from an incrememtal input. 204 * This method is slower than single-call functions, due to state management. 205 * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized. 206 * 207 * XXH state must first be allocated, using XXH*_createState() . 208 * 209 * Start a new hash by initializing state with a seed, using XXH*_reset(). 210 * 211 * Then, feed the hash state by calling XXH*_update() as many times as necessary. 212 * The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. 213 * 214 * Finally, a hash value can be produced anytime, by using XXH*_digest(). 215 * This function returns the nn-bits hash as an int or long long. 216 * 217 * It's still possible to continue inserting input into the hash state after a digest, 218 * and generate some new hash values later on, by invoking again XXH*_digest(). 219 * 220 * When done, release the state, using XXH*_freeState(). 221 */ 222 223 typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */ 224 XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void); 225 XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); 226 XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state); 227 228 XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, XXH32_hash_t seed); 229 XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length); 230 XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr); 231 232 /*====== Canonical representation ======*/ 233 234 /* Default return values from XXH functions are basic unsigned 32 and 64 bits. 235 * This the simplest and fastest format for further post-processing. 236 * However, this leaves open the question of what is the order of bytes, 237 * since little and big endian conventions will write the same number differently. 238 * 239 * The canonical representation settles this issue, 240 * by mandating big-endian convention, 241 * aka, the same convention as human-readable numbers (large digits first). 242 * When writing hash values to storage, sending them over a network, or printing them, 243 * it's highly recommended to use the canonical representation, 244 * to ensure portability across a wider range of systems, present and future. 245 * 246 * The following functions allow transformation of hash values into and from canonical format. 247 */ 248 249 typedef struct { unsigned char digest[4]; } XXH32_canonical_t; 250 XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash); 251 XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src); 252 253 254 #ifndef XXH_NO_LONG_LONG 255 /*-********************************************************************** 256 * 64-bit hash 257 ************************************************************************/ 258 #if !defined (__VMS) \ 259 && (defined (__cplusplus) \ 260 || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) 261 # include <stdint.h> 262 typedef uint64_t XXH64_hash_t; 263 #else 264 /* the following type must have a width of 64-bit */ 265 typedef unsigned long long XXH64_hash_t; 266 #endif 267 268 /*! XXH64() : 269 Calculate the 64-bit hash of sequence of length "len" stored at memory address "input". 270 "seed" can be used to alter the result predictably. 271 This function runs faster on 64-bit systems, but slower on 32-bit systems (see benchmark). 272 */ 273 XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, XXH64_hash_t seed); 274 275 /*====== Streaming ======*/ 276 typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */ 277 XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void); 278 XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); 279 XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dst_state, const XXH64_state_t* src_state); 280 281 XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, XXH64_hash_t seed); 282 XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); 283 XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr); 284 285 /*====== Canonical representation ======*/ 286 typedef struct { unsigned char digest[8]; } XXH64_canonical_t; 287 XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash); 288 XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src); 289 290 291 #endif /* XXH_NO_LONG_LONG */ 292 293 294 295 #ifdef XXH_STATIC_LINKING_ONLY 296 297 /* ================================================================================================ 298 This section contains declarations which are not guaranteed to remain stable. 299 They may change in future versions, becoming incompatible with a different version of the library. 300 These declarations should only be used with static linking. 301 Never use them in association with dynamic linking ! 302 =================================================================================================== */ 303 304 /* These definitions are only present to allow 305 * static allocation of XXH state, on stack or in a struct for example. 306 * Never **ever** use members directly. */ 307 308 struct XXH32_state_s { 309 XXH32_hash_t total_len_32; 310 XXH32_hash_t large_len; 311 XXH32_hash_t v1; 312 XXH32_hash_t v2; 313 XXH32_hash_t v3; 314 XXH32_hash_t v4; 315 XXH32_hash_t mem32[4]; 316 XXH32_hash_t memsize; 317 XXH32_hash_t reserved; /* never read nor write, might be removed in a future version */ 318 }; /* typedef'd to XXH32_state_t */ 319 320 #ifndef XXH_NO_LONG_LONG /* remove 64-bit support */ 321 struct XXH64_state_s { 322 XXH64_hash_t total_len; 323 XXH64_hash_t v1; 324 XXH64_hash_t v2; 325 XXH64_hash_t v3; 326 XXH64_hash_t v4; 327 XXH64_hash_t mem64[4]; 328 XXH32_hash_t memsize; 329 XXH32_hash_t reserved32; /* required for padding anyway */ 330 XXH64_hash_t reserved64; /* never read nor write, might be removed in a future version */ 331 }; /* typedef'd to XXH64_state_t */ 332 #endif /* XXH_NO_LONG_LONG */ 333 334 335 /*-********************************************************************** 336 * XXH3 337 * New experimental hash 338 ************************************************************************/ 339 #ifndef XXH_NO_LONG_LONG 340 341 342 /* ============================================ 343 * XXH3 is a new hash algorithm, 344 * featuring improved speed performance for both small and large inputs. 345 * See full speed analysis at : http://fastcompression.blogspot.com/2019/03/presenting-xxh3.html 346 * In general, expect XXH3 to run about ~2x faster on large inputs, 347 * and >3x faster on small ones, though exact differences depend on platform. 348 * 349 * The algorithm is portable, will generate the same hash on all platforms. 350 * It benefits greatly from vectorization units, but does not require it. 351 * 352 * XXH3 offers 2 variants, _64bits and _128bits. 353 * When only 64 bits are needed, prefer calling the _64bits variant : 354 * it reduces the amount of mixing, resulting in faster speed on small inputs. 355 * It's also generally simpler to manipulate a scalar return type than a struct. 356 * 357 * The XXH3 algorithm is still considered experimental. 358 * Produced results can still change between versions. 359 * Results produced by v0.7.x are not comparable with results from v0.7.y . 360 * It's nonetheless possible to use XXH3 for ephemeral data (local sessions), 361 * but avoid storing values in long-term storage for later reads. 362 * 363 * The API supports one-shot hashing, streaming mode, and custom secrets. 364 * 365 * There are still a number of opened questions that community can influence during the experimental period. 366 * I'm trying to list a few of them below, though don't consider this list as complete. 367 * 368 * - 128-bits output type : currently defined as a structure of two 64-bits fields. 369 * That's because 128-bit values do not exist in C standard. 370 * Note that it means that, at byte level, result is not identical depending on endianess. 371 * However, at field level, they are identical on all platforms. 372 * The canonical representation solves the issue of identical byte-level representation across platforms, 373 * which is necessary for serialization. 374 * Q1 : Would there be a better representation for a 128-bit hash result ? 375 * Q2 : Are the names of the inner 64-bit fields important ? Should they be changed ? 376 * 377 * - Prototype XXH128() : XXH128() uses the same arguments as XXH64(), for consistency. 378 * It means it maps to XXH3_128bits_withSeed(). 379 * This variant is slightly slower than XXH3_128bits(), 380 * because the seed is now part of the algorithm, and can't be simplified. 381 * Is that a good idea ? 382 * 383 * - Seed type for XXH128() : currently, it's a single 64-bit value, like the 64-bit variant. 384 * It could be argued that it's more logical to offer a 128-bit seed input parameter for a 128-bit hash. 385 * But 128-bit seed is more difficult to use, since it requires to pass a structure instead of a scalar value. 386 * Such a variant could either replace current one, or become an additional one. 387 * Farmhash, for example, offers both variants (the 128-bits seed variant is called `doubleSeed`). 388 * Follow up question : if both 64-bit and 128-bit seeds are allowed, which variant should be called XXH128 ? 389 * 390 * - Result for len==0 : Currently, the result of hashing a zero-length input is always `0`. 391 * It seems okay as a return value when using "default" secret and seed. 392 * But is it still fine to return `0` when secret or seed are non-default ? 393 * Are there use cases which could depend on generating a different hash result for zero-length input when the secret is different ? 394 * 395 * - Consistency (1) : Streaming XXH128 uses an XXH3 state, which is the same state as XXH3_64bits(). 396 * It means a 128bit streaming loop must invoke the following symbols : 397 * XXH3_createState(), XXH3_128bits_reset(), XXH3_128bits_update() (loop), XXH3_128bits_digest(), XXH3_freeState(). 398 * Is that consistent enough ? 399 * 400 * - Consistency (2) : The canonical representation of `XXH3_64bits` is provided by existing functions 401 * XXH64_canonicalFromHash(), and reverse operation XXH64_hashFromCanonical(). 402 * As a mirror, canonical functions for XXH128_hash_t results generated by `XXH3_128bits` 403 * are XXH128_canonicalFromHash() and XXH128_hashFromCanonical(). 404 * Which means, `XXH3` doesn't appear in the names, because canonical functions operate on a type, 405 * independently of which algorithm was used to generate that type. 406 * Is that consistent enough ? 407 */ 408 409 #ifdef XXH_NAMESPACE 410 # define XXH3_64bits XXH_NAME2(XXH_NAMESPACE, XXH3_64bits) 411 # define XXH3_64bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecret) 412 # define XXH3_64bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSeed) 413 414 # define XXH3_createState XXH_NAME2(XXH_NAMESPACE, XXH3_createState) 415 # define XXH3_freeState XXH_NAME2(XXH_NAMESPACE, XXH3_freeState) 416 # define XXH3_copyState XXH_NAME2(XXH_NAMESPACE, XXH3_copyState) 417 418 # define XXH3_64bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset) 419 # define XXH3_64bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSeed) 420 # define XXH3_64bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecret) 421 # define XXH3_64bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_update) 422 # define XXH3_64bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_digest) 423 #endif 424 425 /* XXH3_64bits() : 426 * default 64-bit variant, using default secret and default seed of 0. 427 * It's the fastest variant. */ 428 XXH_PUBLIC_API XXH64_hash_t XXH3_64bits(const void* data, size_t len); 429 430 /* XXH3_64bits_withSecret() : 431 * It's possible to provide any blob of bytes as a "secret" to generate the hash. 432 * This makes it more difficult for an external actor to prepare an intentional collision. 433 * The secret *must* be large enough (>= XXH3_SECRET_SIZE_MIN). 434 * It should consist of random bytes. 435 * Avoid repeating same character, or sequences of bytes, 436 * and especially avoid swathes of \0. 437 * Failure to respect these conditions will result in a poor quality hash. 438 */ 439 #define XXH3_SECRET_SIZE_MIN 136 440 XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize); 441 442 /* XXH3_64bits_withSeed() : 443 * This variant generates on the fly a custom secret, 444 * based on the default secret, altered using the `seed` value. 445 * While this operation is decently fast, note that it's not completely free. 446 * note : seed==0 produces same results as XXH3_64bits() */ 447 XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_withSeed(const void* data, size_t len, XXH64_hash_t seed); 448 449 450 /* streaming 64-bit */ 451 452 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11+ */ 453 # include <stdalign.h> 454 # define XXH_ALIGN(n) alignas(n) 455 #elif defined(__GNUC__) 456 # define XXH_ALIGN(n) __attribute__ ((aligned(n))) 457 #elif defined(_MSC_VER) 458 # define XXH_ALIGN(n) __declspec(align(n)) 459 #else 460 # define XXH_ALIGN(n) /* disabled */ 461 #endif 462 463 typedef struct XXH3_state_s XXH3_state_t; 464 465 #define XXH3_SECRET_DEFAULT_SIZE 192 /* minimum XXH3_SECRET_SIZE_MIN */ 466 #define XXH3_INTERNALBUFFER_SIZE 256 467 struct XXH3_state_s { 468 XXH_ALIGN(64) XXH64_hash_t acc[8]; 469 XXH_ALIGN(64) unsigned char customSecret[XXH3_SECRET_DEFAULT_SIZE]; /* used to store a custom secret generated from the seed. Makes state larger. Design might change */ 470 XXH_ALIGN(64) unsigned char buffer[XXH3_INTERNALBUFFER_SIZE]; 471 XXH32_hash_t bufferedSize; 472 XXH32_hash_t nbStripesPerBlock; 473 XXH32_hash_t nbStripesSoFar; 474 XXH32_hash_t secretLimit; 475 XXH32_hash_t reserved32; 476 XXH32_hash_t reserved32_2; 477 XXH64_hash_t totalLen; 478 XXH64_hash_t seed; 479 XXH64_hash_t reserved64; 480 const unsigned char* secret; /* note : there is some padding after, due to alignment on 64 bytes */ 481 }; /* typedef'd to XXH3_state_t */ 482 483 /* Streaming requires state maintenance. 484 * This operation costs memory and cpu. 485 * As a consequence, streaming is slower than one-shot hashing. 486 * For better performance, prefer using one-shot functions whenever possible. */ 487 488 XXH_PUBLIC_API XXH3_state_t* XXH3_createState(void); 489 XXH_PUBLIC_API XXH_errorcode XXH3_freeState(XXH3_state_t* statePtr); 490 XXH_PUBLIC_API void XXH3_copyState(XXH3_state_t* dst_state, const XXH3_state_t* src_state); 491 492 493 /* XXH3_64bits_reset() : 494 * initialize with default parameters. 495 * result will be equivalent to `XXH3_64bits()`. */ 496 XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset(XXH3_state_t* statePtr); 497 /* XXH3_64bits_reset_withSeed() : 498 * generate a custom secret from `seed`, and store it into state. 499 * digest will be equivalent to `XXH3_64bits_withSeed()`. */ 500 XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed); 501 /* XXH3_64bits_reset_withSecret() : 502 * `secret` is referenced, and must outlive the hash streaming session. 503 * secretSize must be >= XXH3_SECRET_SIZE_MIN. 504 */ 505 XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize); 506 507 XXH_PUBLIC_API XXH_errorcode XXH3_64bits_update (XXH3_state_t* statePtr, const void* input, size_t length); 508 XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_digest (const XXH3_state_t* statePtr); 509 510 511 /* 128-bit */ 512 513 #ifdef XXH_NAMESPACE 514 # define XXH128 XXH_NAME2(XXH_NAMESPACE, XXH128) 515 # define XXH3_128bits XXH_NAME2(XXH_NAMESPACE, XXH3_128bits) 516 # define XXH3_128bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSeed) 517 # define XXH3_128bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecret) 518 519 # define XXH3_128bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset) 520 # define XXH3_128bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSeed) 521 # define XXH3_128bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecret) 522 # define XXH3_128bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_update) 523 # define XXH3_128bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_digest) 524 525 # define XXH128_isEqual XXH_NAME2(XXH_NAMESPACE, XXH128_isEqual) 526 # define XXH128_cmp XXH_NAME2(XXH_NAMESPACE, XXH128_cmp) 527 # define XXH128_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH128_canonicalFromHash) 528 # define XXH128_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH128_hashFromCanonical) 529 #endif 530 531 typedef struct { 532 XXH64_hash_t low64; 533 XXH64_hash_t high64; 534 } XXH128_hash_t; 535 536 XXH_PUBLIC_API XXH128_hash_t XXH128(const void* data, size_t len, XXH64_hash_t seed); 537 XXH_PUBLIC_API XXH128_hash_t XXH3_128bits(const void* data, size_t len); 538 XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_withSeed(const void* data, size_t len, XXH64_hash_t seed); /* == XXH128() */ 539 XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize); 540 541 XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset(XXH3_state_t* statePtr); 542 XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed); 543 XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize); 544 545 XXH_PUBLIC_API XXH_errorcode XXH3_128bits_update (XXH3_state_t* statePtr, const void* input, size_t length); 546 XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_digest (const XXH3_state_t* statePtr); 547 548 549 /* Note : for better performance, following functions can be inlined, 550 * using XXH_INLINE_ALL */ 551 552 /* return : 1 is equal, 0 if different */ 553 XXH_PUBLIC_API int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2); 554 555 /* This comparator is compatible with stdlib's qsort(). 556 * return : >0 if *h128_1 > *h128_2 557 * <0 if *h128_1 < *h128_2 558 * =0 if *h128_1 == *h128_2 */ 559 XXH_PUBLIC_API int XXH128_cmp(const void* h128_1, const void* h128_2); 560 561 562 /*====== Canonical representation ======*/ 563 typedef struct { unsigned char digest[16]; } XXH128_canonical_t; 564 XXH_PUBLIC_API void XXH128_canonicalFromHash(XXH128_canonical_t* dst, XXH128_hash_t hash); 565 XXH_PUBLIC_API XXH128_hash_t XXH128_hashFromCanonical(const XXH128_canonical_t* src); 566 567 568 #endif /* XXH_NO_LONG_LONG */ 569 570 571 /*-********************************************************************** 572 * XXH_INLINE_ALL 573 ************************************************************************/ 574 #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) 575 # include "xxhash.c" /* include xxhash function bodies as `static`, for inlining */ 576 #endif 577 578 579 580 #endif /* XXH_STATIC_LINKING_ONLY */ 581 582 583 #if defined (__cplusplus) 584 } 585 #endif 586 587 #endif /* XXHASH_H_5627135585666179 */ 588