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