2014-08-15 09:27:04 +00:00
|
|
|
/*
|
2020-03-02 20:51:29 +00:00
|
|
|
* xxhsum - Command line interface for xxhash algorithms
|
2020-03-09 01:28:43 +00:00
|
|
|
* Copyright (C) 2013-2020 Yann Collet
|
2020-03-02 20:51:29 +00:00
|
|
|
*
|
|
|
|
* GPL v2 License
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* You can contact the author at:
|
2020-03-05 02:36:13 +00:00
|
|
|
* - xxHash homepage: https://www.xxhash.com
|
2020-03-02 20:51:29 +00:00
|
|
|
* - xxHash source repository: https://github.com/Cyan4973/xxHash
|
|
|
|
*/
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*
|
|
|
|
* xxhsum:
|
2016-01-07 22:54:51 +00:00
|
|
|
* Provides hash value of a file content, or a list of files, or stdin
|
|
|
|
* Display convention is Big Endian, for both 32 and 64 bits algorithms
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2018-03-20 17:41:22 +00:00
|
|
|
* Compiler Options
|
|
|
|
**************************************/
|
2014-10-29 01:42:26 +00:00
|
|
|
/* MS Visual */
|
|
|
|
#if defined(_MSC_VER) || defined(_WIN32)
|
|
|
|
# define _CRT_SECURE_NO_WARNINGS /* removes visual warnings */
|
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 01:42:26 +00:00
|
|
|
/* Under Linux at least, pull in the *64 commands */
|
2016-01-14 02:01:39 +00:00
|
|
|
#ifndef _LARGEFILE64_SOURCE
|
|
|
|
# define _LARGEFILE64_SOURCE
|
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2018-03-20 17:41:22 +00:00
|
|
|
* Includes
|
|
|
|
**************************************/
|
2020-06-12 18:39:51 +00:00
|
|
|
#include <limits.h>
|
2016-08-14 02:28:17 +00:00
|
|
|
#include <stdlib.h> /* malloc, calloc, free, exit */
|
2020-06-12 18:39:51 +00:00
|
|
|
#include <string.h> /* strcmp, memcpy */
|
2018-02-19 00:56:14 +00:00
|
|
|
#include <stdio.h> /* fprintf, fopen, ftello64, fread, stdin, stdout, _fileno (when present) */
|
|
|
|
#include <sys/types.h> /* stat, stat64, _stat64 */
|
|
|
|
#include <sys/stat.h> /* stat, stat64, _stat64 */
|
2016-04-07 23:50:06 +00:00
|
|
|
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
|
2018-02-19 00:56:14 +00:00
|
|
|
#include <assert.h> /* assert */
|
2019-03-15 15:56:58 +00:00
|
|
|
#include <errno.h> /* errno */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2019-10-11 14:50:06 +00:00
|
|
|
#define XXH_STATIC_LINKING_ONLY /* *_state_t */
|
2020-02-12 22:36:11 +00:00
|
|
|
#include "xxhash.h"
|
2019-10-11 14:50:06 +00:00
|
|
|
|
2020-06-09 06:15:08 +00:00
|
|
|
#ifdef XXHSUM_DISPATCH
|
|
|
|
# include "xxh_x86dispatch.h"
|
|
|
|
#endif
|
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
|
2018-03-20 17:41:22 +00:00
|
|
|
/* ************************************
|
|
|
|
* OS-Specific Includes
|
|
|
|
**************************************/
|
2018-09-30 06:09:23 +00:00
|
|
|
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \
|
|
|
|
|| defined(__midipix__) || defined(__VMS))
|
|
|
|
# if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */ \
|
|
|
|
|| defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */
|
|
|
|
# define PLATFORM_POSIX_VERSION 200112L
|
|
|
|
# else
|
|
|
|
# if defined(__linux__) || defined(__linux)
|
|
|
|
# ifndef _POSIX_C_SOURCE
|
|
|
|
# define _POSIX_C_SOURCE 200112L /* use feature test macro */
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
# include <unistd.h> /* declares _POSIX_VERSION */
|
|
|
|
# if defined(_POSIX_VERSION) /* POSIX compliant */
|
|
|
|
# define PLATFORM_POSIX_VERSION _POSIX_VERSION
|
|
|
|
# else
|
|
|
|
# define PLATFORM_POSIX_VERSION 0
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#if !defined(PLATFORM_POSIX_VERSION)
|
|
|
|
# define PLATFORM_POSIX_VERSION -1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) \
|
|
|
|
|| (PLATFORM_POSIX_VERSION >= 200112L) \
|
|
|
|
|| defined(__DJGPP__) \
|
|
|
|
|| defined(__MSYS__)
|
|
|
|
# include <unistd.h> /* isatty */
|
|
|
|
# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
|
2020-02-11 00:36:38 +00:00
|
|
|
#elif defined(MSDOS) || defined(OS2)
|
2018-09-30 06:09:23 +00:00
|
|
|
# include <io.h> /* _isatty */
|
2014-10-29 12:55:58 +00:00
|
|
|
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
|
2018-09-30 06:09:23 +00:00
|
|
|
#elif defined(WIN32) || defined(_WIN32)
|
|
|
|
# include <io.h> /* _isatty */
|
|
|
|
# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
|
|
|
|
# include <stdio.h> /* FILE */
|
|
|
|
static __inline int IS_CONSOLE(FILE* stdStream) {
|
|
|
|
DWORD dummy;
|
|
|
|
return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define IS_CONSOLE(stdStream) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
|
|
|
|
# include <fcntl.h> /* _O_BINARY */
|
|
|
|
# include <io.h> /* _setmode, _fileno, _get_osfhandle */
|
|
|
|
# if !defined(__DJGPP__)
|
|
|
|
# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
|
|
|
|
# include <winioctl.h> /* FSCTL_SET_SPARSE */
|
|
|
|
# define SET_BINARY_MODE(file) { int const unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
|
|
|
|
# else
|
|
|
|
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
|
|
|
|
# endif
|
2014-10-29 12:55:58 +00:00
|
|
|
#else
|
|
|
|
# define SET_BINARY_MODE(file)
|
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
#if !defined(S_ISREG)
|
|
|
|
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
2020-03-28 19:40:51 +00:00
|
|
|
/* Unicode helpers for Windows to make UTF-8 act as it should. */
|
|
|
|
#ifdef _WIN32
|
2020-03-10 18:06:09 +00:00
|
|
|
/*
|
|
|
|
* Converts a UTF-8 string to UTF-16. Acts like strdup. The string must be freed afterwards.
|
|
|
|
* This version allows keeping the output length.
|
|
|
|
*/
|
2020-05-28 23:10:00 +00:00
|
|
|
static wchar_t* utf8_to_utf16_len(const char* str, int* lenOut)
|
2020-02-13 01:37:34 +00:00
|
|
|
{
|
2020-05-28 23:10:00 +00:00
|
|
|
int const len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
|
|
|
|
if (lenOut != NULL) *lenOut = len;
|
|
|
|
if (len == 0) return NULL;
|
|
|
|
{ wchar_t* buf = (wchar_t*)malloc((size_t)len * sizeof(wchar_t));
|
2020-02-13 01:37:34 +00:00
|
|
|
if (buf != NULL) {
|
|
|
|
if (MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, len) == 0) {
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2020-05-28 23:10:00 +00:00
|
|
|
} }
|
2020-02-13 01:37:34 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 18:06:09 +00:00
|
|
|
|
|
|
|
/* Converts a UTF-8 string to UTF-16. Acts like strdup. The string must be freed afterwards. */
|
2020-05-28 23:10:00 +00:00
|
|
|
static wchar_t* utf8_to_utf16(const char *str)
|
2020-03-10 18:06:09 +00:00
|
|
|
{
|
|
|
|
return utf8_to_utf16_len(str, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts a UTF-16 string to UTF-8. Acts like strdup. The string must be freed afterwards.
|
|
|
|
* This version allows keeping the output length.
|
|
|
|
*/
|
2020-05-28 23:10:00 +00:00
|
|
|
static char* utf16_to_utf8_len(const wchar_t *str, int *lenOut)
|
2020-02-13 01:37:34 +00:00
|
|
|
{
|
|
|
|
int len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
|
2020-05-28 23:10:00 +00:00
|
|
|
if (lenOut != NULL) *lenOut = len;
|
|
|
|
if (len == 0) return NULL;
|
|
|
|
{ char* const buf = (char*)malloc((size_t)len * sizeof(char));
|
2020-02-13 01:37:34 +00:00
|
|
|
if (buf != NULL) {
|
|
|
|
if (WideCharToMultiByte(CP_UTF8, 0, str, -1, buf, len, NULL, NULL) == 0) {
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2020-05-28 23:10:00 +00:00
|
|
|
} }
|
|
|
|
return buf;
|
2020-02-13 01:37:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:06:09 +00:00
|
|
|
/* Converts a UTF-16 string to UTF-8. Acts like strdup. The string must be freed afterwards. */
|
|
|
|
static char *utf16_to_utf8(const wchar_t *str)
|
|
|
|
{
|
|
|
|
return utf16_to_utf8_len(str, NULL);
|
|
|
|
}
|
|
|
|
|
2020-02-15 01:48:12 +00:00
|
|
|
/*
|
2020-03-04 03:08:12 +00:00
|
|
|
* fopen wrapper that supports UTF-8
|
2020-02-15 01:48:12 +00:00
|
|
|
*
|
|
|
|
* fopen will only accept ANSI filenames, which means that we can't open Unicode filenames.
|
|
|
|
*
|
|
|
|
* In order to open a Unicode filename, we need to convert filenames to UTF-16 and use _wfopen.
|
|
|
|
*/
|
2020-05-28 23:10:00 +00:00
|
|
|
static FILE* XXH_fopen_wrapped(const char *filename, const wchar_t *mode)
|
2020-02-13 01:37:34 +00:00
|
|
|
{
|
2020-05-28 23:10:00 +00:00
|
|
|
wchar_t* const wide_filename = utf8_to_utf16(filename);
|
|
|
|
if (wide_filename == NULL) return NULL;
|
|
|
|
{ FILE* const f = _wfopen(wide_filename, mode);
|
2020-02-13 01:37:34 +00:00
|
|
|
free(wide_filename);
|
2020-05-28 23:10:00 +00:00
|
|
|
return f;
|
2020-02-13 01:37:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:06:09 +00:00
|
|
|
/*
|
|
|
|
* In case it isn't available, this is what MSVC 2019 defines in stdarg.h.
|
|
|
|
*/
|
|
|
|
#if defined(_MSC_VER) && !defined(__clang__) && !defined(va_copy)
|
|
|
|
# define va_copy(destination, source) ((destination) = (source))
|
|
|
|
#endif
|
|
|
|
|
2020-03-04 01:56:19 +00:00
|
|
|
/*
|
2020-03-04 03:08:12 +00:00
|
|
|
* fprintf wrapper that supports UTF-8.
|
2020-03-04 01:56:19 +00:00
|
|
|
*
|
2020-03-10 18:06:09 +00:00
|
|
|
* fprintf doesn't properly handle Unicode on Windows.
|
2020-03-04 03:08:12 +00:00
|
|
|
*
|
2020-03-10 18:12:15 +00:00
|
|
|
* Additionally, it is codepage sensitive on console and may crash the program.
|
2020-03-04 03:08:12 +00:00
|
|
|
*
|
2020-03-10 18:12:15 +00:00
|
|
|
* Instead, we use vsnprintf, and either print with fwrite or convert to UTF-16
|
|
|
|
* for console output and use the codepage-independent WriteConsoleW.
|
2020-03-04 01:56:19 +00:00
|
|
|
*
|
|
|
|
* Credit to t-mat: https://github.com/t-mat/xxHash/commit/5691423
|
|
|
|
*/
|
|
|
|
static int fprintf_utf8(FILE *stream, const char *format, ...)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
va_list args;
|
2020-03-10 18:06:09 +00:00
|
|
|
va_list copy;
|
|
|
|
|
2020-03-04 01:56:19 +00:00
|
|
|
va_start(args, format);
|
2020-03-10 18:06:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To be safe, make a va_copy.
|
|
|
|
*
|
|
|
|
* Note that Microsoft doesn't use va_copy in its sample code:
|
|
|
|
* https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/vsprintf-vsprintf-l-vswprintf-vswprintf-l-vswprintf-l?view=vs-2019
|
|
|
|
*/
|
|
|
|
va_copy(copy, args);
|
|
|
|
/* Counts the number of characters needed for vsnprintf. */
|
|
|
|
result = _vscprintf(format, copy);
|
|
|
|
va_end(copy);
|
|
|
|
|
2020-03-04 01:56:19 +00:00
|
|
|
if (result > 0) {
|
2020-03-10 18:06:09 +00:00
|
|
|
/* Create a buffer for vsnprintf */
|
2020-03-04 01:56:19 +00:00
|
|
|
const size_t nchar = (size_t)result + 1;
|
2020-03-10 18:06:09 +00:00
|
|
|
char* u8_str = (char*)malloc(nchar * sizeof(u8_str[0]));
|
|
|
|
|
2020-03-04 01:56:19 +00:00
|
|
|
if (u8_str == NULL) {
|
|
|
|
result = -1;
|
|
|
|
} else {
|
2020-03-10 18:06:09 +00:00
|
|
|
/* Generate the UTF-8 string with vsnprintf. */
|
|
|
|
result = _vsnprintf(u8_str, nchar - 1, format, args);
|
|
|
|
u8_str[nchar - 1] = '\0';
|
2020-03-04 01:56:19 +00:00
|
|
|
if (result > 0) {
|
2020-03-10 18:06:09 +00:00
|
|
|
/*
|
2020-03-10 18:12:15 +00:00
|
|
|
* Check if we are outputting to a console. Don't use IS_CONSOLE
|
|
|
|
* directly -- we don't need to call _get_osfhandle twice.
|
2020-03-10 18:06:09 +00:00
|
|
|
*/
|
|
|
|
int fileNb = _fileno(stream);
|
|
|
|
intptr_t handle_raw = _get_osfhandle(fileNb);
|
|
|
|
HANDLE handle = (HANDLE)handle_raw;
|
|
|
|
DWORD dwTemp;
|
|
|
|
|
|
|
|
if (handle_raw < 0) {
|
|
|
|
result = -1;
|
|
|
|
} else if (_isatty(fileNb) && GetConsoleMode(handle, &dwTemp)) {
|
|
|
|
/*
|
|
|
|
* Convert to UTF-16 and output with WriteConsoleW.
|
|
|
|
*
|
2020-03-10 18:12:15 +00:00
|
|
|
* This is codepage independent and works on Windows XP's
|
|
|
|
* default msvcrt.dll.
|
2020-03-10 18:06:09 +00:00
|
|
|
*/
|
|
|
|
int len;
|
|
|
|
wchar_t *const u16_buf = utf8_to_utf16_len(u8_str, &len);
|
|
|
|
if (u16_buf == NULL) {
|
|
|
|
result = -1;
|
|
|
|
} else {
|
|
|
|
if (WriteConsoleW(handle, u16_buf, (DWORD)len - 1, &dwTemp, NULL)) {
|
|
|
|
result = (int)dwTemp;
|
|
|
|
} else {
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
free(u16_buf);
|
|
|
|
}
|
2020-03-04 01:56:19 +00:00
|
|
|
} else {
|
2020-03-10 18:06:09 +00:00
|
|
|
/* fwrite the UTF-8 string if we are printing to a file */
|
|
|
|
result = (int)fwrite(u8_str, 1, nchar - 1, stream);
|
|
|
|
if (result == 0) {
|
|
|
|
result = -1;
|
|
|
|
}
|
2020-03-04 01:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(u8_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
2020-02-15 01:48:12 +00:00
|
|
|
/*
|
|
|
|
* Since we always use literals in the "mode" argument, it is just easier to append "L" to
|
|
|
|
* the string to make it UTF-16 and avoid the hassle of a second manual conversion.
|
|
|
|
*/
|
2020-02-13 01:37:34 +00:00
|
|
|
# define XXH_fopen(filename, mode) XXH_fopen_wrapped(filename, L##mode)
|
|
|
|
#else
|
|
|
|
# define XXH_fopen(filename, mode) fopen(filename, mode)
|
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* Basic Types
|
2016-01-04 07:32:38 +00:00
|
|
|
**************************************/
|
2020-03-10 00:03:15 +00:00
|
|
|
#if defined(__cplusplus) /* C++ */ \
|
|
|
|
|| (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) /* C99 */
|
|
|
|
# include <stdint.h>
|
2019-10-07 14:52:32 +00:00
|
|
|
typedef uint8_t U8;
|
|
|
|
typedef uint32_t U32;
|
|
|
|
typedef uint64_t U64;
|
2020-03-10 00:03:15 +00:00
|
|
|
# else
|
2019-10-06 23:14:12 +00:00
|
|
|
# include <limits.h>
|
2019-10-07 14:52:32 +00:00
|
|
|
typedef unsigned char U8;
|
2019-10-06 23:14:12 +00:00
|
|
|
# if UINT_MAX == 0xFFFFFFFFUL
|
2019-10-07 14:52:32 +00:00
|
|
|
typedef unsigned int U32;
|
2019-10-06 23:14:12 +00:00
|
|
|
# else
|
2019-10-07 14:52:32 +00:00
|
|
|
typedef unsigned long U32;
|
2019-10-06 23:14:12 +00:00
|
|
|
# endif
|
2019-10-07 14:52:32 +00:00
|
|
|
typedef unsigned long long U64;
|
2020-03-10 00:03:15 +00:00
|
|
|
#endif /* not C++/C99 */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
static unsigned BMK_isLittleEndian(void)
|
|
|
|
{
|
2020-03-03 17:10:19 +00:00
|
|
|
const union { U32 u; U8 c[4]; } one = { 1 }; /* don't use static: performance detrimental */
|
2015-05-07 14:27:27 +00:00
|
|
|
return one.c[0];
|
|
|
|
}
|
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* *************************************
|
2018-03-20 17:41:22 +00:00
|
|
|
* Constants
|
|
|
|
***************************************/
|
2016-01-03 22:56:13 +00:00
|
|
|
#define LIB_VERSION XXH_VERSION_MAJOR.XXH_VERSION_MINOR.XXH_VERSION_RELEASE
|
|
|
|
#define QUOTE(str) #str
|
|
|
|
#define EXPAND_AND_QUOTE(str) QUOTE(str)
|
|
|
|
#define PROGRAM_VERSION EXPAND_AND_QUOTE(LIB_VERSION)
|
2019-03-15 15:56:58 +00:00
|
|
|
|
2020-04-07 22:19:08 +00:00
|
|
|
/* Show compiler versions in WELCOME_MESSAGE. CC_VERSION_FMT will return the printf specifiers,
|
|
|
|
* and VERSION will contain the comma separated list of arguments to the CC_VERSION_FMT string. */
|
2019-03-15 15:56:58 +00:00
|
|
|
#if defined(__clang_version__)
|
|
|
|
/* Clang does its own thing. */
|
|
|
|
# ifdef __apple_build_version__
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION_FMT "Apple Clang %s"
|
2019-03-15 15:56:58 +00:00
|
|
|
# else
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION_FMT "Clang %s"
|
2019-03-15 15:56:58 +00:00
|
|
|
# endif
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION __clang_version__
|
2019-03-15 15:56:58 +00:00
|
|
|
#elif defined(__VERSION__)
|
|
|
|
/* GCC and ICC */
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION_FMT "%s"
|
2019-03-15 15:56:58 +00:00
|
|
|
# ifdef __INTEL_COMPILER /* icc adds its prefix */
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION __VERSION__
|
2019-03-15 15:56:58 +00:00
|
|
|
# else /* assume GCC */
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION "GCC " __VERSION__
|
2019-03-15 15:56:58 +00:00
|
|
|
# endif
|
|
|
|
#elif defined(_MSC_FULL_VER) && defined(_MSC_BUILD)
|
2020-03-03 17:10:19 +00:00
|
|
|
/*
|
|
|
|
* MSVC
|
|
|
|
* "For example, if the version number of the Visual C++ compiler is
|
|
|
|
* 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706."
|
|
|
|
*
|
|
|
|
* https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2017
|
|
|
|
*/
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION_FMT "MSVC %02i.%02i.%05i.%02i"
|
|
|
|
# define CC_VERSION _MSC_FULL_VER / 10000000 % 100, _MSC_FULL_VER / 100000 % 100, _MSC_FULL_VER % 100000, _MSC_BUILD
|
2019-03-15 15:56:58 +00:00
|
|
|
#elif defined(__TINYC__)
|
|
|
|
/* tcc stores its version in the __TINYC__ macro. */
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION_FMT "tcc %i.%i.%i"
|
|
|
|
# define CC_VERSION __TINYC__ / 10000 % 100, __TINYC__ / 100 % 100, __TINYC__ % 100
|
2019-03-15 15:56:58 +00:00
|
|
|
#else
|
2020-04-07 22:19:08 +00:00
|
|
|
# define CC_VERSION_FMT "%s"
|
|
|
|
# define CC_VERSION "unknown compiler"
|
2019-03-15 15:56:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* makes the next part easier */
|
2020-03-03 04:24:27 +00:00
|
|
|
#if defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
|
2020-02-28 18:39:27 +00:00
|
|
|
# define ARCH_X64 1
|
2019-03-15 15:56:58 +00:00
|
|
|
# define ARCH_X86 "x86_64"
|
2020-02-28 18:39:27 +00:00
|
|
|
#elif defined(__i386__) || defined(_M_IX86) || defined(_M_IX86_FP)
|
2019-03-15 15:56:58 +00:00
|
|
|
# define ARCH_X86 "i386"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Try to detect the architecture. */
|
|
|
|
#if defined(ARCH_X86)
|
2020-06-16 00:09:39 +00:00
|
|
|
# if defined(XXHSUM_DISPATCH)
|
|
|
|
# define ARCH ARCH_X86 " autoVec"
|
|
|
|
# elif defined(__AVX512F__)
|
2020-04-06 07:58:26 +00:00
|
|
|
# define ARCH ARCH_X86 " + AVX512"
|
|
|
|
# elif defined(__AVX2__)
|
2019-03-15 15:56:58 +00:00
|
|
|
# define ARCH ARCH_X86 " + AVX2"
|
|
|
|
# elif defined(__AVX__)
|
|
|
|
# define ARCH ARCH_X86 " + AVX"
|
2020-03-03 04:24:27 +00:00
|
|
|
# elif defined(_M_X64) || defined(_M_AMD64) || defined(__x86_64__) \
|
2020-02-28 18:39:27 +00:00
|
|
|
|| defined(__SSE2__) || (defined(_M_IX86_FP) && _M_IX86_FP == 2)
|
2019-03-15 15:56:58 +00:00
|
|
|
# define ARCH ARCH_X86 " + SSE2"
|
|
|
|
# else
|
2020-02-28 18:39:27 +00:00
|
|
|
# define ARCH ARCH_X86
|
2019-03-15 15:56:58 +00:00
|
|
|
# endif
|
|
|
|
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
|
2020-02-28 18:39:27 +00:00
|
|
|
# define ARCH "aarch64 + NEON"
|
2019-03-15 15:56:58 +00:00
|
|
|
#elif defined(__arm__) || defined(__thumb__) || defined(__thumb2__) || defined(_M_ARM)
|
2020-02-28 18:39:27 +00:00
|
|
|
/* ARM has a lot of different features that can change xxHash significantly. */
|
|
|
|
# if defined(__thumb2__) || (defined(__thumb__) && (__thumb__ == 2 || __ARM_ARCH >= 7))
|
|
|
|
# define ARCH_THUMB " Thumb-2"
|
|
|
|
# elif defined(__thumb__)
|
|
|
|
# define ARCH_THUMB " Thumb-1"
|
|
|
|
# else
|
|
|
|
# define ARCH_THUMB ""
|
|
|
|
# endif
|
|
|
|
/* ARMv7 has unaligned by default */
|
|
|
|
# if defined(__ARM_FEATURE_UNALIGNED) || __ARM_ARCH >= 7 || defined(_M_ARMV7VE)
|
|
|
|
# define ARCH_UNALIGNED " + unaligned"
|
|
|
|
# else
|
|
|
|
# define ARCH_UNALIGNED ""
|
|
|
|
# endif
|
2019-03-15 15:56:58 +00:00
|
|
|
# if defined(__ARM_NEON) || defined(__ARM_NEON__)
|
2020-02-28 18:39:27 +00:00
|
|
|
# define ARCH_NEON " + NEON"
|
2019-03-15 15:56:58 +00:00
|
|
|
# else
|
2020-02-28 18:39:27 +00:00
|
|
|
# define ARCH_NEON ""
|
2019-03-15 15:56:58 +00:00
|
|
|
# endif
|
2020-02-28 18:39:27 +00:00
|
|
|
# define ARCH "ARMv" EXPAND_AND_QUOTE(__ARM_ARCH) ARCH_THUMB ARCH_NEON ARCH_UNALIGNED
|
2019-03-15 15:56:58 +00:00
|
|
|
#elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
|
2019-08-21 01:06:11 +00:00
|
|
|
# if defined(__GNUC__) && defined(__POWER9_VECTOR__)
|
|
|
|
# define ARCH "ppc64 + POWER9 vector"
|
|
|
|
# elif defined(__GNUC__) && defined(__POWER8_VECTOR__)
|
|
|
|
# define ARCH "ppc64 + POWER8 vector"
|
2019-04-26 19:56:26 +00:00
|
|
|
# else
|
|
|
|
# define ARCH "ppc64"
|
|
|
|
# endif
|
2019-03-15 15:56:58 +00:00
|
|
|
#elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
|
|
|
|
# define ARCH "ppc"
|
|
|
|
#elif defined(__AVR)
|
|
|
|
# define ARCH "AVR"
|
|
|
|
#elif defined(__mips64)
|
|
|
|
# define ARCH "mips64"
|
|
|
|
#elif defined(__mips)
|
|
|
|
# define ARCH "mips"
|
2019-12-14 21:30:22 +00:00
|
|
|
#elif defined(__s390x__)
|
|
|
|
# define ARCH "s390x"
|
|
|
|
#elif defined(__s390__)
|
|
|
|
# define ARCH "s390"
|
2019-03-15 15:56:58 +00:00
|
|
|
#else
|
|
|
|
# define ARCH "unknown"
|
|
|
|
#endif
|
|
|
|
|
2015-08-19 14:11:24 +00:00
|
|
|
static const int g_nbBits = (int)(sizeof(void*)*8);
|
|
|
|
static const char g_lename[] = "little endian";
|
|
|
|
static const char g_bename[] = "big endian";
|
|
|
|
#define ENDIAN_NAME (BMK_isLittleEndian() ? g_lename : g_bename)
|
2015-05-07 14:27:27 +00:00
|
|
|
static const char author[] = "Yann Collet";
|
2020-04-07 22:19:08 +00:00
|
|
|
#define WELCOME_MESSAGE(exename) "%s %s by %s, compiled as %i-bit %s %s with " CC_VERSION_FMT " \n", \
|
|
|
|
exename, PROGRAM_VERSION, author, g_nbBits, ARCH, ENDIAN_NAME, CC_VERSION
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2018-02-19 01:38:48 +00:00
|
|
|
#define KB *( 1<<10)
|
|
|
|
#define MB *( 1<<20)
|
|
|
|
#define GB *(1U<<30)
|
|
|
|
|
|
|
|
static size_t XXH_DEFAULT_SAMPLE_SIZE = 100 KB;
|
2016-04-07 23:50:06 +00:00
|
|
|
#define NBLOOPS 3 /* Default number of benchmark iterations */
|
|
|
|
#define TIMELOOP_S 1
|
2019-12-10 21:12:28 +00:00
|
|
|
#define TIMELOOP (TIMELOOP_S * CLOCKS_PER_SEC) /* target timing per iteration */
|
|
|
|
#define TIMELOOP_MIN (TIMELOOP / 2) /* minimum timing to validate a result */
|
2016-04-07 23:50:06 +00:00
|
|
|
#define XXHSUM32_DEFAULT_SEED 0 /* Default seed for algo_xxh32 */
|
|
|
|
#define XXHSUM64_DEFAULT_SEED 0 /* Default seed for algo_xxh64 */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
#define MAX_MEM (2 GB - 64 MB)
|
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
static const char stdinName[] = "-";
|
2019-09-28 00:40:36 +00:00
|
|
|
typedef enum { algo_xxh32, algo_xxh64, algo_xxh128 } algoType;
|
2016-01-10 16:06:34 +00:00
|
|
|
static const algoType g_defaultAlgo = algo_xxh64; /* required within main() & usage() */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-22 05:53:32 +00:00
|
|
|
/* <16 hex char> <SPC> <SPC> <filename> <'\0'>
|
|
|
|
* '4096' is typical Linux PATH_MAX configuration. */
|
|
|
|
#define DEFAULT_LINE_LENGTH (sizeof(XXH64_hash_t) * 2 + 2 + 4096 + 1)
|
2016-01-20 14:45:52 +00:00
|
|
|
|
2016-01-22 05:53:32 +00:00
|
|
|
/* Maximum acceptable line length. */
|
|
|
|
#define MAX_LINE_LENGTH (32 KB)
|
2016-01-20 14:45:52 +00:00
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2018-03-20 17:41:22 +00:00
|
|
|
* Display macros
|
|
|
|
**************************************/
|
2020-03-04 01:56:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define DISPLAY(...) fprintf_utf8(stderr, __VA_ARGS__)
|
|
|
|
#define DISPLAYRESULT(...) fprintf_utf8(stdout, __VA_ARGS__)
|
|
|
|
#else
|
2014-10-23 20:36:23 +00:00
|
|
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#define DISPLAYRESULT(...) fprintf(stdout, __VA_ARGS__)
|
2020-03-04 01:56:19 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-17 18:53:52 +00:00
|
|
|
#define DISPLAYLEVEL(l, ...) do { if (g_displayLevel>=l) DISPLAY(__VA_ARGS__); } while (0)
|
|
|
|
static int g_displayLevel = 2;
|
2014-10-18 11:03:38 +00:00
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2018-03-20 17:41:22 +00:00
|
|
|
* Local variables
|
|
|
|
**************************************/
|
2019-10-07 14:52:32 +00:00
|
|
|
static U32 g_nbIterations = NBLOOPS;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2018-03-20 17:41:22 +00:00
|
|
|
* Benchmark Functions
|
|
|
|
**************************************/
|
2016-04-07 23:50:06 +00:00
|
|
|
static clock_t BMK_clockSpan( clock_t start )
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2016-04-07 23:50:06 +00:00
|
|
|
return clock() - start; /* works even if overflow; Typical max span ~ 30 mn */
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-07 14:52:32 +00:00
|
|
|
static size_t BMK_findMaxMem(U64 requiredMem)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2016-05-28 00:14:28 +00:00
|
|
|
size_t const step = 64 MB;
|
2016-06-06 12:12:27 +00:00
|
|
|
void* testmem = NULL;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-06-28 11:20:23 +00:00
|
|
|
requiredMem = (((requiredMem >> 26) + 1) << 26);
|
|
|
|
requiredMem += 2*step;
|
|
|
|
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-05-28 00:14:28 +00:00
|
|
|
while (!testmem) {
|
2015-06-28 11:20:23 +00:00
|
|
|
if (requiredMem > step) requiredMem -= step;
|
|
|
|
else requiredMem >>= 1;
|
2016-06-06 12:12:27 +00:00
|
|
|
testmem = malloc ((size_t)requiredMem);
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
free (testmem);
|
|
|
|
|
2015-06-28 11:20:23 +00:00
|
|
|
/* keep some space available */
|
|
|
|
if (requiredMem > step) requiredMem -= step;
|
|
|
|
else requiredMem >>= 1;
|
|
|
|
|
|
|
|
return (size_t)requiredMem;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-07 14:52:32 +00:00
|
|
|
static U64 BMK_GetFileSize(const char* infilename)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
struct _stat64 statbuf;
|
|
|
|
r = _stat64(infilename, &statbuf);
|
|
|
|
#else
|
|
|
|
struct stat statbuf;
|
|
|
|
r = stat(infilename, &statbuf);
|
|
|
|
#endif
|
2015-01-01 16:10:04 +00:00
|
|
|
if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
|
2019-10-07 14:52:32 +00:00
|
|
|
return (U64)statbuf.st_size;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:02:48 +00:00
|
|
|
/*
|
|
|
|
* Allocates a string containing s1 and s2 concatenated. Acts like strdup.
|
|
|
|
* The result must be freed.
|
|
|
|
*/
|
|
|
|
static char* XXH_strcatDup(const char* s1, const char* s2)
|
|
|
|
{
|
|
|
|
assert(s1 != NULL);
|
|
|
|
assert(s2 != NULL);
|
|
|
|
{ size_t len1 = strlen(s1);
|
|
|
|
size_t len2 = strlen(s2);
|
|
|
|
char* buf = (char*)malloc(len1 + len2 + 1);
|
|
|
|
if (buf != NULL) {
|
|
|
|
/* strcpy(buf, s1) */
|
|
|
|
memcpy(buf, s1, len1);
|
|
|
|
/* strcat(buf, s2) */
|
|
|
|
memcpy(buf + len1, s2, len2 + 1);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 23:44:04 +00:00
|
|
|
|
|
|
|
|
2020-05-29 18:30:52 +00:00
|
|
|
/* use #define to make them constant, required for initialization */
|
|
|
|
#define PRIME32 2654435761U
|
|
|
|
#define PRIME64 11400714785074694797ULL
|
|
|
|
|
2020-03-09 23:44:04 +00:00
|
|
|
/*
|
|
|
|
* Fills a test buffer with pseudorandom data.
|
|
|
|
*
|
|
|
|
* This is used in the sanity check - its values must not be changed.
|
|
|
|
*/
|
|
|
|
static void BMK_fillTestBuffer(U8* buffer, size_t len)
|
|
|
|
{
|
|
|
|
U64 byteGen = PRIME32;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
assert(buffer != NULL);
|
|
|
|
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
buffer[i] = (U8)(byteGen>>56);
|
|
|
|
byteGen *= PRIME64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A secret buffer used for benchmarking XXH3's withSecret variants.
|
|
|
|
*
|
|
|
|
* In order for the bench to be realistic, the secret buffer would need to be
|
|
|
|
* pre-generated.
|
|
|
|
*
|
|
|
|
* Adding a pointer to the parameter list would be messy.
|
|
|
|
*/
|
|
|
|
static U8 g_benchSecretBuf[XXH3_SECRET_SIZE_MIN];
|
|
|
|
|
2020-03-09 23:02:48 +00:00
|
|
|
/*
|
|
|
|
* Wrappers for the benchmark.
|
|
|
|
*
|
|
|
|
* If you would like to add other hashes to the bench, create a wrapper and add
|
|
|
|
* it to the g_hashesToBench table. It will automatically be added.
|
|
|
|
*/
|
2019-10-07 14:52:32 +00:00
|
|
|
typedef U32 (*hashFunction)(const void* buffer, size_t bufferSize, U32 seed);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-03-09 23:02:48 +00:00
|
|
|
static U32 localXXH32(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
return XXH32(buffer, bufferSize, seed);
|
|
|
|
}
|
|
|
|
static U32 localXXH64(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
return (U32)XXH64(buffer, bufferSize, seed);
|
|
|
|
}
|
|
|
|
static U32 localXXH3_64b(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
(void)seed;
|
|
|
|
return (U32)XXH3_64bits(buffer, bufferSize);
|
|
|
|
}
|
|
|
|
static U32 localXXH3_64b_seeded(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
return (U32)XXH3_64bits_withSeed(buffer, bufferSize, seed);
|
|
|
|
}
|
2020-03-09 23:44:04 +00:00
|
|
|
static U32 localXXH3_64b_secret(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
(void)seed;
|
|
|
|
return (U32)XXH3_64bits_withSecret(buffer, bufferSize, g_benchSecretBuf, sizeof(g_benchSecretBuf));
|
|
|
|
}
|
2020-03-09 23:02:48 +00:00
|
|
|
static U32 localXXH3_128b(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
(void)seed;
|
|
|
|
return (U32)(XXH3_128bits(buffer, bufferSize).low64);
|
|
|
|
}
|
|
|
|
static U32 localXXH3_128b_seeded(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
return (U32)(XXH3_128bits_withSeed(buffer, bufferSize, seed).low64);
|
|
|
|
}
|
2020-03-09 23:44:04 +00:00
|
|
|
static U32 localXXH3_128b_secret(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
(void)seed;
|
|
|
|
return (U32)(XXH3_128bits_withSecret(buffer, bufferSize, g_benchSecretBuf, sizeof(g_benchSecretBuf)).low64);
|
|
|
|
}
|
2020-06-13 01:35:20 +00:00
|
|
|
static U32 localXXH3_stream(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
XXH3_state_t state;
|
|
|
|
(void)seed;
|
|
|
|
XXH3_64bits_reset(&state);
|
|
|
|
XXH3_64bits_update(&state, buffer, bufferSize);
|
|
|
|
return (U32)XXH3_64bits_digest(&state);
|
|
|
|
}
|
|
|
|
static U32 localXXH128_stream(const void* buffer, size_t bufferSize, U32 seed)
|
|
|
|
{
|
|
|
|
XXH3_state_t state;
|
|
|
|
(void)seed;
|
|
|
|
XXH3_128bits_reset(&state);
|
|
|
|
XXH3_128bits_update(&state, buffer, bufferSize);
|
|
|
|
return (U32)(XXH3_128bits_digest(&state).low64);
|
|
|
|
}
|
2020-03-09 23:44:04 +00:00
|
|
|
|
2015-08-17 14:17:12 +00:00
|
|
|
|
2020-03-09 23:02:48 +00:00
|
|
|
typedef struct {
|
|
|
|
const char* name;
|
|
|
|
hashFunction func;
|
|
|
|
} hashInfo;
|
2015-08-17 14:17:12 +00:00
|
|
|
|
2020-06-13 01:35:20 +00:00
|
|
|
#define NB_HASHFUNC 10
|
2020-06-12 21:52:46 +00:00
|
|
|
static const hashInfo g_hashesToBench[NB_HASHFUNC] = {
|
2020-03-10 01:26:25 +00:00
|
|
|
{ "XXH32", &localXXH32 },
|
|
|
|
{ "XXH64", &localXXH64 },
|
|
|
|
{ "XXH3_64b", &localXXH3_64b },
|
|
|
|
{ "XXH3_64b w/seed", &localXXH3_64b_seeded },
|
|
|
|
{ "XXH3_64b w/secret", &localXXH3_64b_secret },
|
|
|
|
{ "XXH128", &localXXH3_128b },
|
|
|
|
{ "XXH128 w/seed", &localXXH3_128b_seeded },
|
2020-06-13 01:35:20 +00:00
|
|
|
{ "XXH128 w/secret", &localXXH3_128b_secret },
|
|
|
|
{ "XXH3_stream", &localXXH3_stream },
|
|
|
|
{ "XXH128_stream", &localXXH128_stream },
|
2020-03-09 23:02:48 +00:00
|
|
|
};
|
2019-02-26 20:36:23 +00:00
|
|
|
|
2020-06-12 21:52:46 +00:00
|
|
|
#define NB_TESTFUNC (1 + 2 * NB_HASHFUNC)
|
|
|
|
static char g_testIDs[NB_TESTFUNC] = { 0 };
|
|
|
|
static const char k_testIDs_default[NB_TESTFUNC] = { 0,
|
|
|
|
1 /*XXH32*/, 0,
|
|
|
|
1 /*XXH64*/, 0,
|
|
|
|
1 /*XXH3*/, 0, 0, 0, 0, 0,
|
|
|
|
1 /*XXH128*/ };
|
2019-04-25 16:55:10 +00:00
|
|
|
|
2020-06-12 21:52:46 +00:00
|
|
|
#define HASHNAME_MAX 29
|
|
|
|
static void BMK_benchHash(hashFunction h, const char* hName, int testID,
|
2020-06-12 18:39:51 +00:00
|
|
|
const void* buffer, size_t bufferSize)
|
2015-05-07 12:30:27 +00:00
|
|
|
{
|
2020-06-12 18:39:51 +00:00
|
|
|
U32 nbh_perIteration = (U32)((300 MB) / (bufferSize+1)) + 1; /* first iteration conservatively aims for 300 MB/s */
|
2020-06-12 21:52:46 +00:00
|
|
|
unsigned iterationNb, nbIterations = g_nbIterations + !g_nbIterations /* min 1 */;
|
2015-08-17 14:17:12 +00:00
|
|
|
double fastestH = 100000000.;
|
2020-03-09 23:02:48 +00:00
|
|
|
assert(HASHNAME_MAX > 2);
|
|
|
|
DISPLAYLEVEL(2, "\r%80s\r", ""); /* Clean display line */
|
2020-06-12 21:52:46 +00:00
|
|
|
|
2020-06-12 19:25:15 +00:00
|
|
|
for (iterationNb = 1; iterationNb <= nbIterations; iterationNb++) {
|
2019-10-07 14:52:32 +00:00
|
|
|
U32 r=0;
|
2016-04-07 23:50:06 +00:00
|
|
|
clock_t cStart;
|
|
|
|
|
2020-06-12 18:39:51 +00:00
|
|
|
DISPLAYLEVEL(2, "%2u-%-*.*s : %10u ->\r",
|
2020-06-12 21:52:46 +00:00
|
|
|
iterationNb,
|
2020-06-12 18:39:51 +00:00
|
|
|
HASHNAME_MAX, HASHNAME_MAX, hName,
|
2020-03-09 23:02:48 +00:00
|
|
|
(unsigned)bufferSize);
|
2016-04-07 23:50:06 +00:00
|
|
|
cStart = clock();
|
|
|
|
while (clock() == cStart); /* starts clock() at its exact beginning */
|
|
|
|
cStart = clock();
|
|
|
|
|
2019-10-07 14:52:32 +00:00
|
|
|
{ U32 u;
|
2019-03-13 22:55:24 +00:00
|
|
|
for (u=0; u<nbh_perIteration; u++)
|
|
|
|
r += h(buffer, bufferSize, u);
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
2020-06-12 21:52:46 +00:00
|
|
|
if (r==0) DISPLAYLEVEL(3,".\r"); /* do something with r to defeat compiler "optimizing" hash away */
|
2019-03-13 22:55:24 +00:00
|
|
|
|
|
|
|
{ clock_t const nbTicks = BMK_clockSpan(cStart);
|
2019-12-10 21:12:28 +00:00
|
|
|
double const ticksPerHash = ((double)nbTicks / TIMELOOP) / nbh_perIteration;
|
2020-03-03 17:10:19 +00:00
|
|
|
/*
|
|
|
|
* clock() is the only decent portable timer, but it isn't very
|
|
|
|
* precise.
|
|
|
|
*
|
|
|
|
* Sometimes, this lack of precision is enough that the benchmark
|
|
|
|
* finishes before there are enough ticks to get a meaningful result.
|
|
|
|
*
|
|
|
|
* For example, on a Core 2 Duo (without any sort of Turbo Boost),
|
|
|
|
* the imprecise timer caused peculiar results like so:
|
|
|
|
*
|
|
|
|
* XXH3_64b 4800.0 MB/s // conveniently even
|
|
|
|
* XXH3_64b unaligned 4800.0 MB/s
|
|
|
|
* XXH3_64b seeded 9600.0 MB/s // magical 2x speedup?!
|
|
|
|
* XXH3_64b seeded unaligned 4800.0 MB/s
|
|
|
|
*
|
|
|
|
* If we sense a suspiciously low number of ticks, we increase the
|
|
|
|
* iterations until we can get something meaningful.
|
|
|
|
*/
|
2019-12-10 21:12:28 +00:00
|
|
|
if (nbTicks < TIMELOOP_MIN) {
|
2020-03-03 17:10:19 +00:00
|
|
|
/* Not enough time spent in benchmarking, risk of rounding bias */
|
2019-12-10 21:12:28 +00:00
|
|
|
if (nbTicks == 0) { /* faster than resolution timer */
|
|
|
|
nbh_perIteration *= 100;
|
|
|
|
} else {
|
2020-03-03 17:10:19 +00:00
|
|
|
/*
|
|
|
|
* update nbh_perIteration so that the next round lasts
|
|
|
|
* approximately 1 second.
|
|
|
|
*/
|
2019-12-10 21:12:28 +00:00
|
|
|
double nbh_perSecond = (1 / ticksPerHash) + 1;
|
|
|
|
if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20); /* avoid overflow */
|
|
|
|
nbh_perIteration = (U32)nbh_perSecond;
|
|
|
|
}
|
2020-06-12 19:25:15 +00:00
|
|
|
/* g_nbIterations==0 => quick evaluation, no claim of accuracy */
|
|
|
|
if (g_nbIterations>0) {
|
|
|
|
iterationNb--; /* new round for a more accurate speed evaluation */
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-13 22:55:24 +00:00
|
|
|
}
|
2019-12-10 21:12:28 +00:00
|
|
|
if (ticksPerHash < fastestH) fastestH = ticksPerHash;
|
2020-06-12 19:25:15 +00:00
|
|
|
if (fastestH>0.) { /* avoid div by zero */
|
|
|
|
DISPLAYLEVEL(2, "%2u-%-*.*s : %10u -> %8.0f it/s (%7.1f MB/s) \r",
|
2020-06-12 21:52:46 +00:00
|
|
|
iterationNb,
|
2020-06-12 18:39:51 +00:00
|
|
|
HASHNAME_MAX, HASHNAME_MAX, hName,
|
2020-03-09 23:02:48 +00:00
|
|
|
(unsigned)bufferSize,
|
2019-12-10 21:12:28 +00:00
|
|
|
(double)1 / fastestH,
|
2020-03-09 23:02:48 +00:00
|
|
|
((double)bufferSize / (1 MB)) / fastestH);
|
2020-06-12 19:25:15 +00:00
|
|
|
} }
|
2019-03-13 22:55:24 +00:00
|
|
|
{ double nbh_perSecond = (1 / fastestH) + 1;
|
2019-12-10 21:12:28 +00:00
|
|
|
if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20); /* avoid overflow */
|
2019-10-07 14:52:32 +00:00
|
|
|
nbh_perIteration = (U32)nbh_perSecond;
|
2019-03-13 22:55:24 +00:00
|
|
|
}
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
2020-06-12 18:39:51 +00:00
|
|
|
DISPLAYLEVEL(1, "%2i#%-*.*s : %10u -> %8.0f it/s (%7.1f MB/s) \n",
|
2020-06-12 21:52:46 +00:00
|
|
|
testID,
|
2020-03-09 23:02:48 +00:00
|
|
|
HASHNAME_MAX, HASHNAME_MAX, hName,
|
|
|
|
(unsigned)bufferSize,
|
2019-12-10 21:12:28 +00:00
|
|
|
(double)1 / fastestH,
|
2020-03-09 23:02:48 +00:00
|
|
|
((double)bufferSize / (1 MB)) / fastestH);
|
2018-02-19 01:54:58 +00:00
|
|
|
if (g_displayLevel<1)
|
2019-12-28 00:17:33 +00:00
|
|
|
DISPLAYLEVEL(0, "%u, ", (unsigned)((double)1 / fastestH));
|
2015-08-17 14:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*!
|
|
|
|
* BMK_benchMem():
|
2020-03-09 23:02:48 +00:00
|
|
|
* buffer: Must be 16-byte aligned.
|
2020-03-02 20:20:49 +00:00
|
|
|
* The real allocated size of buffer is supposed to be >= (bufferSize+3).
|
|
|
|
* returns: 0 on success, 1 if error (invalid mode selected)
|
|
|
|
*/
|
2020-06-12 21:52:46 +00:00
|
|
|
static void BMK_benchMem(const void* buffer, size_t bufferSize)
|
2015-08-17 14:17:12 +00:00
|
|
|
{
|
2020-03-09 23:02:48 +00:00
|
|
|
assert((((size_t)buffer) & 15) == 0); /* ensure alignment */
|
2020-06-12 21:52:46 +00:00
|
|
|
BMK_fillTestBuffer(g_benchSecretBuf, sizeof(g_benchSecretBuf));
|
|
|
|
{ int i;
|
|
|
|
for (i = 1; i < NB_TESTFUNC; i++) {
|
|
|
|
int const hashFuncID = (i-1) / 2;
|
|
|
|
assert(g_hashesToBench[hashFuncID].name != NULL);
|
|
|
|
if (g_testIDs[i] == 0) continue;
|
2020-03-09 23:02:48 +00:00
|
|
|
/* aligned */
|
2020-06-12 21:52:46 +00:00
|
|
|
if ((i % 2) == 1) {
|
|
|
|
BMK_benchHash(g_hashesToBench[hashFuncID].func, g_hashesToBench[hashFuncID].name, i, buffer, bufferSize);
|
2020-03-09 23:02:48 +00:00
|
|
|
}
|
|
|
|
/* unaligned */
|
2020-06-12 21:52:46 +00:00
|
|
|
if ((i % 2) == 0) {
|
2020-03-09 23:02:48 +00:00
|
|
|
/* Append "unaligned". */
|
2020-06-12 21:52:46 +00:00
|
|
|
char* const hashNameBuf = XXH_strcatDup(g_hashesToBench[hashFuncID].name, " unaligned");
|
2020-03-09 23:02:48 +00:00
|
|
|
assert(hashNameBuf != NULL);
|
2020-06-12 21:52:46 +00:00
|
|
|
BMK_benchHash(g_hashesToBench[hashFuncID].func, hashNameBuf, i, ((const char*)buffer)+3, bufferSize);
|
2020-03-09 23:02:48 +00:00
|
|
|
free(hashNameBuf);
|
|
|
|
}
|
2020-05-28 23:10:00 +00:00
|
|
|
} }
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-06 12:12:27 +00:00
|
|
|
static size_t BMK_selectBenchedSize(const char* fileName)
|
2020-05-28 23:10:00 +00:00
|
|
|
{
|
|
|
|
U64 const inFileSize = BMK_GetFileSize(fileName);
|
2016-06-06 12:12:27 +00:00
|
|
|
size_t benchedSize = (size_t) BMK_findMaxMem(inFileSize);
|
2019-10-07 14:52:32 +00:00
|
|
|
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
|
2016-06-06 12:12:27 +00:00
|
|
|
if (benchedSize < inFileSize) {
|
|
|
|
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", fileName, (int)(benchedSize>>20));
|
|
|
|
}
|
|
|
|
return benchedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-12 21:52:46 +00:00
|
|
|
static int BMK_benchFiles(char** fileNamesTable, int nbFiles)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2016-06-06 12:12:27 +00:00
|
|
|
int fileIdx;
|
|
|
|
for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
|
|
|
|
const char* const inFileName = fileNamesTable[fileIdx];
|
2018-09-17 19:28:59 +00:00
|
|
|
assert(inFileName != NULL);
|
2020-03-01 03:19:23 +00:00
|
|
|
|
|
|
|
{ FILE* const inFile = XXH_fopen( inFileName, "rb" );
|
2018-09-17 19:28:59 +00:00
|
|
|
size_t const benchedSize = BMK_selectBenchedSize(inFileName);
|
|
|
|
char* const buffer = (char*)calloc(benchedSize+16+3, 1);
|
|
|
|
void* const alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF); /* align on next 16 bytes */
|
|
|
|
|
|
|
|
/* Checks */
|
|
|
|
if (inFile==NULL){
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("Error: Could not open '%s': %s.\n", inFileName, strerror(errno));
|
2016-05-28 00:14:28 +00:00
|
|
|
free(buffer);
|
2020-06-12 21:52:46 +00:00
|
|
|
exit(11);
|
2018-09-17 19:28:59 +00:00
|
|
|
}
|
|
|
|
if(!buffer) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\nError: Out of memory.\n");
|
2018-09-17 19:28:59 +00:00
|
|
|
fclose(inFile);
|
2020-06-12 21:52:46 +00:00
|
|
|
exit(12);
|
2018-09-17 19:28:59 +00:00
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2018-09-17 19:28:59 +00:00
|
|
|
/* Fill input buffer */
|
|
|
|
{ size_t const readSize = fread(alignedBuffer, 1, benchedSize, inFile);
|
|
|
|
fclose(inFile);
|
|
|
|
if(readSize != benchedSize) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\nError: Could not read '%s': %s.\n", inFileName, strerror(errno));
|
2018-09-17 19:28:59 +00:00
|
|
|
free(buffer);
|
2020-06-12 21:52:46 +00:00
|
|
|
exit(13);
|
2018-09-17 19:28:59 +00:00
|
|
|
} }
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2018-09-17 19:28:59 +00:00
|
|
|
/* bench */
|
2020-06-12 21:52:46 +00:00
|
|
|
BMK_benchMem(alignedBuffer, benchedSize);
|
2018-09-17 19:28:59 +00:00
|
|
|
|
|
|
|
free(buffer);
|
2020-03-01 03:19:23 +00:00
|
|
|
} }
|
2020-06-12 21:52:46 +00:00
|
|
|
return 0;
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
|
2020-06-12 21:52:46 +00:00
|
|
|
static int BMK_benchInternal(size_t keySize)
|
2015-05-07 12:30:27 +00:00
|
|
|
{
|
2018-03-21 13:18:58 +00:00
|
|
|
void* const buffer = calloc(keySize+16+3, 1);
|
2020-06-12 21:52:46 +00:00
|
|
|
if (buffer == NULL) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\nError: Out of memory.\n");
|
2020-06-12 21:52:46 +00:00
|
|
|
exit(12);
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 20:36:23 +00:00
|
|
|
{ const void* const alignedBuffer = ((char*)buffer+15) - (((size_t)((char*)buffer+15)) & 0xF); /* align on next 16 bytes */
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2018-09-17 19:28:59 +00:00
|
|
|
/* bench */
|
|
|
|
DISPLAYLEVEL(1, "Sample of ");
|
|
|
|
if (keySize > 10 KB) {
|
2019-12-28 00:17:33 +00:00
|
|
|
DISPLAYLEVEL(1, "%u KB", (unsigned)(keySize >> 10));
|
2018-09-17 19:28:59 +00:00
|
|
|
} else {
|
2019-12-28 00:17:33 +00:00
|
|
|
DISPLAYLEVEL(1, "%u bytes", (unsigned)keySize);
|
2018-09-17 19:28:59 +00:00
|
|
|
}
|
|
|
|
DISPLAYLEVEL(1, "... \n");
|
|
|
|
|
2020-06-12 21:52:46 +00:00
|
|
|
BMK_benchMem(alignedBuffer, keySize);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
return 0;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-11 22:09:27 +00:00
|
|
|
/* ************************************************
|
2020-03-03 17:10:19 +00:00
|
|
|
* Self-test:
|
2019-03-11 22:09:27 +00:00
|
|
|
* ensure results consistency accross platforms
|
|
|
|
*********************************************** */
|
|
|
|
|
2019-03-13 21:44:41 +00:00
|
|
|
static void BMK_checkResult32(XXH32_hash_t r1, XXH32_hash_t r2)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
2019-03-11 22:09:27 +00:00
|
|
|
if (r1!=r2) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\rError: 32-bit hash test %i: Internal sanity check failed!\n", nbTests);
|
2020-03-02 20:20:49 +00:00
|
|
|
DISPLAY("\rGot 0x%08X, expected 0x%08X.\n", (unsigned)r1, (unsigned)r2);
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\rNote: If you modified the hash functions, make sure to either update the values\n"
|
|
|
|
"or temporarily comment out the tests in BMK_sanityCheck.\n");
|
2014-08-15 09:27:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
2019-03-13 21:44:41 +00:00
|
|
|
static void BMK_checkResult64(XXH64_hash_t r1, XXH64_hash_t r2)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (r1!=r2) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\rError: 64-bit hash test %i: Internal sanity check failed!\n", nbTests);
|
2020-03-02 20:20:49 +00:00
|
|
|
DISPLAY("\rGot 0x%08X%08XULL, expected 0x%08X%08XULL.\n",
|
2019-12-28 00:17:33 +00:00
|
|
|
(unsigned)(r1>>32), (unsigned)r1, (unsigned)(r2>>32), (unsigned)r2);
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\rNote: If you modified the hash functions, make sure to either update the values\n"
|
|
|
|
"or temporarily comment out the tests in BMK_sanityCheck.\n");
|
2014-08-15 09:27:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
2019-03-13 21:44:41 +00:00
|
|
|
static void BMK_checkResult128(XXH128_hash_t r1, XXH128_hash_t r2)
|
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
2019-03-14 20:08:38 +00:00
|
|
|
if ((r1.low64 != r2.low64) || (r1.high64 != r2.high64)) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\rError: 128-bit hash test %i: Internal sanity check failed.\n", nbTests);
|
2020-03-02 20:20:49 +00:00
|
|
|
DISPLAY("\rGot { 0x%08X%08XULL, 0x%08X%08XULL }, expected { 0x%08X%08XULL, 0x%08X%08XULL } \n",
|
2019-12-28 00:17:33 +00:00
|
|
|
(unsigned)(r1.low64>>32), (unsigned)r1.low64, (unsigned)(r1.high64>>32), (unsigned)r1.high64,
|
|
|
|
(unsigned)(r2.low64>>32), (unsigned)r2.low64, (unsigned)(r2.high64>>32), (unsigned)r2.high64 );
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("\rNote: If you modified the hash functions, make sure to either update the values\n"
|
|
|
|
"or temporarily comment out the tests in BMK_sanityCheck.\n");
|
2019-03-13 21:44:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2019-12-28 00:17:33 +00:00
|
|
|
static void BMK_testXXH32(const void* data, size_t len, U32 seed, U32 Nresult)
|
2019-06-14 18:51:44 +00:00
|
|
|
{
|
2020-03-09 00:48:56 +00:00
|
|
|
XXH32_state_t *state = XXH32_createState();
|
2019-06-14 18:51:44 +00:00
|
|
|
size_t pos;
|
|
|
|
|
2020-03-09 00:48:56 +00:00
|
|
|
assert(state != NULL);
|
2019-12-28 00:17:33 +00:00
|
|
|
if (len>0) assert(data != NULL);
|
|
|
|
|
|
|
|
BMK_checkResult32(XXH32(data, len, seed), Nresult);
|
2019-06-14 18:51:44 +00:00
|
|
|
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH32_reset(state, seed);
|
|
|
|
(void)XXH32_update(state, data, len);
|
|
|
|
BMK_checkResult32(XXH32_digest(state), Nresult);
|
2019-06-14 18:51:44 +00:00
|
|
|
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH32_reset(state, seed);
|
2019-06-14 18:51:44 +00:00
|
|
|
for (pos=0; pos<len; pos++)
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH32_update(state, ((const char*)data)+pos, 1);
|
|
|
|
BMK_checkResult32(XXH32_digest(state), Nresult);
|
|
|
|
XXH32_freeState(state);
|
2019-06-14 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 14:52:32 +00:00
|
|
|
static void BMK_testXXH64(const void* data, size_t len, U64 seed, U64 Nresult)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2020-03-09 00:48:56 +00:00
|
|
|
XXH64_state_t *state = XXH64_createState();
|
2016-08-10 23:32:10 +00:00
|
|
|
size_t pos;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-03-09 00:48:56 +00:00
|
|
|
assert(state != NULL);
|
2019-12-28 00:17:33 +00:00
|
|
|
if (len>0) assert(data != NULL);
|
|
|
|
|
2019-05-05 03:10:52 +00:00
|
|
|
BMK_checkResult64(XXH64(data, len, seed), Nresult);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH64_reset(state, seed);
|
|
|
|
(void)XXH64_update(state, data, len);
|
|
|
|
BMK_checkResult64(XXH64_digest(state), Nresult);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH64_reset(state, seed);
|
2018-02-17 18:53:52 +00:00
|
|
|
for (pos=0; pos<len; pos++)
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH64_update(state, ((const char*)data)+pos, 1);
|
|
|
|
BMK_checkResult64(XXH64_digest(state), Nresult);
|
|
|
|
XXH64_freeState(state);
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 17:49:51 +00:00
|
|
|
static U32 BMK_rand(void)
|
|
|
|
{
|
|
|
|
static U64 seed = PRIME32;
|
|
|
|
seed *= PRIME64;
|
|
|
|
return (U32)(seed >> 40);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-29 00:02:19 +00:00
|
|
|
void BMK_testXXH3(const void* data, size_t len, U64 seed, U64 Nresult)
|
2019-03-11 22:09:27 +00:00
|
|
|
{
|
2019-12-28 00:17:33 +00:00
|
|
|
if (len>0) assert(data != NULL);
|
|
|
|
|
2019-10-07 14:52:32 +00:00
|
|
|
{ U64 const Dresult = XXH3_64bits_withSeed(data, len, seed);
|
2019-03-11 22:09:27 +00:00
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that the no-seed variant produces same result as seed==0 */
|
|
|
|
if (seed == 0) {
|
2019-10-07 14:52:32 +00:00
|
|
|
U64 const Dresult = XXH3_64bits(data, len);
|
2019-03-11 22:09:27 +00:00
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
}
|
2019-05-05 03:10:52 +00:00
|
|
|
|
|
|
|
/* streaming API test */
|
2020-05-28 23:10:00 +00:00
|
|
|
{ XXH3_state_t* const state = XXH3_createState();
|
2020-03-09 00:48:56 +00:00
|
|
|
assert(state != NULL);
|
2019-07-03 21:23:43 +00:00
|
|
|
/* single ingestion */
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_64bits_reset_withSeed(state, seed);
|
|
|
|
(void)XXH3_64bits_update(state, data, len);
|
|
|
|
BMK_checkResult64(XXH3_64bits_digest(state), Nresult);
|
2019-05-05 03:10:52 +00:00
|
|
|
|
2020-05-28 23:10:00 +00:00
|
|
|
/* random ingestion */
|
|
|
|
{ size_t p = 0;
|
|
|
|
(void)XXH3_64bits_reset_withSeed(state, seed);
|
|
|
|
while (p < len) {
|
|
|
|
size_t const modulo = len > 2 ? len : 2;
|
2020-05-29 17:49:51 +00:00
|
|
|
size_t l = (size_t)(BMK_rand()) % modulo;
|
2020-05-28 23:10:00 +00:00
|
|
|
if (p + l > len) l = len - p;
|
|
|
|
(void)XXH3_64bits_update(state, (const char*)data+p, l);
|
|
|
|
p += l;
|
|
|
|
}
|
|
|
|
BMK_checkResult64(XXH3_64bits_digest(state), Nresult);
|
|
|
|
}
|
|
|
|
|
2019-05-05 03:10:52 +00:00
|
|
|
/* byte by byte ingestion */
|
|
|
|
{ size_t pos;
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_64bits_reset_withSeed(state, seed);
|
2019-05-05 03:10:52 +00:00
|
|
|
for (pos=0; pos<len; pos++)
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_64bits_update(state, ((const char*)data)+pos, 1);
|
|
|
|
BMK_checkResult64(XXH3_64bits_digest(state), Nresult);
|
|
|
|
}
|
|
|
|
XXH3_freeState(state);
|
|
|
|
}
|
2019-06-14 19:26:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 00:08:36 +00:00
|
|
|
void BMK_testXXH3_withSecret(const void* data, size_t len, const void* secret, size_t secretSize, U64 Nresult)
|
2019-06-14 19:26:33 +00:00
|
|
|
{
|
2019-12-28 00:17:33 +00:00
|
|
|
if (len>0) assert(data != NULL);
|
|
|
|
|
2019-10-07 14:52:32 +00:00
|
|
|
{ U64 const Dresult = XXH3_64bits_withSecret(data, len, secret, secretSize);
|
2019-06-14 19:26:33 +00:00
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* streaming API test */
|
2020-03-09 00:48:56 +00:00
|
|
|
{ XXH3_state_t *state = XXH3_createState();
|
|
|
|
assert(state != NULL);
|
|
|
|
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize);
|
|
|
|
(void)XXH3_64bits_update(state, data, len);
|
|
|
|
BMK_checkResult64(XXH3_64bits_digest(state), Nresult);
|
2019-06-14 19:26:33 +00:00
|
|
|
|
2020-05-29 17:49:51 +00:00
|
|
|
/* random ingestion */
|
|
|
|
{ size_t p = 0;
|
|
|
|
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize);
|
|
|
|
while (p < len) {
|
|
|
|
size_t const modulo = len > 2 ? len : 2;
|
|
|
|
size_t l = (size_t)(BMK_rand()) % modulo;
|
|
|
|
if (p + l > len) l = len - p;
|
|
|
|
(void)XXH3_64bits_update(state, (const char*)data+p, l);
|
|
|
|
p += l;
|
|
|
|
}
|
|
|
|
BMK_checkResult64(XXH3_64bits_digest(state), Nresult);
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:26:33 +00:00
|
|
|
/* byte by byte ingestion */
|
|
|
|
{ size_t pos;
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize);
|
2019-06-14 19:26:33 +00:00
|
|
|
for (pos=0; pos<len; pos++)
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_64bits_update(state, ((const char*)data)+pos, 1);
|
|
|
|
BMK_checkResult64(XXH3_64bits_digest(state), Nresult);
|
|
|
|
}
|
|
|
|
XXH3_freeState(state);
|
|
|
|
}
|
2019-03-11 22:09:27 +00:00
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2019-10-07 14:52:32 +00:00
|
|
|
void BMK_testXXH128(const void* data, size_t len, U64 seed, XXH128_hash_t Nresult)
|
2019-03-13 21:44:41 +00:00
|
|
|
{
|
|
|
|
{ XXH128_hash_t const Dresult = XXH3_128bits_withSeed(data, len, seed);
|
|
|
|
BMK_checkResult128(Dresult, Nresult);
|
2019-07-20 00:53:04 +00:00
|
|
|
}
|
2019-03-13 21:44:41 +00:00
|
|
|
|
2019-07-20 00:53:04 +00:00
|
|
|
/* check that XXH128() is identical to XXH3_128bits_withSeed() */
|
|
|
|
{ XXH128_hash_t const Dresult2 = XXH128(data, len, seed);
|
|
|
|
BMK_checkResult128(Dresult2, Nresult);
|
2019-03-17 04:27:39 +00:00
|
|
|
}
|
2019-03-13 21:44:41 +00:00
|
|
|
|
|
|
|
/* check that the no-seed variant produces same result as seed==0 */
|
|
|
|
if (seed == 0) {
|
|
|
|
XXH128_hash_t const Dresult = XXH3_128bits(data, len);
|
|
|
|
BMK_checkResult128(Dresult, Nresult);
|
|
|
|
}
|
2019-07-20 00:53:04 +00:00
|
|
|
|
|
|
|
/* streaming API test */
|
2020-03-09 00:48:56 +00:00
|
|
|
{ XXH3_state_t *state = XXH3_createState();
|
|
|
|
assert(state != NULL);
|
2019-07-20 00:53:04 +00:00
|
|
|
|
|
|
|
/* single ingestion */
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_128bits_reset_withSeed(state, seed);
|
|
|
|
(void)XXH3_128bits_update(state, data, len);
|
|
|
|
BMK_checkResult128(XXH3_128bits_digest(state), Nresult);
|
2019-07-20 00:53:04 +00:00
|
|
|
|
2020-05-29 17:49:51 +00:00
|
|
|
/* random ingestion */
|
|
|
|
{ size_t p = 0;
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_128bits_reset_withSeed(state, seed);
|
2020-05-29 17:49:51 +00:00
|
|
|
while (p < len) {
|
|
|
|
size_t const modulo = len > 2 ? len : 2;
|
|
|
|
size_t l = (size_t)(BMK_rand()) % modulo;
|
|
|
|
if (p + l > len) l = len - p;
|
|
|
|
(void)XXH3_128bits_update(state, (const char*)data+p, l);
|
|
|
|
p += l;
|
|
|
|
}
|
2020-03-09 00:48:56 +00:00
|
|
|
BMK_checkResult128(XXH3_128bits_digest(state), Nresult);
|
2019-07-20 00:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* byte by byte ingestion */
|
|
|
|
{ size_t pos;
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_128bits_reset_withSeed(state, seed);
|
2019-07-20 00:53:04 +00:00
|
|
|
for (pos=0; pos<len; pos++)
|
2020-03-09 00:48:56 +00:00
|
|
|
(void)XXH3_128bits_update(state, ((const char*)data)+pos, 1);
|
|
|
|
BMK_checkResult128(XXH3_128bits_digest(state), Nresult);
|
|
|
|
}
|
|
|
|
XXH3_freeState(state);
|
|
|
|
}
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:12:47 +00:00
|
|
|
#define SECRET_SAMPLE_NBBYTES 4
|
|
|
|
typedef struct { U8 byte[SECRET_SAMPLE_NBBYTES]; } verifSample_t;
|
|
|
|
|
|
|
|
void BMK_testSecretGenerator(const void* customSeed, size_t len, verifSample_t result)
|
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
|
|
|
const int sampleIndex[SECRET_SAMPLE_NBBYTES] = { 0, 62, 131, 191};
|
|
|
|
U8 secretBuffer[XXH3_SECRET_DEFAULT_SIZE] = {0};
|
|
|
|
verifSample_t samples;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
XXH3_generateSecret(secretBuffer, customSeed, len);
|
|
|
|
for (i=0; i<SECRET_SAMPLE_NBBYTES; i++) {
|
|
|
|
samples.byte[i] = secretBuffer[sampleIndex[i]];
|
|
|
|
}
|
|
|
|
if (memcmp(&samples, &result, sizeof(result))) {
|
|
|
|
DISPLAY("\rError: Secret generation test %i: Internal sanity check failed. \n", nbTests);
|
|
|
|
DISPLAY("\rGot { 0x%02X, 0x%02X, 0x%02X, 0x%02X }, expected { 0x%02X, 0x%02X, 0x%02X, 0x%02X } \n",
|
|
|
|
samples.byte[0], samples.byte[1], samples.byte[2], samples.byte[3],
|
|
|
|
result.byte[0], result.byte[1], result.byte[2], result.byte[3] );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* BMK_sanityCheck():
|
|
|
|
* Runs a sanity check before the benchmark.
|
|
|
|
*
|
|
|
|
* Exits on an incorrect output.
|
|
|
|
*/
|
2014-08-15 09:27:04 +00:00
|
|
|
static void BMK_sanityCheck(void)
|
|
|
|
{
|
2020-06-08 19:12:47 +00:00
|
|
|
#define SANITY_BUFFER_SIZE 2367
|
2019-10-07 14:52:32 +00:00
|
|
|
U8 sanityBuffer[SANITY_BUFFER_SIZE];
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_fillTestBuffer(sanityBuffer, sizeof(sanityBuffer));
|
|
|
|
|
|
|
|
BMK_testXXH32(NULL, 0, 0, 0x02CC5D05);
|
|
|
|
BMK_testXXH32(NULL, 0, PRIME32, 0x36B78AE7);
|
|
|
|
BMK_testXXH32(sanityBuffer, 1, 0, 0xCF65B03E);
|
|
|
|
BMK_testXXH32(sanityBuffer, 1, PRIME32, 0xB4545AA4);
|
|
|
|
BMK_testXXH32(sanityBuffer, 14, 0, 0x1208E7E2);
|
|
|
|
BMK_testXXH32(sanityBuffer, 14, PRIME32, 0x6AF1D1FE);
|
|
|
|
BMK_testXXH32(sanityBuffer,222, 0, 0x5BD11DBD);
|
|
|
|
BMK_testXXH32(sanityBuffer,222, PRIME32, 0x58803C5F);
|
|
|
|
|
|
|
|
BMK_testXXH64(NULL , 0, 0, 0xEF46DB3751D8E999ULL);
|
|
|
|
BMK_testXXH64(NULL , 0, PRIME32, 0xAC75FDA2929B17EFULL);
|
|
|
|
BMK_testXXH64(sanityBuffer, 1, 0, 0xE934A84ADB052768ULL);
|
|
|
|
BMK_testXXH64(sanityBuffer, 1, PRIME32, 0x5014607643A9B4C3ULL);
|
|
|
|
BMK_testXXH64(sanityBuffer, 4, 0, 0x9136A0DCA57457EEULL);
|
|
|
|
BMK_testXXH64(sanityBuffer, 14, 0, 0x8282DCC4994E35C8ULL);
|
|
|
|
BMK_testXXH64(sanityBuffer, 14, PRIME32, 0xC3BD6BF63DEB6DF0ULL);
|
|
|
|
BMK_testXXH64(sanityBuffer,222, 0, 0xB641AE8CB691C174ULL);
|
|
|
|
BMK_testXXH64(sanityBuffer,222, PRIME32, 0x20CB8AB7AE10C14AULL);
|
2019-03-11 22:09:27 +00:00
|
|
|
|
2020-03-01 03:19:23 +00:00
|
|
|
BMK_testXXH3(NULL, 0, 0, 0x776EDDFB6BFD9195ULL); /* empty string */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(NULL, 0, PRIME64, 0x6AFCE90814C488CBULL);
|
2020-03-01 03:19:23 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 1, 0, 0xB936EBAE24CB01C5ULL); /* 1 - 3 */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 1, PRIME64, 0xF541B1905037FC39ULL); /* 1 - 3 */
|
2020-03-03 01:04:11 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 6, 0, 0x27B56A84CD2D7325ULL); /* 4 - 8 */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 6, PRIME64, 0x84589C116AB59AB9ULL); /* 4 - 8 */
|
2020-03-01 03:19:23 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 12, 0, 0xA713DAF0DFBB77E7ULL); /* 9 - 16 */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 12, PRIME64, 0xE7303E1B2336DE0EULL); /* 9 - 16 */
|
2020-02-14 05:38:29 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 24, 0, 0xA3FE70BF9D3510EBULL); /* 17 - 32 */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 24, PRIME64, 0x850E80FC35BDD690ULL); /* 17 - 32 */
|
2020-02-14 05:38:29 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 48, 0, 0x397DA259ECBA1F11ULL); /* 33 - 64 */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 48, PRIME64, 0xADC2CBAA44ACC616ULL); /* 33 - 64 */
|
2020-02-14 05:38:29 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 80, 0, 0xBCDEFBBB2C47C90AULL); /* 65 - 96 */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 80, PRIME64, 0xC6DD0CB699532E73ULL); /* 65 - 96 */
|
2020-02-14 05:38:29 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 195, 0, 0xCD94217EE362EC3AULL); /* 129-240 */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 195, PRIME64, 0xBA68003D370CB3D9ULL); /* 129-240 */
|
2020-02-14 05:38:29 +00:00
|
|
|
|
|
|
|
BMK_testXXH3(sanityBuffer, 403, 0, 0x1B2AFF3B46C74648ULL); /* one block, last stripe is overlapping */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 403, PRIME64, 0xB654F6FFF42AD787ULL); /* one block, last stripe is overlapping */
|
2020-02-14 05:38:29 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 512, 0, 0x43E368661808A9E8ULL); /* one block, finishing at stripe boundary */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer, 512, PRIME64, 0x3A865148E584E5B9ULL); /* one block, finishing at stripe boundary */
|
2020-02-14 05:38:29 +00:00
|
|
|
BMK_testXXH3(sanityBuffer,2048, 0, 0xC7169244BBDA8BD4ULL); /* 2 blocks, finishing at block boundary */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer,2048, PRIME64, 0x74BF9A802BBDFBAEULL); /* 2 blocks, finishing at block boundary */
|
2020-02-14 05:38:29 +00:00
|
|
|
BMK_testXXH3(sanityBuffer,2240, 0, 0x30FEB637E114C0C7ULL); /* 3 blocks, finishing at stripe boundary */
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH3(sanityBuffer,2240, PRIME64, 0xEEF78A36185EB61FULL); /* 3 blocks, finishing at stripe boundary */
|
2020-05-28 23:10:00 +00:00
|
|
|
BMK_testXXH3(sanityBuffer,2367, 0, 0x2EB8FEEDD2D1EF5DULL); /* 3 blocks, last stripe is overlapping */
|
|
|
|
BMK_testXXH3(sanityBuffer,2367, PRIME64, 0xCE1A757AD2D25057ULL); /* 3 blocks, last stripe is overlapping */
|
2019-06-14 18:51:44 +00:00
|
|
|
|
2020-06-08 19:12:47 +00:00
|
|
|
/* XXH3 with Custom Secret */
|
2019-06-14 19:26:33 +00:00
|
|
|
{ const void* const secret = sanityBuffer + 7;
|
2019-06-17 21:16:52 +00:00
|
|
|
const size_t secretSize = XXH3_SECRET_SIZE_MIN + 11;
|
2020-06-18 19:37:03 +00:00
|
|
|
assert(sizeof(sanityBuffer) >= 7 + secretSize);
|
2020-03-01 03:19:23 +00:00
|
|
|
BMK_testXXH3_withSecret(NULL, 0, secret, secretSize, 0x6775FD10343C92C3ULL); /* empty string */
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 1, secret, secretSize, 0xC3382C326E24E3CDULL); /* 1 - 3 */
|
2020-03-03 01:04:11 +00:00
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 6, secret, secretSize, 0x82C90AB0519369ADULL); /* 4 - 8 */
|
2020-03-01 03:19:23 +00:00
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 12, secret, secretSize, 0x14631E773B78EC57ULL); /* 9 - 16 */
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 24, secret, secretSize, 0xCDD5542E4A9D9FE8ULL); /* 17 - 32 */
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 48, secret, secretSize, 0x33ABD54D094B2534ULL); /* 33 - 64 */
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 80, secret, secretSize, 0xE687BA1684965297ULL); /* 65 - 96 */
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 195, secret, secretSize, 0xA057273F5EECFB20ULL); /* 129-240 */
|
|
|
|
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 403, secret, secretSize, 0xF9C0BA5BA3AF70B8ULL); /* one block, last stripe is overlapping */
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer, 512, secret, secretSize, 0x7896E65DCFA09071ULL); /* one block, finishing at stripe boundary */
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer,2048, secret, secretSize, 0xD6545DB87ECFD98CULL); /* >= 2 blocks, at least one scrambling */
|
2020-05-28 23:10:00 +00:00
|
|
|
BMK_testXXH3_withSecret(sanityBuffer,2367, secret, secretSize, 0x857320340D953686ULL); /* >= 2 blocks, at least one scrambling, last stripe unaligned */
|
2020-06-18 19:37:03 +00:00
|
|
|
|
|
|
|
BMK_testXXH3_withSecret(sanityBuffer,64*10*3, secret, secretSize, 0xD4989A002E9850ABULL); /* exactly 3 full blocks, not a multiple of 256 */
|
2020-03-01 03:19:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:12:47 +00:00
|
|
|
/* XXH128 */
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x1F17545BCE1061F1ULL, 0x07FD4E968E916AE1ULL };
|
2020-02-12 23:22:13 +00:00
|
|
|
BMK_testXXH128(NULL, 0, 0, expected); /* empty string */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x7282E631387D51ACULL, 0x8743B0A8131AB9E6ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(NULL, 0, PRIME32, expected);
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xB936EBAE24CB01C5ULL, 0x2554B05763A71A05ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 1, 0, expected); /* 1-3 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xCA57C628C04B45B8ULL, 0x916831F4DCD21CF9ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 1, PRIME32, expected); /* 1-3 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-04 20:32:18 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x3E7039BDDA43CFC6ULL, 0x082AFE0B8162D12AULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 6, 0, expected); /* 4-8 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-04 20:32:18 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x269D8F70BE98856EULL, 0x5A865B5389ABD2B1ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 6, PRIME32, expected); /* 4-8 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-04 20:32:18 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x061A192713F69AD9ULL, 0x6E3EFD8FC7802B18ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 12, 0, expected); /* 9-16 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-04 20:32:18 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x9BE9F9A67F3C7DFBULL, 0xD7E09D518A3405D3ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 12, PRIME32, expected); /* 9-16 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x1E7044D28B1B901DULL, 0x0CE966E4678D3761ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 24, 0, expected); /* 17-32 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xD7304C54EBAD40A9ULL, 0x3162026714A6A243ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 24, PRIME32, expected); /* 17-32 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xF942219AED80F67BULL, 0xA002AC4E5478227EULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 48, 0, expected); /* 33-64 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x7BA3C3E453A1934EULL, 0x163ADDE36C072295ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 48, PRIME32, expected); /* 33-64 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x5E8BAFB9F95FB803ULL, 0x4952F58181AB0042ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 81, 0, expected); /* 65-96 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x703FBB3D7A5F755CULL, 0x2724EC7ADC750FB6ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 81, PRIME32, expected); /* 65-96 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xF1AEBD597CEC6B3AULL, 0x337E09641B948717ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 222, 0, expected); /* 129-240 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xAE995BB8AF917A8DULL, 0x91820016621E97F1ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 222, PRIME32, expected); /* 129-240 */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xCDEB804D65C6DEA4ULL, 0x1B6DE21E332DD73DULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 403, 0, expected); /* one block, last stripe is overlapping */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x6259F6ECFD6443FDULL, 0xBED311971E0BE8F2ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 403, PRIME64, expected); /* one block, last stripe is overlapping */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x1443B8153EBEE367ULL, 0x98EC7E48CD872997ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 512, 0, expected); /* one block, finishing at stripe boundary */
|
2019-07-20 00:53:04 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x43FDC6823A52F1F2ULL, 0x2F748A4F194E1EF0ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer, 512, PRIME64, expected); /* one block, finishing at stripe boundary */
|
2019-07-20 00:53:04 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xF4258501BE8E0623ULL, 0x6930A2267A755B20ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer,2048, 0, expected); /* two blocks, finishing at block boundary */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x10CC56C2FA0AD9ACULL, 0xD0D7A3C2EEF2D892ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer,2048, PRIME32, expected); /* two blocks, finishing at block boundary */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x5890AE7ACBB84A7EULL, 0x85C327B377AA7E62ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer,2240, 0, expected); /* two blocks, ends at stripe boundary */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-03-01 03:19:23 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x205E6D72DCCBD2AAULL, 0x62B70214DB075235ULL };
|
2020-03-09 23:44:04 +00:00
|
|
|
BMK_testXXH128(sanityBuffer,2240, PRIME32, expected); /* two blocks, ends at stripe boundary */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-05-28 23:10:00 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0xCB37AEB9E5D361EDULL, 0xE89C0F6FF369B427ULL };
|
|
|
|
BMK_testXXH128(sanityBuffer,2367, 0, expected); /* two blocks, last stripe is overlapping */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
2020-05-28 23:10:00 +00:00
|
|
|
{ XXH128_hash_t const expected = { 0x6F5360AE69C2F406ULL, 0xD23AAE4B76C31ECBULL };
|
|
|
|
BMK_testXXH128(sanityBuffer,2367, PRIME32, expected); /* two blocks, last stripe is overlapping */
|
2019-03-13 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:12:47 +00:00
|
|
|
|
|
|
|
/* secret generator */
|
|
|
|
{ verifSample_t const expected = { { 0xB8, 0x26, 0x83, 0x7E } };
|
|
|
|
BMK_testSecretGenerator(NULL, 0, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ verifSample_t const expected = { { 0x25, 0x82, 0x62, 0x9A } };
|
|
|
|
BMK_testSecretGenerator(sanityBuffer, 1, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ verifSample_t const expected = { { 0xDA, 0x2A, 0x12, 0x11 } };
|
|
|
|
BMK_testSecretGenerator(sanityBuffer, XXH3_SECRET_SIZE_MIN - 1, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ verifSample_t const expected = { { 0x7E, 0x48, 0x0C, 0xA7 } };
|
|
|
|
BMK_testSecretGenerator(sanityBuffer, XXH3_SECRET_DEFAULT_SIZE + 500, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-17 18:53:52 +00:00
|
|
|
DISPLAYLEVEL(3, "\r%70s\r", ""); /* Clean display line */
|
|
|
|
DISPLAYLEVEL(3, "Sanity check -- all tests ok\n");
|
2014-09-25 20:22:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2016-01-11 00:31:04 +00:00
|
|
|
/* ********************************************************
|
|
|
|
* File Hashing
|
|
|
|
**********************************************************/
|
2020-06-18 21:24:27 +00:00
|
|
|
typedef void (*XSUM_displayHash_f)(const void*, size_t); /* display function signature */
|
2016-01-11 00:31:04 +00:00
|
|
|
|
2020-06-18 21:24:27 +00:00
|
|
|
/* for support of --little-endian display mode */
|
2016-01-10 16:06:34 +00:00
|
|
|
static void BMK_display_LittleEndian(const void* ptr, size_t length)
|
|
|
|
{
|
2020-06-18 21:24:27 +00:00
|
|
|
const U8* const p = (const U8*)ptr;
|
2016-08-11 17:07:23 +00:00
|
|
|
size_t idx;
|
|
|
|
for (idx=length-1; idx<length; idx--) /* intentional underflow to negative to detect end */
|
|
|
|
DISPLAYRESULT("%02x", p[idx]);
|
2016-01-10 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:42:33 +00:00
|
|
|
static void BMK_display_BigEndian(const void* ptr, size_t length)
|
|
|
|
{
|
2020-06-18 21:24:27 +00:00
|
|
|
const U8* const p = (const U8*)ptr;
|
2016-08-11 17:07:23 +00:00
|
|
|
size_t idx;
|
|
|
|
for (idx=0; idx<length; idx++)
|
|
|
|
DISPLAYRESULT("%02x", p[idx]);
|
2014-12-09 17:42:33 +00:00
|
|
|
}
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2019-09-28 21:27:32 +00:00
|
|
|
typedef union {
|
|
|
|
XXH32_hash_t xxh32;
|
|
|
|
XXH64_hash_t xxh64;
|
|
|
|
XXH128_hash_t xxh128;
|
|
|
|
} Multihash;
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*
|
|
|
|
* BMK_hashStream:
|
|
|
|
* Reads data from `inFile`, generating an incremental hash of type hashType,
|
|
|
|
* using `buffer` of size `blockSize` for temporary storage.
|
|
|
|
*/
|
2019-09-28 21:27:32 +00:00
|
|
|
static Multihash
|
|
|
|
BMK_hashStream(FILE* inFile,
|
|
|
|
algoType hashType,
|
|
|
|
void* buffer, size_t blockSize)
|
2016-01-20 14:06:35 +00:00
|
|
|
{
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_state_t state32;
|
2019-09-28 00:40:36 +00:00
|
|
|
XXH64_state_t state64;
|
|
|
|
XXH3_state_t state128;
|
2016-01-20 14:06:35 +00:00
|
|
|
|
|
|
|
/* Init */
|
2018-09-17 19:39:18 +00:00
|
|
|
(void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
|
|
|
|
(void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
|
2019-09-28 00:40:36 +00:00
|
|
|
(void)XXH3_128bits_reset(&state128);
|
2016-01-20 14:06:35 +00:00
|
|
|
|
|
|
|
/* Load file & update hash */
|
2020-05-29 19:45:26 +00:00
|
|
|
{ size_t readSize;
|
|
|
|
while ((readSize = fread(buffer, 1, blockSize, inFile)) > 0) {
|
2019-09-28 21:27:32 +00:00
|
|
|
switch(hashType)
|
|
|
|
{
|
|
|
|
case algo_xxh32:
|
|
|
|
(void)XXH32_update(&state32, buffer, readSize);
|
|
|
|
break;
|
|
|
|
case algo_xxh64:
|
|
|
|
(void)XXH64_update(&state64, buffer, readSize);
|
|
|
|
break;
|
|
|
|
case algo_xxh128:
|
|
|
|
(void)XXH3_128bits_update(&state128, buffer, readSize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2020-05-29 19:45:26 +00:00
|
|
|
}
|
|
|
|
if (ferror(inFile)) {
|
|
|
|
DISPLAY("Error: a failure occurred reading the input file.\n");
|
|
|
|
exit(1);
|
2019-09-28 21:27:32 +00:00
|
|
|
} }
|
|
|
|
|
|
|
|
{ Multihash finalHash;
|
2016-01-20 14:06:35 +00:00
|
|
|
switch(hashType)
|
|
|
|
{
|
|
|
|
case algo_xxh32:
|
2019-09-28 21:27:32 +00:00
|
|
|
finalHash.xxh32 = XXH32_digest(&state32);
|
2016-01-20 14:06:35 +00:00
|
|
|
break;
|
|
|
|
case algo_xxh64:
|
2019-09-28 21:27:32 +00:00
|
|
|
finalHash.xxh64 = XXH64_digest(&state64);
|
2016-01-20 14:06:35 +00:00
|
|
|
break;
|
2019-09-28 00:40:36 +00:00
|
|
|
case algo_xxh128:
|
2019-09-28 21:27:32 +00:00
|
|
|
finalHash.xxh128 = XXH3_128bits_digest(&state128);
|
2019-09-28 00:40:36 +00:00
|
|
|
break;
|
2016-01-20 14:06:35 +00:00
|
|
|
default:
|
2019-09-28 00:50:02 +00:00
|
|
|
assert(0);
|
2016-01-20 14:06:35 +00:00
|
|
|
}
|
2019-09-28 21:27:32 +00:00
|
|
|
return finalHash;
|
2016-01-20 14:06:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:01:39 +00:00
|
|
|
|
2016-01-10 16:06:34 +00:00
|
|
|
typedef enum { big_endian, little_endian} endianess;
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2016-01-10 16:06:34 +00:00
|
|
|
static int BMK_hash(const char* fileName,
|
|
|
|
const algoType hashType,
|
|
|
|
const endianess displayEndianess)
|
2014-09-25 20:22:59 +00:00
|
|
|
{
|
2020-06-18 21:24:27 +00:00
|
|
|
FILE* inFile;
|
2014-09-25 20:22:59 +00:00
|
|
|
size_t const blockSize = 64 KB;
|
2019-09-28 21:27:32 +00:00
|
|
|
Multihash hashValue;
|
2020-06-18 21:24:27 +00:00
|
|
|
XSUM_displayHash_f const f_displayHash = (displayEndianess==big_endian) ?
|
|
|
|
BMK_display_BigEndian : BMK_display_LittleEndian;
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Check file existence */
|
2016-04-07 23:19:06 +00:00
|
|
|
if (fileName == stdinName) {
|
2014-10-29 12:55:58 +00:00
|
|
|
inFile = stdin;
|
2019-03-15 15:56:58 +00:00
|
|
|
fileName = "stdin";
|
2014-10-29 12:55:58 +00:00
|
|
|
SET_BINARY_MODE(stdin);
|
2019-10-07 15:25:57 +00:00
|
|
|
} else {
|
2020-02-13 01:37:34 +00:00
|
|
|
inFile = XXH_fopen( fileName, "rb" );
|
2019-10-07 15:25:57 +00:00
|
|
|
}
|
2016-04-07 23:19:06 +00:00
|
|
|
if (inFile==NULL) {
|
2019-10-07 15:25:57 +00:00
|
|
|
DISPLAY("Error: Could not open '%s': %s. \n", fileName, strerror(errno));
|
2016-01-10 16:06:34 +00:00
|
|
|
return 1;
|
2014-09-25 20:22:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Memory allocation & restrictions */
|
2020-06-18 21:24:27 +00:00
|
|
|
{ void* const buffer = malloc(blockSize);
|
|
|
|
if(!buffer) {
|
|
|
|
DISPLAY("\nError: Out of memory.\n");
|
|
|
|
fclose(inFile);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2020-06-18 21:24:27 +00:00
|
|
|
/* Load file & update hash */
|
|
|
|
hashValue = BMK_hashStream(inFile, hashType, buffer, blockSize);
|
2016-01-20 14:06:35 +00:00
|
|
|
|
2020-06-18 21:24:27 +00:00
|
|
|
fclose(inFile);
|
|
|
|
free(buffer);
|
|
|
|
}
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2019-09-28 21:27:32 +00:00
|
|
|
/* display Hash value followed by file name */
|
2016-01-10 16:06:34 +00:00
|
|
|
switch(hashType)
|
2014-09-25 20:22:59 +00:00
|
|
|
{
|
2016-01-10 16:06:34 +00:00
|
|
|
case algo_xxh32:
|
2016-04-07 23:19:06 +00:00
|
|
|
{ XXH32_canonical_t hcbe32;
|
2019-09-28 21:27:32 +00:00
|
|
|
(void)XXH32_canonicalFromHash(&hcbe32, hashValue.xxh32);
|
2020-06-18 21:24:27 +00:00
|
|
|
f_displayHash(&hcbe32, sizeof(hcbe32));
|
2014-09-25 20:22:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-10 16:06:34 +00:00
|
|
|
case algo_xxh64:
|
2016-04-07 23:19:06 +00:00
|
|
|
{ XXH64_canonical_t hcbe64;
|
2019-09-28 21:27:32 +00:00
|
|
|
(void)XXH64_canonicalFromHash(&hcbe64, hashValue.xxh64);
|
2020-06-18 21:24:27 +00:00
|
|
|
f_displayHash(&hcbe64, sizeof(hcbe64));
|
2014-09-25 20:22:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-28 00:40:36 +00:00
|
|
|
case algo_xxh128:
|
|
|
|
{ XXH128_canonical_t hcbe128;
|
2019-09-28 21:27:32 +00:00
|
|
|
(void)XXH128_canonicalFromHash(&hcbe128, hashValue.xxh128);
|
2020-06-18 21:24:27 +00:00
|
|
|
f_displayHash(&hcbe128, sizeof(hcbe128));
|
2014-09-25 20:22:59 +00:00
|
|
|
break;
|
2019-09-28 00:40:36 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(0);
|
2014-09-25 20:22:59 +00:00
|
|
|
}
|
2019-09-28 21:58:07 +00:00
|
|
|
DISPLAYRESULT(" %s\n", fileName);
|
2014-09-25 20:22:59 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*
|
|
|
|
* BMK_hashFiles:
|
|
|
|
* If fnTotal==0, read from stdin instead.
|
2019-09-28 21:58:07 +00:00
|
|
|
*/
|
2020-02-13 01:37:34 +00:00
|
|
|
static int BMK_hashFiles(char** fnList, int fnTotal,
|
2016-01-10 16:06:34 +00:00
|
|
|
algoType hashType, endianess displayEndianess)
|
2015-08-07 23:22:56 +00:00
|
|
|
{
|
|
|
|
int fnNb;
|
|
|
|
int result = 0;
|
2016-01-10 16:06:34 +00:00
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
if (fnTotal==0)
|
2016-01-10 16:06:34 +00:00
|
|
|
return BMK_hash(stdinName, hashType, displayEndianess);
|
|
|
|
|
|
|
|
for (fnNb=0; fnNb<fnTotal; fnNb++)
|
|
|
|
result += BMK_hash(fnList[fnNb], hashType, displayEndianess);
|
2019-10-07 15:25:57 +00:00
|
|
|
DISPLAYLEVEL(2, "\r%70s\r", "");
|
2015-08-07 23:22:56 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-22 05:53:32 +00:00
|
|
|
typedef enum {
|
|
|
|
GetLine_ok,
|
|
|
|
GetLine_eof,
|
|
|
|
GetLine_exceedMaxLineLength,
|
2020-03-16 16:29:49 +00:00
|
|
|
GetLine_outOfMemory
|
2016-01-22 05:53:32 +00:00
|
|
|
} GetLineResult;
|
|
|
|
|
2016-01-20 14:45:52 +00:00
|
|
|
typedef enum {
|
|
|
|
CanonicalFromString_ok,
|
2020-03-16 16:29:49 +00:00
|
|
|
CanonicalFromString_invalidFormat
|
2016-01-20 14:45:52 +00:00
|
|
|
} CanonicalFromStringResult;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ParseLine_ok,
|
2020-03-16 16:29:49 +00:00
|
|
|
ParseLine_invalidFormat
|
2016-01-20 14:45:52 +00:00
|
|
|
} ParseLineResult;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
LineStatus_hashOk,
|
|
|
|
LineStatus_hashFailed,
|
2020-03-16 16:29:49 +00:00
|
|
|
LineStatus_failedToOpen
|
2016-01-20 14:45:52 +00:00
|
|
|
} LineStatus;
|
|
|
|
|
|
|
|
typedef union {
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_canonical_t xxh32;
|
|
|
|
XXH64_canonical_t xxh64;
|
2019-09-28 00:40:36 +00:00
|
|
|
XXH128_canonical_t xxh128;
|
2016-01-20 14:45:52 +00:00
|
|
|
} Canonical;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Canonical canonical;
|
|
|
|
const char* filename;
|
2020-03-02 20:20:49 +00:00
|
|
|
int xxhBits; /* canonical type: 32:xxh32, 64:xxh64, 128:xxh128 */
|
2016-01-20 14:45:52 +00:00
|
|
|
} ParsedLine;
|
|
|
|
|
|
|
|
typedef struct {
|
2016-01-22 04:22:35 +00:00
|
|
|
unsigned long nProperlyFormattedLines;
|
|
|
|
unsigned long nImproperlyFormattedLines;
|
|
|
|
unsigned long nMismatchedChecksums;
|
|
|
|
unsigned long nOpenOrReadFailures;
|
|
|
|
unsigned long nMixedFormatLines;
|
|
|
|
int xxhBits;
|
|
|
|
int quit;
|
2016-01-20 14:45:52 +00:00
|
|
|
} ParseFileReport;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char* inFileName;
|
|
|
|
FILE* inFile;
|
2016-01-22 05:53:32 +00:00
|
|
|
int lineMax;
|
2016-01-20 14:45:52 +00:00
|
|
|
char* lineBuf;
|
|
|
|
size_t blockSize;
|
|
|
|
char* blockBuf;
|
2019-10-07 14:52:32 +00:00
|
|
|
U32 strictMode;
|
|
|
|
U32 statusOnly;
|
|
|
|
U32 warn;
|
|
|
|
U32 quiet;
|
2016-01-20 14:45:52 +00:00
|
|
|
ParseFileReport report;
|
|
|
|
} ParseFileArg;
|
|
|
|
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*
|
|
|
|
* Reads a line from stream `inFile`.
|
|
|
|
* Returns GetLine_ok, if it reads line successfully.
|
|
|
|
* Returns GetLine_eof, if stream reaches EOF.
|
|
|
|
* Returns GetLine_exceedMaxLineLength, if line length is longer than MAX_LINE_LENGTH.
|
|
|
|
* Returns GetLine_outOfMemory, if line buffer memory allocation failed.
|
2016-01-20 14:45:52 +00:00
|
|
|
*/
|
2016-01-22 05:53:32 +00:00
|
|
|
static GetLineResult getLine(char** lineBuf, int* lineMax, FILE* inFile)
|
2016-01-20 14:45:52 +00:00
|
|
|
{
|
2016-01-22 05:53:32 +00:00
|
|
|
GetLineResult result = GetLine_ok;
|
2019-02-26 20:36:23 +00:00
|
|
|
size_t len = 0;
|
2016-01-22 05:53:32 +00:00
|
|
|
|
2017-06-15 00:07:25 +00:00
|
|
|
if ((*lineBuf == NULL) || (*lineMax<1)) {
|
|
|
|
free(*lineBuf); /* in case it's != NULL */
|
|
|
|
*lineMax = 0;
|
|
|
|
*lineBuf = (char*)malloc(DEFAULT_LINE_LENGTH);
|
2016-01-22 05:53:32 +00:00
|
|
|
if(*lineBuf == NULL) return GetLine_outOfMemory;
|
2017-06-15 00:07:25 +00:00
|
|
|
*lineMax = DEFAULT_LINE_LENGTH;
|
2016-01-22 05:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
for (;;) {
|
2016-01-22 05:53:32 +00:00
|
|
|
const int c = fgetc(inFile);
|
2016-04-07 23:19:06 +00:00
|
|
|
if (c == EOF) {
|
2020-03-03 17:10:19 +00:00
|
|
|
/*
|
|
|
|
* If we meet EOF before first character, returns GetLine_eof,
|
2016-01-22 05:53:32 +00:00
|
|
|
* otherwise GetLine_ok.
|
|
|
|
*/
|
2016-04-07 23:19:06 +00:00
|
|
|
if (len == 0) result = GetLine_eof;
|
2016-01-22 05:53:32 +00:00
|
|
|
break;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2016-01-22 05:53:32 +00:00
|
|
|
|
|
|
|
/* Make enough space for len+1 (for final NUL) bytes. */
|
2019-02-26 20:36:23 +00:00
|
|
|
if (len+1 >= (size_t)*lineMax) {
|
2016-01-22 05:53:32 +00:00
|
|
|
char* newLineBuf = NULL;
|
2019-02-26 20:36:23 +00:00
|
|
|
size_t newBufSize = (size_t)*lineMax;
|
2016-01-22 05:53:32 +00:00
|
|
|
|
|
|
|
newBufSize += (newBufSize/2) + 1; /* x 1.5 */
|
|
|
|
if (newBufSize > MAX_LINE_LENGTH) newBufSize = MAX_LINE_LENGTH;
|
|
|
|
if (len+1 >= newBufSize) return GetLine_exceedMaxLineLength;
|
|
|
|
|
|
|
|
newLineBuf = (char*) realloc(*lineBuf, newBufSize);
|
|
|
|
if (newLineBuf == NULL) return GetLine_outOfMemory;
|
|
|
|
|
|
|
|
*lineBuf = newLineBuf;
|
2019-02-26 20:36:23 +00:00
|
|
|
*lineMax = (int)newBufSize;
|
2016-01-22 05:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (c == '\n') break;
|
2016-01-22 05:53:32 +00:00
|
|
|
(*lineBuf)[len++] = (char) c;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2016-01-22 05:53:32 +00:00
|
|
|
|
|
|
|
(*lineBuf)[len] = '\0';
|
|
|
|
return result;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*
|
|
|
|
* Converts one hexadecimal character to integer.
|
|
|
|
* Returns -1 if the given character is not hexadecimal.
|
2016-01-20 14:45:52 +00:00
|
|
|
*/
|
|
|
|
static int charToHex(char c)
|
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
result = (int) (c - '0');
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
result = (int) (c - 'A') + 0x0a;
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
result = (int) (c - 'a') + 0x0a;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*
|
2020-06-13 01:11:01 +00:00
|
|
|
* Converts canonical ASCII hexadecimal string `hashStr`
|
|
|
|
* to the big endian binary representation in unsigned char array `dst`.
|
2020-03-02 20:20:49 +00:00
|
|
|
*
|
|
|
|
* Returns CANONICAL_FROM_STRING_INVALID_FORMAT if hashStr is not well formatted.
|
|
|
|
* Returns CANONICAL_FROM_STRING_OK if hashStr is parsed successfully.
|
2016-01-20 14:45:52 +00:00
|
|
|
*/
|
|
|
|
static CanonicalFromStringResult canonicalFromString(unsigned char* dst,
|
2016-04-07 23:19:06 +00:00
|
|
|
size_t dstSize,
|
2016-01-20 14:45:52 +00:00
|
|
|
const char* hashStr)
|
|
|
|
{
|
|
|
|
size_t i;
|
2016-04-07 23:19:06 +00:00
|
|
|
for (i = 0; i < dstSize; ++i) {
|
2016-01-20 14:45:52 +00:00
|
|
|
int h0, h1;
|
|
|
|
|
|
|
|
h0 = charToHex(hashStr[i*2 + 0]);
|
2016-04-07 23:19:06 +00:00
|
|
|
if (h0 < 0) return CanonicalFromString_invalidFormat;
|
2016-01-20 14:45:52 +00:00
|
|
|
|
|
|
|
h1 = charToHex(hashStr[i*2 + 1]);
|
2016-04-07 23:19:06 +00:00
|
|
|
if (h1 < 0) return CanonicalFromString_invalidFormat;
|
2016-01-20 14:45:52 +00:00
|
|
|
|
|
|
|
dst[i] = (unsigned char) ((h0 << 4) | h1);
|
|
|
|
}
|
|
|
|
return CanonicalFromString_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*
|
|
|
|
* Parse single line of xxHash checksum file.
|
|
|
|
* Returns PARSE_LINE_ERROR_INVALID_FORMAT if the line is not well formatted.
|
|
|
|
* Returns PARSE_LINE_OK if the line is parsed successfully.
|
|
|
|
* And members of parseLine will be filled by parsed values.
|
2016-01-20 14:45:52 +00:00
|
|
|
*
|
2020-03-02 20:20:49 +00:00
|
|
|
* - line must be terminated with '\0'.
|
2016-01-20 14:45:52 +00:00
|
|
|
* - Since parsedLine.filename will point within given argument `line`,
|
2020-03-02 20:20:49 +00:00
|
|
|
* users must keep `line`s content when they are using parsedLine.
|
2016-01-20 14:45:52 +00:00
|
|
|
*
|
2020-03-02 20:20:49 +00:00
|
|
|
* xxHash checksum lines should have the following format:
|
2016-01-20 14:45:52 +00:00
|
|
|
*
|
2020-03-02 20:20:49 +00:00
|
|
|
* <8, 16, or 32 hexadecimal char> <space> <space> <filename...> <'\0'>
|
2016-01-20 14:45:52 +00:00
|
|
|
*/
|
|
|
|
static ParseLineResult parseLine(ParsedLine* parsedLine, const char* line)
|
|
|
|
{
|
|
|
|
const char* const firstSpace = strchr(line, ' ');
|
2018-09-17 19:28:59 +00:00
|
|
|
if (firstSpace == NULL) return ParseLine_invalidFormat;
|
2016-01-20 14:45:52 +00:00
|
|
|
|
2020-06-15 17:16:39 +00:00
|
|
|
parsedLine->filename = NULL;
|
|
|
|
parsedLine->xxhBits = 0;
|
2016-01-20 14:45:52 +00:00
|
|
|
|
2020-06-15 17:16:39 +00:00
|
|
|
switch (firstSpace - line)
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
{ XXH32_canonical_t* xxh32c = &parsedLine->canonical.xxh32;
|
|
|
|
if (canonicalFromString(xxh32c->digest, sizeof(xxh32c->digest), line)
|
|
|
|
!= CanonicalFromString_ok) {
|
|
|
|
return ParseLine_invalidFormat;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2020-06-15 17:16:39 +00:00
|
|
|
parsedLine->xxhBits = 32;
|
|
|
|
break;
|
|
|
|
}
|
2018-09-17 19:28:59 +00:00
|
|
|
|
2020-06-15 17:16:39 +00:00
|
|
|
case 16:
|
|
|
|
{ XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64;
|
|
|
|
if (canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), line)
|
|
|
|
!= CanonicalFromString_ok) {
|
|
|
|
return ParseLine_invalidFormat;
|
2019-09-28 23:49:11 +00:00
|
|
|
}
|
2020-06-15 17:16:39 +00:00
|
|
|
parsedLine->xxhBits = 64;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-28 23:49:11 +00:00
|
|
|
|
2020-06-15 17:16:39 +00:00
|
|
|
case 32:
|
|
|
|
{ XXH128_canonical_t* xxh128c = &parsedLine->canonical.xxh128;
|
|
|
|
if (canonicalFromString(xxh128c->digest, sizeof(xxh128c->digest), line)
|
|
|
|
!= CanonicalFromString_ok) {
|
2018-09-17 19:28:59 +00:00
|
|
|
return ParseLine_invalidFormat;
|
2020-06-15 17:16:39 +00:00
|
|
|
}
|
|
|
|
parsedLine->xxhBits = 128;
|
|
|
|
break;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:16:39 +00:00
|
|
|
default:
|
|
|
|
return ParseLine_invalidFormat;
|
|
|
|
break;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2020-06-15 17:16:39 +00:00
|
|
|
|
|
|
|
/* note : skipping second separation character, which can be anything,
|
|
|
|
* allowing insertion of custom markers such as '*' */
|
|
|
|
parsedLine->filename = firstSpace + 2;
|
2016-01-20 14:45:52 +00:00
|
|
|
return ParseLine_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-03 17:10:19 +00:00
|
|
|
/*!
|
|
|
|
* Parse xxHash checksum file.
|
2016-01-20 14:45:52 +00:00
|
|
|
*/
|
|
|
|
static void parseFile1(ParseFileArg* parseFileArg)
|
|
|
|
{
|
|
|
|
const char* const inFileName = parseFileArg->inFileName;
|
|
|
|
ParseFileReport* const report = &parseFileArg->report;
|
|
|
|
|
2016-01-22 04:22:35 +00:00
|
|
|
unsigned long lineNumber = 0;
|
2016-01-20 14:45:52 +00:00
|
|
|
memset(report, 0, sizeof(*report));
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
while (!report->quit) {
|
2016-01-20 14:45:52 +00:00
|
|
|
LineStatus lineStatus = LineStatus_hashFailed;
|
|
|
|
ParsedLine parsedLine;
|
|
|
|
memset(&parsedLine, 0, sizeof(parsedLine));
|
|
|
|
|
2016-01-22 04:22:35 +00:00
|
|
|
lineNumber++;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (lineNumber == 0) {
|
2019-09-28 21:58:07 +00:00
|
|
|
/* This is unlikely happen, but md5sum.c has this error check. */
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("%s: Error: Too many checksum lines\n", inFileName);
|
2016-01-20 14:45:52 +00:00
|
|
|
report->quit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-28 21:58:07 +00:00
|
|
|
{ GetLineResult const getLineResult = getLine(&parseFileArg->lineBuf,
|
|
|
|
&parseFileArg->lineMax,
|
|
|
|
parseFileArg->inFile);
|
|
|
|
if (getLineResult != GetLine_ok) {
|
|
|
|
if (getLineResult == GetLine_eof) break;
|
|
|
|
|
|
|
|
switch (getLineResult)
|
|
|
|
{
|
|
|
|
case GetLine_ok:
|
|
|
|
case GetLine_eof:
|
|
|
|
/* These cases never happen. See above getLineResult related "if"s.
|
|
|
|
They exist just for make gcc's -Wswitch-enum happy. */
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DISPLAY("%s:%lu: Error: Unknown error.\n", inFileName, lineNumber);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GetLine_exceedMaxLineLength:
|
|
|
|
DISPLAY("%s:%lu: Error: Line too long.\n", inFileName, lineNumber);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GetLine_outOfMemory:
|
|
|
|
DISPLAY("%s:%lu: Error: Out of memory.\n", inFileName, lineNumber);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
report->quit = 1;
|
2016-01-22 05:53:32 +00:00
|
|
|
break;
|
2019-09-28 21:58:07 +00:00
|
|
|
} }
|
2016-01-22 05:53:32 +00:00
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (parseLine(&parsedLine, parseFileArg->lineBuf) != ParseLine_ok) {
|
2016-01-20 14:45:52 +00:00
|
|
|
report->nImproperlyFormattedLines++;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (parseFileArg->warn) {
|
2019-09-28 21:58:07 +00:00
|
|
|
DISPLAY("%s:%lu: Error: Improperly formatted checksum line.\n",
|
|
|
|
inFileName, lineNumber);
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (report->xxhBits != 0 && report->xxhBits != parsedLine.xxhBits) {
|
2016-01-20 14:45:52 +00:00
|
|
|
/* Don't accept xxh32/xxh64 mixed file */
|
|
|
|
report->nImproperlyFormattedLines++;
|
|
|
|
report->nMixedFormatLines++;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (parseFileArg->warn) {
|
2020-03-03 17:10:19 +00:00
|
|
|
DISPLAY("%s: %lu: Error: Multiple hash types in one file.\n",
|
2019-09-28 21:58:07 +00:00
|
|
|
inFileName, lineNumber);
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
report->nProperlyFormattedLines++;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (report->xxhBits == 0) {
|
2016-01-20 14:45:52 +00:00
|
|
|
report->xxhBits = parsedLine.xxhBits;
|
|
|
|
}
|
|
|
|
|
2019-09-28 21:58:07 +00:00
|
|
|
do {
|
2020-02-13 01:37:34 +00:00
|
|
|
FILE* const fp = XXH_fopen(parsedLine.filename, "rb");
|
2019-09-28 21:58:07 +00:00
|
|
|
if (fp == NULL) {
|
|
|
|
lineStatus = LineStatus_failedToOpen;
|
|
|
|
break;
|
|
|
|
}
|
2016-01-20 14:45:52 +00:00
|
|
|
lineStatus = LineStatus_hashFailed;
|
|
|
|
switch (parsedLine.xxhBits)
|
|
|
|
{
|
|
|
|
case 32:
|
2019-09-28 21:27:32 +00:00
|
|
|
{ Multihash const xxh = BMK_hashStream(fp, algo_xxh32, parseFileArg->blockBuf, parseFileArg->blockSize);
|
|
|
|
if (xxh.xxh32 == XXH32_hashFromCanonical(&parsedLine.canonical.xxh32)) {
|
2016-01-20 14:45:52 +00:00
|
|
|
lineStatus = LineStatus_hashOk;
|
2016-04-07 23:19:06 +00:00
|
|
|
} }
|
2016-01-20 14:45:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 64:
|
2019-09-28 21:27:32 +00:00
|
|
|
{ Multihash const xxh = BMK_hashStream(fp, algo_xxh64, parseFileArg->blockBuf, parseFileArg->blockSize);
|
|
|
|
if (xxh.xxh64 == XXH64_hashFromCanonical(&parsedLine.canonical.xxh64)) {
|
2016-01-20 14:45:52 +00:00
|
|
|
lineStatus = LineStatus_hashOk;
|
2016-04-07 23:19:06 +00:00
|
|
|
} }
|
2016-01-20 14:45:52 +00:00
|
|
|
break;
|
|
|
|
|
2019-09-28 00:40:36 +00:00
|
|
|
case 128:
|
2019-09-28 21:27:32 +00:00
|
|
|
{ Multihash const xxh = BMK_hashStream(fp, algo_xxh128, parseFileArg->blockBuf, parseFileArg->blockSize);
|
|
|
|
if (XXH128_isEqual(xxh.xxh128, XXH128_hashFromCanonical(&parsedLine.canonical.xxh128))) {
|
2019-09-28 00:40:36 +00:00
|
|
|
lineStatus = LineStatus_hashOk;
|
|
|
|
} }
|
|
|
|
break;
|
|
|
|
|
2016-01-20 14:45:52 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2019-09-28 21:58:07 +00:00
|
|
|
} while (0);
|
2016-01-20 14:45:52 +00:00
|
|
|
|
|
|
|
switch (lineStatus)
|
|
|
|
{
|
|
|
|
default:
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("%s: Error: Unknown error.\n", inFileName);
|
2016-01-20 14:45:52 +00:00
|
|
|
report->quit = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LineStatus_failedToOpen:
|
|
|
|
report->nOpenOrReadFailures++;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (!parseFileArg->statusOnly) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAYRESULT("%s:%lu: Could not open or read '%s': %s.\n",
|
|
|
|
inFileName, lineNumber, parsedLine.filename, strerror(errno));
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LineStatus_hashOk:
|
|
|
|
case LineStatus_hashFailed:
|
2016-04-07 23:19:06 +00:00
|
|
|
{ int b = 1;
|
|
|
|
if (lineStatus == LineStatus_hashOk) {
|
2016-01-20 14:45:52 +00:00
|
|
|
/* If --quiet is specified, don't display "OK" */
|
2016-04-07 23:19:06 +00:00
|
|
|
if (parseFileArg->quiet) b = 0;
|
|
|
|
} else {
|
2016-01-20 14:45:52 +00:00
|
|
|
report->nMismatchedChecksums++;
|
|
|
|
}
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (b && !parseFileArg->statusOnly) {
|
2016-01-20 14:45:52 +00:00
|
|
|
DISPLAYRESULT("%s: %s\n", parsedLine.filename
|
|
|
|
, lineStatus == LineStatus_hashOk ? "OK" : "FAILED");
|
2016-04-07 23:19:06 +00:00
|
|
|
} }
|
2016-01-20 14:45:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-28 00:14:28 +00:00
|
|
|
} /* while (!report->quit) */
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse xxHash checksum file.
|
|
|
|
* Returns 1, if all procedures were succeeded.
|
|
|
|
* Returns 0, if any procedures was failed.
|
|
|
|
*
|
|
|
|
* If strictMode != 0, return error code if any line is invalid.
|
|
|
|
* If statusOnly != 0, don't generate any output.
|
|
|
|
* If warn != 0, print a warning message to stderr.
|
|
|
|
* If quiet != 0, suppress "OK" line.
|
|
|
|
*
|
|
|
|
* "All procedures are succeeded" means:
|
|
|
|
* - Checksum file contains at least one line and less than SIZE_T_MAX lines.
|
|
|
|
* - All files are properly opened and read.
|
|
|
|
* - All hash values match with its content.
|
|
|
|
* - (strict mode) All lines in checksum file are consistent and well formatted.
|
|
|
|
*/
|
|
|
|
static int checkFile(const char* inFileName,
|
|
|
|
const endianess displayEndianess,
|
2019-10-07 14:52:32 +00:00
|
|
|
U32 strictMode,
|
|
|
|
U32 statusOnly,
|
|
|
|
U32 warn,
|
|
|
|
U32 quiet)
|
2016-01-20 14:45:52 +00:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
FILE* inFile = NULL;
|
|
|
|
ParseFileArg parseFileArgBody;
|
|
|
|
ParseFileArg* const parseFileArg = &parseFileArgBody;
|
|
|
|
ParseFileReport* const report = &parseFileArg->report;
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (displayEndianess != big_endian) {
|
2016-01-20 14:45:52 +00:00
|
|
|
/* Don't accept little endian */
|
2016-01-22 04:22:35 +00:00
|
|
|
DISPLAY( "Check file mode doesn't support little endian\n" );
|
2016-01-22 04:19:19 +00:00
|
|
|
return 0;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/* note: stdinName is special constant pointer. It is not a string. */
|
2016-04-07 23:19:06 +00:00
|
|
|
if (inFileName == stdinName) {
|
2020-03-03 17:10:19 +00:00
|
|
|
/*
|
|
|
|
* Note: Since we expect text input for xxhash -c mode,
|
|
|
|
* we don't set binary mode for stdin.
|
|
|
|
*/
|
2019-03-15 15:56:58 +00:00
|
|
|
inFileName = "stdin";
|
2016-01-20 14:45:52 +00:00
|
|
|
inFile = stdin;
|
2016-04-07 23:19:06 +00:00
|
|
|
} else {
|
2020-02-13 01:37:34 +00:00
|
|
|
inFile = XXH_fopen( inFileName, "rt" );
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (inFile == NULL) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("Error: Could not open '%s': %s\n", inFileName, strerror(errno));
|
2016-01-22 04:19:19 +00:00
|
|
|
return 0;
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parseFileArg->inFileName = inFileName;
|
|
|
|
parseFileArg->inFile = inFile;
|
2016-01-22 05:53:32 +00:00
|
|
|
parseFileArg->lineMax = DEFAULT_LINE_LENGTH;
|
2020-06-12 23:19:51 +00:00
|
|
|
parseFileArg->lineBuf = (char*) malloc((size_t)parseFileArg->lineMax);
|
2016-01-20 14:45:52 +00:00
|
|
|
parseFileArg->blockSize = 64 * 1024;
|
|
|
|
parseFileArg->blockBuf = (char*) malloc(parseFileArg->blockSize);
|
|
|
|
parseFileArg->strictMode = strictMode;
|
|
|
|
parseFileArg->statusOnly = statusOnly;
|
|
|
|
parseFileArg->warn = warn;
|
|
|
|
parseFileArg->quiet = quiet;
|
|
|
|
|
2020-06-12 23:19:51 +00:00
|
|
|
if ( (parseFileArg->lineBuf == NULL)
|
|
|
|
|| (parseFileArg->blockBuf == NULL) ) {
|
|
|
|
DISPLAY("Error: : memory allocation failed \n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-01-20 14:45:52 +00:00
|
|
|
parseFile1(parseFileArg);
|
|
|
|
|
|
|
|
free(parseFileArg->blockBuf);
|
|
|
|
free(parseFileArg->lineBuf);
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (inFile != stdin) fclose(inFile);
|
2016-01-20 14:45:52 +00:00
|
|
|
|
|
|
|
/* Show error/warning messages. All messages are copied from md5sum.c
|
|
|
|
*/
|
2016-04-07 23:19:06 +00:00
|
|
|
if (report->nProperlyFormattedLines == 0) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAY("%s: no properly formatted xxHash checksum lines found\n", inFileName);
|
2016-04-07 23:19:06 +00:00
|
|
|
} else if (!statusOnly) {
|
|
|
|
if (report->nImproperlyFormattedLines) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAYRESULT("%lu %s are improperly formatted\n"
|
|
|
|
, report->nImproperlyFormattedLines
|
|
|
|
, report->nImproperlyFormattedLines == 1 ? "line" : "lines");
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2016-04-07 23:19:06 +00:00
|
|
|
if (report->nOpenOrReadFailures) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAYRESULT("%lu listed %s could not be read\n"
|
|
|
|
, report->nOpenOrReadFailures
|
|
|
|
, report->nOpenOrReadFailures == 1 ? "file" : "files");
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2016-04-07 23:19:06 +00:00
|
|
|
if (report->nMismatchedChecksums) {
|
2019-03-15 15:56:58 +00:00
|
|
|
DISPLAYRESULT("%lu computed %s did NOT match\n"
|
|
|
|
, report->nMismatchedChecksums
|
|
|
|
, report->nMismatchedChecksums == 1 ? "checksum" : "checksums");
|
2016-04-07 23:19:06 +00:00
|
|
|
} }
|
2016-01-20 14:45:52 +00:00
|
|
|
|
|
|
|
/* Result (exit) code logic is copied from
|
|
|
|
* gnu coreutils/src/md5sum.c digest_check() */
|
|
|
|
result = report->nProperlyFormattedLines != 0
|
|
|
|
&& report->nMismatchedChecksums == 0
|
|
|
|
&& report->nOpenOrReadFailures == 0
|
|
|
|
&& (!strictMode || report->nImproperlyFormattedLines == 0)
|
|
|
|
&& report->quit == 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-13 01:37:34 +00:00
|
|
|
static int checkFiles(char** fnList, int fnTotal,
|
2016-01-20 14:45:52 +00:00
|
|
|
const endianess displayEndianess,
|
2019-10-07 14:52:32 +00:00
|
|
|
U32 strictMode,
|
|
|
|
U32 statusOnly,
|
|
|
|
U32 warn,
|
|
|
|
U32 quiet)
|
2016-01-20 14:45:52 +00:00
|
|
|
{
|
|
|
|
int ok = 1;
|
2016-01-25 09:59:01 +00:00
|
|
|
|
|
|
|
/* Special case for stdinName "-",
|
|
|
|
* note: stdinName is not a string. It's special pointer. */
|
2016-04-07 23:19:06 +00:00
|
|
|
if (fnTotal==0) {
|
2016-01-25 09:59:01 +00:00
|
|
|
ok &= checkFile(stdinName, displayEndianess, strictMode, statusOnly, warn, quiet);
|
2016-04-07 23:19:06 +00:00
|
|
|
} else {
|
2016-01-25 09:59:01 +00:00
|
|
|
int fnNb;
|
|
|
|
for (fnNb=0; fnNb<fnTotal; fnNb++)
|
|
|
|
ok &= checkFile(fnList[fnNb], displayEndianess, strictMode, statusOnly, warn, quiet);
|
|
|
|
}
|
2016-01-20 14:45:52 +00:00
|
|
|
return ok ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ********************************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* Main
|
2016-01-04 07:32:38 +00:00
|
|
|
**********************************************************/
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
static int usage(const char* exename)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2016-04-07 23:50:06 +00:00
|
|
|
DISPLAY( WELCOME_MESSAGE(exename) );
|
2020-04-07 22:19:08 +00:00
|
|
|
DISPLAY( "Print or verify checksums using fast non-cryptographic algorithm xxHash \n\n" );
|
|
|
|
DISPLAY( "Usage: %s [options] [files] \n\n", exename);
|
|
|
|
DISPLAY( "When no filename provided or when '-' is provided, uses stdin as input. \n");
|
|
|
|
DISPLAY( "Options: \n");
|
|
|
|
DISPLAY( " -H# algorithm strength: 0=32bits, 1=64bits, 2=128bits (default: %i) \n", (int)g_defaultAlgo);
|
|
|
|
DISPLAY( " -c read xxHash sums from [files] and check them \n");
|
|
|
|
DISPLAY( " -h, --help display a long help page about advanced options \n");
|
2016-01-10 16:06:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int usage_advanced(const char* exename)
|
|
|
|
{
|
|
|
|
usage(exename);
|
|
|
|
DISPLAY( "Advanced :\n");
|
2020-04-07 22:19:08 +00:00
|
|
|
DISPLAY( " -V, --version Display version information \n");
|
2020-03-02 20:20:49 +00:00
|
|
|
DISPLAY( " --little-endian Display hashes in little endian convention (default: big endian) \n");
|
2020-04-07 22:19:08 +00:00
|
|
|
DISPLAY( " -b Run benchmark (all variants, default) \n");
|
|
|
|
DISPLAY( " -b# Bench only variant # \n");
|
|
|
|
DISPLAY( " -i ITERATIONS Number of times to run the benchmark (default: %u) \n", (unsigned)g_nbIterations);
|
2020-05-27 18:43:41 +00:00
|
|
|
DISPLAY( " -q, --quiet Don't display version header in benchmark mode \n");
|
2016-02-19 14:21:43 +00:00
|
|
|
DISPLAY( "\n");
|
2020-04-07 22:19:08 +00:00
|
|
|
DISPLAY( "The following four options are useful only when verifying checksums (-c): \n");
|
|
|
|
DISPLAY( " -q, --quiet Don't print OK for each successfully verified file \n");
|
|
|
|
DISPLAY( " --status Don't output anything, status code shows success \n");
|
|
|
|
DISPLAY( " --strict Exit non-zero for improperly formatted checksum lines \n");
|
|
|
|
DISPLAY( " --warn Warn about improperly formatted checksum lines \n");
|
2014-08-15 09:27:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
static int badusage(const char* exename)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2020-04-07 22:19:08 +00:00
|
|
|
DISPLAY("Wrong parameters\n\n");
|
2014-08-15 09:27:04 +00:00
|
|
|
usage(exename);
|
2014-09-25 20:22:59 +00:00
|
|
|
return 1;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 20:36:23 +00:00
|
|
|
static void errorOut(const char* msg)
|
|
|
|
{
|
|
|
|
DISPLAY("%s \n", msg); exit(1);
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*!
|
|
|
|
* readU32FromCharChecked():
|
2019-02-26 20:36:23 +00:00
|
|
|
* @return 0 if success, and store the result in *value.
|
2020-03-02 20:20:49 +00:00
|
|
|
* Allows and interprets K, KB, KiB, M, MB and MiB suffix.
|
|
|
|
* Will also modify `*stringPtr`, advancing it to position where it stopped reading.
|
|
|
|
* @return 1 if an overflow error occurs
|
|
|
|
*/
|
2020-03-10 00:03:15 +00:00
|
|
|
static int readU32FromCharChecked(const char** stringPtr, U32* value)
|
2018-02-19 01:38:48 +00:00
|
|
|
{
|
2020-03-10 00:03:15 +00:00
|
|
|
static const U32 max = (((U32)(-1)) / 10) - 1;
|
|
|
|
U32 result = 0;
|
2019-02-26 20:36:23 +00:00
|
|
|
while ((**stringPtr >='0') && (**stringPtr <='9')) {
|
2019-02-26 23:24:59 +00:00
|
|
|
if (result > max) return 1; /* overflow error */
|
2019-02-26 20:36:23 +00:00
|
|
|
result *= 10;
|
2020-03-10 00:03:15 +00:00
|
|
|
result += (U32)(**stringPtr - '0');
|
2019-02-26 20:36:23 +00:00
|
|
|
(*stringPtr)++ ;
|
|
|
|
}
|
2018-02-19 01:38:48 +00:00
|
|
|
if ((**stringPtr=='K') || (**stringPtr=='M')) {
|
2020-03-10 00:03:15 +00:00
|
|
|
U32 const maxK = ((U32)(-1)) >> 10;
|
2019-02-26 23:24:59 +00:00
|
|
|
if (result > maxK) return 1; /* overflow error */
|
2018-02-19 01:38:48 +00:00
|
|
|
result <<= 10;
|
2019-02-26 20:36:23 +00:00
|
|
|
if (**stringPtr=='M') {
|
2019-02-26 23:24:59 +00:00
|
|
|
if (result > maxK) return 1; /* overflow error */
|
2019-02-26 20:36:23 +00:00
|
|
|
result <<= 10;
|
|
|
|
}
|
|
|
|
(*stringPtr)++; /* skip `K` or `M` */
|
2018-02-19 01:38:48 +00:00
|
|
|
if (**stringPtr=='i') (*stringPtr)++;
|
|
|
|
if (**stringPtr=='B') (*stringPtr)++;
|
|
|
|
}
|
2019-02-26 20:36:23 +00:00
|
|
|
*value = result;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:20:49 +00:00
|
|
|
/*!
|
|
|
|
* readU32FromChar():
|
|
|
|
* @return: unsigned integer value read from input in `char` format.
|
2019-02-26 20:36:23 +00:00
|
|
|
* allows and interprets K, KB, KiB, M, MB and MiB suffix.
|
|
|
|
* Will also modify `*stringPtr`, advancing it to position where it stopped reading.
|
2020-03-03 17:10:19 +00:00
|
|
|
* Note: function will exit() program if digit sequence overflows
|
|
|
|
*/
|
2020-03-10 00:03:15 +00:00
|
|
|
static U32 readU32FromChar(const char** stringPtr) {
|
|
|
|
U32 result;
|
2019-02-27 00:42:50 +00:00
|
|
|
if (readU32FromCharChecked(stringPtr, &result)) {
|
2019-03-15 15:56:58 +00:00
|
|
|
static const char errorMsg[] = "Error: numeric value too large";
|
2019-02-27 00:42:50 +00:00
|
|
|
errorOut(errorMsg);
|
|
|
|
}
|
2018-02-19 01:38:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-02-13 01:37:34 +00:00
|
|
|
static int XXH_main(int argc, char** argv)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2018-03-20 17:34:08 +00:00
|
|
|
int i, filenamesStart = 0;
|
2016-08-10 05:00:29 +00:00
|
|
|
const char* const exename = argv[0];
|
2019-10-07 14:52:32 +00:00
|
|
|
U32 benchmarkMode = 0;
|
|
|
|
U32 fileCheckMode = 0;
|
|
|
|
U32 strictMode = 0;
|
|
|
|
U32 statusOnly = 0;
|
|
|
|
U32 warn = 0;
|
2020-06-12 21:52:46 +00:00
|
|
|
U32 selectBenchIDs= 0; /* 0 == use default k_testIDs_default, kBenchAll == bench all */
|
|
|
|
static const U32 kBenchAll = 99;
|
2018-02-19 01:38:48 +00:00
|
|
|
size_t keySize = XXH_DEFAULT_SAMPLE_SIZE;
|
2018-03-20 17:34:08 +00:00
|
|
|
algoType algo = g_defaultAlgo;
|
2016-01-10 16:06:34 +00:00
|
|
|
endianess displayEndianess = big_endian;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-03-03 17:10:19 +00:00
|
|
|
/* special case: xxhNNsum default to NN bits checksum */
|
2019-09-28 00:50:02 +00:00
|
|
|
if (strstr(exename, "xxh32sum") != NULL) algo = algo_xxh32;
|
|
|
|
if (strstr(exename, "xxh64sum") != NULL) algo = algo_xxh64;
|
2019-09-28 00:40:36 +00:00
|
|
|
if (strstr(exename, "xxh128sum") != NULL) algo = algo_xxh128;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
for(i=1; i<argc; i++) {
|
2015-08-07 23:22:56 +00:00
|
|
|
const char* argument = argv[i];
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2020-03-03 17:10:19 +00:00
|
|
|
if(!argument) continue; /* Protection if arguments are empty */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-20 14:45:52 +00:00
|
|
|
if (!strcmp(argument, "--check")) { fileCheckMode = 1; continue; }
|
2020-06-12 21:52:46 +00:00
|
|
|
if (!strcmp(argument, "--benchmark-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
|
|
|
|
if (!strcmp(argument, "--bench-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
|
|
|
|
if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
|
|
|
|
if (!strcmp(argument, "--little-endian")) { displayEndianess = little_endian; continue; }
|
2016-01-20 14:45:52 +00:00
|
|
|
if (!strcmp(argument, "--strict")) { strictMode = 1; continue; }
|
|
|
|
if (!strcmp(argument, "--status")) { statusOnly = 1; continue; }
|
|
|
|
if (!strcmp(argument, "--warn")) { warn = 1; continue; }
|
2016-02-22 13:12:43 +00:00
|
|
|
if (!strcmp(argument, "--help")) { return usage_advanced(exename); }
|
2020-06-12 22:07:20 +00:00
|
|
|
if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE(exename)); BMK_sanityCheck(); return 0; }
|
2016-01-10 16:06:34 +00:00
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (*argument!='-') {
|
2015-08-07 23:22:56 +00:00
|
|
|
if (filenamesStart==0) filenamesStart=i; /* only supports a continuous list of filenames */
|
2014-10-29 12:55:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* command selection */
|
2020-03-03 17:10:19 +00:00
|
|
|
argument++; /* note: *argument=='-' */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
while (*argument!=0) {
|
2014-10-29 12:55:58 +00:00
|
|
|
switch(*argument)
|
2014-09-25 20:22:59 +00:00
|
|
|
{
|
2015-08-19 14:11:24 +00:00
|
|
|
/* Display version */
|
|
|
|
case 'V':
|
2016-04-07 23:50:06 +00:00
|
|
|
DISPLAY(WELCOME_MESSAGE(exename)); return 0;
|
2015-08-19 14:11:24 +00:00
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
/* Display help on usage */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'h':
|
2016-01-10 16:06:34 +00:00
|
|
|
return usage_advanced(exename);
|
2014-10-29 12:55:58 +00:00
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
/* select hash algorithm */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'H':
|
2016-01-10 16:06:34 +00:00
|
|
|
algo = (algoType)(argument[1] - '0');
|
2014-10-29 12:55:58 +00:00
|
|
|
argument+=2;
|
2019-09-28 00:50:02 +00:00
|
|
|
if (!((algo >= algo_xxh32) && (algo <= algo_xxh128)))
|
|
|
|
return badusage(exename);
|
2014-10-29 12:55:58 +00:00
|
|
|
break;
|
|
|
|
|
2016-01-20 14:45:52 +00:00
|
|
|
/* File check mode */
|
|
|
|
case 'c':
|
|
|
|
fileCheckMode=1;
|
|
|
|
argument++;
|
|
|
|
break;
|
|
|
|
|
2016-02-22 13:10:25 +00:00
|
|
|
/* Warning mode (file check mode only, alias of "--warning") */
|
|
|
|
case 'w':
|
|
|
|
warn=1;
|
|
|
|
argument++;
|
|
|
|
break;
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Trigger benchmark mode */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'b':
|
|
|
|
argument++;
|
2018-02-19 01:38:48 +00:00
|
|
|
benchmarkMode = 1;
|
2020-06-12 22:07:20 +00:00
|
|
|
do {
|
|
|
|
if (*argument == ',') argument++;
|
|
|
|
selectBenchIDs = readU32FromChar(&argument); /* select one specific test */
|
|
|
|
if (selectBenchIDs < NB_TESTFUNC) {
|
|
|
|
g_testIDs[selectBenchIDs] = 1;
|
|
|
|
} else
|
|
|
|
selectBenchIDs = kBenchAll;
|
|
|
|
} while (*argument == ',');
|
2014-10-29 12:55:58 +00:00
|
|
|
break;
|
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
/* Modify Nb Iterations (benchmark only) */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'i':
|
2018-02-19 01:38:48 +00:00
|
|
|
argument++;
|
|
|
|
g_nbIterations = readU32FromChar(&argument);
|
2014-10-29 12:55:58 +00:00
|
|
|
break;
|
|
|
|
|
2015-08-12 16:10:16 +00:00
|
|
|
/* Modify Block size (benchmark only) */
|
|
|
|
case 'B':
|
|
|
|
argument++;
|
2018-02-19 01:38:48 +00:00
|
|
|
keySize = readU32FromChar(&argument);
|
2015-08-12 16:10:16 +00:00
|
|
|
break;
|
|
|
|
|
2018-02-17 18:53:52 +00:00
|
|
|
/* Modify verbosity of benchmark output (hidden option) */
|
|
|
|
case 'q':
|
|
|
|
argument++;
|
|
|
|
g_displayLevel--;
|
|
|
|
break;
|
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
default:
|
|
|
|
return badusage(exename);
|
2014-09-25 20:22:59 +00:00
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
2016-04-07 23:19:06 +00:00
|
|
|
} /* for(i=1; i<argc; i++) */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Check benchmark mode */
|
2016-04-07 23:19:06 +00:00
|
|
|
if (benchmarkMode) {
|
2018-02-17 18:53:52 +00:00
|
|
|
DISPLAYLEVEL(2, WELCOME_MESSAGE(exename) );
|
2019-03-29 04:47:22 +00:00
|
|
|
BMK_sanityCheck();
|
2020-06-12 21:52:46 +00:00
|
|
|
if (selectBenchIDs == 0) memcpy(g_testIDs, k_testIDs_default, sizeof(g_testIDs));
|
|
|
|
if (selectBenchIDs == kBenchAll) memset(g_testIDs, 1, sizeof(g_testIDs));
|
|
|
|
if (filenamesStart==0) return BMK_benchInternal(keySize);
|
|
|
|
return BMK_benchFiles(argv+filenamesStart, argc-filenamesStart);
|
2014-10-18 11:10:27 +00:00
|
|
|
}
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Check if input is defined as console; trigger an error in this case */
|
2015-08-07 23:22:56 +00:00
|
|
|
if ( (filenamesStart==0) && IS_CONSOLE(stdin) ) return badusage(exename);
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
if (filenamesStart==0) filenamesStart = argc;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (fileCheckMode) {
|
2017-10-09 19:26:59 +00:00
|
|
|
return checkFiles(argv+filenamesStart, argc-filenamesStart,
|
2019-10-07 15:25:57 +00:00
|
|
|
displayEndianess, strictMode, statusOnly, warn, (g_displayLevel < 2) /*quiet*/);
|
2016-04-07 23:19:06 +00:00
|
|
|
} else {
|
2016-01-20 14:45:52 +00:00
|
|
|
return BMK_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, displayEndianess);
|
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
2020-02-13 01:37:34 +00:00
|
|
|
|
2020-03-10 18:06:09 +00:00
|
|
|
/* Windows main wrapper which properly handles UTF-8 command line arguments. */
|
2020-03-28 19:40:51 +00:00
|
|
|
#ifdef _WIN32
|
2020-02-13 23:48:25 +00:00
|
|
|
/* Converts a UTF-16 argv to UTF-8. */
|
2020-03-28 19:40:51 +00:00
|
|
|
static char** convert_argv(int argc, wchar_t** utf16_argv)
|
2020-02-13 01:37:34 +00:00
|
|
|
{
|
2020-03-28 19:40:51 +00:00
|
|
|
char** utf8_argv = (char**)malloc((size_t)(argc + 1) * sizeof(char*));
|
2020-03-10 18:06:09 +00:00
|
|
|
if (utf8_argv != NULL) {
|
2020-02-13 23:48:25 +00:00
|
|
|
int i;
|
2020-02-13 01:37:34 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
2020-03-10 18:06:09 +00:00
|
|
|
utf8_argv[i] = utf16_to_utf8(utf16_argv[i]);
|
2020-02-13 01:37:34 +00:00
|
|
|
}
|
2020-03-10 18:06:09 +00:00
|
|
|
utf8_argv[argc] = NULL;
|
2020-02-13 01:37:34 +00:00
|
|
|
}
|
2020-03-10 18:06:09 +00:00
|
|
|
return utf8_argv;
|
2020-02-13 01:37:34 +00:00
|
|
|
}
|
2020-02-13 23:48:25 +00:00
|
|
|
/* Frees arguments returned by convert_argv */
|
2020-03-28 19:40:51 +00:00
|
|
|
static void free_argv(int argc, char** argv)
|
2020-02-13 01:37:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
2020-02-13 23:48:25 +00:00
|
|
|
if (argv == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-13 01:37:34 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
free(argv[i]);
|
|
|
|
}
|
|
|
|
free(argv);
|
|
|
|
}
|
|
|
|
|
2020-03-28 19:40:51 +00:00
|
|
|
|
2020-02-13 01:37:34 +00:00
|
|
|
/*
|
|
|
|
* On Windows, main's argv parameter is useless. Instead of UTF-8, you get ANSI
|
2020-03-04 03:08:12 +00:00
|
|
|
* encoding, and any unknown characters will show up as mojibake.
|
2020-02-13 01:37:34 +00:00
|
|
|
*
|
|
|
|
* While this doesn't affect most programs, what does happen is that we can't
|
|
|
|
* open any files with Unicode filenames.
|
|
|
|
*
|
2020-03-28 19:40:51 +00:00
|
|
|
* We instead convert wmain's arguments to UTF-8, preserving Unicode arguments.
|
2020-02-13 01:37:34 +00:00
|
|
|
*
|
2020-03-28 19:40:51 +00:00
|
|
|
* This function is wrapped by `__wgetmainargs()` and `main()` below on MinGW
|
|
|
|
* with Unicode disabled, but if possible, we try to use `wmain()`.
|
2020-02-13 01:37:34 +00:00
|
|
|
*/
|
2020-03-28 19:40:51 +00:00
|
|
|
static int XXH_wmain(int argc, wchar_t** utf16_argv)
|
2020-02-13 01:37:34 +00:00
|
|
|
{
|
2020-02-13 23:48:25 +00:00
|
|
|
/* Convert the UTF-16 arguments to UTF-8. */
|
2020-03-28 19:40:51 +00:00
|
|
|
char** utf8_argv = convert_argv(argc, utf16_argv);
|
2020-02-13 01:37:34 +00:00
|
|
|
|
2020-03-28 19:40:51 +00:00
|
|
|
if (utf8_argv == NULL) {
|
|
|
|
/* An unfortunate but incredibly unlikely error, */
|
2020-02-13 23:48:25 +00:00
|
|
|
fprintf(stderr, "Error converting command line arguments!\n");
|
2020-03-28 19:40:51 +00:00
|
|
|
return 1;
|
2020-02-13 23:48:25 +00:00
|
|
|
} else {
|
2020-03-28 19:40:51 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MinGW's terminal uses full block buffering for stderr.
|
|
|
|
*
|
|
|
|
* This is nonstandard behavior and causes text to not display until
|
|
|
|
* the buffer fills.
|
|
|
|
*
|
|
|
|
* `setvbuf()` can easily correct this to make text display instantly.
|
|
|
|
*/
|
2020-02-13 23:48:25 +00:00
|
|
|
setvbuf(stderr, NULL, _IONBF, 0);
|
2020-02-13 01:37:34 +00:00
|
|
|
|
2020-02-13 23:48:25 +00:00
|
|
|
/* Call our real main function */
|
2020-03-28 19:40:51 +00:00
|
|
|
ret = XXH_main(argc, utf8_argv);
|
2020-02-13 01:37:34 +00:00
|
|
|
|
2020-03-28 19:40:51 +00:00
|
|
|
/* Cleanup */
|
|
|
|
free_argv(argc, utf8_argv);
|
|
|
|
return ret;
|
2020-02-13 23:48:25 +00:00
|
|
|
}
|
2020-03-28 19:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) /* MSVC always accepts wmain */ \
|
|
|
|
|| defined(_UNICODE) || defined(UNICODE) /* defined with -municode on MinGW-w64 */
|
|
|
|
|
|
|
|
/* Preferred: Use the real `wmain()`. */
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C"
|
2020-02-13 01:37:34 +00:00
|
|
|
#endif
|
2020-03-28 19:40:51 +00:00
|
|
|
int wmain(int argc, wchar_t** utf16_argv)
|
|
|
|
{
|
|
|
|
return XXH_wmain(argc, utf16_argv);
|
2020-02-13 01:37:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 19:40:51 +00:00
|
|
|
#else /* Non-Unicode MinGW */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrap `XXH_wmain()` using `main()` and `__wgetmainargs()` on MinGW without
|
|
|
|
* Unicode support.
|
|
|
|
*
|
|
|
|
* `__wgetmainargs()` is used in the CRT startup to retrieve the arguments for
|
|
|
|
* `wmain()`, so we use it on MinGW to emulate `wmain()`.
|
|
|
|
*
|
|
|
|
* It is an internal function and not declared in any public headers, so we
|
|
|
|
* have to declare it manually.
|
|
|
|
*
|
|
|
|
* An alternative that doesn't mess with internal APIs is `GetCommandLineW()`
|
|
|
|
* with `CommandLineToArgvW()`, but the former doesn't expand wildcards and the
|
|
|
|
* latter requires linking to Shell32.dll and its numerous dependencies.
|
|
|
|
*
|
|
|
|
* This method keeps our dependencies to kernel32.dll and the CRT.
|
|
|
|
*
|
|
|
|
* https://docs.microsoft.com/en-us/cpp/c-runtime-library/getmainargs-wgetmainargs?view=vs-2019
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
int newmode;
|
|
|
|
} _startupinfo;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
#endif
|
|
|
|
int __cdecl __wgetmainargs(
|
|
|
|
int* Argc,
|
|
|
|
wchar_t*** Argv,
|
|
|
|
wchar_t*** Env,
|
|
|
|
int DoWildCard,
|
|
|
|
_startupinfo* StartInfo
|
|
|
|
);
|
|
|
|
|
|
|
|
int main(int ansi_argc, char** ansi_argv)
|
|
|
|
{
|
|
|
|
int utf16_argc;
|
|
|
|
wchar_t** utf16_argv;
|
|
|
|
wchar_t** utf16_envp; /* Unused but required */
|
|
|
|
_startupinfo startinfo = {0}; /* 0 == don't change new mode */
|
|
|
|
|
|
|
|
/* Get wmain's UTF-16 arguments. Make sure we expand wildcards. */
|
|
|
|
if (__wgetmainargs(&utf16_argc, &utf16_argv, &utf16_envp, 1, &startinfo) < 0)
|
|
|
|
/* In the very unlikely case of an error, use the ANSI arguments. */
|
|
|
|
return XXH_main(ansi_argc, ansi_argv);
|
|
|
|
|
|
|
|
/* Call XXH_wmain with our UTF-16 arguments */
|
|
|
|
return XXH_wmain(utf16_argc, utf16_argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Non-Unicode MinGW */
|
|
|
|
|
|
|
|
#else /* Not Windows */
|
|
|
|
|
2020-03-10 18:06:09 +00:00
|
|
|
/* Wrap main normally on non-Windows platforms. */
|
2020-03-28 19:40:51 +00:00
|
|
|
int main(int argc, char** argv)
|
2020-02-13 01:37:34 +00:00
|
|
|
{
|
|
|
|
return XXH_main(argc, argv);
|
|
|
|
}
|
2020-03-28 19:40:51 +00:00
|
|
|
#endif /* !Windows */
|