cc-hash.c: New file containing hash-related code from hashsum and dsig.
[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 #include <mLib/str.h>
49
50 #include "ghash.h"
51 #include "cc.h"
52
53 /*----- Static variables --------------------------------------------------*/
54
55 #define f_bogus 1u
56 #define f_verbose 2u
57 #define f_check 4u
58 #define f_files 8u
59 #define f_oddhash 16u
60 #define f_escape 32u
61 #define f_oddenc 64u
62
63 /*----- Support functions -------------------------------------------------*/
64
65 /* --- @gethash@ --- *
66 *
67 * Arguments: @const char *name@ = pointer to name string
68 *
69 * Returns: Pointer to appropriate hash class.
70 *
71 * Use: Chooses a hash function by name.
72 */
73
74 static const gchash *gethash(const char *name)
75 {
76 const gchash *const *g, *gg = 0;
77 size_t sz = strlen(name);
78 for (g = ghashtab; *g; g++) {
79 if (strncmp(name, (*g)->name, sz) == 0) {
80 if ((*g)->name[sz] == 0) {
81 gg = *g;
82 break;
83 } else if (gg)
84 return (0);
85 else
86 gg = *g;
87 }
88 }
89 return (gg);
90 }
91
92 /*----- Guts --------------------------------------------------------------*/
93
94 static int checkhash(const gchash *gch, unsigned f,
95 const char *file, const encodeops *e)
96 {
97 int rc;
98 FILE *fp;
99 dstr d = DSTR_INIT;
100 dstr dd = DSTR_INIT;
101 unsigned long n = 0, nfail = 0;
102 octet *buf = xmalloc(2 * gch->hashsz);
103
104 if (!file || strcmp(file, "-") == 0)
105 fp = stdin;
106 else if ((fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) {
107 moan("couldn't open `%s': %s", file, strerror(errno));
108 return (EXIT_FAILURE);
109 }
110
111 while (DRESET(&d), dstr_putline(&d, fp) != EOF) {
112 char *p = d.buf;
113 char *q;
114 unsigned ff = f;
115
116 /* --- Handle a directive --- */
117
118 if (*p == '#') {
119 p++;
120 if ((q = str_getword(&p)) == 0)
121 continue;
122 if (strcmp(q, "hash") == 0) {
123 const gchash *g;
124 if ((q = str_getword(&p)) == 0)
125 continue;
126 if ((g = gethash(q)) == 0)
127 continue;
128 gch = g;
129 xfree(buf);
130 buf = xmalloc(2 * gch->hashsz);
131 } else if (strcmp(q, "encoding") == 0) {
132 const encodeops *ee;
133 if ((q = str_getword(&p)) == 0)
134 continue;
135 if ((ee = getencoding(q)) == 0)
136 continue;
137 e = ee;
138 } else if (strcmp(q, "escape") == 0)
139 f |= f_escape;
140 continue;
141 }
142
143 /* --- Otherwise it's a hex thing --- */
144
145 q = p;
146 while (*p && *p != ' ')
147 p++;
148 if (!*p)
149 continue;
150 *p++ = 0;
151 if (e->get(q, buf, gch->hashsz, 0) < gch->hashsz)
152 continue;
153 if (*p == '*')
154 ff |= FHF_BINARY;
155 else if (*p != ' ')
156 continue;
157 p++;
158
159 if (f & f_escape) {
160 DRESET(&dd);
161 getstring(&p, &dd, GSF_STRING);
162 p = dd.buf;
163 }
164
165 if (fhash(gch, ff, p, buf + gch->hashsz)) {
166 moan("couldn't read `%s': %s", p, strerror(errno));
167 rc = EXIT_FAILURE;
168 continue;
169 }
170 if (memcmp(buf, buf + gch->hashsz, gch->hashsz) != 0) {
171 if (ff & f_verbose)
172 fprintf(stderr, "FAIL %s\n", p);
173 else
174 moan("%s check failed for `%s'", gch->name, p);
175 nfail++;
176 rc = EXIT_FAILURE;
177 } else {
178 if (ff & f_verbose)
179 fprintf(stderr, "OK %s\n", p);
180 }
181 n++;
182 }
183
184 dstr_destroy(&d);
185 dstr_destroy(&dd);
186 xfree(buf);
187 if ((f & f_verbose) && nfail)
188 moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name);
189 else if (!n)
190 moan("no files checked");
191 return (0);
192 }
193
194 static int dohash(const gchash *gch, unsigned f,
195 const char *file, const encodeops *e)
196 {
197 int rc = 0;
198 octet *p = xmalloc(gch->hashsz);
199
200 if (fhash(gch, f, file, p)) {
201 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
202 rc = EXIT_FAILURE;
203 } else {
204 e->put(p, gch->hashsz, stdout);
205 if (file) {
206 fputc(' ', stdout);
207 fputc(f & FHF_BINARY ? '*' : ' ', stdout);
208 if (f & f_escape)
209 putstring(stdout, file, 0);
210 else
211 fputs(file, stdout);
212 }
213 fputc('\n', stdout);
214 }
215
216 xfree(p);
217 return (rc);
218 }
219
220 static int dofile(const gchash *gch, unsigned f,
221 const char *file, const encodeops *e)
222 { return (f & f_check ? checkhash : dohash)(gch, f, file, e); }
223
224 static int hashfiles(const gchash *gch, unsigned f,
225 const char *file, const encodeops *e)
226 {
227 FILE *fp;
228 dstr d = DSTR_INIT;
229 int rc = 0;
230 int rrc;
231
232 if (!file || strcmp(file, "-") == 0)
233 fp = stdin;
234 else if ((fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) {
235 moan("couldn't open `%s': %s", file, strerror(errno));
236 return (EXIT_FAILURE);
237 }
238
239 for (;;) {
240 DRESET(&d);
241 if (getstring(fp, &d, GSF_FILE | f))
242 break;
243 if ((rrc = dofile(gch, f, d.buf, e)) != 0)
244 rc = rrc;
245 }
246
247 return (rc);
248 }
249
250 static int hashsum(const gchash *gch, unsigned f,
251 const char *file, const encodeops *e)
252 { return (f & f_files ? hashfiles : dofile)(gch, f, file, e); }
253
254 /*----- Main driver -------------------------------------------------------*/
255
256 void version(FILE *fp)
257 {
258 pquis(fp, "$, Catacomb version " VERSION "\n");
259 }
260
261 static void usage(FILE *fp)
262 {
263 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
264 }
265
266 static void help(FILE *fp, const gchash *gch)
267 {
268 version(fp);
269 fputc('\n', fp);
270 usage(fp);
271 pquis(fp, "\n\
272 Generates or checks message digests on files. Options available:\n\
273 \n\
274 -h, --help Display this help message.\n\
275 -V, --version Display program's version number.\n\
276 -u, --usage Display a terse usage message.\n\
277 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
278 \n\
279 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
280 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
281 \n\
282 -f, --files Read a list of file names from standard input.\n\
283 -0, --null File names are null terminated, not plain text.\n\
284 \n\
285 -e, --escape Escape funny characters in filenames.\n\
286 -c, --check Check message digests rather than emitting them.\n\
287 -b, --binary When reading files, treat them as binary.\n\
288 -v, --verbose Be verbose when checking digests.\n\
289 \n\
290 For a list of hashing algorithms and encodings, type `$ --list'.\n\
291 ");
292 if (gch)
293 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
294 }
295
296 #define LISTS(LI) \
297 LI("Lists", list, listtab[i].name, listtab[i].name) \
298 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
299 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
300
301 MAKELISTTAB(listtab, LISTS)
302
303 int main(int argc, char *argv[])
304 {
305 unsigned f = 0;
306 const gchash *gch = 0;
307 const encodeops *e = &encodingtab[0];
308 int rc;
309
310 /* --- Initialization --- */
311
312 ego(argv[0]);
313 sub_init();
314
315 /* --- Choose a hash function from the name --- */
316
317 {
318 char *q = xstrdup(QUIS);
319 size_t len = strlen(q);
320 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
321 q[len - 3] = 0;
322 gch = gethash(q);
323 }
324 if (!gch)
325 gch = gethash("md5");
326 xfree(q);
327 }
328
329 /* --- Read options --- */
330
331 for (;;) {
332 static struct option opts[] = {
333 { "help", 0, 0, 'h' },
334 { "verbose", 0, 0, 'V' },
335 { "usage", 0, 0, 'u' },
336
337 { "algorithm", OPTF_ARGREQ, 0, 'a' },
338 { "hash", OPTF_ARGREQ, 0, 'a' },
339 { "encoding", OPTF_ARGREQ, 0, 'E' },
340 { "list", 0, 0, 'l' },
341
342 { "files", 0, 0, 'f' },
343 { "find", 0, 0, 'f' },
344 { "null", 0, 0, '0' },
345
346 { "escape", 0, 0, 'e' },
347 { "check", 0, 0, 'c' },
348 { "binary", 0, 0, 'b' },
349 { "verbose", 0, 0, 'v' },
350 { "progress", 0, 0, 'p' },
351
352 { 0, 0, 0, 0 }
353 };
354 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecbvp", opts, 0, 0, 0);
355 if (i < 0)
356 break;
357
358 switch (i) {
359 case 'h':
360 help(stdout, gch);
361 exit(0);
362 case 'V':
363 version(stdout);
364 exit(0);
365 case 'u':
366 usage(stdout);
367 exit(0);
368 case 'l':
369 exit(displaylists(listtab, argv + optind));
370 case 'a':
371 if ((gch = gethash(optarg)) == 0)
372 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
373 f |= f_oddhash;
374 break;
375 case 'E':
376 if ((e = getencoding(optarg)) == 0)
377 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
378 f |= f_oddenc;
379 break;
380 case 'f':
381 f |= f_files;
382 break;
383 case '0':
384 f |= GSF_RAW;
385 break;
386 case 'e':
387 f |= f_escape;
388 break;
389 case 'c':
390 f |= f_check;
391 break;
392 case 'b':
393 f |= FHF_BINARY;
394 break;
395 case 'v':
396 f |= f_verbose;
397 break;
398 case 'p':
399 f |= FHF_PROGRESS;
400 break;
401 default:
402 f |= f_bogus;
403 break;
404 }
405 }
406
407 if (f & f_bogus) {
408 usage(stderr);
409 exit(EXIT_FAILURE);
410 }
411 argv += optind;
412 argc -= optind;
413
414 /* --- Generate output --- */
415
416 if (!(f & f_check) && (argc || (f & f_files))) {
417 if (f & f_oddhash) printf("#hash %s\n", gch->name);
418 if (f & f_oddenc) printf("#encoding %s\n", e->name);
419 if (f & f_escape) fputs("#escape\n", stdout);
420 }
421 if (!argc)
422 rc = hashsum(gch, f, 0, e);
423 else {
424 int i;
425 int rrc;
426
427 rc = 0;
428 for (i = 0; i < argc; i++) {
429 if ((rrc = hashsum(gch, f, argv[i], e)) != 0)
430 rc = rrc;
431 }
432 }
433
434 return (rc);
435 }
436
437 /*----- That's all, folks -------------------------------------------------*/