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
);
116 if (ferror(hfp
.fp
)) {
117 moan("error reading input `%s': %s",
118 file ? file
: "<stdin>", strerror(errno
));
124 if ((f
& f_verbose
) && nfail
)
125 moan("%lu of %lu file(s) failed %s check", nfail
, n
, hfp
.gch
->name
);
127 moan("no files checked");
131 static int dohash(const gchash
*gch
, unsigned f
,
132 const char *file
, const encodeops
*e
)
135 octet
*p
= xmalloc(gch
->hashsz
);
137 if (fhash(gch
, f
, file
, p
)) {
138 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
141 e
->put(p
, gch
->hashsz
, stdout
);
144 fputc(f
& FHF_BINARY ?
'*' : ' ', stdout
);
146 putstring(stdout
, file
, 0);
157 static int dofile(const gchash
*gch
, unsigned f
,
158 const char *file
, const encodeops
*e
)
159 { return (f
& f_check ? checkhash
: dohash
)(gch
, f
, file
, e
); }
161 static int hashfiles(const gchash
*gch
, unsigned f
,
162 const char *file
, const encodeops
*e
)
169 if (!file
|| strcmp(file
, "-") == 0)
171 else if ((fp
= fopen(file
, f
& GSF_RAW ?
"r" : "rb")) == 0) {
172 moan("couldn't open `%s': %s", file
, strerror(errno
));
173 return (EXIT_FAILURE
);
178 if (getstring(fp
, &d
, GSF_FILE
| f
))
180 if ((rrc
= dofile(gch
, f
, d
.buf
, e
)) != 0)
187 static int hashsum(const gchash
*gch
, unsigned f
,
188 const char *file
, const encodeops
*e
)
189 { return (f
& f_files ? hashfiles
: dofile
)(gch
, f
, file
, e
); }
191 /*----- Main driver -------------------------------------------------------*/
193 void version(FILE *fp
)
195 pquis(fp
, "$, Catacomb version " VERSION
"\n");
198 static void usage(FILE *fp
)
200 pquis(fp
, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n");
203 static void help(FILE *fp
, const gchash
*gch
)
209 Generates or checks message digests on files. Options available:\n\
211 -h, --help Display this help message.\n\
212 -V, --version Display program's version number.\n\
213 -u, --usage Display a terse usage message.\n\
214 -l, --list [ITEM...] Show known hash functions and/or encodings.\n\
216 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
217 -E, --encoding=ENC Represent hashes using encoding ENC.\n\
219 -f, --files Read a list of file names from standard input.\n\
220 -0, --null File names are null terminated, not plain text.\n\
222 -e, --escape Escape funny characters in filenames.\n\
223 -c, --check Check message digests rather than emitting them.\n\
224 -b, --binary When reading files, treat them as binary.\n\
225 -v, --verbose Be verbose when checking digests.\n\
227 For a list of hashing algorithms and encodings, type `$ --list'.\n\
230 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
234 LI("Lists", list, listtab[i].name, listtab[i].name) \
235 LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \
236 LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name)
238 MAKELISTTAB(listtab
, LISTS
)
240 int main(int argc
, char *argv
[])
243 const gchash
*gch
= 0;
244 const encodeops
*e
= &encodingtab
[ENC_HEX
];
247 /* --- Initialization --- */
252 /* --- Choose a hash function from the name --- */
255 char *q
= xstrdup(QUIS
);
256 size_t len
= strlen(q
);
257 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
262 gch
= gethash("md5");
266 /* --- Read options --- */
269 static struct option opts
[] = {
270 { "help", 0, 0, 'h' },
271 { "verbose", 0, 0, 'V' },
272 { "usage", 0, 0, 'u' },
274 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
275 { "hash", OPTF_ARGREQ
, 0, 'a' },
276 { "encoding", OPTF_ARGREQ
, 0, 'E' },
277 { "list", 0, 0, 'l' },
279 { "files", 0, 0, 'f' },
280 { "find", 0, 0, 'f' },
281 { "null", 0, 0, '0' },
283 { "escape", 0, 0, 'e' },
284 { "check", 0, 0, 'c' },
285 { "binary", 0, 0, 'b' },
286 { "verbose", 0, 0, 'v' },
287 { "progress", 0, 0, 'p' },
291 int i
= mdwopt(argc
, argv
, "hVu a:E:l f0 ecbvp", opts
, 0, 0, 0);
306 exit(displaylists(listtab
, argv
+ optind
));
308 if ((gch
= gethash(optarg
)) == 0)
309 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
313 if ((e
= getencoding(optarg
)) == 0)
314 die(EXIT_FAILURE
, "unknown encoding `%s'", optarg
);
351 /* --- Generate output --- */
353 if (!(f
& f_check
) && (argc
|| (f
& f_files
))) {
354 if (f
& f_oddhash
) printf("#hash %s\n", gch
->name
);
355 if (f
& f_oddenc
) printf("#encoding %s\n", e
->name
);
356 if (f
& f_escape
) fputs("#escape\n", stdout
);
359 rc
= hashsum(gch
, f
, 0, e
);
365 for (i
= 0; i
< argc
; i
++) {
366 if ((rrc
= hashsum(gch
, f
, argv
[i
], e
)) != 0)
374 /*----- That's all, folks -------------------------------------------------*/