3 * $Id: hashsum.c,v 1.1 2000/07/15 20:52:34 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.1 2000/07/15 20:52:34 mdw
34 * Useful replacement for `md5sum' with support for many different hash
35 * functions and for reading filename lists from `find'.
39 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/alloc.h>
50 #include <mLib/dstr.h>
51 #include <mLib/mdwopt.h>
52 #include <mLib/quis.h>
53 #include <mLib/report.h>
68 /*----- Static variables --------------------------------------------------*/
70 static const gchash
*hashtab
[] = {
71 &md5
, &md4
, &sha
, &rmd128
, &rmd160
, &rmd256
, &rmd320
, &tiger
,
86 /*----- Support functions -------------------------------------------------*/
90 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
91 * @unsigned f@ = flags to set
92 * @const gchash *gch@ = pointer to hash function to use
93 * @void *buf@ = pointer to hash output buffer
95 * Returns: Zero if it worked, nonzero on error.
100 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
110 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
114 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
115 h
->ops
->hash(h
, fbuf
, sz
);
116 h
->ops
->done(h
, buf
);
124 /* --- @puthex@ --- *
126 * Arguments: @const octet *buf@ = pointer to a binary buffer
127 * @size_t sz@ = size of the buffer
128 * @FILE *fp@ = pointer to output file handle
132 * Use: Writes a hex dump of a block of memory.
135 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
138 fprintf(fp
, "%02x", *buf
++);
143 /* --- @gethex@ --- *
145 * Arguments: @const char *p@ = pointer to input string
146 * @octet *q@ = pointer to output buffer
147 * @size_t sz@ = size of the output buffer
148 * @char **pp@ = where to put the end pointer
150 * Returns: The number of bytes written to the buffer.
152 * Use: Reads hex dumps from the input string.
155 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
159 isxdigit((unsigned char)p
[0]) &&
160 isxdigit((unsigned char)p
[1])) {
165 *q
++ = strtoul(buf
, 0, 16);
175 /* --- @gethash@ --- *
177 * Arguments: @const char *name@ = pointer to name string
179 * Returns: Pointer to appropriate hash class.
181 * Use: Chooses a hash function by name.
184 static const gchash
*gethash(const char *name
)
186 const gchash
**g
, *gg
= 0;
187 size_t sz
= strlen(name
);
188 for (g
= hashtab
; *g
; g
++) {
189 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
190 if ((*g
)->name
[sz
] == 0) {
202 /* --- @getstring@ --- *
204 * Arguments: @FILE *fp@ = stream from which to read
205 * @const char *p@ = string to read from instead
206 * @dstr *d@ = destination string
207 * @unsigned raw@ = raw or cooked read
209 * Returns: Zero if OK, nonzero on end-of-file.
211 * Use: Reads a filename (or something similar) from a stream.
214 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
219 /* --- Raw: just read exactly what's written up to a null byte --- */
221 #define NEXTCH (fp ? getc(fp) : *p++)
222 #define EOFCH (fp ? EOF : 0)
225 if ((ch
= NEXTCH
) == EOFCH
)
231 if ((ch
= NEXTCH
) == EOFCH
)
238 /* --- Skip as far as whitespace --- *
240 * Also skip past comments.
245 while (isspace((unsigned char)ch
))
248 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
254 /* --- If the character is a quote then read a quoted string --- */
266 /* --- Now read all sorts of interesting things --- */
270 /* --- Handle an escaped thing --- */
277 case 'a': ch
= '\a'; break;
278 case 'b': ch
= '\b'; break;
279 case 'f': ch
= '\f'; break;
280 case 'n': ch
= '\n'; break;
281 case 'r': ch
= '\r'; break;
282 case 't': ch
= '\t'; break;
283 case 'v': ch
= '\v'; break;
290 /* --- If it's a quote or some other end marker then stop --- */
294 if (!q
&& isspace((unsigned char)ch
))
297 /* --- Otherwise contribute and continue --- */
300 if ((ch
= NEXTCH
) == EOFCH
)
313 /* --- @putstring@ --- *
315 * Arguments: @FILE *fp@ = stream to write on
316 * @const char *p@ = pointer to text
317 * @unsigned raw@ = whether the string is to be written raw
321 * Use: Emits a string to a stream.
324 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
326 size_t sz
= strlen(p
);
330 /* --- Just write the string null terminated if raw --- */
333 fwrite(p
, 1, sz
+ 1, fp
);
337 /* --- Check for any dodgy characters --- */
340 for (q
= p
; *q
; q
++) {
341 if (isspace((unsigned char)*q
)) {
350 /* --- Emit the string --- */
352 for (q
= p
; *q
; q
++) {
354 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
355 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
356 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
357 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
358 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
359 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
360 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
361 case '`': fputc('\\', fp
); fputc('`', fp
); break;
362 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
363 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
364 case '#': fputc('\\', fp
); fputc('#', fp
); break;
377 /*----- Guts --------------------------------------------------------------*/
379 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
385 unsigned long n
= 0, nfail
= 0;
386 octet
*buf
= xmalloc(2 * gch
->hashsz
);
390 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
391 moan("couldn't open `%s': %s", file
, strerror(errno
));
392 return (EXIT_FAILURE
);
395 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
400 /* --- Handle a directive --- */
404 if ((q
= str_getword(&p
)) == 0)
406 if (strcmp(q
, "hash") == 0) {
408 if ((q
= str_getword(&p
)) == 0)
410 if ((g
= gethash(q
)) == 0)
414 buf
= xmalloc(2 * gch
->hashsz
);
415 } else if (strcmp(q
, "escape") == 0)
420 /* --- Otherwise it's a hex thing --- */
422 if ((q
= str_getword(&p
)) == 0)
424 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
426 while (isspace((unsigned char)*p
))
437 getstring(0, p
, &dd
, 0);
441 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
442 moan("couldn't read `%s': %s", p
, strerror(errno
));
446 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
448 fprintf(stderr
, "FAIL %s\n", p
);
450 moan("%s check failed for `%s'", gch
->name
, p
);
455 fprintf(stderr
, "OK %s\n", p
);
463 if ((f
& f_verbose
) && nfail
)
464 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
466 moan("no files checked");
470 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
473 octet
*p
= xmalloc(gch
->hashsz
);
475 if (fhash(file
, f
, gch
, p
)) {
476 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
479 puthex(p
, gch
->hashsz
, stdout
);
482 fputc(f
& f_binary ?
'*' : ' ', stdout
);
484 putstring(stdout
, file
, 0);
495 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
504 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
505 moan("couldn't open `%s': %s", file
, strerror(errno
));
506 return (EXIT_FAILURE
);
511 if (getstring(fp
, 0, &d
, f
& f_raw
))
513 if ((rrc
= dohash(d
.buf
, f
, gch
)) != 0)
520 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
523 return (checkhash(file
, f
, gch
));
525 return (hashfiles(file
, f
, gch
));
526 return (dohash(file
, f
, gch
));
529 /*----- Main driver -------------------------------------------------------*/
531 static void version(FILE *fp
)
533 pquis(fp
, "$, Catacomb version " VERSION
"\n");
536 static void usage(FILE *fp
)
538 pquis(fp
, "Usage: $ [-f0bcv] [-a algorithm] [files...]\n");
541 static void help(FILE *fp
, const gchash
*gch
)
547 Generates or checks message digests on files. Options available:\n\
549 -h, --help Display this help message.\n\
550 -V, --version Display program's version number.\n\
551 -u, --usage Display a terse usage message.\n\
553 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
555 -f, --files Read a list of file names from standard input.\n\
556 -0, --null File names are null terminated, not plain text.\n\
558 -e, --escape Escape funny characters in filenames.\n\
559 -c, --check Check message digests rather than emitting them.\n\
560 -b, --binary When reading files, treat them as binary.\n\
561 -v, --verbose Be verbose when checking digests.\n\
563 For a list of supported message digest algorithms, type `$ --list'.\n\
566 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
569 int main(int argc
, char *argv
[])
572 const gchash
*gch
= 0;
575 /* --- Initialization --- */
580 /* --- Choose a hash function from the name --- */
583 char *q
= xstrdup(QUIS
);
584 size_t len
= strlen(q
);
585 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
594 /* --- Read options --- */
597 static struct option opts
[] = {
598 { "help", 0, 0, 'h' },
599 { "verbose", 0, 0, 'V' },
600 { "usage", 0, 0, 'u' },
602 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
603 { "hash", OPTF_ARGREQ
, 0, 'a' },
604 { "list", 0, 0, 'l' },
606 { "files", 0, 0, 'f' },
607 { "find", 0, 0, 'f' },
608 { "null", 0, 0, '0' },
610 { "escape", 0, 0, 'e' },
611 { "check", 0, 0, 'c' },
612 { "binary", 0, 0, 'b' },
613 { "verbose", 0, 0, 'v' },
617 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
632 if ((gch
= gethash(optarg
)) == 0)
633 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
638 for (j
= 0; hashtab
[j
]; j
++) {
641 printf("%s", hashtab
[j
]->name
);
677 /* --- Generate output --- */
679 if (!(f
& f_check
)) {
681 printf("#hash %s\n", gch
->name
);
683 fputs("#escape\n", stdout
);
690 for (i
= 0; i
< argc
; i
++) {
691 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
695 rc
= hashsum(0, f
, gch
);
700 /*----- That's all, folks -------------------------------------------------*/