#include "xsum_config.h"
#include "xsum_arch.h"
#include "xsum_os_specific.h"
#include "xsum_output.h"
#include "xsum_sanity_check.h"
#ifdef XXH_INLINE_ALL
# include "xsum_os_specific.c"
# include "xsum_output.c"
# include "xsum_sanity_check.c"
#endif
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#define XXH_STATIC_LINKING_ONLY
#include "../xxhash.h"
#ifdef XXHSUM_DISPATCH
# include "../xxh_x86dispatch.h"
#endif
static unsigned XSUM_isLittleEndian(void)
{
const union { XSUM_U32 u; XSUM_U8 c[4]; } one = { 1 };
return one.c[0];
}
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 (XSUM_isLittleEndian() ? g_lename : g_bename)
static const char author[] = "Yann Collet";
#define WELCOME_MESSAGE(exename) "%s %s by %s \n", exename, XSUM_PROGRAM_VERSION, author
#define FULL_WELCOME_MESSAGE(exename) "%s %s by %s \n" \
"compiled as %i-bit %s %s with " XSUM_CC_VERSION_FMT " \n", \
exename, XSUM_PROGRAM_VERSION, author, \
g_nbBits, XSUM_ARCH, ENDIAN_NAME, XSUM_CC_VERSION
#define KB *( 1<<10)
#define MB *( 1<<20)
#define GB *(1U<<30)
static size_t XSUM_DEFAULT_SAMPLE_SIZE = 100 KB;
#define NBLOOPS 3
#define TIMELOOP_S 1
#define TIMELOOP (TIMELOOP_S * CLOCKS_PER_SEC)
#define TIMELOOP_MIN (TIMELOOP / 2)
#define XXHSUM32_DEFAULT_SEED 0
#define XXHSUM64_DEFAULT_SEED 0
#define MAX_MEM (2 GB - 64 MB)
static const char stdinName[] = "-";
static const char stdinFileName[] = "stdin";
typedef enum { algo_xxh32=0, algo_xxh64=1, algo_xxh128=2 } AlgoSelected;
static AlgoSelected g_defaultAlgo = algo_xxh64;
#define DEFAULT_LINE_LENGTH (sizeof(XXH64_hash_t) * 2 + 2 + 4096 + 1)
#define MAX_LINE_LENGTH (32 KB)
static XSUM_U32 g_nbIterations = NBLOOPS;
static clock_t XSUM_clockSpan( clock_t start )
{
return clock() - start;
}
static size_t XSUM_findMaxMem(XSUM_U64 requiredMem)
{
size_t const step = 64 MB;
void* testmem = NULL;
requiredMem = (((requiredMem >> 26) + 1) << 26);
requiredMem += 2*step;
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
while (!testmem) {
if (requiredMem > step) requiredMem -= step;
else requiredMem >>= 1;
testmem = malloc ((size_t)requiredMem);
}
free (testmem);
if (requiredMem > step) requiredMem -= step;
else requiredMem >>= 1;
return (size_t)requiredMem;
}
static char* XSUM_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) {
memcpy(buf, s1, len1);
memcpy(buf + len1, s2, len2 + 1);
}
return buf;
}
}
static XSUM_U8 g_benchSecretBuf[XXH3_SECRET_SIZE_MIN];
typedef XSUM_U32 (*hashFunction)(const void* buffer, size_t bufferSize, XSUM_U32 seed);
static XSUM_U32 localXXH32(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
return XXH32(buffer, bufferSize, seed);
}
static XSUM_U32 localXXH64(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
return (XSUM_U32)XXH64(buffer, bufferSize, seed);
}
static XSUM_U32 localXXH3_64b(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
(void)seed;
return (XSUM_U32)XXH3_64bits(buffer, bufferSize);
}
static XSUM_U32 localXXH3_64b_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
return (XSUM_U32)XXH3_64bits_withSeed(buffer, bufferSize, seed);
}
static XSUM_U32 localXXH3_64b_secret(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
(void)seed;
return (XSUM_U32)XXH3_64bits_withSecret(buffer, bufferSize, g_benchSecretBuf, sizeof(g_benchSecretBuf));
}
static XSUM_U32 localXXH3_128b(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
(void)seed;
return (XSUM_U32)(XXH3_128bits(buffer, bufferSize).low64);
}
static XSUM_U32 localXXH3_128b_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
return (XSUM_U32)(XXH3_128bits_withSeed(buffer, bufferSize, seed).low64);
}
static XSUM_U32 localXXH3_128b_secret(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
(void)seed;
return (XSUM_U32)(XXH3_128bits_withSecret(buffer, bufferSize, g_benchSecretBuf, sizeof(g_benchSecretBuf)).low64);
}
static XSUM_U32 localXXH3_stream(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
XXH3_state_t state;
(void)seed;
XXH3_64bits_reset(&state);
XXH3_64bits_update(&state, buffer, bufferSize);
return (XSUM_U32)XXH3_64bits_digest(&state);
}
static XSUM_U32 localXXH3_stream_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
XXH3_state_t state;
XXH3_INITSTATE(&state);
XXH3_64bits_reset_withSeed(&state, (XXH64_hash_t)seed);
XXH3_64bits_update(&state, buffer, bufferSize);
return (XSUM_U32)XXH3_64bits_digest(&state);
}
static XSUM_U32 localXXH128_stream(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
XXH3_state_t state;
(void)seed;
XXH3_128bits_reset(&state);
XXH3_128bits_update(&state, buffer, bufferSize);
return (XSUM_U32)(XXH3_128bits_digest(&state).low64);
}
static XSUM_U32 localXXH128_stream_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
{
XXH3_state_t state;
XXH3_INITSTATE(&state);
XXH3_128bits_reset_withSeed(&state, (XXH64_hash_t)seed);
XXH3_128bits_update(&state, buffer, bufferSize);
return (XSUM_U32)(XXH3_128bits_digest(&state).low64);
}
typedef struct {
const char* name;
hashFunction func;
} hashInfo;
#define NB_HASHFUNC 12
static const hashInfo g_hashesToBench[NB_HASHFUNC] = {
{ "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 },
{ "XXH128 w/secret", &localXXH3_128b_secret },
{ "XXH3_stream", &localXXH3_stream },
{ "XXH3_stream w/seed",&localXXH3_stream_seeded },
{ "XXH128_stream", &localXXH128_stream },
{ "XXH128_stream w/seed",&localXXH128_stream_seeded },
};
#define NB_TESTFUNC (1 + 2 * NB_HASHFUNC)
static char g_testIDs[NB_TESTFUNC] = { 0 };
static const char k_testIDs_default[NB_TESTFUNC] = { 0,
1 , 0,
1 , 0,
1 , 0, 0, 0, 0, 0,
1 };
#define HASHNAME_MAX 29
static void XSUM_benchHash(hashFunction h, const char* hName, int testID,
const void* buffer, size_t bufferSize)
{
XSUM_U32 nbh_perIteration = (XSUM_U32)((300 MB) / (bufferSize+1)) + 1;
unsigned iterationNb, nbIterations = g_nbIterations + !g_nbIterations ;
double fastestH = 100000000.;
assert(HASHNAME_MAX > 2);
XSUM_logVerbose(2, "\r%80s\r", "");
for (iterationNb = 1; iterationNb <= nbIterations; iterationNb++) {
XSUM_U32 r=0;
clock_t cStart;
XSUM_logVerbose(2, "%2u-%-*.*s : %10u ->\r",
iterationNb,
HASHNAME_MAX, HASHNAME_MAX, hName,
(unsigned)bufferSize);
cStart = clock();
while (clock() == cStart);
cStart = clock();
{ XSUM_U32 u;
for (u=0; u<nbh_perIteration; u++)
r += h(buffer, bufferSize, u);
}
if (r==0) XSUM_logVerbose(3,".\r");
{ clock_t const nbTicks = XSUM_clockSpan(cStart);
double const ticksPerHash = ((double)nbTicks / TIMELOOP) / nbh_perIteration;
if (nbTicks < TIMELOOP_MIN) {
if (nbTicks == 0) {
nbh_perIteration *= 100;
} else {
double nbh_perSecond = (1 / ticksPerHash) + 1;
if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20);
nbh_perIteration = (XSUM_U32)nbh_perSecond;
}
if (g_nbIterations>0) {
iterationNb--;
continue;
}
}
if (ticksPerHash < fastestH) fastestH = ticksPerHash;
if (fastestH>0.) {
XSUM_logVerbose(2, "%2u-%-*.*s : %10u -> %8.0f it/s (%7.1f MB/s) \r",
iterationNb,
HASHNAME_MAX, HASHNAME_MAX, hName,
(unsigned)bufferSize,
(double)1 / fastestH,
((double)bufferSize / (1 MB)) / fastestH);
} }
{ double nbh_perSecond = (1 / fastestH) + 1;
if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20);
nbh_perIteration = (XSUM_U32)nbh_perSecond;
}
}
XSUM_logVerbose(1, "%2i#%-*.*s : %10u -> %8.0f it/s (%7.1f MB/s) \n",
testID,
HASHNAME_MAX, HASHNAME_MAX, hName,
(unsigned)bufferSize,
(double)1 / fastestH,
((double)bufferSize / (1 MB)) / fastestH);
if (XSUM_logLevel<1)
XSUM_logVerbose(0, "%u, ", (unsigned)((double)1 / fastestH));
}
static void XSUM_benchMem(const void* buffer, size_t bufferSize)
{
assert((((size_t)buffer) & 15) == 0);
XSUM_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;
if ((i % 2) == 1) {
XSUM_benchHash(g_hashesToBench[hashFuncID].func, g_hashesToBench[hashFuncID].name, i, buffer, bufferSize);
}
if ((i % 2) == 0) {
char* const hashNameBuf = XSUM_strcatDup(g_hashesToBench[hashFuncID].name, " unaligned");
assert(hashNameBuf != NULL);
XSUM_benchHash(g_hashesToBench[hashFuncID].func, hashNameBuf, i, ((const char*)buffer)+3, bufferSize);
free(hashNameBuf);
}
} }
}
static size_t XSUM_selectBenchedSize(const char* fileName)
{
XSUM_U64 const inFileSize = XSUM_getFileSize(fileName);
size_t benchedSize = (size_t) XSUM_findMaxMem(inFileSize);
if ((XSUM_U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
if (benchedSize < inFileSize) {
XSUM_log("Not enough memory for '%s' full size; testing %i MB only...\n", fileName, (int)(benchedSize>>20));
}
return benchedSize;
}
static int XSUM_benchFiles(char*const* fileNamesTable, int nbFiles)
{
int fileIdx;
for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
const char* const inFileName = fileNamesTable[fileIdx];
assert(inFileName != NULL);
{ FILE* const inFile = XSUM_fopen( inFileName, "rb" );
size_t const benchedSize = XSUM_selectBenchedSize(inFileName);
char* const buffer = (char*)calloc(benchedSize+16+3, 1);
void* const alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF);
if (inFile==NULL){
XSUM_log("Error: Could not open '%s': %s.\n", inFileName, strerror(errno));
free(buffer);
exit(11);
}
if(!buffer) {
XSUM_log("\nError: Out of memory.\n");
fclose(inFile);
exit(12);
}
{ size_t const readSize = fread(alignedBuffer, 1, benchedSize, inFile);
fclose(inFile);
if(readSize != benchedSize) {
XSUM_log("\nError: Could not read '%s': %s.\n", inFileName, strerror(errno));
free(buffer);
exit(13);
} }
XSUM_benchMem(alignedBuffer, benchedSize);
free(buffer);
} }
return 0;
}
static int XSUM_benchInternal(size_t keySize)
{
void* const buffer = calloc(keySize+16+3, 1);
if (buffer == NULL) {
XSUM_log("\nError: Out of memory.\n");
exit(12);
}
{ const void* const alignedBuffer = ((char*)buffer+15) - (((size_t)((char*)buffer+15)) & 0xF);
XSUM_logVerbose(1, "Sample of ");
if (keySize > 10 KB) {
XSUM_logVerbose(1, "%u KB", (unsigned)(keySize >> 10));
} else {
XSUM_logVerbose(1, "%u bytes", (unsigned)keySize);
}
XSUM_logVerbose(1, "... \n");
XSUM_benchMem(alignedBuffer, keySize);
free(buffer);
}
return 0;
}
static void XSUM_display_LittleEndian(const void* ptr, size_t length)
{
const XSUM_U8* const p = (const XSUM_U8*)ptr;
size_t idx;
for (idx=length-1; idx<length; idx--)
XSUM_output("%02x", p[idx]);
}
static void XSUM_display_BigEndian(const void* ptr, size_t length)
{
const XSUM_U8* const p = (const XSUM_U8*)ptr;
size_t idx;
for (idx=0; idx<length; idx++)
XSUM_output("%02x", p[idx]);
}
typedef union {
XXH32_hash_t xxh32;
XXH64_hash_t xxh64;
XXH128_hash_t xxh128;
} Multihash;
static Multihash
XSUM_hashStream(FILE* inFile,
AlgoSelected hashType,
void* buffer, size_t blockSize)
{
XXH32_state_t state32;
XXH64_state_t state64;
XXH3_state_t state128;
(void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
(void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
(void)XXH3_128bits_reset(&state128);
{ size_t readSize;
while ((readSize = fread(buffer, 1, blockSize, inFile)) > 0) {
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);
}
}
if (ferror(inFile)) {
XSUM_log("Error: a failure occurred reading the input file.\n");
exit(1);
} }
{ Multihash finalHash = {0};
switch(hashType)
{
case algo_xxh32:
finalHash.xxh32 = XXH32_digest(&state32);
break;
case algo_xxh64:
finalHash.xxh64 = XXH64_digest(&state64);
break;
case algo_xxh128:
finalHash.xxh128 = XXH3_128bits_digest(&state128);
break;
default:
assert(0);
}
return finalHash;
}
}
static const char* XSUM_algoName[] = { "XXH32", "XXH64", "XXH128" };
static const char* XSUM_algoLE_name[] = { "XXH32_LE", "XXH64_LE", "XXH128_LE" };
static const size_t XSUM_algoLength[] = { 4, 8, 16 };
#define XSUM_TABLE_ELT_SIZE(table) (sizeof(table) / sizeof(*table))
typedef void (*XSUM_displayHash_f)(const void*, size_t);
static void XSUM_printLine_BSD_internal(const char* filename,
const void* canonicalHash, const AlgoSelected hashType,
const char* algoString[],
XSUM_displayHash_f f_displayHash)
{
assert(0 <= hashType && hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName));
{ const char* const typeString = algoString[hashType];
const size_t hashLength = XSUM_algoLength[hashType];
XSUM_output("%s (%s) = ", typeString, filename);
f_displayHash(canonicalHash, hashLength);
XSUM_output("\n");
} }
static void XSUM_printLine_BSD_LE(const char* filename, const void* canonicalHash, const AlgoSelected hashType)
{
XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoLE_name, XSUM_display_LittleEndian);
}
static void XSUM_printLine_BSD(const char* filename, const void* canonicalHash, const AlgoSelected hashType)
{
XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoName, XSUM_display_BigEndian);
}
static void XSUM_printLine_GNU_internal(const char* filename,
const void* canonicalHash, const AlgoSelected hashType,
XSUM_displayHash_f f_displayHash)
{
assert(0 <= hashType && hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName));
{ const size_t hashLength = XSUM_algoLength[hashType];
f_displayHash(canonicalHash, hashLength);
XSUM_output(" %s\n", filename);
} }
static void XSUM_printLine_GNU(const char* filename,
const void* canonicalHash, const AlgoSelected hashType)
{
XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_BigEndian);
}
static void XSUM_printLine_GNU_LE(const char* filename,
const void* canonicalHash, const AlgoSelected hashType)
{
XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_LittleEndian);
}
typedef enum { big_endian, little_endian} Display_endianess;
typedef enum { display_gnu, display_bsd } Display_convention;
typedef void (*XSUM_displayLine_f)(const char*, const void*, AlgoSelected);
static XSUM_displayLine_f XSUM_kDisplayLine_fTable[2][2] = {
{ XSUM_printLine_GNU, XSUM_printLine_GNU_LE },
{ XSUM_printLine_BSD, XSUM_printLine_BSD_LE }
};
static int XSUM_hashFile(const char* fileName,
const AlgoSelected hashType,
const Display_endianess displayEndianess,
const Display_convention convention)
{
size_t const blockSize = 64 KB;
XSUM_displayLine_f const f_displayLine = XSUM_kDisplayLine_fTable[convention][displayEndianess];
FILE* inFile;
Multihash hashValue;
assert(displayEndianess==big_endian || displayEndianess==little_endian);
assert(convention==display_gnu || convention==display_bsd);
if (fileName == stdinName) {
inFile = stdin;
fileName = stdinFileName;
XSUM_setBinaryMode(stdin);
} else {
if (XSUM_isDirectory(fileName)) {
XSUM_log("xxhsum: %s: Is a directory \n", fileName);
return 1;
}
inFile = XSUM_fopen( fileName, "rb" );
if (inFile==NULL) {
XSUM_log("Error: Could not open '%s': %s. \n", fileName, strerror(errno));
return 1;
} }
{ void* const buffer = malloc(blockSize);
if (buffer == NULL) {
XSUM_log("\nError: Out of memory.\n");
fclose(inFile);
return 1;
}
hashValue = XSUM_hashStream(inFile, hashType, buffer, blockSize);
fclose(inFile);
free(buffer);
}
switch(hashType)
{
case algo_xxh32:
{ XXH32_canonical_t hcbe32;
(void)XXH32_canonicalFromHash(&hcbe32, hashValue.xxh32);
f_displayLine(fileName, &hcbe32, hashType);
break;
}
case algo_xxh64:
{ XXH64_canonical_t hcbe64;
(void)XXH64_canonicalFromHash(&hcbe64, hashValue.xxh64);
f_displayLine(fileName, &hcbe64, hashType);
break;
}
case algo_xxh128:
{ XXH128_canonical_t hcbe128;
(void)XXH128_canonicalFromHash(&hcbe128, hashValue.xxh128);
f_displayLine(fileName, &hcbe128, hashType);
break;
}
default:
assert(0);
}
return 0;
}
static int XSUM_hashFiles(char*const * fnList, int fnTotal,
AlgoSelected hashType,
Display_endianess displayEndianess,
Display_convention convention)
{
int fnNb;
int result = 0;
if (fnTotal==0)
return XSUM_hashFile(stdinName, hashType, displayEndianess, convention);
for (fnNb=0; fnNb<fnTotal; fnNb++)
result |= XSUM_hashFile(fnList[fnNb], hashType, displayEndianess, convention);
XSUM_logVerbose(2, "\r%70s\r", "");
return result;
}
typedef enum {
GetLine_ok,
GetLine_eof,
GetLine_exceedMaxLineLength,
GetLine_outOfMemory
} GetLineResult;
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 {
XXH32_canonical_t xxh32;
XXH64_canonical_t xxh64;
XXH128_canonical_t xxh128;
} Canonical;
typedef struct {
Canonical canonical;
const char* filename;
int xxhBits;
} ParsedLine;
typedef struct {
unsigned long nProperlyFormattedLines;
unsigned long nImproperlyFormattedLines;
unsigned long nMismatchedChecksums;
unsigned long nOpenOrReadFailures;
unsigned long nMixedFormatLines;
int quit;
} ParseFileReport;
typedef struct {
const char* inFileName;
FILE* inFile;
int lineMax;
char* lineBuf;
size_t blockSize;
char* blockBuf;
XSUM_U32 strictMode;
XSUM_U32 statusOnly;
XSUM_U32 warn;
XSUM_U32 quiet;
ParseFileReport report;
} ParseFileArg;
static GetLineResult XSUM_getLine(char** lineBuf, int* lineMax, FILE* inFile)
{
GetLineResult result = GetLine_ok;
size_t len = 0;
if ((*lineBuf == NULL) || (*lineMax<1)) {
free(*lineBuf);
*lineMax = 0;
*lineBuf = (char*)malloc(DEFAULT_LINE_LENGTH);
if(*lineBuf == NULL) return GetLine_outOfMemory;
*lineMax = DEFAULT_LINE_LENGTH;
}
for (;;) {
const int c = fgetc(inFile);
if (c == EOF) {
if (len == 0) result = GetLine_eof;
break;
}
if (len+1 >= (size_t)*lineMax) {
char* newLineBuf = NULL;
size_t newBufSize = (size_t)*lineMax;
newBufSize += (newBufSize/2) + 1;
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 = (int)newBufSize;
}
if (c == '\n') break;
(*lineBuf)[len++] = (char) c;
}
(*lineBuf)[len] = '\0';
return result;
}
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;
}
static CanonicalFromStringResult XSUM_canonicalFromString(unsigned char* dst,
size_t dstSize,
const char* hashStr,
int reverseBytes)
{
size_t i;
for (i = 0; i < dstSize; ++i) {
int h0, h1;
size_t j = reverseBytes ? dstSize - i - 1 : i;
h0 = charToHex(hashStr[j*2 + 0]);
if (h0 < 0) return CanonicalFromString_invalidFormat;
h1 = charToHex(hashStr[j*2 + 1]);
if (h1 < 0) return CanonicalFromString_invalidFormat;
dst[i] = (unsigned char) ((h0 << 4) | h1);
}
return CanonicalFromString_ok;
}
static ParseLineResult XSUM_parseLine(ParsedLine* parsedLine, char* line, int rev)
{
char* const firstSpace = strchr(line, ' ');
const char* hash_ptr;
size_t hash_len;
parsedLine->filename = NULL;
parsedLine->xxhBits = 0;
if (firstSpace == NULL || !firstSpace[1]) return ParseLine_invalidFormat;
if (firstSpace[1] == '(') {
char* lastSpace = strrchr(line, ' ');
if (lastSpace - firstSpace < 5) return ParseLine_invalidFormat;
if (lastSpace[-1] != '=' || lastSpace[-2] != ' ' || lastSpace[-3] != ')') return ParseLine_invalidFormat;
lastSpace[-3] = '\0';
*firstSpace = '\0';
rev = strstr(line, "_LE") != NULL;
hash_ptr = lastSpace + 1;
hash_len = strlen(hash_ptr);
} else {
hash_ptr = line;
hash_len = (size_t)(firstSpace - line);
}
switch (hash_len)
{
case 8:
{ XXH32_canonical_t* xxh32c = &parsedLine->canonical.xxh32;
if (XSUM_canonicalFromString(xxh32c->digest, sizeof(xxh32c->digest), hash_ptr, rev)
!= CanonicalFromString_ok) {
return ParseLine_invalidFormat;
}
parsedLine->xxhBits = 32;
break;
}
case 16:
{ XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64;
if (XSUM_canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), hash_ptr, rev)
!= CanonicalFromString_ok) {
return ParseLine_invalidFormat;
}
parsedLine->xxhBits = 64;
break;
}
case 32:
{ XXH128_canonical_t* xxh128c = &parsedLine->canonical.xxh128;
if (XSUM_canonicalFromString(xxh128c->digest, sizeof(xxh128c->digest), hash_ptr, rev)
!= CanonicalFromString_ok) {
return ParseLine_invalidFormat;
}
parsedLine->xxhBits = 128;
break;
}
default:
return ParseLine_invalidFormat;
break;
}
parsedLine->filename = firstSpace + 2;
return ParseLine_ok;
}
static void XSUM_parseFile1(ParseFileArg* XSUM_parseFileArg, int rev)
{
const char* const inFileName = XSUM_parseFileArg->inFileName;
ParseFileReport* const report = &XSUM_parseFileArg->report;
unsigned long lineNumber = 0;
memset(report, 0, sizeof(*report));
while (!report->quit) {
LineStatus lineStatus = LineStatus_hashFailed;
ParsedLine parsedLine;
memset(&parsedLine, 0, sizeof(parsedLine));
lineNumber++;
if (lineNumber == 0) {
XSUM_log("%s: Error: Too many checksum lines\n", inFileName);
report->quit = 1;
break;
}
{ GetLineResult const XSUM_getLineResult = XSUM_getLine(&XSUM_parseFileArg->lineBuf,
&XSUM_parseFileArg->lineMax,
XSUM_parseFileArg->inFile);
if (XSUM_getLineResult != GetLine_ok) {
if (XSUM_getLineResult == GetLine_eof) break;
switch (XSUM_getLineResult)
{
case GetLine_ok:
case GetLine_eof:
assert(0);
break;
default:
XSUM_log("%s:%lu: Error: Unknown error.\n", inFileName, lineNumber);
break;
case GetLine_exceedMaxLineLength:
XSUM_log("%s:%lu: Error: Line too long.\n", inFileName, lineNumber);
break;
case GetLine_outOfMemory:
XSUM_log("%s:%lu: Error: Out of memory.\n", inFileName, lineNumber);
break;
}
report->quit = 1;
break;
} }
if (XSUM_parseLine(&parsedLine, XSUM_parseFileArg->lineBuf, rev) != ParseLine_ok) {
report->nImproperlyFormattedLines++;
if (XSUM_parseFileArg->warn) {
XSUM_log("%s:%lu: Error: Improperly formatted checksum line.\n",
inFileName, lineNumber);
}
continue;
}
report->nProperlyFormattedLines++;
do {
int const fnameIsStdin = (strcmp(parsedLine.filename, stdinFileName) == 0);
FILE* const fp = fnameIsStdin ? stdin : XSUM_fopen(parsedLine.filename, "rb");
if (fp == stdin) {
XSUM_setBinaryMode(stdin);
}
if (fp == NULL) {
lineStatus = LineStatus_failedToOpen;
break;
}
lineStatus = LineStatus_hashFailed;
switch (parsedLine.xxhBits)
{
case 32:
{ Multihash const xxh = XSUM_hashStream(fp, algo_xxh32, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
if (xxh.xxh32 == XXH32_hashFromCanonical(&parsedLine.canonical.xxh32)) {
lineStatus = LineStatus_hashOk;
} }
break;
case 64:
{ Multihash const xxh = XSUM_hashStream(fp, algo_xxh64, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
if (xxh.xxh64 == XXH64_hashFromCanonical(&parsedLine.canonical.xxh64)) {
lineStatus = LineStatus_hashOk;
} }
break;
case 128:
{ Multihash const xxh = XSUM_hashStream(fp, algo_xxh128, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
if (XXH128_isEqual(xxh.xxh128, XXH128_hashFromCanonical(&parsedLine.canonical.xxh128))) {
lineStatus = LineStatus_hashOk;
} }
break;
default:
break;
}
if (fp != stdin) fclose(fp);
} while (0);
switch (lineStatus)
{
default:
XSUM_log("%s: Error: Unknown error.\n", inFileName);
report->quit = 1;
break;
case LineStatus_failedToOpen:
report->nOpenOrReadFailures++;
if (!XSUM_parseFileArg->statusOnly) {
XSUM_output("%s:%lu: Could not open or read '%s': %s.\n",
inFileName, lineNumber, parsedLine.filename, strerror(errno));
}
break;
case LineStatus_hashOk:
case LineStatus_hashFailed:
{ int b = 1;
if (lineStatus == LineStatus_hashOk) {
if (XSUM_parseFileArg->quiet) b = 0;
} else {
report->nMismatchedChecksums++;
}
if (b && !XSUM_parseFileArg->statusOnly) {
XSUM_output("%s: %s\n", parsedLine.filename
, lineStatus == LineStatus_hashOk ? "OK" : "FAILED");
} }
break;
}
}
}
static int XSUM_checkFile(const char* inFileName,
const Display_endianess displayEndianess,
XSUM_U32 strictMode,
XSUM_U32 statusOnly,
XSUM_U32 warn,
XSUM_U32 quiet)
{
int result = 0;
FILE* inFile = NULL;
ParseFileArg XSUM_parseFileArgBody;
ParseFileArg* const XSUM_parseFileArg = &XSUM_parseFileArgBody;
ParseFileReport* const report = &XSUM_parseFileArg->report;
if (inFileName == stdinName) {
inFileName = stdinFileName;
inFile = stdin;
} else {
inFile = XSUM_fopen( inFileName, "rt" );
}
if (inFile == NULL) {
XSUM_log("Error: Could not open '%s': %s\n", inFileName, strerror(errno));
return 0;
}
XSUM_parseFileArg->inFileName = inFileName;
XSUM_parseFileArg->inFile = inFile;
XSUM_parseFileArg->lineMax = DEFAULT_LINE_LENGTH;
XSUM_parseFileArg->lineBuf = (char*) malloc((size_t)XSUM_parseFileArg->lineMax);
XSUM_parseFileArg->blockSize = 64 * 1024;
XSUM_parseFileArg->blockBuf = (char*) malloc(XSUM_parseFileArg->blockSize);
XSUM_parseFileArg->strictMode = strictMode;
XSUM_parseFileArg->statusOnly = statusOnly;
XSUM_parseFileArg->warn = warn;
XSUM_parseFileArg->quiet = quiet;
if ( (XSUM_parseFileArg->lineBuf == NULL)
|| (XSUM_parseFileArg->blockBuf == NULL) ) {
XSUM_log("Error: : memory allocation failed \n");
exit(1);
}
XSUM_parseFile1(XSUM_parseFileArg, displayEndianess != big_endian);
free(XSUM_parseFileArg->blockBuf);
free(XSUM_parseFileArg->lineBuf);
if (inFile != stdin) fclose(inFile);
if (report->nProperlyFormattedLines == 0) {
XSUM_log("%s: no properly formatted xxHash checksum lines found\n", inFileName);
} else if (!statusOnly) {
if (report->nImproperlyFormattedLines) {
XSUM_output("%lu %s improperly formatted\n"
, report->nImproperlyFormattedLines
, report->nImproperlyFormattedLines == 1 ? "line is" : "lines are");
}
if (report->nOpenOrReadFailures) {
XSUM_output("%lu listed %s could not be read\n"
, report->nOpenOrReadFailures
, report->nOpenOrReadFailures == 1 ? "file" : "files");
}
if (report->nMismatchedChecksums) {
XSUM_output("%lu computed %s did NOT match\n"
, report->nMismatchedChecksums
, report->nMismatchedChecksums == 1 ? "checksum" : "checksums");
} }
result = report->nProperlyFormattedLines != 0
&& report->nMismatchedChecksums == 0
&& report->nOpenOrReadFailures == 0
&& (!strictMode || report->nImproperlyFormattedLines == 0)
&& report->quit == 0;
return result;
}
static int XSUM_checkFiles(char*const* fnList, int fnTotal,
const Display_endianess displayEndianess,
XSUM_U32 strictMode,
XSUM_U32 statusOnly,
XSUM_U32 warn,
XSUM_U32 quiet)
{
int ok = 1;
if (fnTotal==0) {
ok &= XSUM_checkFile(stdinName, displayEndianess, strictMode, statusOnly, warn, quiet);
} else {
int fnNb;
for (fnNb=0; fnNb<fnTotal; fnNb++)
ok &= XSUM_checkFile(fnList[fnNb], displayEndianess, strictMode, statusOnly, warn, quiet);
}
return ok ? 0 : 1;
}
static int XSUM_usage(const char* exename)
{
XSUM_log( WELCOME_MESSAGE(exename) );
XSUM_log( "Print or verify checksums using fast non-cryptographic algorithm xxHash \n\n" );
XSUM_log( "Usage: %s [options] [files] \n\n", exename);
XSUM_log( "When no filename provided or when '-' is provided, uses stdin as input. \n");
XSUM_log( "Options: \n");
XSUM_log( " -H# algorithm selection: 0,1,2 or 32,64,128 (default: %i) \n", (int)g_defaultAlgo);
XSUM_log( " -c, --check read xxHash checksum from [files] and check them \n");
XSUM_log( " -h, --help display a long help page about advanced options \n");
return 0;
}
static int XSUM_usage_advanced(const char* exename)
{
XSUM_usage(exename);
XSUM_log( "Advanced :\n");
XSUM_log( " -V, --version Display version information \n");
XSUM_log( " --tag Produce BSD-style checksum lines \n");
XSUM_log( " --little-endian Checksum values use little endian convention (default: big endian) \n");
XSUM_log( " -b Run benchmark \n");
XSUM_log( " -b# Bench only algorithm variant # \n");
XSUM_log( " -i# Number of times to run the benchmark (default: %u) \n", (unsigned)g_nbIterations);
XSUM_log( " -q, --quiet Don't display version header in benchmark mode \n");
XSUM_log( "\n");
XSUM_log( "The following four options are useful only when verifying checksums (-c): \n");
XSUM_log( " -q, --quiet Don't print OK for each successfully verified file \n");
XSUM_log( " --status Don't output anything, status code shows success \n");
XSUM_log( " --strict Exit non-zero for improperly formatted checksum lines \n");
XSUM_log( " --warn Warn about improperly formatted checksum lines \n");
return 0;
}
static int XSUM_badusage(const char* exename)
{
XSUM_log("Wrong parameters\n\n");
XSUM_usage(exename);
return 1;
}
static void errorOut(const char* msg)
{
XSUM_log("%s \n", msg);
exit(1);
}
static const char* XSUM_lastNameFromPath(const char* path)
{
const char* name = path;
if (strrchr(name, '/')) name = strrchr(name, '/') + 1;
if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1;
return name;
}
static int XSUM_readU32FromCharChecked(const char** stringPtr, XSUM_U32* value)
{
static const XSUM_U32 max = (((XSUM_U32)(-1)) / 10) - 1;
XSUM_U32 result = 0;
while ((**stringPtr >='0') && (**stringPtr <='9')) {
if (result > max) return 1;
result *= 10;
result += (XSUM_U32)(**stringPtr - '0');
(*stringPtr)++ ;
}
if ((**stringPtr=='K') || (**stringPtr=='M')) {
XSUM_U32 const maxK = ((XSUM_U32)(-1)) >> 10;
if (result > maxK) return 1;
result <<= 10;
if (**stringPtr=='M') {
if (result > maxK) return 1;
result <<= 10;
}
(*stringPtr)++;
if (**stringPtr=='i') (*stringPtr)++;
if (**stringPtr=='B') (*stringPtr)++;
}
*value = result;
return 0;
}
static XSUM_U32 XSUM_readU32FromChar(const char** stringPtr) {
XSUM_U32 result;
if (XSUM_readU32FromCharChecked(stringPtr, &result)) {
static const char errorMsg[] = "Error: numeric value too large";
errorOut(errorMsg);
}
return result;
}
XSUM_API int XSUM_main(int argc, char* argv[])
{
int i, filenamesStart = 0;
const char* const exename = XSUM_lastNameFromPath(argv[0]);
XSUM_U32 benchmarkMode = 0;
XSUM_U32 fileCheckMode = 0;
XSUM_U32 strictMode = 0;
XSUM_U32 statusOnly = 0;
XSUM_U32 warn = 0;
int explicitStdin = 0;
XSUM_U32 selectBenchIDs= 0;
static const XSUM_U32 kBenchAll = 99;
size_t keySize = XSUM_DEFAULT_SAMPLE_SIZE;
AlgoSelected algo = g_defaultAlgo;
Display_endianess displayEndianess = big_endian;
Display_convention convention = display_gnu;
if (strstr(exename, "xxh32sum") != NULL) algo = g_defaultAlgo = algo_xxh32;
if (strstr(exename, "xxh64sum") != NULL) algo = g_defaultAlgo = algo_xxh64;
if (strstr(exename, "xxh128sum") != NULL) algo = g_defaultAlgo = algo_xxh128;
for (i=1; i<argc; i++) {
const char* argument = argv[i];
assert(argument != NULL);
if (!strcmp(argument, "--check")) { fileCheckMode = 1; continue; }
if (!strcmp(argument, "--benchmark-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
if (!strcmp(argument, "--bench-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
if (!strcmp(argument, "--quiet")) { XSUM_logLevel--; continue; }
if (!strcmp(argument, "--little-endian")) { displayEndianess = little_endian; continue; }
if (!strcmp(argument, "--strict")) { strictMode = 1; continue; }
if (!strcmp(argument, "--status")) { statusOnly = 1; continue; }
if (!strcmp(argument, "--warn")) { warn = 1; continue; }
if (!strcmp(argument, "--help")) { return XSUM_usage_advanced(exename); }
if (!strcmp(argument, "--version")) { XSUM_log(FULL_WELCOME_MESSAGE(exename)); XSUM_sanityCheck(); return 0; }
if (!strcmp(argument, "--tag")) { convention = display_bsd; continue; }
if (!strcmp(argument, "--")) {
if (filenamesStart==0 && i!=argc-1) filenamesStart=i+1;
break;
}
if (*argument != '-') {
if (filenamesStart==0) filenamesStart=i;
break;
}
argument++;
if (*argument == 0) explicitStdin = 1;
while (*argument != 0) {
switch(*argument)
{
case 'V':
XSUM_log(FULL_WELCOME_MESSAGE(exename)); return 0;
case 'h':
return XSUM_usage_advanced(exename);
case 'H': argument++;
switch(XSUM_readU32FromChar(&argument)) {
case 0 :
case 32: algo = algo_xxh32; break;
case 1 :
case 64: algo = algo_xxh64; break;
case 2 :
case 128: algo = algo_xxh128; break;
default:
return XSUM_badusage(exename);
}
break;
case 'c':
fileCheckMode=1;
argument++;
break;
case 'w':
warn=1;
argument++;
break;
case 'b':
argument++;
benchmarkMode = 1;
do {
if (*argument == ',') argument++;
selectBenchIDs = XSUM_readU32FromChar(&argument);
if (selectBenchIDs < NB_TESTFUNC) {
g_testIDs[selectBenchIDs] = 1;
} else
selectBenchIDs = kBenchAll;
} while (*argument == ',');
break;
case 'i':
argument++;
g_nbIterations = XSUM_readU32FromChar(&argument);
break;
case 'B':
argument++;
keySize = XSUM_readU32FromChar(&argument);
break;
case 'q':
argument++;
XSUM_logLevel--;
break;
default:
return XSUM_badusage(exename);
}
}
}
if (benchmarkMode) {
XSUM_logVerbose(2, FULL_WELCOME_MESSAGE(exename) );
XSUM_sanityCheck();
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 XSUM_benchInternal(keySize);
return XSUM_benchFiles(argv+filenamesStart, argc-filenamesStart);
}
if ( (filenamesStart==0) && XSUM_isConsole(stdin) && !explicitStdin)
return XSUM_badusage(exename);
if (filenamesStart==0) filenamesStart = argc;
if (fileCheckMode) {
return XSUM_checkFiles(argv+filenamesStart, argc-filenamesStart,
displayEndianess, strictMode, statusOnly, warn, (XSUM_logLevel < 2) );
} else {
return XSUM_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, displayEndianess, convention);
}
}