3 * $Id: hashsum.c,v 1.5 2000/12/06 20:33:27 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.5 2000/12/06 20:33:27 mdw
34 * Make flags be macros rather than enumerations, to ensure that they're
37 * Revision 1.4 2000/08/04 23:23:44 mdw
38 * Various <ctype.h> fixes.
40 * Revision 1.3 2000/07/29 17:02:43 mdw
41 * (checkhash): Be pettier about spaces between the hash and filename, for
42 * compatiblity with `md5sum'.
44 * Revision 1.2 2000/07/15 21:14:05 mdw
45 * Missed `-e' out of the usage string.
47 * Revision 1.1 2000/07/15 20:52:34 mdw
48 * Useful replacement for `md5sum' with support for many different hash
49 * functions and for reading filename lists from `find'.
53 /*----- Header files ------------------------------------------------------*/
63 #include <mLib/alloc.h>
64 #include <mLib/dstr.h>
65 #include <mLib/mdwopt.h>
66 #include <mLib/quis.h>
67 #include <mLib/report.h>
82 /*----- Static variables --------------------------------------------------*/
84 static const gchash
*hashtab
[] = {
85 &md5
, &md4
, &sha
, &rmd128
, &rmd160
, &rmd256
, &rmd320
, &tiger
,
98 /*----- Support functions -------------------------------------------------*/
102 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
103 * @unsigned f@ = flags to set
104 * @const gchash *gch@ = pointer to hash function to use
105 * @void *buf@ = pointer to hash output buffer
107 * Returns: Zero if it worked, nonzero on error.
109 * Use: Hashes a file.
112 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
122 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
126 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
127 h
->ops
->hash(h
, fbuf
, sz
);
128 h
->ops
->done(h
, buf
);
136 /* --- @puthex@ --- *
138 * Arguments: @const octet *buf@ = pointer to a binary buffer
139 * @size_t sz@ = size of the buffer
140 * @FILE *fp@ = pointer to output file handle
144 * Use: Writes a hex dump of a block of memory.
147 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
150 fprintf(fp
, "%02x", *buf
++);
155 /* --- @gethex@ --- *
157 * Arguments: @const char *p@ = pointer to input string
158 * @octet *q@ = pointer to output buffer
159 * @size_t sz@ = size of the output buffer
160 * @char **pp@ = where to put the end pointer
162 * Returns: The number of bytes written to the buffer.
164 * Use: Reads hex dumps from the input string.
167 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
171 isxdigit((unsigned char)p
[0]) &&
172 isxdigit((unsigned char)p
[1])) {
177 *q
++ = strtoul(buf
, 0, 16);
187 /* --- @gethash@ --- *
189 * Arguments: @const char *name@ = pointer to name string
191 * Returns: Pointer to appropriate hash class.
193 * Use: Chooses a hash function by name.
196 static const gchash
*gethash(const char *name
)
198 const gchash
**g
, *gg
= 0;
199 size_t sz
= strlen(name
);
200 for (g
= hashtab
; *g
; g
++) {
201 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
202 if ((*g
)->name
[sz
] == 0) {
214 /* --- @getstring@ --- *
216 * Arguments: @FILE *fp@ = stream from which to read
217 * @const char *p@ = string to read from instead
218 * @dstr *d@ = destination string
219 * @unsigned raw@ = raw or cooked read
221 * Returns: Zero if OK, nonzero on end-of-file.
223 * Use: Reads a filename (or something similar) from a stream.
226 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
231 /* --- Raw: just read exactly what's written up to a null byte --- */
233 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
234 #define EOFCH (fp ? EOF : 0)
237 if ((ch
= NEXTCH
) == EOFCH
)
243 if ((ch
= NEXTCH
) == EOFCH
)
250 /* --- Skip as far as whitespace --- *
252 * Also skip past comments.
260 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
266 /* --- If the character is a quote then read a quoted string --- */
278 /* --- Now read all sorts of interesting things --- */
282 /* --- Handle an escaped thing --- */
289 case 'a': ch
= '\a'; break;
290 case 'b': ch
= '\b'; break;
291 case 'f': ch
= '\f'; break;
292 case 'n': ch
= '\n'; break;
293 case 'r': ch
= '\r'; break;
294 case 't': ch
= '\t'; break;
295 case 'v': ch
= '\v'; break;
302 /* --- If it's a quote or some other end marker then stop --- */
306 if (!q
&& isspace(ch
))
309 /* --- Otherwise contribute and continue --- */
312 if ((ch
= NEXTCH
) == EOFCH
)
325 /* --- @putstring@ --- *
327 * Arguments: @FILE *fp@ = stream to write on
328 * @const char *p@ = pointer to text
329 * @unsigned raw@ = whether the string is to be written raw
333 * Use: Emits a string to a stream.
336 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
338 size_t sz
= strlen(p
);
342 /* --- Just write the string null terminated if raw --- */
345 fwrite(p
, 1, sz
+ 1, fp
);
349 /* --- Check for any dodgy characters --- */
352 for (q
= p
; *q
; q
++) {
353 if (isspace((unsigned char)*q
)) {
362 /* --- Emit the string --- */
364 for (q
= p
; *q
; q
++) {
366 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
367 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
368 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
369 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
370 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
371 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
372 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
373 case '`': fputc('\\', fp
); fputc('`', fp
); break;
374 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
375 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
376 case '#': fputc('\\', fp
); fputc('#', fp
); break;
389 /*----- Guts --------------------------------------------------------------*/
391 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
397 unsigned long n
= 0, nfail
= 0;
398 octet
*buf
= xmalloc(2 * gch
->hashsz
);
402 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
403 moan("couldn't open `%s': %s", file
, strerror(errno
));
404 return (EXIT_FAILURE
);
407 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
412 /* --- Handle a directive --- */
416 if ((q
= str_getword(&p
)) == 0)
418 if (strcmp(q
, "hash") == 0) {
420 if ((q
= str_getword(&p
)) == 0)
422 if ((g
= gethash(q
)) == 0)
426 buf
= xmalloc(2 * gch
->hashsz
);
427 } else if (strcmp(q
, "escape") == 0)
432 /* --- Otherwise it's a hex thing --- */
435 while (*p
&& *p
!= ' ')
440 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
450 getstring(0, p
, &dd
, 0);
454 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
455 moan("couldn't read `%s': %s", p
, strerror(errno
));
459 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
461 fprintf(stderr
, "FAIL %s\n", p
);
463 moan("%s check failed for `%s'", gch
->name
, p
);
468 fprintf(stderr
, "OK %s\n", p
);
476 if ((f
& f_verbose
) && nfail
)
477 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
479 moan("no files checked");
483 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
486 octet
*p
= xmalloc(gch
->hashsz
);
488 if (fhash(file
, f
, gch
, p
)) {
489 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
492 puthex(p
, gch
->hashsz
, stdout
);
495 fputc(f
& f_binary ?
'*' : ' ', stdout
);
497 putstring(stdout
, file
, 0);
508 static int dofile(const char *file
, unsigned f
, const gchash
*gch
)
510 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
);
513 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
522 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
523 moan("couldn't open `%s': %s", file
, strerror(errno
));
524 return (EXIT_FAILURE
);
529 if (getstring(fp
, 0, &d
, f
& f_raw
))
531 if ((rrc
= dofile(d
.buf
, f
, gch
)) != 0)
538 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
540 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
);
543 /*----- Main driver -------------------------------------------------------*/
545 static void version(FILE *fp
)
547 pquis(fp
, "$, Catacomb version " VERSION
"\n");
550 static void usage(FILE *fp
)
552 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
555 static void help(FILE *fp
, const gchash
*gch
)
561 Generates or checks message digests on files. Options available:\n\
563 -h, --help Display this help message.\n\
564 -V, --version Display program's version number.\n\
565 -u, --usage Display a terse usage message.\n\
567 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
569 -f, --files Read a list of file names from standard input.\n\
570 -0, --null File names are null terminated, not plain text.\n\
572 -e, --escape Escape funny characters in filenames.\n\
573 -c, --check Check message digests rather than emitting them.\n\
574 -b, --binary When reading files, treat them as binary.\n\
575 -v, --verbose Be verbose when checking digests.\n\
577 For a list of supported message digest algorithms, type `$ --list'.\n\
580 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
583 int main(int argc
, char *argv
[])
586 const gchash
*gch
= 0;
589 /* --- Initialization --- */
594 /* --- Choose a hash function from the name --- */
597 char *q
= xstrdup(QUIS
);
598 size_t len
= strlen(q
);
599 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
608 /* --- Read options --- */
611 static struct option opts
[] = {
612 { "help", 0, 0, 'h' },
613 { "verbose", 0, 0, 'V' },
614 { "usage", 0, 0, 'u' },
616 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
617 { "hash", OPTF_ARGREQ
, 0, 'a' },
618 { "list", 0, 0, 'l' },
620 { "files", 0, 0, 'f' },
621 { "find", 0, 0, 'f' },
622 { "null", 0, 0, '0' },
624 { "escape", 0, 0, 'e' },
625 { "check", 0, 0, 'c' },
626 { "binary", 0, 0, 'b' },
627 { "verbose", 0, 0, 'v' },
631 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
646 if ((gch
= gethash(optarg
)) == 0)
647 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
652 for (j
= 0; hashtab
[j
]; j
++) {
655 printf("%s", hashtab
[j
]->name
);
691 /* --- Generate output --- */
693 if (!(f
& f_check
)) {
695 printf("#hash %s\n", gch
->name
);
697 fputs("#escape\n", stdout
);
704 for (i
= 0; i
< argc
; i
++) {
705 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
709 rc
= hashsum(0, f
, gch
);
714 /*----- That's all, folks -------------------------------------------------*/