cksum.c revision 1.17 1 1.17 atatat /* $NetBSD: cksum.c,v 1.17 2002/10/18 20:30:13 atatat Exp $ */
2 1.6 glass
3 1.1 cgd /*-
4 1.8 thorpej * Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
5 1.3 cgd * Copyright (c) 1991, 1993
6 1.3 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * This code is derived from software contributed to Berkeley by
9 1.3 cgd * James W. Williams of NASA Goddard Space Flight Center.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.1 cgd * 3. All advertising materials mentioning features or use of this software
20 1.1 cgd * must display the following acknowledgement:
21 1.1 cgd * This product includes software developed by the University of
22 1.1 cgd * California, Berkeley and its contributors.
23 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
24 1.1 cgd * may be used to endorse or promote products derived from this software
25 1.1 cgd * without specific prior written permission.
26 1.1 cgd *
27 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 cgd * SUCH DAMAGE.
38 1.1 cgd */
39 1.1 cgd
40 1.10 lukem #include <sys/cdefs.h>
41 1.16 bjh21 #if defined(__COPYRIGHT) && !defined(lint)
42 1.10 lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
43 1.10 lukem The Regents of the University of California. All rights reserved.\n");
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.16 bjh21 #if defined(__RCSID) && !defined(lint)
47 1.6 glass #if 0
48 1.7 jtc static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95";
49 1.6 glass #endif
50 1.17 atatat __RCSID("$NetBSD: cksum.c,v 1.17 2002/10/18 20:30:13 atatat Exp $");
51 1.1 cgd #endif /* not lint */
52 1.1 cgd
53 1.1 cgd #include <sys/cdefs.h>
54 1.1 cgd #include <sys/types.h>
55 1.5 mycroft
56 1.5 mycroft #include <err.h>
57 1.5 mycroft #include <errno.h>
58 1.1 cgd #include <fcntl.h>
59 1.9 kleink #include <locale.h>
60 1.8 thorpej #include <md5.h>
61 1.13 atatat #include <md4.h>
62 1.13 atatat #include <md2.h>
63 1.13 atatat #include <sha1.h>
64 1.13 atatat #include <rmd160.h>
65 1.1 cgd #include <stdio.h>
66 1.1 cgd #include <stdlib.h>
67 1.1 cgd #include <string.h>
68 1.5 mycroft #include <unistd.h>
69 1.5 mycroft
70 1.1 cgd #include "extern.h"
71 1.1 cgd
72 1.13 atatat #define HASH_MD2 0
73 1.13 atatat #define HASH_MD4 1
74 1.13 atatat #define HASH_MD5 2
75 1.13 atatat #define HASH_SHA1 3
76 1.13 atatat #define HASH_RMD160 4
77 1.13 atatat
78 1.13 atatat typedef char *(*_filefunc)(const char *, char *);
79 1.13 atatat
80 1.13 atatat struct hash {
81 1.13 atatat const char *progname;
82 1.13 atatat const char *hashname;
83 1.13 atatat void (*stringfunc)(const char *);
84 1.13 atatat void (*timetrialfunc)(void);
85 1.13 atatat void (*testsuitefunc)(void);
86 1.13 atatat void (*filterfunc)(int);
87 1.13 atatat char *(*filefunc)(const char *, char *);
88 1.13 atatat } hashes[] = {
89 1.13 atatat { "md2", "MD2",
90 1.13 atatat MD2String, MD2TimeTrial, MD2TestSuite,
91 1.13 atatat MD2Filter, MD2File },
92 1.13 atatat { "md4", "MD4",
93 1.13 atatat MD4String, MD4TimeTrial, MD4TestSuite,
94 1.13 atatat MD4Filter, MD4File },
95 1.13 atatat { "md5", "MD5",
96 1.13 atatat MD5String, MD5TimeTrial, MD5TestSuite,
97 1.13 atatat MD5Filter, MD5File },
98 1.13 atatat { "sha1", "SHA1",
99 1.13 atatat SHA1String, SHA1TimeTrial, SHA1TestSuite,
100 1.13 atatat SHA1Filter, (_filefunc) SHA1File },
101 1.13 atatat { "rmd160", "RMD160",
102 1.13 atatat RMD160String, RMD160TimeTrial, RMD160TestSuite,
103 1.13 atatat RMD160Filter, (_filefunc) RMD160File },
104 1.13 atatat { NULL }
105 1.13 atatat };
106 1.13 atatat
107 1.10 lukem int main __P((int, char **));
108 1.17 atatat int hash_digest_file __P((char *, struct hash *, int));
109 1.13 atatat void requirehash __P((const char *));
110 1.10 lukem void usage __P((void));
111 1.3 cgd
112 1.3 cgd int
113 1.1 cgd main(argc, argv)
114 1.1 cgd int argc;
115 1.1 cgd char **argv;
116 1.1 cgd {
117 1.13 atatat register int ch, fd, rval, dosum, pflag, nohashstdin;
118 1.4 cgd u_int32_t len, val;
119 1.1 cgd char *fn;
120 1.13 atatat const char *progname;
121 1.4 cgd int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
122 1.4 cgd void (*pfncn) __P((char *, u_int32_t, u_int32_t));
123 1.13 atatat struct hash *hash;
124 1.17 atatat int normal;
125 1.1 cgd
126 1.10 lukem cfncn = NULL;
127 1.10 lukem pfncn = NULL;
128 1.13 atatat dosum = pflag = nohashstdin = 0;
129 1.17 atatat normal = 0;
130 1.9 kleink
131 1.9 kleink setlocale(LC_ALL, "");
132 1.8 thorpej
133 1.13 atatat progname = getprogname();
134 1.13 atatat
135 1.13 atatat for (hash = hashes; hash->hashname != NULL; hash++)
136 1.13 atatat if (strcmp(progname, hash->progname) == 0)
137 1.13 atatat break;
138 1.13 atatat
139 1.13 atatat if (hash->hashname == NULL) {
140 1.13 atatat hash = NULL;
141 1.13 atatat
142 1.13 atatat if (!strcmp(progname, "sum")) {
143 1.13 atatat dosum = 1;
144 1.13 atatat cfncn = csum1;
145 1.13 atatat pfncn = psum1;
146 1.13 atatat } else {
147 1.15 atatat cfncn = crc;
148 1.13 atatat pfncn = pcrc;
149 1.13 atatat }
150 1.5 mycroft }
151 1.5 mycroft
152 1.17 atatat while ((ch = getopt(argc, argv, "mno:ps:tx12456")) != -1)
153 1.1 cgd switch(ch) {
154 1.13 atatat case '2':
155 1.13 atatat if (dosum) {
156 1.13 atatat warnx("sum mutually exclusive with md2");
157 1.13 atatat usage();
158 1.13 atatat }
159 1.13 atatat hash = &hashes[HASH_MD2];
160 1.13 atatat break;
161 1.13 atatat case '4':
162 1.13 atatat if (dosum) {
163 1.13 atatat warnx("sum mutually exclusive with md4");
164 1.13 atatat usage();
165 1.13 atatat }
166 1.13 atatat hash = &hashes[HASH_MD4];
167 1.13 atatat break;
168 1.8 thorpej case 'm':
169 1.13 atatat case '5':
170 1.8 thorpej if (dosum) {
171 1.8 thorpej warnx("sum mutually exclusive with md5");
172 1.8 thorpej usage();
173 1.8 thorpej }
174 1.13 atatat hash = &hashes[HASH_MD5];
175 1.13 atatat break;
176 1.13 atatat case '1':
177 1.13 atatat if (dosum) {
178 1.13 atatat warnx("sum mutually exclusive with sha1");
179 1.13 atatat usage();
180 1.13 atatat }
181 1.13 atatat hash = &hashes[HASH_SHA1];
182 1.13 atatat break;
183 1.13 atatat case '6':
184 1.13 atatat if (dosum) {
185 1.13 atatat warnx("sum mutually exclusive with rmd160");
186 1.13 atatat usage();
187 1.13 atatat }
188 1.13 atatat hash = &hashes[HASH_RMD160];
189 1.8 thorpej break;
190 1.17 atatat case 'n':
191 1.17 atatat normal = 1;
192 1.17 atatat break;
193 1.1 cgd case 'o':
194 1.13 atatat if (hash) {
195 1.13 atatat warnx("%s mutually exclusive with sum",
196 1.13 atatat hash->hashname);
197 1.8 thorpej usage();
198 1.8 thorpej }
199 1.5 mycroft if (!strcmp(optarg, "1")) {
200 1.1 cgd cfncn = csum1;
201 1.1 cgd pfncn = psum1;
202 1.5 mycroft } else if (!strcmp(optarg, "2")) {
203 1.1 cgd cfncn = csum2;
204 1.1 cgd pfncn = psum2;
205 1.1 cgd } else {
206 1.5 mycroft warnx("illegal argument to -o option");
207 1.1 cgd usage();
208 1.1 cgd }
209 1.1 cgd break;
210 1.8 thorpej case 'p':
211 1.13 atatat if (hash == NULL)
212 1.13 atatat requirehash("-p");
213 1.8 thorpej pflag = 1;
214 1.8 thorpej break;
215 1.8 thorpej case 's':
216 1.13 atatat if (hash == NULL)
217 1.13 atatat requirehash("-s");
218 1.13 atatat nohashstdin = 1;
219 1.13 atatat hash->stringfunc(optarg);
220 1.8 thorpej break;
221 1.8 thorpej case 't':
222 1.13 atatat if (hash == NULL)
223 1.13 atatat requirehash("-t");
224 1.13 atatat nohashstdin = 1;
225 1.13 atatat hash->timetrialfunc();
226 1.8 thorpej break;
227 1.8 thorpej case 'x':
228 1.13 atatat if (hash == NULL)
229 1.13 atatat requirehash("-x");
230 1.13 atatat nohashstdin = 1;
231 1.13 atatat hash->testsuitefunc();
232 1.8 thorpej break;
233 1.1 cgd case '?':
234 1.1 cgd default:
235 1.1 cgd usage();
236 1.1 cgd }
237 1.1 cgd argc -= optind;
238 1.1 cgd argv += optind;
239 1.1 cgd
240 1.1 cgd fd = STDIN_FILENO;
241 1.3 cgd fn = NULL;
242 1.1 cgd rval = 0;
243 1.1 cgd do {
244 1.1 cgd if (*argv) {
245 1.1 cgd fn = *argv++;
246 1.13 atatat if (hash != NULL) {
247 1.17 atatat if (hash_digest_file(fn, hash, normal)) {
248 1.8 thorpej warn("%s", fn);
249 1.8 thorpej rval = 1;
250 1.8 thorpej }
251 1.8 thorpej continue;
252 1.8 thorpej }
253 1.1 cgd if ((fd = open(fn, O_RDONLY, 0)) < 0) {
254 1.5 mycroft warn("%s", fn);
255 1.1 cgd rval = 1;
256 1.1 cgd continue;
257 1.1 cgd }
258 1.13 atatat } else if (hash && !nohashstdin) {
259 1.13 atatat hash->filterfunc(pflag);
260 1.13 atatat }
261 1.8 thorpej
262 1.13 atatat if (hash == NULL) {
263 1.8 thorpej if (cfncn(fd, &val, &len)) {
264 1.8 thorpej warn("%s", fn ? fn : "stdin");
265 1.8 thorpej rval = 1;
266 1.8 thorpej } else
267 1.8 thorpej pfncn(fn, val, len);
268 1.8 thorpej (void)close(fd);
269 1.1 cgd }
270 1.1 cgd } while (*argv);
271 1.1 cgd exit(rval);
272 1.1 cgd }
273 1.1 cgd
274 1.8 thorpej int
275 1.17 atatat hash_digest_file(fn, hash, normal)
276 1.8 thorpej char *fn;
277 1.13 atatat struct hash *hash;
278 1.17 atatat int normal;
279 1.8 thorpej {
280 1.13 atatat char buf[41], *cp;
281 1.8 thorpej
282 1.13 atatat cp = hash->filefunc(fn, buf);
283 1.8 thorpej if (cp == NULL)
284 1.8 thorpej return (1);
285 1.8 thorpej
286 1.17 atatat if (normal)
287 1.17 atatat printf("%s %s\n", cp, fn);
288 1.17 atatat else
289 1.17 atatat printf("%s (%s) = %s\n", hash->hashname, fn, cp);
290 1.8 thorpej return (0);
291 1.8 thorpej }
292 1.8 thorpej
293 1.8 thorpej void
294 1.13 atatat requirehash(flg)
295 1.8 thorpej const char *flg;
296 1.8 thorpej {
297 1.13 atatat warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
298 1.13 atatat flg);
299 1.8 thorpej usage();
300 1.8 thorpej }
301 1.8 thorpej
302 1.3 cgd void
303 1.1 cgd usage()
304 1.1 cgd {
305 1.5 mycroft
306 1.8 thorpej (void)fprintf(stderr, "usage: cksum [-m | [-o 1 | 2]] [file ...]\n");
307 1.8 thorpej (void)fprintf(stderr, " sum [file ...]\n");
308 1.8 thorpej (void)fprintf(stderr,
309 1.17 atatat " md2 [-n] [-p | -t | -x | -s string] [file ...]\n");
310 1.13 atatat (void)fprintf(stderr,
311 1.17 atatat " md4 [-n] [-p | -t | -x | -s string] [file ...]\n");
312 1.13 atatat (void)fprintf(stderr,
313 1.17 atatat " md5 [-n] [-p | -t | -x | -s string] [file ...]\n");
314 1.13 atatat (void)fprintf(stderr,
315 1.17 atatat " sha1 [-n] [-p | -t | -x | -s string] [file ...]\n");
316 1.13 atatat (void)fprintf(stderr,
317 1.17 atatat " rmd160 [-n] [-p | -t | -x | -s string] [file ...]\n");
318 1.1 cgd exit(1);
319 1.1 cgd }
320