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 ------------------------------------------------------*/
32 #define _FILE_OFFSET_BITS 64
42 #include <mLib/alloc.h>
43 #include <mLib/dstr.h>
44 #include <mLib/mdwopt.h>
45 #include <mLib/quis.h>
46 #include <mLib/report.h>
52 /*----- Static variables --------------------------------------------------*/
62 /*----- Guts --------------------------------------------------------------*/
64 static int checkjunk(const char *path, const struct stat *st, void *p)
70 if (fh->f & f_verbose)
71 fprintf(stderr, "JUNK (error %s) %s\n", strerror(errno), path);
73 moan("couldn't stat junk file `%s': %s", path, strerror(errno));
75 what = describefile(st);
76 if (fh->f & f_verbose)
77 fprintf(stderr, "JUNK %s %s\n", what, path);
79 moan("found junk %s `%s'", what, path);
84 static int warnjunk(const char *path, const struct stat *st, void *p)
87 moan("unexpected %s `%s'", describefile(st), path);
89 moan("couldn't stat unexpected file `%s': %s", path, strerror(errno));
93 static int checkhash(fhashstate *fh, const char *file, const encodeops *e)
99 unsigned long n = 0, nfail = 0;
102 if (!file || strcmp(file, "-") == 0)
104 else if ((hfp.fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
105 moan("couldn't open `%s': %s", file, strerror(errno));
106 return (EXIT_FAILURE);
111 hfp.hbuf = xmalloc(2 * fh->gch->hashsz);
116 while ((hf = hfparse(&hfp)) != HF_EOF) {
120 hfp.hbuf = xmalloc(2 * hfp.gch->hashsz);
123 if (fhash(fh, df.buf, hfp.hbuf + hfp.gch->hashsz)) {
124 moan("couldn't read `%s': %s", df.buf, strerror(errno));
128 if (memcmp(hfp.hbuf, hfp.hbuf + hfp.gch->hashsz,
129 hfp.gch->hashsz) != 0) {
130 if (hfp.f & f_verbose)
131 fprintf(stderr, "FAIL %s\n", df.buf);
133 moan("%s check failed for `%s'", hfp.gch->name, df.buf);
137 if (hfp.f & f_verbose)
138 fprintf(stderr, "OK %s\n", df.buf);
144 if (ferror(hfp.fp)) {
145 moan("error reading input `%s': %s",
146 file ? file : "<stdin>", strerror(errno));
152 if ((fh->f & f_verbose) && nfail)
153 moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
155 moan("no files checked");
159 static int dohash(fhashstate *fh, const char *file, const encodeops *e)
162 octet *p = xmalloc(fh->gch->hashsz);
164 if (fhash(fh, file, p)) {
165 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
168 e->put(p, fh->gch->hashsz, stdout);
171 fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout);
172 if (fh->f & f_escape)
173 putstring(stdout, file, 0);
184 static int dofile(fhashstate *fh, const char *file, const encodeops *e)
185 { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); }
187 static int hashfiles(fhashstate *fh, const char *file, const encodeops *e)
194 if (!file || strcmp(file, "-") == 0)
196 else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
197 moan("couldn't open `%s': %s", file, strerror(errno));
198 return (EXIT_FAILURE);
203 if (getstring(fp, &d, GSF_FILE | fh->f))
205 if ((rrc = dofile(fh, d.buf, e)) != 0)
212 static int hashsum(fhashstate *fh, const char *file, const encodeops *e)
213 { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); }
215 /*----- Main driver -------------------------------------------------------*/
217 void version(FILE *fp)
219 pquis(fp, "$, Catacomb version " VERSION "\n");
222 static void usage(FILE *fp)
224 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
227 static void help(FILE *fp, const gchash *gch)
233 Generates or checks message digests on files. Options available:\n\
235 -h, --help Display this help message.\n\
236 -V, --version Display program's version number.\n\
237 -u, --usage Display a terse usage message.\n\
238 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
240 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
241 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
243 -f, --files Read a list of file names from standard input.\n\
244 -0, --null File names are null terminated, not plain text.\n\
246 -e, --escape Escape funny characters in filenames.\n\
247 -c, --check Check message digests rather than emitting them.\n\
248 -b, --binary When reading files, treat them as binary.\n\
249 -p, --progress Show a progress indicator for large files.\n\
250 -v, --verbose Be verbose when checking digests.\n\
252 For a list of hashing algorithms and encodings, type `$ --list'.\n\
255 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
259 LI("Lists", list, listtab[i].name, listtab[i].name) \
260 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
261 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
263 MAKELISTTAB(listtab, LISTS)
265 int main(int argc, char *argv[])
268 const encodeops *e = &encodingtab[ENC_HEX];
271 /* --- Initialization --- */
275 fhash_init(&fh, 0, 0);
277 /* --- Choose a hash function from the name --- */
280 char *q = xstrdup(QUIS);
281 size_t len = strlen(q);
282 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
287 fh.gch = gethash("md5");
291 /* --- Read options --- */
294 static struct option opts[] = {
295 { "help", 0, 0, 'h' },
296 { "verbose", 0, 0, 'V' },
297 { "usage", 0, 0, 'u' },
299 { "algorithm", OPTF_ARGREQ, 0, 'a' },
300 { "hash", OPTF_ARGREQ, 0, 'a' },
301 { "encoding", OPTF_ARGREQ, 0, 'E' },
302 { "list", 0, 0, 'l' },
304 { "files", 0, 0, 'f' },
305 { "find", 0, 0, 'f' },
306 { "null", 0, 0, '0' },
308 { "escape", 0, 0, 'e' },
309 { "check", 0, 0, 'c' },
310 { "junk", 0, 0, 'j' },
311 { "binary", 0, 0, 'b' },
312 { "verbose", 0, 0, 'v' },
313 { "progress", 0, 0, 'p' },
317 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecjbvp", opts, 0, 0, 0);
323 help(stdout, fh.gch);
332 exit(displaylists(listtab, argv + optind));
334 if ((fh.gch = gethash(optarg)) == 0)
335 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
339 if ((e = getencoding(optarg)) == 0)
340 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
365 fh.f |= FHF_PROGRESS;
373 if (fh.f & f_bogus) {
380 /* --- Generate output --- */
382 if (!(fh.f & f_check) && (argc || (fh.f & f_files))) {
383 if (fh.f & f_oddhash) printf("#hash %s\n", fh.gch->name);
384 if (fh.f & f_oddenc) printf("#encoding %s\n", e->name);
385 if (fh.f & f_escape) fputs("#escape\n", stdout);
388 rc = hashsum(&fh, 0, e);
394 for (i = 0; i < argc; i++) {
395 if ((rrc = hashsum(&fh, argv[i], e)) != 0)
400 if (fh.f & FHF_JUNK) {
401 if (fh.f & f_check) {
402 if (fhash_junk(&fh, checkjunk, &fh)) rc = EXIT_FAILURE;
404 if (fhash_junk(&fh, warnjunk, 0) < 0) rc = EXIT_FAILURE;
412 /*----- That's all, folks -------------------------------------------------*/