Cleanups.
[u/mdw/catacomb] / hashsum.c
CommitLineData
e375fe33 1/* -*-c-*-
2 *
5685a696 3 * $Id$
e375fe33 4 *
5 * Hash files using some secure hash function
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
e375fe33 11 *
12 * This file is part of Catacomb.
13 *
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.
45c0fd36 18 *
e375fe33 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.
45c0fd36 23 *
e375fe33 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,
27 * MA 02111-1307, USA.
28 */
29
e375fe33 30/*----- Header files ------------------------------------------------------*/
31
43d1332f
MW
32#define _FILE_OFFSET_BITS 64
33
e375fe33 34#include "config.h"
35
5685a696 36#include <assert.h>
e375fe33 37#include <errno.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
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>
47#include <mLib/sub.h>
e375fe33 48
49#include "ghash.h"
c65df279 50#include "cc.h"
e375fe33 51
e375fe33 52/*----- Static variables --------------------------------------------------*/
53
18b3351a
MW
54#define f_bogus 1u
55#define f_verbose 2u
56#define f_check 4u
57#define f_files 8u
58#define f_oddhash 16u
59#define f_escape 32u
60#define f_oddenc 64u
e375fe33 61
e375fe33 62/*----- Guts --------------------------------------------------------------*/
63
f5e91c02
MW
64static int checkjunk(const char *path, const struct stat *st, void *p)
65{
66 const char *what;
67 fhashstate *fh = p;
68
69 if (!st) {
70 if (fh->f & f_verbose)
71 fprintf(stderr, "JUNK (error %s) %s\n", strerror(errno), path);
72 else
73 moan("couldn't stat junk file `%s': %s", path, strerror(errno));
74 } else {
75 what = describefile(st);
76 if (fh->f & f_verbose)
77 fprintf(stderr, "JUNK %s %s\n", what, path);
78 else
79 moan("found junk %s `%s'", what, path);
80 }
81 return (0);
82}
83
84static int warnjunk(const char *path, const struct stat *st, void *p)
85{
86 if (st)
87 moan("unexpected %s `%s'", describefile(st), path);
88 else
89 moan("couldn't stat unexpected file `%s': %s", path, strerror(errno));
90 return (0);
91}
92
07290a45 93static int checkhash(fhashstate *fh, const char *file, const encodeops *e)
e375fe33 94{
95 int rc;
f377eee1
MW
96 hfpctx hfp;
97 dstr dl = DSTR_INIT;
98 dstr df = DSTR_INIT;
e375fe33 99 unsigned long n = 0, nfail = 0;
f377eee1 100 int hf;
e375fe33 101
d7e6bc66 102 if (!file || strcmp(file, "-") == 0)
f377eee1 103 hfp.fp = stdin;
07290a45 104 else if ((hfp.fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
e375fe33 105 moan("couldn't open `%s': %s", file, strerror(errno));
106 return (EXIT_FAILURE);
107 }
108
f377eee1
MW
109 hfp.dline = &dl;
110 hfp.dfile = &df;
07290a45
MW
111 hfp.hbuf = xmalloc(2 * fh->gch->hashsz);
112 hfp.gch = fh->gch;
f377eee1 113 hfp.ee = e;
07290a45 114 hfp.f = fh->f;
f377eee1
MW
115
116 while ((hf = hfparse(&hfp)) != HF_EOF) {
117 switch (hf) {
118 case HF_HASH:
119 xfree(hfp.hbuf);
120 hfp.hbuf = xmalloc(2 * hfp.gch->hashsz);
121 break;
122 case HF_FILE:
07290a45 123 if (fhash(fh, df.buf, hfp.hbuf + hfp.gch->hashsz)) {
f377eee1
MW
124 moan("couldn't read `%s': %s", df.buf, strerror(errno));
125 rc = EXIT_FAILURE;
5685a696 126 continue;
f377eee1
MW
127 }
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);
132 else
133 moan("%s check failed for `%s'", hfp.gch->name, df.buf);
134 nfail++;
135 rc = EXIT_FAILURE;
136 } else {
137 if (hfp.f & f_verbose)
138 fprintf(stderr, "OK %s\n", df.buf);
139 }
140 n++;
e375fe33 141 }
e375fe33 142 }
143
95d92463
MW
144 if (ferror(hfp.fp)) {
145 moan("error reading input `%s': %s",
146 file ? file : "<stdin>", strerror(errno));
147 rc = EXIT_FAILURE;
148 }
f377eee1
MW
149 dstr_destroy(&dl);
150 dstr_destroy(&df);
151 xfree(hfp.hbuf);
07290a45 152 if ((fh->f & f_verbose) && nfail)
f377eee1 153 moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
e375fe33 154 else if (!n)
155 moan("no files checked");
df8e52c7 156 return (rc);
e375fe33 157}
158
07290a45 159static int dohash(fhashstate *fh, const char *file, const encodeops *e)
e375fe33 160{
161 int rc = 0;
07290a45 162 octet *p = xmalloc(fh->gch->hashsz);
e375fe33 163
07290a45 164 if (fhash(fh, file, p)) {
e375fe33 165 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
166 rc = EXIT_FAILURE;
167 } else {
07290a45 168 e->put(p, fh->gch->hashsz, stdout);
e375fe33 169 if (file) {
170 fputc(' ', stdout);
07290a45
MW
171 fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout);
172 if (fh->f & f_escape)
e375fe33 173 putstring(stdout, file, 0);
174 else
175 fputs(file, stdout);
176 }
177 fputc('\n', stdout);
178 }
179
180 xfree(p);
181 return (rc);
182}
183
07290a45
MW
184static int dofile(fhashstate *fh, const char *file, const encodeops *e)
185 { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); }
12902a5c 186
07290a45 187static int hashfiles(fhashstate *fh, const char *file, const encodeops *e)
e375fe33 188{
189 FILE *fp;
190 dstr d = DSTR_INIT;
191 int rc = 0;
192 int rrc;
193
d7e6bc66 194 if (!file || strcmp(file, "-") == 0)
e375fe33 195 fp = stdin;
07290a45 196 else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
e375fe33 197 moan("couldn't open `%s': %s", file, strerror(errno));
198 return (EXIT_FAILURE);
199 }
200
201 for (;;) {
202 DRESET(&d);
07290a45 203 if (getstring(fp, &d, GSF_FILE | fh->f))
e375fe33 204 break;
07290a45 205 if ((rrc = dofile(fh, d.buf, e)) != 0)
e375fe33 206 rc = rrc;
207 }
208
209 return (rc);
210}
211
07290a45
MW
212static int hashsum(fhashstate *fh, const char *file, const encodeops *e)
213 { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); }
e375fe33 214
215/*----- Main driver -------------------------------------------------------*/
216
c65df279 217void version(FILE *fp)
e375fe33 218{
219 pquis(fp, "$, Catacomb version " VERSION "\n");
220}
221
222static void usage(FILE *fp)
223{
c65df279 224 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
e375fe33 225}
226
227static void help(FILE *fp, const gchash *gch)
228{
229 version(fp);
230 fputc('\n', fp);
231 usage(fp);
232 pquis(fp, "\n\
233Generates or checks message digests on files. Options available:\n\
234\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\
c65df279 238-l, --list [ITEM...] Show known hash functions and/or encodings.\n\
e375fe33 239\n\
240-a, --algorithm=ALG Use the message digest algorithm ALG.\n\
92c494ce 241-E, --encoding=ENC Represent hashes using encoding ENC.\n\
e375fe33 242\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\
245\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-v, --verbose Be verbose when checking digests.\n\
250\n\
92c494ce 251For a list of hashing algorithms and encodings, type `$ --list'.\n\
e375fe33 252");
253 if (gch)
254 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
255}
256
c65df279 257#define LISTS(LI) \
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)
261
262MAKELISTTAB(listtab, LISTS)
263
e375fe33 264int main(int argc, char *argv[])
265{
07290a45 266 fhashstate fh;
95d92463 267 const encodeops *e = &encodingtab[ENC_HEX];
e375fe33 268 int rc;
269
270 /* --- Initialization --- */
271
272 ego(argv[0]);
273 sub_init();
07290a45 274 fhash_init(&fh, 0, 0);
e375fe33 275
276 /* --- Choose a hash function from the name --- */
277
278 {
279 char *q = xstrdup(QUIS);
280 size_t len = strlen(q);
281 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
282 q[len - 3] = 0;
07290a45 283 fh.gch = gethash(q);
e375fe33 284 }
07290a45
MW
285 if (!fh.gch)
286 fh.gch = gethash("md5");
e375fe33 287 xfree(q);
288 }
289
290 /* --- Read options --- */
291
292 for (;;) {
293 static struct option opts[] = {
294 { "help", 0, 0, 'h' },
295 { "verbose", 0, 0, 'V' },
296 { "usage", 0, 0, 'u' },
297
298 { "algorithm", OPTF_ARGREQ, 0, 'a' },
299 { "hash", OPTF_ARGREQ, 0, 'a' },
5685a696 300 { "encoding", OPTF_ARGREQ, 0, 'E' },
e375fe33 301 { "list", 0, 0, 'l' },
302
303 { "files", 0, 0, 'f' },
304 { "find", 0, 0, 'f' },
305 { "null", 0, 0, '0' },
306
307 { "escape", 0, 0, 'e' },
308 { "check", 0, 0, 'c' },
f5e91c02 309 { "junk", 0, 0, 'j' },
e375fe33 310 { "binary", 0, 0, 'b' },
311 { "verbose", 0, 0, 'v' },
43d1332f 312 { "progress", 0, 0, 'p' },
e375fe33 313
314 { 0, 0, 0, 0 }
315 };
f5e91c02 316 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecjbvp", opts, 0, 0, 0);
e375fe33 317 if (i < 0)
318 break;
319
320 switch (i) {
321 case 'h':
07290a45 322 help(stdout, fh.gch);
e375fe33 323 exit(0);
324 case 'V':
325 version(stdout);
326 exit(0);
327 case 'u':
328 usage(stdout);
329 exit(0);
c65df279 330 case 'l':
331 exit(displaylists(listtab, argv + optind));
e375fe33 332 case 'a':
07290a45 333 if ((fh.gch = gethash(optarg)) == 0)
e375fe33 334 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
07290a45 335 fh.f |= f_oddhash;
e375fe33 336 break;
5685a696 337 case 'E':
c65df279 338 if ((e = getencoding(optarg)) == 0)
5685a696 339 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
07290a45 340 fh.f |= f_oddenc;
5685a696 341 break;
e375fe33 342 case 'f':
07290a45 343 fh.f |= f_files;
e375fe33 344 break;
345 case '0':
07290a45 346 fh.f |= GSF_RAW;
e375fe33 347 break;
348 case 'e':
07290a45 349 fh.f |= f_escape;
e375fe33 350 break;
351 case 'c':
07290a45 352 fh.f |= f_check;
e375fe33 353 break;
f5e91c02
MW
354 case 'j':
355 fh.f |= FHF_JUNK;
356 break;
e375fe33 357 case 'b':
07290a45 358 fh.f |= FHF_BINARY;
e375fe33 359 break;
360 case 'v':
07290a45 361 fh.f |= f_verbose;
e375fe33 362 break;
43d1332f 363 case 'p':
07290a45 364 fh.f |= FHF_PROGRESS;
43d1332f 365 break;
e375fe33 366 default:
07290a45 367 fh.f |= f_bogus;
e375fe33 368 break;
369 }
370 }
371
07290a45 372 if (fh.f & f_bogus) {
e375fe33 373 usage(stderr);
374 exit(EXIT_FAILURE);
375 }
376 argv += optind;
377 argc -= optind;
378
379 /* --- Generate output --- */
380
07290a45
MW
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);
db2d393e 385 }
92c494ce 386 if (!argc)
07290a45 387 rc = hashsum(&fh, 0, e);
92c494ce 388 else {
e375fe33 389 int i;
390 int rrc;
92c494ce 391
e375fe33 392 rc = 0;
393 for (i = 0; i < argc; i++) {
07290a45 394 if ((rrc = hashsum(&fh, argv[i], e)) != 0)
e375fe33 395 rc = rrc;
396 }
92c494ce 397 }
e375fe33 398
f5e91c02
MW
399 if (fh.f & FHF_JUNK) {
400 if (fh.f & f_check) {
401 if (fhash_junk(&fh, checkjunk, &fh)) rc = EXIT_FAILURE;
402 } else {
403 if (fhash_junk(&fh, warnjunk, 0) < 0) rc = EXIT_FAILURE;
404 }
405 }
406 fhash_free(&fh);
407
e375fe33 408 return (rc);
409}
410
411/*----- That's all, folks -------------------------------------------------*/