5 * Hash files using some secure hash function
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
32 #define _FILE_OFFSET_BITS 64
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>
53 /*----- Static variables --------------------------------------------------*/
63 /*----- Support functions -------------------------------------------------*/
65 /* --- @gethash@ --- *
67 * Arguments: @const char *name@ = pointer to name string
69 * Returns: Pointer to appropriate hash class.
71 * Use: Chooses a hash function by name.
74 static const gchash
*gethash(const char *name
)
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) {
92 /*----- Guts --------------------------------------------------------------*/
94 static int checkhash(const gchash
*gch
, unsigned f
,
95 const char *file
, const encodeops
*e
)
101 unsigned long n
= 0, nfail
= 0;
102 octet
*buf
= xmalloc(2 * gch
->hashsz
);
104 if (!file
|| strcmp(file
, "-") == 0)
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
);
111 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
116 /* --- Handle a directive --- */
120 if ((q
= str_getword(&p
)) == 0)
122 if (strcmp(q
, "hash") == 0) {
124 if ((q
= str_getword(&p
)) == 0)
126 if ((g
= gethash(q
)) == 0)
130 buf
= xmalloc(2 * gch
->hashsz
);
131 } else if (strcmp(q
, "encoding") == 0) {
133 if ((q
= str_getword(&p
)) == 0)
135 if ((ee
= getencoding(q
)) == 0)
138 } else if (strcmp(q
, "escape") == 0)
143 /* --- Otherwise it's a hex thing --- */
146 while (*p
&& *p
!= ' ')
151 if (e
->get(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
161 getstring(&p
, &dd
, GSF_STRING
);
165 if (fhash(gch
, ff
, p
, buf
+ gch
->hashsz
)) {
166 moan("couldn't read `%s': %s", p
, strerror(errno
));
170 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
172 fprintf(stderr
, "FAIL %s\n", p
);
174 moan("%s check failed for `%s'", gch
->name
, p
);
179 fprintf(stderr
, "OK %s\n", p
);
187 if ((f
& f_verbose
) && nfail
)
188 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
190 moan("no files checked");
194 static int dohash(const gchash
*gch
, unsigned f
,
195 const char *file
, const encodeops
*e
)
198 octet
*p
= xmalloc(gch
->hashsz
);
200 if (fhash(gch
, f
, file
, p
)) {
201 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
204 e
->put(p
, gch
->hashsz
, stdout
);
207 fputc(f
& FHF_BINARY ?
'*' : ' ', stdout
);
209 putstring(stdout
, file
, 0);
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
); }
224 static int hashfiles(const gchash
*gch
, unsigned f
,
225 const char *file
, const encodeops
*e
)
232 if (!file
|| strcmp(file
, "-") == 0)
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
);
241 if (getstring(fp
, &d
, GSF_FILE
| f
))
243 if ((rrc
= dofile(gch
, f
, d
.buf
, e
)) != 0)
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
); }
254 /*----- Main driver -------------------------------------------------------*/
256 void version(FILE *fp
)
258 pquis(fp
, "$, Catacomb version " VERSION
"\n");
261 static void usage(FILE *fp
)
263 pquis(fp
, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
266 static void help(FILE *fp
, const gchash
*gch
)
272 Generates or checks message digests on files. Options available:\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\
279 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
280 -E, --encoding=ENC Represent hashes using encoding ENC.\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\
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\
290 For a list of hashing algorithms and encodings, type `$ --list'.\n\
293 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
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)
301 MAKELISTTAB(listtab
, LISTS
)
303 int main(int argc
, char *argv
[])
306 const gchash
*gch
= 0;
307 const encodeops
*e
= &encodingtab
[0];
310 /* --- Initialization --- */
315 /* --- Choose a hash function from the name --- */
318 char *q
= xstrdup(QUIS
);
319 size_t len
= strlen(q
);
320 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
325 gch
= gethash("md5");
329 /* --- Read options --- */
332 static struct option opts
[] = {
333 { "help", 0, 0, 'h' },
334 { "verbose", 0, 0, 'V' },
335 { "usage", 0, 0, 'u' },
337 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
338 { "hash", OPTF_ARGREQ
, 0, 'a' },
339 { "encoding", OPTF_ARGREQ
, 0, 'E' },
340 { "list", 0, 0, 'l' },
342 { "files", 0, 0, 'f' },
343 { "find", 0, 0, 'f' },
344 { "null", 0, 0, '0' },
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' },
354 int i
= mdwopt(argc
, argv
, "hVu a:E:l f0 ecbvp", opts
, 0, 0, 0);
369 exit(displaylists(listtab
, argv
+ optind
));
371 if ((gch
= gethash(optarg
)) == 0)
372 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
376 if ((e
= getencoding(optarg
)) == 0)
377 die(EXIT_FAILURE
, "unknown encoding `%s'", optarg
);
414 /* --- Generate output --- */
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
);
422 rc
= hashsum(gch
, f
, 0, e
);
428 for (i
= 0; i
< argc
; i
++) {
429 if ((rrc
= hashsum(gch
, f
, argv
[i
], e
)) != 0)
437 /*----- That's all, folks -------------------------------------------------*/