2014-08-15 09:27:04 +00:00
|
|
|
/*
|
2016-04-07 23:50:06 +00:00
|
|
|
* xxhsum - Command line interface for xxhash algorithms
|
|
|
|
* Copyright (C) Yann Collet 2012-2016
|
|
|
|
*
|
|
|
|
* 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 :
|
|
|
|
* - xxHash homepage : http://www.xxhash.com
|
|
|
|
* - xxHash source repository : https://github.com/Cyan4973/xxHash
|
2014-08-15 09:27:04 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-07 23:50:06 +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-06-06 12:12:27 +00:00
|
|
|
#ifndef XXHASH_C_2097394837
|
|
|
|
#define XXHASH_C_2097394837
|
2016-01-07 22:54:51 +00:00
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* Compiler Options
|
2016-01-04 07:32:38 +00:00
|
|
|
**************************************/
|
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
|
|
|
/* ************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* Includes
|
2016-01-04 07:32:38 +00:00
|
|
|
**************************************/
|
2016-08-14 02:28:17 +00:00
|
|
|
#include <stdlib.h> /* malloc, calloc, free, exit */
|
2018-02-19 00:56:14 +00:00
|
|
|
#include <stdio.h> /* fprintf, fopen, ftello64, fread, stdin, stdout, _fileno (when present) */
|
2014-12-17 11:15:03 +00:00
|
|
|
#include <string.h> /* strcmp */
|
2018-02-19 00:56:14 +00:00
|
|
|
#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 */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-08-06 18:40:25 +00:00
|
|
|
#define XXH_STATIC_LINKING_ONLY /* *_state_t */
|
2016-06-06 12:12:27 +00:00
|
|
|
#include "xxhash.h"
|
2014-10-29 12:55:58 +00:00
|
|
|
|
|
|
|
|
2016-04-07 23:50:06 +00:00
|
|
|
/*-************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* OS-Specific Includes
|
2016-01-04 07:32:38 +00:00
|
|
|
**************************************/
|
2014-10-29 12:55:58 +00:00
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
2015-05-07 14:27:27 +00:00
|
|
|
# include <fcntl.h> /* _O_BINARY */
|
|
|
|
# include <io.h> /* _setmode, _isatty */
|
2014-10-29 12:55:58 +00:00
|
|
|
# ifdef __MINGW32__
|
2015-05-07 14:27:27 +00:00
|
|
|
int _fileno(FILE *stream); /* MINGW somehow forgets to include this windows declaration into <stdio.h> */
|
2014-10-29 12:55:58 +00:00
|
|
|
# endif
|
|
|
|
# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)
|
|
|
|
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
|
|
|
|
#else
|
2015-05-07 14:27:27 +00:00
|
|
|
# include <unistd.h> /* isatty, STDIN_FILENO */
|
2014-10-29 12:55:58 +00:00
|
|
|
# define SET_BINARY_MODE(file)
|
|
|
|
# define IS_CONSOLE(stdStream) isatty(STDIN_FILENO)
|
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
#if !defined(S_ISREG)
|
|
|
|
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
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
|
|
|
**************************************/
|
|
|
|
#ifndef MEM_MODULE
|
|
|
|
# define MEM_MODULE
|
|
|
|
# if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
|
|
|
|
# include <stdint.h>
|
|
|
|
typedef uint8_t BYTE;
|
|
|
|
typedef uint16_t U16;
|
|
|
|
typedef uint32_t U32;
|
|
|
|
typedef int32_t S32;
|
|
|
|
typedef uint64_t U64;
|
|
|
|
# else
|
|
|
|
typedef unsigned char BYTE;
|
|
|
|
typedef unsigned short U16;
|
|
|
|
typedef unsigned int U32;
|
|
|
|
typedef signed int S32;
|
|
|
|
typedef unsigned long long U64;
|
|
|
|
# endif
|
2014-08-15 09:27:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
static unsigned BMK_isLittleEndian(void)
|
|
|
|
{
|
2016-05-28 00:14:28 +00:00
|
|
|
const union { U32 u; BYTE 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
|
|
|
/* *************************************
|
2015-05-07 14:27:27 +00:00
|
|
|
* Constants
|
2016-01-04 07:32:38 +00:00
|
|
|
***************************************/
|
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)
|
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";
|
2017-10-09 19:26:59 +00:00
|
|
|
#define WELCOME_MESSAGE(exename) "%s %s (%i-bits %s), by %s \n", \
|
|
|
|
exename, PROGRAM_VERSION, g_nbBits, ENDIAN_NAME, author
|
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
|
|
|
|
#define TIMELOOP (TIMELOOP_S * CLOCKS_PER_SEC) /* Minimum timing per iteration */
|
|
|
|
#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[] = "-";
|
2016-01-10 16:06:34 +00:00
|
|
|
typedef enum { algo_xxh32, algo_xxh64 } algoType;
|
|
|
|
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
|
|
|
/* ************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* Display macros
|
2016-01-04 07:32:38 +00:00
|
|
|
**************************************/
|
2014-10-23 20:36:23 +00:00
|
|
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#define DISPLAYRESULT(...) fprintf(stdout, __VA_ARGS__)
|
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
|
|
|
/* ************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* Local variables
|
2016-01-04 07:32:38 +00:00
|
|
|
**************************************/
|
2016-04-07 23:50:06 +00:00
|
|
|
static U32 g_nbIterations = NBLOOPS;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
|
2016-01-04 07:32:38 +00:00
|
|
|
/* ************************************
|
2015-05-04 21:56:53 +00:00
|
|
|
* Benchmark Functions
|
2016-01-04 07:32:38 +00:00
|
|
|
**************************************/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-28 11:20:23 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-07 23:22:56 +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... */
|
2014-08-15 09:27:04 +00:00
|
|
|
return (U64)statbuf.st_size;
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:12:27 +00:00
|
|
|
typedef U32 (*hashFunction)(const void* buffer, size_t bufferSize, U32 seed);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-06-06 12:12:27 +00:00
|
|
|
static U32 localXXH32(const void* buffer, size_t bufferSize, U32 seed) { return XXH32(buffer, bufferSize, seed); }
|
2015-08-17 14:17:12 +00:00
|
|
|
|
2016-06-06 12:12:27 +00:00
|
|
|
static U32 localXXH64(const void* buffer, size_t bufferSize, U32 seed) { return (U32)XXH64(buffer, bufferSize, seed); }
|
2015-08-17 14:17:12 +00:00
|
|
|
|
|
|
|
static void BMK_benchHash(hashFunction h, const char* hName, const void* buffer, size_t bufferSize)
|
2015-05-07 12:30:27 +00:00
|
|
|
{
|
2018-02-19 00:56:14 +00:00
|
|
|
U32 nbh_perIteration = ((300 MB) / (bufferSize+1)) + 1; /* first loop conservatively aims for 300 MB/s */
|
2016-04-07 23:50:06 +00:00
|
|
|
U32 iterationNb;
|
2015-08-17 14:17:12 +00:00
|
|
|
double fastestH = 100000000.;
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2018-02-17 18:53:52 +00:00
|
|
|
DISPLAYLEVEL(2, "\r%70s\r", ""); /* Clean display line */
|
2015-08-17 14:17:12 +00:00
|
|
|
if (g_nbIterations<1) g_nbIterations=1;
|
2016-04-07 23:19:06 +00:00
|
|
|
for (iterationNb = 1; iterationNb <= g_nbIterations; iterationNb++) {
|
2018-02-19 00:56:14 +00:00
|
|
|
U32 r=0;
|
2016-04-07 23:50:06 +00:00
|
|
|
clock_t cStart;
|
|
|
|
|
2018-02-17 18:53:52 +00:00
|
|
|
DISPLAYLEVEL(2, "%1i-%-17.17s : %10u ->\r", iterationNb, hName, (U32)bufferSize);
|
2016-04-07 23:50:06 +00:00
|
|
|
cStart = clock();
|
|
|
|
while (clock() == cStart); /* starts clock() at its exact beginning */
|
|
|
|
cStart = clock();
|
|
|
|
|
2018-02-19 00:56:14 +00:00
|
|
|
{ U32 i;
|
|
|
|
for (i=0; i<nbh_perIteration; i++)
|
2016-06-06 12:12:27 +00:00
|
|
|
r += h(buffer, bufferSize, i);
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
2018-02-17 18:53:52 +00:00
|
|
|
if (r==0) DISPLAYLEVEL(3,".\r"); /* need to do something with r to avoid compiler "optimizing" away hash function */
|
2018-02-19 00:56:14 +00:00
|
|
|
{ double const timeS = ((double)BMK_clockSpan(cStart) / CLOCKS_PER_SEC) / nbh_perIteration;
|
2016-04-07 23:50:06 +00:00
|
|
|
if (timeS < fastestH) fastestH = timeS;
|
2018-02-17 18:53:52 +00:00
|
|
|
DISPLAYLEVEL(2, "%1i-%-17.17s : %10u -> %8.0f it/s (%7.1f MB/s) \r",
|
2018-02-17 02:58:40 +00:00
|
|
|
iterationNb, hName, (U32)bufferSize,
|
|
|
|
(double)1 / fastestH,
|
|
|
|
((double)bufferSize / (1<<20)) / fastestH );
|
2016-04-07 23:50:06 +00:00
|
|
|
}
|
2018-02-19 00:56:14 +00:00
|
|
|
assert(fastestH > 2./1000000000); /* avoid U32 overflow */
|
|
|
|
nbh_perIteration = (U32)(1 / fastestH) + 1; /* adjust nbh_perIteration to last roughtly one second */
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
2018-02-19 01:54:58 +00:00
|
|
|
DISPLAYLEVEL(1, "%-19.19s : %10u -> %8.0f it/s (%7.1f MB/s) \n", hName, (U32)bufferSize,
|
2018-02-17 02:58:40 +00:00
|
|
|
(double)1 / fastestH,
|
|
|
|
((double)bufferSize / (1<<20)) / fastestH);
|
2018-02-19 01:54:58 +00:00
|
|
|
if (g_displayLevel<1)
|
|
|
|
DISPLAYLEVEL(0, "%u, ", (U32)((double)1 / fastestH));
|
2015-08-17 14:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-19 01:38:48 +00:00
|
|
|
/* BMK_benchMem():
|
|
|
|
* specificTest : 0 == run all tests, 1+ run only specific test
|
|
|
|
* buffer : is supposed 8-bytes aligned (if malloc'ed, it should be)
|
|
|
|
* @return : 0 on success, 1 if error (invalid mode selected) */
|
|
|
|
static int BMK_benchMem(const void* buffer, size_t bufferSize, U32 specificTest)
|
2015-08-17 14:17:12 +00:00
|
|
|
{
|
2018-02-19 01:38:48 +00:00
|
|
|
assert((((size_t)buffer) & 8) == 0); /* ensure alignment */
|
|
|
|
|
2015-08-17 14:17:12 +00:00
|
|
|
/* XXH32 bench */
|
2018-02-19 01:38:48 +00:00
|
|
|
if ((specificTest==0) | (specificTest==1))
|
|
|
|
BMK_benchHash(localXXH32, "XXH32", buffer, bufferSize);
|
2015-05-07 12:30:27 +00:00
|
|
|
|
|
|
|
/* Bench XXH32 on Unaligned input */
|
2018-02-19 01:38:48 +00:00
|
|
|
if ((specificTest==0) | (specificTest==2))
|
|
|
|
BMK_benchHash(localXXH32, "XXH32 unaligned", ((const char*)buffer)+1, bufferSize);
|
2015-05-07 12:30:27 +00:00
|
|
|
|
|
|
|
/* Bench XXH64 */
|
2018-02-19 01:38:48 +00:00
|
|
|
if ((specificTest==0) | (specificTest==3))
|
|
|
|
BMK_benchHash(localXXH64, "XXH64", buffer, bufferSize);
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2015-08-17 14:17:12 +00:00
|
|
|
/* Bench XXH64 on Unaligned input */
|
2018-02-19 01:38:48 +00:00
|
|
|
if ((specificTest==0) | (specificTest==4))
|
|
|
|
BMK_benchHash(localXXH64, "XXH64 unaligned", ((const char*)buffer)+3, bufferSize);
|
|
|
|
|
|
|
|
if (specificTest > 4) {
|
|
|
|
DISPLAY("benchmark mode invalid \n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-06 12:12:27 +00:00
|
|
|
static size_t BMK_selectBenchedSize(const char* fileName)
|
|
|
|
{ U64 const inFileSize = BMK_GetFileSize(fileName);
|
|
|
|
size_t benchedSize = (size_t) BMK_findMaxMem(inFileSize);
|
|
|
|
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
|
|
|
|
if (benchedSize < inFileSize) {
|
|
|
|
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", fileName, (int)(benchedSize>>20));
|
|
|
|
}
|
|
|
|
return benchedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-19 01:38:48 +00:00
|
|
|
static int BMK_benchFiles(const char** fileNamesTable, int nbFiles, U32 specificTest)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2018-02-19 01:38:48 +00:00
|
|
|
int result = 0;
|
2016-06-06 12:12:27 +00:00
|
|
|
int fileIdx;
|
2018-02-19 01:38:48 +00:00
|
|
|
|
2016-06-06 12:12:27 +00:00
|
|
|
for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
|
|
|
|
const char* const inFileName = fileNamesTable[fileIdx];
|
|
|
|
FILE* const inFile = fopen( inFileName, "rb" );
|
|
|
|
size_t const benchedSize = BMK_selectBenchedSize(inFileName);
|
2016-08-14 02:28:17 +00:00
|
|
|
char* const buffer = (char*)calloc(benchedSize+16+3, 1);
|
2017-10-09 19:26:59 +00:00
|
|
|
void* const alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF); /* align on next 16 bytes */
|
2016-06-06 12:12:27 +00:00
|
|
|
|
|
|
|
/* Checks */
|
2016-04-07 23:19:06 +00:00
|
|
|
if ((inFile==NULL) || (inFileName==NULL)) {
|
2018-02-17 18:53:52 +00:00
|
|
|
DISPLAY("Pb opening %s\n", inFileName);
|
2016-06-06 12:25:18 +00:00
|
|
|
free(buffer);
|
2014-08-15 09:27:04 +00:00
|
|
|
return 11;
|
|
|
|
}
|
2016-04-07 23:19:06 +00:00
|
|
|
if(!buffer) {
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY("\nError: not enough memory!\n");
|
|
|
|
fclose(inFile);
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2015-01-01 16:10:04 +00:00
|
|
|
/* Fill input buffer */
|
2018-02-19 01:54:58 +00:00
|
|
|
DISPLAYLEVEL(1, "\rLoading %s... \n", inFileName);
|
2016-05-28 00:14:28 +00:00
|
|
|
{ size_t const readSize = fread(alignedBuffer, 1, benchedSize, inFile);
|
|
|
|
fclose(inFile);
|
|
|
|
if(readSize != benchedSize) {
|
|
|
|
DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
|
|
|
|
free(buffer);
|
|
|
|
return 13;
|
|
|
|
} }
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* bench */
|
2018-02-19 01:38:48 +00:00
|
|
|
result |= BMK_benchMem(alignedBuffer, benchedSize, specificTest);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
free(buffer);
|
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2018-02-19 01:38:48 +00:00
|
|
|
return result;
|
2015-05-07 12:30:27 +00:00
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-19 01:38:48 +00:00
|
|
|
static int BMK_benchInternal(size_t keySize, int specificTest)
|
2015-05-07 12:30:27 +00:00
|
|
|
{
|
2018-02-19 01:38:48 +00:00
|
|
|
void* const buffer = calloc(keySize+3, 1);
|
2016-04-07 23:19:06 +00:00
|
|
|
if(!buffer) {
|
2015-05-07 12:30:27 +00:00
|
|
|
DISPLAY("\nError: not enough memory!\n");
|
|
|
|
return 12;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* bench */
|
2018-02-19 01:54:58 +00:00
|
|
|
DISPLAYLEVEL(1, "Sample of ");
|
2018-02-19 01:38:48 +00:00
|
|
|
if (keySize > 10 KB) {
|
2018-02-19 01:54:58 +00:00
|
|
|
DISPLAYLEVEL(1, "%u KB", (U32)(keySize >> 10));
|
2018-02-19 01:38:48 +00:00
|
|
|
} else {
|
2018-02-19 01:54:58 +00:00
|
|
|
DISPLAYLEVEL(1, "%u bytes", (U32)keySize);
|
2018-02-19 01:38:48 +00:00
|
|
|
}
|
2018-02-19 01:54:58 +00:00
|
|
|
DISPLAYLEVEL(1, "... \n");
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2018-02-19 01:38:48 +00:00
|
|
|
{ int const result = BMK_benchMem(buffer, keySize, specificTest);
|
|
|
|
free(buffer);
|
|
|
|
return result;
|
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void BMK_checkResult(U32 r1, U32 r2)
|
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
2018-02-17 18:53:52 +00:00
|
|
|
if (r1==r2) {
|
|
|
|
DISPLAYLEVEL(3, "\rTest%3i : %08X == %08X ok ", nbTests, r1, r2);
|
|
|
|
} else {
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY("\rERROR : Test%3i : %08X <> %08X !!!!! \n", nbTests, r1, r2);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void BMK_checkResult64(U64 r1, U64 r2)
|
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (r1!=r2) {
|
2017-12-26 01:57:24 +00:00
|
|
|
DISPLAY("\rERROR : Test%3i : 64-bit values non equals !!!!! \n", nbTests);
|
2015-05-07 14:27:27 +00:00
|
|
|
DISPLAY("\r %08X%08X != %08X%08X \n", (U32)(r1>>32), (U32)r1, (U32)(r2>>32), (U32)r2);
|
2014-08-15 09:27:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-10 07:36:58 +00:00
|
|
|
static void BMK_testSequence64(void* sentence, size_t len, U64 seed, U64 Nresult)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH64_state_t state;
|
2014-08-15 09:27:04 +00:00
|
|
|
U64 Dresult;
|
2016-08-10 23:32:10 +00:00
|
|
|
size_t pos;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
Dresult = XXH64(sentence, len, seed);
|
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH64_reset(&state, seed);
|
|
|
|
XXH64_update(&state, sentence, len);
|
|
|
|
Dresult = XXH64_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH64_reset(&state, seed);
|
2018-02-17 18:53:52 +00:00
|
|
|
for (pos=0; pos<len; pos++)
|
|
|
|
XXH64_update(&state, ((char*)sentence)+pos, 1);
|
2016-05-28 00:14:28 +00:00
|
|
|
Dresult = XXH64_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
static void BMK_testSequence(const void* sequence, size_t len, U32 seed, U32 Nresult)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_state_t state;
|
2014-08-15 09:27:04 +00:00
|
|
|
U32 Dresult;
|
2016-08-10 23:32:10 +00:00
|
|
|
size_t pos;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
Dresult = XXH32(sequence, len, seed);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult(Dresult, Nresult);
|
|
|
|
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_reset(&state, seed);
|
|
|
|
XXH32_update(&state, sequence, len);
|
|
|
|
Dresult = XXH32_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult(Dresult, Nresult);
|
|
|
|
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_reset(&state, seed);
|
2018-02-17 18:53:52 +00:00
|
|
|
for (pos=0; pos<len; pos++)
|
|
|
|
XXH32_update(&state, ((const char*)sequence)+pos, 1);
|
2016-05-28 00:14:28 +00:00
|
|
|
Dresult = XXH32_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult(Dresult, Nresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define SANITY_BUFFER_SIZE 101
|
|
|
|
static void BMK_sanityCheck(void)
|
|
|
|
{
|
2015-05-07 14:27:27 +00:00
|
|
|
static const U32 prime = 2654435761U;
|
2016-04-08 01:57:04 +00:00
|
|
|
BYTE sanityBuffer[SANITY_BUFFER_SIZE];
|
2015-05-07 14:27:27 +00:00
|
|
|
U32 byteGen = prime;
|
|
|
|
|
2016-04-08 01:57:04 +00:00
|
|
|
int i;
|
2016-04-07 23:19:06 +00:00
|
|
|
for (i=0; i<SANITY_BUFFER_SIZE; i++) {
|
2015-05-07 14:27:27 +00:00
|
|
|
sanityBuffer[i] = (BYTE)(byteGen>>24);
|
|
|
|
byteGen *= byteGen;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BMK_testSequence(NULL, 0, 0, 0x02CC5D05);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(NULL, 0, prime, 0x36B78AE7);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 1, 0, 0xB85CBEE5);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 1, prime, 0xD5845D64);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 14, 0, 0xE5AA0AB4);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 14, prime, 0x4481951D);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence(sanityBuffer, SANITY_BUFFER_SIZE, 0, 0x1F1AA412);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(sanityBuffer, SANITY_BUFFER_SIZE, prime, 0x498EC8E2);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
BMK_testSequence64(NULL , 0, 0, 0xEF46DB3751D8E999ULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(NULL , 0, prime, 0xAC75FDA2929B17EFULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 1, 0, 0x4FCE394CC88952D8ULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 1, prime, 0x739840CB819FA723ULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 14, 0, 0xCFFA8DB881BC3A3DULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 14, prime, 0x5B9611585EFCC9CBULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, SANITY_BUFFER_SIZE, 0, 0x0EAB543384F878ADULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, SANITY_BUFFER_SIZE, prime, 0xCAA65939306F1E21ULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
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
|
|
|
|
**********************************************************/
|
|
|
|
|
2016-01-10 16:06:34 +00:00
|
|
|
static void BMK_display_LittleEndian(const void* ptr, size_t length)
|
|
|
|
{
|
|
|
|
const BYTE* p = (const BYTE*)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)
|
|
|
|
{
|
2015-05-05 00:01:10 +00:00
|
|
|
const BYTE* p = (const BYTE*)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
|
|
|
|
2016-01-20 14:06:35 +00:00
|
|
|
static void BMK_hashStream(void* xxhHashValue, const algoType hashType, FILE* inFile, void* buffer, size_t blockSize)
|
|
|
|
{
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH64_state_t state64;
|
|
|
|
XXH32_state_t state32;
|
2016-01-20 14:06:35 +00:00
|
|
|
size_t readSize;
|
|
|
|
|
|
|
|
/* Init */
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
|
|
|
|
XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
|
2016-01-20 14:06:35 +00:00
|
|
|
|
|
|
|
/* Load file & update hash */
|
|
|
|
readSize = 1;
|
2016-04-07 23:19:06 +00:00
|
|
|
while (readSize) {
|
2016-01-20 14:06:35 +00:00
|
|
|
readSize = fread(buffer, 1, blockSize, inFile);
|
|
|
|
switch(hashType)
|
|
|
|
{
|
|
|
|
case algo_xxh32:
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_update(&state32, buffer, readSize);
|
2016-01-20 14:06:35 +00:00
|
|
|
break;
|
|
|
|
case algo_xxh64:
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH64_update(&state64, buffer, readSize);
|
2016-01-20 14:06:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(hashType)
|
|
|
|
{
|
|
|
|
case algo_xxh32:
|
2016-05-28 00:14:28 +00:00
|
|
|
{ U32 const h32 = XXH32_digest(&state32);
|
2016-01-20 14:06:35 +00:00
|
|
|
memcpy(xxhHashValue, &h32, sizeof(h32));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case algo_xxh64:
|
2016-05-28 00:14:28 +00:00
|
|
|
{ U64 const h64 = XXH64_digest(&state64);
|
2016-01-20 14:06:35 +00:00
|
|
|
memcpy(xxhHashValue, &h64, sizeof(h64));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
FILE* inFile;
|
|
|
|
size_t const blockSize = 64 KB;
|
2015-08-07 23:22:56 +00:00
|
|
|
void* buffer;
|
2016-01-20 14:06:35 +00:00
|
|
|
U32 h32 = 0;
|
|
|
|
U64 h64 = 0;
|
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;
|
|
|
|
SET_BINARY_MODE(stdin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
inFile = fopen( fileName, "rb" );
|
2016-04-07 23:19:06 +00:00
|
|
|
if (inFile==NULL) {
|
2014-09-25 20:22:59 +00:00
|
|
|
DISPLAY( "Pb opening %s\n", fileName);
|
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 */
|
2015-08-07 23:22:56 +00:00
|
|
|
buffer = malloc(blockSize);
|
2016-04-07 23:19:06 +00:00
|
|
|
if(!buffer) {
|
2014-09-25 20:22:59 +00:00
|
|
|
DISPLAY("\nError: not enough memory!\n");
|
|
|
|
fclose(inFile);
|
2016-01-10 16:06:34 +00:00
|
|
|
return 1;
|
2014-09-25 20:22:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 23:30:08 +00:00
|
|
|
/* loading notification */
|
2016-04-07 23:19:06 +00:00
|
|
|
{ const size_t fileNameSize = strlen(fileName);
|
2016-01-10 23:30:08 +00:00
|
|
|
const char* const fileNameEnd = fileName + fileNameSize;
|
|
|
|
const size_t maxInfoFilenameSize = fileNameSize > 30 ? 30 : fileNameSize;
|
|
|
|
size_t infoFilenameSize = 1;
|
|
|
|
while ( (infoFilenameSize < maxInfoFilenameSize)
|
2016-02-19 14:33:22 +00:00
|
|
|
&&(fileNameEnd[-1-infoFilenameSize] != '/')
|
|
|
|
&&(fileNameEnd[-1-infoFilenameSize] != '\\') )
|
2016-01-10 23:30:08 +00:00
|
|
|
infoFilenameSize++;
|
2017-08-21 20:27:49 +00:00
|
|
|
DISPLAY("\rLoading %s... \r", fileNameEnd - infoFilenameSize);
|
2016-01-10 23:30:08 +00:00
|
|
|
|
2017-08-21 20:27:49 +00:00
|
|
|
/* Load file & update hash */
|
|
|
|
switch(hashType)
|
|
|
|
{
|
|
|
|
case algo_xxh32:
|
|
|
|
BMK_hashStream(&h32, algo_xxh32, inFile, buffer, blockSize);
|
|
|
|
break;
|
|
|
|
case algo_xxh64:
|
|
|
|
BMK_hashStream(&h64, algo_xxh64, inFile, buffer, blockSize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-01-20 14:06:35 +00:00
|
|
|
|
2017-08-21 20:27:49 +00:00
|
|
|
fclose(inFile);
|
|
|
|
free(buffer);
|
|
|
|
DISPLAY("%s \r", fileNameEnd - infoFilenameSize); /* erase line */
|
|
|
|
}
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* display Hash */
|
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;
|
2016-01-14 02:01:39 +00:00
|
|
|
XXH32_canonicalFromHash(&hcbe32, h32);
|
|
|
|
displayEndianess==big_endian ?
|
|
|
|
BMK_display_BigEndian(&hcbe32, sizeof(hcbe32)) : BMK_display_LittleEndian(&hcbe32, sizeof(hcbe32));
|
2016-01-10 23:30:08 +00:00
|
|
|
DISPLAYRESULT(" %s\n", fileName);
|
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;
|
2016-01-14 02:01:39 +00:00
|
|
|
XXH64_canonicalFromHash(&hcbe64, h64);
|
|
|
|
displayEndianess==big_endian ?
|
|
|
|
BMK_display_BigEndian(&hcbe64, sizeof(hcbe64)) : BMK_display_LittleEndian(&hcbe64, sizeof(hcbe64));
|
2016-01-10 23:30:08 +00:00
|
|
|
DISPLAYRESULT(" %s\n", fileName);
|
2014-09-25 20:22:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 16:06:34 +00:00
|
|
|
static int BMK_hashFiles(const char** fnList, int fnTotal,
|
|
|
|
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);
|
2016-01-10 23:30:08 +00:00
|
|
|
DISPLAY("\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,
|
|
|
|
GetLine_outOfMemory,
|
|
|
|
} GetLineResult;
|
|
|
|
|
2016-01-20 14:45:52 +00:00
|
|
|
typedef enum {
|
|
|
|
CanonicalFromString_ok,
|
|
|
|
CanonicalFromString_invalidFormat,
|
|
|
|
} CanonicalFromStringResult;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ParseLine_ok,
|
|
|
|
ParseLine_invalidFormat,
|
|
|
|
} ParseLineResult;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
LineStatus_hashOk,
|
|
|
|
LineStatus_hashFailed,
|
|
|
|
LineStatus_failedToOpen,
|
|
|
|
} LineStatus;
|
|
|
|
|
|
|
|
typedef union {
|
2016-05-28 00:14:28 +00:00
|
|
|
XXH32_canonical_t xxh32;
|
|
|
|
XXH64_canonical_t xxh64;
|
2016-01-20 14:45:52 +00:00
|
|
|
} Canonical;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Canonical canonical;
|
|
|
|
const char* filename;
|
|
|
|
int xxhBits; /* canonical type : 32:xxh32, 64:xxh64 */
|
|
|
|
} 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;
|
|
|
|
int strictMode;
|
|
|
|
int statusOnly;
|
|
|
|
int warn;
|
|
|
|
int quiet;
|
|
|
|
ParseFileReport report;
|
|
|
|
} ParseFileArg;
|
|
|
|
|
|
|
|
|
2016-01-22 05:53:32 +00:00
|
|
|
/* Read line from stream.
|
|
|
|
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;
|
|
|
|
int len = 0;
|
|
|
|
|
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) {
|
2016-01-22 05:53:32 +00:00
|
|
|
/* If we meet EOF before first character, returns GetLine_eof,
|
|
|
|
* 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. */
|
2016-04-07 23:19:06 +00:00
|
|
|
if (len+1 >= *lineMax) {
|
2016-01-22 05:53:32 +00:00
|
|
|
char* newLineBuf = NULL;
|
|
|
|
int newBufSize = *lineMax;
|
|
|
|
|
|
|
|
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;
|
|
|
|
*lineMax = newBufSize;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Converts one hexadecimal character to integer.
|
|
|
|
* Returns -1, if given character is not hexadecimal.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Converts XXH32 canonical hexadecimal string hashStr to big endian unsigned char array dst.
|
|
|
|
* Returns CANONICAL_FROM_STRING_INVALID_FORMAT, if hashStr is not well formatted.
|
|
|
|
* Returns CANONICAL_FROM_STRING_OK, if hashStr is parsed successfully.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse single line of xxHash checksum file.
|
|
|
|
* Returns PARSE_LINE_ERROR_INVALID_FORMAT, if line is not well formatted.
|
|
|
|
* Returns PARSE_LINE_OK if line is parsed successfully.
|
|
|
|
* And members of parseLine will be filled by parsed values.
|
|
|
|
*
|
|
|
|
* - line must be ended with '\0'.
|
|
|
|
* - Since parsedLine.filename will point within given argument `line`,
|
|
|
|
* users must keep `line`s content during they are using parsedLine.
|
|
|
|
*
|
|
|
|
* Given xxHash checksum line should have the following format:
|
|
|
|
*
|
|
|
|
* <8 or 16 hexadecimal char> <space> <space> <filename...> <'\0'>
|
|
|
|
*/
|
|
|
|
static ParseLineResult parseLine(ParsedLine* parsedLine, const char* line)
|
|
|
|
{
|
|
|
|
const char* const firstSpace = strchr(line, ' ');
|
|
|
|
const char* const secondSpace = firstSpace + 1;
|
|
|
|
|
|
|
|
parsedLine->filename = NULL;
|
|
|
|
parsedLine->xxhBits = 0;
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (firstSpace == NULL || *secondSpace != ' ') return ParseLine_invalidFormat;
|
2016-01-20 14:45:52 +00:00
|
|
|
|
|
|
|
switch (firstSpace - line)
|
|
|
|
{
|
|
|
|
case 8:
|
2016-04-07 23:19:06 +00:00
|
|
|
{ XXH32_canonical_t* xxh32c = &parsedLine->canonical.xxh32;
|
2016-01-20 14:45:52 +00:00
|
|
|
if (canonicalFromString(xxh32c->digest, sizeof(xxh32c->digest), line)
|
2016-04-07 23:19:06 +00:00
|
|
|
!= CanonicalFromString_ok) {
|
2016-01-20 14:45:52 +00:00
|
|
|
return ParseLine_invalidFormat;
|
|
|
|
}
|
|
|
|
parsedLine->xxhBits = 32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 16:
|
2016-04-07 23:19:06 +00:00
|
|
|
{ XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64;
|
2016-01-20 14:45:52 +00:00
|
|
|
if (canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), line)
|
2016-04-07 23:19:06 +00:00
|
|
|
!= CanonicalFromString_ok) {
|
2016-01-20 14:45:52 +00:00
|
|
|
return ParseLine_invalidFormat;
|
|
|
|
}
|
|
|
|
parsedLine->xxhBits = 64;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ParseLine_invalidFormat;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedLine->filename = secondSpace + 1;
|
|
|
|
return ParseLine_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-28 00:14:28 +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
|
|
|
FILE* fp = NULL;
|
|
|
|
LineStatus lineStatus = LineStatus_hashFailed;
|
2016-01-22 05:53:32 +00:00
|
|
|
GetLineResult getLineResult;
|
2016-01-20 14:45:52 +00:00
|
|
|
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) {
|
2016-01-20 14:45:52 +00:00
|
|
|
/* This is unlikely happen, but md5sum.c has this
|
|
|
|
* error check. */
|
|
|
|
DISPLAY("%s : too many checksum lines\n", inFileName);
|
|
|
|
report->quit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-22 05:53:32 +00:00
|
|
|
getLineResult = getLine(&parseFileArg->lineBuf, &parseFileArg->lineMax,
|
|
|
|
parseFileArg->inFile);
|
2016-04-07 23:19:06 +00:00
|
|
|
if (getLineResult != GetLine_ok) {
|
|
|
|
if (getLineResult == GetLine_eof) break;
|
2016-01-22 05:53:32 +00:00
|
|
|
|
|
|
|
switch (getLineResult)
|
|
|
|
{
|
2016-01-22 06:06:25 +00:00
|
|
|
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. */
|
|
|
|
break;
|
|
|
|
|
2016-01-22 05:53:32 +00:00
|
|
|
default:
|
|
|
|
DISPLAY("%s : %lu: unknown error\n", inFileName, lineNumber);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GetLine_exceedMaxLineLength:
|
|
|
|
DISPLAY("%s : %lu: too long line\n", inFileName, lineNumber);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GetLine_outOfMemory:
|
|
|
|
DISPLAY("%s : %lu: out of memory\n", inFileName, lineNumber);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
report->quit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-01-22 05:37:27 +00:00
|
|
|
DISPLAY("%s : %lu: improperly formatted XXHASH checksum line\n"
|
2016-01-22 04:22:35 +00:00
|
|
|
, 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) {
|
2016-01-22 05:37:27 +00:00
|
|
|
DISPLAY("%s : %lu: improperly formatted XXHASH checksum line (XXH32/64)\n"
|
2016-01-22 04:22:35 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(parsedLine.filename, "rb");
|
2016-04-07 23:19:06 +00:00
|
|
|
if (fp == NULL) {
|
2016-01-20 14:45:52 +00:00
|
|
|
lineStatus = LineStatus_failedToOpen;
|
2016-05-28 00:14:28 +00:00
|
|
|
} else {
|
2016-01-20 14:45:52 +00:00
|
|
|
lineStatus = LineStatus_hashFailed;
|
|
|
|
switch (parsedLine.xxhBits)
|
|
|
|
{
|
|
|
|
case 32:
|
2016-04-07 23:19:06 +00:00
|
|
|
{ XXH32_hash_t xxh;
|
2016-01-20 14:45:52 +00:00
|
|
|
BMK_hashStream(&xxh, algo_xxh32, fp, parseFileArg->blockBuf, parseFileArg->blockSize);
|
2016-04-07 23:19:06 +00:00
|
|
|
if (xxh == 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:
|
2016-04-07 23:19:06 +00:00
|
|
|
{ XXH64_hash_t xxh;
|
2016-01-20 14:45:52 +00:00
|
|
|
BMK_hashStream(&xxh, algo_xxh64, fp, parseFileArg->blockBuf, parseFileArg->blockSize);
|
2016-04-07 23:19:06 +00:00
|
|
|
if (xxh == 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;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (lineStatus)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
DISPLAY("%s : unknown error\n", inFileName);
|
|
|
|
report->quit = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LineStatus_failedToOpen:
|
|
|
|
report->nOpenOrReadFailures++;
|
2016-04-07 23:19:06 +00:00
|
|
|
if (!parseFileArg->statusOnly) {
|
2016-01-22 05:37:27 +00:00
|
|
|
DISPLAYRESULT("%s : %lu: FAILED open or read %s\n"
|
2016-01-22 04:22:35 +00:00
|
|
|
, inFileName, lineNumber, parsedLine.filename);
|
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,
|
|
|
|
U32 strictMode,
|
|
|
|
U32 statusOnly,
|
|
|
|
U32 warn,
|
|
|
|
U32 quiet)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-25 09:59:01 +00:00
|
|
|
/* note : stdinName is special constant pointer. It is not a string. */
|
2016-04-07 23:19:06 +00:00
|
|
|
if (inFileName == stdinName) {
|
2016-01-25 09:59:01 +00:00
|
|
|
/* note : Since we expect text input for xxhash -c mode,
|
|
|
|
* Don't set binary mode for stdin */
|
2016-01-20 14:45:52 +00:00
|
|
|
inFile = stdin;
|
2016-04-07 23:19:06 +00:00
|
|
|
} else {
|
2016-01-20 14:45:52 +00:00
|
|
|
inFile = fopen( inFileName, "rt" );
|
|
|
|
}
|
|
|
|
|
2016-04-07 23:19:06 +00:00
|
|
|
if (inFile == NULL) {
|
2016-01-20 14:45:52 +00:00
|
|
|
DISPLAY( "Pb opening %s\n", inFileName);
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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) {
|
2016-01-22 04:22:35 +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) {
|
2016-01-22 05:37:27 +00:00
|
|
|
DISPLAYRESULT("%lu lines are improperly formatted\n"
|
2016-01-22 04:22:35 +00:00
|
|
|
, report->nImproperlyFormattedLines);
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2016-04-07 23:19:06 +00:00
|
|
|
if (report->nOpenOrReadFailures) {
|
2016-01-22 05:37:27 +00:00
|
|
|
DISPLAYRESULT("%lu listed files could not be read\n"
|
2016-01-22 04:22:35 +00:00
|
|
|
, report->nOpenOrReadFailures);
|
2016-01-20 14:45:52 +00:00
|
|
|
}
|
2016-04-07 23:19:06 +00:00
|
|
|
if (report->nMismatchedChecksums) {
|
2016-01-22 05:37:27 +00:00
|
|
|
DISPLAYRESULT("%lu computed checksums did NOT match\n"
|
2016-01-22 04:22:35 +00:00
|
|
|
, report->nMismatchedChecksums);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int checkFiles(const char** fnList, int fnTotal,
|
|
|
|
const endianess displayEndianess,
|
|
|
|
U32 strictMode,
|
|
|
|
U32 statusOnly,
|
|
|
|
U32 warn,
|
|
|
|
U32 quiet)
|
|
|
|
{
|
|
|
|
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) );
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY( "Usage :\n");
|
2016-01-07 22:54:51 +00:00
|
|
|
DISPLAY( " %s [arg] [filenames]\n", exename);
|
2014-10-29 12:55:58 +00:00
|
|
|
DISPLAY( "When no filename provided, or - provided : use stdin as input\n");
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY( "Arguments :\n");
|
2016-01-10 16:06:34 +00:00
|
|
|
DISPLAY( " -H# : hash selection : 0=32bits, 1=64bits (default: %i)\n", (int)g_defaultAlgo);
|
2016-02-19 14:21:43 +00:00
|
|
|
DISPLAY( " -c : read xxHash sums from the [filenames] and check them\n");
|
2016-01-14 02:01:39 +00:00
|
|
|
DISPLAY( " -h : help \n");
|
2016-01-10 16:06:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int usage_advanced(const char* exename)
|
|
|
|
{
|
|
|
|
usage(exename);
|
|
|
|
DISPLAY( "Advanced :\n");
|
2016-02-22 13:17:12 +00:00
|
|
|
DISPLAY( " --little-endian : hash printed using little endian convention (default: big endian)\n");
|
|
|
|
DISPLAY( " -V, --version : display version\n");
|
|
|
|
DISPLAY( " -h, --help : display long help and exit\n");
|
2014-09-25 20:22:59 +00:00
|
|
|
DISPLAY( " -b : benchmark mode \n");
|
2014-10-18 11:03:38 +00:00
|
|
|
DISPLAY( " -i# : number of iterations (benchmark mode; default %i)\n", g_nbIterations);
|
2016-02-19 14:21:43 +00:00
|
|
|
DISPLAY( "\n");
|
|
|
|
DISPLAY( "The following four options are useful only when verifying checksums (-c):\n");
|
|
|
|
DISPLAY( "--strict : don't print OK for each successfully verified file\n");
|
|
|
|
DISPLAY( "--status : don't output anything, status code shows success\n");
|
|
|
|
DISPLAY( "--quiet : 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
|
|
|
{
|
|
|
|
DISPLAY("Wrong parameters\n");
|
|
|
|
usage(exename);
|
2014-09-25 20:22:59 +00:00
|
|
|
return 1;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 01:38:48 +00:00
|
|
|
/*! readU32FromChar() :
|
|
|
|
@return : unsigned integer value read from input in `char` format,
|
|
|
|
0 is no figure at *stringPtr position.
|
|
|
|
Interprets K, KB, KiB, M, MB and MiB suffix.
|
|
|
|
Modifies `*stringPtr`, advancing it to position where reading stopped.
|
|
|
|
Note : function result can overflow if digit string > MAX_UINT */
|
|
|
|
static unsigned readU32FromChar(const char** stringPtr)
|
|
|
|
{
|
|
|
|
unsigned result = 0;
|
|
|
|
while ((**stringPtr >='0') && (**stringPtr <='9'))
|
|
|
|
result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
|
|
|
|
if ((**stringPtr=='K') || (**stringPtr=='M')) {
|
|
|
|
result <<= 10;
|
|
|
|
if (**stringPtr=='M') result <<= 10;
|
|
|
|
(*stringPtr)++ ;
|
|
|
|
if (**stringPtr=='i') (*stringPtr)++;
|
|
|
|
if (**stringPtr=='B') (*stringPtr)++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
int main(int argc, const char** argv)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2014-10-29 12:55:58 +00:00
|
|
|
int i, filenamesStart=0;
|
2016-08-10 05:00:29 +00:00
|
|
|
const char* const exename = argv[0];
|
2014-09-25 20:22:59 +00:00
|
|
|
U32 benchmarkMode = 0;
|
2016-01-20 14:45:52 +00:00
|
|
|
U32 fileCheckMode = 0;
|
|
|
|
U32 strictMode = 0;
|
|
|
|
U32 statusOnly = 0;
|
|
|
|
U32 warn = 0;
|
|
|
|
U32 quiet = 0;
|
2018-02-19 01:38:48 +00:00
|
|
|
U32 specificTest = 0;
|
|
|
|
size_t keySize = XXH_DEFAULT_SAMPLE_SIZE;
|
2016-01-10 16:06:34 +00:00
|
|
|
algoType algo = g_defaultAlgo;
|
|
|
|
endianess displayEndianess = big_endian;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-08-12 17:24:35 +00:00
|
|
|
/* special case : xxh32sum default to 32 bits checksum */
|
2016-01-10 16:06:34 +00:00
|
|
|
if (strstr(exename, "xxh32sum") != NULL) algo = algo_xxh32;
|
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
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
if(!argument) continue; /* Protection, if argument empty */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2016-01-10 16:06:34 +00:00
|
|
|
if (!strcmp(argument, "--little-endian")) { displayEndianess = little_endian; continue; }
|
2016-01-20 14:45:52 +00:00
|
|
|
if (!strcmp(argument, "--check")) { fileCheckMode = 1; continue; }
|
|
|
|
if (!strcmp(argument, "--strict")) { strictMode = 1; continue; }
|
|
|
|
if (!strcmp(argument, "--status")) { statusOnly = 1; continue; }
|
|
|
|
if (!strcmp(argument, "--quiet")) { quiet = 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); }
|
2016-04-07 23:50:06 +00:00
|
|
|
if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE(exename)); 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 */
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
specificTest = readU32FromChar(&argument); /* can select one specific benchmark test (hidden option) */
|
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) );
|
2014-10-29 12:55:58 +00:00
|
|
|
BMK_sanityCheck();
|
2018-02-19 01:38:48 +00:00
|
|
|
if (filenamesStart==0) return BMK_benchInternal(keySize, specificTest);
|
|
|
|
return BMK_benchFiles(argv+filenamesStart, argc-filenamesStart, specificTest);
|
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,
|
|
|
|
displayEndianess, strictMode, statusOnly, warn, 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
|
|
|
}
|
2016-06-06 12:12:27 +00:00
|
|
|
|
|
|
|
#endif /* XXHASH_C_2097394837 */
|