3 * Hash files using some secure hash function
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
40 #include <mLib/alloc.h>
41 #include <mLib/dstr.h>
42 #include <mLib/mdwopt.h>
43 #include <mLib/quis.h>
44 #include <mLib/report.h>
50 /*----- Static variables --------------------------------------------------*/
60 /*----- Guts --------------------------------------------------------------*/
62 static int checkjunk(const char *path, const struct stat *st, void *p)
68 if (fh->f & f_verbose)
69 fprintf(stderr, "JUNK (error %s) %s\n", strerror(errno), path);
71 moan("couldn't stat junk file `%s': %s", path, strerror(errno));
73 what = describefile(st);
74 if (fh->f & f_verbose)
75 fprintf(stderr, "JUNK %s %s\n", what, path);
77 moan("found junk %s `%s'", what, path);
82 static int warnjunk(const char *path, const struct stat *st, void *p)
85 moan("unexpected %s `%s'", describefile(st), path);
87 moan("couldn't stat unexpected file `%s': %s", path, strerror(errno));
91 static int checkhash(fhashstate *fh, const char *file, const encodeops *e)
97 unsigned long n = 0, nfail = 0;
100 if (!file || strcmp(file, "-") == 0)
102 else if ((hfp.fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
103 moan("couldn't open `%s': %s", file, strerror(errno));
104 return (EXIT_FAILURE);
109 hfp.hbuf = xmalloc(2 * fh->gch->hashsz);
114 while ((hf = hfparse(&hfp)) != HF_EOF) {
118 hfp.hbuf = xmalloc(2 * hfp.gch->hashsz);
121 if (fhash(fh, df.buf, hfp.hbuf + hfp.gch->hashsz)) {
122 moan("couldn't read `%s': %s", df.buf, strerror(errno));
126 if (memcmp(hfp.hbuf, hfp.hbuf + hfp.gch->hashsz,
127 hfp.gch->hashsz) != 0) {
128 if (hfp.f & f_verbose)
129 fprintf(stderr, "FAIL %s\n", df.buf);
131 moan("%s check failed for `%s'", hfp.gch->name, df.buf);
135 if (hfp.f & f_verbose)
136 fprintf(stderr, "OK %s\n", df.buf);
142 if (ferror(hfp.fp)) {
143 moan("error reading input `%s': %s",
144 file ? file : "<stdin>", strerror(errno));
150 if ((fh->f & f_verbose) && nfail)
151 moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
153 moan("no files checked");
157 static int dohash(fhashstate *fh, const char *file, const encodeops *e)
160 octet *p = xmalloc(fh->gch->hashsz);
162 if (fhash(fh, file, p)) {
163 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
166 e->put(p, fh->gch->hashsz, stdout);
169 fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout);
170 if (fh->f & f_escape)
171 putstring(stdout, file, 0);
182 static int dofile(fhashstate *fh, const char *file, const encodeops *e)
183 { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); }
185 static int hashfiles(fhashstate *fh, const char *file, const encodeops *e)
192 if (!file || strcmp(file, "-") == 0)
194 else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
195 moan("couldn't open `%s': %s", file, strerror(errno));
196 return (EXIT_FAILURE);
201 if (getstring(fp, &d, GSF_FILE | fh->f))
203 if ((rrc = dofile(fh, d.buf, e)) != 0)
210 static int hashsum(fhashstate *fh, const char *file, const encodeops *e)
211 { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); }
213 /*----- Main driver -------------------------------------------------------*/
215 void version(FILE *fp)
217 pquis(fp, "$, Catacomb version " VERSION "\n");
220 static void usage(FILE *fp)
222 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
225 static void help(FILE *fp, const gchash *gch)
231 Generates or checks message digests on files. Options available:\n\
233 -h, --help Display this help message.\n\
234 -V, --version Display program's version number.\n\
235 -u, --usage Display a terse usage message.\n\
236 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
238 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
239 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
241 -f, --files Read a list of file names from standard input.\n\
242 -0, --null File names are null terminated, not plain text.\n\
244 -e, --escape Escape funny characters in filenames.\n\
245 -c, --check Check message digests rather than emitting them.\n\
246 -b, --binary When reading files, treat them as binary.\n\
247 -p, --progress Show a progress indicator for large files.\n\
248 -v, --verbose Be verbose when checking digests.\n\
250 For a list of hashing algorithms and encodings, type `$ --list'.\n\
253 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
257 LI("Lists", list, listtab[i].name, listtab[i].name) \
258 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
259 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
261 MAKELISTTAB(listtab, LISTS)
263 int main(int argc, char *argv[])
266 const encodeops *e = &encodingtab[ENC_HEX];
269 /* --- Initialization --- */
273 fhash_init(&fh, 0, 0);
275 /* --- Choose a hash function from the name --- */
278 char *q = xstrdup(QUIS);
279 size_t len = strlen(q);
280 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
285 fh.gch = gethash("md5");
289 /* --- Read options --- */
292 static struct option opts[] = {
293 { "help", 0, 0, 'h' },
294 { "verbose", 0, 0, 'V' },
295 { "usage", 0, 0, 'u' },
297 { "algorithm", OPTF_ARGREQ, 0, 'a' },
298 { "hash", OPTF_ARGREQ, 0, 'a' },
299 { "encoding", OPTF_ARGREQ, 0, 'E' },
300 { "list", 0, 0, 'l' },
302 { "files", 0, 0, 'f' },
303 { "find", 0, 0, 'f' },
304 { "null", 0, 0, '0' },
306 { "escape", 0, 0, 'e' },
307 { "check", 0, 0, 'c' },
308 { "junk", 0, 0, 'j' },
309 { "binary", 0, 0, 'b' },
310 { "verbose", 0, 0, 'v' },
311 { "progress", 0, 0, 'p' },
315 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecjbvp", opts, 0, 0, 0);
321 help(stdout, fh.gch);
330 exit(displaylists(listtab, argv + optind));
332 if ((fh.gch = gethash(optarg)) == 0)
333 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
337 if ((e = getencoding(optarg)) == 0)
338 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
363 fh.f |= FHF_PROGRESS;
371 if (fh.f & f_bogus) {
378 /* --- Generate output --- */
380 if (!(fh.f & f_check) && (argc || (fh.f & f_files))) {
381 if (fh.f & f_oddhash) printf("#hash %s\n", fh.gch->name);
382 if (fh.f & f_oddenc) printf("#encoding %s\n", e->name);
383 if (fh.f & f_escape) fputs("#escape\n", stdout);
386 rc = hashsum(&fh, 0, e);
392 for (i = 0; i < argc; i++) {
393 if ((rrc = hashsum(&fh, argv[i], e)) != 0)
398 if (fh.f & FHF_JUNK) {
399 if (fh.f & f_check) {
400 if (fhash_junk(&fh, checkjunk, &fh)) rc = EXIT_FAILURE;
402 if (fhash_junk(&fh, warnjunk, 0) < 0) rc = EXIT_FAILURE;
410 /*----- That's all, folks -------------------------------------------------*/