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 ------------------------------------------------------*/
41 #include <mLib/alloc.h>
42 #include <mLib/dstr.h>
43 #include <mLib/mdwopt.h>
44 #include <mLib/quis.h>
45 #include <mLib/report.h>
50 #include <mLib/base32.h>
51 #include <mLib/base64.h>
55 /*----- Static variables --------------------------------------------------*/
67 /*----- Encoding and decoding ---------------------------------------------*/
69 /* --- Hex encoding --- */
71 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
74 fprintf(fp
, "%02x", *buf
++);
79 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
83 isxdigit((unsigned char)p
[0]) &&
84 isxdigit((unsigned char)p
[1])) {
89 *q
++ = strtoul(buf
, 0, 16);
99 /* --- Base64 encoding --- */
101 static void putb64(const octet
*buf
, size_t sz
, FILE *fp
)
109 base64_encode(&b
, buf
, sz
, &d
);
110 base64_encode(&b
, 0, 0, &d
);
115 static size_t getb64(const char *p
, octet
*q
, size_t sz
, char **pp
)
119 size_t n
= strlen(p
);
122 base64_decode(&b
, p
, n
, &d
);
123 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
124 base64_decode(&b
, 0, 0, &d
);
126 memcpy(q
, d
.buf
, sz
);
132 /* --- Base32 encoding --- */
134 static void putb32(const octet
*buf
, size_t sz
, FILE *fp
)
142 base32_encode(&b
, buf
, sz
, &d
);
143 base32_encode(&b
, 0, 0, &d
);
148 static size_t getb32(const char *p
, octet
*q
, size_t sz
, char **pp
)
152 size_t n
= strlen(p
);
155 base32_decode(&b
, p
, n
, &d
);
156 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
157 base32_decode(&b
, 0, 0, &d
);
159 memcpy(q
, d
.buf
, sz
);
167 typedef struct encops
{
169 void (*put
)(const octet
*, size_t, FILE *);
170 size_t (*get
)(const char *, octet
*, size_t, char **);
173 static const encops enctab
[] = {
174 { "hex", puthex
, gethex
},
175 { "base64", putb64
, getb64
},
176 { "base32", putb32
, getb32
},
180 static const encops
*getenc(const char *ename
)
184 for (e
= enctab
; e
->name
; e
++) {
185 if (strcmp(ename
, e
->name
) == 0)
191 /*----- Support functions -------------------------------------------------*/
195 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
196 * @unsigned f@ = flags to set
197 * @const gchash *gch@ = pointer to hash function to use
198 * @void *buf@ = pointer to hash output buffer
200 * Returns: Zero if it worked, nonzero on error.
202 * Use: Hashes a file.
205 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
215 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
219 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
220 GH_HASH(h
, fbuf
, sz
);
229 /* --- @gethash@ --- *
231 * Arguments: @const char *name@ = pointer to name string
233 * Returns: Pointer to appropriate hash class.
235 * Use: Chooses a hash function by name.
238 static const gchash
*gethash(const char *name
)
240 const gchash
*const *g
, *gg
= 0;
241 size_t sz
= strlen(name
);
242 for (g
= ghashtab
; *g
; g
++) {
243 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
244 if ((*g
)->name
[sz
] == 0) {
256 /* --- @getstring@ --- *
258 * Arguments: @FILE *fp@ = stream from which to read
259 * @const char *p@ = string to read from instead
260 * @dstr *d@ = destination string
261 * @unsigned raw@ = raw or cooked read
263 * Returns: Zero if OK, nonzero on end-of-file.
265 * Use: Reads a filename (or something similar) from a stream.
268 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
273 /* --- Raw: just read exactly what's written up to a null byte --- */
275 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
276 #define EOFCH (fp ? EOF : 0)
279 if ((ch
= NEXTCH
) == EOFCH
)
285 if ((ch
= NEXTCH
) == EOFCH
)
292 /* --- Skip as far as whitespace --- *
294 * Also skip past comments.
302 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
308 /* --- If the character is a quote then read a quoted string --- */
320 /* --- Now read all sorts of interesting things --- */
324 /* --- Handle an escaped thing --- */
331 case 'a': ch
= '\a'; break;
332 case 'b': ch
= '\b'; break;
333 case 'f': ch
= '\f'; break;
334 case 'n': ch
= '\n'; break;
335 case 'r': ch
= '\r'; break;
336 case 't': ch
= '\t'; break;
337 case 'v': ch
= '\v'; break;
344 /* --- If it's a quote or some other end marker then stop --- */
348 if (!q
&& isspace(ch
))
351 /* --- Otherwise contribute and continue --- */
354 if ((ch
= NEXTCH
) == EOFCH
)
367 /* --- @putstring@ --- *
369 * Arguments: @FILE *fp@ = stream to write on
370 * @const char *p@ = pointer to text
371 * @unsigned raw@ = whether the string is to be written raw
375 * Use: Emits a string to a stream.
378 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
380 size_t sz
= strlen(p
);
384 /* --- Just write the string null terminated if raw --- */
387 fwrite(p
, 1, sz
+ 1, fp
);
391 /* --- Check for any dodgy characters --- */
394 for (q
= p
; *q
; q
++) {
395 if (isspace((unsigned char)*q
)) {
404 /* --- Emit the string --- */
406 for (q
= p
; *q
; q
++) {
408 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
409 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
410 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
411 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
412 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
413 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
414 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
415 case '`': fputc('\\', fp
); fputc('`', fp
); break;
416 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
417 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
418 case '#': fputc('\\', fp
); fputc('#', fp
); break;
431 /*----- Guts --------------------------------------------------------------*/
433 static int checkhash(const char *file
, unsigned f
,
434 const gchash
*gch
, const encops
*e
)
440 unsigned long n
= 0, nfail
= 0;
441 octet
*buf
= xmalloc(2 * gch
->hashsz
);
445 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
446 moan("couldn't open `%s': %s", file
, strerror(errno
));
447 return (EXIT_FAILURE
);
450 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
455 /* --- Handle a directive --- */
459 if ((q
= str_getword(&p
)) == 0)
461 if (strcmp(q
, "hash") == 0) {
463 if ((q
= str_getword(&p
)) == 0)
465 if ((g
= gethash(q
)) == 0)
469 buf
= xmalloc(2 * gch
->hashsz
);
470 } else if (strcmp(q
, "encoding") == 0) {
472 if ((q
= str_getword(&p
)) == 0)
474 if ((ee
= getenc(q
)) == 0)
477 } else if (strcmp(q
, "escape") == 0)
482 /* --- Otherwise it's a hex thing --- */
485 while (*p
&& *p
!= ' ')
490 if (e
->get(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
500 getstring(0, p
, &dd
, 0);
504 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
505 moan("couldn't read `%s': %s", p
, strerror(errno
));
509 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
511 fprintf(stderr
, "FAIL %s\n", p
);
513 moan("%s check failed for `%s'", gch
->name
, p
);
518 fprintf(stderr
, "OK %s\n", p
);
526 if ((f
& f_verbose
) && nfail
)
527 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
529 moan("no files checked");
533 static int dohash(const char *file
, unsigned f
,
534 const gchash
*gch
, const encops
*e
)
537 octet
*p
= xmalloc(gch
->hashsz
);
539 if (fhash(file
, f
, gch
, p
)) {
540 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
543 e
->put(p
, gch
->hashsz
, stdout
);
546 fputc(f
& f_binary ?
'*' : ' ', stdout
);
548 putstring(stdout
, file
, 0);
559 static int dofile(const char *file
, unsigned f
,
560 const gchash
*gch
, const encops
*e
)
562 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
, e
);
565 static int hashfiles(const char *file
, unsigned f
,
566 const gchash
*gch
, const encops
*e
)
575 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
576 moan("couldn't open `%s': %s", file
, strerror(errno
));
577 return (EXIT_FAILURE
);
582 if (getstring(fp
, 0, &d
, f
& f_raw
))
584 if ((rrc
= dofile(d
.buf
, f
, gch
, e
)) != 0)
591 static int hashsum(const char *file
, unsigned f
,
592 const gchash
*gch
, const encops
*e
)
594 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
, e
);
597 /*----- Main driver -------------------------------------------------------*/
599 static void version(FILE *fp
)
601 pquis(fp
, "$, Catacomb version " VERSION
"\n");
604 static void usage(FILE *fp
)
606 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
609 static void help(FILE *fp
, const gchash
*gch
)
615 Generates or checks message digests on files. Options available:\n\
617 -h, --help Display this help message.\n\
618 -V, --version Display program's version number.\n\
619 -u, --usage Display a terse usage message.\n\
621 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
622 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
624 -f, --files Read a list of file names from standard input.\n\
625 -0, --null File names are null terminated, not plain text.\n\
627 -e, --escape Escape funny characters in filenames.\n\
628 -c, --check Check message digests rather than emitting them.\n\
629 -b, --binary When reading files, treat them as binary.\n\
630 -v, --verbose Be verbose when checking digests.\n\
632 For a list of hashing algorithms and encodings, type `$ --list'.\n\
635 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
638 int main(int argc
, char *argv
[])
641 const gchash
*gch
= 0;
642 const encops
*e
= &enctab
[0];
645 /* --- Initialization --- */
650 /* --- Choose a hash function from the name --- */
653 char *q
= xstrdup(QUIS
);
654 size_t len
= strlen(q
);
655 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
660 gch
= gethash("md5");
664 /* --- Read options --- */
667 static struct option opts
[] = {
668 { "help", 0, 0, 'h' },
669 { "verbose", 0, 0, 'V' },
670 { "usage", 0, 0, 'u' },
672 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
673 { "hash", OPTF_ARGREQ
, 0, 'a' },
674 { "encoding", OPTF_ARGREQ
, 0, 'E' },
675 { "list", 0, 0, 'l' },
677 { "files", 0, 0, 'f' },
678 { "find", 0, 0, 'f' },
679 { "null", 0, 0, '0' },
681 { "escape", 0, 0, 'e' },
682 { "check", 0, 0, 'c' },
683 { "binary", 0, 0, 'b' },
684 { "verbose", 0, 0, 'v' },
688 int i
= mdwopt(argc
, argv
, "hVu a:E:l f0 ecbv", opts
, 0, 0, 0);
703 if ((gch
= gethash(optarg
)) == 0)
704 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
709 printf("Algorithms: ");
710 for (j
= 0; ghashtab
[j
]; j
++) {
711 if (j
) fputc(' ', stdout
);
712 printf("%s", ghashtab
[j
]->name
);
715 printf("Encodings: ");
716 for (j
= 0; enctab
[j
].name
; j
++) {
717 if (j
) fputc(' ', stdout
);
718 printf("%s", enctab
[j
].name
);
724 if ((e
= getenc(optarg
)) == 0)
725 die(EXIT_FAILURE
, "unknown encoding `%s'", optarg
);
759 /* --- Generate output --- */
762 rc
= hashsum(0, f
, gch
, e
);
768 if (!(f
& f_check
)) {
769 if (f
& f_oddhash
) printf("#hash %s\n", gch
->name
);
770 if (f
& f_oddenc
) printf("#encoding %s\n", e
->name
);
771 if (f
& f_escape
) fputs("#escape\n", stdout
);
773 for (i
= 0; i
< argc
; i
++) {
774 if ((rrc
= hashsum(argv
[i
], f
, gch
, e
)) != 0)
782 /*----- That's all, folks -------------------------------------------------*/