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(fhashstate
*fh
, const char *file
, const encodeops
*e
)
70 unsigned long n
= 0, nfail
= 0;
73 if (!file
|| strcmp(file
, "-") == 0)
75 else if ((hfp
.fp
= fopen(file
, fh
->f
& GSF_RAW ?
"r" : "rb")) == 0) {
76 moan("couldn't open `%s': %s", file
, strerror(errno
));
77 return (EXIT_FAILURE
);
82 hfp
.hbuf
= xmalloc(2 * fh
->gch
->hashsz
);
87 while ((hf
= hfparse(&hfp
)) != HF_EOF
) {
91 hfp
.hbuf
= xmalloc(2 * hfp
.gch
->hashsz
);
94 if (fhash(fh
, df
.buf
, hfp
.hbuf
+ hfp
.gch
->hashsz
)) {
95 moan("couldn't read `%s': %s", df
.buf
, strerror(errno
));
99 if (memcmp(hfp
.hbuf
, hfp
.hbuf
+ hfp
.gch
->hashsz
,
100 hfp
.gch
->hashsz
) != 0) {
101 if (hfp
.f
& f_verbose
)
102 fprintf(stderr
, "FAIL %s\n", df
.buf
);
104 moan("%s check failed for `%s'", hfp
.gch
->name
, df
.buf
);
108 if (hfp
.f
& f_verbose
)
109 fprintf(stderr
, "OK %s\n", df
.buf
);
115 if (ferror(hfp
.fp
)) {
116 moan("error reading input `%s': %s",
117 file ? file
: "<stdin>", strerror(errno
));
123 if ((fh
->f
& f_verbose
) && nfail
)
124 moan("%lu of %lu file(s) failed %s check", nfail
, n
, hfp
.gch
->name
);
126 moan("no files checked");
130 static int dohash(fhashstate
*fh
, const char *file
, const encodeops
*e
)
133 octet
*p
= xmalloc(fh
->gch
->hashsz
);
135 if (fhash(fh
, file
, p
)) {
136 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
139 e
->put(p
, fh
->gch
->hashsz
, stdout
);
142 fputc(fh
->f
& FHF_BINARY ?
'*' : ' ', stdout
);
143 if (fh
->f
& f_escape
)
144 putstring(stdout
, file
, 0);
155 static int dofile(fhashstate
*fh
, const char *file
, const encodeops
*e
)
156 { return (fh
->f
& f_check ? checkhash
: dohash
)(fh
, file
, e
); }
158 static int hashfiles(fhashstate
*fh
, const char *file
, const encodeops
*e
)
165 if (!file
|| strcmp(file
, "-") == 0)
167 else if ((fp
= fopen(file
, fh
->f
& GSF_RAW ?
"r" : "rb")) == 0) {
168 moan("couldn't open `%s': %s", file
, strerror(errno
));
169 return (EXIT_FAILURE
);
174 if (getstring(fp
, &d
, GSF_FILE
| fh
->f
))
176 if ((rrc
= dofile(fh
, d
.buf
, e
)) != 0)
183 static int hashsum(fhashstate
*fh
, const char *file
, const encodeops
*e
)
184 { return (fh
->f
& f_files ? hashfiles
: dofile
)(fh
, 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 encodeops
*e
= &encodingtab
[ENC_HEX
];
241 /* --- Initialization --- */
245 fhash_init(&fh
, 0, 0);
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 fh
.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);
292 help(stdout
, fh
.gch
);
301 exit(displaylists(listtab
, argv
+ optind
));
303 if ((fh
.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
);
331 fh
.f
|= FHF_PROGRESS
;
339 if (fh
.f
& f_bogus
) {
346 /* --- Generate output --- */
348 if (!(fh
.f
& f_check
) && (argc
|| (fh
.f
& f_files
))) {
349 if (fh
.f
& f_oddhash
) printf("#hash %s\n", fh
.gch
->name
);
350 if (fh
.f
& f_oddenc
) printf("#encoding %s\n", e
->name
);
351 if (fh
.f
& f_escape
) fputs("#escape\n", stdout
);
354 rc
= hashsum(&fh
, 0, e
);
360 for (i
= 0; i
< argc
; i
++) {
361 if ((rrc
= hashsum(&fh
, argv
[i
], e
)) != 0)
369 /*----- That's all, folks -------------------------------------------------*/