xxhsum.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. /*
  2. * xxhsum - Command line interface for xxhash algorithms
  3. * Copyright (C) 2013-2020 Yann Collet
  4. *
  5. * GPL v2 License
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * You can contact the author at:
  22. * - xxHash homepage: https://www.xxhash.com
  23. * - xxHash source repository: https://github.com/Cyan4973/xxHash
  24. */
  25. /*
  26. * xxhsum:
  27. * Provides hash value of a file content, or a list of files, or stdin
  28. * Display convention is Big Endian, for both 32 and 64 bits algorithms
  29. */
  30. /* Transitional headers */
  31. #include "cli/xsum_config.h"
  32. #include "cli/xsum_arch.h"
  33. #include "cli/xsum_os_specific.h"
  34. #include "cli/xsum_output.h"
  35. #include "cli/xsum_sanity_check.h"
  36. #ifdef XXH_INLINE_ALL
  37. # include "cli/xsum_os_specific.c"
  38. # include "cli/xsum_output.c"
  39. # include "cli/xsum_sanity_check.c"
  40. #endif
  41. /* ************************************
  42. * Includes
  43. **************************************/
  44. #include <limits.h>
  45. #include <stdlib.h> /* malloc, calloc, free, exit */
  46. #include <string.h> /* strcmp, memcpy */
  47. #include <stdio.h> /* fprintf, fopen, ftello64, fread, stdin, stdout, _fileno (when present) */
  48. #include <sys/types.h> /* stat, stat64, _stat64 */
  49. #include <sys/stat.h> /* stat, stat64, _stat64 */
  50. #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
  51. #include <assert.h> /* assert */
  52. #include <errno.h> /* errno */
  53. #define XXH_STATIC_LINKING_ONLY /* *_state_t */
  54. #include "xxhash.h"
  55. #ifdef XXHSUM_DISPATCH
  56. # include "xxh_x86dispatch.h"
  57. #endif
  58. static unsigned XSUM_isLittleEndian(void)
  59. {
  60. const union { XSUM_U32 u; XSUM_U8 c[4]; } one = { 1 }; /* don't use static: performance detrimental */
  61. return one.c[0];
  62. }
  63. static const int g_nbBits = (int)(sizeof(void*)*8);
  64. static const char g_lename[] = "little endian";
  65. static const char g_bename[] = "big endian";
  66. #define ENDIAN_NAME (XSUM_isLittleEndian() ? g_lename : g_bename)
  67. static const char author[] = "Yann Collet";
  68. #define WELCOME_MESSAGE(exename) "%s %s by %s \n", exename, XSUM_PROGRAM_VERSION, author
  69. #define FULL_WELCOME_MESSAGE(exename) "%s %s by %s \n" \
  70. "compiled as %i-bit %s %s with " XSUM_CC_VERSION_FMT " \n", \
  71. exename, XSUM_PROGRAM_VERSION, author, \
  72. g_nbBits, XSUM_ARCH, ENDIAN_NAME, XSUM_CC_VERSION
  73. #define KB *( 1<<10)
  74. #define MB *( 1<<20)
  75. #define GB *(1U<<30)
  76. static size_t XSUM_DEFAULT_SAMPLE_SIZE = 100 KB;
  77. #define NBLOOPS 3 /* Default number of benchmark iterations */
  78. #define TIMELOOP_S 1
  79. #define TIMELOOP (TIMELOOP_S * CLOCKS_PER_SEC) /* target timing per iteration */
  80. #define TIMELOOP_MIN (TIMELOOP / 2) /* minimum timing to validate a result */
  81. #define XXHSUM32_DEFAULT_SEED 0 /* Default seed for algo_xxh32 */
  82. #define XXHSUM64_DEFAULT_SEED 0 /* Default seed for algo_xxh64 */
  83. #define MAX_MEM (2 GB - 64 MB)
  84. static const char stdinName[] = "-";
  85. typedef enum { algo_xxh32=0, algo_xxh64=1, algo_xxh128=2 } AlgoSelected;
  86. static AlgoSelected g_defaultAlgo = algo_xxh64; /* required within main() & XSUM_usage() */
  87. /* <16 hex char> <SPC> <SPC> <filename> <'\0'>
  88. * '4096' is typical Linux PATH_MAX configuration. */
  89. #define DEFAULT_LINE_LENGTH (sizeof(XXH64_hash_t) * 2 + 2 + 4096 + 1)
  90. /* Maximum acceptable line length. */
  91. #define MAX_LINE_LENGTH (32 KB)
  92. /* ************************************
  93. * Display macros
  94. **************************************/
  95. /* ************************************
  96. * Local variables
  97. **************************************/
  98. static XSUM_U32 g_nbIterations = NBLOOPS;
  99. /* ************************************
  100. * Benchmark Functions
  101. **************************************/
  102. static clock_t XSUM_clockSpan( clock_t start )
  103. {
  104. return clock() - start; /* works even if overflow; Typical max span ~ 30 mn */
  105. }
  106. static size_t XSUM_findMaxMem(XSUM_U64 requiredMem)
  107. {
  108. size_t const step = 64 MB;
  109. void* testmem = NULL;
  110. requiredMem = (((requiredMem >> 26) + 1) << 26);
  111. requiredMem += 2*step;
  112. if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
  113. while (!testmem) {
  114. if (requiredMem > step) requiredMem -= step;
  115. else requiredMem >>= 1;
  116. testmem = malloc ((size_t)requiredMem);
  117. }
  118. free (testmem);
  119. /* keep some space available */
  120. if (requiredMem > step) requiredMem -= step;
  121. else requiredMem >>= 1;
  122. return (size_t)requiredMem;
  123. }
  124. /*
  125. * Allocates a string containing s1 and s2 concatenated. Acts like strdup.
  126. * The result must be freed.
  127. */
  128. static char* XSUM_strcatDup(const char* s1, const char* s2)
  129. {
  130. assert(s1 != NULL);
  131. assert(s2 != NULL);
  132. { size_t len1 = strlen(s1);
  133. size_t len2 = strlen(s2);
  134. char* buf = (char*)malloc(len1 + len2 + 1);
  135. if (buf != NULL) {
  136. /* strcpy(buf, s1) */
  137. memcpy(buf, s1, len1);
  138. /* strcat(buf, s2) */
  139. memcpy(buf + len1, s2, len2 + 1);
  140. }
  141. return buf;
  142. }
  143. }
  144. /*
  145. * A secret buffer used for benchmarking XXH3's withSecret variants.
  146. *
  147. * In order for the bench to be realistic, the secret buffer would need to be
  148. * pre-generated.
  149. *
  150. * Adding a pointer to the parameter list would be messy.
  151. */
  152. static XSUM_U8 g_benchSecretBuf[XXH3_SECRET_SIZE_MIN];
  153. /*
  154. * Wrappers for the benchmark.
  155. *
  156. * If you would like to add other hashes to the bench, create a wrapper and add
  157. * it to the g_hashesToBench table. It will automatically be added.
  158. */
  159. typedef XSUM_U32 (*hashFunction)(const void* buffer, size_t bufferSize, XSUM_U32 seed);
  160. static XSUM_U32 localXXH32(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  161. {
  162. return XXH32(buffer, bufferSize, seed);
  163. }
  164. static XSUM_U32 localXXH64(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  165. {
  166. return (XSUM_U32)XXH64(buffer, bufferSize, seed);
  167. }
  168. static XSUM_U32 localXXH3_64b(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  169. {
  170. (void)seed;
  171. return (XSUM_U32)XXH3_64bits(buffer, bufferSize);
  172. }
  173. static XSUM_U32 localXXH3_64b_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  174. {
  175. return (XSUM_U32)XXH3_64bits_withSeed(buffer, bufferSize, seed);
  176. }
  177. static XSUM_U32 localXXH3_64b_secret(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  178. {
  179. (void)seed;
  180. return (XSUM_U32)XXH3_64bits_withSecret(buffer, bufferSize, g_benchSecretBuf, sizeof(g_benchSecretBuf));
  181. }
  182. static XSUM_U32 localXXH3_128b(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  183. {
  184. (void)seed;
  185. return (XSUM_U32)(XXH3_128bits(buffer, bufferSize).low64);
  186. }
  187. static XSUM_U32 localXXH3_128b_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  188. {
  189. return (XSUM_U32)(XXH3_128bits_withSeed(buffer, bufferSize, seed).low64);
  190. }
  191. static XSUM_U32 localXXH3_128b_secret(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  192. {
  193. (void)seed;
  194. return (XSUM_U32)(XXH3_128bits_withSecret(buffer, bufferSize, g_benchSecretBuf, sizeof(g_benchSecretBuf)).low64);
  195. }
  196. static XSUM_U32 localXXH3_stream(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  197. {
  198. XXH3_state_t state;
  199. (void)seed;
  200. XXH3_64bits_reset(&state);
  201. XXH3_64bits_update(&state, buffer, bufferSize);
  202. return (XSUM_U32)XXH3_64bits_digest(&state);
  203. }
  204. static XSUM_U32 localXXH3_stream_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  205. {
  206. XXH3_state_t state;
  207. XXH3_INITSTATE(&state);
  208. XXH3_64bits_reset_withSeed(&state, (XXH64_hash_t)seed);
  209. XXH3_64bits_update(&state, buffer, bufferSize);
  210. return (XSUM_U32)XXH3_64bits_digest(&state);
  211. }
  212. static XSUM_U32 localXXH128_stream(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  213. {
  214. XXH3_state_t state;
  215. (void)seed;
  216. XXH3_128bits_reset(&state);
  217. XXH3_128bits_update(&state, buffer, bufferSize);
  218. return (XSUM_U32)(XXH3_128bits_digest(&state).low64);
  219. }
  220. static XSUM_U32 localXXH128_stream_seeded(const void* buffer, size_t bufferSize, XSUM_U32 seed)
  221. {
  222. XXH3_state_t state;
  223. XXH3_INITSTATE(&state);
  224. XXH3_128bits_reset_withSeed(&state, (XXH64_hash_t)seed);
  225. XXH3_128bits_update(&state, buffer, bufferSize);
  226. return (XSUM_U32)(XXH3_128bits_digest(&state).low64);
  227. }
  228. typedef struct {
  229. const char* name;
  230. hashFunction func;
  231. } hashInfo;
  232. #define NB_HASHFUNC 12
  233. static const hashInfo g_hashesToBench[NB_HASHFUNC] = {
  234. { "XXH32", &localXXH32 },
  235. { "XXH64", &localXXH64 },
  236. { "XXH3_64b", &localXXH3_64b },
  237. { "XXH3_64b w/seed", &localXXH3_64b_seeded },
  238. { "XXH3_64b w/secret", &localXXH3_64b_secret },
  239. { "XXH128", &localXXH3_128b },
  240. { "XXH128 w/seed", &localXXH3_128b_seeded },
  241. { "XXH128 w/secret", &localXXH3_128b_secret },
  242. { "XXH3_stream", &localXXH3_stream },
  243. { "XXH3_stream w/seed",&localXXH3_stream_seeded },
  244. { "XXH128_stream", &localXXH128_stream },
  245. { "XXH128_stream w/seed",&localXXH128_stream_seeded },
  246. };
  247. #define NB_TESTFUNC (1 + 2 * NB_HASHFUNC)
  248. static char g_testIDs[NB_TESTFUNC] = { 0 };
  249. static const char k_testIDs_default[NB_TESTFUNC] = { 0,
  250. 1 /*XXH32*/, 0,
  251. 1 /*XXH64*/, 0,
  252. 1 /*XXH3*/, 0, 0, 0, 0, 0,
  253. 1 /*XXH128*/ };
  254. #define HASHNAME_MAX 29
  255. static void XSUM_benchHash(hashFunction h, const char* hName, int testID,
  256. const void* buffer, size_t bufferSize)
  257. {
  258. XSUM_U32 nbh_perIteration = (XSUM_U32)((300 MB) / (bufferSize+1)) + 1; /* first iteration conservatively aims for 300 MB/s */
  259. unsigned iterationNb, nbIterations = g_nbIterations + !g_nbIterations /* min 1 */;
  260. double fastestH = 100000000.;
  261. assert(HASHNAME_MAX > 2);
  262. XSUM_logVerbose(2, "\r%80s\r", ""); /* Clean display line */
  263. for (iterationNb = 1; iterationNb <= nbIterations; iterationNb++) {
  264. XSUM_U32 r=0;
  265. clock_t cStart;
  266. XSUM_logVerbose(2, "%2u-%-*.*s : %10u ->\r",
  267. iterationNb,
  268. HASHNAME_MAX, HASHNAME_MAX, hName,
  269. (unsigned)bufferSize);
  270. cStart = clock();
  271. while (clock() == cStart); /* starts clock() at its exact beginning */
  272. cStart = clock();
  273. { XSUM_U32 u;
  274. for (u=0; u<nbh_perIteration; u++)
  275. r += h(buffer, bufferSize, u);
  276. }
  277. if (r==0) XSUM_logVerbose(3,".\r"); /* do something with r to defeat compiler "optimizing" hash away */
  278. { clock_t const nbTicks = XSUM_clockSpan(cStart);
  279. double const ticksPerHash = ((double)nbTicks / TIMELOOP) / nbh_perIteration;
  280. /*
  281. * clock() is the only decent portable timer, but it isn't very
  282. * precise.
  283. *
  284. * Sometimes, this lack of precision is enough that the benchmark
  285. * finishes before there are enough ticks to get a meaningful result.
  286. *
  287. * For example, on a Core 2 Duo (without any sort of Turbo Boost),
  288. * the imprecise timer caused peculiar results like so:
  289. *
  290. * XXH3_64b 4800.0 MB/s // conveniently even
  291. * XXH3_64b unaligned 4800.0 MB/s
  292. * XXH3_64b seeded 9600.0 MB/s // magical 2x speedup?!
  293. * XXH3_64b seeded unaligned 4800.0 MB/s
  294. *
  295. * If we sense a suspiciously low number of ticks, we increase the
  296. * iterations until we can get something meaningful.
  297. */
  298. if (nbTicks < TIMELOOP_MIN) {
  299. /* Not enough time spent in benchmarking, risk of rounding bias */
  300. if (nbTicks == 0) { /* faster than resolution timer */
  301. nbh_perIteration *= 100;
  302. } else {
  303. /*
  304. * update nbh_perIteration so that the next round lasts
  305. * approximately 1 second.
  306. */
  307. double nbh_perSecond = (1 / ticksPerHash) + 1;
  308. if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20); /* avoid overflow */
  309. nbh_perIteration = (XSUM_U32)nbh_perSecond;
  310. }
  311. /* g_nbIterations==0 => quick evaluation, no claim of accuracy */
  312. if (g_nbIterations>0) {
  313. iterationNb--; /* new round for a more accurate speed evaluation */
  314. continue;
  315. }
  316. }
  317. if (ticksPerHash < fastestH) fastestH = ticksPerHash;
  318. if (fastestH>0.) { /* avoid div by zero */
  319. XSUM_logVerbose(2, "%2u-%-*.*s : %10u -> %8.0f it/s (%7.1f MB/s) \r",
  320. iterationNb,
  321. HASHNAME_MAX, HASHNAME_MAX, hName,
  322. (unsigned)bufferSize,
  323. (double)1 / fastestH,
  324. ((double)bufferSize / (1 MB)) / fastestH);
  325. } }
  326. { double nbh_perSecond = (1 / fastestH) + 1;
  327. if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20); /* avoid overflow */
  328. nbh_perIteration = (XSUM_U32)nbh_perSecond;
  329. }
  330. }
  331. XSUM_logVerbose(1, "%2i#%-*.*s : %10u -> %8.0f it/s (%7.1f MB/s) \n",
  332. testID,
  333. HASHNAME_MAX, HASHNAME_MAX, hName,
  334. (unsigned)bufferSize,
  335. (double)1 / fastestH,
  336. ((double)bufferSize / (1 MB)) / fastestH);
  337. if (XSUM_logLevel<1)
  338. XSUM_logVerbose(0, "%u, ", (unsigned)((double)1 / fastestH));
  339. }
  340. /*!
  341. * XSUM_benchMem():
  342. * buffer: Must be 16-byte aligned.
  343. * The real allocated size of buffer is supposed to be >= (bufferSize+3).
  344. * returns: 0 on success, 1 if error (invalid mode selected)
  345. */
  346. static void XSUM_benchMem(const void* buffer, size_t bufferSize)
  347. {
  348. assert((((size_t)buffer) & 15) == 0); /* ensure alignment */
  349. XSUM_fillTestBuffer(g_benchSecretBuf, sizeof(g_benchSecretBuf));
  350. { int i;
  351. for (i = 1; i < NB_TESTFUNC; i++) {
  352. int const hashFuncID = (i-1) / 2;
  353. assert(g_hashesToBench[hashFuncID].name != NULL);
  354. if (g_testIDs[i] == 0) continue;
  355. /* aligned */
  356. if ((i % 2) == 1) {
  357. XSUM_benchHash(g_hashesToBench[hashFuncID].func, g_hashesToBench[hashFuncID].name, i, buffer, bufferSize);
  358. }
  359. /* unaligned */
  360. if ((i % 2) == 0) {
  361. /* Append "unaligned". */
  362. char* const hashNameBuf = XSUM_strcatDup(g_hashesToBench[hashFuncID].name, " unaligned");
  363. assert(hashNameBuf != NULL);
  364. XSUM_benchHash(g_hashesToBench[hashFuncID].func, hashNameBuf, i, ((const char*)buffer)+3, bufferSize);
  365. free(hashNameBuf);
  366. }
  367. } }
  368. }
  369. static size_t XSUM_selectBenchedSize(const char* fileName)
  370. {
  371. XSUM_U64 const inFileSize = XSUM_getFileSize(fileName);
  372. size_t benchedSize = (size_t) XSUM_findMaxMem(inFileSize);
  373. if ((XSUM_U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
  374. if (benchedSize < inFileSize) {
  375. XSUM_log("Not enough memory for '%s' full size; testing %i MB only...\n", fileName, (int)(benchedSize>>20));
  376. }
  377. return benchedSize;
  378. }
  379. static int XSUM_benchFiles(char*const* fileNamesTable, int nbFiles)
  380. {
  381. int fileIdx;
  382. for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
  383. const char* const inFileName = fileNamesTable[fileIdx];
  384. assert(inFileName != NULL);
  385. { FILE* const inFile = XSUM_fopen( inFileName, "rb" );
  386. size_t const benchedSize = XSUM_selectBenchedSize(inFileName);
  387. char* const buffer = (char*)calloc(benchedSize+16+3, 1);
  388. void* const alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF); /* align on next 16 bytes */
  389. /* Checks */
  390. if (inFile==NULL){
  391. XSUM_log("Error: Could not open '%s': %s.\n", inFileName, strerror(errno));
  392. free(buffer);
  393. exit(11);
  394. }
  395. if(!buffer) {
  396. XSUM_log("\nError: Out of memory.\n");
  397. fclose(inFile);
  398. exit(12);
  399. }
  400. /* Fill input buffer */
  401. { size_t const readSize = fread(alignedBuffer, 1, benchedSize, inFile);
  402. fclose(inFile);
  403. if(readSize != benchedSize) {
  404. XSUM_log("\nError: Could not read '%s': %s.\n", inFileName, strerror(errno));
  405. free(buffer);
  406. exit(13);
  407. } }
  408. /* bench */
  409. XSUM_benchMem(alignedBuffer, benchedSize);
  410. free(buffer);
  411. } }
  412. return 0;
  413. }
  414. static int XSUM_benchInternal(size_t keySize)
  415. {
  416. void* const buffer = calloc(keySize+16+3, 1);
  417. if (buffer == NULL) {
  418. XSUM_log("\nError: Out of memory.\n");
  419. exit(12);
  420. }
  421. { const void* const alignedBuffer = ((char*)buffer+15) - (((size_t)((char*)buffer+15)) & 0xF); /* align on next 16 bytes */
  422. /* bench */
  423. XSUM_logVerbose(1, "Sample of ");
  424. if (keySize > 10 KB) {
  425. XSUM_logVerbose(1, "%u KB", (unsigned)(keySize >> 10));
  426. } else {
  427. XSUM_logVerbose(1, "%u bytes", (unsigned)keySize);
  428. }
  429. XSUM_logVerbose(1, "... \n");
  430. XSUM_benchMem(alignedBuffer, keySize);
  431. free(buffer);
  432. }
  433. return 0;
  434. }
  435. /* ********************************************************
  436. * File Hashing
  437. **********************************************************/
  438. /* for support of --little-endian display mode */
  439. static void XSUM_display_LittleEndian(const void* ptr, size_t length)
  440. {
  441. const XSUM_U8* const p = (const XSUM_U8*)ptr;
  442. size_t idx;
  443. for (idx=length-1; idx<length; idx--) /* intentional underflow to negative to detect end */
  444. XSUM_output("%02x", p[idx]);
  445. }
  446. static void XSUM_display_BigEndian(const void* ptr, size_t length)
  447. {
  448. const XSUM_U8* const p = (const XSUM_U8*)ptr;
  449. size_t idx;
  450. for (idx=0; idx<length; idx++)
  451. XSUM_output("%02x", p[idx]);
  452. }
  453. typedef union {
  454. XXH32_hash_t xxh32;
  455. XXH64_hash_t xxh64;
  456. XXH128_hash_t xxh128;
  457. } Multihash;
  458. /*
  459. * XSUM_hashStream:
  460. * Reads data from `inFile`, generating an incremental hash of type hashType,
  461. * using `buffer` of size `blockSize` for temporary storage.
  462. */
  463. static Multihash
  464. XSUM_hashStream(FILE* inFile,
  465. AlgoSelected hashType,
  466. void* buffer, size_t blockSize)
  467. {
  468. XXH32_state_t state32;
  469. XXH64_state_t state64;
  470. XXH3_state_t state128;
  471. /* Init */
  472. (void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
  473. (void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
  474. (void)XXH3_128bits_reset(&state128);
  475. /* Load file & update hash */
  476. { size_t readSize;
  477. while ((readSize = fread(buffer, 1, blockSize, inFile)) > 0) {
  478. switch(hashType)
  479. {
  480. case algo_xxh32:
  481. (void)XXH32_update(&state32, buffer, readSize);
  482. break;
  483. case algo_xxh64:
  484. (void)XXH64_update(&state64, buffer, readSize);
  485. break;
  486. case algo_xxh128:
  487. (void)XXH3_128bits_update(&state128, buffer, readSize);
  488. break;
  489. default:
  490. assert(0);
  491. }
  492. }
  493. if (ferror(inFile)) {
  494. XSUM_log("Error: a failure occurred reading the input file.\n");
  495. exit(1);
  496. } }
  497. { Multihash finalHash = {0};
  498. switch(hashType)
  499. {
  500. case algo_xxh32:
  501. finalHash.xxh32 = XXH32_digest(&state32);
  502. break;
  503. case algo_xxh64:
  504. finalHash.xxh64 = XXH64_digest(&state64);
  505. break;
  506. case algo_xxh128:
  507. finalHash.xxh128 = XXH3_128bits_digest(&state128);
  508. break;
  509. default:
  510. assert(0);
  511. }
  512. return finalHash;
  513. }
  514. }
  515. /* algo_xxh32, algo_xxh64, algo_xxh128 */
  516. static const char* XSUM_algoName[] = { "XXH32", "XXH64", "XXH128" };
  517. static const char* XSUM_algoLE_name[] = { "XXH32_LE", "XXH64_LE", "XXH128_LE" };
  518. static const size_t XSUM_algoLength[] = { 4, 8, 16 };
  519. #define XSUM_TABLE_ELT_SIZE(table) (sizeof(table) / sizeof(*table))
  520. typedef void (*XSUM_displayHash_f)(const void*, size_t); /* display function signature */
  521. static void XSUM_printLine_BSD_internal(const char* filename,
  522. const void* canonicalHash, const AlgoSelected hashType,
  523. const char* algoString[],
  524. XSUM_displayHash_f f_displayHash)
  525. {
  526. assert(0 <= hashType && hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName));
  527. { const char* const typeString = algoString[hashType];
  528. const size_t hashLength = XSUM_algoLength[hashType];
  529. XSUM_output("%s (%s) = ", typeString, filename);
  530. f_displayHash(canonicalHash, hashLength);
  531. XSUM_output("\n");
  532. } }
  533. static void XSUM_printLine_BSD_LE(const char* filename, const void* canonicalHash, const AlgoSelected hashType)
  534. {
  535. XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoLE_name, XSUM_display_LittleEndian);
  536. }
  537. static void XSUM_printLine_BSD(const char* filename, const void* canonicalHash, const AlgoSelected hashType)
  538. {
  539. XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoName, XSUM_display_BigEndian);
  540. }
  541. static void XSUM_printLine_GNU_internal(const char* filename,
  542. const void* canonicalHash, const AlgoSelected hashType,
  543. XSUM_displayHash_f f_displayHash)
  544. {
  545. assert(0 <= hashType && hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName));
  546. { const size_t hashLength = XSUM_algoLength[hashType];
  547. f_displayHash(canonicalHash, hashLength);
  548. XSUM_output(" %s\n", filename);
  549. } }
  550. static void XSUM_printLine_GNU(const char* filename,
  551. const void* canonicalHash, const AlgoSelected hashType)
  552. {
  553. XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_BigEndian);
  554. }
  555. static void XSUM_printLine_GNU_LE(const char* filename,
  556. const void* canonicalHash, const AlgoSelected hashType)
  557. {
  558. XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_LittleEndian);
  559. }
  560. typedef enum { big_endian, little_endian} Display_endianess;
  561. typedef enum { display_gnu, display_bsd } Display_convention;
  562. typedef void (*XSUM_displayLine_f)(const char*, const void*, AlgoSelected); /* line display signature */
  563. static XSUM_displayLine_f XSUM_kDisplayLine_fTable[2][2] = {
  564. { XSUM_printLine_GNU, XSUM_printLine_GNU_LE },
  565. { XSUM_printLine_BSD, XSUM_printLine_BSD_LE }
  566. };
  567. static int XSUM_hashFile(const char* fileName,
  568. const AlgoSelected hashType,
  569. const Display_endianess displayEndianess,
  570. const Display_convention convention)
  571. {
  572. size_t const blockSize = 64 KB;
  573. XSUM_displayLine_f const f_displayLine = XSUM_kDisplayLine_fTable[convention][displayEndianess];
  574. FILE* inFile;
  575. Multihash hashValue;
  576. assert(displayEndianess==big_endian || displayEndianess==little_endian);
  577. assert(convention==display_gnu || convention==display_bsd);
  578. /* Check file existence */
  579. if (fileName == stdinName) {
  580. inFile = stdin;
  581. fileName = "stdin";
  582. XSUM_setBinaryMode(stdin);
  583. } else {
  584. if (XSUM_isDirectory(fileName)) {
  585. XSUM_log("xxhsum: %s: Is a directory \n", fileName);
  586. return 1;
  587. }
  588. inFile = XSUM_fopen( fileName, "rb" );
  589. if (inFile==NULL) {
  590. XSUM_log("Error: Could not open '%s': %s. \n", fileName, strerror(errno));
  591. return 1;
  592. } }
  593. /* Memory allocation & streaming */
  594. { void* const buffer = malloc(blockSize);
  595. if (buffer == NULL) {
  596. XSUM_log("\nError: Out of memory.\n");
  597. fclose(inFile);
  598. return 1;
  599. }
  600. /* Stream file & update hash */
  601. hashValue = XSUM_hashStream(inFile, hashType, buffer, blockSize);
  602. fclose(inFile);
  603. free(buffer);
  604. }
  605. /* display Hash value in selected format */
  606. switch(hashType)
  607. {
  608. case algo_xxh32:
  609. { XXH32_canonical_t hcbe32;
  610. (void)XXH32_canonicalFromHash(&hcbe32, hashValue.xxh32);
  611. f_displayLine(fileName, &hcbe32, hashType);
  612. break;
  613. }
  614. case algo_xxh64:
  615. { XXH64_canonical_t hcbe64;
  616. (void)XXH64_canonicalFromHash(&hcbe64, hashValue.xxh64);
  617. f_displayLine(fileName, &hcbe64, hashType);
  618. break;
  619. }
  620. case algo_xxh128:
  621. { XXH128_canonical_t hcbe128;
  622. (void)XXH128_canonicalFromHash(&hcbe128, hashValue.xxh128);
  623. f_displayLine(fileName, &hcbe128, hashType);
  624. break;
  625. }
  626. default:
  627. assert(0); /* not possible */
  628. }
  629. return 0;
  630. }
  631. /*
  632. * XSUM_hashFiles:
  633. * If fnTotal==0, read from stdin instead.
  634. */
  635. static int XSUM_hashFiles(char*const * fnList, int fnTotal,
  636. AlgoSelected hashType,
  637. Display_endianess displayEndianess,
  638. Display_convention convention)
  639. {
  640. int fnNb;
  641. int result = 0;
  642. if (fnTotal==0)
  643. return XSUM_hashFile(stdinName, hashType, displayEndianess, convention);
  644. for (fnNb=0; fnNb<fnTotal; fnNb++)
  645. result |= XSUM_hashFile(fnList[fnNb], hashType, displayEndianess, convention);
  646. XSUM_logVerbose(2, "\r%70s\r", "");
  647. return result;
  648. }
  649. typedef enum {
  650. GetLine_ok,
  651. GetLine_eof,
  652. GetLine_exceedMaxLineLength,
  653. GetLine_outOfMemory
  654. } GetLineResult;
  655. typedef enum {
  656. CanonicalFromString_ok,
  657. CanonicalFromString_invalidFormat
  658. } CanonicalFromStringResult;
  659. typedef enum {
  660. ParseLine_ok,
  661. ParseLine_invalidFormat
  662. } ParseLineResult;
  663. typedef enum {
  664. LineStatus_hashOk,
  665. LineStatus_hashFailed,
  666. LineStatus_failedToOpen
  667. } LineStatus;
  668. typedef union {
  669. XXH32_canonical_t xxh32;
  670. XXH64_canonical_t xxh64;
  671. XXH128_canonical_t xxh128;
  672. } Canonical;
  673. typedef struct {
  674. Canonical canonical;
  675. const char* filename;
  676. int xxhBits; /* canonical type: 32:xxh32, 64:xxh64, 128:xxh128 */
  677. } ParsedLine;
  678. typedef struct {
  679. unsigned long nProperlyFormattedLines;
  680. unsigned long nImproperlyFormattedLines;
  681. unsigned long nMismatchedChecksums;
  682. unsigned long nOpenOrReadFailures;
  683. unsigned long nMixedFormatLines;
  684. int quit;
  685. } ParseFileReport;
  686. typedef struct {
  687. const char* inFileName;
  688. FILE* inFile;
  689. int lineMax;
  690. char* lineBuf;
  691. size_t blockSize;
  692. char* blockBuf;
  693. XSUM_U32 strictMode;
  694. XSUM_U32 statusOnly;
  695. XSUM_U32 warn;
  696. XSUM_U32 quiet;
  697. ParseFileReport report;
  698. } ParseFileArg;
  699. /*
  700. * Reads a line from stream `inFile`.
  701. * Returns GetLine_ok, if it reads line successfully.
  702. * Returns GetLine_eof, if stream reaches EOF.
  703. * Returns GetLine_exceedMaxLineLength, if line length is longer than MAX_LINE_LENGTH.
  704. * Returns GetLine_outOfMemory, if line buffer memory allocation failed.
  705. */
  706. static GetLineResult XSUM_getLine(char** lineBuf, int* lineMax, FILE* inFile)
  707. {
  708. GetLineResult result = GetLine_ok;
  709. size_t len = 0;
  710. if ((*lineBuf == NULL) || (*lineMax<1)) {
  711. free(*lineBuf); /* in case it's != NULL */
  712. *lineMax = 0;
  713. *lineBuf = (char*)malloc(DEFAULT_LINE_LENGTH);
  714. if(*lineBuf == NULL) return GetLine_outOfMemory;
  715. *lineMax = DEFAULT_LINE_LENGTH;
  716. }
  717. for (;;) {
  718. const int c = fgetc(inFile);
  719. if (c == EOF) {
  720. /*
  721. * If we meet EOF before first character, returns GetLine_eof,
  722. * otherwise GetLine_ok.
  723. */
  724. if (len == 0) result = GetLine_eof;
  725. break;
  726. }
  727. /* Make enough space for len+1 (for final NUL) bytes. */
  728. if (len+1 >= (size_t)*lineMax) {
  729. char* newLineBuf = NULL;
  730. size_t newBufSize = (size_t)*lineMax;
  731. newBufSize += (newBufSize/2) + 1; /* x 1.5 */
  732. if (newBufSize > MAX_LINE_LENGTH) newBufSize = MAX_LINE_LENGTH;
  733. if (len+1 >= newBufSize) return GetLine_exceedMaxLineLength;
  734. newLineBuf = (char*) realloc(*lineBuf, newBufSize);
  735. if (newLineBuf == NULL) return GetLine_outOfMemory;
  736. *lineBuf = newLineBuf;
  737. *lineMax = (int)newBufSize;
  738. }
  739. if (c == '\n') break;
  740. (*lineBuf)[len++] = (char) c;
  741. }
  742. (*lineBuf)[len] = '\0';
  743. return result;
  744. }
  745. /*
  746. * Converts one hexadecimal character to integer.
  747. * Returns -1 if the given character is not hexadecimal.
  748. */
  749. static int charToHex(char c)
  750. {
  751. int result = -1;
  752. if (c >= '0' && c <= '9') {
  753. result = (int) (c - '0');
  754. } else if (c >= 'A' && c <= 'F') {
  755. result = (int) (c - 'A') + 0x0a;
  756. } else if (c >= 'a' && c <= 'f') {
  757. result = (int) (c - 'a') + 0x0a;
  758. }
  759. return result;
  760. }
  761. /*
  762. * Converts canonical ASCII hexadecimal string `hashStr`
  763. * to the big endian binary representation in unsigned char array `dst`.
  764. *
  765. * Returns CanonicalFromString_invalidFormat if hashStr is not well formatted.
  766. * Returns CanonicalFromString_ok if hashStr is parsed successfully.
  767. */
  768. static CanonicalFromStringResult XSUM_canonicalFromString(unsigned char* dst,
  769. size_t dstSize,
  770. const char* hashStr,
  771. int reverseBytes)
  772. {
  773. size_t i;
  774. for (i = 0; i < dstSize; ++i) {
  775. int h0, h1;
  776. size_t j = reverseBytes ? dstSize - i - 1 : i;
  777. h0 = charToHex(hashStr[j*2 + 0]);
  778. if (h0 < 0) return CanonicalFromString_invalidFormat;
  779. h1 = charToHex(hashStr[j*2 + 1]);
  780. if (h1 < 0) return CanonicalFromString_invalidFormat;
  781. dst[i] = (unsigned char) ((h0 << 4) | h1);
  782. }
  783. return CanonicalFromString_ok;
  784. }
  785. /*
  786. * Parse single line of xxHash checksum file.
  787. * Returns ParseLine_invalidFormat if the line is not well formatted.
  788. * Returns ParseLine_ok if the line is parsed successfully.
  789. * And members of XSUM_parseLine will be filled by parsed values.
  790. *
  791. * - line must be terminated with '\0' without a trailing newline.
  792. * - Since parsedLine.filename will point within given argument `line`,
  793. * users must keep `line`s content when they are using parsedLine.
  794. * - The line may be modified to carve up the information it contains.
  795. *
  796. * xxHash checksum lines should have the following format:
  797. *
  798. * <8, 16, or 32 hexadecimal char> <space> <space> <filename...> <'\0'>
  799. *
  800. * or:
  801. *
  802. * <algorithm> <' ('> <filename> <') = '> <hexstring> <'\0'>
  803. */
  804. static ParseLineResult XSUM_parseLine(ParsedLine* parsedLine, char* line, int rev)
  805. {
  806. char* const firstSpace = strchr(line, ' ');
  807. const char* hash_ptr;
  808. size_t hash_len;
  809. parsedLine->filename = NULL;
  810. parsedLine->xxhBits = 0;
  811. if (firstSpace == NULL || !firstSpace[1]) return ParseLine_invalidFormat;
  812. if (firstSpace[1] == '(') {
  813. char* lastSpace = strrchr(line, ' ');
  814. if (lastSpace - firstSpace < 5) return ParseLine_invalidFormat;
  815. if (lastSpace[-1] != '=' || lastSpace[-2] != ' ' || lastSpace[-3] != ')') return ParseLine_invalidFormat;
  816. lastSpace[-3] = '\0'; /* Terminate the filename */
  817. *firstSpace = '\0';
  818. rev = strstr(line, "_LE") != NULL; /* was output little-endian */
  819. hash_ptr = lastSpace + 1;
  820. hash_len = strlen(hash_ptr);
  821. /* NOTE: This currently ignores the hash description at the start of the string.
  822. * In the future we should parse it and verify that it matches the hash length.
  823. * It could also be used to allow both XXH64 & XXH3_64bits to be differentiated. */
  824. } else {
  825. hash_ptr = line;
  826. hash_len = (size_t)(firstSpace - line);
  827. }
  828. switch (hash_len)
  829. {
  830. case 8:
  831. { XXH32_canonical_t* xxh32c = &parsedLine->canonical.xxh32;
  832. if (XSUM_canonicalFromString(xxh32c->digest, sizeof(xxh32c->digest), hash_ptr, rev)
  833. != CanonicalFromString_ok) {
  834. return ParseLine_invalidFormat;
  835. }
  836. parsedLine->xxhBits = 32;
  837. break;
  838. }
  839. case 16:
  840. { XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64;
  841. if (XSUM_canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), hash_ptr, rev)
  842. != CanonicalFromString_ok) {
  843. return ParseLine_invalidFormat;
  844. }
  845. parsedLine->xxhBits = 64;
  846. break;
  847. }
  848. case 32:
  849. { XXH128_canonical_t* xxh128c = &parsedLine->canonical.xxh128;
  850. if (XSUM_canonicalFromString(xxh128c->digest, sizeof(xxh128c->digest), hash_ptr, rev)
  851. != CanonicalFromString_ok) {
  852. return ParseLine_invalidFormat;
  853. }
  854. parsedLine->xxhBits = 128;
  855. break;
  856. }
  857. default:
  858. return ParseLine_invalidFormat;
  859. break;
  860. }
  861. /* note : skipping second separation character, which can be anything,
  862. * allowing insertion of custom markers such as '*' */
  863. parsedLine->filename = firstSpace + 2;
  864. return ParseLine_ok;
  865. }
  866. /*!
  867. * Parse xxHash checksum file.
  868. */
  869. static void XSUM_parseFile1(ParseFileArg* XSUM_parseFileArg, int rev)
  870. {
  871. const char* const inFileName = XSUM_parseFileArg->inFileName;
  872. ParseFileReport* const report = &XSUM_parseFileArg->report;
  873. unsigned long lineNumber = 0;
  874. memset(report, 0, sizeof(*report));
  875. while (!report->quit) {
  876. LineStatus lineStatus = LineStatus_hashFailed;
  877. ParsedLine parsedLine;
  878. memset(&parsedLine, 0, sizeof(parsedLine));
  879. lineNumber++;
  880. if (lineNumber == 0) {
  881. /* This is unlikely happen, but md5sum.c has this error check. */
  882. XSUM_log("%s: Error: Too many checksum lines\n", inFileName);
  883. report->quit = 1;
  884. break;
  885. }
  886. { GetLineResult const XSUM_getLineResult = XSUM_getLine(&XSUM_parseFileArg->lineBuf,
  887. &XSUM_parseFileArg->lineMax,
  888. XSUM_parseFileArg->inFile);
  889. if (XSUM_getLineResult != GetLine_ok) {
  890. if (XSUM_getLineResult == GetLine_eof) break;
  891. switch (XSUM_getLineResult)
  892. {
  893. case GetLine_ok:
  894. case GetLine_eof:
  895. /* These cases never happen. See above XSUM_getLineResult related "if"s.
  896. They exist just for make gcc's -Wswitch-enum happy. */
  897. assert(0);
  898. break;
  899. default:
  900. XSUM_log("%s:%lu: Error: Unknown error.\n", inFileName, lineNumber);
  901. break;
  902. case GetLine_exceedMaxLineLength:
  903. XSUM_log("%s:%lu: Error: Line too long.\n", inFileName, lineNumber);
  904. break;
  905. case GetLine_outOfMemory:
  906. XSUM_log("%s:%lu: Error: Out of memory.\n", inFileName, lineNumber);
  907. break;
  908. }
  909. report->quit = 1;
  910. break;
  911. } }
  912. if (XSUM_parseLine(&parsedLine, XSUM_parseFileArg->lineBuf, rev) != ParseLine_ok) {
  913. report->nImproperlyFormattedLines++;
  914. if (XSUM_parseFileArg->warn) {
  915. XSUM_log("%s:%lu: Error: Improperly formatted checksum line.\n",
  916. inFileName, lineNumber);
  917. }
  918. continue;
  919. }
  920. report->nProperlyFormattedLines++;
  921. do {
  922. FILE* const fp = XSUM_fopen(parsedLine.filename, "rb");
  923. if (fp == NULL) {
  924. lineStatus = LineStatus_failedToOpen;
  925. break;
  926. }
  927. lineStatus = LineStatus_hashFailed;
  928. switch (parsedLine.xxhBits)
  929. {
  930. case 32:
  931. { Multihash const xxh = XSUM_hashStream(fp, algo_xxh32, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
  932. if (xxh.xxh32 == XXH32_hashFromCanonical(&parsedLine.canonical.xxh32)) {
  933. lineStatus = LineStatus_hashOk;
  934. } }
  935. break;
  936. case 64:
  937. { Multihash const xxh = XSUM_hashStream(fp, algo_xxh64, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
  938. if (xxh.xxh64 == XXH64_hashFromCanonical(&parsedLine.canonical.xxh64)) {
  939. lineStatus = LineStatus_hashOk;
  940. } }
  941. break;
  942. case 128:
  943. { Multihash const xxh = XSUM_hashStream(fp, algo_xxh128, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
  944. if (XXH128_isEqual(xxh.xxh128, XXH128_hashFromCanonical(&parsedLine.canonical.xxh128))) {
  945. lineStatus = LineStatus_hashOk;
  946. } }
  947. break;
  948. default:
  949. break;
  950. }
  951. fclose(fp);
  952. } while (0);
  953. switch (lineStatus)
  954. {
  955. default:
  956. XSUM_log("%s: Error: Unknown error.\n", inFileName);
  957. report->quit = 1;
  958. break;
  959. case LineStatus_failedToOpen:
  960. report->nOpenOrReadFailures++;
  961. if (!XSUM_parseFileArg->statusOnly) {
  962. XSUM_output("%s:%lu: Could not open or read '%s': %s.\n",
  963. inFileName, lineNumber, parsedLine.filename, strerror(errno));
  964. }
  965. break;
  966. case LineStatus_hashOk:
  967. case LineStatus_hashFailed:
  968. { int b = 1;
  969. if (lineStatus == LineStatus_hashOk) {
  970. /* If --quiet is specified, don't display "OK" */
  971. if (XSUM_parseFileArg->quiet) b = 0;
  972. } else {
  973. report->nMismatchedChecksums++;
  974. }
  975. if (b && !XSUM_parseFileArg->statusOnly) {
  976. XSUM_output("%s: %s\n", parsedLine.filename
  977. , lineStatus == LineStatus_hashOk ? "OK" : "FAILED");
  978. } }
  979. break;
  980. }
  981. } /* while (!report->quit) */
  982. }
  983. /* Parse xxHash checksum file.
  984. * Returns 1, if all procedures were succeeded.
  985. * Returns 0, if any procedures was failed.
  986. *
  987. * If strictMode != 0, return error code if any line is invalid.
  988. * If statusOnly != 0, don't generate any output.
  989. * If warn != 0, print a warning message to stderr.
  990. * If quiet != 0, suppress "OK" line.
  991. *
  992. * "All procedures are succeeded" means:
  993. * - Checksum file contains at least one line and less than SIZE_T_MAX lines.
  994. * - All files are properly opened and read.
  995. * - All hash values match with its content.
  996. * - (strict mode) All lines in checksum file are consistent and well formatted.
  997. */
  998. static int XSUM_checkFile(const char* inFileName,
  999. const Display_endianess displayEndianess,
  1000. XSUM_U32 strictMode,
  1001. XSUM_U32 statusOnly,
  1002. XSUM_U32 warn,
  1003. XSUM_U32 quiet)
  1004. {
  1005. int result = 0;
  1006. FILE* inFile = NULL;
  1007. ParseFileArg XSUM_parseFileArgBody;
  1008. ParseFileArg* const XSUM_parseFileArg = &XSUM_parseFileArgBody;
  1009. ParseFileReport* const report = &XSUM_parseFileArg->report;
  1010. /* note: stdinName is special constant pointer. It is not a string. */
  1011. if (inFileName == stdinName) {
  1012. /*
  1013. * Note: Since we expect text input for xxhash -c mode,
  1014. * we don't set binary mode for stdin.
  1015. */
  1016. inFileName = "stdin";
  1017. inFile = stdin;
  1018. } else {
  1019. inFile = XSUM_fopen( inFileName, "rt" );
  1020. }
  1021. if (inFile == NULL) {
  1022. XSUM_log("Error: Could not open '%s': %s\n", inFileName, strerror(errno));
  1023. return 0;
  1024. }
  1025. XSUM_parseFileArg->inFileName = inFileName;
  1026. XSUM_parseFileArg->inFile = inFile;
  1027. XSUM_parseFileArg->lineMax = DEFAULT_LINE_LENGTH;
  1028. XSUM_parseFileArg->lineBuf = (char*) malloc((size_t)XSUM_parseFileArg->lineMax);
  1029. XSUM_parseFileArg->blockSize = 64 * 1024;
  1030. XSUM_parseFileArg->blockBuf = (char*) malloc(XSUM_parseFileArg->blockSize);
  1031. XSUM_parseFileArg->strictMode = strictMode;
  1032. XSUM_parseFileArg->statusOnly = statusOnly;
  1033. XSUM_parseFileArg->warn = warn;
  1034. XSUM_parseFileArg->quiet = quiet;
  1035. if ( (XSUM_parseFileArg->lineBuf == NULL)
  1036. || (XSUM_parseFileArg->blockBuf == NULL) ) {
  1037. XSUM_log("Error: : memory allocation failed \n");
  1038. exit(1);
  1039. }
  1040. XSUM_parseFile1(XSUM_parseFileArg, displayEndianess != big_endian);
  1041. free(XSUM_parseFileArg->blockBuf);
  1042. free(XSUM_parseFileArg->lineBuf);
  1043. if (inFile != stdin) fclose(inFile);
  1044. /* Show error/warning messages. All messages are copied from md5sum.c
  1045. */
  1046. if (report->nProperlyFormattedLines == 0) {
  1047. XSUM_log("%s: no properly formatted xxHash checksum lines found\n", inFileName);
  1048. } else if (!statusOnly) {
  1049. if (report->nImproperlyFormattedLines) {
  1050. XSUM_output("%lu %s improperly formatted\n"
  1051. , report->nImproperlyFormattedLines
  1052. , report->nImproperlyFormattedLines == 1 ? "line is" : "lines are");
  1053. }
  1054. if (report->nOpenOrReadFailures) {
  1055. XSUM_output("%lu listed %s could not be read\n"
  1056. , report->nOpenOrReadFailures
  1057. , report->nOpenOrReadFailures == 1 ? "file" : "files");
  1058. }
  1059. if (report->nMismatchedChecksums) {
  1060. XSUM_output("%lu computed %s did NOT match\n"
  1061. , report->nMismatchedChecksums
  1062. , report->nMismatchedChecksums == 1 ? "checksum" : "checksums");
  1063. } }
  1064. /* Result (exit) code logic is copied from
  1065. * gnu coreutils/src/md5sum.c digest_check() */
  1066. result = report->nProperlyFormattedLines != 0
  1067. && report->nMismatchedChecksums == 0
  1068. && report->nOpenOrReadFailures == 0
  1069. && (!strictMode || report->nImproperlyFormattedLines == 0)
  1070. && report->quit == 0;
  1071. return result;
  1072. }
  1073. static int XSUM_checkFiles(char*const* fnList, int fnTotal,
  1074. const Display_endianess displayEndianess,
  1075. XSUM_U32 strictMode,
  1076. XSUM_U32 statusOnly,
  1077. XSUM_U32 warn,
  1078. XSUM_U32 quiet)
  1079. {
  1080. int ok = 1;
  1081. /* Special case for stdinName "-",
  1082. * note: stdinName is not a string. It's special pointer. */
  1083. if (fnTotal==0) {
  1084. ok &= XSUM_checkFile(stdinName, displayEndianess, strictMode, statusOnly, warn, quiet);
  1085. } else {
  1086. int fnNb;
  1087. for (fnNb=0; fnNb<fnTotal; fnNb++)
  1088. ok &= XSUM_checkFile(fnList[fnNb], displayEndianess, strictMode, statusOnly, warn, quiet);
  1089. }
  1090. return ok ? 0 : 1;
  1091. }
  1092. /* ********************************************************
  1093. * Main
  1094. **********************************************************/
  1095. static int XSUM_usage(const char* exename)
  1096. {
  1097. XSUM_log( WELCOME_MESSAGE(exename) );
  1098. XSUM_log( "Print or verify checksums using fast non-cryptographic algorithm xxHash \n\n" );
  1099. XSUM_log( "Usage: %s [options] [files] \n\n", exename);
  1100. XSUM_log( "When no filename provided or when '-' is provided, uses stdin as input. \n");
  1101. XSUM_log( "Options: \n");
  1102. XSUM_log( " -H# algorithm selection: 0,1,2 or 32,64,128 (default: %i) \n", (int)g_defaultAlgo);
  1103. XSUM_log( " -c, --check read xxHash checksum from [files] and check them \n");
  1104. XSUM_log( " -h, --help display a long help page about advanced options \n");
  1105. return 0;
  1106. }
  1107. static int XSUM_usage_advanced(const char* exename)
  1108. {
  1109. XSUM_usage(exename);
  1110. XSUM_log( "Advanced :\n");
  1111. XSUM_log( " -V, --version Display version information \n");
  1112. XSUM_log( " --tag Produce BSD-style checksum lines \n");
  1113. XSUM_log( " --little-endian Checksum values use little endian convention (default: big endian) \n");
  1114. XSUM_log( " -b Run benchmark \n");
  1115. XSUM_log( " -b# Bench only algorithm variant # \n");
  1116. XSUM_log( " -i# Number of times to run the benchmark (default: %u) \n", (unsigned)g_nbIterations);
  1117. XSUM_log( " -q, --quiet Don't display version header in benchmark mode \n");
  1118. XSUM_log( "\n");
  1119. XSUM_log( "The following four options are useful only when verifying checksums (-c): \n");
  1120. XSUM_log( " -q, --quiet Don't print OK for each successfully verified file \n");
  1121. XSUM_log( " --status Don't output anything, status code shows success \n");
  1122. XSUM_log( " --strict Exit non-zero for improperly formatted checksum lines \n");
  1123. XSUM_log( " --warn Warn about improperly formatted checksum lines \n");
  1124. return 0;
  1125. }
  1126. static int XSUM_badusage(const char* exename)
  1127. {
  1128. XSUM_log("Wrong parameters\n\n");
  1129. XSUM_usage(exename);
  1130. return 1;
  1131. }
  1132. static void errorOut(const char* msg)
  1133. {
  1134. XSUM_log("%s \n", msg);
  1135. exit(1);
  1136. }
  1137. static const char* XSUM_lastNameFromPath(const char* path)
  1138. {
  1139. const char* name = path;
  1140. if (strrchr(name, '/')) name = strrchr(name, '/') + 1;
  1141. if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1; /* windows */
  1142. return name;
  1143. }
  1144. /*!
  1145. * XSUM_readU32FromCharChecked():
  1146. * @return 0 if success, and store the result in *value.
  1147. * Allows and interprets K, KB, KiB, M, MB and MiB suffix.
  1148. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
  1149. * @return 1 if an overflow error occurs
  1150. */
  1151. static int XSUM_readU32FromCharChecked(const char** stringPtr, XSUM_U32* value)
  1152. {
  1153. static const XSUM_U32 max = (((XSUM_U32)(-1)) / 10) - 1;
  1154. XSUM_U32 result = 0;
  1155. while ((**stringPtr >='0') && (**stringPtr <='9')) {
  1156. if (result > max) return 1; /* overflow error */
  1157. result *= 10;
  1158. result += (XSUM_U32)(**stringPtr - '0');
  1159. (*stringPtr)++ ;
  1160. }
  1161. if ((**stringPtr=='K') || (**stringPtr=='M')) {
  1162. XSUM_U32 const maxK = ((XSUM_U32)(-1)) >> 10;
  1163. if (result > maxK) return 1; /* overflow error */
  1164. result <<= 10;
  1165. if (**stringPtr=='M') {
  1166. if (result > maxK) return 1; /* overflow error */
  1167. result <<= 10;
  1168. }
  1169. (*stringPtr)++; /* skip `K` or `M` */
  1170. if (**stringPtr=='i') (*stringPtr)++;
  1171. if (**stringPtr=='B') (*stringPtr)++;
  1172. }
  1173. *value = result;
  1174. return 0;
  1175. }
  1176. /*!
  1177. * XSUM_readU32FromChar():
  1178. * @return: unsigned integer value read from input in `char` format.
  1179. * allows and interprets K, KB, KiB, M, MB and MiB suffix.
  1180. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
  1181. * Note: function will exit() program if digit sequence overflows
  1182. */
  1183. static XSUM_U32 XSUM_readU32FromChar(const char** stringPtr) {
  1184. XSUM_U32 result;
  1185. if (XSUM_readU32FromCharChecked(stringPtr, &result)) {
  1186. static const char errorMsg[] = "Error: numeric value too large";
  1187. errorOut(errorMsg);
  1188. }
  1189. return result;
  1190. }
  1191. XSUM_API int XSUM_main(int argc, char* argv[])
  1192. {
  1193. int i, filenamesStart = 0;
  1194. const char* const exename = XSUM_lastNameFromPath(argv[0]);
  1195. XSUM_U32 benchmarkMode = 0;
  1196. XSUM_U32 fileCheckMode = 0;
  1197. XSUM_U32 strictMode = 0;
  1198. XSUM_U32 statusOnly = 0;
  1199. XSUM_U32 warn = 0;
  1200. int explicitStdin = 0;
  1201. XSUM_U32 selectBenchIDs= 0; /* 0 == use default k_testIDs_default, kBenchAll == bench all */
  1202. static const XSUM_U32 kBenchAll = 99;
  1203. size_t keySize = XSUM_DEFAULT_SAMPLE_SIZE;
  1204. AlgoSelected algo = g_defaultAlgo;
  1205. Display_endianess displayEndianess = big_endian;
  1206. Display_convention convention = display_gnu;
  1207. /* special case: xxhNNsum default to NN bits checksum */
  1208. if (strstr(exename, "xxh32sum") != NULL) algo = g_defaultAlgo = algo_xxh32;
  1209. if (strstr(exename, "xxh64sum") != NULL) algo = g_defaultAlgo = algo_xxh64;
  1210. if (strstr(exename, "xxh128sum") != NULL) algo = g_defaultAlgo = algo_xxh128;
  1211. for (i=1; i<argc; i++) {
  1212. const char* argument = argv[i];
  1213. assert(argument != NULL);
  1214. if (!strcmp(argument, "--check")) { fileCheckMode = 1; continue; }
  1215. if (!strcmp(argument, "--benchmark-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
  1216. if (!strcmp(argument, "--bench-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
  1217. if (!strcmp(argument, "--quiet")) { XSUM_logLevel--; continue; }
  1218. if (!strcmp(argument, "--little-endian")) { displayEndianess = little_endian; continue; }
  1219. if (!strcmp(argument, "--strict")) { strictMode = 1; continue; }
  1220. if (!strcmp(argument, "--status")) { statusOnly = 1; continue; }
  1221. if (!strcmp(argument, "--warn")) { warn = 1; continue; }
  1222. if (!strcmp(argument, "--help")) { return XSUM_usage_advanced(exename); }
  1223. if (!strcmp(argument, "--version")) { XSUM_log(FULL_WELCOME_MESSAGE(exename)); XSUM_sanityCheck(); return 0; }
  1224. if (!strcmp(argument, "--tag")) { convention = display_bsd; continue; }
  1225. if (!strcmp(argument, "--")) {
  1226. if (filenamesStart==0 && i!=argc-1) filenamesStart=i+1; /* only supports a continuous list of filenames */
  1227. break; /* treat rest of arguments as strictly file names */
  1228. }
  1229. if (*argument != '-') {
  1230. if (filenamesStart==0) filenamesStart=i; /* only supports a continuous list of filenames */
  1231. break; /* treat rest of arguments as strictly file names */
  1232. }
  1233. /* command selection */
  1234. argument++; /* note: *argument=='-' */
  1235. if (*argument == 0) explicitStdin = 1;
  1236. while (*argument != 0) {
  1237. switch(*argument)
  1238. {
  1239. /* Display version */
  1240. case 'V':
  1241. XSUM_log(FULL_WELCOME_MESSAGE(exename)); return 0;
  1242. /* Display help on XSUM_usage */
  1243. case 'h':
  1244. return XSUM_usage_advanced(exename);
  1245. /* select hash algorithm */
  1246. case 'H': argument++;
  1247. switch(XSUM_readU32FromChar(&argument)) {
  1248. case 0 :
  1249. case 32: algo = algo_xxh32; break;
  1250. case 1 :
  1251. case 64: algo = algo_xxh64; break;
  1252. case 2 :
  1253. case 128: algo = algo_xxh128; break;
  1254. default:
  1255. return XSUM_badusage(exename);
  1256. }
  1257. break;
  1258. /* File check mode */
  1259. case 'c':
  1260. fileCheckMode=1;
  1261. argument++;
  1262. break;
  1263. /* Warning mode (file check mode only, alias of "--warning") */
  1264. case 'w':
  1265. warn=1;
  1266. argument++;
  1267. break;
  1268. /* Trigger benchmark mode */
  1269. case 'b':
  1270. argument++;
  1271. benchmarkMode = 1;
  1272. do {
  1273. if (*argument == ',') argument++;
  1274. selectBenchIDs = XSUM_readU32FromChar(&argument); /* select one specific test */
  1275. if (selectBenchIDs < NB_TESTFUNC) {
  1276. g_testIDs[selectBenchIDs] = 1;
  1277. } else
  1278. selectBenchIDs = kBenchAll;
  1279. } while (*argument == ',');
  1280. break;
  1281. /* Modify Nb Iterations (benchmark only) */
  1282. case 'i':
  1283. argument++;
  1284. g_nbIterations = XSUM_readU32FromChar(&argument);
  1285. break;
  1286. /* Modify Block size (benchmark only) */
  1287. case 'B':
  1288. argument++;
  1289. keySize = XSUM_readU32FromChar(&argument);
  1290. break;
  1291. /* Modify verbosity of benchmark output (hidden option) */
  1292. case 'q':
  1293. argument++;
  1294. XSUM_logLevel--;
  1295. break;
  1296. default:
  1297. return XSUM_badusage(exename);
  1298. }
  1299. }
  1300. } /* for(i=1; i<argc; i++) */
  1301. /* Check benchmark mode */
  1302. if (benchmarkMode) {
  1303. XSUM_logVerbose(2, FULL_WELCOME_MESSAGE(exename) );
  1304. XSUM_sanityCheck();
  1305. if (selectBenchIDs == 0) memcpy(g_testIDs, k_testIDs_default, sizeof(g_testIDs));
  1306. if (selectBenchIDs == kBenchAll) memset(g_testIDs, 1, sizeof(g_testIDs));
  1307. if (filenamesStart==0) return XSUM_benchInternal(keySize);
  1308. return XSUM_benchFiles(argv+filenamesStart, argc-filenamesStart);
  1309. }
  1310. /* Check if input is defined as console; trigger an error in this case */
  1311. if ( (filenamesStart==0) && XSUM_isConsole(stdin) && !explicitStdin)
  1312. return XSUM_badusage(exename);
  1313. if (filenamesStart==0) filenamesStart = argc;
  1314. if (fileCheckMode) {
  1315. return XSUM_checkFiles(argv+filenamesStart, argc-filenamesStart,
  1316. displayEndianess, strictMode, statusOnly, warn, (XSUM_logLevel < 2) /*quiet*/);
  1317. } else {
  1318. return XSUM_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, displayEndianess, convention);
  1319. }
  1320. }