Make flags be macros rather than enumerations, to ensure that they're
[u/mdw/catacomb] / hashsum.c
1 /* -*-c-*-
2 *
3 * $Id: hashsum.c,v 1.5 2000/12/06 20:33:27 mdw Exp $
4 *
5 * Hash files using some secure hash function
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: hashsum.c,v $
33 * Revision 1.5 2000/12/06 20:33:27 mdw
34 * Make flags be macros rather than enumerations, to ensure that they're
35 * unsigned.
36 *
37 * Revision 1.4 2000/08/04 23:23:44 mdw
38 * Various <ctype.h> fixes.
39 *
40 * Revision 1.3 2000/07/29 17:02:43 mdw
41 * (checkhash): Be pettier about spaces between the hash and filename, for
42 * compatiblity with `md5sum'.
43 *
44 * Revision 1.2 2000/07/15 21:14:05 mdw
45 * Missed `-e' out of the usage string.
46 *
47 * Revision 1.1 2000/07/15 20:52:34 mdw
48 * Useful replacement for `md5sum' with support for many different hash
49 * functions and for reading filename lists from `find'.
50 *
51 */
52
53 /*----- Header files ------------------------------------------------------*/
54
55 #include "config.h"
56
57 #include <ctype.h>
58 #include <errno.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #include <mLib/alloc.h>
64 #include <mLib/dstr.h>
65 #include <mLib/mdwopt.h>
66 #include <mLib/quis.h>
67 #include <mLib/report.h>
68 #include <mLib/sub.h>
69 #include <mLib/str.h>
70
71 #include "ghash.h"
72
73 #include "md4.h"
74 #include "md5.h"
75 #include "rmd128.h"
76 #include "rmd160.h"
77 #include "rmd256.h"
78 #include "rmd320.h"
79 #include "sha.h"
80 #include "tiger.h"
81
82 /*----- Static variables --------------------------------------------------*/
83
84 static const gchash *hashtab[] = {
85 &md5, &md4, &sha, &rmd128, &rmd160, &rmd256, &rmd320, &tiger,
86 0
87 };
88
89 #define f_binary 1u
90 #define f_bogus 2u
91 #define f_verbose 4u
92 #define f_check 8u
93 #define f_files 16u
94 #define f_raw 32u
95 #define f_oddhash 64u
96 #define f_escape 128u
97
98 /*----- Support functions -------------------------------------------------*/
99
100 /* --- @fhash@ --- *
101 *
102 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
103 * @unsigned f@ = flags to set
104 * @const gchash *gch@ = pointer to hash function to use
105 * @void *buf@ = pointer to hash output buffer
106 *
107 * Returns: Zero if it worked, nonzero on error.
108 *
109 * Use: Hashes a file.
110 */
111
112 static int fhash(const char *file, unsigned f, const gchash *gch, void *buf)
113 {
114 FILE *fp;
115 char fbuf[BUFSIZ];
116 size_t sz;
117 ghash *h;
118 int e;
119
120 if (!file)
121 fp = stdin;
122 else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0)
123 return (-1);
124
125 h = gch->init();
126 while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0)
127 h->ops->hash(h, fbuf, sz);
128 h->ops->done(h, buf);
129 h->ops->destroy(h);
130 e = ferror(fp);
131 if (file)
132 fclose(fp);
133 return (e ? -1 : 0);
134 }
135
136 /* --- @puthex@ --- *
137 *
138 * Arguments: @const octet *buf@ = pointer to a binary buffer
139 * @size_t sz@ = size of the buffer
140 * @FILE *fp@ = pointer to output file handle
141 *
142 * Returns: ---
143 *
144 * Use: Writes a hex dump of a block of memory.
145 */
146
147 static void puthex(const octet *buf, size_t sz, FILE *fp)
148 {
149 while (sz) {
150 fprintf(fp, "%02x", *buf++);
151 sz--;
152 }
153 }
154
155 /* --- @gethex@ --- *
156 *
157 * Arguments: @const char *p@ = pointer to input string
158 * @octet *q@ = pointer to output buffer
159 * @size_t sz@ = size of the output buffer
160 * @char **pp@ = where to put the end pointer
161 *
162 * Returns: The number of bytes written to the buffer.
163 *
164 * Use: Reads hex dumps from the input string.
165 */
166
167 static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
168 {
169 size_t i = 0;
170 while (sz > 0 &&
171 isxdigit((unsigned char)p[0]) &&
172 isxdigit((unsigned char)p[1])) {
173 char buf[3];
174 buf[0] = p[0];
175 buf[1] = p[1];
176 buf[2] = 0;
177 *q++ = strtoul(buf, 0, 16);
178 sz--;
179 p += 2;
180 i++;
181 }
182 if (pp)
183 *pp = (char *)p;
184 return (i);
185 }
186
187 /* --- @gethash@ --- *
188 *
189 * Arguments: @const char *name@ = pointer to name string
190 *
191 * Returns: Pointer to appropriate hash class.
192 *
193 * Use: Chooses a hash function by name.
194 */
195
196 static const gchash *gethash(const char *name)
197 {
198 const gchash **g, *gg = 0;
199 size_t sz = strlen(name);
200 for (g = hashtab; *g; g++) {
201 if (strncmp(name, (*g)->name, sz) == 0) {
202 if ((*g)->name[sz] == 0) {
203 gg = *g;
204 break;
205 } else if (gg)
206 return (0);
207 else
208 gg = *g;
209 }
210 }
211 return (gg);
212 }
213
214 /* --- @getstring@ --- *
215 *
216 * Arguments: @FILE *fp@ = stream from which to read
217 * @const char *p@ = string to read from instead
218 * @dstr *d@ = destination string
219 * @unsigned raw@ = raw or cooked read
220 *
221 * Returns: Zero if OK, nonzero on end-of-file.
222 *
223 * Use: Reads a filename (or something similar) from a stream.
224 */
225
226 static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw)
227 {
228 int ch;
229 int q = 0;
230
231 /* --- Raw: just read exactly what's written up to a null byte --- */
232
233 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
234 #define EOFCH (fp ? EOF : 0)
235
236 if (raw) {
237 if ((ch = NEXTCH) == EOFCH)
238 return (EOF);
239 for (;;) {
240 if (!ch)
241 break;
242 DPUTC(d, ch);
243 if ((ch = NEXTCH) == EOFCH)
244 break;
245 }
246 DPUTZ(d);
247 return (0);
248 }
249
250 /* --- Skip as far as whitespace --- *
251 *
252 * Also skip past comments.
253 */
254
255 again:
256 ch = NEXTCH;
257 while (isspace(ch))
258 ch = NEXTCH;
259 if (ch == '#') {
260 do ch = NEXTCH; while (ch != '\n' && ch != EOFCH);
261 goto again;
262 }
263 if (ch == EOFCH)
264 return (EOF);
265
266 /* --- If the character is a quote then read a quoted string --- */
267
268 switch (ch) {
269 case '`':
270 ch = '\'';
271 case '\'':
272 case '\"':
273 q = ch;
274 ch = NEXTCH;
275 break;
276 }
277
278 /* --- Now read all sorts of interesting things --- */
279
280 for (;;) {
281
282 /* --- Handle an escaped thing --- */
283
284 if (ch == '\\') {
285 ch = NEXTCH;
286 if (ch == EOFCH)
287 break;
288 switch (ch) {
289 case 'a': ch = '\a'; break;
290 case 'b': ch = '\b'; break;
291 case 'f': ch = '\f'; break;
292 case 'n': ch = '\n'; break;
293 case 'r': ch = '\r'; break;
294 case 't': ch = '\t'; break;
295 case 'v': ch = '\v'; break;
296 }
297 DPUTC(d, ch);
298 ch = NEXTCH;
299 continue;
300 }
301
302 /* --- If it's a quote or some other end marker then stop --- */
303
304 if (ch == q)
305 break;
306 if (!q && isspace(ch))
307 break;
308
309 /* --- Otherwise contribute and continue --- */
310
311 DPUTC(d, ch);
312 if ((ch = NEXTCH) == EOFCH)
313 break;
314 }
315
316 /* --- Done --- */
317
318 DPUTZ(d);
319 return (0);
320
321 #undef NEXTCH
322 #undef EOFCH
323 }
324
325 /* --- @putstring@ --- *
326 *
327 * Arguments: @FILE *fp@ = stream to write on
328 * @const char *p@ = pointer to text
329 * @unsigned raw@ = whether the string is to be written raw
330 *
331 * Returns: ---
332 *
333 * Use: Emits a string to a stream.
334 */
335
336 static void putstring(FILE *fp, const char *p, unsigned raw)
337 {
338 size_t sz = strlen(p);
339 unsigned qq;
340 const char *q;
341
342 /* --- Just write the string null terminated if raw --- */
343
344 if (raw) {
345 fwrite(p, 1, sz + 1, fp);
346 return;
347 }
348
349 /* --- Check for any dodgy characters --- */
350
351 qq = 0;
352 for (q = p; *q; q++) {
353 if (isspace((unsigned char)*q)) {
354 qq = '\"';
355 break;
356 }
357 }
358
359 if (qq)
360 putc(qq, fp);
361
362 /* --- Emit the string --- */
363
364 for (q = p; *q; q++) {
365 switch (*q) {
366 case '\a': fputc('\\', fp); fputc('a', fp); break;
367 case '\b': fputc('\\', fp); fputc('b', fp); break;
368 case '\f': fputc('\\', fp); fputc('f', fp); break;
369 case '\n': fputc('\\', fp); fputc('n', fp); break;
370 case '\r': fputc('\\', fp); fputc('r', fp); break;
371 case '\t': fputc('\\', fp); fputc('t', fp); break;
372 case '\v': fputc('\\', fp); fputc('v', fp); break;
373 case '`': fputc('\\', fp); fputc('`', fp); break;
374 case '\'': fputc('\\', fp); fputc('\'', fp); break;
375 case '\"': fputc('\\', fp); fputc('\"', fp); break;
376 case '#': fputc('\\', fp); fputc('#', fp); break;
377 default:
378 putc(*q, fp);
379 break;
380 }
381 }
382
383 /* --- Done --- */
384
385 if (qq)
386 putc(qq, fp);
387 }
388
389 /*----- Guts --------------------------------------------------------------*/
390
391 static int checkhash(const char *file, unsigned f, const gchash *gch)
392 {
393 int rc;
394 FILE *fp;
395 dstr d = DSTR_INIT;
396 dstr dd = DSTR_INIT;
397 unsigned long n = 0, nfail = 0;
398 octet *buf = xmalloc(2 * gch->hashsz);
399
400 if (!file)
401 fp = stdin;
402 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
403 moan("couldn't open `%s': %s", file, strerror(errno));
404 return (EXIT_FAILURE);
405 }
406
407 while (DRESET(&d), dstr_putline(&d, fp) != EOF) {
408 char *p = d.buf;
409 char *q;
410 unsigned ff = f;
411
412 /* --- Handle a directive --- */
413
414 if (*p == '#') {
415 p++;
416 if ((q = str_getword(&p)) == 0)
417 continue;
418 if (strcmp(q, "hash") == 0) {
419 const gchash *g;
420 if ((q = str_getword(&p)) == 0)
421 continue;
422 if ((g = gethash(q)) == 0)
423 continue;
424 gch = g;
425 xfree(buf);
426 buf = xmalloc(2 * gch->hashsz);
427 } else if (strcmp(q, "escape") == 0)
428 f |= f_escape;
429 continue;
430 }
431
432 /* --- Otherwise it's a hex thing --- */
433
434 q = p;
435 while (*p && *p != ' ')
436 p++;
437 if (!*p)
438 continue;
439 *p++ = 0;
440 if (gethex(q, buf, gch->hashsz, 0) < gch->hashsz)
441 continue;
442 if (*p == '*')
443 ff |= f_binary;
444 else if (*p != ' ')
445 continue;
446 p++;
447
448 if (f & f_escape) {
449 DRESET(&dd);
450 getstring(0, p, &dd, 0);
451 p = dd.buf;
452 }
453
454 if (fhash(p, ff, gch, buf + gch->hashsz)) {
455 moan("couldn't read `%s': %s", p, strerror(errno));
456 rc = EXIT_FAILURE;
457 continue;
458 }
459 if (memcmp(buf, buf + gch->hashsz, gch->hashsz) != 0) {
460 if (ff & f_verbose)
461 fprintf(stderr, "FAIL %s\n", p);
462 else
463 moan("%s check failed for `%s'", gch->name, p);
464 nfail++;
465 rc = EXIT_FAILURE;
466 } else {
467 if (ff & f_verbose)
468 fprintf(stderr, "OK %s\n", p);
469 }
470 n++;
471 }
472
473 dstr_destroy(&d);
474 dstr_destroy(&dd);
475 xfree(buf);
476 if ((f & f_verbose) && nfail)
477 moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name);
478 else if (!n)
479 moan("no files checked");
480 return (0);
481 }
482
483 static int dohash(const char *file, unsigned f, const gchash *gch)
484 {
485 int rc = 0;
486 octet *p = xmalloc(gch->hashsz);
487
488 if (fhash(file, f, gch, p)) {
489 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
490 rc = EXIT_FAILURE;
491 } else {
492 puthex(p, gch->hashsz, stdout);
493 if (file) {
494 fputc(' ', stdout);
495 fputc(f & f_binary ? '*' : ' ', stdout);
496 if (f & f_escape)
497 putstring(stdout, file, 0);
498 else
499 fputs(file, stdout);
500 }
501 fputc('\n', stdout);
502 }
503
504 xfree(p);
505 return (rc);
506 }
507
508 static int dofile(const char *file, unsigned f, const gchash *gch)
509 {
510 return (f & f_check ? checkhash : dohash)(file, f, gch);
511 }
512
513 static int hashfiles(const char *file, unsigned f, const gchash *gch)
514 {
515 FILE *fp;
516 dstr d = DSTR_INIT;
517 int rc = 0;
518 int rrc;
519
520 if (!file)
521 fp = stdin;
522 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
523 moan("couldn't open `%s': %s", file, strerror(errno));
524 return (EXIT_FAILURE);
525 }
526
527 for (;;) {
528 DRESET(&d);
529 if (getstring(fp, 0, &d, f & f_raw))
530 break;
531 if ((rrc = dofile(d.buf, f, gch)) != 0)
532 rc = rrc;
533 }
534
535 return (rc);
536 }
537
538 static int hashsum(const char *file, unsigned f, const gchash *gch)
539 {
540 return (f & f_files ? hashfiles : dofile)(file, f, gch);
541 }
542
543 /*----- Main driver -------------------------------------------------------*/
544
545 static void version(FILE *fp)
546 {
547 pquis(fp, "$, Catacomb version " VERSION "\n");
548 }
549
550 static void usage(FILE *fp)
551 {
552 pquis(fp, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
553 }
554
555 static void help(FILE *fp, const gchash *gch)
556 {
557 version(fp);
558 fputc('\n', fp);
559 usage(fp);
560 pquis(fp, "\n\
561 Generates or checks message digests on files. Options available:\n\
562 \n\
563 -h, --help Display this help message.\n\
564 -V, --version Display program's version number.\n\
565 -u, --usage Display a terse usage message.\n\
566 \n\
567 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
568 \n\
569 -f, --files Read a list of file names from standard input.\n\
570 -0, --null File names are null terminated, not plain text.\n\
571 \n\
572 -e, --escape Escape funny characters in filenames.\n\
573 -c, --check Check message digests rather than emitting them.\n\
574 -b, --binary When reading files, treat them as binary.\n\
575 -v, --verbose Be verbose when checking digests.\n\
576 \n\
577 For a list of supported message digest algorithms, type `$ --list'.\n\
578 ");
579 if (gch)
580 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
581 }
582
583 int main(int argc, char *argv[])
584 {
585 unsigned f = 0;
586 const gchash *gch = 0;
587 int rc;
588
589 /* --- Initialization --- */
590
591 ego(argv[0]);
592 sub_init();
593
594 /* --- Choose a hash function from the name --- */
595
596 {
597 char *q = xstrdup(QUIS);
598 size_t len = strlen(q);
599 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
600 q[len - 3] = 0;
601 gch = gethash(q);
602 }
603 if (!gch)
604 gch = hashtab[0];
605 xfree(q);
606 }
607
608 /* --- Read options --- */
609
610 for (;;) {
611 static struct option opts[] = {
612 { "help", 0, 0, 'h' },
613 { "verbose", 0, 0, 'V' },
614 { "usage", 0, 0, 'u' },
615
616 { "algorithm", OPTF_ARGREQ, 0, 'a' },
617 { "hash", OPTF_ARGREQ, 0, 'a' },
618 { "list", 0, 0, 'l' },
619
620 { "files", 0, 0, 'f' },
621 { "find", 0, 0, 'f' },
622 { "null", 0, 0, '0' },
623
624 { "escape", 0, 0, 'e' },
625 { "check", 0, 0, 'c' },
626 { "binary", 0, 0, 'b' },
627 { "verbose", 0, 0, 'v' },
628
629 { 0, 0, 0, 0 }
630 };
631 int i = mdwopt(argc, argv, "hVu a:l f0 ecbv", opts, 0, 0, 0);
632 if (i < 0)
633 break;
634
635 switch (i) {
636 case 'h':
637 help(stdout, gch);
638 exit(0);
639 case 'V':
640 version(stdout);
641 exit(0);
642 case 'u':
643 usage(stdout);
644 exit(0);
645 case 'a':
646 if ((gch = gethash(optarg)) == 0)
647 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
648 f |= f_oddhash;
649 break;
650 case 'l': {
651 unsigned j;
652 for (j = 0; hashtab[j]; j++) {
653 if (j)
654 fputc(' ', stdout);
655 printf("%s", hashtab[j]->name);
656 }
657 fputc('\n', stdout);
658 exit(0);
659 } break;
660 case 'f':
661 f |= f_files;
662 break;
663 case '0':
664 f |= f_raw;
665 break;
666 case 'e':
667 f |= f_escape;
668 break;
669 case 'c':
670 f |= f_check;
671 break;
672 case 'b':
673 f |= f_binary;
674 break;
675 case 'v':
676 f |= f_verbose;
677 break;
678 default:
679 f |= f_bogus;
680 break;
681 }
682 }
683
684 if (f & f_bogus) {
685 usage(stderr);
686 exit(EXIT_FAILURE);
687 }
688 argv += optind;
689 argc -= optind;
690
691 /* --- Generate output --- */
692
693 if (!(f & f_check)) {
694 if (f & f_oddhash)
695 printf("#hash %s\n", gch->name);
696 if (f & f_escape)
697 fputs("#escape\n", stdout);
698 }
699
700 if (argc) {
701 int i;
702 int rrc;
703 rc = 0;
704 for (i = 0; i < argc; i++) {
705 if ((rrc = hashsum(argv[i], f, gch)) != 0)
706 rc = rrc;
707 }
708 } else
709 rc = hashsum(0, f, gch);
710
711 return (rc);
712 }
713
714 /*----- That's all, folks -------------------------------------------------*/