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