Merge branch '2.5.x'
[catacomb] / progs / hashsum.c
CommitLineData
e375fe33 1/* -*-c-*-
2 *
e375fe33 3 * Hash files using some secure hash function
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
e375fe33 9 *
10 * This file is part of Catacomb.
11 *
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.
45c0fd36 16 *
e375fe33 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.
45c0fd36 21 *
e375fe33 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,
25 * MA 02111-1307, USA.
26 */
27
e375fe33 28/*----- Header files ------------------------------------------------------*/
29
43d1332f
MW
30#define _FILE_OFFSET_BITS 64
31
e375fe33 32#include "config.h"
33
5685a696 34#include <assert.h>
e375fe33 35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
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>
45#include <mLib/sub.h>
e375fe33 46
47#include "ghash.h"
c65df279 48#include "cc.h"
e375fe33 49
e375fe33 50/*----- Static variables --------------------------------------------------*/
51
18b3351a
MW
52#define f_bogus 1u
53#define f_verbose 2u
54#define f_check 4u
55#define f_files 8u
56#define f_oddhash 16u
57#define f_escape 32u
58#define f_oddenc 64u
e375fe33 59
e375fe33 60/*----- Guts --------------------------------------------------------------*/
61
f5e91c02
MW
62static int checkjunk(const char *path, const struct stat *st, void *p)
63{
64 const char *what;
65 fhashstate *fh = p;
66
67 if (!st) {
68 if (fh->f & f_verbose)
69 fprintf(stderr, "JUNK (error %s) %s\n", strerror(errno), path);
70 else
71 moan("couldn't stat junk file `%s': %s", path, strerror(errno));
72 } else {
73 what = describefile(st);
74 if (fh->f & f_verbose)
75 fprintf(stderr, "JUNK %s %s\n", what, path);
76 else
77 moan("found junk %s `%s'", what, path);
78 }
79 return (0);
80}
81
82static int warnjunk(const char *path, const struct stat *st, void *p)
83{
84 if (st)
85 moan("unexpected %s `%s'", describefile(st), path);
86 else
87 moan("couldn't stat unexpected file `%s': %s", path, strerror(errno));
88 return (0);
89}
90
07290a45 91static int checkhash(fhashstate *fh, const char *file, const encodeops *e)
e375fe33 92{
24e27402 93 int rc = 0;
f377eee1
MW
94 hfpctx hfp;
95 dstr dl = DSTR_INIT;
96 dstr df = DSTR_INIT;
e375fe33 97 unsigned long n = 0, nfail = 0;
f377eee1 98 int hf;
e375fe33 99
d7e6bc66 100 if (!file || strcmp(file, "-") == 0)
f377eee1 101 hfp.fp = stdin;
07290a45 102 else if ((hfp.fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
e375fe33 103 moan("couldn't open `%s': %s", file, strerror(errno));
104 return (EXIT_FAILURE);
105 }
106
f377eee1
MW
107 hfp.dline = &dl;
108 hfp.dfile = &df;
07290a45
MW
109 hfp.hbuf = xmalloc(2 * fh->gch->hashsz);
110 hfp.gch = fh->gch;
f377eee1 111 hfp.ee = e;
07290a45 112 hfp.f = fh->f;
f377eee1
MW
113
114 while ((hf = hfparse(&hfp)) != HF_EOF) {
115 switch (hf) {
116 case HF_HASH:
117 xfree(hfp.hbuf);
118 hfp.hbuf = xmalloc(2 * hfp.gch->hashsz);
24e27402 119 fh->gch = hfp.gch;
f377eee1
MW
120 break;
121 case HF_FILE:
07290a45 122 if (fhash(fh, df.buf, hfp.hbuf + hfp.gch->hashsz)) {
f377eee1
MW
123 moan("couldn't read `%s': %s", df.buf, strerror(errno));
124 rc = EXIT_FAILURE;
5685a696 125 continue;
f377eee1
MW
126 }
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);
131 else
132 moan("%s check failed for `%s'", hfp.gch->name, df.buf);
133 nfail++;
134 rc = EXIT_FAILURE;
135 } else {
136 if (hfp.f & f_verbose)
137 fprintf(stderr, "OK %s\n", df.buf);
138 }
139 n++;
e375fe33 140 }
e375fe33 141 }
142
95d92463
MW
143 if (ferror(hfp.fp)) {
144 moan("error reading input `%s': %s",
145 file ? file : "<stdin>", strerror(errno));
146 rc = EXIT_FAILURE;
147 }
f377eee1
MW
148 dstr_destroy(&dl);
149 dstr_destroy(&df);
150 xfree(hfp.hbuf);
07290a45 151 if ((fh->f & f_verbose) && nfail)
f377eee1 152 moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
e375fe33 153 else if (!n)
154 moan("no files checked");
df8e52c7 155 return (rc);
e375fe33 156}
157
07290a45 158static int dohash(fhashstate *fh, const char *file, const encodeops *e)
e375fe33 159{
160 int rc = 0;
07290a45 161 octet *p = xmalloc(fh->gch->hashsz);
e375fe33 162
07290a45 163 if (fhash(fh, file, p)) {
e375fe33 164 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
165 rc = EXIT_FAILURE;
166 } else {
07290a45 167 e->put(p, fh->gch->hashsz, stdout);
e375fe33 168 if (file) {
169 fputc(' ', stdout);
07290a45
MW
170 fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout);
171 if (fh->f & f_escape)
e375fe33 172 putstring(stdout, file, 0);
173 else
174 fputs(file, stdout);
175 }
176 fputc('\n', stdout);
177 }
178
179 xfree(p);
180 return (rc);
181}
182
07290a45
MW
183static int dofile(fhashstate *fh, const char *file, const encodeops *e)
184 { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); }
12902a5c 185
07290a45 186static int hashfiles(fhashstate *fh, const char *file, const encodeops *e)
e375fe33 187{
188 FILE *fp;
189 dstr d = DSTR_INIT;
190 int rc = 0;
191 int rrc;
192
d7e6bc66 193 if (!file || strcmp(file, "-") == 0)
e375fe33 194 fp = stdin;
07290a45 195 else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
e375fe33 196 moan("couldn't open `%s': %s", file, strerror(errno));
197 return (EXIT_FAILURE);
198 }
199
200 for (;;) {
201 DRESET(&d);
07290a45 202 if (getstring(fp, &d, GSF_FILE | fh->f))
e375fe33 203 break;
07290a45 204 if ((rrc = dofile(fh, d.buf, e)) != 0)
e375fe33 205 rc = rrc;
206 }
207
208 return (rc);
209}
210
07290a45
MW
211static int hashsum(fhashstate *fh, const char *file, const encodeops *e)
212 { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); }
e375fe33 213
214/*----- Main driver -------------------------------------------------------*/
215
c65df279 216void version(FILE *fp)
e375fe33 217{
218 pquis(fp, "$, Catacomb version " VERSION "\n");
219}
220
221static void usage(FILE *fp)
222{
c65df279 223 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
e375fe33 224}
225
226static void help(FILE *fp, const gchash *gch)
227{
228 version(fp);
229 fputc('\n', fp);
230 usage(fp);
231 pquis(fp, "\n\
232Generates or checks message digests on files. Options available:\n\
233\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\
c65df279 237-l, --list [ITEM...] Show known hash functions and/or encodings.\n\
e375fe33 238\n\
239-a, --algorithm=ALG Use the message digest algorithm ALG.\n\
92c494ce 240-E, --encoding=ENC Represent hashes using encoding ENC.\n\
e375fe33 241\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\
244\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\
b2776fdf 248-p, --progress Show a progress indicator for large files.\n\
e375fe33 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 -------------------------------------------------*/