3 * $Id: hashsum.c,v 1.4 2000/08/04 23:23:44 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.4 2000/08/04 23:23:44 mdw
34 * Various <ctype.h> fixes.
36 * Revision 1.3 2000/07/29 17:02:43 mdw
37 * (checkhash): Be pettier about spaces between the hash and filename, for
38 * compatiblity with `md5sum'.
40 * Revision 1.2 2000/07/15 21:14:05 mdw
41 * Missed `-e' out of the usage string.
43 * Revision 1.1 2000/07/15 20:52:34 mdw
44 * Useful replacement for `md5sum' with support for many different hash
45 * functions and for reading filename lists from `find'.
49 /*----- Header files ------------------------------------------------------*/
59 #include <mLib/alloc.h>
60 #include <mLib/dstr.h>
61 #include <mLib/mdwopt.h>
62 #include <mLib/quis.h>
63 #include <mLib/report.h>
78 /*----- Static variables --------------------------------------------------*/
80 static const gchash
*hashtab
[] = {
81 &md5
, &md4
, &sha
, &rmd128
, &rmd160
, &rmd256
, &rmd320
, &tiger
,
96 /*----- Support functions -------------------------------------------------*/
100 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
101 * @unsigned f@ = flags to set
102 * @const gchash *gch@ = pointer to hash function to use
103 * @void *buf@ = pointer to hash output buffer
105 * Returns: Zero if it worked, nonzero on error.
107 * Use: Hashes a file.
110 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
120 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
124 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
125 h
->ops
->hash(h
, fbuf
, sz
);
126 h
->ops
->done(h
, buf
);
134 /* --- @puthex@ --- *
136 * Arguments: @const octet *buf@ = pointer to a binary buffer
137 * @size_t sz@ = size of the buffer
138 * @FILE *fp@ = pointer to output file handle
142 * Use: Writes a hex dump of a block of memory.
145 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
148 fprintf(fp
, "%02x", *buf
++);
153 /* --- @gethex@ --- *
155 * Arguments: @const char *p@ = pointer to input string
156 * @octet *q@ = pointer to output buffer
157 * @size_t sz@ = size of the output buffer
158 * @char **pp@ = where to put the end pointer
160 * Returns: The number of bytes written to the buffer.
162 * Use: Reads hex dumps from the input string.
165 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
169 isxdigit((unsigned char)p
[0]) &&
170 isxdigit((unsigned char)p
[1])) {
175 *q
++ = strtoul(buf
, 0, 16);
185 /* --- @gethash@ --- *
187 * Arguments: @const char *name@ = pointer to name string
189 * Returns: Pointer to appropriate hash class.
191 * Use: Chooses a hash function by name.
194 static const gchash
*gethash(const char *name
)
196 const gchash
**g
, *gg
= 0;
197 size_t sz
= strlen(name
);
198 for (g
= hashtab
; *g
; g
++) {
199 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
200 if ((*g
)->name
[sz
] == 0) {
212 /* --- @getstring@ --- *
214 * Arguments: @FILE *fp@ = stream from which to read
215 * @const char *p@ = string to read from instead
216 * @dstr *d@ = destination string
217 * @unsigned raw@ = raw or cooked read
219 * Returns: Zero if OK, nonzero on end-of-file.
221 * Use: Reads a filename (or something similar) from a stream.
224 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
229 /* --- Raw: just read exactly what's written up to a null byte --- */
231 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
232 #define EOFCH (fp ? EOF : 0)
235 if ((ch
= NEXTCH
) == EOFCH
)
241 if ((ch
= NEXTCH
) == EOFCH
)
248 /* --- Skip as far as whitespace --- *
250 * Also skip past comments.
258 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
264 /* --- If the character is a quote then read a quoted string --- */
276 /* --- Now read all sorts of interesting things --- */
280 /* --- Handle an escaped thing --- */
287 case 'a': ch
= '\a'; break;
288 case 'b': ch
= '\b'; break;
289 case 'f': ch
= '\f'; break;
290 case 'n': ch
= '\n'; break;
291 case 'r': ch
= '\r'; break;
292 case 't': ch
= '\t'; break;
293 case 'v': ch
= '\v'; break;
300 /* --- If it's a quote or some other end marker then stop --- */
304 if (!q
&& isspace(ch
))
307 /* --- Otherwise contribute and continue --- */
310 if ((ch
= NEXTCH
) == EOFCH
)
323 /* --- @putstring@ --- *
325 * Arguments: @FILE *fp@ = stream to write on
326 * @const char *p@ = pointer to text
327 * @unsigned raw@ = whether the string is to be written raw
331 * Use: Emits a string to a stream.
334 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
336 size_t sz
= strlen(p
);
340 /* --- Just write the string null terminated if raw --- */
343 fwrite(p
, 1, sz
+ 1, fp
);
347 /* --- Check for any dodgy characters --- */
350 for (q
= p
; *q
; q
++) {
351 if (isspace((unsigned char)*q
)) {
360 /* --- Emit the string --- */
362 for (q
= p
; *q
; q
++) {
364 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
365 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
366 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
367 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
368 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
369 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
370 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
371 case '`': fputc('\\', fp
); fputc('`', fp
); break;
372 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
373 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
374 case '#': fputc('\\', fp
); fputc('#', fp
); break;
387 /*----- Guts --------------------------------------------------------------*/
389 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
395 unsigned long n
= 0, nfail
= 0;
396 octet
*buf
= xmalloc(2 * gch
->hashsz
);
400 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
401 moan("couldn't open `%s': %s", file
, strerror(errno
));
402 return (EXIT_FAILURE
);
405 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
410 /* --- Handle a directive --- */
414 if ((q
= str_getword(&p
)) == 0)
416 if (strcmp(q
, "hash") == 0) {
418 if ((q
= str_getword(&p
)) == 0)
420 if ((g
= gethash(q
)) == 0)
424 buf
= xmalloc(2 * gch
->hashsz
);
425 } else if (strcmp(q
, "escape") == 0)
430 /* --- Otherwise it's a hex thing --- */
433 while (*p
&& *p
!= ' ')
438 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
448 getstring(0, p
, &dd
, 0);
452 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
453 moan("couldn't read `%s': %s", p
, strerror(errno
));
457 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
459 fprintf(stderr
, "FAIL %s\n", p
);
461 moan("%s check failed for `%s'", gch
->name
, p
);
466 fprintf(stderr
, "OK %s\n", p
);
474 if ((f
& f_verbose
) && nfail
)
475 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
477 moan("no files checked");
481 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
484 octet
*p
= xmalloc(gch
->hashsz
);
486 if (fhash(file
, f
, gch
, p
)) {
487 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
490 puthex(p
, gch
->hashsz
, stdout
);
493 fputc(f
& f_binary ?
'*' : ' ', stdout
);
495 putstring(stdout
, file
, 0);
506 static int dofile(const char *file
, unsigned f
, const gchash
*gch
)
508 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
);
511 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
520 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
521 moan("couldn't open `%s': %s", file
, strerror(errno
));
522 return (EXIT_FAILURE
);
527 if (getstring(fp
, 0, &d
, f
& f_raw
))
529 if ((rrc
= dofile(d
.buf
, f
, gch
)) != 0)
536 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
538 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
);
541 /*----- Main driver -------------------------------------------------------*/
543 static void version(FILE *fp
)
545 pquis(fp
, "$, Catacomb version " VERSION
"\n");
548 static void usage(FILE *fp
)
550 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
553 static void help(FILE *fp
, const gchash
*gch
)
559 Generates or checks message digests on files. Options available:\n\
561 -h, --help Display this help message.\n\
562 -V, --version Display program's version number.\n\
563 -u, --usage Display a terse usage message.\n\
565 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
567 -f, --files Read a list of file names from standard input.\n\
568 -0, --null File names are null terminated, not plain text.\n\
570 -e, --escape Escape funny characters in filenames.\n\
571 -c, --check Check message digests rather than emitting them.\n\
572 -b, --binary When reading files, treat them as binary.\n\
573 -v, --verbose Be verbose when checking digests.\n\
575 For a list of supported message digest algorithms, type `$ --list'.\n\
578 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
581 int main(int argc
, char *argv
[])
584 const gchash
*gch
= 0;
587 /* --- Initialization --- */
592 /* --- Choose a hash function from the name --- */
595 char *q
= xstrdup(QUIS
);
596 size_t len
= strlen(q
);
597 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
606 /* --- Read options --- */
609 static struct option opts
[] = {
610 { "help", 0, 0, 'h' },
611 { "verbose", 0, 0, 'V' },
612 { "usage", 0, 0, 'u' },
614 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
615 { "hash", OPTF_ARGREQ
, 0, 'a' },
616 { "list", 0, 0, 'l' },
618 { "files", 0, 0, 'f' },
619 { "find", 0, 0, 'f' },
620 { "null", 0, 0, '0' },
622 { "escape", 0, 0, 'e' },
623 { "check", 0, 0, 'c' },
624 { "binary", 0, 0, 'b' },
625 { "verbose", 0, 0, 'v' },
629 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
644 if ((gch
= gethash(optarg
)) == 0)
645 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
650 for (j
= 0; hashtab
[j
]; j
++) {
653 printf("%s", hashtab
[j
]->name
);
689 /* --- Generate output --- */
691 if (!(f
& f_check
)) {
693 printf("#hash %s\n", gch
->name
);
695 fputs("#escape\n", stdout
);
702 for (i
= 0; i
< argc
; i
++) {
703 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
707 rc
= hashsum(0, f
, gch
);
712 /*----- That's all, folks -------------------------------------------------*/