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
42 #include <mLib/alloc.h>
43 #include <mLib/dstr.h>
44 #include <mLib/mdwopt.h>
45 #include <mLib/quis.h>
46 #include <mLib/report.h>
52 /*----- Static variables --------------------------------------------------*/
62 /*----- Guts --------------------------------------------------------------*/
64 static int checkhash(const gchash
*gch
, unsigned f
,
65 const char *file
, const encodeops
*e
)
71 unsigned long n
= 0, nfail
= 0;
74 if (!file
|| strcmp(file
, "-") == 0)
76 else if ((hfp
.fp
= fopen(file
, f
& GSF_RAW ?
"r" : "rb")) == 0) {
77 moan("couldn't open `%s': %s", file
, strerror(errno
));
78 return (EXIT_FAILURE
);
83 hfp
.hbuf
= xmalloc(2 * gch
->hashsz
);
88 while ((hf
= hfparse(&hfp
)) != HF_EOF
) {
92 hfp
.hbuf
= xmalloc(2 * hfp
.gch
->hashsz
);
95 if (fhash(hfp
.gch
, hfp
.f
, df
.buf
, hfp
.hbuf
+ hfp
.gch
->hashsz
)) {
96 moan("couldn't read `%s': %s", df
.buf
, strerror(errno
));
100 if (memcmp(hfp
.hbuf
, hfp
.hbuf
+ hfp
.gch
->hashsz
,
101 hfp
.gch
->hashsz
) != 0) {
102 if (hfp
.f
& f_verbose
)
103 fprintf(stderr
, "FAIL %s\n", df
.buf
);
105 moan("%s check failed for `%s'", hfp
.gch
->name
, df
.buf
);
109 if (hfp
.f
& f_verbose
)
110 fprintf(stderr
, "OK %s\n", df
.buf
);
119 if ((f
& f_verbose
) && nfail
)
120 moan("%lu of %lu file(s) failed %s check", nfail
, n
, hfp
.gch
->name
);
122 moan("no files checked");
126 static int dohash(const gchash
*gch
, unsigned f
,
127 const char *file
, const encodeops
*e
)
130 octet
*p
= xmalloc(gch
->hashsz
);
132 if (fhash(gch
, f
, file
, p
)) {
133 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
136 e
->put(p
, gch
->hashsz
, stdout
);
139 fputc(f
& FHF_BINARY ?
'*' : ' ', stdout
);
141 putstring(stdout
, file
, 0);
152 static int dofile(const gchash
*gch
, unsigned f
,
153 const char *file
, const encodeops
*e
)
154 { return (f
& f_check ? checkhash
: dohash
)(gch
, f
, file
, e
); }
156 static int hashfiles(const gchash
*gch
, unsigned f
,
157 const char *file
, const encodeops
*e
)
164 if (!file
|| strcmp(file
, "-") == 0)
166 else if ((fp
= fopen(file
, f
& GSF_RAW ?
"r" : "rb")) == 0) {
167 moan("couldn't open `%s': %s", file
, strerror(errno
));
168 return (EXIT_FAILURE
);
173 if (getstring(fp
, &d
, GSF_FILE
| f
))
175 if ((rrc
= dofile(gch
, f
, d
.buf
, e
)) != 0)
182 static int hashsum(const gchash
*gch
, unsigned f
,
183 const char *file
, const encodeops
*e
)
184 { return (f
& f_files ? hashfiles
: dofile
)(gch
, f
, file
, e
); }
186 /*----- Main driver -------------------------------------------------------*/
188 void version(FILE *fp
)
190 pquis(fp
, "$, Catacomb version " VERSION
"\n");
193 static void usage(FILE *fp
)
195 pquis(fp
, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
198 static void help(FILE *fp
, const gchash
*gch
)
204 Generates or checks message digests on files. Options available:\n\
206 -h, --help Display this help message.\n\
207 -V, --version Display program's version number.\n\
208 -u, --usage Display a terse usage message.\n\
209 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
211 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
212 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
214 -f, --files Read a list of file names from standard input.\n\
215 -0, --null File names are null terminated, not plain text.\n\
217 -e, --escape Escape funny characters in filenames.\n\
218 -c, --check Check message digests rather than emitting them.\n\
219 -b, --binary When reading files, treat them as binary.\n\
220 -v, --verbose Be verbose when checking digests.\n\
222 For a list of hashing algorithms and encodings, type `$ --list'.\n\
225 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
229 LI("Lists", list, listtab[i].name, listtab[i].name) \
230 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
231 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
233 MAKELISTTAB(listtab
, LISTS
)
235 int main(int argc
, char *argv
[])
238 const gchash
*gch
= 0;
239 const encodeops
*e
= &encodingtab
[0];
242 /* --- Initialization --- */
247 /* --- Choose a hash function from the name --- */
250 char *q
= xstrdup(QUIS
);
251 size_t len
= strlen(q
);
252 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
257 gch
= gethash("md5");
261 /* --- Read options --- */
264 static struct option opts
[] = {
265 { "help", 0, 0, 'h' },
266 { "verbose", 0, 0, 'V' },
267 { "usage", 0, 0, 'u' },
269 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
270 { "hash", OPTF_ARGREQ
, 0, 'a' },
271 { "encoding", OPTF_ARGREQ
, 0, 'E' },
272 { "list", 0, 0, 'l' },
274 { "files", 0, 0, 'f' },
275 { "find", 0, 0, 'f' },
276 { "null", 0, 0, '0' },
278 { "escape", 0, 0, 'e' },
279 { "check", 0, 0, 'c' },
280 { "binary", 0, 0, 'b' },
281 { "verbose", 0, 0, 'v' },
282 { "progress", 0, 0, 'p' },
286 int i
= mdwopt(argc
, argv
, "hVu a:E:l f0 ecbvp", opts
, 0, 0, 0);
301 exit(displaylists(listtab
, argv
+ optind
));
303 if ((gch
= gethash(optarg
)) == 0)
304 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
308 if ((e
= getencoding(optarg
)) == 0)
309 die(EXIT_FAILURE
, "unknown encoding `%s'", optarg
);
346 /* --- Generate output --- */
348 if (!(f
& f_check
) && (argc
|| (f
& f_files
))) {
349 if (f
& f_oddhash
) printf("#hash %s\n", gch
->name
);
350 if (f
& f_oddenc
) printf("#encoding %s\n", e
->name
);
351 if (f
& f_escape
) fputs("#escape\n", stdout
);
354 rc
= hashsum(gch
, f
, 0, e
);
360 for (i
= 0; i
< argc
; i
++) {
361 if ((rrc
= hashsum(gch
, f
, argv
[i
], e
)) != 0)
369 /*----- That's all, folks -------------------------------------------------*/