| 1 |
/* |
| 2 |
* xxhsum - Command line interface for xxhash algorithms |
| 3 |
* Copyright (C) 2013-2023 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 |
/* |
| 27 |
* xxhsum: |
| 28 |
* Provides hash value of a file content, or a list of files, or stdin |
| 29 |
* Display convention is Big Endian, for both 32 and 64 bits algorithms |
| 30 |
*/ |
| 31 |
|
| 32 |
/* Transitional headers */ |
| 33 |
#include "xsum_arch.h" /* XSUM_PROGRAM_VERSION */ |
| 34 |
#include "xsum_os_specific.h" /* XSUM_setBinaryMode */ |
| 35 |
#include "xsum_output.h" /* XSUM_output */ |
| 36 |
#include "xsum_sanity_check.h" /* XSUM_sanityCheck */ |
| 37 |
#include "xsum_bench.h" /* NBLOOPS_DEFAULT */ |
| 38 |
#ifdef XXH_INLINE_ALL |
| 39 |
# include "xsum_os_specific.c" |
| 40 |
# include "xsum_output.c" |
| 41 |
# include "xsum_sanity_check.c" |
| 42 |
# include "xsum_bench.c" |
| 43 |
#endif |
| 44 |
|
| 45 |
/* ************************************ |
| 46 |
* Includes |
| 47 |
**************************************/ |
| 48 |
#include <stdlib.h> /* malloc, calloc, free, exit */ |
| 49 |
#include <string.h> /* strerror, strcmp, memcpy */ |
| 50 |
#include <assert.h> /* assert */ |
| 51 |
#include <errno.h> /* errno */ |
| 52 |
|
| 53 |
#define XXH_STATIC_LINKING_ONLY /* *_state_t */ |
| 54 |
#include "../xxhash.h" |
| 55 |
|
| 56 |
#ifdef XXHSUM_DISPATCH |
| 57 |
# include "../xxh_x86dispatch.h" |
| 58 |
#endif |
| 59 |
|
| 60 |
static unsigned XSUM_isLittleEndian(void) |
| 61 |
{ |
| 62 |
const union { XSUM_U32 u; XSUM_U8 c[4]; } one = { 1 }; /* don't use static: performance detrimental */ |
| 63 |
return one.c[0]; |
| 64 |
} |
| 65 |
|
| 66 |
static const int g_nbBits = (int)(sizeof(void*)*8); |
| 67 |
static const char g_lename[] = "little endian"; |
| 68 |
static const char g_bename[] = "big endian"; |
| 69 |
#define ENDIAN_NAME (XSUM_isLittleEndian() ? g_lename : g_bename) |
| 70 |
static const char author[] = "Yann Collet"; |
| 71 |
#define WELCOME_MESSAGE(exename) "%s %s by %s \n", exename, XSUM_PROGRAM_VERSION, author |
| 72 |
#define FULL_WELCOME_MESSAGE(exename) "%s %s by %s \n" \ |
| 73 |
"compiled as %i-bit %s %s with " XSUM_CC_VERSION_FMT " \n", \ |
| 74 |
exename, XSUM_PROGRAM_VERSION, author, \ |
| 75 |
g_nbBits, XSUM_ARCH, ENDIAN_NAME, XSUM_CC_VERSION |
| 76 |
|
| 77 |
|
| 78 |
static const char stdinName[] = "-"; |
| 79 |
static const char stdinFileName[] = "stdin"; |
| 80 |
typedef enum { algo_xxh32=0, algo_xxh64=1, algo_xxh128=2, algo_xxh3=3 } AlgoSelected; |
| 81 |
static AlgoSelected g_defaultAlgo = algo_xxh64; /* required within main() & XSUM_usage() */ |
| 82 |
|
| 83 |
typedef enum { |
| 84 |
algo_bitmask_xxh32 = 1 << algo_xxh32, /* 1 << 0 */ |
| 85 |
algo_bitmask_xxh64 = 1 << algo_xxh64, /* 1 << 1 */ |
| 86 |
algo_bitmask_xxh128 = 1 << algo_xxh128, /* 1 << 2 */ |
| 87 |
algo_bitmask_xxh3 = 1 << algo_xxh3, /* 1 << 3 */ |
| 88 |
algo_bitmask_all = algo_bitmask_xxh32 /* All algorithms */ |
| 89 |
| algo_bitmask_xxh64 |
| 90 |
| algo_bitmask_xxh128 |
| 91 |
| algo_bitmask_xxh3 |
| 92 |
} AlgoBitmask; |
| 93 |
|
| 94 |
|
| 95 |
/* <16 hex char> <SPC> <SPC> <filename> <'\0'> |
| 96 |
* '4096' is typical Linux PATH_MAX configuration. */ |
| 97 |
#define DEFAULT_LINE_LENGTH (sizeof(XXH64_hash_t) * 2 + 2 + 4096 + 1) |
| 98 |
|
| 99 |
/* Maximum acceptable line length. */ |
| 100 |
#define MAX_LINE_LENGTH (32 KB) |
| 101 |
|
| 102 |
static size_t XSUM_DEFAULT_SAMPLE_SIZE = 100 KB; |
| 103 |
|
| 104 |
|
| 105 |
/* ******************************************************** |
| 106 |
* Filename (un)escaping |
| 107 |
**********************************************************/ |
| 108 |
static int XSUM_filenameNeedsEscape(const char* filename) { |
| 109 |
return strchr(filename, '\\') |
| 110 |
|| strchr(filename, '\n') |
| 111 |
|| strchr(filename, '\r'); |
| 112 |
} |
| 113 |
|
| 114 |
static int XSUM_lineNeedsUnescape(const char* line) { |
| 115 |
/* Skip white-space characters */ |
| 116 |
while (*line == ' ' || *line == '\t') { |
| 117 |
++line; |
| 118 |
} |
| 119 |
/* Returns true if first non-white-space character is '\\' (0x5c) */ |
| 120 |
return *line == '\\'; |
| 121 |
} |
| 122 |
|
| 123 |
static void XSUM_printFilename(const char* filename, int needsEscape) { |
| 124 |
if (!needsEscape) { |
| 125 |
XSUM_output("%s", filename); |
| 126 |
} else { |
| 127 |
const char* p; |
| 128 |
for (p = filename; *p != '\0'; ++p) { |
| 129 |
switch (*p) |
| 130 |
{ |
| 131 |
case '\n': |
| 132 |
XSUM_output("\\n"); |
| 133 |
break; |
| 134 |
case '\r': |
| 135 |
XSUM_output("\\r"); |
| 136 |
break; |
| 137 |
case '\\': |
| 138 |
XSUM_output("\\\\"); |
| 139 |
break; |
| 140 |
default: |
| 141 |
XSUM_output("%c", *p); |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/* Unescape filename in place. |
| 149 |
|
| 150 |
- Replace '\\', 'n' (0x5c, 0x6e) with '\n' (0x0a). |
| 151 |
- Replace '\\', 'r' (0x5c, 0x72) with '\r' (0x0d). |
| 152 |
- Replace '\\', '\\' (0x5c, 0x5c) with '\\' (0x5c). |
| 153 |
- filename may not contain other backslash sequences. |
| 154 |
- filename may not ends with backslash. |
| 155 |
- filename may not contain NUL (0x00). |
| 156 |
|
| 157 |
Return filename if everything is okay. |
| 158 |
Return NULL if something wrong. |
| 159 |
*/ |
| 160 |
static char* XSUM_filenameUnescape(char* filename, size_t filenameLen) { |
| 161 |
char *p = filename; |
| 162 |
size_t i; |
| 163 |
for (i = 0; i < filenameLen; ++i) { |
| 164 |
switch (filename[i]) |
| 165 |
{ |
| 166 |
case '\\': |
| 167 |
++i; |
| 168 |
if (i == filenameLen) { |
| 169 |
return NULL; /* Don't accept '\\', <EOL> */ |
| 170 |
} |
| 171 |
switch (filename[i]) |
| 172 |
{ |
| 173 |
case 'n': |
| 174 |
*p++ = '\n'; |
| 175 |
break; |
| 176 |
case 'r': |
| 177 |
*p++ = '\r'; |
| 178 |
break; |
| 179 |
case '\\': |
| 180 |
*p++ = '\\'; |
| 181 |
break; |
| 182 |
default: |
| 183 |
return NULL; /* Don't accept any other backslash sequence */ |
| 184 |
} |
| 185 |
break; |
| 186 |
case '\0': |
| 187 |
return NULL; /* Don't accept NUL (0x00) */ |
| 188 |
default: |
| 189 |
*p++ = filename[i]; |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
if (p < filename + filenameLen) { |
| 194 |
*p = '\0'; |
| 195 |
} |
| 196 |
return filename; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/* ******************************************************** |
| 201 |
* Algorithm Bitmask |
| 202 |
**********************************************************/ |
| 203 |
/* Compute AlgoBitmask (as a U32) from AlgoSelected */ |
| 204 |
static XSUM_U32 XSUM_algoBitmask_ComputeAlgoBitmaskFromAlgoSelected(AlgoSelected algoSelected) { |
| 205 |
return (XSUM_U32) (1U << algoSelected); |
| 206 |
} |
| 207 |
|
| 208 |
/* Returns true (!0) if algoBitmask contains (accepts) parsedLineAlgo */ |
| 209 |
static int XSUM_algoBitmask_Accepts(XSUM_U32 algoBitmask, AlgoSelected parsedLineAlgo) { |
| 210 |
const XSUM_U32 mask = XSUM_algoBitmask_ComputeAlgoBitmaskFromAlgoSelected(parsedLineAlgo); |
| 211 |
return (algoBitmask & mask) != 0; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/* ******************************************************** |
| 216 |
* File Hashing |
| 217 |
**********************************************************/ |
| 218 |
|
| 219 |
#define XXHSUM32_DEFAULT_SEED 0 /* Default seed for algo_xxh32 */ |
| 220 |
#define XXHSUM64_DEFAULT_SEED 0 /* Default seed for algo_xxh64 */ |
| 221 |
|
| 222 |
/* for support of --little-endian display mode */ |
| 223 |
static void XSUM_display_LittleEndian(const void* ptr, size_t length) |
| 224 |
{ |
| 225 |
const XSUM_U8* const p = (const XSUM_U8*)ptr; |
| 226 |
size_t idx; |
| 227 |
for (idx=length-1; idx<length; idx--) /* intentional underflow to negative to detect end */ |
| 228 |
XSUM_output("%02x", p[idx]); |
| 229 |
} |
| 230 |
|
| 231 |
static void XSUM_display_BigEndian(const void* ptr, size_t length) |
| 232 |
{ |
| 233 |
const XSUM_U8* const p = (const XSUM_U8*)ptr; |
| 234 |
size_t idx; |
| 235 |
for (idx=0; idx<length; idx++) |
| 236 |
XSUM_output("%02x", p[idx]); |
| 237 |
} |
| 238 |
|
| 239 |
typedef union { |
| 240 |
XXH32_hash_t hash32; |
| 241 |
XXH64_hash_t hash64; /* also for xxh3_64bits */ |
| 242 |
XXH128_hash_t hash128; |
| 243 |
} Multihash; |
| 244 |
|
| 245 |
/* |
| 246 |
* XSUM_hashStream: |
| 247 |
* Reads data from `inFile`, generating an incremental hash of type hashType, |
| 248 |
* using `buffer` of size `blockSize` for temporary storage. |
| 249 |
*/ |
| 250 |
static Multihash |
| 251 |
XSUM_hashStream(FILE* inFile, |
| 252 |
AlgoSelected hashType, |
| 253 |
void* buffer, size_t blockSize) |
| 254 |
{ |
| 255 |
XXH32_state_t state32; |
| 256 |
XXH64_state_t state64; |
| 257 |
XXH3_state_t state3; |
| 258 |
|
| 259 |
/* Init */ |
| 260 |
(void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED); |
| 261 |
(void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED); |
| 262 |
(void)XXH3_128bits_reset(&state3); |
| 263 |
|
| 264 |
/* Load file & update hash */ |
| 265 |
{ size_t readSize; |
| 266 |
while ((readSize = fread(buffer, 1, blockSize, inFile)) > 0) { |
| 267 |
switch(hashType) |
| 268 |
{ |
| 269 |
case algo_xxh32: |
| 270 |
(void)XXH32_update(&state32, buffer, readSize); |
| 271 |
break; |
| 272 |
case algo_xxh64: |
| 273 |
(void)XXH64_update(&state64, buffer, readSize); |
| 274 |
break; |
| 275 |
case algo_xxh128: |
| 276 |
(void)XXH3_128bits_update(&state3, buffer, readSize); |
| 277 |
break; |
| 278 |
case algo_xxh3: |
| 279 |
(void)XXH3_64bits_update(&state3, buffer, readSize); |
| 280 |
break; |
| 281 |
default: |
| 282 |
assert(0); |
| 283 |
} |
| 284 |
} |
| 285 |
if (ferror(inFile)) { |
| 286 |
XSUM_log("Error: a failure occurred reading the input file.\n"); |
| 287 |
exit(1); |
| 288 |
} } |
| 289 |
|
| 290 |
{ Multihash finalHash = {0}; |
| 291 |
switch(hashType) |
| 292 |
{ |
| 293 |
case algo_xxh32: |
| 294 |
finalHash.hash32 = XXH32_digest(&state32); |
| 295 |
break; |
| 296 |
case algo_xxh64: |
| 297 |
finalHash.hash64 = XXH64_digest(&state64); |
| 298 |
break; |
| 299 |
case algo_xxh128: |
| 300 |
finalHash.hash128 = XXH3_128bits_digest(&state3); |
| 301 |
break; |
| 302 |
case algo_xxh3: |
| 303 |
finalHash.hash64 = XXH3_64bits_digest(&state3); |
| 304 |
break; |
| 305 |
default: |
| 306 |
assert(0); |
| 307 |
} |
| 308 |
return finalHash; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/* algo_xxh32, algo_xxh64, algo_xxh128 */ |
| 313 |
static const char* XSUM_algoName[] = { "XXH32", "XXH64", "XXH128", "XXH3" }; |
| 314 |
static const char* XSUM_algoLE_name[] = { "XXH32_LE", "XXH64_LE", "XXH128_LE", "XXH3_LE" }; |
| 315 |
static const size_t XSUM_algoLength[] = { 4, 8, 16, 8 }; |
| 316 |
|
| 317 |
#define XSUM_TABLE_ELT_SIZE(table) (sizeof(table) / sizeof(*table)) |
| 318 |
|
| 319 |
typedef void (*XSUM_displayHash_f)(const void*, size_t); /* display function signature */ |
| 320 |
|
| 321 |
static void XSUM_printLine_BSD_internal(const char* filename, |
| 322 |
const void* canonicalHash, const AlgoSelected hashType, |
| 323 |
const char* algoString[], |
| 324 |
XSUM_displayHash_f f_displayHash) |
| 325 |
{ |
| 326 |
assert(0 <= hashType && (size_t)hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName)); |
| 327 |
{ const char* const typeString = algoString[hashType]; |
| 328 |
const size_t hashLength = XSUM_algoLength[hashType]; |
| 329 |
const int needsEscape = XSUM_filenameNeedsEscape(filename); |
| 330 |
if (needsEscape) { |
| 331 |
XSUM_output("%c", '\\'); |
| 332 |
} |
| 333 |
XSUM_output("%s (", typeString); |
| 334 |
XSUM_printFilename(filename, needsEscape); |
| 335 |
XSUM_output(") = "); |
| 336 |
f_displayHash(canonicalHash, hashLength); |
| 337 |
XSUM_output("\n"); |
| 338 |
} } |
| 339 |
|
| 340 |
static void XSUM_printLine_BSD_LE(const char* filename, const void* canonicalHash, const AlgoSelected hashType) |
| 341 |
{ |
| 342 |
XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoLE_name, XSUM_display_LittleEndian); |
| 343 |
} |
| 344 |
|
| 345 |
static void XSUM_printLine_BSD(const char* filename, const void* canonicalHash, const AlgoSelected hashType) |
| 346 |
{ |
| 347 |
XSUM_printLine_BSD_internal(filename, canonicalHash, hashType, XSUM_algoName, XSUM_display_BigEndian); |
| 348 |
} |
| 349 |
|
| 350 |
static void XSUM_displayPrefix(const AlgoSelected hashType) |
| 351 |
{ |
| 352 |
assert(0 <= hashType && (size_t)hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName)); |
| 353 |
if (hashType == algo_xxh3) { |
| 354 |
XSUM_output("XXH3_"); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
static void XSUM_printLine_GNU_internal(const char* filename, |
| 359 |
const void* canonicalHash, const AlgoSelected hashType, |
| 360 |
XSUM_displayHash_f f_displayHash) |
| 361 |
{ |
| 362 |
assert(0 <= hashType && (size_t)hashType <= XSUM_TABLE_ELT_SIZE(XSUM_algoName)); |
| 363 |
{ const size_t hashLength = XSUM_algoLength[hashType]; |
| 364 |
const int needsEscape = XSUM_filenameNeedsEscape(filename); |
| 365 |
if (needsEscape) { |
| 366 |
XSUM_output("%c", '\\'); |
| 367 |
} |
| 368 |
XSUM_displayPrefix(hashType); |
| 369 |
f_displayHash(canonicalHash, hashLength); |
| 370 |
XSUM_output(" "); |
| 371 |
XSUM_printFilename(filename, needsEscape); |
| 372 |
XSUM_output("\n"); |
| 373 |
} } |
| 374 |
|
| 375 |
static void XSUM_printLine_GNU(const char* filename, |
| 376 |
const void* canonicalHash, const AlgoSelected hashType) |
| 377 |
{ |
| 378 |
XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_BigEndian); |
| 379 |
} |
| 380 |
|
| 381 |
static void XSUM_printLine_GNU_LE(const char* filename, |
| 382 |
const void* canonicalHash, const AlgoSelected hashType) |
| 383 |
{ |
| 384 |
XSUM_printLine_GNU_internal(filename, canonicalHash, hashType, XSUM_display_LittleEndian); |
| 385 |
} |
| 386 |
|
| 387 |
typedef enum { big_endian, little_endian} Display_endianness; |
| 388 |
|
| 389 |
typedef enum { display_gnu, display_bsd } Display_convention; |
| 390 |
|
| 391 |
typedef void (*XSUM_displayLine_f)(const char*, const void*, AlgoSelected); /* line display signature */ |
| 392 |
|
| 393 |
typedef enum { |
| 394 |
LineStatus_hashOk, |
| 395 |
LineStatus_hashFailed, |
| 396 |
LineStatus_failedToOpen, |
| 397 |
LineStatus_isDirectory, |
| 398 |
LineStatus_memoryError |
| 399 |
} LineStatus; |
| 400 |
|
| 401 |
static XSUM_displayLine_f XSUM_kDisplayLine_fTable[2][2] = { |
| 402 |
{ XSUM_printLine_GNU, XSUM_printLine_GNU_LE }, |
| 403 |
{ XSUM_printLine_BSD, XSUM_printLine_BSD_LE } |
| 404 |
}; |
| 405 |
|
| 406 |
static LineStatus XSUM_hashFile(const char* fileName, |
| 407 |
const AlgoSelected hashType, |
| 408 |
const Display_endianness displayEndianness, |
| 409 |
const Display_convention convention) |
| 410 |
{ |
| 411 |
size_t const blockSize = 64 KB; |
| 412 |
XSUM_displayLine_f const f_displayLine = XSUM_kDisplayLine_fTable[convention][displayEndianness]; |
| 413 |
FILE* inFile; |
| 414 |
Multihash hashValue; |
| 415 |
assert(displayEndianness==big_endian || displayEndianness==little_endian); |
| 416 |
assert(convention==display_gnu || convention==display_bsd); |
| 417 |
|
| 418 |
/* Check file existence */ |
| 419 |
if (fileName == stdinName) { |
| 420 |
inFile = stdin; |
| 421 |
fileName = stdinFileName; /* "stdin" */ |
| 422 |
XSUM_setBinaryMode(stdin); |
| 423 |
} else { |
| 424 |
if (XSUM_isDirectory(fileName)) { |
| 425 |
return LineStatus_isDirectory; |
| 426 |
} |
| 427 |
inFile = XSUM_fopen( fileName, "rb" ); |
| 428 |
if (inFile==NULL) { |
| 429 |
return LineStatus_failedToOpen; |
| 430 |
} } |
| 431 |
|
| 432 |
/* Memory allocation & streaming */ |
| 433 |
{ void* const buffer = malloc(blockSize); |
| 434 |
if (buffer == NULL) { |
| 435 |
XSUM_log("\nError: Out of memory.\n"); |
| 436 |
fclose(inFile); |
| 437 |
return LineStatus_memoryError; |
| 438 |
} |
| 439 |
|
| 440 |
/* Stream file & update hash */ |
| 441 |
hashValue = XSUM_hashStream(inFile, hashType, buffer, blockSize); |
| 442 |
|
| 443 |
fclose(inFile); |
| 444 |
free(buffer); |
| 445 |
} |
| 446 |
|
| 447 |
/* display Hash value in selected format */ |
| 448 |
switch(hashType) |
| 449 |
{ |
| 450 |
case algo_xxh32: |
| 451 |
{ XXH32_canonical_t hcbe32; |
| 452 |
(void)XXH32_canonicalFromHash(&hcbe32, hashValue.hash32); |
| 453 |
f_displayLine(fileName, &hcbe32, hashType); |
| 454 |
break; |
| 455 |
} |
| 456 |
case algo_xxh64: |
| 457 |
{ XXH64_canonical_t hcbe64; |
| 458 |
(void)XXH64_canonicalFromHash(&hcbe64, hashValue.hash64); |
| 459 |
f_displayLine(fileName, &hcbe64, hashType); |
| 460 |
break; |
| 461 |
} |
| 462 |
case algo_xxh128: |
| 463 |
{ XXH128_canonical_t hcbe128; |
| 464 |
(void)XXH128_canonicalFromHash(&hcbe128, hashValue.hash128); |
| 465 |
f_displayLine(fileName, &hcbe128, hashType); |
| 466 |
break; |
| 467 |
} |
| 468 |
case algo_xxh3: |
| 469 |
{ XXH64_canonical_t hcbe64; |
| 470 |
(void)XXH64_canonicalFromHash(&hcbe64, hashValue.hash64); |
| 471 |
f_displayLine(fileName, &hcbe64, hashType); |
| 472 |
break; |
| 473 |
} |
| 474 |
default: |
| 475 |
assert(0); /* not possible */ |
| 476 |
} |
| 477 |
|
| 478 |
return LineStatus_hashOk; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
/* |
| 483 |
* XSUM_hashFiles: |
| 484 |
* If fnTotal==0, read from stdin instead. |
| 485 |
*/ |
| 486 |
static int XSUM_hashFiles(const char* fnList[], int fnTotal, |
| 487 |
AlgoSelected hashType, |
| 488 |
Display_endianness displayEndianness, |
| 489 |
Display_convention convention) |
| 490 |
{ |
| 491 |
int fnNb; |
| 492 |
int result = 0; |
| 493 |
|
| 494 |
if (fnTotal == 0) |
| 495 |
{ |
| 496 |
LineStatus filestatus = XSUM_hashFile(stdinName, hashType, displayEndianness, convention); |
| 497 |
switch (filestatus) |
| 498 |
{ |
| 499 |
case LineStatus_hashOk: |
| 500 |
case LineStatus_hashFailed: |
| 501 |
break; |
| 502 |
case LineStatus_isDirectory: |
| 503 |
XSUM_log("xxhsum: %s: Is a directory \n", stdinName); |
| 504 |
break; |
| 505 |
case LineStatus_failedToOpen: |
| 506 |
XSUM_log("Error: Could not open '%s': %s. \n", stdinName, strerror(errno)); |
| 507 |
break; |
| 508 |
case LineStatus_memoryError: |
| 509 |
XSUM_log("\nError: Out of memory.\n"); |
| 510 |
break; |
| 511 |
} |
| 512 |
|
| 513 |
if (filestatus != LineStatus_hashOk) |
| 514 |
result = 1; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
for (fnNb = 0; fnNb < fnTotal; fnNb++) |
| 519 |
{ |
| 520 |
LineStatus filestatus = XSUM_hashFile(fnList[fnNb], hashType, displayEndianness, convention); |
| 521 |
switch (filestatus) |
| 522 |
{ |
| 523 |
case LineStatus_hashOk: |
| 524 |
case LineStatus_hashFailed: |
| 525 |
break; |
| 526 |
case LineStatus_isDirectory: |
| 527 |
XSUM_log("xxhsum: %s: Is a directory \n", fnList[fnNb]); |
| 528 |
break; |
| 529 |
case LineStatus_failedToOpen: |
| 530 |
XSUM_log("Error: Could not open '%s': %s. \n", fnList[fnNb], strerror(errno)); |
| 531 |
break; |
| 532 |
case LineStatus_memoryError: |
| 533 |
XSUM_log("\nError: Out of memory.\n"); |
| 534 |
break; |
| 535 |
} |
| 536 |
|
| 537 |
if (filestatus != LineStatus_hashOk) |
| 538 |
result = 1; |
| 539 |
} |
| 540 |
|
| 541 |
return result; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
typedef enum { |
| 546 |
GetLine_ok, |
| 547 |
GetLine_comment, |
| 548 |
GetLine_eof, |
| 549 |
GetLine_exceedMaxLineLength, |
| 550 |
GetLine_outOfMemory |
| 551 |
} GetLineResult; |
| 552 |
|
| 553 |
typedef enum { |
| 554 |
CanonicalFromString_ok, |
| 555 |
CanonicalFromString_invalidFormat |
| 556 |
} CanonicalFromStringResult; |
| 557 |
|
| 558 |
typedef enum { |
| 559 |
ParseLine_ok, |
| 560 |
ParseLine_invalidFormat |
| 561 |
} ParseLineResult; |
| 562 |
|
| 563 |
typedef union { |
| 564 |
XXH32_canonical_t xxh32; |
| 565 |
XXH64_canonical_t xxh64; |
| 566 |
XXH128_canonical_t xxh128; |
| 567 |
} Canonical; |
| 568 |
|
| 569 |
typedef struct { |
| 570 |
Canonical canonical; |
| 571 |
const char* filename; |
| 572 |
AlgoSelected algo; |
| 573 |
} ParsedLine; |
| 574 |
|
| 575 |
typedef struct { |
| 576 |
unsigned long nProperlyFormattedLines; |
| 577 |
unsigned long nImproperlyFormattedLines; |
| 578 |
unsigned long nMismatchedChecksums; |
| 579 |
unsigned long nMatchedChecksums; |
| 580 |
unsigned long nOpenOrReadFailures; |
| 581 |
unsigned long nMixedFormatLines; |
| 582 |
unsigned long nMissing; |
| 583 |
int quit; |
| 584 |
} ParseFileReport; |
| 585 |
|
| 586 |
typedef struct { |
| 587 |
const char* inFileName; |
| 588 |
FILE* inFile; |
| 589 |
int lineMax; |
| 590 |
char* lineBuf; |
| 591 |
size_t blockSize; |
| 592 |
char* blockBuf; |
| 593 |
int strictMode; |
| 594 |
int statusOnly; |
| 595 |
int ignoreMissing; |
| 596 |
int warn; |
| 597 |
int quiet; |
| 598 |
XSUM_U32 algoBitmask; |
| 599 |
ParseFileReport report; |
| 600 |
} ParseFileArg; |
| 601 |
|
| 602 |
|
| 603 |
/* |
| 604 |
* Reads a line from stream `inFile`. |
| 605 |
* Returns GetLine_ok, if it reads line successfully. |
| 606 |
* Returns GetLine_comment, if the line is beginning with '#'. |
| 607 |
* Returns GetLine_eof, if stream reaches EOF. |
| 608 |
* Returns GetLine_exceedMaxLineLength, if line length is longer than MAX_LINE_LENGTH. |
| 609 |
* Returns GetLine_outOfMemory, if line buffer memory allocation failed. |
| 610 |
*/ |
| 611 |
static GetLineResult XSUM_getLine(char** lineBuf, int* lineMax, FILE* inFile) |
| 612 |
{ |
| 613 |
GetLineResult result = GetLine_ok; |
| 614 |
size_t len = 0; |
| 615 |
|
| 616 |
if ((*lineBuf == NULL) || (*lineMax<1)) { |
| 617 |
free(*lineBuf); /* in case it's != NULL */ |
| 618 |
*lineMax = 0; |
| 619 |
*lineBuf = (char*)malloc(DEFAULT_LINE_LENGTH); |
| 620 |
if(*lineBuf == NULL) return GetLine_outOfMemory; |
| 621 |
*lineMax = DEFAULT_LINE_LENGTH; |
| 622 |
} |
| 623 |
|
| 624 |
for (;;) { |
| 625 |
const int c = fgetc(inFile); |
| 626 |
if (c == EOF) { |
| 627 |
/* |
| 628 |
* If we meet EOF before first character, returns GetLine_eof, |
| 629 |
* otherwise GetLine_ok. |
| 630 |
*/ |
| 631 |
if (len == 0) result = GetLine_eof; |
| 632 |
break; |
| 633 |
} |
| 634 |
|
| 635 |
/* Make enough space for len+1 (for final NUL) bytes. */ |
| 636 |
if (len+1 >= (size_t)*lineMax) { |
| 637 |
char* newLineBuf = NULL; |
| 638 |
size_t newBufSize = (size_t)*lineMax; |
| 639 |
|
| 640 |
newBufSize += (newBufSize/2) + 1; /* x 1.5 */ |
| 641 |
if (newBufSize > MAX_LINE_LENGTH) newBufSize = MAX_LINE_LENGTH; |
| 642 |
if (len+1 >= newBufSize) return GetLine_exceedMaxLineLength; |
| 643 |
|
| 644 |
newLineBuf = (char*) realloc(*lineBuf, newBufSize); |
| 645 |
if (newLineBuf == NULL) return GetLine_outOfMemory; |
| 646 |
|
| 647 |
*lineBuf = newLineBuf; |
| 648 |
*lineMax = (int)newBufSize; |
| 649 |
} |
| 650 |
|
| 651 |
if (c == '\n') break; |
| 652 |
(*lineBuf)[len++] = (char) c; |
| 653 |
} |
| 654 |
|
| 655 |
(*lineBuf)[len] = '\0'; |
| 656 |
|
| 657 |
/* Ignore comment lines, which begin with a '#' character. */ |
| 658 |
if (result == GetLine_ok && len > 0 && ((*lineBuf)[0] == '#')) { |
| 659 |
result = GetLine_comment; |
| 660 |
} |
| 661 |
|
| 662 |
return result; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
/* |
| 667 |
* Converts one hexadecimal character to integer. |
| 668 |
* Returns -1 if the given character is not hexadecimal. |
| 669 |
*/ |
| 670 |
static int charToHex(char c) |
| 671 |
{ |
| 672 |
int result = -1; |
| 673 |
if (c >= '0' && c <= '9') { |
| 674 |
result = (int) (c - '0'); |
| 675 |
} else if (c >= 'A' && c <= 'F') { |
| 676 |
result = (int) (c - 'A') + 0x0a; |
| 677 |
} else if (c >= 'a' && c <= 'f') { |
| 678 |
result = (int) (c - 'a') + 0x0a; |
| 679 |
} |
| 680 |
return result; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
/* |
| 685 |
* Converts canonical ASCII hexadecimal string `hashStr` |
| 686 |
* to the big endian binary representation in unsigned char array `dst`. |
| 687 |
* |
| 688 |
* Returns CanonicalFromString_invalidFormat if hashStr is not well formatted. |
| 689 |
* Returns CanonicalFromString_ok if hashStr is parsed successfully. |
| 690 |
*/ |
| 691 |
static CanonicalFromStringResult XSUM_canonicalFromString(unsigned char* dst, |
| 692 |
size_t dstSize, |
| 693 |
const char* hashStr, |
| 694 |
int reverseBytes) |
| 695 |
{ |
| 696 |
size_t i; |
| 697 |
for (i = 0; i < dstSize; ++i) { |
| 698 |
int h0, h1; |
| 699 |
size_t j = reverseBytes ? dstSize - i - 1 : i; |
| 700 |
|
| 701 |
h0 = charToHex(hashStr[j*2 + 0]); |
| 702 |
if (h0 < 0) return CanonicalFromString_invalidFormat; |
| 703 |
|
| 704 |
h1 = charToHex(hashStr[j*2 + 1]); |
| 705 |
if (h1 < 0) return CanonicalFromString_invalidFormat; |
| 706 |
|
| 707 |
dst[i] = (unsigned char) ((h0 << 4) | h1); |
| 708 |
} |
| 709 |
return CanonicalFromString_ok; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
/* |
| 714 |
* Parse single line of xxHash checksum file. |
| 715 |
* Returns ParseLine_invalidFormat if the line is not well formatted. |
| 716 |
* Returns ParseLine_ok if the line is parsed successfully. |
| 717 |
* And members of XSUM_parseLine will be filled by parsed values. |
| 718 |
* |
| 719 |
* - line must be terminated with '\0' without a trailing newline. |
| 720 |
* - Since parsedLine.filename will point within given argument `line`, |
| 721 |
* users must keep `line`s content when they are using parsedLine. |
| 722 |
* - The line may be modified to carve up the information it contains. |
| 723 |
* |
| 724 |
* xxHash checksum lines should have the following format: |
| 725 |
* |
| 726 |
* <8, 16, or 32 hexadecimal char> <space> <space> <filename...> <'\0'> |
| 727 |
* |
| 728 |
* or: |
| 729 |
* |
| 730 |
* <algorithm> <' ('> <filename> <') = '> <hexstring> <'\0'> |
| 731 |
*/ |
| 732 |
static ParseLineResult XSUM_parseLine1(ParsedLine* parsedLine, char* line, int rev, int needsUnescape, XSUM_U32 algoBitmask) |
| 733 |
{ |
| 734 |
char* const firstSpace = strchr(line, ' '); |
| 735 |
const char* hash_ptr; |
| 736 |
size_t hash_len; |
| 737 |
|
| 738 |
parsedLine->filename = NULL; |
| 739 |
parsedLine->algo = algo_xxh64; /* default - will be overwritten */ |
| 740 |
if (firstSpace == NULL || !firstSpace[1]) return ParseLine_invalidFormat; |
| 741 |
|
| 742 |
if (firstSpace[1] == '(') { |
| 743 |
char* lastSpace = strrchr(line, ' '); |
| 744 |
if (lastSpace - firstSpace < 5) return ParseLine_invalidFormat; |
| 745 |
if (lastSpace[-1] != '=' || lastSpace[-2] != ' ' || lastSpace[-3] != ')') return ParseLine_invalidFormat; |
| 746 |
lastSpace[-3] = '\0'; /* Terminate the filename */ |
| 747 |
*firstSpace = '\0'; |
| 748 |
rev = strstr(line, "_LE") != NULL; /* was output little-endian */ |
| 749 |
hash_ptr = lastSpace + 1; |
| 750 |
hash_len = strlen(hash_ptr); |
| 751 |
if (!memcmp(line, "XXH3", 4)) parsedLine->algo = algo_xxh3; |
| 752 |
if (!memcmp(line, "XXH32", 5)) parsedLine->algo = algo_xxh32; |
| 753 |
if (!memcmp(line, "XXH64", 5)) parsedLine->algo = algo_xxh64; |
| 754 |
if (!memcmp(line, "XXH128", 6)) parsedLine->algo = algo_xxh128; |
| 755 |
} else { |
| 756 |
hash_ptr = line; |
| 757 |
hash_len = (size_t)(firstSpace - line); |
| 758 |
if (hash_len==8) parsedLine->algo = algo_xxh32; |
| 759 |
if (hash_len==16) parsedLine->algo = algo_xxh64; |
| 760 |
if (hash_len==21) parsedLine->algo = algo_xxh3; |
| 761 |
if (hash_len==32) parsedLine->algo = algo_xxh128; |
| 762 |
} |
| 763 |
|
| 764 |
/* Check current CLI accepts the algorithm or not */ |
| 765 |
if(! XSUM_algoBitmask_Accepts(algoBitmask, parsedLine->algo)) { |
| 766 |
return ParseLine_invalidFormat; |
| 767 |
} |
| 768 |
|
| 769 |
switch (hash_len) |
| 770 |
{ |
| 771 |
case 8: |
| 772 |
if (parsedLine->algo != algo_xxh32) return ParseLine_invalidFormat; |
| 773 |
{ XXH32_canonical_t* xxh32c = &parsedLine->canonical.xxh32; |
| 774 |
if (XSUM_canonicalFromString(xxh32c->digest, sizeof(xxh32c->digest), hash_ptr, rev) |
| 775 |
!= CanonicalFromString_ok) { |
| 776 |
return ParseLine_invalidFormat; |
| 777 |
} |
| 778 |
break; |
| 779 |
} |
| 780 |
|
| 781 |
case 16: |
| 782 |
if (parsedLine->algo != algo_xxh64 && parsedLine->algo != algo_xxh3) return ParseLine_invalidFormat; |
| 783 |
{ XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64; |
| 784 |
if (XSUM_canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), hash_ptr, rev) |
| 785 |
!= CanonicalFromString_ok) { |
| 786 |
return ParseLine_invalidFormat; |
| 787 |
} |
| 788 |
break; |
| 789 |
} |
| 790 |
|
| 791 |
case 21: |
| 792 |
if (parsedLine->algo != algo_xxh3) return ParseLine_invalidFormat; |
| 793 |
/* check XXH3 prefix*/ |
| 794 |
if (memcmp(hash_ptr, "XXH3_", 5) != 0) return ParseLine_invalidFormat; |
| 795 |
{ XXH64_canonical_t* xxh64c = &parsedLine->canonical.xxh64; |
| 796 |
if (XSUM_canonicalFromString(xxh64c->digest, sizeof(xxh64c->digest), (const char*)hash_ptr+5, rev) |
| 797 |
!= CanonicalFromString_ok) { |
| 798 |
return ParseLine_invalidFormat; |
| 799 |
} |
| 800 |
break; |
| 801 |
} |
| 802 |
|
| 803 |
case 32: |
| 804 |
if (parsedLine->algo != algo_xxh128) return ParseLine_invalidFormat; |
| 805 |
{ XXH128_canonical_t* xxh128c = &parsedLine->canonical.xxh128; |
| 806 |
if (XSUM_canonicalFromString(xxh128c->digest, sizeof(xxh128c->digest), hash_ptr, rev) |
| 807 |
!= CanonicalFromString_ok) { |
| 808 |
return ParseLine_invalidFormat; |
| 809 |
} |
| 810 |
break; |
| 811 |
} |
| 812 |
|
| 813 |
default: |
| 814 |
return ParseLine_invalidFormat; |
| 815 |
break; |
| 816 |
} |
| 817 |
|
| 818 |
/* note : skipping second separation character, which can be anything, |
| 819 |
* allowing insertion of custom markers such as '*' */ |
| 820 |
{ |
| 821 |
char* const filename = firstSpace + 2; |
| 822 |
const size_t filenameLen = strlen(filename); |
| 823 |
if (needsUnescape) { |
| 824 |
char* const result = XSUM_filenameUnescape(filename, filenameLen); |
| 825 |
if (result == NULL) { |
| 826 |
return ParseLine_invalidFormat; |
| 827 |
} |
| 828 |
} |
| 829 |
parsedLine->filename = filename; |
| 830 |
} |
| 831 |
return ParseLine_ok; |
| 832 |
} |
| 833 |
|
| 834 |
static ParseLineResult XSUM_parseLine(ParsedLine* parsedLine, char* line, int rev, XSUM_U32 algoBitmask) { |
| 835 |
const int needsUnescape = XSUM_lineNeedsUnescape(line); |
| 836 |
if (needsUnescape) { |
| 837 |
++line; |
| 838 |
} |
| 839 |
return XSUM_parseLine1(parsedLine, line, rev, needsUnescape, algoBitmask); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
/*! |
| 845 |
* Parse xxHash checksum file. |
| 846 |
*/ |
| 847 |
static void XSUM_parseFile1(ParseFileArg* XSUM_parseFileArg, int rev) |
| 848 |
{ |
| 849 |
const char* const inFileName = XSUM_parseFileArg->inFileName; |
| 850 |
ParseFileReport* const report = &XSUM_parseFileArg->report; |
| 851 |
|
| 852 |
unsigned long lineNumber = 0; |
| 853 |
memset(report, 0, sizeof(*report)); |
| 854 |
|
| 855 |
while (!report->quit) { |
| 856 |
LineStatus lineStatus = LineStatus_hashFailed; |
| 857 |
ParsedLine parsedLine; |
| 858 |
memset(&parsedLine, 0, sizeof(parsedLine)); |
| 859 |
|
| 860 |
lineNumber++; |
| 861 |
if (lineNumber == 0) { |
| 862 |
/* This is unlikely happen, but md5sum.c has this error check. */ |
| 863 |
XSUM_log("%s: Error: Too many checksum lines\n", inFileName); |
| 864 |
report->quit = 1; |
| 865 |
break; |
| 866 |
} |
| 867 |
|
| 868 |
{ GetLineResult const XSUM_getLineResult = |
| 869 |
XSUM_getLine(&XSUM_parseFileArg->lineBuf, |
| 870 |
&XSUM_parseFileArg->lineMax, |
| 871 |
XSUM_parseFileArg->inFile); |
| 872 |
|
| 873 |
/* Ignore comment lines */ |
| 874 |
if (XSUM_getLineResult == GetLine_comment) { |
| 875 |
continue; |
| 876 |
} |
| 877 |
|
| 878 |
if (XSUM_getLineResult != GetLine_ok) { |
| 879 |
if (XSUM_getLineResult == GetLine_eof) break; |
| 880 |
|
| 881 |
switch (XSUM_getLineResult) |
| 882 |
{ |
| 883 |
case GetLine_ok: |
| 884 |
case GetLine_comment: |
| 885 |
case GetLine_eof: |
| 886 |
/* These cases never happen. See above XSUM_getLineResult related "if"s. |
| 887 |
They exist just for make gcc's -Wswitch-enum happy. */ |
| 888 |
assert(0); |
| 889 |
break; |
| 890 |
|
| 891 |
default: |
| 892 |
XSUM_log("%s:%lu: Error: Unknown error.\n", inFileName, lineNumber); |
| 893 |
break; |
| 894 |
|
| 895 |
case GetLine_exceedMaxLineLength: |
| 896 |
XSUM_log("%s:%lu: Error: Line too long.\n", inFileName, lineNumber); |
| 897 |
break; |
| 898 |
|
| 899 |
case GetLine_outOfMemory: |
| 900 |
XSUM_log("%s:%lu: Error: Out of memory.\n", inFileName, lineNumber); |
| 901 |
break; |
| 902 |
} |
| 903 |
report->quit = 1; |
| 904 |
break; |
| 905 |
} } |
| 906 |
|
| 907 |
if (XSUM_parseLine(&parsedLine, XSUM_parseFileArg->lineBuf, rev, XSUM_parseFileArg->algoBitmask) != ParseLine_ok) { |
| 908 |
report->nImproperlyFormattedLines++; |
| 909 |
if (XSUM_parseFileArg->warn) { |
| 910 |
XSUM_log("%s:%lu: Error: Improperly formatted checksum line.\n", |
| 911 |
inFileName, lineNumber); |
| 912 |
} |
| 913 |
continue; |
| 914 |
} |
| 915 |
|
| 916 |
report->nProperlyFormattedLines++; |
| 917 |
|
| 918 |
do { |
| 919 |
int const fnameIsStdin = (strcmp(parsedLine.filename, stdinFileName) == 0); /* "stdin" */ |
| 920 |
FILE* const fp = fnameIsStdin ? stdin : XSUM_fopen(parsedLine.filename, "rb"); |
| 921 |
if (fp == stdin) { |
| 922 |
XSUM_setBinaryMode(stdin); |
| 923 |
} |
| 924 |
if (fp == NULL) { |
| 925 |
lineStatus = LineStatus_failedToOpen; |
| 926 |
break; |
| 927 |
} |
| 928 |
lineStatus = LineStatus_hashFailed; |
| 929 |
{ Multihash const xxh = XSUM_hashStream(fp, parsedLine.algo, XSUM_parseFileArg->blockBuf, XSUM_parseFileArg->blockSize); |
| 930 |
switch (parsedLine.algo) |
| 931 |
{ |
| 932 |
case algo_xxh32: |
| 933 |
if (xxh.hash32 == XXH32_hashFromCanonical(&parsedLine.canonical.xxh32)) { |
| 934 |
lineStatus = LineStatus_hashOk; |
| 935 |
} |
| 936 |
break; |
| 937 |
|
| 938 |
case algo_xxh64: |
| 939 |
case algo_xxh3: |
| 940 |
if (xxh.hash64 == XXH64_hashFromCanonical(&parsedLine.canonical.xxh64)) { |
| 941 |
lineStatus = LineStatus_hashOk; |
| 942 |
} |
| 943 |
break; |
| 944 |
|
| 945 |
case algo_xxh128: |
| 946 |
if (XXH128_isEqual(xxh.hash128, XXH128_hashFromCanonical(&parsedLine.canonical.xxh128))) { |
| 947 |
lineStatus = LineStatus_hashOk; |
| 948 |
} |
| 949 |
break; |
| 950 |
|
| 951 |
default: |
| 952 |
break; |
| 953 |
} |
| 954 |
} |
| 955 |
if (fp != stdin) fclose(fp); |
| 956 |
} while (0); |
| 957 |
|
| 958 |
switch (lineStatus) |
| 959 |
{ |
| 960 |
default: |
| 961 |
XSUM_log("%s: Error: Unknown error.\n", inFileName); |
| 962 |
report->quit = 1; |
| 963 |
break; |
| 964 |
|
| 965 |
case LineStatus_memoryError: |
| 966 |
case LineStatus_isDirectory: |
| 967 |
assert(0); /* Never happens on these paths */ |
| 968 |
break; |
| 969 |
|
| 970 |
case LineStatus_failedToOpen: |
| 971 |
if (XSUM_parseFileArg->ignoreMissing) { |
| 972 |
report->nMissing++; |
| 973 |
} else { |
| 974 |
report->nOpenOrReadFailures++; |
| 975 |
if (!XSUM_parseFileArg->statusOnly) { |
| 976 |
XSUM_output("%s:%lu: Could not open or read '%s': %s.\n", |
| 977 |
inFileName, lineNumber, parsedLine.filename, strerror(errno)); |
| 978 |
} |
| 979 |
} |
| 980 |
break; |
| 981 |
|
| 982 |
case LineStatus_hashOk: |
| 983 |
case LineStatus_hashFailed: |
| 984 |
{ int b = 1; |
| 985 |
if (lineStatus == LineStatus_hashOk) { |
| 986 |
report->nMatchedChecksums++; |
| 987 |
/* If --quiet is specified, don't display "OK" */ |
| 988 |
if (XSUM_parseFileArg->quiet) b = 0; |
| 989 |
} else { |
| 990 |
report->nMismatchedChecksums++; |
| 991 |
} |
| 992 |
|
| 993 |
if (b && !XSUM_parseFileArg->statusOnly) { |
| 994 |
const int needsEscape = XSUM_filenameNeedsEscape(parsedLine.filename); |
| 995 |
if (needsEscape) { |
| 996 |
XSUM_output("%c", '\\'); |
| 997 |
} |
| 998 |
XSUM_printFilename(parsedLine.filename, needsEscape); |
| 999 |
XSUM_output(": %s\n", lineStatus == LineStatus_hashOk ? "OK" : "FAILED"); |
| 1000 |
} } |
| 1001 |
break; |
| 1002 |
} |
| 1003 |
} /* while (!report->quit) */ |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
/* Parse xxHash checksum file. |
| 1008 |
* Returns 1, if all procedures were succeeded. |
| 1009 |
* Returns 0, if any procedures was failed. |
| 1010 |
* |
| 1011 |
* If strictMode != 0, return error code if any line is invalid. |
| 1012 |
* If statusOnly != 0, don't generate any output. |
| 1013 |
* If ignoreMissing != 0, ignore missing file. But if no file was verified, returns 0 (failed). |
| 1014 |
* If warn != 0, print a warning message to stderr. |
| 1015 |
* If quiet != 0, suppress "OK" line. |
| 1016 |
* |
| 1017 |
* "All procedures are succeeded" means: |
| 1018 |
* - Checksum file contains at least one line and less than SIZE_T_MAX lines. |
| 1019 |
* - All files are properly opened and read. |
| 1020 |
* - All hash values match with its content. |
| 1021 |
* - (strict mode) All lines in checksum file are consistent and well formatted. |
| 1022 |
*/ |
| 1023 |
static int XSUM_checkFile(const char* inFileName, |
| 1024 |
const Display_endianness displayEndianness, |
| 1025 |
int strictMode, |
| 1026 |
int statusOnly, |
| 1027 |
int ignoreMissing, |
| 1028 |
int warn, |
| 1029 |
int quiet, |
| 1030 |
XSUM_U32 algoBitmask) |
| 1031 |
{ |
| 1032 |
int result = 0; |
| 1033 |
FILE* inFile = NULL; |
| 1034 |
ParseFileArg XSUM_parseFileArgBody; |
| 1035 |
ParseFileArg* const XSUM_parseFileArg = &XSUM_parseFileArgBody; |
| 1036 |
ParseFileReport* const report = &XSUM_parseFileArg->report; |
| 1037 |
|
| 1038 |
/* note: stdinName is special constant pointer. It is not a string. */ |
| 1039 |
if (inFileName == stdinName) { |
| 1040 |
/* |
| 1041 |
* Note: Since we expect text input for xxhash -c mode, |
| 1042 |
* we don't set binary mode for stdin. |
| 1043 |
*/ |
| 1044 |
inFileName = stdinFileName; /* "stdin" */ |
| 1045 |
inFile = stdin; |
| 1046 |
} else { |
| 1047 |
inFile = XSUM_fopen( inFileName, "rt" ); |
| 1048 |
} |
| 1049 |
|
| 1050 |
if (inFile == NULL) { |
| 1051 |
XSUM_log("Error: Could not open '%s': %s\n", inFileName, strerror(errno)); |
| 1052 |
return 0; |
| 1053 |
} |
| 1054 |
|
| 1055 |
XSUM_parseFileArg->inFileName = inFileName; |
| 1056 |
XSUM_parseFileArg->inFile = inFile; |
| 1057 |
XSUM_parseFileArg->lineMax = DEFAULT_LINE_LENGTH; |
| 1058 |
XSUM_parseFileArg->lineBuf = (char*) malloc((size_t)XSUM_parseFileArg->lineMax); |
| 1059 |
XSUM_parseFileArg->blockSize = 64 * 1024; |
| 1060 |
XSUM_parseFileArg->blockBuf = (char*) malloc(XSUM_parseFileArg->blockSize); |
| 1061 |
XSUM_parseFileArg->strictMode = strictMode; |
| 1062 |
XSUM_parseFileArg->statusOnly = statusOnly; |
| 1063 |
XSUM_parseFileArg->ignoreMissing = ignoreMissing; |
| 1064 |
XSUM_parseFileArg->warn = warn; |
| 1065 |
XSUM_parseFileArg->quiet = quiet; |
| 1066 |
XSUM_parseFileArg->algoBitmask = algoBitmask; |
| 1067 |
|
| 1068 |
if ( (XSUM_parseFileArg->lineBuf == NULL) |
| 1069 |
|| (XSUM_parseFileArg->blockBuf == NULL) ) { |
| 1070 |
XSUM_log("Error: : memory allocation failed \n"); |
| 1071 |
exit(1); |
| 1072 |
} |
| 1073 |
XSUM_parseFile1(XSUM_parseFileArg, displayEndianness != big_endian); |
| 1074 |
|
| 1075 |
free(XSUM_parseFileArg->blockBuf); |
| 1076 |
free(XSUM_parseFileArg->lineBuf); |
| 1077 |
|
| 1078 |
if (inFile != stdin) fclose(inFile); |
| 1079 |
|
| 1080 |
/* Show error/warning messages. All messages are copied from md5sum.c |
| 1081 |
*/ |
| 1082 |
if (report->nProperlyFormattedLines == 0) { |
| 1083 |
XSUM_log("%s: no properly formatted xxHash checksum lines found\n", inFileName); |
| 1084 |
} else if (!statusOnly) { |
| 1085 |
if (report->nImproperlyFormattedLines) { |
| 1086 |
XSUM_output("%lu %s improperly formatted\n" |
| 1087 |
, report->nImproperlyFormattedLines |
| 1088 |
, report->nImproperlyFormattedLines == 1 ? "line is" : "lines are"); |
| 1089 |
} |
| 1090 |
if (report->nOpenOrReadFailures) { |
| 1091 |
XSUM_output("%lu listed %s could not be read\n" |
| 1092 |
, report->nOpenOrReadFailures |
| 1093 |
, report->nOpenOrReadFailures == 1 ? "file" : "files"); |
| 1094 |
} |
| 1095 |
if (report->nMismatchedChecksums) { |
| 1096 |
XSUM_output("%lu computed %s did NOT match\n" |
| 1097 |
, report->nMismatchedChecksums |
| 1098 |
, report->nMismatchedChecksums == 1 ? "checksum" : "checksums"); |
| 1099 |
} } |
| 1100 |
|
| 1101 |
/* Result (exit) code logic is copied from |
| 1102 |
* gnu coreutils/src/md5sum.c digest_check() */ |
| 1103 |
result = report->nProperlyFormattedLines != 0 |
| 1104 |
&& report->nMismatchedChecksums == 0 |
| 1105 |
&& report->nOpenOrReadFailures == 0 |
| 1106 |
&& (!strictMode || report->nImproperlyFormattedLines == 0) |
| 1107 |
&& report->quit == 0; |
| 1108 |
|
| 1109 |
/* If "--ignore-missing" is enabled and there's no matched checksum, report it as error. |
| 1110 |
* See https://github.com/coreutils/coreutils/blob/2f1cffe07ab0f0b4135a52d95f1689d7fc7f26c9/src/digest.c#L1325-L1328 */ |
| 1111 |
if (ignoreMissing && report->nMatchedChecksums == 0) { |
| 1112 |
XSUM_output("%s: no file was verified\n", inFileName); |
| 1113 |
result = 0; |
| 1114 |
} |
| 1115 |
|
| 1116 |
return result; |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
static int XSUM_checkFiles(const char* fnList[], int fnTotal, |
| 1121 |
const Display_endianness displayEndianness, |
| 1122 |
int strictMode, |
| 1123 |
int statusOnly, |
| 1124 |
int ignoreMissing, |
| 1125 |
int warn, |
| 1126 |
int quiet, |
| 1127 |
XSUM_U32 algoBitmask) |
| 1128 |
{ |
| 1129 |
int ok = 1; |
| 1130 |
|
| 1131 |
/* Special case for stdinName "-", |
| 1132 |
* note: stdinName is not a string. It's special pointer. */ |
| 1133 |
if (fnTotal==0) { |
| 1134 |
ok &= XSUM_checkFile(stdinName, displayEndianness, strictMode, statusOnly, ignoreMissing, warn, quiet, algoBitmask); |
| 1135 |
} else { |
| 1136 |
int fnNb; |
| 1137 |
for (fnNb=0; fnNb<fnTotal; fnNb++) |
| 1138 |
ok &= XSUM_checkFile(fnList[fnNb], displayEndianness, strictMode, statusOnly, ignoreMissing, warn, quiet, algoBitmask); |
| 1139 |
} |
| 1140 |
return ok ? 0 : 1; |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
/* |
| 1145 |
* |
| 1146 |
* Parse single filename from list to generate hashes for. |
| 1147 |
* Returns ParseLine_invalidFormat if the filename is not well formatted. |
| 1148 |
* Returns ParseLine_ok if the filename is parsed successfully. |
| 1149 |
*/ |
| 1150 |
static ParseLineResult XSUM_parseGenLine(ParsedLine * parsedLine, |
| 1151 |
char* filename) |
| 1152 |
{ |
| 1153 |
if (XSUM_lineNeedsUnescape(filename)) { |
| 1154 |
size_t filenameLen; |
| 1155 |
++filename; |
| 1156 |
filenameLen = strlen(filename); |
| 1157 |
|
| 1158 |
if (XSUM_filenameUnescape(filename, filenameLen) == NULL) { |
| 1159 |
parsedLine->filename = NULL; |
| 1160 |
return ParseLine_invalidFormat; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
parsedLine->filename = filename; |
| 1165 |
|
| 1166 |
return ParseLine_ok; |
| 1167 |
} |
| 1168 |
|
| 1169 |
/* |
| 1170 |
* Parse gen source file. |
| 1171 |
*/ |
| 1172 |
static void XSUM_parseGenFile1(ParseFileArg* XSUM_parseGenArg, |
| 1173 |
AlgoSelected hashType, |
| 1174 |
Display_endianness displayEndianness, |
| 1175 |
Display_convention convention) |
| 1176 |
{ |
| 1177 |
const char* const inFileName = XSUM_parseGenArg->inFileName; |
| 1178 |
ParseFileReport* const report = &XSUM_parseGenArg->report; |
| 1179 |
|
| 1180 |
unsigned long lineNumber = 0; |
| 1181 |
memset(report, 0, sizeof(*report)); |
| 1182 |
|
| 1183 |
while (!report->quit) { |
| 1184 |
LineStatus lineStatus = LineStatus_hashFailed; |
| 1185 |
ParsedLine parsedLine; |
| 1186 |
memset(&parsedLine, 0, sizeof(parsedLine)); |
| 1187 |
|
| 1188 |
lineNumber++; |
| 1189 |
if (lineNumber == 0) { |
| 1190 |
/* This is unlikely happen, but md5sum.c has this error check. */ |
| 1191 |
XSUM_log("%s: Error: Too many generate lines\n", inFileName); |
| 1192 |
report->quit = 1; |
| 1193 |
break; |
| 1194 |
} |
| 1195 |
|
| 1196 |
{ GetLineResult const XSUM_getLineResult = XSUM_getLine(&XSUM_parseGenArg->lineBuf, |
| 1197 |
&XSUM_parseGenArg->lineMax, |
| 1198 |
XSUM_parseGenArg->inFile); |
| 1199 |
|
| 1200 |
/* Ignore comment lines */ |
| 1201 |
if (XSUM_getLineResult == GetLine_comment) { |
| 1202 |
continue; |
| 1203 |
} |
| 1204 |
|
| 1205 |
if (XSUM_getLineResult != GetLine_ok) { |
| 1206 |
if (XSUM_getLineResult == GetLine_eof) break; |
| 1207 |
|
| 1208 |
switch (XSUM_getLineResult) |
| 1209 |
{ |
| 1210 |
case GetLine_ok: |
| 1211 |
case GetLine_comment: |
| 1212 |
case GetLine_eof: |
| 1213 |
/* These cases never happen. See above XSUM_getLineResult related "if"s. |
| 1214 |
They exist just for make gcc's -Wswitch-enum happy. */ |
| 1215 |
assert(0); |
| 1216 |
break; |
| 1217 |
|
| 1218 |
default: |
| 1219 |
XSUM_log("%s:%lu: Error: Unknown error.\n", inFileName, lineNumber); |
| 1220 |
break; |
| 1221 |
|
| 1222 |
case GetLine_exceedMaxLineLength: |
| 1223 |
XSUM_log("%s:%lu: Error: Line too long.\n", inFileName, lineNumber); |
| 1224 |
break; |
| 1225 |
|
| 1226 |
case GetLine_outOfMemory: |
| 1227 |
XSUM_log("%s:%lu: Error: Out of memory.\n", inFileName, lineNumber); |
| 1228 |
break; |
| 1229 |
} |
| 1230 |
report->quit = 1; |
| 1231 |
break; |
| 1232 |
} } |
| 1233 |
|
| 1234 |
if (XSUM_parseGenLine(&parsedLine, XSUM_parseGenArg->lineBuf) != ParseLine_ok) { |
| 1235 |
report->nImproperlyFormattedLines++; |
| 1236 |
if (XSUM_parseGenArg->warn) { |
| 1237 |
XSUM_log("%s:%lu: Error: Improperly formatted line.\n", |
| 1238 |
inFileName, lineNumber); |
| 1239 |
} |
| 1240 |
continue; |
| 1241 |
} |
| 1242 |
|
| 1243 |
report->nProperlyFormattedLines++; |
| 1244 |
|
| 1245 |
lineStatus = XSUM_hashFile(parsedLine.filename, hashType, displayEndianness, convention); |
| 1246 |
|
| 1247 |
switch (lineStatus) |
| 1248 |
{ |
| 1249 |
default: |
| 1250 |
XSUM_log("%s: Error: Unknown error.\n", parsedLine.filename); |
| 1251 |
report->quit = 1; |
| 1252 |
break; |
| 1253 |
|
| 1254 |
case LineStatus_memoryError: |
| 1255 |
XSUM_log("\nError: Out of memory.\n"); |
| 1256 |
break; |
| 1257 |
|
| 1258 |
case LineStatus_failedToOpen: |
| 1259 |
case LineStatus_isDirectory: |
| 1260 |
if (XSUM_parseGenArg->ignoreMissing) { |
| 1261 |
report->nMissing++; |
| 1262 |
} |
| 1263 |
else { |
| 1264 |
report->nOpenOrReadFailures++; |
| 1265 |
if (!XSUM_parseGenArg->statusOnly) { |
| 1266 |
XSUM_output( |
| 1267 |
lineStatus == LineStatus_failedToOpen ? |
| 1268 |
"%s:%lu: Could not open or read '%s': %s.\n" : |
| 1269 |
"%s:%lu: Target is a directory '%s'.\n", /* Leaves errno argument unconsumed */ |
| 1270 |
inFileName, lineNumber, parsedLine.filename, strerror(errno)); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
break; |
| 1274 |
|
| 1275 |
case LineStatus_hashOk: |
| 1276 |
case LineStatus_hashFailed: |
| 1277 |
break; |
| 1278 |
} |
| 1279 |
} /* while (!report->quit) */ |
| 1280 |
} |
| 1281 |
|
| 1282 |
|
| 1283 |
/* Parse text file for list of targets. |
| 1284 |
*/ |
| 1285 |
static int XSUM_generateFile(const char* inFileName, |
| 1286 |
AlgoSelected hashType, |
| 1287 |
Display_endianness displayEndianness, |
| 1288 |
Display_convention convention, |
| 1289 |
int statusOnly, |
| 1290 |
int ignoreMissing, |
| 1291 |
int warn) |
| 1292 |
{ |
| 1293 |
int result = 0; |
| 1294 |
FILE* inFile = NULL; |
| 1295 |
ParseFileArg XSUM_parseGenArgBody; |
| 1296 |
ParseFileArg* const XSUM_parseGenArg = &XSUM_parseGenArgBody; |
| 1297 |
ParseFileReport* const report = &XSUM_parseGenArg->report; |
| 1298 |
|
| 1299 |
/* note: stdinName is special constant pointer. It is not a string. */ |
| 1300 |
if (inFileName == stdinName) { |
| 1301 |
/* |
| 1302 |
* Note: Since we expect text input for xxhash -c mode, |
| 1303 |
* we don't set binary mode for stdin. |
| 1304 |
*/ |
| 1305 |
inFileName = stdinFileName; /* "stdin" */ |
| 1306 |
inFile = stdin; |
| 1307 |
} |
| 1308 |
else { |
| 1309 |
inFile = XSUM_fopen(inFileName, "rt"); |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (inFile == NULL) { |
| 1313 |
XSUM_log("Error: Could not open '%s': %s\n", inFileName, strerror(errno)); |
| 1314 |
return 0; |
| 1315 |
} |
| 1316 |
|
| 1317 |
XSUM_parseGenArg->inFileName = inFileName; |
| 1318 |
XSUM_parseGenArg->inFile = inFile; |
| 1319 |
XSUM_parseGenArg->lineMax = DEFAULT_LINE_LENGTH; |
| 1320 |
XSUM_parseGenArg->lineBuf = (char*)malloc((size_t)XSUM_parseGenArg->lineMax); |
| 1321 |
XSUM_parseGenArg->blockSize = 64 * 1024; |
| 1322 |
XSUM_parseGenArg->blockBuf = (char*)malloc(XSUM_parseGenArg->blockSize); |
| 1323 |
XSUM_parseGenArg->statusOnly = statusOnly; |
| 1324 |
XSUM_parseGenArg->ignoreMissing = ignoreMissing; |
| 1325 |
XSUM_parseGenArg->warn = warn; |
| 1326 |
|
| 1327 |
if ((XSUM_parseGenArg->lineBuf == NULL) |
| 1328 |
|| (XSUM_parseGenArg->blockBuf == NULL)) { |
| 1329 |
XSUM_log("Error: : memory allocation failed \n"); |
| 1330 |
exit(1); |
| 1331 |
} |
| 1332 |
XSUM_parseGenFile1(XSUM_parseGenArg, hashType, displayEndianness, convention); |
| 1333 |
|
| 1334 |
free(XSUM_parseGenArg->blockBuf); |
| 1335 |
free(XSUM_parseGenArg->lineBuf); |
| 1336 |
|
| 1337 |
if (inFile != stdin) fclose(inFile); |
| 1338 |
|
| 1339 |
/* Show error/warning messages. All messages are copied from md5sum.c |
| 1340 |
*/ |
| 1341 |
if (report->nProperlyFormattedLines == 0) { |
| 1342 |
XSUM_log("%s: no properly formatted filename lines found\n", inFileName); |
| 1343 |
} |
| 1344 |
if (report->nImproperlyFormattedLines) { |
| 1345 |
XSUM_output("%lu %s improperly formatted\n" |
| 1346 |
, report->nImproperlyFormattedLines |
| 1347 |
, report->nImproperlyFormattedLines == 1 ? "line is" : "lines are"); |
| 1348 |
} |
| 1349 |
if (report->nOpenOrReadFailures) { |
| 1350 |
XSUM_output("%lu listed %s could not be read\n" |
| 1351 |
, report->nOpenOrReadFailures |
| 1352 |
, report->nOpenOrReadFailures == 1 ? "file" : "files"); |
| 1353 |
} |
| 1354 |
/* Result (exit) code logic is copied from |
| 1355 |
* gnu coreutils/src/md5sum.c digest_check() */ |
| 1356 |
result = report->nProperlyFormattedLines != 0 |
| 1357 |
&& report->nOpenOrReadFailures == 0 |
| 1358 |
&& (report->nImproperlyFormattedLines == 0) |
| 1359 |
&& report->quit == 0; |
| 1360 |
|
| 1361 |
return result; |
| 1362 |
} |
| 1363 |
|
| 1364 |
static int XSUM_generateFiles(const char* fnList[], int fnTotal, |
| 1365 |
AlgoSelected hashType, |
| 1366 |
Display_endianness displayEndianness, |
| 1367 |
Display_convention convention, |
| 1368 |
int statusOnly, |
| 1369 |
int ignoreMissing, |
| 1370 |
int warn) |
| 1371 |
{ |
| 1372 |
int ok = 1; |
| 1373 |
|
| 1374 |
/* Special case for stdinName "-", |
| 1375 |
* note: stdinName is not a string. It's special pointer. */ |
| 1376 |
if (fnTotal == 0) { |
| 1377 |
ok &= XSUM_generateFile(stdinName, hashType, displayEndianness, convention, statusOnly, ignoreMissing, warn); |
| 1378 |
} |
| 1379 |
else { |
| 1380 |
int fnNb; |
| 1381 |
for (fnNb = 0; fnNb < fnTotal; fnNb++) |
| 1382 |
ok &= XSUM_generateFile(fnList[fnNb], hashType, displayEndianness, convention, statusOnly, ignoreMissing, warn); |
| 1383 |
} |
| 1384 |
return ok ? 0 : 1; |
| 1385 |
} |
| 1386 |
|
| 1387 |
|
| 1388 |
/* ******************************************************** |
| 1389 |
* Main |
| 1390 |
**********************************************************/ |
| 1391 |
|
| 1392 |
static int XSUM_usage(const char* exename) |
| 1393 |
{ |
| 1394 |
XSUM_log( WELCOME_MESSAGE(exename) ); |
| 1395 |
XSUM_log( "Create or verify checksums using fast non-cryptographic algorithm xxHash \n\n" ); |
| 1396 |
XSUM_log( "Usage: %s [options] [files] \n\n", exename); |
| 1397 |
XSUM_log( "When no filename provided or when '-' is provided, uses stdin as input. \n"); |
| 1398 |
XSUM_log( "\nOptions: \n"); |
| 1399 |
XSUM_log( " -H# select an xxhash algorithm (default: %i) \n", (int)g_defaultAlgo); |
| 1400 |
XSUM_log( " 0: XXH32 \n"); |
| 1401 |
XSUM_log( " 1: XXH64 \n"); |
| 1402 |
XSUM_log( " 2: XXH128 (also called XXH3_128bits) \n"); |
| 1403 |
XSUM_log( " 3: XXH3 (also called XXH3_64bits) \n"); |
| 1404 |
XSUM_log( " -c, --check read xxHash checksum from [files] and check them \n"); |
| 1405 |
XSUM_log( " --filelist generate hashes for files listed in [files] \n"); |
| 1406 |
XSUM_log( " -h, --help display a long help page about advanced options \n"); |
| 1407 |
return 0; |
| 1408 |
} |
| 1409 |
|
| 1410 |
|
| 1411 |
static int XSUM_usage_advanced(const char* exename) |
| 1412 |
{ |
| 1413 |
XSUM_usage(exename); |
| 1414 |
XSUM_log( "\nAdvanced :\n"); |
| 1415 |
XSUM_log( " -V, --version Display version information \n"); |
| 1416 |
XSUM_log( " --tag Produce BSD-style checksum lines \n"); |
| 1417 |
XSUM_log( " --little-endian Checksum values use little endian convention (default: big endian) \n"); |
| 1418 |
XSUM_log( " --binary Read in binary mode \n"); |
| 1419 |
XSUM_log( " -b Run benchmark \n"); |
| 1420 |
XSUM_log( " -b# Bench only algorithm variant # \n"); |
| 1421 |
XSUM_log( " -i# Number of times to run the benchmark (default: %i) \n", NBLOOPS_DEFAULT); |
| 1422 |
XSUM_log( " -q, --quiet Don't display version header in benchmark mode \n"); |
| 1423 |
XSUM_log( "\n"); |
| 1424 |
XSUM_log( "The following five options are useful only when using lists in [files] to verify or generate checksums: \n"); |
| 1425 |
XSUM_log( " -q, --quiet Don't print OK for each successfully verified hash \n"); |
| 1426 |
XSUM_log( " --status Don't output anything, status code shows success \n"); |
| 1427 |
XSUM_log( " --strict Exit non-zero for improperly formatted lines in [files] \n"); |
| 1428 |
XSUM_log( " --warn Warn about improperly formatted lines in [files] \n"); |
| 1429 |
XSUM_log( " --ignore-missing Don't fail or report status for missing files \n"); |
| 1430 |
return 0; |
| 1431 |
} |
| 1432 |
|
| 1433 |
static int XSUM_badusage(const char* exename) |
| 1434 |
{ |
| 1435 |
XSUM_log("Wrong parameters\n\n"); |
| 1436 |
XSUM_usage(exename); |
| 1437 |
return 1; |
| 1438 |
} |
| 1439 |
|
| 1440 |
static void errorOut(const char* msg) |
| 1441 |
{ |
| 1442 |
XSUM_log("%s \n", msg); |
| 1443 |
exit(1); |
| 1444 |
} |
| 1445 |
|
| 1446 |
static const char* XSUM_lastNameFromPath(const char* path) |
| 1447 |
{ |
| 1448 |
const char* name = path; |
| 1449 |
if (strrchr(name, '/')) name = strrchr(name, '/') + 1; |
| 1450 |
if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1; /* windows */ |
| 1451 |
return name; |
| 1452 |
} |
| 1453 |
|
| 1454 |
/*! |
| 1455 |
* XSUM_readU32FromCharChecked(): |
| 1456 |
* @return 0 if success, and store the result in *value. |
| 1457 |
* Allows and interprets K, KB, KiB, M, MB and MiB suffix. |
| 1458 |
* Will also modify `*stringPtr`, advancing it to position where it stopped reading. |
| 1459 |
* @return 1 if an overflow error occurs |
| 1460 |
*/ |
| 1461 |
static int XSUM_readU32FromCharChecked(const char** stringPtr, XSUM_U32* value) |
| 1462 |
{ |
| 1463 |
static const XSUM_U32 max = (((XSUM_U32)(-1)) / 10) - 1; |
| 1464 |
XSUM_U32 result = 0; |
| 1465 |
while ((**stringPtr >='0') && (**stringPtr <='9')) { |
| 1466 |
if (result > max) return 1; /* overflow error */ |
| 1467 |
result *= 10; |
| 1468 |
result += (XSUM_U32)(**stringPtr - '0'); |
| 1469 |
(*stringPtr)++ ; |
| 1470 |
} |
| 1471 |
if ((**stringPtr=='K') || (**stringPtr=='M')) { |
| 1472 |
XSUM_U32 const maxK = ((XSUM_U32)(-1)) >> 10; |
| 1473 |
if (result > maxK) return 1; /* overflow error */ |
| 1474 |
result <<= 10; |
| 1475 |
if (**stringPtr=='M') { |
| 1476 |
if (result > maxK) return 1; /* overflow error */ |
| 1477 |
result <<= 10; |
| 1478 |
} |
| 1479 |
(*stringPtr)++; /* skip `K` or `M` */ |
| 1480 |
if (**stringPtr=='i') (*stringPtr)++; |
| 1481 |
if (**stringPtr=='B') (*stringPtr)++; |
| 1482 |
} |
| 1483 |
*value = result; |
| 1484 |
return 0; |
| 1485 |
} |
| 1486 |
|
| 1487 |
/*! |
| 1488 |
* XSUM_readU32FromChar(): |
| 1489 |
* @return: unsigned integer value read from input in `char` format. |
| 1490 |
* allows and interprets K, KB, KiB, M, MB and MiB suffix. |
| 1491 |
* Will also modify `*stringPtr`, advancing it to position where it stopped reading. |
| 1492 |
* Note: function will exit() program if digit sequence overflows |
| 1493 |
*/ |
| 1494 |
static XSUM_U32 XSUM_readU32FromChar(const char** stringPtr) { |
| 1495 |
XSUM_U32 result; |
| 1496 |
if (XSUM_readU32FromCharChecked(stringPtr, &result)) { |
| 1497 |
static const char errorMsg[] = "Error: numeric value too large"; |
| 1498 |
errorOut(errorMsg); |
| 1499 |
} |
| 1500 |
return result; |
| 1501 |
} |
| 1502 |
|
| 1503 |
XSUM_API int XSUM_main(int argc, const char* argv[]) |
| 1504 |
{ |
| 1505 |
int i, filenamesStart = 0; |
| 1506 |
const char* const exename = XSUM_lastNameFromPath(argv[0]); |
| 1507 |
int benchmarkMode = 0; |
| 1508 |
int fileCheckMode = 0; |
| 1509 |
int readFilenamesMode = 0; |
| 1510 |
int strictMode = 0; |
| 1511 |
int statusOnly = 0; |
| 1512 |
int warn = 0; |
| 1513 |
int ignoreMissing = 0; |
| 1514 |
XSUM_U32 algoBitmask = algo_bitmask_all; |
| 1515 |
int explicitStdin = 0; |
| 1516 |
XSUM_U32 selectBenchIDs= 0; /* 0 == use default k_testIDs_default, kBenchAll == bench all */ |
| 1517 |
static const XSUM_U32 kBenchAll = 99; |
| 1518 |
size_t keySize = XSUM_DEFAULT_SAMPLE_SIZE; |
| 1519 |
AlgoSelected algo = g_defaultAlgo; |
| 1520 |
Display_endianness displayEndianness = big_endian; |
| 1521 |
Display_convention convention = display_gnu; |
| 1522 |
int nbIterations = NBLOOPS_DEFAULT; |
| 1523 |
|
| 1524 |
/* special case: xxhNNsum default to NN bits checksum */ |
| 1525 |
if (strstr(exename, "xxh32sum") != NULL) { algo = g_defaultAlgo = algo_xxh32; algoBitmask = algo_bitmask_xxh32; } |
| 1526 |
if (strstr(exename, "xxh64sum") != NULL) { algo = g_defaultAlgo = algo_xxh64; algoBitmask = algo_bitmask_xxh64; } |
| 1527 |
if (strstr(exename, "xxh128sum") != NULL) { algo = g_defaultAlgo = algo_xxh128; algoBitmask = algo_bitmask_xxh128; } |
| 1528 |
if (strstr(exename, "xxh3sum") != NULL) { algo = g_defaultAlgo = algo_xxh3; algoBitmask = algo_bitmask_xxh3; } |
| 1529 |
|
| 1530 |
for (i=1; i<argc; i++) { |
| 1531 |
const char* argument = argv[i]; |
| 1532 |
assert(argument != NULL); |
| 1533 |
|
| 1534 |
if (!strcmp(argument, "--check")) { fileCheckMode = 1; continue; } |
| 1535 |
if (!strcmp(argument, "--files-from")) { readFilenamesMode = 1; continue; } |
| 1536 |
if (!strcmp(argument, "--filelist")) { readFilenamesMode = 1; continue; } |
| 1537 |
if (!strcmp(argument, "--benchmark-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; } |
| 1538 |
if (!strcmp(argument, "--bench-all")) { benchmarkMode = 1; selectBenchIDs = kBenchAll; continue; } |
| 1539 |
if (!strcmp(argument, "--quiet")) { XSUM_logLevel--; continue; } |
| 1540 |
if (!strcmp(argument, "--little-endian")) { displayEndianness = little_endian; continue; } |
| 1541 |
if (!strcmp(argument, "--strict")) { strictMode = 1; continue; } |
| 1542 |
if (!strcmp(argument, "--status")) { statusOnly = 1; continue; } |
| 1543 |
if (!strcmp(argument, "--warn")) { warn = 1; continue; } |
| 1544 |
if (!strcmp(argument, "--binary")) { continue; } /* Just ignore it. See https://github.com/Cyan4973/xxHash/issues/812 */ |
| 1545 |
if (!strcmp(argument, "--ignore-missing")) { ignoreMissing = 1; continue; } |
| 1546 |
if (!strcmp(argument, "--help")) { return XSUM_usage_advanced(exename); } |
| 1547 |
if (!strcmp(argument, "--version")) { XSUM_log(FULL_WELCOME_MESSAGE(exename)); XSUM_sanityCheck(); return 0; } |
| 1548 |
if (!strcmp(argument, "--tag")) { convention = display_bsd; continue; } |
| 1549 |
|
| 1550 |
if (!strcmp(argument, "--")) { |
| 1551 |
if (filenamesStart==0 && i!=argc-1) filenamesStart=i+1; /* only supports a continuous list of filenames */ |
| 1552 |
break; /* treat rest of arguments as strictly file names */ |
| 1553 |
} |
| 1554 |
if (*argument != '-') { |
| 1555 |
if (filenamesStart==0) filenamesStart=i; /* only supports a continuous list of filenames */ |
| 1556 |
break; /* treat rest of arguments as strictly file names */ |
| 1557 |
} |
| 1558 |
|
| 1559 |
/* command selection */ |
| 1560 |
argument++; /* note: argument[0] was =='-' */ |
| 1561 |
if (*argument == 0) explicitStdin = 1; |
| 1562 |
|
| 1563 |
while (*argument != 0) { |
| 1564 |
switch(*argument) |
| 1565 |
{ |
| 1566 |
/* Display version */ |
| 1567 |
case 'V': |
| 1568 |
XSUM_log(FULL_WELCOME_MESSAGE(exename)); |
| 1569 |
XSUM_sanityCheck(); |
| 1570 |
return 0; |
| 1571 |
|
| 1572 |
/* Display help on XSUM_usage */ |
| 1573 |
case 'h': |
| 1574 |
return XSUM_usage_advanced(exename); |
| 1575 |
|
| 1576 |
/* select hash algorithm */ |
| 1577 |
case 'H': argument++; |
| 1578 |
switch(XSUM_readU32FromChar(&argument)) { |
| 1579 |
case 0 : |
| 1580 |
case 32: algo = algo_xxh32; break; |
| 1581 |
case 1 : |
| 1582 |
case 64: algo = algo_xxh64; break; |
| 1583 |
case 2 : |
| 1584 |
case 128: algo = algo_xxh128; break; |
| 1585 |
case 3 : |
| 1586 |
algo = algo_xxh3; break; |
| 1587 |
default: |
| 1588 |
return XSUM_badusage(exename); |
| 1589 |
} |
| 1590 |
break; |
| 1591 |
|
| 1592 |
/* File check mode */ |
| 1593 |
case 'c': |
| 1594 |
fileCheckMode=1; |
| 1595 |
argument++; |
| 1596 |
break; |
| 1597 |
|
| 1598 |
/* Generate hash mode (tar style short form of --files-from) |
| 1599 |
case 'T': |
| 1600 |
readFilenamesMode = 2; |
| 1601 |
argument++; |
| 1602 |
break; */ |
| 1603 |
|
| 1604 |
/* Warning mode (file check mode only, alias of "--warning") */ |
| 1605 |
case 'w': |
| 1606 |
warn=1; |
| 1607 |
argument++; |
| 1608 |
break; |
| 1609 |
|
| 1610 |
/* Trigger benchmark mode */ |
| 1611 |
case 'b': |
| 1612 |
argument++; |
| 1613 |
benchmarkMode = 1; |
| 1614 |
do { |
| 1615 |
if (*argument == ',') argument++; |
| 1616 |
selectBenchIDs = XSUM_readU32FromChar(&argument); /* select one specific test */ |
| 1617 |
if ((int)selectBenchIDs < g_nbTestFunctions) { |
| 1618 |
g_testIDs[selectBenchIDs] = 1; |
| 1619 |
} else { |
| 1620 |
selectBenchIDs = kBenchAll; |
| 1621 |
} |
| 1622 |
} while (*argument == ','); |
| 1623 |
break; |
| 1624 |
|
| 1625 |
/* Modify Nb Iterations (benchmark only) */ |
| 1626 |
case 'i': |
| 1627 |
argument++; |
| 1628 |
nbIterations = (int)XSUM_readU32FromChar(&argument); |
| 1629 |
break; |
| 1630 |
|
| 1631 |
/* Modify Block size (benchmark only) */ |
| 1632 |
case 'B': |
| 1633 |
argument++; |
| 1634 |
keySize = XSUM_readU32FromChar(&argument); |
| 1635 |
break; |
| 1636 |
|
| 1637 |
/* Modify verbosity of benchmark output (hidden option) */ |
| 1638 |
case 'q': |
| 1639 |
argument++; |
| 1640 |
XSUM_logLevel--; |
| 1641 |
break; |
| 1642 |
|
| 1643 |
default: |
| 1644 |
return XSUM_badusage(exename); |
| 1645 |
} |
| 1646 |
} |
| 1647 |
} /* for(i=1; i<argc; i++) */ |
| 1648 |
|
| 1649 |
/* Check benchmark mode */ |
| 1650 |
if (benchmarkMode) { |
| 1651 |
XSUM_logVerbose(2, FULL_WELCOME_MESSAGE(exename) ); |
| 1652 |
XSUM_sanityCheck(); |
| 1653 |
g_nbIterations = nbIterations; |
| 1654 |
if (selectBenchIDs == 0) memcpy(g_testIDs, k_testIDs_default, (size_t)g_nbTestFunctions); |
| 1655 |
if (selectBenchIDs == kBenchAll) memset(g_testIDs, 1, (size_t)g_nbTestFunctions); |
| 1656 |
if (filenamesStart==0) return XSUM_benchInternal(keySize); |
| 1657 |
return XSUM_benchFiles(argv+filenamesStart, argc-filenamesStart); |
| 1658 |
} |
| 1659 |
|
| 1660 |
/* Check if input is defined as console; trigger an error in this case */ |
| 1661 |
if ( (filenamesStart==0) && XSUM_isConsole(stdin) && !explicitStdin) |
| 1662 |
return XSUM_badusage(exename); |
| 1663 |
|
| 1664 |
if (filenamesStart==0) filenamesStart = argc; |
| 1665 |
|
| 1666 |
if (fileCheckMode) { |
| 1667 |
return XSUM_checkFiles(argv+filenamesStart, argc-filenamesStart, |
| 1668 |
displayEndianness, strictMode, statusOnly, ignoreMissing, warn, (XSUM_logLevel < 2) /*quiet*/, algoBitmask); |
| 1669 |
} |
| 1670 |
|
| 1671 |
if (readFilenamesMode) { |
| 1672 |
return XSUM_generateFiles(argv + filenamesStart, argc - filenamesStart, algo, displayEndianness, convention, statusOnly, ignoreMissing, warn); |
| 1673 |
} |
| 1674 |
|
| 1675 |
return XSUM_hashFiles(argv+filenamesStart, argc-filenamesStart, algo, displayEndianness, convention); |
| 1676 |
|
| 1677 |
} |