31
#ifndef XXHASH_C_2097394837
32
#define XXHASH_C_2097394837
38
#if defined(_MSC_VER) || defined(_WIN32)
39
# define _CRT_SECURE_NO_WARNINGS
43
#ifndef _LARGEFILE64_SOURCE
44
# define _LARGEFILE64_SOURCE
59
#define XXH_STATIC_LINKING_ONLY
66
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
69
# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)
70
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
73
# define SET_BINARY_MODE(file)
74
# define IS_CONSOLE(stdStream) isatty(STDIN_FILENO)
78
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
87
# if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
95
typedef unsigned char BYTE;
96
typedef unsigned short U16;
97
typedef unsigned int U32;
98
typedef signed int S32;
99
typedef unsigned long long U64;
103
static unsigned BMK_isLittleEndian(void)
105
const union { U32 u; BYTE c[4]; } one = { 1 };
113
#define LIB_VERSION XXH_VERSION_MAJOR.XXH_VERSION_MINOR.XXH_VERSION_RELEASE
114
#define QUOTE(str) #str
115
#define EXPAND_AND_QUOTE(str) QUOTE(str)
116
#define PROGRAM_VERSION EXPAND_AND_QUOTE(LIB_VERSION)
117
static const int g_nbBits = (int)(sizeof(void*)*8);
118
static const char g_lename[] = "little endian";
119
static const char g_bename[] = "big endian";
120
#define ENDIAN_NAME (BMK_isLittleEndian() ? g_lename : g_bename)
121
static const char author[] = "Yann Collet";
122
#define WELCOME_MESSAGE(exename) "%s %s (%i-bits %s), by %s \n", \
123
exename, PROGRAM_VERSION, g_nbBits, ENDIAN_NAME, author
129
static size_t XXH_DEFAULT_SAMPLE_SIZE = 100 KB;
132
#define TIMELOOP (TIMELOOP_S * CLOCKS_PER_SEC)
133
#define XXHSUM32_DEFAULT_SEED 0
134
#define XXHSUM64_DEFAULT_SEED 0
136
#define MAX_MEM (2 GB - 64 MB)
138
static const char stdinName[] = "-";
139
typedef enum { algo_xxh32, algo_xxh64 } algoType;
140
static const algoType g_defaultAlgo = algo_xxh64;
144
#define DEFAULT_LINE_LENGTH (sizeof(XXH64_hash_t) * 2 + 2 + 4096 + 1)
147
#define MAX_LINE_LENGTH (32 KB)
153
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
154
#define DISPLAYRESULT(...) fprintf(stdout, __VA_ARGS__)
155
#define DISPLAYLEVEL(l, ...) do { if (g_displayLevel>=l) DISPLAY(__VA_ARGS__); } while (0)
156
static int g_displayLevel = 2;
162
static U32 g_nbIterations = NBLOOPS;
168
static clock_t BMK_clockSpan( clock_t start )
170
return clock() - start;
174
static size_t BMK_findMaxMem(U64 requiredMem)
176
size_t const step = 64 MB;
177
void* testmem = NULL;
179
requiredMem = (((requiredMem >> 26) + 1) << 26);
180
requiredMem += 2*step;
181
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
184
if (requiredMem > step) requiredMem -= step;
185
else requiredMem >>= 1;
186
testmem = malloc ((size_t)requiredMem);
191
if (requiredMem > step) requiredMem -= step;
192
else requiredMem >>= 1;
194
return (size_t)requiredMem;
198
static U64 BMK_GetFileSize(const char* infilename)
202
struct _stat64 statbuf;
203
r = _stat64(infilename, &statbuf);
206
r = stat(infilename, &statbuf);
208
if (r || !S_ISREG(statbuf.st_mode)) return 0;
209
return (U64)statbuf.st_size;
212
typedef U32 (*hashFunction)(const void* buffer, size_t bufferSize, U32 seed);
214
static U32 localXXH32(const void* buffer, size_t bufferSize, U32 seed) { return XXH32(buffer, bufferSize, seed); }
216
static U32 localXXH64(const void* buffer, size_t bufferSize, U32 seed) { return (U32)XXH64(buffer, bufferSize, seed); }
218
static void BMK_benchHash(hashFunction h, const char* hName, const void* buffer, size_t bufferSize)
220
U32 nbh_perIteration = ((300 MB) / (bufferSize+1)) + 1;
222
double fastestH = 100000000.;
224
DISPLAYLEVEL(2, "\r%70s\r", "");
225
if (g_nbIterations<1) g_nbIterations=1;
226
for (iterationNb = 1; iterationNb <= g_nbIterations; iterationNb++) {
230
DISPLAYLEVEL(2, "%1i-%-17.17s : %10u ->\r", iterationNb, hName, (U32)bufferSize);
232
while (clock() == cStart);
236
for (i=0; i<nbh_perIteration; i++)
237
r += h(buffer, bufferSize, i);
239
if (r==0) DISPLAYLEVEL(3,".\r");
240
{ double const timeS = ((double)BMK_clockSpan(cStart) / CLOCKS_PER_SEC) / nbh_perIteration;
241
if (timeS < fastestH) fastestH = timeS;
242
DISPLAYLEVEL(2, "%1i-%-17.17s : %10u -> %8.0f it/s (%7.1f MB/s) \r",
243
iterationNb, hName, (U32)bufferSize,
244
(double)1 / fastestH,
245
((double)bufferSize / (1<<20)) / fastestH );
247
assert(fastestH > 1./2000000000);
248
nbh_perIteration = (U32)(1 / fastestH) + 1;
250
DISPLAYLEVEL(1, "%-19.19s : %10u -> %8.0f it/s (%7.1f MB/s) \n", hName, (U32)bufferSize,
251
(double)1 / fastestH,
252
((double)bufferSize / (1<<20)) / fastestH);
253
if (g_displayLevel<1)
254
DISPLAYLEVEL(0, "%u, ", (U32)((double)1 / fastestH));
263
static int BMK_benchMem(const void* buffer, size_t bufferSize, U32 specificTest)
265
assert((((size_t)buffer) & 8) == 0);
268
if ((specificTest==0) | (specificTest==1))
269
BMK_benchHash(localXXH32, "XXH32", buffer, bufferSize);
272
if ((specificTest==0) | (specificTest==2))
273
BMK_benchHash(localXXH32, "XXH32 unaligned", ((const char*)buffer)+1, bufferSize);
276
if ((specificTest==0) | (specificTest==3))
277
BMK_benchHash(localXXH64, "XXH64", buffer, bufferSize);
280
if ((specificTest==0) | (specificTest==4))
281
BMK_benchHash(localXXH64, "XXH64 unaligned", ((const char*)buffer)+3, bufferSize);
283
if (specificTest > 4) {
284
DISPLAY("benchmark mode invalid \n");
291
static size_t BMK_selectBenchedSize(const char* fileName)
292
{ U64 const inFileSize = BMK_GetFileSize(fileName);
293
size_t benchedSize = (size_t) BMK_findMaxMem(inFileSize);
294
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
295
if (benchedSize < inFileSize) {
296
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", fileName, (int)(benchedSize>>20));
302
static int BMK_benchFiles(const char** fileNamesTable, int nbFiles, U32 specificTest)
307
for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
308
const char* const inFileName = fileNamesTable[fileIdx];
309
FILE* const inFile = fopen( inFileName, "rb" );
310
size_t const benchedSize = BMK_selectBenchedSize(inFileName);
311
char* const buffer = (char*)calloc(benchedSize+16+3, 1);
312
void* const alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF);
315
if ((inFile==NULL) || (inFileName==NULL)) {
316
DISPLAY("Pb opening %s\n", inFileName);
321
DISPLAY("\nError: not enough memory!\n");
327
DISPLAYLEVEL(1, "\rLoading %s... \n", inFileName);
328
{ size_t const readSize = fread(alignedBuffer, 1, benchedSize, inFile);
330
if(readSize != benchedSize) {
331
DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
337
result |= BMK_benchMem(alignedBuffer, benchedSize, specificTest);
347
static int BMK_benchInternal(size_t keySize, int specificTest)
349
void* const buffer = calloc(keySize+16+3, 1);
350
void* const alignedBuffer = ((char*)buffer+15) - (((size_t)((char*)buffer+15)) & 0xF);
352
DISPLAY("\nError: not enough memory!\n");
357
DISPLAYLEVEL(1, "Sample of ");
358
if (keySize > 10 KB) {
359
DISPLAYLEVEL(1, "%u KB", (U32)(keySize >> 10));
361
DISPLAYLEVEL(1, "%u bytes", (U32)keySize);
363
DISPLAYLEVEL(1, "... \n");
365
{ int const result = BMK_benchMem(alignedBuffer, keySize, specificTest);
372
static void BMK_checkResult(U32 r1, U32 r2)
374
static int nbTests = 1;
376
DISPLAYLEVEL(3, "\rTest%3i : %08X == %08X ok ", nbTests, r1, r2);
378
DISPLAY("\rERROR : Test%3i : %08X <> %08X !!!!! \n", nbTests, r1, r2);
385
static void BMK_checkResult64(U64 r1, U64 r2)
387
static int nbTests = 1;
389
DISPLAY("\rERROR : Test%3i : 64-bit values non equals !!!!! \n", nbTests);
390
DISPLAY("\r %08X%08X != %08X%08X \n", (U32)(r1>>32), (U32)r1, (U32)(r2>>32), (U32)r2);
397
static void BMK_testSequence64(void* sentence, size_t len, U64 seed, U64 Nresult)
403
Dresult = XXH64(sentence, len, seed);
404
BMK_checkResult64(Dresult, Nresult);
406
XXH64_reset(&state, seed);
407
XXH64_update(&state, sentence, len);
408
Dresult = XXH64_digest(&state);
409
BMK_checkResult64(Dresult, Nresult);
411
XXH64_reset(&state, seed);
412
for (pos=0; pos<len; pos++)
413
XXH64_update(&state, ((char*)sentence)+pos, 1);
414
Dresult = XXH64_digest(&state);
415
BMK_checkResult64(Dresult, Nresult);
419
static void BMK_testSequence(const void* sequence, size_t len, U32 seed, U32 Nresult)
425
Dresult = XXH32(sequence, len, seed);
426
BMK_checkResult(Dresult, Nresult);
428
XXH32_reset(&state, seed);
429
XXH32_update(&state, sequence, len);
430
Dresult = XXH32_digest(&state);
431
BMK_checkResult(Dresult, Nresult);
433
XXH32_reset(&state, seed);
434
for (pos=0; pos<len; pos++)
435
XXH32_update(&state, ((const char*)sequence)+pos, 1);
436
Dresult = XXH32_digest(&state);
437
BMK_checkResult(Dresult, Nresult);
441
#define SANITY_BUFFER_SIZE 101
442
static void BMK_sanityCheck(void)
444
static const U32 prime = 2654435761U;
445
BYTE sanityBuffer[SANITY_BUFFER_SIZE];
449
for (i=0; i<SANITY_BUFFER_SIZE; i++) {
450
sanityBuffer[i] = (BYTE)(byteGen>>24);
454
BMK_testSequence(NULL, 0, 0, 0x02CC5D05);
455
BMK_testSequence(NULL, 0, prime, 0x36B78AE7);
456
BMK_testSequence(sanityBuffer, 1, 0, 0xB85CBEE5);
457
BMK_testSequence(sanityBuffer, 1, prime, 0xD5845D64);
458
BMK_testSequence(sanityBuffer, 14, 0, 0xE5AA0AB4);
459
BMK_testSequence(sanityBuffer, 14, prime, 0x4481951D);
460
BMK_testSequence(sanityBuffer, SANITY_BUFFER_SIZE, 0, 0x1F1AA412);
461
BMK_testSequence(sanityBuffer, SANITY_BUFFER_SIZE, prime, 0x498EC8E2);
463
BMK_testSequence64(NULL , 0, 0, 0xEF46DB3751D8E999ULL);
464
BMK_testSequence64(NULL , 0, prime, 0xAC75FDA2929B17EFULL);
465
BMK_testSequence64(sanityBuffer, 1, 0, 0x4FCE394CC88952D8ULL);
466
BMK_testSequence64(sanityBuffer, 1, prime, 0x739840CB819FA723ULL);
467
BMK_testSequence64(sanityBuffer, 14, 0, 0xCFFA8DB881BC3A3DULL);
468
BMK_testSequence64(sanityBuffer, 14, prime, 0x5B9611585EFCC9CBULL);
469
BMK_testSequence64(sanityBuffer, SANITY_BUFFER_SIZE, 0, 0x0EAB543384F878ADULL);
470
BMK_testSequence64(sanityBuffer, SANITY_BUFFER_SIZE, prime, 0xCAA65939306F1E21ULL);
472
DISPLAYLEVEL(3, "\r%70s\r", "");
473
DISPLAYLEVEL(3, "Sanity check -- all tests ok\n");
481
static void BMK_display_LittleEndian(const void* ptr, size_t length)
483
const BYTE* p = (const BYTE*)ptr;
485
for (idx=length-1; idx<length; idx--)
486
DISPLAYRESULT("%02x", p[idx]);
489
static void BMK_display_BigEndian(const void* ptr, size_t length)
491
const BYTE* p = (const BYTE*)ptr;
493
for (idx=0; idx<length; idx++)
494
DISPLAYRESULT("%02x", p[idx]);
497
static void BMK_hashStream(void* xxhHashValue, const algoType hashType, FILE* inFile, void* buffer, size_t blockSize)
499
XXH64_state_t state64;
500
XXH32_state_t state32;
504
XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
505
XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
510
readSize = fread(buffer, 1, blockSize, inFile);
514
XXH32_update(&state32, buffer, readSize);
517
XXH64_update(&state64, buffer, readSize);
527
{ U32 const h32 = XXH32_digest(&state32);
528
memcpy(xxhHashValue, &h32, sizeof(h32));
532
{ U64 const h64 = XXH64_digest(&state64);
533
memcpy(xxhHashValue, &h64, sizeof(h64));
542
typedef enum { big_endian, little_endian} endianess;
544
static int BMK_hash(const char* fileName,
545
const algoType hashType,
546
const endianess displayEndianess)
549
size_t const blockSize = 64 KB;
555
if (fileName == stdinName) {
557
SET_BINARY_MODE(stdin);
560
inFile = fopen( fileName, "rb" );
562
DISPLAY( "Pb opening %s\n", fileName);
567
buffer = malloc(blockSize);
569
DISPLAY("\nError: not enough memory!\n");
575
{ const size_t fileNameSize = strlen(fileName);
576
const char* const fileNameEnd = fileName + fileNameSize;
577
const int maxInfoFilenameSize = (int)(fileNameSize > 30 ? 30 : fileNameSize);
578
int infoFilenameSize = 1;
579
while ((infoFilenameSize < maxInfoFilenameSize)
580
&& (fileNameEnd[-1-infoFilenameSize] != '/')
581
&& (fileNameEnd[-1-infoFilenameSize] != '\\') )
583
DISPLAY("\rLoading %s... \r", fileNameEnd - infoFilenameSize);
589
BMK_hashStream(&h32, algo_xxh32, inFile, buffer, blockSize);
592
BMK_hashStream(&h64, algo_xxh64, inFile, buffer, blockSize);
600
DISPLAY("%s \r", fileNameEnd - infoFilenameSize);
607
{ XXH32_canonical_t hcbe32;
608
XXH32_canonicalFromHash(&hcbe32, h32);
609
displayEndianess==big_endian ?
610
BMK_display_BigEndian(&hcbe32, sizeof(hcbe32)) : BMK_display_LittleEndian(&hcbe32, sizeof(hcbe32));
611
DISPLAYRESULT(" %s\n", fileName);
615
{ XXH64_canonical_t hcbe64;
616
XXH64_canonicalFromHash(&hcbe64, h64);
617
displayEndianess==big_endian ?
618
BMK_display_BigEndian(&hcbe64, sizeof(hcbe64)) : BMK_display_LittleEndian(&hcbe64, sizeof(hcbe64));
619
DISPLAYRESULT(" %s\n", fileName);
630
static int BMK_hashFiles(const char** fnList, int fnTotal,
631
algoType hashType, endianess displayEndianess)
637
return BMK_hash(stdinName, hashType, displayEndianess);
639
for (fnNb=0; fnNb<fnTotal; fnNb++)
640
result += BMK_hash(fnList[fnNb], hashType, displayEndianess);
641
DISPLAY("\r%70s\r", "");
649
GetLine_exceedMaxLineLength,
654
CanonicalFromString_ok,
655
CanonicalFromString_invalidFormat,
656
} CanonicalFromStringResult;
660
ParseLine_invalidFormat,
665
LineStatus_hashFailed,
666
LineStatus_failedToOpen,
670
XXH32_canonical_t xxh32;
671
XXH64_canonical_t xxh64;
676
const char* filename;
681
unsigned long nProperlyFormattedLines;
682
unsigned long nImproperlyFormattedLines;
683
unsigned long nMismatchedChecksums;
684
unsigned long nOpenOrReadFailures;
685
unsigned long nMixedFormatLines;
691
const char* inFileName;
701
ParseFileReport report;
711
static GetLineResult getLine(char** lineBuf, int* lineMax, FILE* inFile)
713
GetLineResult result = GetLine_ok;
716
if ((*lineBuf == NULL) || (*lineMax<1)) {
719
*lineBuf = (char*)malloc(DEFAULT_LINE_LENGTH);
720
if(*lineBuf == NULL) return GetLine_outOfMemory;
721
*lineMax = DEFAULT_LINE_LENGTH;
725
const int c = fgetc(inFile);
730
if (len == 0) result = GetLine_eof;
735
if (len+1 >= *lineMax) {
736
char* newLineBuf = NULL;
737
int newBufSize = *lineMax;
739
newBufSize += (newBufSize/2) + 1;
740
if (newBufSize > MAX_LINE_LENGTH) newBufSize = MAX_LINE_LENGTH;
741
if (len+1 >= newBufSize) return GetLine_exceedMaxLineLength;
743
newLineBuf = (char*) realloc(*lineBuf, newBufSize);
744
if (newLineBuf == NULL) return GetLine_outOfMemory;
746
*lineBuf = newLineBuf;
747
*lineMax = newBufSize;
750
if (c == '\n') break;
751
(*lineBuf)[len++] = (char) c;
754
(*lineBuf)[len] = '\0';
762
static int charToHex(char c)
765
if (c >= '0' && c <= '9') {
766
result = (int) (c - '0');
767
} else if (c >= 'A' && c <= 'F') {
768
result = (int) (c - 'A') + 0x0a;
769
} else if (c >= 'a' && c <= 'f') {
770
result = (int) (c - 'a') + 0x0a;
780
static CanonicalFromStringResult canonicalFromString(unsigned char* dst,
785
for (i = 0; i < dstSize; ++i) {
788
h0 = charToHex(hashStr[i*2 + 0]);
789
if (h0 < 0) return CanonicalFromString_invalidFormat;
791
h1 = charToHex(hashStr[i*2 + 1]);
792
if (h1 < 0) return CanonicalFromString_invalidFormat;
794
dst[i] = (unsigned char) ((h0 << 4) | h1);
796
return CanonicalFromString_ok;
813
static ParseLineResult parseLine(ParsedLine* parsedLine, const char* line)
815
const char* const firstSpace = strchr(line, ' ');
816
const char* const secondSpace = firstSpace + 1;
818
parsedLine->filename = NULL;
819
parsedLine->xxhBits = 0;
821
if (firstSpace == NULL || *secondSpace != ' ') return ParseLine_invalidFormat;
823
switch (firstSpace - line)
826
{ XXH32_canonical_t* xxh32c = &parsedLine->canonical.xxh32;
827
if (canonicalFromString(xxh32c->digest, sizeof(xxh32c->digest), line)
828
!= CanonicalFromString_ok) {
829
return ParseLine_invalidFormat;
831
parsedLine->xxhBits = 32;
836
{ XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64;
837
if (canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), line)
838
!= CanonicalFromString_ok) {
839
return ParseLine_invalidFormat;
841
parsedLine->xxhBits = 64;
846
return ParseLine_invalidFormat;
850
parsedLine->filename = secondSpace + 1;
857
static void parseFile1(ParseFileArg* parseFileArg)
859
const char* const inFileName = parseFileArg->inFileName;
860
ParseFileReport* const report = &parseFileArg->report;
862
unsigned long lineNumber = 0;
863
memset(report, 0, sizeof(*report));
865
while (!report->quit) {
867
LineStatus lineStatus = LineStatus_hashFailed;
868
GetLineResult getLineResult;
869
ParsedLine parsedLine;
870
memset(&parsedLine, 0, sizeof(parsedLine));
873
if (lineNumber == 0) {
876
DISPLAY("%s : too many checksum lines\n", inFileName);
881
getLineResult = getLine(&parseFileArg->lineBuf, &parseFileArg->lineMax,
882
parseFileArg->inFile);
883
if (getLineResult != GetLine_ok) {
884
if (getLineResult == GetLine_eof) break;
886
switch (getLineResult)
895
DISPLAY("%s : %lu: unknown error\n", inFileName, lineNumber);
898
case GetLine_exceedMaxLineLength:
899
DISPLAY("%s : %lu: too long line\n", inFileName, lineNumber);
902
case GetLine_outOfMemory:
903
DISPLAY("%s : %lu: out of memory\n", inFileName, lineNumber);
910
if (parseLine(&parsedLine, parseFileArg->lineBuf) != ParseLine_ok) {
911
report->nImproperlyFormattedLines++;
912
if (parseFileArg->warn) {
913
DISPLAY("%s : %lu: improperly formatted XXHASH checksum line\n"
914
, inFileName, lineNumber);
919
if (report->xxhBits != 0 && report->xxhBits != parsedLine.xxhBits) {
921
report->nImproperlyFormattedLines++;
922
report->nMixedFormatLines++;
923
if (parseFileArg->warn) {
924
DISPLAY("%s : %lu: improperly formatted XXHASH checksum line (XXH32/64)\n"
925
, inFileName, lineNumber);
930
report->nProperlyFormattedLines++;
931
if (report->xxhBits == 0) {
932
report->xxhBits = parsedLine.xxhBits;
935
fp = fopen(parsedLine.filename, "rb");
937
lineStatus = LineStatus_failedToOpen;
939
lineStatus = LineStatus_hashFailed;
940
switch (parsedLine.xxhBits)
944
BMK_hashStream(&xxh, algo_xxh32, fp, parseFileArg->blockBuf, parseFileArg->blockSize);
945
if (xxh == XXH32_hashFromCanonical(&parsedLine.canonical.xxh32)) {
946
lineStatus = LineStatus_hashOk;
952
BMK_hashStream(&xxh, algo_xxh64, fp, parseFileArg->blockBuf, parseFileArg->blockSize);
953
if (xxh == XXH64_hashFromCanonical(&parsedLine.canonical.xxh64)) {
954
lineStatus = LineStatus_hashOk;
967
DISPLAY("%s : unknown error\n", inFileName);
971
case LineStatus_failedToOpen:
972
report->nOpenOrReadFailures++;
973
if (!parseFileArg->statusOnly) {
974
DISPLAYRESULT("%s : %lu: FAILED open or read %s\n"
975
, inFileName, lineNumber, parsedLine.filename);
979
case LineStatus_hashOk:
980
case LineStatus_hashFailed:
982
if (lineStatus == LineStatus_hashOk) {
984
if (parseFileArg->quiet) b = 0;
986
report->nMismatchedChecksums++;
989
if (b && !parseFileArg->statusOnly) {
990
DISPLAYRESULT("%s: %s\n", parsedLine.filename
991
, lineStatus == LineStatus_hashOk ? "OK" : "FAILED");
1015
static int checkFile(const char* inFileName,
1016
const endianess displayEndianess,
1023
FILE* inFile = NULL;
1024
ParseFileArg parseFileArgBody;
1025
ParseFileArg* const parseFileArg = &parseFileArgBody;
1026
ParseFileReport* const report = &parseFileArg->report;
1028
if (displayEndianess != big_endian) {
1030
DISPLAY( "Check file mode doesn't support little endian\n" );
1035
if (inFileName == stdinName) {
1040
inFile = fopen( inFileName, "rt" );
1043
if (inFile == NULL) {
1044
DISPLAY( "Pb opening %s\n", inFileName);
1048
parseFileArg->inFileName = inFileName;
1049
parseFileArg->inFile = inFile;
1050
parseFileArg->lineMax = DEFAULT_LINE_LENGTH;
1051
parseFileArg->lineBuf = (char*) malloc((size_t) parseFileArg->lineMax);
1052
parseFileArg->blockSize = 64 * 1024;
1053
parseFileArg->blockBuf = (char*) malloc(parseFileArg->blockSize);
1054
parseFileArg->strictMode = strictMode;
1055
parseFileArg->statusOnly = statusOnly;
1056
parseFileArg->warn = warn;
1057
parseFileArg->quiet = quiet;
1059
parseFile1(parseFileArg);
1061
free(parseFileArg->blockBuf);
1062
free(parseFileArg->lineBuf);
1064
if (inFile != stdin) fclose(inFile);
1068
if (report->nProperlyFormattedLines == 0) {
1069
DISPLAY("%s: no properly formatted XXHASH checksum lines found\n", inFileName);
1070
} else if (!statusOnly) {
1071
if (report->nImproperlyFormattedLines) {
1072
DISPLAYRESULT("%lu lines are improperly formatted\n"
1073
, report->nImproperlyFormattedLines);
1075
if (report->nOpenOrReadFailures) {
1076
DISPLAYRESULT("%lu listed files could not be read\n"
1077
, report->nOpenOrReadFailures);
1079
if (report->nMismatchedChecksums) {
1080
DISPLAYRESULT("%lu computed checksums did NOT match\n"
1081
, report->nMismatchedChecksums);
1086
result = report->nProperlyFormattedLines != 0
1087
&& report->nMismatchedChecksums == 0
1088
&& report->nOpenOrReadFailures == 0
1089
&& (!strictMode || report->nImproperlyFormattedLines == 0)
1090
&& report->quit == 0;
1095
static int checkFiles(const char** fnList, int fnTotal,
1096
const endianess displayEndianess,
1107
ok &= checkFile(stdinName, displayEndianess, strictMode, statusOnly, warn, quiet);
1110
for (fnNb=0; fnNb<fnTotal; fnNb++)
1111
ok &= checkFile(fnList[fnNb], displayEndianess, strictMode, statusOnly, warn, quiet);
1121
static int usage(const char* exename)
1123
DISPLAY( WELCOME_MESSAGE(exename) );
1124
DISPLAY( "Usage :\n");
1125
DISPLAY( " %s [arg] [filenames]\n", exename);
1126
DISPLAY( "When no filename provided, or - provided : use stdin as input\n");
1127
DISPLAY( "Arguments :\n");
1128
DISPLAY( " -H# : hash selection : 0=32bits, 1=64bits (default: %i)\n", (int)g_defaultAlgo);
1129
DISPLAY( " -c : read xxHash sums from the [filenames] and check them\n");
1130
DISPLAY( " -h : help \n");
1135
static int usage_advanced(const char* exename)
1138
DISPLAY( "Advanced :\n");
1139
DISPLAY( " --little-endian : hash printed using little endian convention (default: big endian)\n");
1140
DISPLAY( " -V, --version : display version\n");
1141
DISPLAY( " -h, --help : display long help and exit\n");
1142
DISPLAY( " -b : benchmark mode \n");
1143
DISPLAY( " -i# : number of iterations (benchmark mode; default %i)\n", g_nbIterations);
1145
DISPLAY( "The following four options are useful only when verifying checksums (-c):\n");
1146
DISPLAY( "--strict : don't print OK for each successfully verified file\n");
1147
DISPLAY( "--status : don't output anything, status code shows success\n");
1148
DISPLAY( "--quiet : exit non-zero for improperly formatted checksum lines\n");
1149
DISPLAY( "--warn : warn about improperly formatted checksum lines\n");
1153
static int badusage(const char* exename)
1155
DISPLAY("Wrong parameters\n");
1166
static unsigned readU32FromChar(const char** stringPtr)
1168
unsigned result = 0;
1169
while ((**stringPtr >='0') && (**stringPtr <='9'))
1170
result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
1171
if ((**stringPtr=='K') || (**stringPtr=='M')) {
1173
if (**stringPtr=='M') result <<= 10;
1175
if (**stringPtr=='i') (*stringPtr)++;
1176
if (**stringPtr=='B') (*stringPtr)++;
1181
int main(int argc, const char** argv)
1183
int i, filenamesStart = 0;
1184
const char* const exename = argv[0];
1185
U32 benchmarkMode = 0;
1186
U32 fileCheckMode = 0;
1191
U32 specificTest = 0;
1192
size_t keySize = XXH_DEFAULT_SAMPLE_SIZE;
1193
algoType algo = g_defaultAlgo;
1194
endianess displayEndianess = big_endian;
1197
if (strstr(exename, "xxh32sum") != NULL) algo = algo_xxh32;
1199
for(i=1; i<argc; i++) {
1200
const char* argument = argv[i];
1202
if(!argument) continue;
1204
if (!strcmp(argument, "--little-endian")) { displayEndianess = little_endian; continue; }
1205
if (!strcmp(argument, "--check")) { fileCheckMode = 1; continue; }
1206
if (!strcmp(argument, "--strict")) { strictMode = 1; continue; }
1207
if (!strcmp(argument, "--status")) { statusOnly = 1; continue; }
1208
if (!strcmp(argument, "--quiet")) { quiet = 1; continue; }
1209
if (!strcmp(argument, "--warn")) { warn = 1; continue; }
1210
if (!strcmp(argument, "--help")) { return usage_advanced(exename); }
1211
if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE(exename)); return 0; }
1213
if (*argument!='-') {
1214
if (filenamesStart==0) filenamesStart=i;
1221
while (*argument!=0) {
1226
DISPLAY(WELCOME_MESSAGE(exename)); return 0;
1230
return usage_advanced(exename);
1234
algo = (algoType)(argument[1] - '0');
1254
specificTest = readU32FromChar(&argument);
1260
g_nbIterations = readU32FromChar(&argument);
1266
keySize = readU32FromChar(&argument);
1276
return badusage(exename);
1282
if (benchmarkMode) {
1283
DISPLAYLEVEL(2, WELCOME_MESSAGE(exename) );
1285
if (filenamesStart==0) return BMK_benchInternal(keySize, specificTest);
1286
return BMK_benchFiles(argv+filenamesStart, argc-filenamesStart, specificTest);
1290
if ( (filenamesStart==0) && IS_CONSOLE(stdin) ) return badusage(exename);
1292
if (filenamesStart==0) filenamesStart = argc;
1293
if (fileCheckMode) {
1294
return checkFiles(argv+filenamesStart, argc-filenamesStart,
1295
displayEndianess, strictMode, statusOnly, warn, quiet);
1297
return BMK_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, displayEndianess);