Rizin
unix-like reverse engineering framework and cli tools
xxhash.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2012-2016, Yann Collet
2 // SPDX-License-Identifier: BSD-2-Clause
3 
4 /*
5  xxHash - Extremely Fast Hash algorithm
6  Header File
7  Copyright (C) 2012-2016, Yann Collet.
8 
9  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  * Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17  * Redistributions in binary form must reproduce the above
18  copyright notice, this list of conditions and the following disclaimer
19  in the documentation and/or other materials provided with the
20  distribution.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34  You can contact the author at :
35  - xxHash source repository : https://github.com/Cyan4973/xxHash
36 */
37 
38 /* Notice extracted from xxHash homepage :
39 
40 xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
41 It also successfully passes all tests from the SMHasher suite.
42 
43 Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
44 
45 Name Speed Q.Score Author
46 xxHash 5.4 GB/s 10
47 CrapWow 3.2 GB/s 2 Andrew
48 MumurHash 3a 2.7 GB/s 10 Austin Appleby
49 SpookyHash 2.0 GB/s 10 Bob Jenkins
50 SBox 1.4 GB/s 9 Bret Mulvey
51 Lookup3 1.2 GB/s 9 Bob Jenkins
52 SuperFastHash 1.2 GB/s 1 Paul Hsieh
53 CityHash64 1.05 GB/s 10 Pike & Alakuijala
54 FNV 0.55 GB/s 5 Fowler, Noll, Vo
55 CRC32 0.43 GB/s 9
56 MD5-32 0.33 GB/s 10 Ronald L. Rivest
57 SHA1-32 0.28 GB/s 10
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  ******************************/
98 #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
99 # ifndef XXH_STATIC_LINKING_ONLY
100 # define XXH_STATIC_LINKING_ONLY
101 # endif
102 # if defined(__GNUC__)
103 # define XXH_PUBLIC_API static __inline __attribute__((unused))
104 # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
105 # define XXH_PUBLIC_API static inline
106 # elif defined(_MSC_VER)
107 # define XXH_PUBLIC_API static __inline
108 # else
109  /* this version may generate warnings for unused static functions */
110 # define XXH_PUBLIC_API static
111 # endif
112 #else
113 # define XXH_PUBLIC_API /* do nothing */
114 #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */
115 
127 #ifdef XXH_NAMESPACE
128 # define XXH_CAT(A,B) A##B
129 # define XXH_NAME2(A,B) XXH_CAT(A,B)
130 # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
131 # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
132 # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
133 # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
134 # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
135 # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
136 # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
137 # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
138 # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
139 # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
140 # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
141 # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
142 # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
143 # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
144 # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
145 # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
146 # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
147 # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
148 # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
149 #endif
150 
151 
152 /* *************************************
153 * Version
154 ***************************************/
155 #define XXH_VERSION_MAJOR 0
156 #define XXH_VERSION_MINOR 6
157 #define XXH_VERSION_RELEASE 5
158 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
159 XXH_PUBLIC_API unsigned XXH_versionNumber (void);
160 
161 
162 /*-**********************************************************************
163 * 32-bit hash
164 ************************************************************************/
165 typedef unsigned int XXH32_hash_t;
166 
172 XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);
173 
174 /*====== Streaming ======*/
175 typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */
178 XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state);
179 
180 XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned int seed);
181 XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
183 
184 /*
185  * Streaming functions generate the xxHash of an input provided in multiple segments.
186  * Note that, for small input, they are slower than single-call functions, due to state management.
187  * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized.
188  *
189  * XXH state must first be allocated, using XXH*_createState() .
190  *
191  * Start a new hash by initializing state with a seed, using XXH*_reset().
192  *
193  * Then, feed the hash state by calling XXH*_update() as many times as necessary.
194  * The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
195  *
196  * Finally, a hash value can be produced anytime, by using XXH*_digest().
197  * This function returns the nn-bits hash as an int or long long.
198  *
199  * It's still possible to continue inserting input into the hash state after a digest,
200  * and generate some new hashes later on, by calling again XXH*_digest().
201  *
202  * When done, free XXH state space if it was allocated dynamically.
203  */
204 
205 /*====== Canonical representation ======*/
206 
207 typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
210 
211 /* Default result type for XXH functions are primitive unsigned 32 and 64 bits.
212  * The canonical representation uses human-readable write convention, aka big-endian (large digits first).
213  * These functions allow transformation of hash result into and from its canonical format.
214  * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
215  */
216 
217 
218 #ifndef XXH_NO_LONG_LONG
219 /*-**********************************************************************
220 * 64-bit hash
221 ************************************************************************/
222 typedef unsigned long long XXH64_hash_t;
223 
229 XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);
230 
231 /*====== Streaming ======*/
232 typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
235 XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dst_state, const XXH64_state_t* src_state);
236 
237 XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed);
238 XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
240 
241 /*====== Canonical representation ======*/
242 typedef struct { unsigned char digest[8]; } XXH64_canonical_t;
245 #endif /* XXH_NO_LONG_LONG */
246 
247 
248 
249 #ifdef XXH_STATIC_LINKING_ONLY
250 
251 /* ================================================================================================
252  This section contains declarations which are not guaranteed to remain stable.
253  They may change in future versions, becoming incompatible with a different version of the library.
254  These declarations should only be used with static linking.
255  Never use them in association with dynamic linking !
256 =================================================================================================== */
257 
258 /* These definitions are only present to allow
259  * static allocation of XXH state, on stack or in a struct for example.
260  * Never **ever** use members directly. */
261 
262 #if !defined (__VMS) \
263  && (defined (__cplusplus) \
264  || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
265 # include <stdint.h>
266 
267 struct XXH32_state_s {
268  uint32_t total_len_32;
269  uint32_t large_len;
270  uint32_t v1;
271  uint32_t v2;
272  uint32_t v3;
273  uint32_t v4;
274  uint32_t mem32[4];
275  uint32_t memsize;
276  uint32_t reserved; /* never read nor write, might be removed in a future version */
277 }; /* typedef'd to XXH32_state_t */
278 
279 struct XXH64_state_s {
280  uint64_t total_len;
281  uint64_t v1;
282  uint64_t v2;
283  uint64_t v3;
284  uint64_t v4;
285  uint64_t mem64[4];
286  uint32_t memsize;
287  uint32_t reserved[2]; /* never read nor write, might be removed in a future version */
288 }; /* typedef'd to XXH64_state_t */
289 
290 # else
291 
292 struct XXH32_state_s {
293  unsigned total_len_32;
294  unsigned large_len;
295  unsigned v1;
296  unsigned v2;
297  unsigned v3;
298  unsigned v4;
299  unsigned mem32[4];
300  unsigned memsize;
301  unsigned reserved; /* never read nor write, might be removed in a future version */
302 }; /* typedef'd to XXH32_state_t */
303 
304 # ifndef XXH_NO_LONG_LONG /* remove 64-bit support */
305 struct XXH64_state_s {
306  unsigned long long total_len;
307  unsigned long long v1;
308  unsigned long long v2;
309  unsigned long long v3;
310  unsigned long long v4;
311  unsigned long long mem64[4];
312  unsigned memsize;
313  unsigned reserved[2]; /* never read nor write, might be removed in a future version */
314 }; /* typedef'd to XXH64_state_t */
315 # endif
316 
317 # endif
318 
319 
320 #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
321 # include "xxhash.c" /* include xxhash function bodies as `static`, for inlining */
322 #endif
323 
324 #endif /* XXH_STATIC_LINKING_ONLY */
325 
326 
327 #if defined (__cplusplus)
328 }
329 #endif
330 
331 #endif /* XXHASH_H_5627135585666179 */
lzma_index * src
Definition: index.h:567
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
@ v1
Definition: lanai.h:85
@ reserved
Definition: lm32_isa.h:94
struct XXH32_state_s XXH32_state_t
Definition: xxhash.h:172
struct XXH64_state_s XXH64_state_t
Definition: xxhash.h:229
XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t *src)
Definition: xxhash.c:572
XXH_PUBLIC_API XXH32_state_t * XXH32_createState(void)
Definition: xxhash.c:422
XXH_PUBLIC_API XXH32_hash_t XXH32_digest(const XXH32_state_t *statePtr)
Definition: xxhash.c:546
unsigned long long XXH64_hash_t
Definition: xxhash.h:219
XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t *dst_state, const XXH64_state_t *src_state)
Definition: xxhash.c:893
XXH_errorcode
Definition: xxhash.h:79
@ XXH_ERROR
Definition: xxhash.h:79
@ XXH_OK
Definition: xxhash.h:79
XXH_PUBLIC_API XXH_errorcode XXH64_update(XXH64_state_t *statePtr, const void *input, size_t length)
Definition: xxhash.c:971
XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t *statePtr, unsigned int seed)
Definition: xxhash.c:437
XXH_PUBLIC_API XXH64_hash_t XXH64(const void *input, size_t length, unsigned long long seed)
Definition: xxhash.c:855
XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t *statePtr, unsigned long long seed)
Definition: xxhash.c:898
XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t *dst_state, const XXH32_state_t *src_state)
Definition: xxhash.c:432
XXH_PUBLIC_API XXH64_state_t * XXH64_createState(void)
Definition: xxhash.c:883
XXH_PUBLIC_API XXH32_hash_t XXH32(const void *input, size_t length, unsigned int seed)
Definition: xxhash.c:392
XXH_PUBLIC_API XXH64_hash_t XXH64_digest(const XXH64_state_t *statePtr)
Definition: xxhash.c:1005
XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t *statePtr)
Definition: xxhash.c:426
XXH_PUBLIC_API XXH_errorcode XXH32_update(XXH32_state_t *statePtr, const void *input, size_t length)
Definition: xxhash.c:515
unsigned int XXH32_hash_t
Definition: xxhash.h:162
XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t *src)
Definition: xxhash.c:1025
XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t *dst, XXH32_hash_t hash)
Definition: xxhash.c:565
XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t *statePtr)
Definition: xxhash.c:887
XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t *dst, XXH64_hash_t hash)
Definition: xxhash.c:1018
XXH_PUBLIC_API unsigned XXH_versionNumber(void)
Definition: xxhash.c:257
char * dst
Definition: lz4.h:724
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
#define XXH_PUBLIC_API
Definition: xxhash.h:113
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)