digest-xxhash 0.0.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,424 @@
1
+ diff --git a/xxhsum.c b/xxhsum.c
2
+ index 8232409..3be0562 100644
3
+ --- a/xxhsum.c
4
+ +++ b/xxhsum.c
5
+ @@ -54,6 +54,18 @@
6
+ #include <assert.h> /* assert */
7
+ #include <errno.h> /* errno */
8
+
9
+ +typedef enum {
10
+ + XSUM_SEED = 0,
11
+ + XSUM_SECRET = 1
12
+ +} XSUM_seed_type_t;
13
+ +
14
+ +typedef struct {
15
+ + XSUM_seed_type_t type;
16
+ + XSUM_U64 seed;
17
+ + void *secret;
18
+ + size_t secret_length;
19
+ +} XSUM_seed_t;
20
+ +
21
+ #define XXH_STATIC_LINKING_ONLY /* *_state_t */
22
+ #include "xxhash.h"
23
+
24
+ @@ -93,7 +105,7 @@ static size_t XSUM_DEFAULT_SAMPLE_SIZE = 100 KB;
25
+ #define MAX_MEM (2 GB - 64 MB)
26
+
27
+ static const char stdinName[] = "-";
28
+ -typedef enum { algo_xxh32=0, algo_xxh64=1, algo_xxh128=2 } AlgoSelected;
29
+ +typedef enum { algo_xxh32=0, algo_xxh64=1, algo_xxh128=2, algo_xxh3=3 } AlgoSelected;
30
+ static AlgoSelected g_defaultAlgo = algo_xxh64; /* required within main() & XSUM_usage() */
31
+
32
+ /* <16 hex char> <SPC> <SPC> <filename> <'\0'>
33
+ @@ -103,7 +115,6 @@ static AlgoSelected g_defaultAlgo = algo_xxh64; /* required within main() & X
34
+ /* Maximum acceptable line length. */
35
+ #define MAX_LINE_LENGTH (32 KB)
36
+
37
+ -
38
+ /* ************************************
39
+ * Display macros
40
+ **************************************/
41
+ @@ -511,6 +522,7 @@ typedef union {
42
+ XXH32_hash_t xxh32;
43
+ XXH64_hash_t xxh64;
44
+ XXH128_hash_t xxh128;
45
+ + XXH64_hash_t xxh3;
46
+ } Multihash;
47
+
48
+ /*
49
+ @@ -521,16 +533,45 @@ typedef union {
50
+ static Multihash
51
+ XSUM_hashStream(FILE* inFile,
52
+ AlgoSelected hashType,
53
+ + XSUM_seed_t seed,
54
+ void* buffer, size_t blockSize)
55
+ {
56
+ XXH32_state_t state32;
57
+ XXH64_state_t state64;
58
+ XXH3_state_t state128;
59
+ + XXH3_state_t state3;
60
+ + XXH_errorcode error_code;
61
+
62
+ /* Init */
63
+ - (void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
64
+ - (void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
65
+ - (void)XXH3_128bits_reset(&state128);
66
+ + switch(hashType)
67
+ + {
68
+ + case algo_xxh32:
69
+ + error_code = XXH32_reset(&state32, (XSUM_U32) (seed.seed & 0xFFFFFFFF));
70
+ + break;
71
+ + case algo_xxh64:
72
+ + error_code = XXH64_reset(&state64, seed.seed);
73
+ + break;
74
+ + case algo_xxh128:
75
+ + if (seed.type == XSUM_SEED)
76
+ + error_code = XXH3_128bits_reset_withSeed(&state128, seed.seed);
77
+ + else
78
+ + error_code = XXH3_128bits_reset_withSecret(&state128, seed.secret, seed.secret_length);
79
+ + break;
80
+ + case algo_xxh3:
81
+ + if (seed.type == XSUM_SEED)
82
+ + error_code = XXH3_64bits_reset_withSeed(&state3, seed.seed);
83
+ + else
84
+ + error_code = XXH3_64bits_reset_withSecret(&state3, seed.secret, seed.secret_length);
85
+ + break;
86
+ + default:
87
+ + assert(0);
88
+ + }
89
+ +
90
+ + if (error_code != XXH_OK) {
91
+ + XSUM_log("Error: An error occurred while initializing a state.\n");
92
+ + XSUM_log("Algo: %d\n", hashType);
93
+ + exit(1);
94
+ + }
95
+
96
+ /* Load file & update hash */
97
+ { size_t readSize;
98
+ @@ -538,17 +579,24 @@ XSUM_hashStream(FILE* inFile,
99
+ switch(hashType)
100
+ {
101
+ case algo_xxh32:
102
+ - (void)XXH32_update(&state32, buffer, readSize);
103
+ + error_code = XXH32_update(&state32, buffer, readSize);
104
+ break;
105
+ case algo_xxh64:
106
+ - (void)XXH64_update(&state64, buffer, readSize);
107
+ + error_code = XXH64_update(&state64, buffer, readSize);
108
+ break;
109
+ case algo_xxh128:
110
+ - (void)XXH3_128bits_update(&state128, buffer, readSize);
111
+ + error_code = XXH3_128bits_update(&state128, buffer, readSize);
112
+ + break;
113
+ + case algo_xxh3:
114
+ + error_code = XXH3_64bits_update(&state3, buffer, readSize);
115
+ break;
116
+ default:
117
+ assert(0);
118
+ }
119
+ + if (error_code != XXH_OK) {
120
+ + XSUM_log("Error: An error occurred while updating digest data.\n");
121
+ + exit(1);
122
+ + }
123
+ }
124
+ if (ferror(inFile)) {
125
+ XSUM_log("Error: a failure occurred reading the input file.\n");
126
+ @@ -567,6 +615,9 @@ XSUM_hashStream(FILE* inFile,
127
+ case algo_xxh128:
128
+ finalHash.xxh128 = XXH3_128bits_digest(&state128);
129
+ break;
130
+ + case algo_xxh3:
131
+ + finalHash.xxh3 = XXH3_64bits_digest(&state3);
132
+ + break;
133
+ default:
134
+ assert(0);
135
+ }
136
+ @@ -575,9 +626,9 @@ XSUM_hashStream(FILE* inFile,
137
+ }
138
+
139
+ /* algo_xxh32, algo_xxh64, algo_xxh128 */
140
+ -static const char* XSUM_algoName[] = { "XXH32", "XXH64", "XXH128" };
141
+ -static const char* XSUM_algoLE_name[] = { "XXH32_LE", "XXH64_LE", "XXH128_LE" };
142
+ -static const size_t XSUM_algoLength[] = { 4, 8, 16 };
143
+ +static const char* XSUM_algoName[] = { "XXH32", "XXH64", "XXH128", "XXH3" };
144
+ +static const char* XSUM_algoLE_name[] = { "XXH32_LE", "XXH64_LE", "XXH128_LE", "XXH3_LE" };
145
+ +static const size_t XSUM_algoLength[] = { 4, 8, 16, 8 };
146
+
147
+ #define XSUM_TABLE_ELT_SIZE(table) (sizeof(table) / sizeof(*table))
148
+
149
+ @@ -641,6 +692,7 @@ static XSUM_displayLine_f XSUM_kDisplayLine_fTable[2][2] = {
150
+
151
+ static int XSUM_hashFile(const char* fileName,
152
+ const AlgoSelected hashType,
153
+ + const XSUM_seed_t seed,
154
+ const Display_endianess displayEndianess,
155
+ const Display_convention convention)
156
+ {
157
+ @@ -676,7 +728,7 @@ static int XSUM_hashFile(const char* fileName,
158
+ }
159
+
160
+ /* Stream file & update hash */
161
+ - hashValue = XSUM_hashStream(inFile, hashType, buffer, blockSize);
162
+ + hashValue = XSUM_hashStream(inFile, hashType, seed, buffer, blockSize);
163
+
164
+ fclose(inFile);
165
+ free(buffer);
166
+ @@ -703,6 +755,12 @@ static int XSUM_hashFile(const char* fileName,
167
+ f_displayLine(fileName, &hcbe128, hashType);
168
+ break;
169
+ }
170
+ + case algo_xxh3:
171
+ + { XXH64_canonical_t hcbe3;
172
+ + (void)XXH64_canonicalFromHash(&hcbe3, hashValue.xxh3);
173
+ + f_displayLine(fileName, &hcbe3, hashType);
174
+ + break;
175
+ + }
176
+ default:
177
+ assert(0); /* not possible */
178
+ }
179
+ @@ -717,6 +775,7 @@ static int XSUM_hashFile(const char* fileName,
180
+ */
181
+ static int XSUM_hashFiles(char*const * fnList, int fnTotal,
182
+ AlgoSelected hashType,
183
+ + XSUM_seed_t seed,
184
+ Display_endianess displayEndianess,
185
+ Display_convention convention)
186
+ {
187
+ @@ -724,10 +783,10 @@ static int XSUM_hashFiles(char*const * fnList, int fnTotal,
188
+ int result = 0;
189
+
190
+ if (fnTotal==0)
191
+ - return XSUM_hashFile(stdinName, hashType, displayEndianess, convention);
192
+ + return XSUM_hashFile(stdinName, hashType, seed, displayEndianess, convention);
193
+
194
+ for (fnNb=0; fnNb<fnTotal; fnNb++)
195
+ - result |= XSUM_hashFile(fnList[fnNb], hashType, displayEndianess, convention);
196
+ + result |= XSUM_hashFile(fnList[fnNb], hashType, seed, displayEndianess, convention);
197
+ XSUM_logVerbose(2, "\r%70s\r", "");
198
+ return result;
199
+ }
200
+ @@ -760,12 +819,13 @@ typedef union {
201
+ XXH32_canonical_t xxh32;
202
+ XXH64_canonical_t xxh64;
203
+ XXH128_canonical_t xxh128;
204
+ + XXH64_canonical_t xxh3;
205
+ } Canonical;
206
+
207
+ typedef struct {
208
+ Canonical canonical;
209
+ const char* filename;
210
+ - int xxhBits; /* canonical type: 32:xxh32, 64:xxh64, 128:xxh128 */
211
+ + int xxhBits; /* canonical type: 32:xxh32, 64:xxh64, 128:xxh128, 3:xxh3 */
212
+ } ParsedLine;
213
+
214
+ typedef struct {
215
+ @@ -1062,26 +1122,33 @@ static void XSUM_parseFile1(ParseFileArg* XSUM_parseFileArg, int rev)
216
+ switch (parsedLine.xxhBits)
217
+ {
218
+ case 32:
219
+ - { Multihash const xxh = XSUM_hashStream(fp, algo_xxh32, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
220
+ + { Multihash const xxh = XSUM_hashStream(fp, algo_xxh32, (XSUM_seed_t){0}, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
221
+ if (xxh.xxh32 == XXH32_hashFromCanonical(&parsedLine.canonical.xxh32)) {
222
+ lineStatus = LineStatus_hashOk;
223
+ } }
224
+ break;
225
+
226
+ case 64:
227
+ - { Multihash const xxh = XSUM_hashStream(fp, algo_xxh64, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
228
+ + { Multihash const xxh = XSUM_hashStream(fp, algo_xxh64, (XSUM_seed_t){0}, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
229
+ if (xxh.xxh64 == XXH64_hashFromCanonical(&parsedLine.canonical.xxh64)) {
230
+ lineStatus = LineStatus_hashOk;
231
+ } }
232
+ break;
233
+
234
+ case 128:
235
+ - { Multihash const xxh = XSUM_hashStream(fp, algo_xxh128, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
236
+ + { Multihash const xxh = XSUM_hashStream(fp, algo_xxh128, (XSUM_seed_t){0}, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
237
+ if (XXH128_isEqual(xxh.xxh128, XXH128_hashFromCanonical(&parsedLine.canonical.xxh128))) {
238
+ lineStatus = LineStatus_hashOk;
239
+ } }
240
+ break;
241
+
242
+ + case 3:
243
+ + { Multihash const xxh = XSUM_hashStream(fp, algo_xxh3, (XSUM_seed_t){0}, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
244
+ + if (xxh.xxh3 == XXH64_hashFromCanonical(&parsedLine.canonical.xxh3)) {
245
+ + lineStatus = LineStatus_hashOk;
246
+ + } }
247
+ + break;
248
+ +
249
+ default:
250
+ break;
251
+ }
252
+ @@ -1256,7 +1323,9 @@ static int XSUM_usage(const char* exename)
253
+ XSUM_log( "Usage: %s [options] [files] \n\n", exename);
254
+ XSUM_log( "When no filename provided or when '-' is provided, uses stdin as input. \n");
255
+ XSUM_log( "Options: \n");
256
+ - XSUM_log( " -H# algorithm selection: 0,1,2 or 32,64,128 (default: %i) \n", (int)g_defaultAlgo);
257
+ + XSUM_log( " -H# algorithm selection: 0,1,2,3 or 32,64,128,364 (default: %i) \n", (int)g_defaultAlgo);
258
+ + XSUM_log( " -s<seed> seed in hex\n");
259
+ + XSUM_log( " -S<secret> secret in hex (minimum %d decoded bytes)\n", XXH3_SECRET_SIZE_MIN);
260
+ XSUM_log( " -c, --check read xxHash checksum from [files] and check them \n");
261
+ XSUM_log( " -h, --help display a long help page about advanced options \n");
262
+ return 0;
263
+ @@ -1353,9 +1422,72 @@ static XSUM_U32 XSUM_readU32FromChar(const char** stringPtr) {
264
+ return result;
265
+ }
266
+
267
+ +static int hex_decode_str_implied(const unsigned char *src, size_t len, unsigned char *dest)
268
+ +{
269
+ + unsigned char low, high;
270
+ +
271
+ + if (len % 2) {
272
+ + low = *src++;
273
+ +
274
+ + if (low >= '0' && low <= '9') {
275
+ + low -= '0';
276
+ + } else if (low >= 'A' && low <= 'F') {
277
+ + low -= 'A' - 10;
278
+ + } else if (low >= 'a' && low <= 'f') {
279
+ + low -= 'a' - 10;
280
+ + } else {
281
+ + return 0;
282
+ + }
283
+ +
284
+ + *dest++ = low;
285
+ + --len;
286
+ + }
287
+ +
288
+ + for (; len > 0; len -= 2) {
289
+ + high = *src++;
290
+ +
291
+ + if (high >= '0' && high <= '9') {
292
+ + high -= '0';
293
+ + } else if (high >= 'A' && high <= 'F') {
294
+ + high -= 'A' - 10;
295
+ + } else if (high >= 'a' && high <= 'f') {
296
+ + high -= 'a' - 10;
297
+ + } else {
298
+ + return 0;
299
+ + }
300
+ +
301
+ + low = *src++;
302
+ +
303
+ + if (low >= '0' && low <= '9') {
304
+ + low -= '0';
305
+ + } else if (low >= 'A' && low <= 'F') {
306
+ + low -= 'A' - 10;
307
+ + } else if (low >= 'a' && low <= 'f') {
308
+ + low -= 'a' - 10;
309
+ + } else {
310
+ + return 0;
311
+ + }
312
+ +
313
+ + *dest++ = high << 4 | low;
314
+ + }
315
+ +
316
+ + return -1;
317
+ +}
318
+ +
319
+ +static size_t calc_hex_decoded_str_length(size_t hex_encoded_length)
320
+ +{
321
+ + if (hex_encoded_length == 0)
322
+ + return 0;
323
+ +
324
+ + if (hex_encoded_length % 2)
325
+ + ++hex_encoded_length;
326
+ +
327
+ + return hex_encoded_length / 2;
328
+ +}
329
+ +
330
+ XSUM_API int XSUM_main(int argc, char* argv[])
331
+ {
332
+ - int i, filenamesStart = 0;
333
+ + int i, j, filenamesStart = 0;
334
+ const char* const exename = XSUM_lastNameFromPath(argv[0]);
335
+ XSUM_U32 benchmarkMode = 0;
336
+ XSUM_U32 fileCheckMode = 0;
337
+ @@ -1369,6 +1501,9 @@ XSUM_API int XSUM_main(int argc, char* argv[])
338
+ AlgoSelected algo = g_defaultAlgo;
339
+ Display_endianess displayEndianess = big_endian;
340
+ Display_convention convention = display_gnu;
341
+ + XSUM_seed_t seed = {0};
342
+ + char seed_hex[32];
343
+ + char *secret_hex, *error;
344
+
345
+ /* special case: xxhNNsum default to NN bits checksum */
346
+ if (strstr(exename, "xxh32sum") != NULL) algo = g_defaultAlgo = algo_xxh32;
347
+ @@ -1424,11 +1559,54 @@ XSUM_API int XSUM_main(int argc, char* argv[])
348
+ case 64: algo = algo_xxh64; break;
349
+ case 2 :
350
+ case 128: algo = algo_xxh128; break;
351
+ + case 3 :
352
+ + case 364: algo = algo_xxh3; break;
353
+ default:
354
+ return XSUM_badusage(exename);
355
+ }
356
+ break;
357
+
358
+ + case 's': argument++;
359
+ + errno = 0;
360
+ + j = 0;
361
+ + while (*argument >= '0' && *argument <= '9' || *argument >= 'a' && *argument <= 'f'
362
+ + || *argument >= 'A' && *argument <= 'F') {
363
+ + if (j < 32)
364
+ + seed_hex[j++] = *argument;
365
+ + argument++;
366
+ + }
367
+ + seed_hex[j] = 0;
368
+ + if (seed.type == XSUM_SECRET && seed.secret)
369
+ + free(seed.secret);
370
+ + seed.type = XSUM_SEED;
371
+ + seed.seed = strtoull(seed_hex, 0, 16);
372
+ + if (errno) {
373
+ + error = strerror(errno);
374
+ + XSUM_log("Error: Failed to parse seed argument: %s\n", error);
375
+ + return XSUM_badusage(exename);
376
+ + }
377
+ + break;
378
+ +
379
+ + case 'S': argument++;
380
+ + size_t hex_length = strlen(argument);
381
+ + size_t hex_decoded_len = calc_hex_decoded_str_length(hex_length);
382
+ + if (hex_decoded_len < XXH3_SECRET_SIZE_MIN) {
383
+ + XSUM_log("Error: Secret needs to be at least %d bytes in length.\n",
384
+ + XXH3_SECRET_SIZE_MIN);
385
+ + return XSUM_badusage(exename);
386
+ + }
387
+ + if (seed.type == XSUM_SECRET && seed.secret)
388
+ + free(seed.secret);
389
+ + seed.type = XSUM_SECRET;
390
+ + seed.secret = malloc(hex_decoded_len);
391
+ + seed.secret_length = hex_decoded_len;
392
+ + if (! hex_decode_str_implied(argument, hex_length, seed.secret)) {
393
+ + XSUM_log("Error: Invalid hex argument: %s\n", argument);
394
+ + return XSUM_badusage(exename);
395
+ + }
396
+ + argument += hex_length;
397
+ + break;
398
+ +
399
+ /* File check mode */
400
+ case 'c':
401
+ fileCheckMode=1;
402
+ @@ -1479,6 +1657,14 @@ XSUM_API int XSUM_main(int argc, char* argv[])
403
+ }
404
+ } /* for(i=1; i<argc; i++) */
405
+
406
+ + if (seed.type == XSUM_SECRET && (algo == 0 || algo == 1)) {
407
+ + XSUM_log("Error: XXH32 or XXH64 does not support secrets.\n");
408
+ + return XSUM_badusage(exename);
409
+ + } else if (algo == 0 && seed.seed > UINT32_MAX) {
410
+ + XSUM_log("Error: Seed value too large for 32-bit algorithm.\n");
411
+ + return XSUM_badusage(exename);
412
+ + }
413
+ +
414
+ /* Check benchmark mode */
415
+ if (benchmarkMode) {
416
+ XSUM_logVerbose(2, FULL_WELCOME_MESSAGE(exename) );
417
+ @@ -1498,6 +1684,6 @@ XSUM_API int XSUM_main(int argc, char* argv[])
418
+ return XSUM_checkFiles(argv+filenamesStart, argc-filenamesStart,
419
+ displayEndianess, strictMode, statusOnly, warn, (XSUM_logLevel < 2) /*quiet*/);
420
+ } else {
421
+ - return XSUM_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, displayEndianess, convention);
422
+ + return XSUM_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, seed, displayEndianess, convention);
423
+ }
424
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: digest-xxhash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - konsolebox
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-07 00:00:00.000000000 Z
11
+ date: 2021-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,7 +52,10 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.8'
55
- description: An XXHash library that complies with Digest::Instance's functional design.
55
+ description: |2
56
+ This gem provides XXH32, XXH64, XXH3_64bits and XXH3_128bits
57
+ functionalities for Ruby. It inherits Digest::Class and complies
58
+ with Digest::Instance's functional design.
56
59
  email:
57
60
  - konsolebox@gmail.com
58
61
  executables: []
@@ -69,17 +72,18 @@ files:
69
72
  - ext/digest/xxhash/ext.c
70
73
  - ext/digest/xxhash/extconf.rb
71
74
  - ext/digest/xxhash/utils.h
72
- - ext/digest/xxhash/xxhash.c
73
75
  - ext/digest/xxhash/xxhash.h
74
76
  - lib/digest/xxhash/version.rb
75
77
  - test/produce-vectors-with-ruby-xxhash.rb
78
+ - test/produce-vectors-with-xxhsum.rb
76
79
  - test/test.rb
77
80
  - test/test.vectors
81
+ - test/xxhsum.c.c0e86bc0.diff
78
82
  homepage: https://github.com/konsolebox/digest-xxhash-ruby
79
83
  licenses:
80
84
  - MIT
81
85
  metadata: {}
82
- post_install_message:
86
+ post_install_message:
83
87
  rdoc_options: []
84
88
  require_paths:
85
89
  - lib
@@ -94,12 +98,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
98
  - !ruby/object:Gem::Version
95
99
  version: '0'
96
100
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.6.12
99
- signing_key:
101
+ rubygems_version: 3.2.14
102
+ signing_key:
100
103
  specification_version: 4
101
- summary: XXHash for Ruby
104
+ summary: A Digest framework based XXHash library for Ruby
102
105
  test_files:
103
106
  - test/produce-vectors-with-ruby-xxhash.rb
107
+ - test/produce-vectors-with-xxhsum.rb
104
108
  - test/test.rb
105
109
  - test/test.vectors
110
+ - test/xxhsum.c.c0e86bc0.diff