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 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/alloc.h>
42 #include <mLib/dstr.h>
43 #include <mLib/mdwopt.h>
44 #include <mLib/quis.h>
45 #include <mLib/report.h>
50 #include <mLib/base32.h>
51 #include <mLib/base64.h>
56 /*----- Static variables --------------------------------------------------*/
68 /*----- Encoding and decoding ---------------------------------------------*/
70 /* --- Hex encoding --- */
72 static void puthex(const octet *buf, size_t sz, FILE *fp)
75 fprintf(fp, "%02x", *buf++);
80 static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
84 isxdigit((unsigned char)p[0]) &&
85 isxdigit((unsigned char)p[1])) {
90 *q++ = strtoul(buf, 0, 16);
100 /* --- Base64 encoding --- */
102 static void putb64(const octet *buf, size_t sz, FILE *fp)
110 base64_encode(&b, buf, sz, &d);
111 base64_encode(&b, 0, 0, &d);
116 static size_t getb64(const char *p, octet *q, size_t sz, char **pp)
120 size_t n = strlen(p);
123 base64_decode(&b, p, n, &d);
124 if (pp) *pp = (/*unconst*/ char *)p + n;
125 base64_decode(&b, 0, 0, &d);
127 memcpy(q, d.buf, sz);
133 /* --- Base32 encoding --- */
135 static void putb32(const octet *buf, size_t sz, FILE *fp)
143 base32_encode(&b, buf, sz, &d);
144 base32_encode(&b, 0, 0, &d);
149 static size_t getb32(const char *p, octet *q, size_t sz, char **pp)
153 size_t n = strlen(p);
156 base32_decode(&b, p, n, &d);
157 if (pp) *pp = (/*unconst*/ char *)p + n;
158 base32_decode(&b, 0, 0, &d);
160 memcpy(q, d.buf, sz);
168 typedef struct encodeops {
170 void (*put)(const octet *, size_t, FILE *);
171 size_t (*get)(const char *, octet *, size_t, char **);
174 static const encodeops encodingtab[] = {
175 { "hex", puthex, gethex },
176 { "base64", putb64, getb64 },
177 { "base32", putb32, getb32 },
181 static const encodeops *getencoding(const char *ename)
185 for (e = encodingtab; e->name; e++) {
186 if (strcmp(ename, e->name) == 0)
192 /*----- Support functions -------------------------------------------------*/
196 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
197 * @unsigned f@ = flags to set
198 * @const gchash *gch@ = pointer to hash function to use
199 * @void *buf@ = pointer to hash output buffer
201 * Returns: Zero if it worked, nonzero on error.
203 * Use: Hashes a file.
206 static int fhash(const char *file, unsigned f, const gchash *gch, void *buf)
214 if (!file || strcmp(file, "-") == 0)
216 else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0)
220 while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0)
221 GH_HASH(h, fbuf, sz);
230 /* --- @gethash@ --- *
232 * Arguments: @const char *name@ = pointer to name string
234 * Returns: Pointer to appropriate hash class.
236 * Use: Chooses a hash function by name.
239 static const gchash *gethash(const char *name)
241 const gchash *const *g, *gg = 0;
242 size_t sz = strlen(name);
243 for (g = ghashtab; *g; g++) {
244 if (strncmp(name, (*g)->name, sz) == 0) {
245 if ((*g)->name[sz] == 0) {
257 /* --- @getstring@ --- *
259 * Arguments: @FILE *fp@ = stream from which to read
260 * @const char *p@ = string to read from instead
261 * @dstr *d@ = destination string
262 * @unsigned raw@ = raw or cooked read
264 * Returns: Zero if OK, nonzero on end-of-file.
266 * Use: Reads a filename (or something similar) from a stream.
269 static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw)
274 /* --- Raw: just read exactly what's written up to a null byte --- */
276 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
277 #define EOFCH (fp ? EOF : 0)
280 if ((ch = NEXTCH) == EOFCH)
286 if ((ch = NEXTCH) == EOFCH)
293 /* --- Skip as far as whitespace --- *
295 * Also skip past comments.
303 do ch = NEXTCH; while (ch != '\n' && ch != EOFCH);
309 /* --- If the character is a quote then read a quoted string --- */
321 /* --- Now read all sorts of interesting things --- */
325 /* --- Handle an escaped thing --- */
332 case 'a': ch = '\a'; break;
333 case 'b': ch = '\b'; break;
334 case 'f': ch = '\f'; break;
335 case 'n': ch = '\n'; break;
336 case 'r': ch = '\r'; break;
337 case 't': ch = '\t'; break;
338 case 'v': ch = '\v'; break;
345 /* --- If it's a quote or some other end marker then stop --- */
349 if (!q && isspace(ch))
352 /* --- Otherwise contribute and continue --- */
355 if ((ch = NEXTCH) == EOFCH)
368 /* --- @putstring@ --- *
370 * Arguments: @FILE *fp@ = stream to write on
371 * @const char *p@ = pointer to text
372 * @unsigned raw@ = whether the string is to be written raw
376 * Use: Emits a string to a stream.
379 static void putstring(FILE *fp, const char *p, unsigned raw)
381 size_t sz = strlen(p);
385 /* --- Just write the string null terminated if raw --- */
388 fwrite(p, 1, sz + 1, fp);
392 /* --- Check for any dodgy characters --- */
395 for (q = p; *q; q++) {
396 if (isspace((unsigned char)*q)) {
405 /* --- Emit the string --- */
407 for (q = p; *q; q++) {
409 case '\a': fputc('\\', fp); fputc('a', fp); break;
410 case '\b': fputc('\\', fp); fputc('b', fp); break;
411 case '\f': fputc('\\', fp); fputc('f', fp); break;
412 case '\n': fputc('\\', fp); fputc('n', fp); break;
413 case '\r': fputc('\\', fp); fputc('r', fp); break;
414 case '\t': fputc('\\', fp); fputc('t', fp); break;
415 case '\v': fputc('\\', fp); fputc('v', fp); break;
416 case '`': fputc('\\', fp); fputc('`', fp); break;
417 case '\'': fputc('\\', fp); fputc('\'', fp); break;
418 case '\"': fputc('\\', fp); fputc('\"', fp); break;
419 case '#': fputc('\\', fp); fputc('#', fp); break;
432 /*----- Guts --------------------------------------------------------------*/
434 static int checkhash(const char *file, unsigned f,
435 const gchash *gch, const encodeops *e)
441 unsigned long n = 0, nfail = 0;
442 octet *buf = xmalloc(2 * gch->hashsz);
444 if (!file || strcmp(file, "-") == 0)
446 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
447 moan("couldn't open `%s': %s", file, strerror(errno));
448 return (EXIT_FAILURE);
451 while (DRESET(&d), dstr_putline(&d, fp) != EOF) {
456 /* --- Handle a directive --- */
460 if ((q = str_getword(&p)) == 0)
462 if (strcmp(q, "hash") == 0) {
464 if ((q = str_getword(&p)) == 0)
466 if ((g = gethash(q)) == 0)
470 buf = xmalloc(2 * gch->hashsz);
471 } else if (strcmp(q, "encoding") == 0) {
473 if ((q = str_getword(&p)) == 0)
475 if ((ee = getencoding(q)) == 0)
478 } else if (strcmp(q, "escape") == 0)
483 /* --- Otherwise it's a hex thing --- */
486 while (*p && *p != ' ')
491 if (e->get(q, buf, gch->hashsz, 0) < gch->hashsz)
501 getstring(0, p, &dd, 0);
505 if (fhash(p, ff, gch, buf + gch->hashsz)) {
506 moan("couldn't read `%s': %s", p, strerror(errno));
510 if (memcmp(buf, buf + gch->hashsz, gch->hashsz) != 0) {
512 fprintf(stderr, "FAIL %s\n", p);
514 moan("%s check failed for `%s'", gch->name, p);
519 fprintf(stderr, "OK %s\n", p);
527 if ((f & f_verbose) && nfail)
528 moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name);
530 moan("no files checked");
534 static int dohash(const char *file, unsigned f,
535 const gchash *gch, const encodeops *e)
538 octet *p = xmalloc(gch->hashsz);
540 if (fhash(file, f, gch, p)) {
541 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
544 e->put(p, gch->hashsz, stdout);
547 fputc(f & f_binary ? '*' : ' ', stdout);
549 putstring(stdout, file, 0);
560 static int dofile(const char *file, unsigned f,
561 const gchash *gch, const encodeops *e)
563 return (f & f_check ? checkhash : dohash)(file, f, gch, e);
566 static int hashfiles(const char *file, unsigned f,
567 const gchash *gch, const encodeops *e)
574 if (!file || strcmp(file, "-") == 0)
576 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
577 moan("couldn't open `%s': %s", file, strerror(errno));
578 return (EXIT_FAILURE);
583 if (getstring(fp, 0, &d, f & f_raw))
585 if ((rrc = dofile(d.buf, f, gch, e)) != 0)
592 static int hashsum(const char *file, unsigned f,
593 const gchash *gch, const encodeops *e)
595 return (f & f_files ? hashfiles : dofile)(file, f, gch, e);
598 /*----- Main driver -------------------------------------------------------*/
600 void version(FILE *fp)
602 pquis(fp, "$, Catacomb version " VERSION "\n");
605 static void usage(FILE *fp)
607 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
610 static void help(FILE *fp, const gchash *gch)
616 Generates or checks message digests on files. Options available:\n\
618 -h, --help Display this help message.\n\
619 -V, --version Display program's version number.\n\
620 -u, --usage Display a terse usage message.\n\
621 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
623 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
624 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
626 -f, --files Read a list of file names from standard input.\n\
627 -0, --null File names are null terminated, not plain text.\n\
629 -e, --escape Escape funny characters in filenames.\n\
630 -c, --check Check message digests rather than emitting them.\n\
631 -b, --binary When reading files, treat them as binary.\n\
632 -v, --verbose Be verbose when checking digests.\n\
634 For a list of hashing algorithms and encodings, type `$ --list'.\n\
637 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
641 LI("Lists", list, listtab[i].name, listtab[i].name) \
642 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
643 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
645 MAKELISTTAB(listtab, LISTS)
647 int main(int argc, char *argv[])
650 const gchash *gch = 0;
651 const encodeops *e = &encodingtab[0];
654 /* --- Initialization --- */
659 /* --- Choose a hash function from the name --- */
662 char *q = xstrdup(QUIS);
663 size_t len = strlen(q);
664 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
669 gch = gethash("md5");
673 /* --- Read options --- */
676 static struct option opts[] = {
677 { "help", 0, 0, 'h' },
678 { "verbose", 0, 0, 'V' },
679 { "usage", 0, 0, 'u' },
681 { "algorithm", OPTF_ARGREQ, 0, 'a' },
682 { "hash", OPTF_ARGREQ, 0, 'a' },
683 { "encoding", OPTF_ARGREQ, 0, 'E' },
684 { "list", 0, 0, 'l' },
686 { "files", 0, 0, 'f' },
687 { "find", 0, 0, 'f' },
688 { "null", 0, 0, '0' },
690 { "escape", 0, 0, 'e' },
691 { "check", 0, 0, 'c' },
692 { "binary", 0, 0, 'b' },
693 { "verbose", 0, 0, 'v' },
697 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecbv", opts, 0, 0, 0);
712 exit(displaylists(listtab, argv + optind));
714 if ((gch = gethash(optarg)) == 0)
715 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
719 if ((e = getencoding(optarg)) == 0)
720 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
754 /* --- Generate output --- */
757 rc = hashsum(0, f, gch, e);
763 if (!(f & f_check)) {
764 if (f & f_oddhash) printf("#hash %s\n", gch->name);
765 if (f & f_oddenc) printf("#encoding %s\n", e->name);
766 if (f & f_escape) fputs("#escape\n", stdout);
768 for (i = 0; i < argc; i++) {
769 if ((rrc = hashsum(argv[i], f, gch, e)) != 0)
777 /*----- That's all, folks -------------------------------------------------*/