3 * $Id: hashsum.c,v 1.8 2001/04/19 18:26:33 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.8 2001/04/19 18:26:33 mdw
34 * Add CRC as another hash function.
36 * Revision 1.7 2001/02/21 20:03:22 mdw
37 * Added support for MD2 hash function.
39 * Revision 1.6 2001/01/25 21:40:14 mdw
40 * Support for new SHA variants added.
42 * Revision 1.5 2000/12/06 20:33:27 mdw
43 * Make flags be macros rather than enumerations, to ensure that they're
46 * Revision 1.4 2000/08/04 23:23:44 mdw
47 * Various <ctype.h> fixes.
49 * Revision 1.3 2000/07/29 17:02:43 mdw
50 * (checkhash): Be pettier about spaces between the hash and filename, for
51 * compatiblity with `md5sum'.
53 * Revision 1.2 2000/07/15 21:14:05 mdw
54 * Missed `-e' out of the usage string.
56 * Revision 1.1 2000/07/15 20:52:34 mdw
57 * Useful replacement for `md5sum' with support for many different hash
58 * functions and for reading filename lists from `find'.
62 /*----- Header files ------------------------------------------------------*/
72 #include <mLib/alloc.h>
73 #include <mLib/dstr.h>
74 #include <mLib/mdwopt.h>
75 #include <mLib/quis.h>
76 #include <mLib/report.h>
96 /*----- Static variables --------------------------------------------------*/
98 static const gchash
*hashtab
[] = {
100 &sha
, &sha256
, &sha384
, &sha512
,
101 &rmd128
, &rmd160
, &rmd256
, &rmd320
,
112 #define f_oddhash 64u
113 #define f_escape 128u
115 /*----- Support functions -------------------------------------------------*/
119 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
120 * @unsigned f@ = flags to set
121 * @const gchash *gch@ = pointer to hash function to use
122 * @void *buf@ = pointer to hash output buffer
124 * Returns: Zero if it worked, nonzero on error.
126 * Use: Hashes a file.
129 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
139 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
143 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
144 h
->ops
->hash(h
, fbuf
, sz
);
145 h
->ops
->done(h
, buf
);
153 /* --- @puthex@ --- *
155 * Arguments: @const octet *buf@ = pointer to a binary buffer
156 * @size_t sz@ = size of the buffer
157 * @FILE *fp@ = pointer to output file handle
161 * Use: Writes a hex dump of a block of memory.
164 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
167 fprintf(fp
, "%02x", *buf
++);
172 /* --- @gethex@ --- *
174 * Arguments: @const char *p@ = pointer to input string
175 * @octet *q@ = pointer to output buffer
176 * @size_t sz@ = size of the output buffer
177 * @char **pp@ = where to put the end pointer
179 * Returns: The number of bytes written to the buffer.
181 * Use: Reads hex dumps from the input string.
184 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
188 isxdigit((unsigned char)p
[0]) &&
189 isxdigit((unsigned char)p
[1])) {
194 *q
++ = strtoul(buf
, 0, 16);
204 /* --- @gethash@ --- *
206 * Arguments: @const char *name@ = pointer to name string
208 * Returns: Pointer to appropriate hash class.
210 * Use: Chooses a hash function by name.
213 static const gchash
*gethash(const char *name
)
215 const gchash
**g
, *gg
= 0;
216 size_t sz
= strlen(name
);
217 for (g
= hashtab
; *g
; g
++) {
218 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
219 if ((*g
)->name
[sz
] == 0) {
231 /* --- @getstring@ --- *
233 * Arguments: @FILE *fp@ = stream from which to read
234 * @const char *p@ = string to read from instead
235 * @dstr *d@ = destination string
236 * @unsigned raw@ = raw or cooked read
238 * Returns: Zero if OK, nonzero on end-of-file.
240 * Use: Reads a filename (or something similar) from a stream.
243 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
248 /* --- Raw: just read exactly what's written up to a null byte --- */
250 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
251 #define EOFCH (fp ? EOF : 0)
254 if ((ch
= NEXTCH
) == EOFCH
)
260 if ((ch
= NEXTCH
) == EOFCH
)
267 /* --- Skip as far as whitespace --- *
269 * Also skip past comments.
277 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
283 /* --- If the character is a quote then read a quoted string --- */
295 /* --- Now read all sorts of interesting things --- */
299 /* --- Handle an escaped thing --- */
306 case 'a': ch
= '\a'; break;
307 case 'b': ch
= '\b'; break;
308 case 'f': ch
= '\f'; break;
309 case 'n': ch
= '\n'; break;
310 case 'r': ch
= '\r'; break;
311 case 't': ch
= '\t'; break;
312 case 'v': ch
= '\v'; break;
319 /* --- If it's a quote or some other end marker then stop --- */
323 if (!q
&& isspace(ch
))
326 /* --- Otherwise contribute and continue --- */
329 if ((ch
= NEXTCH
) == EOFCH
)
342 /* --- @putstring@ --- *
344 * Arguments: @FILE *fp@ = stream to write on
345 * @const char *p@ = pointer to text
346 * @unsigned raw@ = whether the string is to be written raw
350 * Use: Emits a string to a stream.
353 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
355 size_t sz
= strlen(p
);
359 /* --- Just write the string null terminated if raw --- */
362 fwrite(p
, 1, sz
+ 1, fp
);
366 /* --- Check for any dodgy characters --- */
369 for (q
= p
; *q
; q
++) {
370 if (isspace((unsigned char)*q
)) {
379 /* --- Emit the string --- */
381 for (q
= p
; *q
; q
++) {
383 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
384 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
385 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
386 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
387 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
388 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
389 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
390 case '`': fputc('\\', fp
); fputc('`', fp
); break;
391 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
392 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
393 case '#': fputc('\\', fp
); fputc('#', fp
); break;
406 /*----- Guts --------------------------------------------------------------*/
408 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
414 unsigned long n
= 0, nfail
= 0;
415 octet
*buf
= xmalloc(2 * gch
->hashsz
);
419 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
420 moan("couldn't open `%s': %s", file
, strerror(errno
));
421 return (EXIT_FAILURE
);
424 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
429 /* --- Handle a directive --- */
433 if ((q
= str_getword(&p
)) == 0)
435 if (strcmp(q
, "hash") == 0) {
437 if ((q
= str_getword(&p
)) == 0)
439 if ((g
= gethash(q
)) == 0)
443 buf
= xmalloc(2 * gch
->hashsz
);
444 } else if (strcmp(q
, "escape") == 0)
449 /* --- Otherwise it's a hex thing --- */
452 while (*p
&& *p
!= ' ')
457 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
467 getstring(0, p
, &dd
, 0);
471 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
472 moan("couldn't read `%s': %s", p
, strerror(errno
));
476 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
478 fprintf(stderr
, "FAIL %s\n", p
);
480 moan("%s check failed for `%s'", gch
->name
, p
);
485 fprintf(stderr
, "OK %s\n", p
);
493 if ((f
& f_verbose
) && nfail
)
494 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
496 moan("no files checked");
500 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
503 octet
*p
= xmalloc(gch
->hashsz
);
505 if (fhash(file
, f
, gch
, p
)) {
506 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
509 puthex(p
, gch
->hashsz
, stdout
);
512 fputc(f
& f_binary ?
'*' : ' ', stdout
);
514 putstring(stdout
, file
, 0);
525 static int dofile(const char *file
, unsigned f
, const gchash
*gch
)
527 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
);
530 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
539 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
540 moan("couldn't open `%s': %s", file
, strerror(errno
));
541 return (EXIT_FAILURE
);
546 if (getstring(fp
, 0, &d
, f
& f_raw
))
548 if ((rrc
= dofile(d
.buf
, f
, gch
)) != 0)
555 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
557 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
);
560 /*----- Main driver -------------------------------------------------------*/
562 static void version(FILE *fp
)
564 pquis(fp
, "$, Catacomb version " VERSION
"\n");
567 static void usage(FILE *fp
)
569 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
572 static void help(FILE *fp
, const gchash
*gch
)
578 Generates or checks message digests on files. Options available:\n\
580 -h, --help Display this help message.\n\
581 -V, --version Display program's version number.\n\
582 -u, --usage Display a terse usage message.\n\
584 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
586 -f, --files Read a list of file names from standard input.\n\
587 -0, --null File names are null terminated, not plain text.\n\
589 -e, --escape Escape funny characters in filenames.\n\
590 -c, --check Check message digests rather than emitting them.\n\
591 -b, --binary When reading files, treat them as binary.\n\
592 -v, --verbose Be verbose when checking digests.\n\
594 For a list of supported message digest algorithms, type `$ --list'.\n\
597 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
600 int main(int argc
, char *argv
[])
603 const gchash
*gch
= 0;
606 /* --- Initialization --- */
611 /* --- Choose a hash function from the name --- */
614 char *q
= xstrdup(QUIS
);
615 size_t len
= strlen(q
);
616 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
625 /* --- Read options --- */
628 static struct option opts
[] = {
629 { "help", 0, 0, 'h' },
630 { "verbose", 0, 0, 'V' },
631 { "usage", 0, 0, 'u' },
633 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
634 { "hash", OPTF_ARGREQ
, 0, 'a' },
635 { "list", 0, 0, 'l' },
637 { "files", 0, 0, 'f' },
638 { "find", 0, 0, 'f' },
639 { "null", 0, 0, '0' },
641 { "escape", 0, 0, 'e' },
642 { "check", 0, 0, 'c' },
643 { "binary", 0, 0, 'b' },
644 { "verbose", 0, 0, 'v' },
648 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
663 if ((gch
= gethash(optarg
)) == 0)
664 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
669 for (j
= 0; hashtab
[j
]; j
++) {
672 printf("%s", hashtab
[j
]->name
);
708 /* --- Generate output --- */
710 if (!(f
& f_check
)) {
712 printf("#hash %s\n", gch
->name
);
714 fputs("#escape\n", stdout
);
721 for (i
= 0; i
< argc
; i
++) {
722 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
726 rc
= hashsum(0, f
, gch
);
731 /*----- That's all, folks -------------------------------------------------*/