3 * $Id: hashsum.c,v 1.10 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/alloc.h>
41 #include <mLib/dstr.h>
42 #include <mLib/mdwopt.h>
43 #include <mLib/quis.h>
44 #include <mLib/report.h>
50 /*----- Static variables --------------------------------------------------*/
61 /*----- Support functions -------------------------------------------------*/
65 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
66 * @unsigned f@ = flags to set
67 * @const gchash *gch@ = pointer to hash function to use
68 * @void *buf@ = pointer to hash output buffer
70 * Returns: Zero if it worked, nonzero on error.
75 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
85 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
89 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
101 * Arguments: @const octet *buf@ = pointer to a binary buffer
102 * @size_t sz@ = size of the buffer
103 * @FILE *fp@ = pointer to output file handle
107 * Use: Writes a hex dump of a block of memory.
110 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
113 fprintf(fp
, "%02x", *buf
++);
118 /* --- @gethex@ --- *
120 * Arguments: @const char *p@ = pointer to input string
121 * @octet *q@ = pointer to output buffer
122 * @size_t sz@ = size of the output buffer
123 * @char **pp@ = where to put the end pointer
125 * Returns: The number of bytes written to the buffer.
127 * Use: Reads hex dumps from the input string.
130 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
134 isxdigit((unsigned char)p
[0]) &&
135 isxdigit((unsigned char)p
[1])) {
140 *q
++ = strtoul(buf
, 0, 16);
150 /* --- @gethash@ --- *
152 * Arguments: @const char *name@ = pointer to name string
154 * Returns: Pointer to appropriate hash class.
156 * Use: Chooses a hash function by name.
159 static const gchash
*gethash(const char *name
)
161 const gchash
*const *g
, *gg
= 0;
162 size_t sz
= strlen(name
);
163 for (g
= ghashtab
; *g
; g
++) {
164 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
165 if ((*g
)->name
[sz
] == 0) {
177 /* --- @getstring@ --- *
179 * Arguments: @FILE *fp@ = stream from which to read
180 * @const char *p@ = string to read from instead
181 * @dstr *d@ = destination string
182 * @unsigned raw@ = raw or cooked read
184 * Returns: Zero if OK, nonzero on end-of-file.
186 * Use: Reads a filename (or something similar) from a stream.
189 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
194 /* --- Raw: just read exactly what's written up to a null byte --- */
196 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
197 #define EOFCH (fp ? EOF : 0)
200 if ((ch
= NEXTCH
) == EOFCH
)
206 if ((ch
= NEXTCH
) == EOFCH
)
213 /* --- Skip as far as whitespace --- *
215 * Also skip past comments.
223 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
229 /* --- If the character is a quote then read a quoted string --- */
241 /* --- Now read all sorts of interesting things --- */
245 /* --- Handle an escaped thing --- */
252 case 'a': ch
= '\a'; break;
253 case 'b': ch
= '\b'; break;
254 case 'f': ch
= '\f'; break;
255 case 'n': ch
= '\n'; break;
256 case 'r': ch
= '\r'; break;
257 case 't': ch
= '\t'; break;
258 case 'v': ch
= '\v'; break;
265 /* --- If it's a quote or some other end marker then stop --- */
269 if (!q
&& isspace(ch
))
272 /* --- Otherwise contribute and continue --- */
275 if ((ch
= NEXTCH
) == EOFCH
)
288 /* --- @putstring@ --- *
290 * Arguments: @FILE *fp@ = stream to write on
291 * @const char *p@ = pointer to text
292 * @unsigned raw@ = whether the string is to be written raw
296 * Use: Emits a string to a stream.
299 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
301 size_t sz
= strlen(p
);
305 /* --- Just write the string null terminated if raw --- */
308 fwrite(p
, 1, sz
+ 1, fp
);
312 /* --- Check for any dodgy characters --- */
315 for (q
= p
; *q
; q
++) {
316 if (isspace((unsigned char)*q
)) {
325 /* --- Emit the string --- */
327 for (q
= p
; *q
; q
++) {
329 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
330 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
331 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
332 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
333 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
334 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
335 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
336 case '`': fputc('\\', fp
); fputc('`', fp
); break;
337 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
338 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
339 case '#': fputc('\\', fp
); fputc('#', fp
); break;
352 /*----- Guts --------------------------------------------------------------*/
354 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
360 unsigned long n
= 0, nfail
= 0;
361 octet
*buf
= xmalloc(2 * gch
->hashsz
);
365 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
366 moan("couldn't open `%s': %s", file
, strerror(errno
));
367 return (EXIT_FAILURE
);
370 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
375 /* --- Handle a directive --- */
379 if ((q
= str_getword(&p
)) == 0)
381 if (strcmp(q
, "hash") == 0) {
383 if ((q
= str_getword(&p
)) == 0)
385 if ((g
= gethash(q
)) == 0)
389 buf
= xmalloc(2 * gch
->hashsz
);
390 } else if (strcmp(q
, "escape") == 0)
395 /* --- Otherwise it's a hex thing --- */
398 while (*p
&& *p
!= ' ')
403 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
413 getstring(0, p
, &dd
, 0);
417 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
418 moan("couldn't read `%s': %s", p
, strerror(errno
));
422 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
424 fprintf(stderr
, "FAIL %s\n", p
);
426 moan("%s check failed for `%s'", gch
->name
, p
);
431 fprintf(stderr
, "OK %s\n", p
);
439 if ((f
& f_verbose
) && nfail
)
440 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
442 moan("no files checked");
446 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
449 octet
*p
= xmalloc(gch
->hashsz
);
451 if (fhash(file
, f
, gch
, p
)) {
452 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
455 puthex(p
, gch
->hashsz
, stdout
);
458 fputc(f
& f_binary ?
'*' : ' ', stdout
);
460 putstring(stdout
, file
, 0);
471 static int dofile(const char *file
, unsigned f
, const gchash
*gch
)
473 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
);
476 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
485 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
486 moan("couldn't open `%s': %s", file
, strerror(errno
));
487 return (EXIT_FAILURE
);
492 if (getstring(fp
, 0, &d
, f
& f_raw
))
494 if ((rrc
= dofile(d
.buf
, f
, gch
)) != 0)
501 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
503 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
);
506 /*----- Main driver -------------------------------------------------------*/
508 static void version(FILE *fp
)
510 pquis(fp
, "$, Catacomb version " VERSION
"\n");
513 static void usage(FILE *fp
)
515 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
518 static void help(FILE *fp
, const gchash
*gch
)
524 Generates or checks message digests on files. Options available:\n\
526 -h, --help Display this help message.\n\
527 -V, --version Display program's version number.\n\
528 -u, --usage Display a terse usage message.\n\
530 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
532 -f, --files Read a list of file names from standard input.\n\
533 -0, --null File names are null terminated, not plain text.\n\
535 -e, --escape Escape funny characters in filenames.\n\
536 -c, --check Check message digests rather than emitting them.\n\
537 -b, --binary When reading files, treat them as binary.\n\
538 -v, --verbose Be verbose when checking digests.\n\
540 For a list of supported message digest algorithms, type `$ --list'.\n\
543 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
546 int main(int argc
, char *argv
[])
549 const gchash
*gch
= 0;
552 /* --- Initialization --- */
557 /* --- Choose a hash function from the name --- */
560 char *q
= xstrdup(QUIS
);
561 size_t len
= strlen(q
);
562 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
567 gch
= gethash("md5");
571 /* --- Read options --- */
574 static struct option opts
[] = {
575 { "help", 0, 0, 'h' },
576 { "verbose", 0, 0, 'V' },
577 { "usage", 0, 0, 'u' },
579 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
580 { "hash", OPTF_ARGREQ
, 0, 'a' },
581 { "list", 0, 0, 'l' },
583 { "files", 0, 0, 'f' },
584 { "find", 0, 0, 'f' },
585 { "null", 0, 0, '0' },
587 { "escape", 0, 0, 'e' },
588 { "check", 0, 0, 'c' },
589 { "binary", 0, 0, 'b' },
590 { "verbose", 0, 0, 'v' },
594 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
609 if ((gch
= gethash(optarg
)) == 0)
610 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
615 for (j
= 0; ghashtab
[j
]; j
++) {
618 printf("%s", ghashtab
[j
]->name
);
654 /* --- Generate output --- */
656 if (!(f
& f_check
)) {
658 printf("#hash %s\n", gch
->name
);
660 fputs("#escape\n", stdout
);
667 for (i
= 0; i
< argc
; i
++) {
668 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
672 rc
= hashsum(0, f
, gch
);
677 /*----- That's all, folks -------------------------------------------------*/