3 * $Id: hashsum.c,v 1.2 2000/07/15 21:14:05 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/07/15 21:14:05 mdw
34 * Missed `-e' out of the usage string.
36 * Revision 1.1 2000/07/15 20:52:34 mdw
37 * Useful replacement for `md5sum' with support for many different hash
38 * functions and for reading filename lists from `find'.
42 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/alloc.h>
53 #include <mLib/dstr.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
71 /*----- Static variables --------------------------------------------------*/
73 static const gchash
*hashtab
[] = {
74 &md5
, &md4
, &sha
, &rmd128
, &rmd160
, &rmd256
, &rmd320
, &tiger
,
89 /*----- Support functions -------------------------------------------------*/
93 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
94 * @unsigned f@ = flags to set
95 * @const gchash *gch@ = pointer to hash function to use
96 * @void *buf@ = pointer to hash output buffer
98 * Returns: Zero if it worked, nonzero on error.
100 * Use: Hashes a file.
103 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
113 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
117 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
118 h
->ops
->hash(h
, fbuf
, sz
);
119 h
->ops
->done(h
, buf
);
127 /* --- @puthex@ --- *
129 * Arguments: @const octet *buf@ = pointer to a binary buffer
130 * @size_t sz@ = size of the buffer
131 * @FILE *fp@ = pointer to output file handle
135 * Use: Writes a hex dump of a block of memory.
138 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
141 fprintf(fp
, "%02x", *buf
++);
146 /* --- @gethex@ --- *
148 * Arguments: @const char *p@ = pointer to input string
149 * @octet *q@ = pointer to output buffer
150 * @size_t sz@ = size of the output buffer
151 * @char **pp@ = where to put the end pointer
153 * Returns: The number of bytes written to the buffer.
155 * Use: Reads hex dumps from the input string.
158 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
162 isxdigit((unsigned char)p
[0]) &&
163 isxdigit((unsigned char)p
[1])) {
168 *q
++ = strtoul(buf
, 0, 16);
178 /* --- @gethash@ --- *
180 * Arguments: @const char *name@ = pointer to name string
182 * Returns: Pointer to appropriate hash class.
184 * Use: Chooses a hash function by name.
187 static const gchash
*gethash(const char *name
)
189 const gchash
**g
, *gg
= 0;
190 size_t sz
= strlen(name
);
191 for (g
= hashtab
; *g
; g
++) {
192 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
193 if ((*g
)->name
[sz
] == 0) {
205 /* --- @getstring@ --- *
207 * Arguments: @FILE *fp@ = stream from which to read
208 * @const char *p@ = string to read from instead
209 * @dstr *d@ = destination string
210 * @unsigned raw@ = raw or cooked read
212 * Returns: Zero if OK, nonzero on end-of-file.
214 * Use: Reads a filename (or something similar) from a stream.
217 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
222 /* --- Raw: just read exactly what's written up to a null byte --- */
224 #define NEXTCH (fp ? getc(fp) : *p++)
225 #define EOFCH (fp ? EOF : 0)
228 if ((ch
= NEXTCH
) == EOFCH
)
234 if ((ch
= NEXTCH
) == EOFCH
)
241 /* --- Skip as far as whitespace --- *
243 * Also skip past comments.
248 while (isspace((unsigned char)ch
))
251 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
257 /* --- If the character is a quote then read a quoted string --- */
269 /* --- Now read all sorts of interesting things --- */
273 /* --- Handle an escaped thing --- */
280 case 'a': ch
= '\a'; break;
281 case 'b': ch
= '\b'; break;
282 case 'f': ch
= '\f'; break;
283 case 'n': ch
= '\n'; break;
284 case 'r': ch
= '\r'; break;
285 case 't': ch
= '\t'; break;
286 case 'v': ch
= '\v'; break;
293 /* --- If it's a quote or some other end marker then stop --- */
297 if (!q
&& isspace((unsigned char)ch
))
300 /* --- Otherwise contribute and continue --- */
303 if ((ch
= NEXTCH
) == EOFCH
)
316 /* --- @putstring@ --- *
318 * Arguments: @FILE *fp@ = stream to write on
319 * @const char *p@ = pointer to text
320 * @unsigned raw@ = whether the string is to be written raw
324 * Use: Emits a string to a stream.
327 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
329 size_t sz
= strlen(p
);
333 /* --- Just write the string null terminated if raw --- */
336 fwrite(p
, 1, sz
+ 1, fp
);
340 /* --- Check for any dodgy characters --- */
343 for (q
= p
; *q
; q
++) {
344 if (isspace((unsigned char)*q
)) {
353 /* --- Emit the string --- */
355 for (q
= p
; *q
; q
++) {
357 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
358 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
359 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
360 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
361 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
362 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
363 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
364 case '`': fputc('\\', fp
); fputc('`', fp
); break;
365 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
366 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
367 case '#': fputc('\\', fp
); fputc('#', fp
); break;
380 /*----- Guts --------------------------------------------------------------*/
382 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
388 unsigned long n
= 0, nfail
= 0;
389 octet
*buf
= xmalloc(2 * gch
->hashsz
);
393 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
394 moan("couldn't open `%s': %s", file
, strerror(errno
));
395 return (EXIT_FAILURE
);
398 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
403 /* --- Handle a directive --- */
407 if ((q
= str_getword(&p
)) == 0)
409 if (strcmp(q
, "hash") == 0) {
411 if ((q
= str_getword(&p
)) == 0)
413 if ((g
= gethash(q
)) == 0)
417 buf
= xmalloc(2 * gch
->hashsz
);
418 } else if (strcmp(q
, "escape") == 0)
423 /* --- Otherwise it's a hex thing --- */
425 if ((q
= str_getword(&p
)) == 0)
427 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
429 while (isspace((unsigned char)*p
))
440 getstring(0, p
, &dd
, 0);
444 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
445 moan("couldn't read `%s': %s", p
, strerror(errno
));
449 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
451 fprintf(stderr
, "FAIL %s\n", p
);
453 moan("%s check failed for `%s'", gch
->name
, p
);
458 fprintf(stderr
, "OK %s\n", p
);
466 if ((f
& f_verbose
) && nfail
)
467 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
469 moan("no files checked");
473 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
476 octet
*p
= xmalloc(gch
->hashsz
);
478 if (fhash(file
, f
, gch
, p
)) {
479 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
482 puthex(p
, gch
->hashsz
, stdout
);
485 fputc(f
& f_binary ?
'*' : ' ', stdout
);
487 putstring(stdout
, file
, 0);
498 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
507 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
508 moan("couldn't open `%s': %s", file
, strerror(errno
));
509 return (EXIT_FAILURE
);
514 if (getstring(fp
, 0, &d
, f
& f_raw
))
516 if ((rrc
= dohash(d
.buf
, f
, gch
)) != 0)
523 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
526 return (checkhash(file
, f
, gch
));
528 return (hashfiles(file
, f
, gch
));
529 return (dohash(file
, f
, gch
));
532 /*----- Main driver -------------------------------------------------------*/
534 static void version(FILE *fp
)
536 pquis(fp
, "$, Catacomb version " VERSION
"\n");
539 static void usage(FILE *fp
)
541 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
544 static void help(FILE *fp
, const gchash
*gch
)
550 Generates or checks message digests on files. Options available:\n\
552 -h, --help Display this help message.\n\
553 -V, --version Display program's version number.\n\
554 -u, --usage Display a terse usage message.\n\
556 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
558 -f, --files Read a list of file names from standard input.\n\
559 -0, --null File names are null terminated, not plain text.\n\
561 -e, --escape Escape funny characters in filenames.\n\
562 -c, --check Check message digests rather than emitting them.\n\
563 -b, --binary When reading files, treat them as binary.\n\
564 -v, --verbose Be verbose when checking digests.\n\
566 For a list of supported message digest algorithms, type `$ --list'.\n\
569 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
572 int main(int argc
, char *argv
[])
575 const gchash
*gch
= 0;
578 /* --- Initialization --- */
583 /* --- Choose a hash function from the name --- */
586 char *q
= xstrdup(QUIS
);
587 size_t len
= strlen(q
);
588 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
597 /* --- Read options --- */
600 static struct option opts
[] = {
601 { "help", 0, 0, 'h' },
602 { "verbose", 0, 0, 'V' },
603 { "usage", 0, 0, 'u' },
605 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
606 { "hash", OPTF_ARGREQ
, 0, 'a' },
607 { "list", 0, 0, 'l' },
609 { "files", 0, 0, 'f' },
610 { "find", 0, 0, 'f' },
611 { "null", 0, 0, '0' },
613 { "escape", 0, 0, 'e' },
614 { "check", 0, 0, 'c' },
615 { "binary", 0, 0, 'b' },
616 { "verbose", 0, 0, 'v' },
620 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
635 if ((gch
= gethash(optarg
)) == 0)
636 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
641 for (j
= 0; hashtab
[j
]; j
++) {
644 printf("%s", hashtab
[j
]->name
);
680 /* --- Generate output --- */
682 if (!(f
& f_check
)) {
684 printf("#hash %s\n", gch
->name
);
686 fputs("#escape\n", stdout
);
693 for (i
= 0; i
< argc
; i
++) {
694 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
698 rc
= hashsum(0, f
, gch
);
703 /*----- That's all, folks -------------------------------------------------*/