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 ------------------------------------------------------*/
32 #define _FILE_OFFSET_BITS 64
43 #include <mLib/alloc.h>
44 #include <mLib/dstr.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
52 #include <mLib/base32.h>
53 #include <mLib/base64.h>
58 /*----- Static variables --------------------------------------------------*/
69 #define f_progress 512u
71 /*----- Encoding and decoding ---------------------------------------------*/
73 /* --- Hex encoding --- */
75 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
78 fprintf(fp
, "%02x", *buf
++);
83 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
87 isxdigit((unsigned char)p
[0]) &&
88 isxdigit((unsigned char)p
[1])) {
93 *q
++ = strtoul(buf
, 0, 16);
103 /* --- Base64 encoding --- */
105 static void putb64(const octet
*buf
, size_t sz
, FILE *fp
)
113 base64_encode(&b
, buf
, sz
, &d
);
114 base64_encode(&b
, 0, 0, &d
);
119 static size_t getb64(const char *p
, octet
*q
, size_t sz
, char **pp
)
123 size_t n
= strlen(p
);
126 base64_decode(&b
, p
, n
, &d
);
127 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
128 base64_decode(&b
, 0, 0, &d
);
130 memcpy(q
, d
.buf
, sz
);
136 /* --- Base32 encoding --- */
138 static void putb32(const octet
*buf
, size_t sz
, FILE *fp
)
146 base32_encode(&b
, buf
, sz
, &d
);
147 base32_encode(&b
, 0, 0, &d
);
152 static size_t getb32(const char *p
, octet
*q
, size_t sz
, char **pp
)
156 size_t n
= strlen(p
);
159 base32_decode(&b
, p
, n
, &d
);
160 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
161 base32_decode(&b
, 0, 0, &d
);
163 memcpy(q
, d
.buf
, sz
);
171 typedef struct encodeops
{
173 void (*put
)(const octet
*, size_t, FILE *);
174 size_t (*get
)(const char *, octet
*, size_t, char **);
177 static const encodeops encodingtab
[] = {
178 { "hex", puthex
, gethex
},
179 { "base64", putb64
, getb64
},
180 { "base32", putb32
, getb32
},
184 static const encodeops
*getencoding(const char *ename
)
188 for (e
= encodingtab
; e
->name
; e
++) {
189 if (strcmp(ename
, e
->name
) == 0)
195 /*----- Support functions -------------------------------------------------*/
199 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
200 * @unsigned f@ = flags to set
201 * @const gchash *gch@ = pointer to hash function to use
202 * @void *buf@ = pointer to hash output buffer
204 * Returns: Zero if it worked, nonzero on error.
206 * Use: Hashes a file.
209 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
212 char fbuf
[1024 * 128];
218 if (!file
|| strcmp(file
, "-") == 0)
220 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
223 if (f
& f_progress
) {
224 if (fprogress_init(&ff
, file
, fp
)) return (-1);
228 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0) {
229 GH_HASH(h
, fbuf
, sz
);
230 if (f
& f_progress
) fprogress_update(&ff
, sz
);
234 if (f
& f_progress
) fprogress_done(&ff
);
241 /* --- @gethash@ --- *
243 * Arguments: @const char *name@ = pointer to name string
245 * Returns: Pointer to appropriate hash class.
247 * Use: Chooses a hash function by name.
250 static const gchash
*gethash(const char *name
)
252 const gchash
*const *g
, *gg
= 0;
253 size_t sz
= strlen(name
);
254 for (g
= ghashtab
; *g
; g
++) {
255 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
256 if ((*g
)->name
[sz
] == 0) {
268 /* --- @getstring@ --- *
270 * Arguments: @FILE *fp@ = stream from which to read
271 * @const char *p@ = string to read from instead
272 * @dstr *d@ = destination string
273 * @unsigned raw@ = raw or cooked read
275 * Returns: Zero if OK, nonzero on end-of-file.
277 * Use: Reads a filename (or something similar) from a stream.
280 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
285 /* --- Raw: just read exactly what's written up to a null byte --- */
287 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
288 #define EOFCH (fp ? EOF : 0)
291 if ((ch
= NEXTCH
) == EOFCH
)
297 if ((ch
= NEXTCH
) == EOFCH
)
304 /* --- Skip as far as whitespace --- *
306 * Also skip past comments.
314 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
320 /* --- If the character is a quote then read a quoted string --- */
332 /* --- Now read all sorts of interesting things --- */
336 /* --- Handle an escaped thing --- */
343 case 'a': ch
= '\a'; break;
344 case 'b': ch
= '\b'; break;
345 case 'f': ch
= '\f'; break;
346 case 'n': ch
= '\n'; break;
347 case 'r': ch
= '\r'; break;
348 case 't': ch
= '\t'; break;
349 case 'v': ch
= '\v'; break;
356 /* --- If it's a quote or some other end marker then stop --- */
360 if (!q
&& isspace(ch
))
363 /* --- Otherwise contribute and continue --- */
366 if ((ch
= NEXTCH
) == EOFCH
)
379 /* --- @putstring@ --- *
381 * Arguments: @FILE *fp@ = stream to write on
382 * @const char *p@ = pointer to text
383 * @unsigned raw@ = whether the string is to be written raw
387 * Use: Emits a string to a stream.
390 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
392 size_t sz
= strlen(p
);
396 /* --- Just write the string null terminated if raw --- */
399 fwrite(p
, 1, sz
+ 1, fp
);
403 /* --- Check for any dodgy characters --- */
406 for (q
= p
; *q
; q
++) {
407 if (isspace((unsigned char)*q
)) {
416 /* --- Emit the string --- */
418 for (q
= p
; *q
; q
++) {
420 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
421 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
422 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
423 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
424 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
425 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
426 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
427 case '`': fputc('\\', fp
); fputc('`', fp
); break;
428 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
429 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
430 case '#': fputc('\\', fp
); fputc('#', fp
); break;
443 /*----- Guts --------------------------------------------------------------*/
445 static int checkhash(const char *file
, unsigned f
,
446 const gchash
*gch
, const encodeops
*e
)
452 unsigned long n
= 0, nfail
= 0;
453 octet
*buf
= xmalloc(2 * gch
->hashsz
);
455 if (!file
|| strcmp(file
, "-") == 0)
457 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
458 moan("couldn't open `%s': %s", file
, strerror(errno
));
459 return (EXIT_FAILURE
);
462 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
467 /* --- Handle a directive --- */
471 if ((q
= str_getword(&p
)) == 0)
473 if (strcmp(q
, "hash") == 0) {
475 if ((q
= str_getword(&p
)) == 0)
477 if ((g
= gethash(q
)) == 0)
481 buf
= xmalloc(2 * gch
->hashsz
);
482 } else if (strcmp(q
, "encoding") == 0) {
484 if ((q
= str_getword(&p
)) == 0)
486 if ((ee
= getencoding(q
)) == 0)
489 } else if (strcmp(q
, "escape") == 0)
494 /* --- Otherwise it's a hex thing --- */
497 while (*p
&& *p
!= ' ')
502 if (e
->get(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
512 getstring(0, p
, &dd
, 0);
516 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
517 moan("couldn't read `%s': %s", p
, strerror(errno
));
521 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
523 fprintf(stderr
, "FAIL %s\n", p
);
525 moan("%s check failed for `%s'", gch
->name
, p
);
530 fprintf(stderr
, "OK %s\n", p
);
538 if ((f
& f_verbose
) && nfail
)
539 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
541 moan("no files checked");
545 static int dohash(const char *file
, unsigned f
,
546 const gchash
*gch
, const encodeops
*e
)
549 octet
*p
= xmalloc(gch
->hashsz
);
551 if (fhash(file
, f
, gch
, p
)) {
552 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
555 e
->put(p
, gch
->hashsz
, stdout
);
558 fputc(f
& f_binary ?
'*' : ' ', stdout
);
560 putstring(stdout
, file
, 0);
571 static int dofile(const char *file
, unsigned f
,
572 const gchash
*gch
, const encodeops
*e
)
574 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
, e
);
577 static int hashfiles(const char *file
, unsigned f
,
578 const gchash
*gch
, const encodeops
*e
)
585 if (!file
|| strcmp(file
, "-") == 0)
587 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
588 moan("couldn't open `%s': %s", file
, strerror(errno
));
589 return (EXIT_FAILURE
);
594 if (getstring(fp
, 0, &d
, f
& f_raw
))
596 if ((rrc
= dofile(d
.buf
, f
, gch
, e
)) != 0)
603 static int hashsum(const char *file
, unsigned f
,
604 const gchash
*gch
, const encodeops
*e
)
606 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
, e
);
609 /*----- Main driver -------------------------------------------------------*/
611 void version(FILE *fp
)
613 pquis(fp
, "$, Catacomb version " VERSION
"\n");
616 static void usage(FILE *fp
)
618 pquis(fp
, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
621 static void help(FILE *fp
, const gchash
*gch
)
627 Generates or checks message digests on files. Options available:\n\
629 -h, --help Display this help message.\n\
630 -V, --version Display program's version number.\n\
631 -u, --usage Display a terse usage message.\n\
632 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
634 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
635 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
637 -f, --files Read a list of file names from standard input.\n\
638 -0, --null File names are null terminated, not plain text.\n\
640 -e, --escape Escape funny characters in filenames.\n\
641 -c, --check Check message digests rather than emitting them.\n\
642 -b, --binary When reading files, treat them as binary.\n\
643 -v, --verbose Be verbose when checking digests.\n\
645 For a list of hashing algorithms and encodings, type `$ --list'.\n\
648 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
652 LI("Lists", list, listtab[i].name, listtab[i].name) \
653 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
654 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
656 MAKELISTTAB(listtab
, LISTS
)
658 int main(int argc
, char *argv
[])
661 const gchash
*gch
= 0;
662 const encodeops
*e
= &encodingtab
[0];
665 /* --- Initialization --- */
670 /* --- Choose a hash function from the name --- */
673 char *q
= xstrdup(QUIS
);
674 size_t len
= strlen(q
);
675 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
680 gch
= gethash("md5");
684 /* --- Read options --- */
687 static struct option opts
[] = {
688 { "help", 0, 0, 'h' },
689 { "verbose", 0, 0, 'V' },
690 { "usage", 0, 0, 'u' },
692 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
693 { "hash", OPTF_ARGREQ
, 0, 'a' },
694 { "encoding", OPTF_ARGREQ
, 0, 'E' },
695 { "list", 0, 0, 'l' },
697 { "files", 0, 0, 'f' },
698 { "find", 0, 0, 'f' },
699 { "null", 0, 0, '0' },
701 { "escape", 0, 0, 'e' },
702 { "check", 0, 0, 'c' },
703 { "binary", 0, 0, 'b' },
704 { "verbose", 0, 0, 'v' },
705 { "progress", 0, 0, 'p' },
709 int i
= mdwopt(argc
, argv
, "hVu a:E:l f0 ecbvp", opts
, 0, 0, 0);
724 exit(displaylists(listtab
, argv
+ optind
));
726 if ((gch
= gethash(optarg
)) == 0)
727 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
731 if ((e
= getencoding(optarg
)) == 0)
732 die(EXIT_FAILURE
, "unknown encoding `%s'", optarg
);
769 /* --- Generate output --- */
771 if (!(f
& f_check
) && (argc
|| (f
& f_files
))) {
772 if (f
& f_oddhash
) printf("#hash %s\n", gch
->name
);
773 if (f
& f_oddenc
) printf("#encoding %s\n", e
->name
);
774 if (f
& f_escape
) fputs("#escape\n", stdout
);
777 rc
= hashsum(0, f
, gch
, e
);
783 for (i
= 0; i
< argc
; i
++) {
784 if ((rrc
= hashsum(argv
[i
], f
, gch
, e
)) != 0)
792 /*----- That's all, folks -------------------------------------------------*/