"Fossies" - the Fresh Open Source Software Archive 
Member "littleutils-1.2.5/littleutils/filehash.c.bak" (29 Oct 2021, 14999 Bytes) of package /linux/privat/littleutils-1.2.5.tar.lz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /* filehash: Print various hash digests and filesizes for specified files.
2
3 Copyright (C) 2004-2021 by Brian Lindholm.
4 This file is part of the littleutils utility set.
5
6 The filehash utility is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 The filehash utility is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 the littleutils. If not, see <https://www.gnu.org/licenses/>. */
18
19
20 #include <config.h>
21
22 #if HAVE_INTTYPES_H
23 # include <inttypes.h>
24 #endif
25 #include <limits.h>
26 #ifdef HAVE_STDIO_H
27 # include <stdio.h>
28 #endif
29 #ifdef HAVE_STDLIB_H
30 # include <stdlib.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 # include <string.h>
34 #endif
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 # define OPTEND -1
45 #else
46 # define OPTEND EOF
47 #endif
48 #ifdef HAVE_GETOPT_H
49 # include <getopt.h>
50 #endif
51
52 #ifdef MSDOS
53 # include "io.h"
54 #endif
55
56 #include "md5.h"
57 #include "sha1.h"
58 #include "sha256.h"
59 #include "sha512.h"
60 #include "b2sum.h"
61
62 #ifdef HAVE_FSEEKO
63 # define fseek(a,b,c) fseeko((a),(off_t)(b),(c))
64 #endif
65
66 #ifdef __MINGW32__
67 extern int getopt (int argc, char * const *argv, const char *optstring);
68 extern char *optarg;
69 extern int optind;
70 #endif
71
72 #ifdef DJGPP
73 unsigned short _djstat_flags = 63;
74 #endif
75
76 #ifndef PATH_MAX
77 # define PATH_MAX 256
78 #endif
79
80 char *sizefmt = (sizeof (off_t) <= sizeof (long) ? "%lu" : "%llu");
81
82
83 static void
84 help (FILE *where)
85 {
86 fprintf (where,
87 "filehash " PACKAGE_VERSION "\n"
88 "usage: filehash [-1(MD5)] [-2(SHA1)] [-3(SHA224) ][-4(SHA256)]\n"
89 " [-5(SHA384)] [-6(SHA512)] [-7(BLAKE2B_256)] [-8(BLAKE2B_256)]\n"
90 " [-b(ase64url)] [-c(lassic)] [-f file_list] [-h(elp)]\n"
91 " [-n byte_count] [-o offset] [-p(ipe) ] [-q(uiet)] [-s(ize)]\n"
92 " [-v(erbose)] file...\n");
93 }
94
95
96 static void
97 print_hash (unsigned char *hash, int bytes, int use_base64)
98 {
99 int i;
100 static const unsigned char base64_table[65] =
101 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
102
103 if (use_base64) {
104 for (i = 0; i < (bytes / 3); ++i) {
105 fprintf (stdout, "%1c%1c%1c%1c", base64_table[hash[i*3] >> 2],
106 base64_table[((hash[i*3] & 0x03) << 4) | (hash[i*3+1] >> 4)],
107 base64_table[((hash[i*3+1] & 0x0F) << 2) | (hash[i*3+2] >> 6)],
108 base64_table[hash[i*3+2] & 0x3F]);
109 }
110 if ((bytes % 3) == 2)
111 fprintf (stdout, "%1c%1c%1c", base64_table[hash[bytes-2] >> 2],
112 base64_table[((hash[bytes-2] & 0x03) << 4) | (hash[bytes-1] >> 4)],
113 base64_table[(hash[bytes-1] & 0x0F) << 2]);
114 else if ((bytes % 3) == 1)
115 fprintf (stdout, "%1c%1c", base64_table[hash[bytes-1] >> 2],
116 base64_table[(hash[bytes-1] & 0x03) << 4]);
117 }
118 else {
119 for (i = 0; i < bytes; ++i)
120 fprintf (stdout, "%02x", (unsigned int) hash[i]);
121 }
122 }
123
124
125 static void
126 print_filehash (char *filename, off_t offset, off_t read_bytes, int print_size,
127 int run_md5, int run_sha1, int run_sha224, int run_sha256, int run_sha384,
128 int run_sha512, int run_blake2b_256, int run_blake2b_512, int classic,
129 int use_base64, int verbose)
130 {
131 FILE *file;
132 int i, not_first, rc;
133 struct stat file_stats;
134 unsigned char md5result[16], sha1result[20], sha224result[28],
135 sha256result[32], sha384result[48], sha512result[64],
136 blake2b_256result[32], blake2b_512result[64];
137
138 if (stat (filename, &file_stats))
139 fprintf (stderr, "filehash error: can't stat %s\n", filename);
140 else if (((file_stats.st_mode & S_IFDIR) != S_IFDIR) &&
141 ((file_stats.st_mode & S_IFREG) == S_IFREG))
142 {
143 if ((file = fopen (filename, "rb")) == NULL)
144 fprintf (stderr, "filehash error: can't open %s\n", filename);
145 else
146 {
147 not_first = 0;
148 if (run_md5) {
149 if (not_first || (offset > 0))
150 (void) fseek (file, (off_t) offset, 0);
151 rc = md5_stream (file, md5result, read_bytes);
152 if (rc)
153 fprintf (stderr, "filehash error: md5_stream failed on %s\n", filename);
154 not_first = 1;
155 }
156 if (run_sha1) {
157 if (not_first || (offset > 0))
158 (void) fseek (file, (off_t) offset, 0);
159 rc = sha1_stream (file, sha1result, read_bytes);
160 if (rc)
161 fprintf (stderr, "filehash error: sha1_stream failed on %s\n", filename);
162 not_first = 1;
163 }
164 if (run_sha224) {
165 if (not_first || (offset > 0))
166 (void) fseek (file, (off_t) offset, 0);
167 rc = sha224_stream (file, sha224result, read_bytes);
168 if (rc)
169 fprintf (stderr, "filehash error: sha224_stream failed on %s\n", filename);
170 not_first = 1;
171 }
172 if (run_sha256) {
173 if (not_first || (offset > 0))
174 (void) fseek (file, (off_t) offset, 0);
175 rc = sha256_stream (file, sha256result, read_bytes);
176 if (rc)
177 fprintf (stderr, "filehash error: sha256_stream failed on %s\n", filename);
178 not_first = 1;
179 }
180 if (run_sha384) {
181 if (not_first || (offset > 0))
182 (void) fseek (file, (off_t) offset, 0);
183 rc = sha384_stream (file, sha384result, read_bytes);
184 if (rc)
185 fprintf (stderr, "filehash error: sha384_stream failed on %s\n", filename);
186 not_first = 1;
187 }
188 if (run_sha512) {
189 if (not_first || (offset > 0))
190 (void) fseek (file, (off_t) offset, 0);
191 rc = sha512_stream (file, sha512result, read_bytes);
192 if (rc)
193 fprintf (stderr, "filehash error: sha512_stream failed on %s\n", filename);
194 not_first = 1;
195 }
196 if (run_blake2b_256) {
197 if (not_first || (offset > 0))
198 (void) fseek (file, (off_t) offset, 0);
199 rc = blake2b_stream (file, blake2b_256result, 32, read_bytes);
200 if (rc)
201 fprintf (stderr, "filehash error: sha384_stream failed on %s\n", filename);
202 not_first = 1;
203 }
204 if (run_blake2b_512) {
205 if (not_first || (offset > 0))
206 (void) fseek (file, (off_t) offset, 0);
207 rc = blake2b_stream (file, blake2b_512result, 64, read_bytes);
208 if (rc)
209 fprintf (stderr, "filehash error: sha512_stream failed on %s\n", filename);
210 not_first = 1;
211 }
212 (void) fclose (file);
213 not_first = 0;
214 if ((verbose == 1) && (classic == 0)) {
215 fprintf (stdout, "%s", filename);
216 not_first = 1;
217 }
218 if (print_size) {
219 if (classic) {
220 fprintf (stdout, sizefmt, file_stats.st_size);
221 fprintf (stdout, " %s\n", filename);
222 }
223 else {
224 if (not_first)
225 fprintf (stdout, "\t");
226 fprintf (stdout, sizefmt, file_stats.st_size);
227 not_first = 1;
228 }
229 }
230 if (run_md5) {
231 if (classic) {
232 print_hash(md5result, 16, use_base64);
233 fprintf (stdout, " %s\n", filename);
234 }
235 else {
236 if (not_first)
237 fprintf (stdout, "\t");
238 print_hash(md5result, 16, use_base64);
239 not_first = 1;
240 }
241 }
242 if (run_sha1) {
243 if (classic) {
244 print_hash(sha1result, 20, use_base64);
245 fprintf (stdout, " %s\n", filename);
246 }
247 else {
248 if (not_first)
249 fprintf (stdout, "\t");
250 print_hash(sha1result, 20, use_base64);
251 not_first = 1;
252 }
253 }
254 if (run_sha224) {
255 if (classic) {
256 print_hash(sha224result, 28, use_base64);
257 fprintf (stdout, " %s\n", filename);
258 }
259 else {
260 if (not_first)
261 fprintf (stdout, "\t");
262 print_hash(sha224result, 28, use_base64);
263 not_first = 1;
264 }
265 }
266 if (run_sha256) {
267 if (classic) {
268 print_hash(sha256result, 32, use_base64);
269 fprintf (stdout, " %s\n", filename);
270 }
271 else {
272 if (not_first)
273 fprintf (stdout, "\t");
274 print_hash(sha256result, 32, use_base64);
275 not_first = 1;
276 }
277 }
278 if (run_sha384) {
279 if (classic) {
280 print_hash(sha384result, 48, use_base64);
281 fprintf (stdout, " %s\n", filename);
282 }
283 else {
284 if (not_first)
285 fprintf (stdout, "\t");
286 print_hash(sha384result, 48, use_base64);
287 not_first = 1;
288 }
289 }
290 if (run_sha512) {
291 if (classic) {
292 print_hash(sha512result, 64, use_base64);
293 fprintf (stdout, " %s\n", filename);
294 }
295 else {
296 if (not_first)
297 fprintf (stdout, "\t");
298 print_hash(sha512result, 64, use_base64);
299 }
300 }
301 if (run_blake2b_256) {
302 if (classic) {
303 print_hash(blake2b_256result, 32, use_base64);
304 fprintf (stdout, " %s\n", filename);
305 }
306 else {
307 if (not_first)
308 fprintf (stdout, "\t");
309 print_hash(blake2b_256result, 32, use_base64);
310 }
311 }
312 if (run_blake2b_512) {
313 if (classic) {
314 print_hash(blake2b_512result, 64, use_base64);
315 fprintf (stdout, " %s\n", filename);
316 }
317 else {
318 if (not_first)
319 fprintf (stdout, "\t");
320 print_hash(blake2b_512result, 64, use_base64);
321 }
322 }
323 if (classic == 0)
324 fprintf (stdout, "\n");
325 }
326 }
327 }
328
329
330 int
331 main (int argc, char **argv)
332 {
333 FILE *infile;
334 char filename[PATH_MAX], *listname, *newline, *rc;
335 int argn, classic, offset, opt, print_size, run_md5, run_sha1, run_sha224,
336 run_sha256, run_sha384, run_sha512, run_blake2b_256, run_blake2b_512,
337 use_base64, use_file, use_pipe, verbose;
338 off_t read_bytes, tmp_bytes;
339
340 /* parse options */
341
342 classic = 0;
343 listname = "";
344 offset = 0;
345 print_size = 0;
346 read_bytes = -1;
347 run_md5 = 0;
348 run_sha1 = 0;
349 run_sha224 = 0;
350 run_sha256 = 0;
351 run_sha384 = 0;
352 run_sha512 = 0;
353 run_blake2b_256 = 0;
354 run_blake2b_512 = 0;
355 use_base64 = 0;
356 use_file = 0;
357 use_pipe = 0;
358 verbose = 0;
359 while ((opt = getopt (argc, argv, "12345678bcf:hpn:o:qsv")) != OPTEND)
360 switch (opt)
361 {
362 case '1':
363 run_md5 = 1;
364 break;
365 case '2':
366 run_sha1 = 1;
367 break;
368 case '3':
369 run_sha224 = 1;
370 break;
371 case '4':
372 run_sha256 = 1;
373 break;
374 case '5':
375 run_sha384 = 1;
376 break;
377 case '6':
378 run_sha512 = 1;
379 break;
380 case '7':
381 run_blake2b_256 = 1;
382 break;
383 case '8':
384 run_blake2b_512 = 1;
385 break;
386 case 'b':
387 use_base64 = 1;
388 break;
389 case 'c':
390 classic = 1;
391 break;
392 case 'f':
393 use_file = 1;
394 listname = optarg;
395 break;
396 case 'h':
397 help (stdout);
398 return (0);
399 case 'n':
400 tmp_bytes = (sizeof (off_t) <= sizeof (long) ? atol(optarg) : atoll(optarg));
401 if (tmp_bytes > 0)
402 read_bytes = tmp_bytes;
403 else
404 read_bytes = 0;
405 break;
406 case 'o':
407 tmp_bytes = (sizeof (off_t) <= sizeof (long) ? atol(optarg) : atoll(optarg));
408 if (tmp_bytes > 0)
409 offset = tmp_bytes;
410 else
411 offset = 0;
412 break;
413 case 'p':
414 use_pipe = 1;
415 break;
416 case 'q':
417 verbose = -1;
418 break;
419 case 's':
420 print_size = 1;
421 break;
422 case 'v':
423 verbose = 1;
424 break;
425 case '?':
426 help (stderr);
427 return (1);
428 }
429
430 /* finalize options */
431
432 if ((optind == argc) && (use_file == 0) && (use_pipe == 0))
433 {
434 help (stdout);
435 return (0);
436 }
437 if (verbose == 0)
438 {
439 if (((argc - optind) != 1) || use_file || use_pipe)
440 verbose = 1;
441 else
442 verbose = -1;
443 }
444 if ((run_md5 + run_sha1 + run_sha224 + run_sha256 + run_sha384 + run_sha512 +
445 run_blake2b_256 + run_blake2b_512) == 0)
446 run_sha256 = 1;
447
448 /* process files in listed in file specified by -f option */
449
450 if (use_file)
451 {
452 infile = fopen (listname, "r");
453 if (infile == NULL)
454 fprintf (stderr, "filehash error: can't open %s!\n", listname);
455 else
456 {
457 while (!feof (infile))
458 {
459 rc = fgets (filename, PATH_MAX - 1, infile);
460 if (rc != NULL)
461 {
462 newline = strchr (filename, '\n');
463 if (newline != NULL)
464 *newline = '\0';
465 if (strlen (filename) != 0)
466 print_filehash (filename, offset, read_bytes, print_size,
467 run_md5, run_sha1, run_sha224, run_sha256, run_sha384,
468 run_sha512, run_blake2b_256, run_blake2b_512, classic,
469 use_base64, verbose);
470 }
471 }
472 (void) fclose (infile);
473 }
474 }
475
476 /* process files listed on stdin (i.e., the -p option) */
477
478 if (use_pipe)
479 while (!feof (stdin))
480 {
481 rc = fgets (filename, PATH_MAX - 1, stdin);
482 if (rc != NULL)
483 {
484 newline = strchr (filename, '\n');
485 if (newline != NULL)
486 *newline = '\0';
487 if (strlen (filename) != 0)
488 print_filehash (filename, offset, read_bytes, print_size, run_md5,
489 run_sha1, run_sha224, run_sha256, run_sha384, run_sha512,
490 run_blake2b_256, run_blake2b_512, classic, use_base64, verbose);
491 }
492 }
493
494 /* process files given in the argument list */
495
496 for (argn = optind; argn < argc; argn++)
497 print_filehash (argv[argn], offset, read_bytes, print_size, run_md5,
498 run_sha1, run_sha224, run_sha256, run_sha384, run_sha512,
499 run_blake2b_256, run_blake2b_512, classic, use_base64, verbose);
500
501 /* indicate successful finish */
502
503 return (0);
504 }