3 * $Id: hashsum.c,v 1.3 2000/07/29 17:02:43 mdw Exp $
5 * Hash files using some secure hash function
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2000/07/29 17:02:43 mdw
34 * (checkhash): Be pettier about spaces between the hash and filename, for
35 * compatiblity with `md5sum'.
37 * Revision 1.2 2000/07/15 21:14:05 mdw
38 * Missed `-e' out of the usage string.
40 * Revision 1.1 2000/07/15 20:52:34 mdw
41 * Useful replacement for `md5sum' with support for many different hash
42 * functions and for reading filename lists from `find'.
46 /*----- Header files ------------------------------------------------------*/
56 #include <mLib/alloc.h>
57 #include <mLib/dstr.h>
58 #include <mLib/mdwopt.h>
59 #include <mLib/quis.h>
60 #include <mLib/report.h>
75 /*----- Static variables --------------------------------------------------*/
77 static const gchash *hashtab[] = {
78 &md5, &md4, &sha, &rmd128, &rmd160, &rmd256, &rmd320, &tiger,
93 /*----- Support functions -------------------------------------------------*/
97 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
98 * @unsigned f@ = flags to set
99 * @const gchash *gch@ = pointer to hash function to use
100 * @void *buf@ = pointer to hash output buffer
102 * Returns: Zero if it worked, nonzero on error.
104 * Use: Hashes a file.
107 static int fhash(const char *file, unsigned f, const gchash *gch, void *buf)
117 else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0)
121 while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0)
122 h->ops->hash(h, fbuf, sz);
123 h->ops->done(h, buf);
131 /* --- @puthex@ --- *
133 * Arguments: @const octet *buf@ = pointer to a binary buffer
134 * @size_t sz@ = size of the buffer
135 * @FILE *fp@ = pointer to output file handle
139 * Use: Writes a hex dump of a block of memory.
142 static void puthex(const octet *buf, size_t sz, FILE *fp)
145 fprintf(fp, "%02x", *buf++);
150 /* --- @gethex@ --- *
152 * Arguments: @const char *p@ = pointer to input string
153 * @octet *q@ = pointer to output buffer
154 * @size_t sz@ = size of the output buffer
155 * @char **pp@ = where to put the end pointer
157 * Returns: The number of bytes written to the buffer.
159 * Use: Reads hex dumps from the input string.
162 static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
166 isxdigit((unsigned char)p[0]) &&
167 isxdigit((unsigned char)p[1])) {
172 *q++ = strtoul(buf, 0, 16);
182 /* --- @gethash@ --- *
184 * Arguments: @const char *name@ = pointer to name string
186 * Returns: Pointer to appropriate hash class.
188 * Use: Chooses a hash function by name.
191 static const gchash *gethash(const char *name)
193 const gchash **g, *gg = 0;
194 size_t sz = strlen(name);
195 for (g = hashtab; *g; g++) {
196 if (strncmp(name, (*g)->name, sz) == 0) {
197 if ((*g)->name[sz] == 0) {
209 /* --- @getstring@ --- *
211 * Arguments: @FILE *fp@ = stream from which to read
212 * @const char *p@ = string to read from instead
213 * @dstr *d@ = destination string
214 * @unsigned raw@ = raw or cooked read
216 * Returns: Zero if OK, nonzero on end-of-file.
218 * Use: Reads a filename (or something similar) from a stream.
221 static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw)
226 /* --- Raw: just read exactly what's written up to a null byte --- */
228 #define NEXTCH (fp ? getc(fp) : *p++)
229 #define EOFCH (fp ? EOF : 0)
232 if ((ch = NEXTCH) == EOFCH)
238 if ((ch = NEXTCH) == EOFCH)
245 /* --- Skip as far as whitespace --- *
247 * Also skip past comments.
252 while (isspace((unsigned char)ch))
255 do ch = NEXTCH; while (ch != '\n' && ch != EOFCH);
261 /* --- If the character is a quote then read a quoted string --- */
273 /* --- Now read all sorts of interesting things --- */
277 /* --- Handle an escaped thing --- */
284 case 'a': ch = '\a'; break;
285 case 'b': ch = '\b'; break;
286 case 'f': ch = '\f'; break;
287 case 'n': ch = '\n'; break;
288 case 'r': ch = '\r'; break;
289 case 't': ch = '\t'; break;
290 case 'v': ch = '\v'; break;
297 /* --- If it's a quote or some other end marker then stop --- */
301 if (!q && isspace((unsigned char)ch))
304 /* --- Otherwise contribute and continue --- */
307 if ((ch = NEXTCH) == EOFCH)
320 /* --- @putstring@ --- *
322 * Arguments: @FILE *fp@ = stream to write on
323 * @const char *p@ = pointer to text
324 * @unsigned raw@ = whether the string is to be written raw
328 * Use: Emits a string to a stream.
331 static void putstring(FILE *fp, const char *p, unsigned raw)
333 size_t sz = strlen(p);
337 /* --- Just write the string null terminated if raw --- */
340 fwrite(p, 1, sz + 1, fp);
344 /* --- Check for any dodgy characters --- */
347 for (q = p; *q; q++) {
348 if (isspace((unsigned char)*q)) {
357 /* --- Emit the string --- */
359 for (q = p; *q; q++) {
361 case '\a': fputc('\\', fp); fputc('a', fp); break;
362 case '\b': fputc('\\', fp); fputc('b', fp); break;
363 case '\f': fputc('\\', fp); fputc('f', fp); break;
364 case '\n': fputc('\\', fp); fputc('n', fp); break;
365 case '\r': fputc('\\', fp); fputc('r', fp); break;
366 case '\t': fputc('\\', fp); fputc('t', fp); break;
367 case '\v': fputc('\\', fp); fputc('v', fp); break;
368 case '`': fputc('\\', fp); fputc('`', fp); break;
369 case '\'': fputc('\\', fp); fputc('\'', fp); break;
370 case '\"': fputc('\\', fp); fputc('\"', fp); break;
371 case '#': fputc('\\', fp); fputc('#', fp); break;
384 /*----- Guts --------------------------------------------------------------*/
386 static int checkhash(const char *file, unsigned f, const gchash *gch)
392 unsigned long n = 0, nfail = 0;
393 octet *buf = xmalloc(2 * gch->hashsz);
397 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
398 moan("couldn't open `%s': %s", file, strerror(errno));
399 return (EXIT_FAILURE);
402 while (DRESET(&d), dstr_putline(&d, fp) != EOF) {
407 /* --- Handle a directive --- */
411 if ((q = str_getword(&p)) == 0)
413 if (strcmp(q, "hash") == 0) {
415 if ((q = str_getword(&p)) == 0)
417 if ((g = gethash(q)) == 0)
421 buf = xmalloc(2 * gch->hashsz);
422 } else if (strcmp(q, "escape") == 0)
427 /* --- Otherwise it's a hex thing --- */
430 while (*p && *p != ' ')
435 if (gethex(q, buf, gch->hashsz, 0) < gch->hashsz)
445 getstring(0, p, &dd, 0);
449 if (fhash(p, ff, gch, buf + gch->hashsz)) {
450 moan("couldn't read `%s': %s", p, strerror(errno));
454 if (memcmp(buf, buf + gch->hashsz, gch->hashsz) != 0) {
456 fprintf(stderr, "FAIL %s\n", p);
458 moan("%s check failed for `%s'", gch->name, p);
463 fprintf(stderr, "OK %s\n", p);
471 if ((f & f_verbose) && nfail)
472 moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name);
474 moan("no files checked");
478 static int dohash(const char *file, unsigned f, const gchash *gch)
481 octet *p = xmalloc(gch->hashsz);
483 if (fhash(file, f, gch, p)) {
484 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
487 puthex(p, gch->hashsz, stdout);
490 fputc(f & f_binary ? '*' : ' ', stdout);
492 putstring(stdout, file, 0);
503 static int dofile(const char *file, unsigned f, const gchash *gch)
505 return (f & f_check ? checkhash : dohash)(file, f, gch);
508 static int hashfiles(const char *file, unsigned f, const gchash *gch)
517 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
518 moan("couldn't open `%s': %s", file, strerror(errno));
519 return (EXIT_FAILURE);
524 if (getstring(fp, 0, &d, f & f_raw))
526 if ((rrc = dofile(d.buf, f, gch)) != 0)
533 static int hashsum(const char *file, unsigned f, const gchash *gch)
535 return (f & f_files ? hashfiles : dofile)(file, f, gch);
538 /*----- Main driver -------------------------------------------------------*/
540 static void version(FILE *fp)
542 pquis(fp, "$, Catacomb version " VERSION "\n");
545 static void usage(FILE *fp)
547 pquis(fp, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
550 static void help(FILE *fp, const gchash *gch)
556 Generates or checks message digests on files. Options available:\n\
558 -h, --help Display this help message.\n\
559 -V, --version Display program's version number.\n\
560 -u, --usage Display a terse usage message.\n\
562 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
564 -f, --files Read a list of file names from standard input.\n\
565 -0, --null File names are null terminated, not plain text.\n\
567 -e, --escape Escape funny characters in filenames.\n\
568 -c, --check Check message digests rather than emitting them.\n\
569 -b, --binary When reading files, treat them as binary.\n\
570 -v, --verbose Be verbose when checking digests.\n\
572 For a list of supported message digest algorithms, type `$ --list'.\n\
575 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
578 int main(int argc, char *argv[])
581 const gchash *gch = 0;
584 /* --- Initialization --- */
589 /* --- Choose a hash function from the name --- */
592 char *q = xstrdup(QUIS);
593 size_t len = strlen(q);
594 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
603 /* --- Read options --- */
606 static struct option opts[] = {
607 { "help", 0, 0, 'h' },
608 { "verbose", 0, 0, 'V' },
609 { "usage", 0, 0, 'u' },
611 { "algorithm", OPTF_ARGREQ, 0, 'a' },
612 { "hash", OPTF_ARGREQ, 0, 'a' },
613 { "list", 0, 0, 'l' },
615 { "files", 0, 0, 'f' },
616 { "find", 0, 0, 'f' },
617 { "null", 0, 0, '0' },
619 { "escape", 0, 0, 'e' },
620 { "check", 0, 0, 'c' },
621 { "binary", 0, 0, 'b' },
622 { "verbose", 0, 0, 'v' },
626 int i = mdwopt(argc, argv, "hVu a:l f0 ecbv", opts, 0, 0, 0);
641 if ((gch = gethash(optarg)) == 0)
642 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
647 for (j = 0; hashtab[j]; j++) {
650 printf("%s", hashtab[j]->name);
686 /* --- Generate output --- */
688 if (!(f & f_check)) {
690 printf("#hash %s\n", gch->name);
692 fputs("#escape\n", stdout);
699 for (i = 0; i < argc; i++) {
700 if ((rrc = hashsum(argv[i], f, gch)) != 0)
704 rc = hashsum(0, f, gch);
709 /*----- That's all, folks -------------------------------------------------*/