Home | History | Annotate | Line # | Download | only in cksum
cksum.c revision 1.18
      1 /*	$NetBSD: cksum.c,v 1.18 2003/08/07 11:13:20 agc 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.18 2003/08/07 11:13:20 agc 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 len, val;
    149 	char *fn;
    150 	const char *progname;
    151 	int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
    152 	void (*pfncn) __P((char *, u_int32_t, u_int32_t));
    153 	struct hash *hash;
    154 	int normal;
    155 
    156 	cfncn = NULL;
    157 	pfncn = NULL;
    158 	dosum = pflag = nohashstdin = 0;
    159 	normal = 0;
    160 
    161 	setlocale(LC_ALL, "");
    162 
    163 	progname = getprogname();
    164 
    165 	for (hash = hashes; hash->hashname != NULL; hash++)
    166 		if (strcmp(progname, hash->progname) == 0)
    167 			break;
    168 
    169 	if (hash->hashname == NULL) {
    170 		hash = NULL;
    171 
    172 		if (!strcmp(progname, "sum")) {
    173 			dosum = 1;
    174 			cfncn = csum1;
    175 			pfncn = psum1;
    176 		} else {
    177 			cfncn = crc;
    178 			pfncn = pcrc;
    179 		}
    180 	}
    181 
    182 	while ((ch = getopt(argc, argv, "mno:ps:tx12456")) != -1)
    183 		switch(ch) {
    184 		case '2':
    185 			if (dosum) {
    186 				warnx("sum mutually exclusive with md2");
    187 				usage();
    188 			}
    189 			hash = &hashes[HASH_MD2];
    190 			break;
    191 		case '4':
    192 			if (dosum) {
    193 				warnx("sum mutually exclusive with md4");
    194 				usage();
    195 			}
    196 			hash = &hashes[HASH_MD4];
    197 			break;
    198 		case 'm':
    199 		case '5':
    200 			if (dosum) {
    201 				warnx("sum mutually exclusive with md5");
    202 				usage();
    203 			}
    204 			hash = &hashes[HASH_MD5];
    205 			break;
    206 		case '1':
    207 			if (dosum) {
    208 				warnx("sum mutually exclusive with sha1");
    209 				usage();
    210 			}
    211 			hash = &hashes[HASH_SHA1];
    212 			break;
    213 		case '6':
    214 			if (dosum) {
    215 				warnx("sum mutually exclusive with rmd160");
    216 				usage();
    217 			}
    218 			hash = &hashes[HASH_RMD160];
    219 			break;
    220 		case 'n':
    221 			normal = 1;
    222 			break;
    223 		case 'o':
    224 			if (hash) {
    225 				warnx("%s mutually exclusive with sum",
    226 				      hash->hashname);
    227 				usage();
    228 			}
    229 			if (!strcmp(optarg, "1")) {
    230 				cfncn = csum1;
    231 				pfncn = psum1;
    232 			} else if (!strcmp(optarg, "2")) {
    233 				cfncn = csum2;
    234 				pfncn = psum2;
    235 			} else {
    236 				warnx("illegal argument to -o option");
    237 				usage();
    238 			}
    239 			break;
    240 		case 'p':
    241 			if (hash == NULL)
    242 				requirehash("-p");
    243 			pflag = 1;
    244 			break;
    245 		case 's':
    246 			if (hash == NULL)
    247 				requirehash("-s");
    248 			nohashstdin = 1;
    249 			hash->stringfunc(optarg);
    250 			break;
    251 		case 't':
    252 			if (hash == NULL)
    253 				requirehash("-t");
    254 			nohashstdin = 1;
    255 			hash->timetrialfunc();
    256 			break;
    257 		case 'x':
    258 			if (hash == NULL)
    259 				requirehash("-x");
    260 			nohashstdin = 1;
    261 			hash->testsuitefunc();
    262 			break;
    263 		case '?':
    264 		default:
    265 			usage();
    266 		}
    267 	argc -= optind;
    268 	argv += optind;
    269 
    270 	fd = STDIN_FILENO;
    271 	fn = NULL;
    272 	rval = 0;
    273 	do {
    274 		if (*argv) {
    275 			fn = *argv++;
    276 			if (hash != NULL) {
    277 				if (hash_digest_file(fn, hash, normal)) {
    278 					warn("%s", fn);
    279 					rval = 1;
    280 				}
    281 				continue;
    282 			}
    283 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
    284 				warn("%s", fn);
    285 				rval = 1;
    286 				continue;
    287 			}
    288 		} else if (hash && !nohashstdin) {
    289 			hash->filterfunc(pflag);
    290 		}
    291 
    292 		if (hash == NULL) {
    293 			if (cfncn(fd, &val, &len)) {
    294 				warn("%s", fn ? fn : "stdin");
    295 				rval = 1;
    296 			} else
    297 				pfncn(fn, val, len);
    298 			(void)close(fd);
    299 		}
    300 	} while (*argv);
    301 	exit(rval);
    302 }
    303 
    304 int
    305 hash_digest_file(fn, hash, normal)
    306 	char *fn;
    307 	struct hash *hash;
    308 	int normal;
    309 {
    310 	char buf[41], *cp;
    311 
    312 	cp = hash->filefunc(fn, buf);
    313 	if (cp == NULL)
    314 		return (1);
    315 
    316 	if (normal)
    317 		printf("%s %s\n", cp, fn);
    318 	else
    319 		printf("%s (%s) = %s\n", hash->hashname, fn, cp);
    320 	return (0);
    321 }
    322 
    323 void
    324 requirehash(flg)
    325 	const char *flg;
    326 {
    327 	warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
    328 	    flg);
    329 	usage();
    330 }
    331 
    332 void
    333 usage()
    334 {
    335 
    336 	(void)fprintf(stderr, "usage: cksum [-m | [-o 1 | 2]] [file ...]\n");
    337 	(void)fprintf(stderr, "       sum [file ...]\n");
    338 	(void)fprintf(stderr,
    339 	    "       md2 [-n] [-p | -t | -x | -s string] [file ...]\n");
    340 	(void)fprintf(stderr,
    341 	    "       md4 [-n] [-p | -t | -x | -s string] [file ...]\n");
    342 	(void)fprintf(stderr,
    343 	    "       md5 [-n] [-p | -t | -x | -s string] [file ...]\n");
    344 	(void)fprintf(stderr,
    345 	    "       sha1 [-n] [-p | -t | -x | -s string] [file ...]\n");
    346 	(void)fprintf(stderr,
    347 	    "       rmd160 [-n] [-p | -t | -x | -s string] [file ...]\n");
    348 	exit(1);
    349 }
    350