dsig.c: Allow precomputed hashes to be read from a file.
[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
18b3351a
MW
64static int checkhash(const gchash *gch, unsigned f,
65 const char *file, const encodeops *e)
e375fe33 66{
67 int rc;
f377eee1
MW
68 hfpctx hfp;
69 dstr dl = DSTR_INIT;
70 dstr df = DSTR_INIT;
e375fe33 71 unsigned long n = 0, nfail = 0;
f377eee1 72 int hf;
e375fe33 73
d7e6bc66 74 if (!file || strcmp(file, "-") == 0)
f377eee1
MW
75 hfp.fp = stdin;
76 else if ((hfp.fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) {
e375fe33 77 moan("couldn't open `%s': %s", file, strerror(errno));
78 return (EXIT_FAILURE);
79 }
80
f377eee1
MW
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;
5685a696 98 continue;
f377eee1
MW
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++;
e375fe33 113 }
e375fe33 114 }
115
95d92463
MW
116 if (ferror(hfp.fp)) {
117 moan("error reading input `%s': %s",
118 file ? file : "<stdin>", strerror(errno));
119 rc = EXIT_FAILURE;
120 }
f377eee1
MW
121 dstr_destroy(&dl);
122 dstr_destroy(&df);
123 xfree(hfp.hbuf);
e375fe33 124 if ((f & f_verbose) && nfail)
f377eee1 125 moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
e375fe33 126 else if (!n)
127 moan("no files checked");
128 return (0);
129}
130
18b3351a
MW
131static int dohash(const gchash *gch, unsigned f,
132 const char *file, const encodeops *e)
e375fe33 133{
134 int rc = 0;
135 octet *p = xmalloc(gch->hashsz);
136
18b3351a 137 if (fhash(gch, f, file, p)) {
e375fe33 138 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
139 rc = EXIT_FAILURE;
140 } else {
5685a696 141 e->put(p, gch->hashsz, stdout);
e375fe33 142 if (file) {
143 fputc(' ', stdout);
18b3351a 144 fputc(f & FHF_BINARY ? '*' : ' ', stdout);
e375fe33 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
18b3351a
MW
157static 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); }
12902a5c 160
18b3351a
MW
161static int hashfiles(const gchash *gch, unsigned f,
162 const char *file, const encodeops *e)
e375fe33 163{
164 FILE *fp;
165 dstr d = DSTR_INIT;
166 int rc = 0;
167 int rrc;
168
d7e6bc66 169 if (!file || strcmp(file, "-") == 0)
e375fe33 170 fp = stdin;
18b3351a 171 else if ((fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) {
e375fe33 172 moan("couldn't open `%s': %s", file, strerror(errno));
173 return (EXIT_FAILURE);
174 }
175
176 for (;;) {
177 DRESET(&d);
18b3351a 178 if (getstring(fp, &d, GSF_FILE | f))
e375fe33 179 break;
18b3351a 180 if ((rrc = dofile(gch, f, d.buf, e)) != 0)
e375fe33 181 rc = rrc;
182 }
183
184 return (rc);
185}
186
18b3351a
MW
187static 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); }
e375fe33 190
191/*----- Main driver -------------------------------------------------------*/
192
c65df279 193void version(FILE *fp)
e375fe33 194{
195 pquis(fp, "$, Catacomb version " VERSION "\n");
196}
197
198static void usage(FILE *fp)
199{
c65df279 200 pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
e375fe33 201}
202
203static void help(FILE *fp, const gchash *gch)
204{
205 version(fp);
206 fputc('\n', fp);
207 usage(fp);
208 pquis(fp, "\n\
209Generates 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\
c65df279 214-l, --list [ITEM...] Show known hash functions and/or encodings.\n\
e375fe33 215\n\
216-a, --algorithm=ALG Use the message digest algorithm ALG.\n\
92c494ce 217-E, --encoding=ENC Represent hashes using encoding ENC.\n\
e375fe33 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\
92c494ce 227For a list of hashing algorithms and encodings, type `$ --list'.\n\
e375fe33 228");
229 if (gch)
230 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
231}
232
c65df279 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
238MAKELISTTAB(listtab, LISTS)
239
e375fe33 240int main(int argc, char *argv[])
241{
242 unsigned f = 0;
243 const gchash *gch = 0;
95d92463 244 const encodeops *e = &encodingtab[ENC_HEX];
e375fe33 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)
e9026a0a 262 gch = gethash("md5");
e375fe33 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' },
5685a696 276 { "encoding", OPTF_ARGREQ, 0, 'E' },
e375fe33 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' },
43d1332f 287 { "progress", 0, 0, 'p' },
e375fe33 288
289 { 0, 0, 0, 0 }
290 };
43d1332f 291 int i = mdwopt(argc, argv, "hVu a:E:l f0 ecbvp", opts, 0, 0, 0);
e375fe33 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);
c65df279 305 case 'l':
306 exit(displaylists(listtab, argv + optind));
e375fe33 307 case 'a':
308 if ((gch = gethash(optarg)) == 0)
309 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
310 f |= f_oddhash;
311 break;
5685a696 312 case 'E':
c65df279 313 if ((e = getencoding(optarg)) == 0)
5685a696 314 die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
315 f |= f_oddenc;
316 break;
e375fe33 317 case 'f':
318 f |= f_files;
319 break;
320 case '0':
18b3351a 321 f |= GSF_RAW;
e375fe33 322 break;
323 case 'e':
324 f |= f_escape;
325 break;
326 case 'c':
327 f |= f_check;
328 break;
329 case 'b':
18b3351a 330 f |= FHF_BINARY;
e375fe33 331 break;
332 case 'v':
333 f |= f_verbose;
334 break;
43d1332f 335 case 'p':
18b3351a 336 f |= FHF_PROGRESS;
43d1332f 337 break;
e375fe33 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
db2d393e
MW
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 }
92c494ce 358 if (!argc)
18b3351a 359 rc = hashsum(gch, f, 0, e);
92c494ce 360 else {
e375fe33 361 int i;
362 int rrc;
92c494ce 363
e375fe33 364 rc = 0;
365 for (i = 0; i < argc; i++) {
18b3351a 366 if ((rrc = hashsum(gch, f, argv[i], e)) != 0)
e375fe33 367 rc = rrc;
368 }
92c494ce 369 }
e375fe33 370
371 return (rc);
372}
373
374/*----- That's all, folks -------------------------------------------------*/