Skip to content

Commit b7e79ef

Browse files
author
d06i
committed
cli: support uint64_t seed for xxh64 and xxh3
1 parent d12764e commit b7e79ef

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

cli/xxhsum.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ static int XSUM_algoBitmask_Accepts(XSUM_U32 algoBitmask, AlgoSelected parsedLin
210210
* File Hashing
211211
**********************************************************/
212212

213-
XSUM_U32 XXHSUM32_DEFAULT_SEED = 0; /* Default seed for algo_xxh32 */
214-
XSUM_U64 XXHSUM64_DEFAULT_SEED = 0; /* Default seed for algo_xxh64 */
213+
static XSUM_U32 g_default_seed_u32 = 0; /* Default seed for algo_xxh32 */
214+
static XSUM_U64 g_default_seed_u64 = 0; /* Default seed for algo_xxh64, algo_xxh3 and algo_xxh128 */
215215

216216
/* for support of --little-endian display mode */
217217
static void XSUM_display_LittleEndian(const void* ptr, size_t length)
@@ -251,9 +251,10 @@ XSUM_hashStream(FILE* inFile,
251251
XXH3_state_t state3;
252252

253253
/* Init */
254-
(void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
255-
(void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
254+
(void)XXH32_reset(&state32, g_default_seed_u32);
255+
(void)XXH64_reset(&state64, g_default_seed_u64);
256256
(void)XXH3_128bits_reset(&state3);
257+
(void)XXH3_128bits_reset_withSeed(&state3, g_default_seed_u64);
257258

258259
/* Load file & update hash */
259260
{ size_t readSize;
@@ -1412,7 +1413,7 @@ static int XSUM_usage_advanced(const char* exename)
14121413
XSUM_log( " --tag Produce BSD-style checksum lines \n");
14131414
XSUM_log( " --little-endian Checksum values use little endian convention (default: big endian) \n");
14141415
XSUM_log( " --binary Read in binary mode \n");
1415-
XSUM_log( " -s#, Set seed (default: 0 [max: 2^32]) \n");
1416+
XSUM_log( " -s#, Set seed (default: 0) \n");
14161417
XSUM_log( " -b Run benchmark \n");
14171418
XSUM_log( " -b# Bench only algorithm variant # \n");
14181419
XSUM_log( " -i# Number of times to run the benchmark (default: %i) \n", NBLOOPS_DEFAULT);
@@ -1497,6 +1498,28 @@ static XSUM_U32 XSUM_readU32FromChar(const char** stringPtr) {
14971498
return result;
14981499
}
14991500

1501+
/* Similar to XSUM_readU32FromCharChecked but for XSUM_U64 */
1502+
static XSUM_U64 XSUM_readU64FromChar( const char** number_str ){
1503+
1504+
static const XSUM_U64 max = (((XSUM_U64)(-1)) / 10) - 1;
1505+
XSUM_U64 res = 0;
1506+
1507+
if ( **number_str == '-' ) /* check negative value */
1508+
errorOut("Error: numeric value cannot be negative");
1509+
1510+
if ((**number_str < '0') || (**number_str > '9'))
1511+
errorOut("Error: invalid numeric value");
1512+
1513+
while ( **number_str >= '0' && **number_str <= '9' ){
1514+
if ( res > max ) errorOut("Error: numeric value too large"); /* overflow */
1515+
res *= 10;
1516+
res += (XSUM_U64)(**number_str - '0');
1517+
(*number_str)++;
1518+
}
1519+
1520+
return res;
1521+
}
1522+
15001523
XSUM_API int XSUM_main(int argc, const char* argv[])
15011524
{
15021525
int i, filenamesStart = 0;
@@ -1640,10 +1663,10 @@ XSUM_API int XSUM_main(int argc, const char* argv[])
16401663
/* Modify seed */
16411664
case 's': argument++;
16421665
switch( algo ){
1643-
case algo_xxh32: XXHSUM32_DEFAULT_SEED = XSUM_readU32FromChar(&argument); break;
1644-
case algo_xxh64: XXHSUM64_DEFAULT_SEED = XSUM_readU32FromChar(&argument); break;
1645-
case algo_xxh3: break;
1646-
case algo_xxh128: break;
1666+
case algo_xxh32 : g_default_seed_u32 = XSUM_readU32FromChar(&argument); break;
1667+
case algo_xxh64 :
1668+
case algo_xxh3 :
1669+
case algo_xxh128 : g_default_seed_u64 = XSUM_readU64FromChar(&argument); break;
16471670
default:
16481671
return XSUM_badusage(exename);
16491672
}

0 commit comments

Comments
 (0)