8419779fcd0d18c9a4ba1737ff49dd8afcd571d2
[u/mdw/catacomb] / hashsum.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Hash files using some secure hash function
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
30 /*----- Header files ------------------------------------------------------*/
31
32 #define _FILE_OFFSET_BITS 64
33
34 #include "config.h"
35
36 #include <assert.h>
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>
48
49 #include "ghash.h"
50 #include "cc.h"
51
52 /*----- Static variables --------------------------------------------------*/
53
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
61
62 /*----- Guts --------------------------------------------------------------*/
63
64 static int checkhash(fhashstate *fh, const char *file, const encodeops *e)
65 {
66 int rc;
67 hfpctx hfp;
68 dstr dl = DSTR_INIT;
69 dstr df = DSTR_INIT;
70 unsigned long n = 0, nfail = 0;
71 int hf;
72
73 if (!file || strcmp(file, "-") == 0)
74 hfp.fp = stdin;
75 else if ((hfp.fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
76 moan("couldn't open `%s': %s", file, strerror(errno));
77 return (EXIT_FAILURE);
78 }
79
80 hfp.dline = &dl;
81 hfp.dfile = &df;
82 hfp.hbuf = xmalloc(2 * fh->gch->hashsz);
83 hfp.gch = fh->gch;
84 hfp.ee = e;
85 hfp.f = fh->f;
86
87 while ((hf = hfparse(&hfp)) != HF_EOF) {
88 switch (hf) {
89 case HF_HASH:
90 xfree(hfp.hbuf);
91 hfp.hbuf = xmalloc(2 * hfp.gch->hashsz);
92 break;
93 case HF_FILE:
94 if (fhash(fh, df.buf, hfp.hbuf + hfp.gch->hashsz)) {
95 moan("couldn't read `%s': %s", df.buf, strerror(errno));
96 rc = EXIT_FAILURE;
97 continue;
98 }
99 if (memcmp(hfp.hbuf, hfp.hbuf + hfp.gch->hashsz,
100 hfp.gch->hashsz) != 0) {
101 if (hfp.f & f_verbose)
102 fprintf(stderr, "FAIL %s\n", df.buf);
103 else
104 moan("%s check failed for `%s'", hfp.gch->name, df.buf);
105 nfail++;
106 rc = EXIT_FAILURE;
107 } else {
108 if (hfp.f & f_verbose)
109 fprintf(stderr, "OK %s\n", df.buf);
110 }
111 n++;
112 }
113 }
114
115 if (ferror(hfp.fp)) {
116 moan("error reading input `%s': %s",
117 file ? file : "<stdin>", strerror(errno));
118 rc = EXIT_FAILURE;
119 }
120 dstr_destroy(&dl);
121 dstr_destroy(&df);
122 xfree(hfp.hbuf);
123 if ((fh->f & f_verbose) && nfail)
124 moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
125 else if (!n)
126 moan("no files checked");
127 return (rc);
128 }
129
130 static int dohash(fhashstate *fh, const char *file, const encodeops *e)
131 {
132 int rc = 0;
133 octet *p = xmalloc(fh->gch->hashsz);
134
135 if (fhash(fh, file, p)) {
136 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
137 rc = EXIT_FAILURE;
138 } else {
139 e->put(p, fh->gch->hashsz, stdout);
140 if (file) {
141 fputc(' ', stdout);
142 fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout);
143 if (fh->f & f_escape)
144 putstring(stdout, file, 0);
145 else
146 fputs(file, stdout);
147 }
148 fputc('\n', stdout);
149 }
150
151 xfree(p);
152 return (rc);
153 }
154
155 static int dofile(fhashstate *fh, const char *file, const encodeops *e)
156 { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); }
157
158 static int hashfiles(fhashstate *fh, const char *file, const encodeops *e)
159 {
160 FILE *fp;
161 dstr d = DSTR_INIT;
162 int rc = 0;
163 int rrc;
164
165 if (!file || strcmp(file, "-") == 0)
166 fp = stdin;
167 else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
168 moan("couldn't open `%s': %s", file, strerror(errno));
169 return (EXIT_FAILURE);
170 }
171
172 for (;;) {
173 DRESET(&d);
174 if (getstring(fp, &d, GSF_FILE | fh->f))
175 break;
176 if ((rrc = dofile(fh, d.buf, e)) != 0)
177 rc = rrc;
178 }
179
180 return (rc);
181 }
182
183 static int hashsum(fhashstate *fh, const char *file, const encodeops *e)
184 { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); }
185
186 /*----- Main driver -------------------------------------------------------*/
187
188 void version(FILE *fp)
189 {
190 pquis(fp, "$, Catacomb version " VERSION "\n");
191 }
192
193 static void usage(FILE *fp)
194 {
195 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
196 }
197
198 static void help(FILE *fp, const gchash *gch)
199 {
200 version(fp);
201 fputc('\n', fp);
202 usage(fp);
203 pquis(fp, "\n\
204 Generates or checks message digests on files. Options available:\n\
205 \n\
206 -h, --help Display this help message.\n\
207 -V, --version Display program's version number.\n\
208 -u, --usage Display a terse usage message.\n\
209 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
210 \n\
211 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
212 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
213 \n\
214 -f, --files Read a list of file names from standard input.\n\
215 -0, --null File names are null terminated, not plain text.\n\
216 \n\
217 -e, --escape Escape funny characters in filenames.\n\
218 -c, --check Check message digests rather than emitting them.\n\
219 -b, --binary When reading files, treat them as binary.\n\
220 -v, --verbose Be verbose when checking digests.\n\
221 \n\
222 For a list of hashing algorithms and encodings, type `$ --list'.\n\
223 ");
224 if (gch)
225 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
226 }
227
228 #define LISTS(LI) \
229 LI("Lists", list, listtab[i].name, listtab[i].name) \
230 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
231 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
232
233 MAKELISTTAB(listtab, LISTS)
234
235 int main(int argc, char *argv[])
236 {
237 fhashstate fh;
238 const encodeops *e = &encodingtab[ENC_HEX];
239 int rc;
240
241 /* --- Initialization --- */
242
243 ego(argv[0]);
244 sub_init();
245 fhash_init(&fh, 0, 0);
246
247 /* --- Choose a hash function from the name --- */
248
249 {
250 char *q = xstrdup(QUIS);
251 size_t len = strlen(q);
252 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
253 q[len - 3] = 0;
254 fh.gch = gethash(q);
255 }
256 if (!fh.gch)
257 fh.gch = gethash("md5");
258 xfree(q);
259 }
260
261 /* --- Read options --- */
262
263 for (;;) {
264 static struct option opts[] = {
265 { "help", 0, 0, 'h' },
266 { "verbose", 0, 0, 'V' },
267 { "usage", 0, 0, 'u' },
268
269 { "algorithm", OPTF_ARGREQ, 0, 'a' },
270 { "hash", OPTF_ARGREQ, 0, 'a' },
271 { "encoding", OPTF_ARGREQ, 0, 'E' },
272 { "list", 0, 0, 'l' },
273
274 { "files", 0, 0, 'f' },
275 { "find", 0, 0, 'f' },
276 { "null", 0, 0, '0' },
277
278 { "escape", 0, 0, 'e' },
279 { "check", 0, 0, 'c' },
280 { "binary", 0, 0, 'b' },
281 { "verbose", 0, 0, 'v' },
282 { "progress", 0, 0, 'p' },
283
284 { 0, 0, 0, 0 }
285 };
286 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecbvp", opts, 0, 0, 0);
287 if (i < 0)
288 break;
289
290 switch (i) {
291 case 'h':
292 help(stdout, fh.gch);
293 exit(0);
294 case 'V':
295 version(stdout);
296 exit(0);
297 case 'u':
298 usage(stdout);
299 exit(0);
300 case 'l':
301 exit(displaylists(listtab, argv + optind));
302 case 'a':
303 if ((fh.gch = gethash(optarg)) == 0)
304 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
305 fh.f |= f_oddhash;
306 break;
307 case 'E':
308 if ((e = getencoding(optarg)) == 0)
309 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
310 fh.f |= f_oddenc;
311 break;
312 case 'f':
313 fh.f |= f_files;
314 break;
315 case '0':
316 fh.f |= GSF_RAW;
317 break;
318 case 'e':
319 fh.f |= f_escape;
320 break;
321 case 'c':
322 fh.f |= f_check;
323 break;
324 case 'b':
325 fh.f |= FHF_BINARY;
326 break;
327 case 'v':
328 fh.f |= f_verbose;
329 break;
330 case 'p':
331 fh.f |= FHF_PROGRESS;
332 break;
333 default:
334 fh.f |= f_bogus;
335 break;
336 }
337 }
338
339 if (fh.f & f_bogus) {
340 usage(stderr);
341 exit(EXIT_FAILURE);
342 }
343 argv += optind;
344 argc -= optind;
345
346 /* --- Generate output --- */
347
348 if (!(fh.f & f_check) && (argc || (fh.f & f_files))) {
349 if (fh.f & f_oddhash) printf("#hash %s\n", fh.gch->name);
350 if (fh.f & f_oddenc) printf("#encoding %s\n", e->name);
351 if (fh.f & f_escape) fputs("#escape\n", stdout);
352 }
353 if (!argc)
354 rc = hashsum(&fh, 0, e);
355 else {
356 int i;
357 int rrc;
358
359 rc = 0;
360 for (i = 0; i < argc; i++) {
361 if ((rrc = hashsum(&fh, argv[i], e)) != 0)
362 rc = rrc;
363 }
364 }
365
366 return (rc);
367 }
368
369 /*----- That's all, folks -------------------------------------------------*/