1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Example application (plain C) */
3 /* Copyright(c) 2016-2017 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 /*Disable some warnings*/
23 #define _CRT_SECURE_NO_WARNINGS 1
27 #include "mhash_384.h"
29 #include "utilities.h"
33 #include "self_test.h"
41 #define BUFF_SIZE 4096
43 /*Win32-only headers*/
49 /*The "main" function*/
50 int MAIN(int argc, CHAR *argv[])
54 uint8_t buffer[BUFF_SIZE], result[MHASH_384_LEN];
56 uint64_t size_total, size_processed;
57 uint_fast16_t update_iter;
58 clock_t ts_start, ts_finish;
62 _setmode(_fileno(stdin), _O_BINARY);
65 /*install CTRL+C handler*/
66 signal(SIGINT, sigint_handler);
68 /*process command-line arguments*/
69 if (!parse_arguments(¶m, argc, argv))
74 /*run self-test, if in test mode*/
79 fprintf(stderr, "Not compiled with self-test code!\n");
87 if (!(source = param.filename ? FOPEN(param.filename, T("rb")) : stdin))
90 fprintf(stderr, "Failed to open input file:\n" FMT_S "\n\n%s\n\n", param.filename ? param.filename : T("<STDIN>"), strerror(errno));
94 /*determine file size*/
95 size_total = get_file_size(source);
103 mhash_384_initialize(&context);
105 /*process file contents*/
106 while(!(ferror(source) || feof(source)))
108 count = (uint_fast32_t) fread(buffer, sizeof(uint8_t), BUFF_SIZE, source);
111 mhash_384_update(&context, buffer, count);
112 size_processed += count;
114 if (g_interrupted || (count != BUFF_SIZE))
118 if (param.show_progress && ((update_iter++ & 0x3FF) == 0))
120 print_progress(size_total, size_processed);
125 /*check for interruption*/
128 fprintf(stderr, "\nInterrupted!\n\n");
133 if (param.show_progress)
135 print_progress(size_total, size_processed);
136 fprintf(stderr, " done\n\n");
140 /*check file error status*/
144 fprintf(stderr, "File read error has occurred!\n");
149 /*finalize the hash*/
150 mhash_384_finalize(&context, result);
154 if (param.enable_bench)
156 const double time_total = ((double)(ts_finish - ts_start)) / ((double)CLOCKS_PER_SEC);
157 const double throughput = (size_processed) / time_total;
158 fprintf(stderr, "Processed %" PRIu64 " bytes in %.1f seconds, %.1f bytes/sec.\n\n", size_processed, time_total, throughput);
162 /*output result as Hex string*/
163 print_digest(result, param.use_upper_case);