xxhash 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -1
- data/CHANGELOG.md +9 -2
- data/README.md +7 -3
- data/ext/xxhash/libxxhash.c +564 -601
- data/ext/xxhash/libxxhash.h +206 -61
- data/ext/xxhash/xxhash.c +73 -26
- data/ext/xxhash/xxhash.h +15 -2
- data/lib/xxhash.rb +66 -6
- data/lib/xxhash/version.rb +1 -1
- data/test/xxhash_test.rb +78 -2
- metadata +3 -3
data/ext/xxhash/libxxhash.h
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
/*
|
2
2
|
xxHash - Extremely Fast Hash algorithm
|
3
3
|
Header File
|
4
|
-
Copyright (C) 2012-
|
4
|
+
Copyright (C) 2012-2016, Yann Collet.
|
5
|
+
|
5
6
|
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
6
7
|
|
7
8
|
Redistribution and use in source and binary forms, with or without
|
@@ -28,7 +29,7 @@
|
|
28
29
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
30
|
|
30
31
|
You can contact the author at :
|
31
|
-
- xxHash source repository :
|
32
|
+
- xxHash source repository : https://github.com/Cyan4973/xxHash
|
32
33
|
*/
|
33
34
|
|
34
35
|
/* Notice extracted from xxHash homepage :
|
@@ -55,102 +56,246 @@ SHA1-32 0.28 GB/s 10
|
|
55
56
|
Q.Score is a measure of quality of the hash function.
|
56
57
|
It depends on successfully passing SMHasher test set.
|
57
58
|
10 is a perfect score.
|
59
|
+
|
60
|
+
A 64-bits version, named XXH64, is available since r35.
|
61
|
+
It offers much better speed, but for 64-bits applications only.
|
62
|
+
Name Speed on 64 bits Speed on 32 bits
|
63
|
+
XXH64 13.8 GB/s 1.9 GB/s
|
64
|
+
XXH32 6.8 GB/s 6.0 GB/s
|
58
65
|
*/
|
59
66
|
|
60
|
-
#
|
67
|
+
#ifndef XXHASH_H_5627135585666179
|
68
|
+
#define XXHASH_H_5627135585666179 1
|
61
69
|
|
62
70
|
#if defined (__cplusplus)
|
63
71
|
extern "C" {
|
64
72
|
#endif
|
65
73
|
|
66
74
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
#
|
75
|
+
/* ****************************
|
76
|
+
* Compiler specifics
|
77
|
+
******************************/
|
78
|
+
#if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* ! C99 */
|
79
|
+
# define restrict /* disable restrict */
|
80
|
+
#endif
|
71
81
|
|
72
82
|
|
73
|
-
|
74
|
-
|
75
|
-
|
83
|
+
/* ****************************
|
84
|
+
* Definitions
|
85
|
+
******************************/
|
86
|
+
#include <stddef.h> /* size_t */
|
76
87
|
typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
|
77
88
|
|
78
89
|
|
90
|
+
/* ****************************
|
91
|
+
* API modifier
|
92
|
+
******************************/
|
93
|
+
/** XXH_PRIVATE_API
|
94
|
+
* This is useful to include xxhash functions in `static` mode
|
95
|
+
* in order to inline them, and remove their symbol from the public list.
|
96
|
+
* Methodology :
|
97
|
+
* #define XXH_PRIVATE_API
|
98
|
+
* #include "xxhash.h"
|
99
|
+
* `xxhash.c` is automatically included.
|
100
|
+
* It's not useful to compile and link it as a separate module.
|
101
|
+
*/
|
102
|
+
#ifdef XXH_PRIVATE_API
|
103
|
+
# ifndef XXH_STATIC_LINKING_ONLY
|
104
|
+
# define XXH_STATIC_LINKING_ONLY
|
105
|
+
# endif
|
106
|
+
# if defined(__GNUC__)
|
107
|
+
# define XXH_PUBLIC_API static __inline __attribute__((unused))
|
108
|
+
# elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
|
109
|
+
# define XXH_PUBLIC_API static inline
|
110
|
+
# elif defined(_MSC_VER)
|
111
|
+
# define XXH_PUBLIC_API static __inline
|
112
|
+
# else
|
113
|
+
# define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */
|
114
|
+
# endif
|
115
|
+
#else
|
116
|
+
# define XXH_PUBLIC_API /* do nothing */
|
117
|
+
#endif /* XXH_PRIVATE_API */
|
118
|
+
|
119
|
+
/*!XXH_NAMESPACE, aka Namespace Emulation :
|
120
|
+
|
121
|
+
If you want to include _and expose_ xxHash functions from within your own library,
|
122
|
+
but also want to avoid symbol collisions with other libraries which may also include xxHash,
|
123
|
+
|
124
|
+
you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library
|
125
|
+
with the value of XXH_NAMESPACE (therefore, avoid NULL and numeric values).
|
126
|
+
|
127
|
+
Note that no change is required within the calling program as long as it includes `xxhash.h` :
|
128
|
+
regular symbol name will be automatically translated by this header.
|
129
|
+
*/
|
130
|
+
#ifdef XXH_NAMESPACE
|
131
|
+
# define XXH_CAT(A,B) A##B
|
132
|
+
# define XXH_NAME2(A,B) XXH_CAT(A,B)
|
133
|
+
# define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
|
134
|
+
# define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
|
135
|
+
# define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
|
136
|
+
# define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
|
137
|
+
# define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
|
138
|
+
# define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
|
139
|
+
# define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
|
140
|
+
# define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
|
141
|
+
# define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
|
142
|
+
# define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
|
143
|
+
# define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
|
144
|
+
# define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
|
145
|
+
# define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
|
146
|
+
# define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
|
147
|
+
# define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
|
148
|
+
# define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
|
149
|
+
# define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
|
150
|
+
# define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
|
151
|
+
# define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
|
152
|
+
#endif
|
79
153
|
|
80
|
-
/*****************************
|
81
|
-
Simple Hash Functions
|
82
|
-
*****************************/
|
83
154
|
|
84
|
-
|
85
|
-
|
155
|
+
/* *************************************
|
156
|
+
* Version
|
157
|
+
***************************************/
|
158
|
+
#define XXH_VERSION_MAJOR 0
|
159
|
+
#define XXH_VERSION_MINOR 6
|
160
|
+
#define XXH_VERSION_RELEASE 2
|
161
|
+
#define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
|
162
|
+
XXH_PUBLIC_API unsigned XXH_versionNumber (void);
|
86
163
|
|
87
|
-
|
88
|
-
|
164
|
+
|
165
|
+
/*-**********************************************************************
|
166
|
+
* 32-bits hash
|
167
|
+
************************************************************************/
|
168
|
+
typedef unsigned int XXH32_hash_t;
|
169
|
+
|
170
|
+
/*! XXH32() :
|
89
171
|
Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input".
|
90
172
|
The memory between input & input+length must be valid (allocated and read-accessible).
|
91
173
|
"seed" can be used to alter the result predictably.
|
92
|
-
|
93
|
-
|
94
|
-
XXH64() :
|
95
|
-
Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".
|
96
|
-
*/
|
97
|
-
|
174
|
+
Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s */
|
175
|
+
XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);
|
98
176
|
|
177
|
+
/*====== Streaming ======*/
|
178
|
+
typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */
|
179
|
+
XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);
|
180
|
+
XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
|
181
|
+
XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dst_state, const XXH32_state_t* restrict src_state);
|
99
182
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
typedef struct { long long ll[ 6]; } XXH32_state_t;
|
104
|
-
typedef struct { long long ll[11]; } XXH64_state_t;
|
183
|
+
XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned int seed);
|
184
|
+
XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
|
185
|
+
XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
|
105
186
|
|
106
187
|
/*
|
107
|
-
These
|
108
|
-
|
109
|
-
|
110
|
-
If you prefer dynamic allocation, please refer to functions below.
|
111
|
-
*/
|
188
|
+
These functions generate the xxHash of an input provided in multiple segments.
|
189
|
+
Note that, for small input, they are slower than single-call functions, due to state management.
|
190
|
+
For small input, prefer `XXH32()` and `XXH64()` .
|
112
191
|
|
113
|
-
|
114
|
-
XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
|
192
|
+
XXH state must first be allocated, using XXH*_createState() .
|
115
193
|
|
116
|
-
|
117
|
-
XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
|
194
|
+
Start a new hash by initializing state with a seed, using XXH*_reset().
|
118
195
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
*/
|
196
|
+
Then, feed the hash state by calling XXH*_update() as many times as necessary.
|
197
|
+
Obviously, input must be allocated and read accessible.
|
198
|
+
The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
|
123
199
|
|
200
|
+
Finally, a hash value can be produced anytime, by using XXH*_digest().
|
201
|
+
This function returns the nn-bits hash as an int or long long.
|
124
202
|
|
125
|
-
|
126
|
-
|
127
|
-
unsigned int XXH32_digest (const XXH32_state_t* statePtr);
|
203
|
+
It's still possible to continue inserting input into the hash state after a digest,
|
204
|
+
and generate some new hashes later on, by calling again XXH*_digest().
|
128
205
|
|
129
|
-
|
130
|
-
|
131
|
-
unsigned long long XXH64_digest (const XXH64_state_t* statePtr);
|
206
|
+
When done, free XXH state space if it was allocated dynamically.
|
207
|
+
*/
|
132
208
|
|
133
|
-
|
134
|
-
These functions calculate the xxHash of an input provided in multiple smaller packets,
|
135
|
-
as opposed to an input provided as a single block.
|
209
|
+
/*====== Canonical representation ======*/
|
136
210
|
|
137
|
-
|
211
|
+
typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
|
212
|
+
XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
|
213
|
+
XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
|
138
214
|
|
139
|
-
|
215
|
+
/* Default result type for XXH functions are primitive unsigned 32 and 64 bits.
|
216
|
+
* The canonical representation uses human-readable write convention, aka big-endian (large digits first).
|
217
|
+
* These functions allow transformation of hash result into and from its canonical format.
|
218
|
+
* This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
|
219
|
+
*/
|
140
220
|
|
141
|
-
Then, feed the hash state by calling XXHnn_update() as many times as necessary.
|
142
|
-
Obviously, input must be valid, meaning allocated and read accessible.
|
143
|
-
The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
|
144
221
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
222
|
+
#ifndef XXH_NO_LONG_LONG
|
223
|
+
/*-**********************************************************************
|
224
|
+
* 64-bits hash
|
225
|
+
************************************************************************/
|
226
|
+
typedef unsigned long long XXH64_hash_t;
|
149
227
|
|
150
|
-
|
228
|
+
/*! XXH64() :
|
229
|
+
Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".
|
230
|
+
"seed" can be used to alter the result predictably.
|
231
|
+
This function runs faster on 64-bits systems, but slower on 32-bits systems (see benchmark).
|
151
232
|
*/
|
233
|
+
XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);
|
234
|
+
|
235
|
+
/*====== Streaming ======*/
|
236
|
+
typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
|
237
|
+
XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);
|
238
|
+
XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
|
239
|
+
XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dst_state, const XXH64_state_t* restrict src_state);
|
240
|
+
|
241
|
+
XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed);
|
242
|
+
XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
|
243
|
+
XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);
|
244
|
+
|
245
|
+
/*====== Canonical representation ======*/
|
246
|
+
typedef struct { unsigned char digest[8]; } XXH64_canonical_t;
|
247
|
+
XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);
|
248
|
+
XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);
|
249
|
+
#endif /* XXH_NO_LONG_LONG */
|
250
|
+
|
251
|
+
|
252
|
+
#ifdef XXH_STATIC_LINKING_ONLY
|
253
|
+
|
254
|
+
/* ================================================================================================
|
255
|
+
This section contains definitions which are not guaranteed to remain stable.
|
256
|
+
They may change in future versions, becoming incompatible with a different version of the library.
|
257
|
+
They shall only be used with static linking.
|
258
|
+
Never use these definitions in association with dynamic linking !
|
259
|
+
=================================================================================================== */
|
260
|
+
|
261
|
+
/* These definitions are only meant to allow allocation of XXH state
|
262
|
+
statically, on stack, or in a struct for example.
|
263
|
+
Do not use members directly. */
|
264
|
+
|
265
|
+
struct XXH32_state_s {
|
266
|
+
unsigned total_len_32;
|
267
|
+
unsigned large_len;
|
268
|
+
unsigned v1;
|
269
|
+
unsigned v2;
|
270
|
+
unsigned v3;
|
271
|
+
unsigned v4;
|
272
|
+
unsigned mem32[4]; /* buffer defined as U32 for alignment */
|
273
|
+
unsigned memsize;
|
274
|
+
unsigned reserved; /* never read nor write, will be removed in a future version */
|
275
|
+
}; /* typedef'd to XXH32_state_t */
|
276
|
+
|
277
|
+
#ifndef XXH_NO_LONG_LONG
|
278
|
+
struct XXH64_state_s {
|
279
|
+
unsigned long long total_len;
|
280
|
+
unsigned long long v1;
|
281
|
+
unsigned long long v2;
|
282
|
+
unsigned long long v3;
|
283
|
+
unsigned long long v4;
|
284
|
+
unsigned long long mem64[4]; /* buffer defined as U64 for alignment */
|
285
|
+
unsigned memsize;
|
286
|
+
unsigned reserved[2]; /* never read nor write, will be removed in a future version */
|
287
|
+
}; /* typedef'd to XXH64_state_t */
|
288
|
+
#endif
|
289
|
+
|
290
|
+
# ifdef XXH_PRIVATE_API
|
291
|
+
# include "xxhash.c" /* include xxhash function bodies as `static`, for inlining */
|
292
|
+
# endif
|
293
|
+
|
294
|
+
#endif /* XXH_STATIC_LINKING_ONLY */
|
152
295
|
|
153
296
|
|
154
297
|
#if defined (__cplusplus)
|
155
298
|
}
|
156
|
-
#endif
|
299
|
+
#endif
|
300
|
+
|
301
|
+
#endif /* XXHASH_H_5627135585666179 */
|
data/ext/xxhash/xxhash.c
CHANGED
@@ -5,29 +5,49 @@ VALUE xxhash_xxh32(VALUE mod, VALUE input, VALUE seed)
|
|
5
5
|
return ULL2NUM(XXH32(StringValuePtr(input), (size_t)RSTRING_LEN(input), (unsigned int)NUM2ULL(seed)));
|
6
6
|
}
|
7
7
|
|
8
|
-
void xxhash32_streaming_hash_free(
|
8
|
+
void xxhash32_streaming_hash_free(xxhash_xxh32_t* storage)
|
9
9
|
{
|
10
10
|
// Digest frees the memory.
|
11
|
-
XXH32_freeState(state);
|
11
|
+
XXH32_freeState(storage->state);
|
12
|
+
xfree(storage);
|
12
13
|
}
|
13
14
|
|
14
15
|
VALUE xxhash32_streaming_hash_new(VALUE klass, VALUE seed)
|
15
16
|
{
|
16
|
-
|
17
|
-
|
17
|
+
XXH_errorcode code;
|
18
|
+
xxhash_xxh32_t* storage;
|
19
|
+
storage = ALLOC(xxhash_xxh32_t);
|
20
|
+
storage->state = XXH32_createState();
|
21
|
+
storage->seed = (unsigned int)NUM2ULL(seed);
|
22
|
+
code = XXH32_reset(storage->state, storage->seed);
|
18
23
|
if(code != XXH_OK) {
|
19
24
|
rb_raise(rb_eRuntimeError, "Error during reset.");
|
20
25
|
return Qnil;
|
21
26
|
}
|
22
|
-
return Data_Wrap_Struct(klass, 0, xxhash32_streaming_hash_free,
|
27
|
+
return Data_Wrap_Struct(klass, 0, xxhash32_streaming_hash_free, storage);
|
23
28
|
}
|
24
29
|
|
30
|
+
VALUE xxhash32_streaming_hash_reset(VALUE self)
|
31
|
+
{
|
32
|
+
XXH_errorcode code;
|
33
|
+
xxhash_xxh32_t* storage;
|
34
|
+
Data_Get_Struct(self, xxhash_xxh32_t, storage);
|
35
|
+
|
36
|
+
code = XXH32_reset(storage->state, storage->seed);
|
37
|
+
if(code != XXH_OK) {
|
38
|
+
rb_raise(rb_eRuntimeError, "Error during reset.");
|
39
|
+
}
|
40
|
+
return Qnil;
|
41
|
+
}
|
42
|
+
|
43
|
+
|
25
44
|
VALUE xxhash32_streaming_hash_update(VALUE self, VALUE data)
|
26
45
|
{
|
27
|
-
|
28
|
-
|
46
|
+
XXH_errorcode code;
|
47
|
+
xxhash_xxh32_t* storage;
|
48
|
+
Data_Get_Struct(self, xxhash_xxh32_t, storage);
|
29
49
|
|
30
|
-
|
50
|
+
code = XXH32_update(storage->state, StringValuePtr(data), (size_t)RSTRING_LEN(data));
|
31
51
|
if(code != XXH_OK) {
|
32
52
|
rb_raise(rb_eRuntimeError, "Error during update.");
|
33
53
|
}
|
@@ -36,11 +56,11 @@ VALUE xxhash32_streaming_hash_update(VALUE self, VALUE data)
|
|
36
56
|
|
37
57
|
VALUE xxhash32_streaming_hash_digest(VALUE self)
|
38
58
|
{
|
39
|
-
|
40
|
-
Data_Get_Struct(self,
|
59
|
+
xxhash_xxh32_t* storage;
|
60
|
+
Data_Get_Struct(self, xxhash_xxh32_t, storage);
|
41
61
|
|
42
62
|
// Do not free memory now.
|
43
|
-
return ULL2NUM(XXH32_digest(state));
|
63
|
+
return ULL2NUM(XXH32_digest(storage->state));
|
44
64
|
}
|
45
65
|
|
46
66
|
VALUE xxhash_xxh64(VALUE mod, VALUE input, VALUE seed)
|
@@ -48,29 +68,49 @@ VALUE xxhash_xxh64(VALUE mod, VALUE input, VALUE seed)
|
|
48
68
|
return ULL2NUM(XXH64(StringValuePtr(input), (size_t)RSTRING_LEN(input), (unsigned int)NUM2ULL(seed)));
|
49
69
|
}
|
50
70
|
|
51
|
-
void xxhash64_streaming_hash_free(
|
71
|
+
void xxhash64_streaming_hash_free(xxhash_xxh64_t* storage)
|
52
72
|
{
|
53
73
|
// Digest frees the memory.
|
54
|
-
XXH64_freeState(state);
|
74
|
+
XXH64_freeState(storage->state);
|
75
|
+
xfree(storage);
|
55
76
|
}
|
56
77
|
|
57
78
|
VALUE xxhash64_streaming_hash_new(VALUE klass, VALUE seed)
|
58
79
|
{
|
59
|
-
|
60
|
-
|
80
|
+
XXH_errorcode code;
|
81
|
+
xxhash_xxh64_t* storage;
|
82
|
+
storage = ALLOC(xxhash_xxh64_t);
|
83
|
+
storage->state = XXH64_createState();
|
84
|
+
storage->seed = (unsigned int)NUM2ULL(seed);
|
85
|
+
|
86
|
+
code = XXH64_reset(storage->state, storage->seed);
|
61
87
|
if(code != XXH_OK) {
|
62
88
|
rb_raise(rb_eRuntimeError, "Error during reset.");
|
63
89
|
return Qnil;
|
64
90
|
}
|
65
|
-
return Data_Wrap_Struct(klass, 0, xxhash64_streaming_hash_free,
|
91
|
+
return Data_Wrap_Struct(klass, 0, xxhash64_streaming_hash_free, storage);
|
92
|
+
}
|
93
|
+
|
94
|
+
VALUE xxhash64_streaming_hash_reset(VALUE self)
|
95
|
+
{
|
96
|
+
XXH_errorcode code;
|
97
|
+
xxhash_xxh64_t* storage;
|
98
|
+
Data_Get_Struct(self, xxhash_xxh64_t, storage);
|
99
|
+
|
100
|
+
code = XXH64_reset(storage->state, storage->seed);
|
101
|
+
if(code != XXH_OK) {
|
102
|
+
rb_raise(rb_eRuntimeError, "Error during reset.");
|
103
|
+
}
|
104
|
+
return Qnil;
|
66
105
|
}
|
67
106
|
|
68
107
|
VALUE xxhash64_streaming_hash_update(VALUE self, VALUE data)
|
69
108
|
{
|
70
|
-
|
71
|
-
|
109
|
+
XXH_errorcode code;
|
110
|
+
xxhash_xxh64_t* storage;
|
111
|
+
Data_Get_Struct(self, xxhash_xxh64_t, storage);
|
72
112
|
|
73
|
-
|
113
|
+
code = XXH64_update(storage->state, StringValuePtr(data), (size_t)RSTRING_LEN(data));
|
74
114
|
if(code != XXH_OK) {
|
75
115
|
rb_raise(rb_eRuntimeError, "Error during update.");
|
76
116
|
}
|
@@ -79,31 +119,38 @@ VALUE xxhash64_streaming_hash_update(VALUE self, VALUE data)
|
|
79
119
|
|
80
120
|
VALUE xxhash64_streaming_hash_digest(VALUE self)
|
81
121
|
{
|
82
|
-
|
83
|
-
Data_Get_Struct(self,
|
122
|
+
xxhash_xxh64_t* storage;
|
123
|
+
Data_Get_Struct(self, xxhash_xxh64_t, storage);
|
84
124
|
|
85
125
|
// Do not free memory now.
|
86
|
-
return ULL2NUM(XXH64_digest(state));
|
126
|
+
return ULL2NUM(XXH64_digest(storage->state));
|
87
127
|
}
|
88
128
|
|
89
129
|
|
90
130
|
void Init_xxhash(void)
|
91
131
|
{
|
92
|
-
VALUE
|
93
|
-
VALUE
|
132
|
+
VALUE cStreamingHash;
|
133
|
+
VALUE cStreamingHash64;
|
134
|
+
VALUE mXXhash;
|
135
|
+
VALUE mInternal;
|
136
|
+
|
137
|
+
mXXhash = rb_define_module("XXhash");
|
138
|
+
mInternal = rb_define_module_under(mXXhash, "XXhashInternal");
|
94
139
|
|
95
140
|
rb_define_singleton_method(mInternal, "xxh32", (ruby_method*) &xxhash_xxh32, 2);
|
96
141
|
rb_define_singleton_method(mInternal, "xxh64", (ruby_method*) &xxhash_xxh64, 2);
|
97
142
|
|
98
|
-
|
143
|
+
cStreamingHash = rb_define_class_under(mInternal, "StreamingHash32", rb_cObject);
|
99
144
|
|
100
145
|
rb_define_singleton_method(cStreamingHash, "new", (ruby_method*) &xxhash32_streaming_hash_new, 1);
|
101
146
|
rb_define_method(cStreamingHash, "update", (ruby_method*) &xxhash32_streaming_hash_update, 1);
|
102
147
|
rb_define_method(cStreamingHash, "digest", (ruby_method*) &xxhash32_streaming_hash_digest, 0);
|
148
|
+
rb_define_method(cStreamingHash, "reset", (ruby_method*) &xxhash32_streaming_hash_reset, 0);
|
103
149
|
|
104
|
-
|
150
|
+
cStreamingHash64 = rb_define_class_under(mInternal, "StreamingHash64", rb_cObject);
|
105
151
|
|
106
152
|
rb_define_singleton_method(cStreamingHash64, "new", (ruby_method*) &xxhash64_streaming_hash_new, 1);
|
107
153
|
rb_define_method(cStreamingHash64, "update", (ruby_method*) &xxhash64_streaming_hash_update, 1);
|
108
154
|
rb_define_method(cStreamingHash64, "digest", (ruby_method*) &xxhash64_streaming_hash_digest, 0);
|
155
|
+
rb_define_method(cStreamingHash64, "reset", (ruby_method*) &xxhash64_streaming_hash_reset, 0);
|
109
156
|
}
|