3 * $Id: hashsum.c,v 1.6 2001/01/25 21:40:14 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.6 2001/01/25 21:40:14 mdw
34 * Support for new SHA variants added.
36 * Revision 1.5 2000/12/06 20:33:27 mdw
37 * Make flags be macros rather than enumerations, to ensure that they're
40 * Revision 1.4 2000/08/04 23:23:44 mdw
41 * Various <ctype.h> fixes.
43 * Revision 1.3 2000/07/29 17:02:43 mdw
44 * (checkhash): Be pettier about spaces between the hash and filename, for
45 * compatiblity with `md5sum'.
47 * Revision 1.2 2000/07/15 21:14:05 mdw
48 * Missed `-e' out of the usage string.
50 * Revision 1.1 2000/07/15 20:52:34 mdw
51 * Useful replacement for `md5sum' with support for many different hash
52 * functions and for reading filename lists from `find'.
56 /*----- Header files ------------------------------------------------------*/
66 #include <mLib/alloc.h>
67 #include <mLib/dstr.h>
68 #include <mLib/mdwopt.h>
69 #include <mLib/quis.h>
70 #include <mLib/report.h>
88 /*----- Static variables --------------------------------------------------*/
90 static const gchash
*hashtab
[] = {
92 &sha
, &sha256
, &sha384
, &sha512
,
93 &rmd128
, &rmd160
, &rmd256
, &rmd320
,
104 #define f_oddhash 64u
105 #define f_escape 128u
107 /*----- Support functions -------------------------------------------------*/
111 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
112 * @unsigned f@ = flags to set
113 * @const gchash *gch@ = pointer to hash function to use
114 * @void *buf@ = pointer to hash output buffer
116 * Returns: Zero if it worked, nonzero on error.
118 * Use: Hashes a file.
121 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
131 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
135 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
136 h
->ops
->hash(h
, fbuf
, sz
);
137 h
->ops
->done(h
, buf
);
145 /* --- @puthex@ --- *
147 * Arguments: @const octet *buf@ = pointer to a binary buffer
148 * @size_t sz@ = size of the buffer
149 * @FILE *fp@ = pointer to output file handle
153 * Use: Writes a hex dump of a block of memory.
156 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
159 fprintf(fp
, "%02x", *buf
++);
164 /* --- @gethex@ --- *
166 * Arguments: @const char *p@ = pointer to input string
167 * @octet *q@ = pointer to output buffer
168 * @size_t sz@ = size of the output buffer
169 * @char **pp@ = where to put the end pointer
171 * Returns: The number of bytes written to the buffer.
173 * Use: Reads hex dumps from the input string.
176 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
180 isxdigit((unsigned char)p
[0]) &&
181 isxdigit((unsigned char)p
[1])) {
186 *q
++ = strtoul(buf
, 0, 16);
196 /* --- @gethash@ --- *
198 * Arguments: @const char *name@ = pointer to name string
200 * Returns: Pointer to appropriate hash class.
202 * Use: Chooses a hash function by name.
205 static const gchash
*gethash(const char *name
)
207 const gchash
**g
, *gg
= 0;
208 size_t sz
= strlen(name
);
209 for (g
= hashtab
; *g
; g
++) {
210 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
211 if ((*g
)->name
[sz
] == 0) {
223 /* --- @getstring@ --- *
225 * Arguments: @FILE *fp@ = stream from which to read
226 * @const char *p@ = string to read from instead
227 * @dstr *d@ = destination string
228 * @unsigned raw@ = raw or cooked read
230 * Returns: Zero if OK, nonzero on end-of-file.
232 * Use: Reads a filename (or something similar) from a stream.
235 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
240 /* --- Raw: just read exactly what's written up to a null byte --- */
242 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
243 #define EOFCH (fp ? EOF : 0)
246 if ((ch
= NEXTCH
) == EOFCH
)
252 if ((ch
= NEXTCH
) == EOFCH
)
259 /* --- Skip as far as whitespace --- *
261 * Also skip past comments.
269 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
275 /* --- If the character is a quote then read a quoted string --- */
287 /* --- Now read all sorts of interesting things --- */
291 /* --- Handle an escaped thing --- */
298 case 'a': ch
= '\a'; break;
299 case 'b': ch
= '\b'; break;
300 case 'f': ch
= '\f'; break;
301 case 'n': ch
= '\n'; break;
302 case 'r': ch
= '\r'; break;
303 case 't': ch
= '\t'; break;
304 case 'v': ch
= '\v'; break;
311 /* --- If it's a quote or some other end marker then stop --- */
315 if (!q
&& isspace(ch
))
318 /* --- Otherwise contribute and continue --- */
321 if ((ch
= NEXTCH
) == EOFCH
)
334 /* --- @putstring@ --- *
336 * Arguments: @FILE *fp@ = stream to write on
337 * @const char *p@ = pointer to text
338 * @unsigned raw@ = whether the string is to be written raw
342 * Use: Emits a string to a stream.
345 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
347 size_t sz
= strlen(p
);
351 /* --- Just write the string null terminated if raw --- */
354 fwrite(p
, 1, sz
+ 1, fp
);
358 /* --- Check for any dodgy characters --- */
361 for (q
= p
; *q
; q
++) {
362 if (isspace((unsigned char)*q
)) {
371 /* --- Emit the string --- */
373 for (q
= p
; *q
; q
++) {
375 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
376 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
377 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
378 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
379 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
380 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
381 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
382 case '`': fputc('\\', fp
); fputc('`', fp
); break;
383 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
384 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
385 case '#': fputc('\\', fp
); fputc('#', fp
); break;
398 /*----- Guts --------------------------------------------------------------*/
400 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
406 unsigned long n
= 0, nfail
= 0;
407 octet
*buf
= xmalloc(2 * gch
->hashsz
);
411 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
412 moan("couldn't open `%s': %s", file
, strerror(errno
));
413 return (EXIT_FAILURE
);
416 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
421 /* --- Handle a directive --- */
425 if ((q
= str_getword(&p
)) == 0)
427 if (strcmp(q
, "hash") == 0) {
429 if ((q
= str_getword(&p
)) == 0)
431 if ((g
= gethash(q
)) == 0)
435 buf
= xmalloc(2 * gch
->hashsz
);
436 } else if (strcmp(q
, "escape") == 0)
441 /* --- Otherwise it's a hex thing --- */
444 while (*p
&& *p
!= ' ')
449 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
459 getstring(0, p
, &dd
, 0);
463 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
464 moan("couldn't read `%s': %s", p
, strerror(errno
));
468 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
470 fprintf(stderr
, "FAIL %s\n", p
);
472 moan("%s check failed for `%s'", gch
->name
, p
);
477 fprintf(stderr
, "OK %s\n", p
);
485 if ((f
& f_verbose
) && nfail
)
486 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
488 moan("no files checked");
492 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
495 octet
*p
= xmalloc(gch
->hashsz
);
497 if (fhash(file
, f
, gch
, p
)) {
498 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
501 puthex(p
, gch
->hashsz
, stdout
);
504 fputc(f
& f_binary ?
'*' : ' ', stdout
);
506 putstring(stdout
, file
, 0);
517 static int dofile(const char *file
, unsigned f
, const gchash
*gch
)
519 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
);
522 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
531 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
532 moan("couldn't open `%s': %s", file
, strerror(errno
));
533 return (EXIT_FAILURE
);
538 if (getstring(fp
, 0, &d
, f
& f_raw
))
540 if ((rrc
= dofile(d
.buf
, f
, gch
)) != 0)
547 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
549 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
);
552 /*----- Main driver -------------------------------------------------------*/
554 static void version(FILE *fp
)
556 pquis(fp
, "$, Catacomb version " VERSION
"\n");
559 static void usage(FILE *fp
)
561 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
564 static void help(FILE *fp
, const gchash
*gch
)
570 Generates or checks message digests on files. Options available:\n\
572 -h, --help Display this help message.\n\
573 -V, --version Display program's version number.\n\
574 -u, --usage Display a terse usage message.\n\
576 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
578 -f, --files Read a list of file names from standard input.\n\
579 -0, --null File names are null terminated, not plain text.\n\
581 -e, --escape Escape funny characters in filenames.\n\
582 -c, --check Check message digests rather than emitting them.\n\
583 -b, --binary When reading files, treat them as binary.\n\
584 -v, --verbose Be verbose when checking digests.\n\
586 For a list of supported message digest algorithms, type `$ --list'.\n\
589 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
592 int main(int argc
, char *argv
[])
595 const gchash
*gch
= 0;
598 /* --- Initialization --- */
603 /* --- Choose a hash function from the name --- */
606 char *q
= xstrdup(QUIS
);
607 size_t len
= strlen(q
);
608 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
617 /* --- Read options --- */
620 static struct option opts
[] = {
621 { "help", 0, 0, 'h' },
622 { "verbose", 0, 0, 'V' },
623 { "usage", 0, 0, 'u' },
625 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
626 { "hash", OPTF_ARGREQ
, 0, 'a' },
627 { "list", 0, 0, 'l' },
629 { "files", 0, 0, 'f' },
630 { "find", 0, 0, 'f' },
631 { "null", 0, 0, '0' },
633 { "escape", 0, 0, 'e' },
634 { "check", 0, 0, 'c' },
635 { "binary", 0, 0, 'b' },
636 { "verbose", 0, 0, 'v' },
640 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
655 if ((gch
= gethash(optarg
)) == 0)
656 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
661 for (j
= 0; hashtab
[j
]; j
++) {
664 printf("%s", hashtab
[j
]->name
);
700 /* --- Generate output --- */
702 if (!(f
& f_check
)) {
704 printf("#hash %s\n", gch
->name
);
706 fputs("#escape\n", stdout
);
713 for (i
= 0; i
< argc
; i
++) {
714 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
718 rc
= hashsum(0, f
, gch
);
723 /*----- That's all, folks -------------------------------------------------*/