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