1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Example application (utility functions) */
3 /* Copyright(c) 2016-2018 LoRd_MuldeR <mulder2@gmx.de> */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */
6 /* and associated documentation files (the "Software"), to deal in the Software without */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish, */
8 /* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the */
9 /* Software is furnished to do so, subject to the following conditions: */
11 /* The above copyright notice and this permission notice shall be included in all copies or */
12 /* substantial portions of the Software. */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
16 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
19 /* ---------------------------------------------------------------------------------------------- */
21 #ifndef MHASH_CLI_UTILS_INCLUDED
22 #define MHASH_CLI_UTILS_INCLUDED
24 /*Enable large files*/
25 #define __USE_LARGEFILE64 1
38 #define MY_HASH_LENGTH mhash_384::MHash384::HASH_LEN
40 #define MY_HASH_LENGTH MHASH_384_LEN
44 typedef struct param_t
57 typedef struct version_t
66 static version_t get_version(void)
68 version_t version_info;
70 mhash_384::MHash384::version(version_info.major, version_info.minor, version_info.patch);
72 version_info.major = MHASH_384_VERSION_MAJOR;
73 version_info.minor = MHASH_384_VERSION_MINOR;
74 version_info.patch = MHASH_384_VERSION_PATCH;
80 static void print_logo(void)
82 const version_t version = get_version();
83 fprintf(stderr, "\nMHash384 v%u.%u.%u, simple fast portable header-only hashing library [%s]\n", (unsigned int)version.major, (unsigned int)version.minor, (unsigned int)version.patch, __DATE__);
84 fprintf(stderr, "Copyright(c) 2016-2018 LoRd_MuldeR <mulder2@gmx.de>, released under the MIT License.\n\n");
89 #define EXE_SUFFIX ".exe"
95 static void print_help(void)
97 fprintf(stderr, "Built with " COMPILER_TYPE " v%u.%u.%u on " SYSTEM_TYPE " [" COMPILER_ARCH "]\n\n", COMPILER_VERS_MAJOR, COMPILER_VERS_MINOR, COMPILER_VERS_PATCH);
98 fprintf(stderr, "Usage:\n");
99 fprintf(stderr, " mhash384" EXE_SUFFIX " [options] [input_file]\n\n");
100 fprintf(stderr, "Options:\n");
101 fprintf(stderr, " -p, --progress show progress while processing\n");
102 fprintf(stderr, " -u, --upper print digest in upper case letters\n");
103 fprintf(stderr, " -c, --curly print digest using C-style curly brackets\n");
104 fprintf(stderr, " -r, --raw output \"raw\" bytes (no \"hex\" encoding)\n");
105 fprintf(stderr, " -b, --bench compute and print throughput\n");
106 fprintf(stderr, " -v, --version print the version string and exit\n");
107 fprintf(stderr, " -t, --test execute self-test and exit\n");
108 fprintf(stderr, " -h, --help print this help screen and exit\n\n");
109 fprintf(stderr, "If no input file is specified, input will be read from STDIN.\n\n");
113 static int parse_arguments(param_t *param, int argc, CHAR *argv[])
116 memset(param, 0, sizeof(param_t));
117 for (i = 1; i < argc; ++i)
121 if (!STRICMP(argv[i], T("--")))
126 else if (!STRICMP(argv[i], T("-b")) || !STRICMP(argv[i], T("--bench")))
128 param->enable_bench = 1;
131 else if (!STRICMP(argv[i], T("-t")) || !STRICMP(argv[i], T("--test")))
133 param->test_mode = 1;
136 else if (!STRICMP(argv[i], T("-p")) || !STRICMP(argv[i], T("--progress")))
138 param->show_progress = 1;
141 else if (!STRICMP(argv[i], T("-u")) || !STRICMP(argv[i], T("--upper")))
143 param->use_upper_case = 1;
146 else if (!STRICMP(argv[i], T("-c")) || !STRICMP(argv[i], T("--curly")))
148 param->curly_brackets = 1;
151 else if (!STRICMP(argv[i], T("-r")) || !STRICMP(argv[i], T("--raw")))
153 param->raw_output = 1;
156 else if (!STRICMP(argv[i], T("-h")) || !STRICMP(argv[i], T("--help")) || !STRICMP(argv[i], T("/?")))
162 else if (!STRICMP(argv[i], T("-v")) || !STRICMP(argv[i], T("--version")))
164 const version_t version = get_version();
165 printf("mhash-384 version %u.%u.%u (built %s)\n", (unsigned int)version.major, (unsigned int)version.minor, (unsigned int)version.patch, __DATE__);
168 else if ((argv[i][0] == T('-')) && (argv[i][1] != T('\0')))
171 fprintf(stderr, "Unknown option:\n" FMT_S "\n\n", argv[i]);
175 if (!param->filename)
177 param->filename = argv[i];
182 fprintf(stderr, "Excess argument:\n" FMT_S "\n\n", argv[i]);
186 if (param->filename && (!STRICMP(param->filename, T("-"))) && (!stop))
188 param->filename = NULL;
190 if (param->raw_output)
192 if (param->use_upper_case)
194 fprintf(stderr, "Error: Options \"-u\" and \"-r\" are mutually exclusive!\n\n");
197 if (param->curly_brackets)
199 fprintf(stderr, "Error: Options \"-c\" and \"-r\" are mutually exclusive!\n\n");
207 static uint64_t get_file_size(FILE *const file)
210 if (fstat64(fileno(file), &info) || ((info.st_mode & S_IFMT) != S_IFREG))
214 return (uint64_t) info.st_size;
218 static void print_progress(const uint64_t size_total, const uint64_t size_processed)
222 fprintf(stderr, "\r%.1f%% of %" PRIu64 " bytes processed...", ((double)size_processed) / ((double)size_total) * 100.0, size_total);
226 fprintf(stderr, "\r%" PRIu64 " bytes processed...", size_processed);
231 #define _PUT_HEX_CHAR(X,Y,Z) putchar(X[((Y) >> (Z)) & 0xFU])
232 static void print_digest(const uint8_t *const digest, const int uppercase, const int curly)
234 static const char *const HEX_UPR = "0123456789ABCDEF";
235 static const char *const HEX_LWR = "0123456789abcdef";
236 const char *const hex = uppercase ? HEX_UPR : HEX_LWR;
242 for (count = 0U; count < MY_HASH_LENGTH; ++count)
246 fputs(count ? ", 0x" : "0x", stdout);
248 _PUT_HEX_CHAR(hex, digest[count], 4);
249 _PUT_HEX_CHAR(hex, digest[count], 0);
259 static volatile int g_interrupted = 0;
260 static void sigint_handler(int sig_no)
263 signal(sig_no, sigint_handler);
267 #endif /*MHASH_CLI_UTILS_INCLUDED*/