xxhsum.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /*
  2. * xxhsum - Command line interface for xxhash algorithms
  3. * Copyright (C) 2013-2021 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 "xsum_arch.h" /* XSUM_PROGRAM_VERSION */
  32. #include "xsum_os_specific.h" /* XSUM_setBinaryMode */
  33. #include "xsum_output.h" /* XSUM_output */
  34. #include "xsum_sanity_check.h" /* XSUM_sanityCheck */
  35. #include "xsum_bench.h" /* NBLOOPS_DEFAULT */
  36. #ifdef XXH_INLINE_ALL
  37. # include "xsum_os_specific.c"
  38. # include "xsum_output.c"
  39. # include "xsum_sanity_check.c"
  40. # include "xsum_bench.c"
  41. #endif
  42. /* ************************************
  43. * Includes
  44. **************************************/
  45. #include <stdlib.h> /* malloc, calloc, free, exit */
  46. #include <string.h> /* strerror, strcmp, memcpy */
  47. #include <assert.h> /* assert */
  48. #include <errno.h> /* errno */
  49. #define XXH_STATIC_LINKING_ONLY /* *_state_t */
  50. #include "../xxhash.h"
  51. #ifdef XXHSUM_DISPATCH
  52. # include "../xxh_x86dispatch.h"
  53. #endif
  54. static unsigned XSUM_isLittleEndian(void)
  55. {
  56. const union { XSUM_U32 u; XSUM_U8 c[4]; } one = { 1 }; /* don't use static: performance detrimental */
  57. return one.c[0];
  58. }
  59. static const int g_nbBits = (int)(sizeof(void*)*8);
  60. static const char g_lename[] = "little endian";
  61. static const char g_bename[] = "big endian";
  62. #define ENDIAN_NAME (XSUM_isLittleEndian() ? g_lename : g_bename)
  63. static const char author[] = "Yann Collet";
  64. #define WELCOME_MESSAGE(exename) "%s %s by %s \n", exename, XSUM_PROGRAM_VERSION, author
  65. #define FULL_WELCOME_MESSAGE(exename) "%s %s by %s \n" \
  66. "compiled as %i-bit %s %s with " XSUM_CC_VERSION_FMT " \n", \
  67. exename, XSUM_PROGRAM_VERSION, author, \
  68. g_nbBits, XSUM_ARCH, ENDIAN_NAME, XSUM_CC_VERSION
  69. static const char stdinName[] = "-";
  70. static const char stdinFileName[] = "stdin";
  71. typedef enum { algo_xxh32=0, algo_xxh64=1, algo_xxh128=2, algo_xxh3=3 } AlgoSelected;
  72. static AlgoSelected g_defaultAlgo = algo_xxh64; /* required within main() & XSUM_usage() */
  73. typedef enum {
  74. algo_bitmask_xxh32 = 1 << algo_xxh32, /* 1 << 0 */
  75. algo_bitmask_xxh64 = 1 << algo_xxh64, /* 1 << 1 */
  76. algo_bitmask_xxh128 = 1 << algo_xxh128, /* 1 << 2 */
  77. algo_bitmask_xxh3 = 1 << algo_xxh3, /* 1 << 3 */
  78. algo_bitmask_all = algo_bitmask_xxh32 /* All algorithms */
  79. | algo_bitmask_xxh64
  80. | algo_bitmask_xxh128
  81. | algo_bitmask_xxh3
  82. } AlgoBitmask;
  83. /* <16 hex char> <SPC> <SPC> <filename> <'\0'>
  84. * '4096' is typical Linux PATH_MAX configuration. */
  85. #define DEFAULT_LINE_LENGTH (sizeof(XXH64_hash_t) * 2 + 2 + 4096 + 1)
  86. /* Maximum acceptable line length. */
  87. #define MAX_LINE_LENGTH (32 KB)
  88. static size_t XSUM_DEFAULT_SAMPLE_SIZE = 100 KB;
  89. /* ********************************************************
  90. * Filename (un)escaping
  91. **********************************************************/
  92. static int XSUM_filenameNeedsEscape(const char* filename) {
  93. return strchr(filename, '\\')
  94. || strchr(filename, '\n')
  95. || strchr(filename, '\r');
  96. }
  97. static int XSUM_lineNeedsUnescape(const char* line) {
  98. /* Skip white-space characters */
  99. while (*line == ' ' || *line == '\t') {
  100. ++line;
  101. }
  102. /* Returns true if first non-white-space character is '\\' (0x5c) */
  103. return *line == '\\';
  104. }
  105. static void XSUM_printFilename(const char* filename, int needsEscape) {
  106. if (!needsEscape) {
  107. XSUM_output("%s", filename);
  108. } else {
  109. const char* p;
  110. for (p = filename; *p != '\0'; ++p) {
  111. switch (*p)
  112. {
  113. case '\n':
  114. XSUM_output("\\n");
  115. break;
  116. case '\r':
  117. XSUM_output("\\r");
  118. break;
  119. case '\\':
  120. XSUM_output("\\\\");
  121. break;
  122. default:
  123. XSUM_output("%c", *p);
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. /* Unescape filename in place.
  130. - Replace '\\', 'n' (0x5c, 0x6e) with '\n' (0x0a).
  131. - Replace '\\', 'r' (0x5c, 0x72) with '\r' (0x0d).
  132. - Replace '\\', '\\' (0x5c, 0x5c) with '\\' (0x5c).
  133. - filename may not contain other backslash sequences.
  134. - filename may not ends with backslash.
  135. - filename may not contain NUL (0x00).
  136. Return filename if everything is okay.
  137. Return NULL if something wrong.
  138. */
  139. static char* XSUM_filenameUnescape(char* filename, size_t filenameLen) {
  140. char *p = filename;
  141. size_t i;
  142. for (i = 0; i < filenameLen; ++i) {
  143. switch (filename[i])
  144. {
  145. case '\\':
  146. ++i;
  147. if (i == filenameLen) {
  148. return NULL; /* Don't accept '\\', <EOL> */
  149. }
  150. switch (filename[i])
  151. {
  152. case 'n':
  153. *p++ = '\n';
  154. break;
  155. case 'r':
  156. *p++ = '\r';
  157. break;
  158. case '\\':
  159. *p++ = '\\';
  160. break;
  161. default:
  162. return NULL; /* Don't accept any other backslash sequence */
  163. }
  164. break;
  165. case '\0':
  166. return NULL; /* Don't accept NUL (0x00) */
  167. default:
  168. *p++ = filename[i];
  169. break;
  170. }
  171. }
  172. if (p < filename + filenameLen) {
  173. *p = '\0';
  174. }
  175. return filename;
  176. }
  177. /* ********************************************************
  178. * Algorithm Bitmask
  179. **********************************************************/
  180. /* Compute AlgoBitmask (as a U32) from AlgoSelected */
  181. static XSUM_U32 XSUM_algoBitmask_ComputeAlgoBitmaskFromAlgoSelected(AlgoSelected algoSelected) {
  182. return (XSUM_U32) (1U << algoSelected);
  183. }
  184. /* Returns true (!0) if algoBitmask contains (accepts) parsedLineAlgo */
  185. static int XSUM_algoBitmask_Accepts(XSUM_U32 algoBitmask, AlgoSelected parsedLineAlgo) {
  186. const XSUM_U32 mask = XSUM_algoBitmask_ComputeAlgoBitmaskFromAlgoSelected(parsedLineAlgo);
  187. return (algoBitmask & mask) != 0;
  188. }
  189. /* ********************************************************
  190. * File Hashing
  191. **********************************************************/
  192. #define XXHSUM32_DEFAULT_SEED 0 /* Default seed for algo_xxh32 */
  193. #define XXHSUM64_DEFAULT_SEED 0 /* Default seed for algo_xxh64 */
  194. /* for support of --little-endian display mode */
  195. static void XSUM_display_LittleEndian(const void* ptr, size_t length)
  196. {
  197. const XSUM_U8* const p = (const XSUM_U8*)ptr;
  198. size_t idx;
  199. for (idx=length-1; idx<length; idx--) /* intentional underflow to negative to detect end */
  200. XSUM_output("%02x", p[idx]);
  201. }
  202. static void XSUM_display_BigEndian(const void* ptr, size_t length)
  203. {
  204. const XSUM_U8* const p = (const XSUM_U8*)ptr;
  205. size_t idx;
  206. for (idx=0; idx<length; idx++)
  207. XSUM_output("%02x", p[idx]);
  208. }
  209. typedef union {
  210. XXH32_hash_t hash32;
  211. XXH64_hash_t hash64; /* also for xxh3_64bits */
  212. XXH128_hash_t hash128;
  213. } Multihash;
  214. /*
  215. * XSUM_hashStream:
  216. * Reads data from `inFile`, generating an incremental hash of type hashType,
  217. * using `buffer` of size `blockSize` for temporary storage.
  218. */
  219. static Multihash
  220. XSUM_hashStream(FILE* inFile,
  221. AlgoSelected hashType,
  222. void* buffer, size_t blockSize)
  223. {
  224. XXH32_state_t state32;
  225. XXH64_state_t state64;
  226. XXH3_state_t state3;
  227. /* Init */
  228. (void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
  229. (void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
  230. (void)XXH3_128bits_reset(&state3);
  231. /* Load file & update hash */
  232. { size_t readSize;
  233. while ((readSize = fread(buffer, 1, blockSize, inFile)) > 0) {
  234. switch(hashType)
  235. {
  236. case algo_xxh32:
  237. (void)XXH32_update(&state32, buffer, readSize);
  238. break;
  239. case algo_xxh64:
  240. (void)XXH64_update(&state64, buffer, readSize);
  241. break;
  242. case algo_xxh128:
  243. (void)XXH3_128bits_update(&state3, buffer, readSize);
  244. break;
  245. case algo_xxh3:
  246. (void)XXH3_64bits_update(&state3, buffer, readSize);
  247. break;
  248. default:
  249. assert(0);
  250. }
  251. }
  252. if (ferror(inFile)) {
  253. XSUM_log("Error: a failure occurred reading the input file.\n");
  254. exit(1);
  255. } }
  256. { Multihash finalHash = {0};
  257. switch(hashType)
  258. {
  259. case algo_xxh32:
  260. finalHash.hash32 = XXH32_digest(&state32);
  261. break;
  262. case algo_xxh64:
  263. finalHash.hash64 = XXH64_digest(&state64);
  264. break;
  265. case algo_xxh128:
  266. finalHash.hash128 = XXH3_128bits_digest(&state3);
  267. break;
  268. case algo_xxh3:
  269. finalHash.hash64 = XXH3_64bits_digest(&state3);
  270. break;
  271. default:
  272. assert(0);
  273. }
  274. return finalHash;
  275. }
  276. }
  277. /* algo_xxh32, algo_xxh64, algo_xxh128 */
  278. static const char* XSUM_algoName[] = { "XXH32", "XXH64", "XXH128", "XXH3" };
  279. static const char* XSUM_algoLE_name[] = { "XXH32_LE", "XXH64_LE", "XXH128_LE", "XXH3_LE" };
  280. static const size_t XSUM_algoLength[] = { 4, 8, 16, 8 };
  281. #define XSUM_TABLE_ELT_SIZE(table) (sizeof(table) / sizeof(*table))
  282. typedef void (*XSUM_displayHash_f)(const void*, size_t); /* display function signature */
  283. static void XSUM_printLine_BSD_internal(const char* filename,
  284. const void* canonicalHash, const AlgoSelected hashType,
  285. const char* algoString[],
  286. XSUM_displayHash_f f_displayHash)
  287. {
  288. assert(0 <= hashType && (size_t)hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName));
  289. { const char* const typeString = algoString[hashType];
  290. const size_t hashLength = XSUM_algoLength[hashType];
  291. const int needsEscape = XSUM_filenameNeedsEscape(filename);
  292. if (needsEscape) {
  293. XSUM_output("%c", '\\');
  294. }
  295. XSUM_output("%s (", typeString);
  296. XSUM_printFilename(filename, needsEscape);
  297. XSUM_output(") = ");
  298. f_displayHash(canonicalHash, hashLength);
  299. XSUM_output("\n");
  300. } }
  301. static void XSUM_printLine_BSD_LE(const char* filename, const void* canonicalHash, const AlgoSelected hashType)
  302. {
  303. XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoLE_name, XSUM_display_LittleEndian);
  304. }
  305. static void XSUM_printLine_BSD(const char* filename, const void* canonicalHash, const AlgoSelected hashType)
  306. {
  307. XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoName, XSUM_display_BigEndian);
  308. }
  309. static void XSUM_printLine_GNU_internal(const char* filename,
  310. const void* canonicalHash, const AlgoSelected hashType,
  311. XSUM_displayHash_f f_displayHash)
  312. {
  313. assert(0 <= hashType && (size_t)hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName));
  314. { const size_t hashLength = XSUM_algoLength[hashType];
  315. const int needsEscape = XSUM_filenameNeedsEscape(filename);
  316. if (needsEscape) {
  317. XSUM_output("%c", '\\');
  318. }
  319. f_displayHash(canonicalHash, hashLength);
  320. XSUM_output(" ");
  321. XSUM_printFilename(filename, needsEscape);
  322. XSUM_output("\n");
  323. } }
  324. static void XSUM_printLine_GNU(const char* filename,
  325. const void* canonicalHash, const AlgoSelected hashType)
  326. {
  327. XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_BigEndian);
  328. }
  329. static void XSUM_printLine_GNU_LE(const char* filename,
  330. const void* canonicalHash, const AlgoSelected hashType)
  331. {
  332. XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_LittleEndian);
  333. }
  334. typedef enum { big_endian, little_endian} Display_endianess;
  335. typedef enum { display_gnu, display_bsd } Display_convention;
  336. typedef void (*XSUM_displayLine_f)(const char*, const void*, AlgoSelected); /* line display signature */
  337. static XSUM_displayLine_f XSUM_kDisplayLine_fTable[2][2] = {
  338. { XSUM_printLine_GNU, XSUM_printLine_GNU_LE },
  339. { XSUM_printLine_BSD, XSUM_printLine_BSD_LE }
  340. };
  341. static int XSUM_hashFile(const char* fileName,
  342. const AlgoSelected hashType,
  343. const Display_endianess displayEndianess,
  344. const Display_convention convention)
  345. {
  346. size_t const blockSize = 64 KB;
  347. XSUM_displayLine_f const f_displayLine = XSUM_kDisplayLine_fTable[convention][displayEndianess];
  348. FILE* inFile;
  349. Multihash hashValue;
  350. assert(displayEndianess==big_endian || displayEndianess==little_endian);
  351. assert(convention==display_gnu || convention==display_bsd);
  352. /* Check file existence */
  353. if (fileName == stdinName) {
  354. inFile = stdin;
  355. fileName = stdinFileName; /* "stdin" */
  356. XSUM_setBinaryMode(stdin);
  357. } else {
  358. if (XSUM_isDirectory(fileName)) {
  359. XSUM_log("xxhsum: %s: Is a directory \n", fileName);
  360. return 1;
  361. }
  362. inFile = XSUM_fopen( fileName, "rb" );
  363. if (inFile==NULL) {
  364. XSUM_log("Error: Could not open '%s': %s. \n", fileName, strerror(errno));
  365. return 1;
  366. } }
  367. /* Memory allocation & streaming */
  368. { void* const buffer = malloc(blockSize);
  369. if (buffer == NULL) {
  370. XSUM_log("\nError: Out of memory.\n");
  371. fclose(inFile);
  372. return 1;
  373. }
  374. /* Stream file & update hash */
  375. hashValue = XSUM_hashStream(inFile, hashType, buffer, blockSize);
  376. fclose(inFile);
  377. free(buffer);
  378. }
  379. /* display Hash value in selected format */
  380. switch(hashType)
  381. {
  382. case algo_xxh32:
  383. { XXH32_canonical_t hcbe32;
  384. (void)XXH32_canonicalFromHash(&hcbe32, hashValue.hash32);
  385. f_displayLine(fileName, &hcbe32, hashType);
  386. break;
  387. }
  388. case algo_xxh64:
  389. { XXH64_canonical_t hcbe64;
  390. (void)XXH64_canonicalFromHash(&hcbe64, hashValue.hash64);
  391. f_displayLine(fileName, &hcbe64, hashType);
  392. break;
  393. }
  394. case algo_xxh128:
  395. { XXH128_canonical_t hcbe128;
  396. (void)XXH128_canonicalFromHash(&hcbe128, hashValue.hash128);
  397. f_displayLine(fileName, &hcbe128, hashType);
  398. break;
  399. }
  400. case algo_xxh3:
  401. { XXH64_canonical_t hcbe64;
  402. (void)XXH64_canonicalFromHash(&hcbe64, hashValue.hash64);
  403. f_displayLine(fileName, &hcbe64, hashType);
  404. break;
  405. }
  406. default:
  407. assert(0); /* not possible */
  408. }
  409. return 0;
  410. }
  411. /*
  412. * XSUM_hashFiles:
  413. * If fnTotal==0, read from stdin instead.
  414. */
  415. static int XSUM_hashFiles(const char* fnList[], int fnTotal,
  416. AlgoSelected hashType,
  417. Display_endianess displayEndianess,
  418. Display_convention convention)
  419. {
  420. int fnNb;
  421. int result = 0;
  422. if (fnTotal==0)
  423. return XSUM_hashFile(stdinName, hashType, displayEndianess, convention);
  424. for (fnNb=0; fnNb<fnTotal; fnNb++)
  425. result |= XSUM_hashFile(fnList[fnNb], hashType, displayEndianess, convention);
  426. XSUM_logVerbose(2, "\r%70s\r", "");
  427. return result;
  428. }
  429. typedef enum {
  430. GetLine_ok,
  431. GetLine_comment,
  432. GetLine_eof,
  433. GetLine_exceedMaxLineLength,
  434. GetLine_outOfMemory
  435. } GetLineResult;
  436. typedef enum {
  437. CanonicalFromString_ok,
  438. CanonicalFromString_invalidFormat
  439. } CanonicalFromStringResult;
  440. typedef enum {
  441. ParseLine_ok,
  442. ParseLine_invalidFormat
  443. } ParseLineResult;
  444. typedef enum {
  445. LineStatus_hashOk,
  446. LineStatus_hashFailed,
  447. LineStatus_failedToOpen
  448. } LineStatus;
  449. typedef union {
  450. XXH32_canonical_t xxh32;
  451. XXH64_canonical_t xxh64;
  452. XXH128_canonical_t xxh128;
  453. } Canonical;
  454. typedef struct {
  455. Canonical canonical;
  456. const char* filename;
  457. AlgoSelected algo;
  458. } ParsedLine;
  459. typedef struct {
  460. unsigned long nProperlyFormattedLines;
  461. unsigned long nImproperlyFormattedLines;
  462. unsigned long nMismatchedChecksums;
  463. unsigned long nMatchedChecksums;
  464. unsigned long nOpenOrReadFailures;
  465. unsigned long nMixedFormatLines;
  466. unsigned long nMissing;
  467. int quit;
  468. } ParseFileReport;
  469. typedef struct {
  470. const char* inFileName;
  471. FILE* inFile;
  472. int lineMax;
  473. char* lineBuf;
  474. size_t blockSize;
  475. char* blockBuf;
  476. XSUM_U32 strictMode;
  477. XSUM_U32 statusOnly;
  478. XSUM_U32 ignoreMissing;
  479. XSUM_U32 warn;
  480. XSUM_U32 quiet;
  481. XSUM_U32 algoBitmask;
  482. ParseFileReport report;
  483. } ParseFileArg;
  484. /*
  485. * Reads a line from stream `inFile`.
  486. * Returns GetLine_ok, if it reads line successfully.
  487. * Returns GetLine_comment, if the line is beginning with '#'.
  488. * Returns GetLine_eof, if stream reaches EOF.
  489. * Returns GetLine_exceedMaxLineLength, if line length is longer than MAX_LINE_LENGTH.
  490. * Returns GetLine_outOfMemory, if line buffer memory allocation failed.
  491. */
  492. static GetLineResult XSUM_getLine(char** lineBuf, int* lineMax, FILE* inFile)
  493. {
  494. GetLineResult result = GetLine_ok;
  495. size_t len = 0;
  496. if ((*lineBuf == NULL) || (*lineMax<1)) {
  497. free(*lineBuf); /* in case it's != NULL */
  498. *lineMax = 0;
  499. *lineBuf = (char*)malloc(DEFAULT_LINE_LENGTH);
  500. if(*lineBuf == NULL) return GetLine_outOfMemory;
  501. *lineMax = DEFAULT_LINE_LENGTH;
  502. }
  503. for (;;) {
  504. const int c = fgetc(inFile);
  505. if (c == EOF) {
  506. /*
  507. * If we meet EOF before first character, returns GetLine_eof,
  508. * otherwise GetLine_ok.
  509. */
  510. if (len == 0) result = GetLine_eof;
  511. break;
  512. }
  513. /* Make enough space for len+1 (for final NUL) bytes. */
  514. if (len+1 >= (size_t)*lineMax) {
  515. char* newLineBuf = NULL;
  516. size_t newBufSize = (size_t)*lineMax;
  517. newBufSize += (newBufSize/2) + 1; /* x 1.5 */
  518. if (newBufSize > MAX_LINE_LENGTH) newBufSize = MAX_LINE_LENGTH;
  519. if (len+1 >= newBufSize) return GetLine_exceedMaxLineLength;
  520. newLineBuf = (char*) realloc(*lineBuf, newBufSize);
  521. if (newLineBuf == NULL) return GetLine_outOfMemory;
  522. *lineBuf = newLineBuf;
  523. *lineMax = (int)newBufSize;
  524. }
  525. if (c == '\n') break;
  526. (*lineBuf)[len++] = (char) c;
  527. }
  528. (*lineBuf)[len] = '\0';
  529. /* Ignore comment lines, which begin with a '#' character. */
  530. if (result == GetLine_ok && len > 0 && ((*lineBuf)[0] == '#')) {
  531. result = GetLine_comment;
  532. }
  533. return result;
  534. }
  535. /*
  536. * Converts one hexadecimal character to integer.
  537. * Returns -1 if the given character is not hexadecimal.
  538. */
  539. static int charToHex(char c)
  540. {
  541. int result = -1;
  542. if (c >= '0' && c <= '9') {
  543. result = (int) (c - '0');
  544. } else if (c >= 'A' && c <= 'F') {
  545. result = (int) (c - 'A') + 0x0a;
  546. } else if (c >= 'a' && c <= 'f') {
  547. result = (int) (c - 'a') + 0x0a;
  548. }
  549. return result;
  550. }
  551. /*
  552. * Converts canonical ASCII hexadecimal string `hashStr`
  553. * to the big endian binary representation in unsigned char array `dst`.
  554. *
  555. * Returns CanonicalFromString_invalidFormat if hashStr is not well formatted.
  556. * Returns CanonicalFromString_ok if hashStr is parsed successfully.
  557. */
  558. static CanonicalFromStringResult XSUM_canonicalFromString(unsigned char* dst,
  559. size_t dstSize,
  560. const char* hashStr,
  561. int reverseBytes)
  562. {
  563. size_t i;
  564. for (i = 0; i < dstSize; ++i) {
  565. int h0, h1;
  566. size_t j = reverseBytes ? dstSize - i - 1 : i;
  567. h0 = charToHex(hashStr[j*2 + 0]);
  568. if (h0 < 0) return CanonicalFromString_invalidFormat;
  569. h1 = charToHex(hashStr[j*2 + 1]);
  570. if (h1 < 0) return CanonicalFromString_invalidFormat;
  571. dst[i] = (unsigned char) ((h0 << 4) | h1);
  572. }
  573. return CanonicalFromString_ok;
  574. }
  575. /*
  576. * Parse single line of xxHash checksum file.
  577. * Returns ParseLine_invalidFormat if the line is not well formatted.
  578. * Returns ParseLine_ok if the line is parsed successfully.
  579. * And members of XSUM_parseLine will be filled by parsed values.
  580. *
  581. * - line must be terminated with '\0' without a trailing newline.
  582. * - Since parsedLine.filename will point within given argument `line`,
  583. * users must keep `line`s content when they are using parsedLine.
  584. * - The line may be modified to carve up the information it contains.
  585. *
  586. * xxHash checksum lines should have the following format:
  587. *
  588. * <8, 16, or 32 hexadecimal char> <space> <space> <filename...> <'\0'>
  589. *
  590. * or:
  591. *
  592. * <algorithm> <' ('> <filename> <') = '> <hexstring> <'\0'>
  593. */
  594. static ParseLineResult XSUM_parseLine1(ParsedLine* parsedLine, char* line, int rev, int needsUnescape, XSUM_U32 algoBitmask)
  595. {
  596. char* const firstSpace = strchr(line, ' ');
  597. const char* hash_ptr;
  598. size_t hash_len;
  599. parsedLine->filename = NULL;
  600. parsedLine->algo = algo_xxh64; /* default - will be overwritten */
  601. if (firstSpace == NULL || !firstSpace[1]) return ParseLine_invalidFormat;
  602. if (firstSpace[1] == '(') {
  603. char* lastSpace = strrchr(line, ' ');
  604. if (lastSpace - firstSpace < 5) return ParseLine_invalidFormat;
  605. if (lastSpace[-1] != '=' || lastSpace[-2] != ' ' || lastSpace[-3] != ')') return ParseLine_invalidFormat;
  606. lastSpace[-3] = '\0'; /* Terminate the filename */
  607. *firstSpace = '\0';
  608. rev = strstr(line, "_LE") != NULL; /* was output little-endian */
  609. hash_ptr = lastSpace + 1;
  610. hash_len = strlen(hash_ptr);
  611. if (!memcmp(line, "XXH3", 4)) parsedLine->algo = algo_xxh3;
  612. if (!memcmp(line, "XXH32", 5)) parsedLine->algo = algo_xxh32;
  613. if (!memcmp(line, "XXH64", 5)) parsedLine->algo = algo_xxh64;
  614. if (!memcmp(line, "XXH128", 6)) parsedLine->algo = algo_xxh128;
  615. } else {
  616. hash_ptr = line;
  617. hash_len = (size_t)(firstSpace - line);
  618. if (hash_len==8) parsedLine->algo = algo_xxh32;
  619. if (hash_len==16) parsedLine->algo = algo_xxh64;
  620. if (hash_len==32) parsedLine->algo = algo_xxh128;
  621. }
  622. /* Check current CLI accepts the algorithm or not */
  623. if(! XSUM_algoBitmask_Accepts(algoBitmask, parsedLine->algo)) {
  624. return ParseLine_invalidFormat;
  625. }
  626. switch (hash_len)
  627. {
  628. case 8:
  629. if (parsedLine->algo != algo_xxh32) return ParseLine_invalidFormat;
  630. { XXH32_canonical_t* xxh32c = &parsedLine->canonical.xxh32;
  631. if (XSUM_canonicalFromString(xxh32c->digest, sizeof(xxh32c->digest), hash_ptr, rev)
  632. != CanonicalFromString_ok) {
  633. return ParseLine_invalidFormat;
  634. }
  635. break;
  636. }
  637. case 16:
  638. if (parsedLine->algo != algo_xxh64 && parsedLine->algo != algo_xxh3) return ParseLine_invalidFormat;
  639. { XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64;
  640. if (XSUM_canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), hash_ptr, rev)
  641. != CanonicalFromString_ok) {
  642. return ParseLine_invalidFormat;
  643. }
  644. break;
  645. }
  646. case 32:
  647. if (parsedLine->algo != algo_xxh128) return ParseLine_invalidFormat;
  648. { XXH128_canonical_t* xxh128c = &parsedLine->canonical.xxh128;
  649. if (XSUM_canonicalFromString(xxh128c->digest, sizeof(xxh128c->digest), hash_ptr, rev)
  650. != CanonicalFromString_ok) {
  651. return ParseLine_invalidFormat;
  652. }
  653. break;
  654. }
  655. default:
  656. return ParseLine_invalidFormat;
  657. break;
  658. }
  659. /* note : skipping second separation character, which can be anything,
  660. * allowing insertion of custom markers such as '*' */
  661. {
  662. char* const filename = firstSpace + 2;
  663. const size_t filenameLen = strlen(filename);
  664. if (needsUnescape) {
  665. char* const result = XSUM_filenameUnescape(filename, filenameLen);
  666. if (result == NULL) {
  667. return ParseLine_invalidFormat;
  668. }
  669. }
  670. parsedLine->filename = filename;
  671. }
  672. return ParseLine_ok;
  673. }
  674. static ParseLineResult XSUM_parseLine(ParsedLine* parsedLine, char* line, int rev, XSUM_U32 algoBitmask) {
  675. const int needsUnescape = XSUM_lineNeedsUnescape(line);
  676. if (needsUnescape) {
  677. ++line;
  678. }
  679. return XSUM_parseLine1(parsedLine, line, rev, needsUnescape, algoBitmask);
  680. }
  681. /*!
  682. * Parse xxHash checksum file.
  683. */
  684. static void XSUM_parseFile1(ParseFileArg* XSUM_parseFileArg, int rev)
  685. {
  686. const char* const inFileName = XSUM_parseFileArg->inFileName;
  687. ParseFileReport* const report = &XSUM_parseFileArg->report;
  688. unsigned long lineNumber = 0;
  689. memset(report, 0, sizeof(*report));
  690. while (!report->quit) {
  691. LineStatus lineStatus = LineStatus_hashFailed;
  692. ParsedLine parsedLine;
  693. memset(&parsedLine, 0, sizeof(parsedLine));
  694. lineNumber++;
  695. if (lineNumber == 0) {
  696. /* This is unlikely happen, but md5sum.c has this error check. */
  697. XSUM_log("%s: Error: Too many checksum lines\n", inFileName);
  698. report->quit = 1;
  699. break;
  700. }
  701. { GetLineResult const XSUM_getLineResult = XSUM_getLine(&XSUM_parseFileArg->lineBuf,
  702. &XSUM_parseFileArg->lineMax,
  703. XSUM_parseFileArg->inFile);
  704. /* Ignore comment lines */
  705. if (XSUM_getLineResult == GetLine_comment) {
  706. continue;
  707. }
  708. if (XSUM_getLineResult != GetLine_ok) {
  709. if (XSUM_getLineResult == GetLine_eof) break;
  710. switch (XSUM_getLineResult)
  711. {
  712. case GetLine_ok:
  713. case GetLine_comment:
  714. case GetLine_eof:
  715. /* These cases never happen. See above XSUM_getLineResult related "if"s.
  716. They exist just for make gcc's -Wswitch-enum happy. */
  717. assert(0);
  718. break;
  719. default:
  720. XSUM_log("%s:%lu: Error: Unknown error.\n", inFileName, lineNumber);
  721. break;
  722. case GetLine_exceedMaxLineLength:
  723. XSUM_log("%s:%lu: Error: Line too long.\n", inFileName, lineNumber);
  724. break;
  725. case GetLine_outOfMemory:
  726. XSUM_log("%s:%lu: Error: Out of memory.\n", inFileName, lineNumber);
  727. break;
  728. }
  729. report->quit = 1;
  730. break;
  731. } }
  732. if (XSUM_parseLine(&parsedLine, XSUM_parseFileArg->lineBuf, rev, XSUM_parseFileArg->algoBitmask) != ParseLine_ok) {
  733. report->nImproperlyFormattedLines++;
  734. if (XSUM_parseFileArg->warn) {
  735. XSUM_log("%s:%lu: Error: Improperly formatted checksum line.\n",
  736. inFileName, lineNumber);
  737. }
  738. continue;
  739. }
  740. report->nProperlyFormattedLines++;
  741. do {
  742. int const fnameIsStdin = (strcmp(parsedLine.filename, stdinFileName) == 0); /* "stdin" */
  743. FILE* const fp = fnameIsStdin ? stdin : XSUM_fopen(parsedLine.filename, "rb");
  744. if (fp == stdin) {
  745. XSUM_setBinaryMode(stdin);
  746. }
  747. if (fp == NULL) {
  748. lineStatus = LineStatus_failedToOpen;
  749. break;
  750. }
  751. lineStatus = LineStatus_hashFailed;
  752. { Multihash const xxh = XSUM_hashStream(fp, parsedLine.algo, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize);
  753. switch (parsedLine.algo)
  754. {
  755. case algo_xxh32:
  756. if (xxh.hash32 == XXH32_hashFromCanonical(&parsedLine.canonical.xxh32)) {
  757. lineStatus = LineStatus_hashOk;
  758. }
  759. break;
  760. case algo_xxh64:
  761. case algo_xxh3:
  762. if (xxh.hash64 == XXH64_hashFromCanonical(&parsedLine.canonical.xxh64)) {
  763. lineStatus = LineStatus_hashOk;
  764. }
  765. break;
  766. case algo_xxh128:
  767. if (XXH128_isEqual(xxh.hash128, XXH128_hashFromCanonical(&parsedLine.canonical.xxh128))) {
  768. lineStatus = LineStatus_hashOk;
  769. }
  770. break;
  771. default:
  772. break;
  773. }
  774. }
  775. if (fp != stdin) fclose(fp);
  776. } while (0);
  777. switch (lineStatus)
  778. {
  779. default:
  780. XSUM_log("%s: Error: Unknown error.\n", inFileName);
  781. report->quit = 1;
  782. break;
  783. case LineStatus_failedToOpen:
  784. if (XSUM_parseFileArg->ignoreMissing) {
  785. report->nMissing++;
  786. } else {
  787. report->nOpenOrReadFailures++;
  788. if (!XSUM_parseFileArg->statusOnly) {
  789. XSUM_output("%s:%lu: Could not open or read '%s': %s.\n",
  790. inFileName, lineNumber, parsedLine.filename, strerror(errno));
  791. }
  792. }
  793. break;
  794. case LineStatus_hashOk:
  795. case LineStatus_hashFailed:
  796. { int b = 1;
  797. if (lineStatus == LineStatus_hashOk) {
  798. report->nMatchedChecksums++;
  799. /* If --quiet is specified, don't display "OK" */
  800. if (XSUM_parseFileArg->quiet) b = 0;
  801. } else {
  802. report->nMismatchedChecksums++;
  803. }
  804. if (b && !XSUM_parseFileArg->statusOnly) {
  805. const int needsEscape = XSUM_filenameNeedsEscape(parsedLine.filename);
  806. if (needsEscape) {
  807. XSUM_output("%c", '\\');
  808. }
  809. XSUM_printFilename(parsedLine.filename, needsEscape);
  810. XSUM_output(": %s\n", lineStatus == LineStatus_hashOk ? "OK" : "FAILED");
  811. } }
  812. break;
  813. }
  814. } /* while (!report->quit) */
  815. }
  816. /* Parse xxHash checksum file.
  817. * Returns 1, if all procedures were succeeded.
  818. * Returns 0, if any procedures was failed.
  819. *
  820. * If strictMode != 0, return error code if any line is invalid.
  821. * If statusOnly != 0, don't generate any output.
  822. * If ignoreMissing != 0, ignore missing file. But if no file was verified, returns 0 (failed).
  823. * If warn != 0, print a warning message to stderr.
  824. * If quiet != 0, suppress "OK" line.
  825. *
  826. * "All procedures are succeeded" means:
  827. * - Checksum file contains at least one line and less than SIZE_T_MAX lines.
  828. * - All files are properly opened and read.
  829. * - All hash values match with its content.
  830. * - (strict mode) All lines in checksum file are consistent and well formatted.
  831. */
  832. static int XSUM_checkFile(const char* inFileName,
  833. const Display_endianess displayEndianess,
  834. XSUM_U32 strictMode,
  835. XSUM_U32 statusOnly,
  836. XSUM_U32 ignoreMissing,
  837. XSUM_U32 warn,
  838. XSUM_U32 quiet,
  839. XSUM_U32 algoBitmask)
  840. {
  841. int result = 0;
  842. FILE* inFile = NULL;
  843. ParseFileArg XSUM_parseFileArgBody;
  844. ParseFileArg* const XSUM_parseFileArg = &XSUM_parseFileArgBody;
  845. ParseFileReport* const report = &XSUM_parseFileArg->report;
  846. /* note: stdinName is special constant pointer. It is not a string. */
  847. if (inFileName == stdinName) {
  848. /*
  849. * Note: Since we expect text input for xxhash -c mode,
  850. * we don't set binary mode for stdin.
  851. */
  852. inFileName = stdinFileName; /* "stdin" */
  853. inFile = stdin;
  854. } else {
  855. inFile = XSUM_fopen( inFileName, "rt" );
  856. }
  857. if (inFile == NULL) {
  858. XSUM_log("Error: Could not open '%s': %s\n", inFileName, strerror(errno));
  859. return 0;
  860. }
  861. XSUM_parseFileArg->inFileName = inFileName;
  862. XSUM_parseFileArg->inFile = inFile;
  863. XSUM_parseFileArg->lineMax = DEFAULT_LINE_LENGTH;
  864. XSUM_parseFileArg->lineBuf = (char*) malloc((size_t)XSUM_parseFileArg->lineMax);
  865. XSUM_parseFileArg->blockSize = 64 * 1024;
  866. XSUM_parseFileArg->blockBuf = (char*) malloc(XSUM_parseFileArg->blockSize);
  867. XSUM_parseFileArg->strictMode = strictMode;
  868. XSUM_parseFileArg->statusOnly = statusOnly;
  869. XSUM_parseFileArg->ignoreMissing = ignoreMissing;
  870. XSUM_parseFileArg->warn = warn;
  871. XSUM_parseFileArg->quiet = quiet;
  872. XSUM_parseFileArg->algoBitmask = algoBitmask;
  873. if ( (XSUM_parseFileArg->lineBuf == NULL)
  874. || (XSUM_parseFileArg->blockBuf == NULL) ) {
  875. XSUM_log("Error: : memory allocation failed \n");
  876. exit(1);
  877. }
  878. XSUM_parseFile1(XSUM_parseFileArg, displayEndianess != big_endian);
  879. free(XSUM_parseFileArg->blockBuf);
  880. free(XSUM_parseFileArg->lineBuf);
  881. if (inFile != stdin) fclose(inFile);
  882. /* Show error/warning messages. All messages are copied from md5sum.c
  883. */
  884. if (report->nProperlyFormattedLines == 0) {
  885. XSUM_log("%s: no properly formatted xxHash checksum lines found\n", inFileName);
  886. } else if (!statusOnly) {
  887. if (report->nImproperlyFormattedLines) {
  888. XSUM_output("%lu %s improperly formatted\n"
  889. , report->nImproperlyFormattedLines
  890. , report->nImproperlyFormattedLines == 1 ? "line is" : "lines are");
  891. }
  892. if (report->nOpenOrReadFailures) {
  893. XSUM_output("%lu listed %s could not be read\n"
  894. , report->nOpenOrReadFailures
  895. , report->nOpenOrReadFailures == 1 ? "file" : "files");
  896. }
  897. if (report->nMismatchedChecksums) {
  898. XSUM_output("%lu computed %s did NOT match\n"
  899. , report->nMismatchedChecksums
  900. , report->nMismatchedChecksums == 1 ? "checksum" : "checksums");
  901. } }
  902. /* Result (exit) code logic is copied from
  903. * gnu coreutils/src/md5sum.c digest_check() */
  904. result = report->nProperlyFormattedLines != 0
  905. && report->nMismatchedChecksums == 0
  906. && report->nOpenOrReadFailures == 0
  907. && (!strictMode || report->nImproperlyFormattedLines == 0)
  908. && report->quit == 0;
  909. /* If "--ignore-missing" is enabled and there's no matched checksum, report it as error.
  910. * See https://github.com/coreutils/coreutils/blob/2f1cffe07ab0f0b4135a52d95f1689d7fc7f26c9/src/digest.c#L1325-L1328 */
  911. if (ignoreMissing && report->nMatchedChecksums == 0) {
  912. XSUM_output("%s: no file was verified\n", inFileName);
  913. result = 0;
  914. }
  915. return result;
  916. }
  917. static int XSUM_checkFiles(const char* fnList[], int fnTotal,
  918. const Display_endianess displayEndianess,
  919. XSUM_U32 strictMode,
  920. XSUM_U32 statusOnly,
  921. XSUM_U32 ignoreMissing,
  922. XSUM_U32 warn,
  923. XSUM_U32 quiet,
  924. XSUM_U32 algoBitmask)
  925. {
  926. int ok = 1;
  927. /* Special case for stdinName "-",
  928. * note: stdinName is not a string. It's special pointer. */
  929. if (fnTotal==0) {
  930. ok &= XSUM_checkFile(stdinName, displayEndianess, strictMode, statusOnly, ignoreMissing, warn, quiet, algoBitmask);
  931. } else {
  932. int fnNb;
  933. for (fnNb=0; fnNb<fnTotal; fnNb++)
  934. ok &= XSUM_checkFile(fnList[fnNb], displayEndianess, strictMode, statusOnly, ignoreMissing, warn, quiet, algoBitmask);
  935. }
  936. return ok ? 0 : 1;
  937. }
  938. /* ********************************************************
  939. * Main
  940. **********************************************************/
  941. static int XSUM_usage(const char* exename)
  942. {
  943. XSUM_log( WELCOME_MESSAGE(exename) );
  944. XSUM_log( "Print or verify checksums using fast non-cryptographic algorithm xxHash \n\n" );
  945. XSUM_log( "Usage: %s [options] [files] \n\n", exename);
  946. XSUM_log( "When no filename provided or when '-' is provided, uses stdin as input. \n");
  947. XSUM_log( "Options: \n");
  948. XSUM_log( " -H# algorithm selection: 0,1,2,3 or 32,64,128 (default: %i) \n", (int)g_defaultAlgo);
  949. XSUM_log( " -c, --check read xxHash checksum from [files] and check them \n");
  950. XSUM_log( " -h, --help display a long help page about advanced options \n");
  951. return 0;
  952. }
  953. static int XSUM_usage_advanced(const char* exename)
  954. {
  955. XSUM_usage(exename);
  956. XSUM_log( "Advanced :\n");
  957. XSUM_log( " -V, --version Display version information \n");
  958. XSUM_log( " --tag Produce BSD-style checksum lines \n");
  959. XSUM_log( " --little-endian Checksum values use little endian convention (default: big endian) \n");
  960. XSUM_log( " --binary Read in binary mode \n");
  961. XSUM_log( " -b Run benchmark \n");
  962. XSUM_log( " -b# Bench only algorithm variant # \n");
  963. XSUM_log( " -i# Number of times to run the benchmark (default: %i) \n", NBLOOPS_DEFAULT);
  964. XSUM_log( " -q, --quiet Don't display version header in benchmark mode \n");
  965. XSUM_log( "\n");
  966. XSUM_log( "The following five options are useful only when verifying checksums (-c): \n");
  967. XSUM_log( " -q, --quiet Don't print OK for each successfully verified file \n");
  968. XSUM_log( " --status Don't output anything, status code shows success \n");
  969. XSUM_log( " --strict Exit non-zero for improperly formatted checksum lines \n");
  970. XSUM_log( " --warn Warn about improperly formatted checksum lines \n");
  971. XSUM_log( " --ignore-missing Don't fail or report status for missing files \n");
  972. return 0;
  973. }
  974. static int XSUM_badusage(const char* exename)
  975. {
  976. XSUM_log("Wrong parameters\n\n");
  977. XSUM_usage(exename);
  978. return 1;
  979. }
  980. static void errorOut(const char* msg)
  981. {
  982. XSUM_log("%s \n", msg);
  983. exit(1);
  984. }
  985. static const char* XSUM_lastNameFromPath(const char* path)
  986. {
  987. const char* name = path;
  988. if (strrchr(name, '/')) name = strrchr(name, '/') + 1;
  989. if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1; /* windows */
  990. return name;
  991. }
  992. /*!
  993. * XSUM_readU32FromCharChecked():
  994. * @return 0 if success, and store the result in *value.
  995. * Allows and interprets K, KB, KiB, M, MB and MiB suffix.
  996. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
  997. * @return 1 if an overflow error occurs
  998. */
  999. static int XSUM_readU32FromCharChecked(const char** stringPtr, XSUM_U32* value)
  1000. {
  1001. static const XSUM_U32 max = (((XSUM_U32)(-1)) / 10) - 1;
  1002. XSUM_U32 result = 0;
  1003. while ((**stringPtr >='0') && (**stringPtr <='9')) {
  1004. if (result > max) return 1; /* overflow error */
  1005. result *= 10;
  1006. result += (XSUM_U32)(**stringPtr - '0');
  1007. (*stringPtr)++ ;
  1008. }
  1009. if ((**stringPtr=='K') || (**stringPtr=='M')) {
  1010. XSUM_U32 const maxK = ((XSUM_U32)(-1)) >> 10;
  1011. if (result > maxK) return 1; /* overflow error */
  1012. result <<= 10;
  1013. if (**stringPtr=='M') {
  1014. if (result > maxK) return 1; /* overflow error */
  1015. result <<= 10;
  1016. }
  1017. (*stringPtr)++; /* skip `K` or `M` */
  1018. if (**stringPtr=='i') (*stringPtr)++;
  1019. if (**stringPtr=='B') (*stringPtr)++;
  1020. }
  1021. *value = result;
  1022. return 0;
  1023. }
  1024. /*!
  1025. * XSUM_readU32FromChar():
  1026. * @return: unsigned integer value read from input in `char` format.
  1027. * allows and interprets K, KB, KiB, M, MB and MiB suffix.
  1028. * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
  1029. * Note: function will exit() program if digit sequence overflows
  1030. */
  1031. static XSUM_U32 XSUM_readU32FromChar(const char** stringPtr) {
  1032. XSUM_U32 result;
  1033. if (XSUM_readU32FromCharChecked(stringPtr, &result)) {
  1034. static const char errorMsg[] = "Error: numeric value too large";
  1035. errorOut(errorMsg);
  1036. }
  1037. return result;
  1038. }
  1039. XSUM_API int XSUM_main(int argc, const char* argv[])
  1040. {
  1041. int i, filenamesStart = 0;
  1042. const char* const exename = XSUM_lastNameFromPath(argv[0]);
  1043. XSUM_U32 benchmarkMode = 0;
  1044. XSUM_U32 fileCheckMode = 0;
  1045. XSUM_U32 strictMode = 0;
  1046. XSUM_U32 statusOnly = 0;
  1047. XSUM_U32 warn = 0;
  1048. XSUM_U32 ignoreMissing = 0;
  1049. XSUM_U32 algoBitmask = algo_bitmask_all;
  1050. int explicitStdin = 0;
  1051. XSUM_U32 selectBenchIDs= 0; /* 0 == use default k_testIDs_default, kBenchAll == bench all */
  1052. static const XSUM_U32 kBenchAll = 99;
  1053. size_t keySize = XSUM_DEFAULT_SAMPLE_SIZE;
  1054. AlgoSelected algo = g_defaultAlgo;
  1055. Display_endianess displayEndianess = big_endian;
  1056. Display_convention convention = display_gnu;
  1057. int nbIterations = NBLOOPS_DEFAULT;
  1058. /* special case: xxhNNsum default to NN bits checksum */
  1059. if (strstr(exename, "xxh32sum") != NULL) { algo = g_defaultAlgo = algo_xxh32; algoBitmask = algo_bitmask_xxh32; }
  1060. if (strstr(exename, "xxh64sum") != NULL) { algo = g_defaultAlgo = algo_xxh64; algoBitmask = algo_bitmask_xxh64; }
  1061. if (strstr(exename, "xxh128sum") != NULL) { algo = g_defaultAlgo = algo_xxh128; algoBitmask = algo_bitmask_xxh128; }
  1062. for (i=1; i<argc; i++) {
  1063. const char* argument = argv[i];
  1064. assert(argument != NULL);
  1065. if (!strcmp(argument, "--check")) { fileCheckMode = 1; continue; }
  1066. if (!strcmp(argument, "--benchmark-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
  1067. if (!strcmp(argument, "--bench-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; }
  1068. if (!strcmp(argument, "--quiet")) { XSUM_logLevel--; continue; }
  1069. if (!strcmp(argument, "--little-endian")) { displayEndianess = little_endian; continue; }
  1070. if (!strcmp(argument, "--strict")) { strictMode = 1; continue; }
  1071. if (!strcmp(argument, "--status")) { statusOnly = 1; continue; }
  1072. if (!strcmp(argument, "--warn")) { warn = 1; continue; }
  1073. if (!strcmp(argument, "--binary")) { continue; } /* Just ignore it. See https://github.com/Cyan4973/xxHash/issues/812 */
  1074. if (!strcmp(argument, "--ignore-missing")) { ignoreMissing = 1; continue; }
  1075. if (!strcmp(argument, "--help")) { return XSUM_usage_advanced(exename); }
  1076. if (!strcmp(argument, "--version")) { XSUM_log(FULL_WELCOME_MESSAGE(exename)); XSUM_sanityCheck(); return 0; }
  1077. if (!strcmp(argument, "--tag")) { convention = display_bsd; continue; }
  1078. if (!strcmp(argument, "--")) {
  1079. if (filenamesStart==0 && i!=argc-1) filenamesStart=i+1; /* only supports a continuous list of filenames */
  1080. break; /* treat rest of arguments as strictly file names */
  1081. }
  1082. if (*argument != '-') {
  1083. if (filenamesStart==0) filenamesStart=i; /* only supports a continuous list of filenames */
  1084. break; /* treat rest of arguments as strictly file names */
  1085. }
  1086. /* command selection */
  1087. argument++; /* note: *argument=='-' */
  1088. if (*argument == 0) explicitStdin = 1;
  1089. while (*argument != 0) {
  1090. switch(*argument)
  1091. {
  1092. /* Display version */
  1093. case 'V':
  1094. XSUM_log(FULL_WELCOME_MESSAGE(exename));
  1095. XSUM_sanityCheck();
  1096. return 0;
  1097. /* Display help on XSUM_usage */
  1098. case 'h':
  1099. return XSUM_usage_advanced(exename);
  1100. /* select hash algorithm */
  1101. case 'H': argument++;
  1102. switch(XSUM_readU32FromChar(&argument)) {
  1103. case 0 :
  1104. case 32: algo = algo_xxh32; break;
  1105. case 1 :
  1106. case 64: algo = algo_xxh64; break;
  1107. case 2 :
  1108. case 128: algo = algo_xxh128; break;
  1109. case 3 : /* xxh3 - necessarily uses BSD convention to avoid confusion with XXH64 */
  1110. algo = algo_xxh3;
  1111. convention = display_bsd;
  1112. break;
  1113. default:
  1114. return XSUM_badusage(exename);
  1115. }
  1116. break;
  1117. /* File check mode */
  1118. case 'c':
  1119. fileCheckMode=1;
  1120. argument++;
  1121. break;
  1122. /* Warning mode (file check mode only, alias of "--warning") */
  1123. case 'w':
  1124. warn=1;
  1125. argument++;
  1126. break;
  1127. /* Trigger benchmark mode */
  1128. case 'b':
  1129. argument++;
  1130. benchmarkMode = 1;
  1131. do {
  1132. if (*argument == ',') argument++;
  1133. selectBenchIDs = XSUM_readU32FromChar(&argument); /* select one specific test */
  1134. if ((int)selectBenchIDs < g_nbTestFunctions) {
  1135. g_testIDs[selectBenchIDs] = 1;
  1136. } else {
  1137. selectBenchIDs = kBenchAll;
  1138. }
  1139. } while (*argument == ',');
  1140. break;
  1141. /* Modify Nb Iterations (benchmark only) */
  1142. case 'i':
  1143. argument++;
  1144. nbIterations = (int)XSUM_readU32FromChar(&argument);
  1145. break;
  1146. /* Modify Block size (benchmark only) */
  1147. case 'B':
  1148. argument++;
  1149. keySize = XSUM_readU32FromChar(&argument);
  1150. break;
  1151. /* Modify verbosity of benchmark output (hidden option) */
  1152. case 'q':
  1153. argument++;
  1154. XSUM_logLevel--;
  1155. break;
  1156. default:
  1157. return XSUM_badusage(exename);
  1158. }
  1159. }
  1160. } /* for(i=1; i<argc; i++) */
  1161. /* Check benchmark mode */
  1162. if (benchmarkMode) {
  1163. XSUM_logVerbose(2, FULL_WELCOME_MESSAGE(exename) );
  1164. XSUM_sanityCheck();
  1165. g_nbIterations = nbIterations;
  1166. if (selectBenchIDs == 0) memcpy(g_testIDs, k_testIDs_default, (size_t)g_nbTestFunctions);
  1167. if (selectBenchIDs == kBenchAll) memset(g_testIDs, 1, (size_t)g_nbTestFunctions);
  1168. if (filenamesStart==0) return XSUM_benchInternal(keySize);
  1169. return XSUM_benchFiles(argv+filenamesStart, argc-filenamesStart);
  1170. }
  1171. /* Check if input is defined as console; trigger an error in this case */
  1172. if ( (filenamesStart==0) && XSUM_isConsole(stdin) && !explicitStdin)
  1173. return XSUM_badusage(exename);
  1174. if (filenamesStart==0) filenamesStart = argc;
  1175. if (fileCheckMode) {
  1176. return XSUM_checkFiles(argv+filenamesStart, argc-filenamesStart,
  1177. displayEndianess, strictMode, statusOnly, ignoreMissing, warn, (XSUM_logLevel < 2) /*quiet*/, algoBitmask);
  1178. } else {
  1179. return XSUM_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, displayEndianess, convention);
  1180. }
  1181. }