Digest-xxHash
view release on metacpan or search on metacpan
ext/xxHash/xxhsum.c view on Meta::CPAN
# include <io.h> /* _setmode, _isatty */
# ifdef __MINGW32__
int _fileno(FILE *stream); /* MINGW somehow forgets to include this windows declaration into <stdio.h> */
# endif
# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
#else
# include <unistd.h> /* isatty, STDIN_FILENO */
# define SET_BINARY_MODE(file)
# define IS_CONSOLE(stdStream) isatty(STDIN_FILENO)
#endif
#if !defined(S_ISREG)
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif
/* ************************************
* Basic Types
**************************************/
#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
#endif
static unsigned BMK_isLittleEndian(void)
{
const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
return one.c[0];
}
/* *************************************
* Constants
***************************************/
#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)
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)
#define COMPILED __DATE__
static const char author[] = "Yann Collet";
#define WELCOME_MESSAGE(exename) "%s %s (%i-bits %s), by %s (%s) \n", exename, PROGRAM_VERSION, g_nbBits, ENDIAN_NAME, author, COMPILED
#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 */
#define KB *( 1<<10)
#define MB *( 1<<20)
#define GB *(1U<<30)
#define MAX_MEM (2 GB - 64 MB)
static const char stdinName[] = "-";
typedef enum { algo_xxh32, algo_xxh64 } algoType;
static const algoType g_defaultAlgo = algo_xxh64; /* required within main() & usage() */
/* <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)
/* Maximum acceptable line length. */
#define MAX_LINE_LENGTH (32 KB)
/* ************************************
* Display macros
**************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define DISPLAYRESULT(...) fprintf(stdout, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) DISPLAY(__VA_ARGS__);
static U32 g_displayLevel = 1;
/* ************************************
* Local variables
**************************************/
static size_t g_sampleSize = 100 KB;
static U32 g_nbIterations = NBLOOPS;
/* ************************************
* Benchmark Functions
**************************************/
static clock_t BMK_clockSpan( clock_t start )
{
return clock() - start; /* works even if overflow; Typical max span ~ 30 mn */
}
static size_t BMK_findMaxMem(U64 requiredMem)
{
size_t const step = 64 MB;
BYTE* 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;
ext/xxHash/xxhsum.c view on Meta::CPAN
/* 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;
/* Special case for stdinName "-",
* note: stdinName is not a string. It's special pointer. */
if (fnTotal==0) {
ok &= checkFile(stdinName, displayEndianess, strictMode, statusOnly, warn, quiet);
} else {
int fnNb;
for (fnNb=0; fnNb<fnTotal; fnNb++)
ok &= checkFile(fnList[fnNb], displayEndianess, strictMode, statusOnly, warn, quiet);
}
return ok ? 0 : 1;
}
/* ********************************************************
* Main
**********************************************************/
static int usage(const char* exename)
{
DISPLAY( WELCOME_MESSAGE(exename) );
DISPLAY( "Usage :\n");
DISPLAY( " %s [arg] [filenames]\n", exename);
DISPLAY( "When no filename provided, or - provided : use stdin as input\n");
DISPLAY( "Arguments :\n");
DISPLAY( " -H# : hash selection : 0=32bits, 1=64bits (default: %i)\n", (int)g_defaultAlgo);
DISPLAY( " -c : read xxHash sums from the [filenames] and check them\n");
DISPLAY( " -h : help \n");
return 0;
}
static int usage_advanced(const char* exename)
{
usage(exename);
DISPLAY( "Advanced :\n");
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");
DISPLAY( " -b : benchmark mode \n");
DISPLAY( " -i# : number of iterations (benchmark mode; default %i)\n", g_nbIterations);
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");
return 0;
}
static int badusage(const char* exename)
{
DISPLAY("Wrong parameters\n");
usage(exename);
return 1;
}
int main(int argc, const char** argv)
{
int i, filenamesStart=0;
const char* exename = argv[0];
U32 benchmarkMode = 0;
U32 fileCheckMode = 0;
U32 strictMode = 0;
U32 statusOnly = 0;
U32 warn = 0;
U32 quiet = 0;
algoType algo = g_defaultAlgo;
endianess displayEndianess = big_endian;
/* special case : xxh32sum default to 32 bits checksum */
if (strstr(exename, "xxh32sum") != NULL) algo = algo_xxh32;
for(i=1; i<argc; i++) {
const char* argument = argv[i];
if(!argument) continue; /* Protection, if argument empty */
if (!strcmp(argument, "--little-endian")) { displayEndianess = little_endian; continue; }
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; }
if (!strcmp(argument, "--help")) { return usage_advanced(exename); }
if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE(exename)); return 0; }
if (*argument!='-') {
if (filenamesStart==0) filenamesStart=i; /* only supports a continuous list of filenames */
continue;
}
/* command selection */
argument++; /* note : *argument=='-' */
while (*argument!=0) {
switch(*argument)
{
/* Display version */
case 'V':
( run in 0.650 second using v1.01-cache-2.11-cpan-71847e10f99 )