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);
122 if (fhash(fh, df.buf, hfp.hbuf + hfp.gch->hashsz)) {
123 moan("couldn't read `%s': %s", df.buf, strerror(errno));
127 if (memcmp(hfp.hbuf, hfp.hbuf + hfp.gch->hashsz,
128 hfp.gch->hashsz) != 0) {
129 if (hfp.f & f_verbose)
130 fprintf(stderr, "FAIL %s\n", df.buf);
132 moan("%s check failed for `%s'", hfp.gch->name, df.buf);
136 if (hfp.f & f_verbose)
137 fprintf(stderr, "OK %s\n", df.buf);
143 if (ferror(hfp.fp)) {
144 moan("error reading input `%s': %s",
145 file ? file : "<stdin>", strerror(errno));
151 if ((fh->f & f_verbose) && nfail)
152 moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
154 moan("no files checked");
158 static int dohash(fhashstate *fh, const char *file, const encodeops *e)
161 octet *p = xmalloc(fh->gch->hashsz);
163 if (fhash(fh, file, p)) {
164 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
167 e->put(p, fh->gch->hashsz, stdout);
170 fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout);
171 if (fh->f & f_escape)
172 putstring(stdout, file, 0);
183 static int dofile(fhashstate *fh, const char *file, const encodeops *e)
184 { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); }
186 static int hashfiles(fhashstate *fh, const char *file, const encodeops *e)
193 if (!file || strcmp(file, "-") == 0)
195 else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
196 moan("couldn't open `%s': %s", file, strerror(errno));
197 return (EXIT_FAILURE);
202 if (getstring(fp, &d, GSF_FILE | fh->f))
204 if ((rrc = dofile(fh, d.buf, e)) != 0)
211 static int hashsum(fhashstate *fh, const char *file, const encodeops *e)
212 { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); }
214 /*----- Main driver -------------------------------------------------------*/
216 void version(FILE *fp)
218 pquis(fp, "$, Catacomb version " VERSION "\n");
221 static void usage(FILE *fp)
223 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
226 static void help(FILE *fp, const gchash *gch)
232 Generates or checks message digests on files. Options available:\n\
234 -h, --help Display this help message.\n\
235 -V, --version Display program's version number.\n\
236 -u, --usage Display a terse usage message.\n\
237 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
239 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
240 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
242 -f, --files Read a list of file names from standard input.\n\
243 -0, --null File names are null terminated, not plain text.\n\
245 -e, --escape Escape funny characters in filenames.\n\
246 -c, --check Check message digests rather than emitting them.\n\
247 -b, --binary When reading files, treat them as binary.\n\
248 -p, --progress Show a progress indicator for large files.\n\
249 -v, --verbose Be verbose when checking digests.\n\
251 For a list of hashing algorithms and encodings, type `$ --list'.\n\
254 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
258 LI("Lists", list, listtab[i].name, listtab[i].name) \
259 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
260 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
262 MAKELISTTAB(listtab, LISTS)
264 int main(int argc, char *argv[])
267 const encodeops *e = &encodingtab[ENC_HEX];
270 /* --- Initialization --- */
274 fhash_init(&fh, 0, 0);
276 /* --- Choose a hash function from the name --- */
279 char *q = xstrdup(QUIS);
280 size_t len = strlen(q);
281 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
286 fh.gch = gethash("md5");
290 /* --- Read options --- */
293 static struct option opts[] = {
294 { "help", 0, 0, 'h' },
295 { "verbose", 0, 0, 'V' },
296 { "usage", 0, 0, 'u' },
298 { "algorithm", OPTF_ARGREQ, 0, 'a' },
299 { "hash", OPTF_ARGREQ, 0, 'a' },
300 { "encoding", OPTF_ARGREQ, 0, 'E' },
301 { "list", 0, 0, 'l' },
303 { "files", 0, 0, 'f' },
304 { "find", 0, 0, 'f' },
305 { "null", 0, 0, '0' },
307 { "escape", 0, 0, 'e' },
308 { "check", 0, 0, 'c' },
309 { "junk", 0, 0, 'j' },
310 { "binary", 0, 0, 'b' },
311 { "verbose", 0, 0, 'v' },
312 { "progress", 0, 0, 'p' },
316 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecjbvp", opts, 0, 0, 0);
322 help(stdout, fh.gch);
331 exit(displaylists(listtab, argv + optind));
333 if ((fh.gch = gethash(optarg)) == 0)
334 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
338 if ((e = getencoding(optarg)) == 0)
339 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
364 fh.f |= FHF_PROGRESS;
372 if (fh.f & f_bogus) {
379 /* --- Generate output --- */
381 if (!(fh.f & f_check) && (argc || (fh.f & f_files))) {
382 if (fh.f & f_oddhash) printf("#hash %s\n", fh.gch->name);
383 if (fh.f & f_oddenc) printf("#encoding %s\n", e->name);
384 if (fh.f & f_escape) fputs("#escape\n", stdout);
387 rc = hashsum(&fh, 0, e);
393 for (i = 0; i < argc; i++) {
394 if ((rrc = hashsum(&fh, argv[i], e)) != 0)
399 if (fh.f & FHF_JUNK) {
400 if (fh.f & f_check) {
401 if (fhash_junk(&fh, checkjunk, &fh)) rc = EXIT_FAILURE;
403 if (fhash_junk(&fh, warnjunk, 0) < 0) rc = EXIT_FAILURE;
411 /*----- That's all, folks -------------------------------------------------*/