Home | History | Annotate | Line # | Download | only in cksum
cksum.c revision 1.37
      1  1.37  hubertf /*	$NetBSD: cksum.c,v 1.37 2006/05/07 12:22:55 hubertf 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.20      jmc #if HAVE_NBTOOL_CONFIG_H
     71  1.20      jmc #include "nbtool_config.h"
     72  1.20      jmc #endif
     73  1.20      jmc 
     74  1.10    lukem #include <sys/cdefs.h>
     75  1.16    bjh21 #if defined(__COPYRIGHT) && !defined(lint)
     76  1.10    lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     77  1.10    lukem 	The Regents of the University of California.  All rights reserved.\n");
     78   1.1      cgd #endif /* not lint */
     79   1.1      cgd 
     80  1.16    bjh21 #if defined(__RCSID) && !defined(lint)
     81   1.6    glass #if 0
     82   1.7      jtc static char sccsid[] = "@(#)cksum.c	8.2 (Berkeley) 4/28/95";
     83   1.6    glass #endif
     84  1.37  hubertf __RCSID("$NetBSD: cksum.c,v 1.37 2006/05/07 12:22:55 hubertf Exp $");
     85   1.1      cgd #endif /* not lint */
     86   1.1      cgd 
     87   1.1      cgd #include <sys/cdefs.h>
     88   1.1      cgd #include <sys/types.h>
     89   1.5  mycroft 
     90  1.37  hubertf #include <ctype.h>
     91   1.5  mycroft #include <err.h>
     92   1.5  mycroft #include <errno.h>
     93   1.1      cgd #include <fcntl.h>
     94   1.9   kleink #include <locale.h>
     95   1.8  thorpej #include <md5.h>
     96  1.13   atatat #include <md4.h>
     97  1.13   atatat #include <md2.h>
     98  1.13   atatat #include <sha1.h>
     99  1.27     elad #include <crypto/sha2.h>
    100  1.31     elad #include <crypto/rmd160.h>
    101   1.1      cgd #include <stdio.h>
    102   1.1      cgd #include <stdlib.h>
    103   1.1      cgd #include <string.h>
    104   1.5  mycroft #include <unistd.h>
    105   1.5  mycroft 
    106   1.1      cgd #include "extern.h"
    107   1.1      cgd 
    108  1.13   atatat #define HASH_MD2	0
    109  1.13   atatat #define HASH_MD4	1
    110  1.13   atatat #define HASH_MD5	2
    111  1.13   atatat #define HASH_SHA1	3
    112  1.13   atatat #define HASH_RMD160	4
    113  1.13   atatat 
    114  1.13   atatat typedef char *(*_filefunc)(const char *, char *);
    115  1.13   atatat 
    116  1.13   atatat struct hash {
    117  1.13   atatat 	const char *progname;
    118  1.13   atatat 	const char *hashname;
    119  1.13   atatat 	void (*stringfunc)(const char *);
    120  1.13   atatat 	void (*timetrialfunc)(void);
    121  1.13   atatat 	void (*testsuitefunc)(void);
    122  1.13   atatat 	void (*filterfunc)(int);
    123  1.13   atatat 	char *(*filefunc)(const char *, char *);
    124  1.13   atatat } hashes[] = {
    125  1.13   atatat 	{ "md2", "MD2",
    126  1.13   atatat 	  MD2String, MD2TimeTrial, MD2TestSuite,
    127  1.13   atatat 	  MD2Filter, MD2File },
    128  1.13   atatat 	{ "md4", "MD4",
    129  1.13   atatat 	  MD4String, MD4TimeTrial, MD4TestSuite,
    130  1.13   atatat 	  MD4Filter, MD4File },
    131  1.13   atatat 	{ "md5", "MD5",
    132  1.13   atatat 	  MD5String, MD5TimeTrial, MD5TestSuite,
    133  1.13   atatat 	  MD5Filter, MD5File },
    134  1.13   atatat 	{ "sha1", "SHA1",
    135  1.13   atatat 	  SHA1String, SHA1TimeTrial, SHA1TestSuite,
    136  1.13   atatat 	  SHA1Filter, (_filefunc) SHA1File },
    137  1.13   atatat 	{ "rmd160", "RMD160",
    138  1.13   atatat 	  RMD160String, RMD160TimeTrial, RMD160TestSuite,
    139  1.13   atatat 	  RMD160Filter, (_filefunc) RMD160File },
    140  1.27     elad 	{ "sha256", "SHA256",
    141  1.27     elad 	  SHA256_String, SHA256_TimeTrial, SHA256_TestSuite,
    142  1.27     elad 	  SHA256_Filter, (_filefunc) SHA256_File },
    143  1.27     elad 	{ "sha384", "SHA384",
    144  1.27     elad 	  SHA384_String, SHA384_TimeTrial, SHA384_TestSuite,
    145  1.27     elad 	  SHA384_Filter, (_filefunc) SHA384_File },
    146  1.27     elad 	{ "sha512", "SHA512",
    147  1.27     elad 	  SHA512_String, SHA512_TimeTrial, SHA512_TestSuite,
    148  1.27     elad 	  SHA512_Filter, (_filefunc) SHA512_File },
    149  1.13   atatat 	{ NULL }
    150  1.13   atatat };
    151  1.13   atatat 
    152  1.23  xtraeme int	hash_digest_file(char *, struct hash *, int);
    153  1.23  xtraeme void	requirehash(const char *);
    154  1.23  xtraeme void	usage(void);
    155   1.3      cgd 
    156   1.3      cgd int
    157  1.22  xtraeme main(int argc, char **argv)
    158   1.1      cgd {
    159  1.24   simonb 	int ch, fd, rval, dosum, pflag, nohashstdin;
    160  1.19   kleink 	u_int32_t val;
    161  1.19   kleink 	off_t len;
    162   1.1      cgd 	char *fn;
    163  1.13   atatat 	const char *progname;
    164  1.22  xtraeme 	int (*cfncn) (int, u_int32_t *, off_t *);
    165  1.22  xtraeme 	void (*pfncn) (char *, u_int32_t, off_t);
    166  1.13   atatat 	struct hash *hash;
    167  1.35  hubertf 	int normal, i, check_warn, do_check;
    168   1.1      cgd 
    169  1.10    lukem 	cfncn = NULL;
    170  1.10    lukem 	pfncn = NULL;
    171  1.13   atatat 	dosum = pflag = nohashstdin = 0;
    172  1.17   atatat 	normal = 0;
    173  1.33  hubertf 	check_warn = 0;
    174  1.35  hubertf 	do_check = 0;
    175   1.9   kleink 
    176   1.9   kleink 	setlocale(LC_ALL, "");
    177   1.8  thorpej 
    178  1.13   atatat 	progname = getprogname();
    179  1.13   atatat 
    180  1.13   atatat 	for (hash = hashes; hash->hashname != NULL; hash++)
    181  1.13   atatat 		if (strcmp(progname, hash->progname) == 0)
    182  1.13   atatat 			break;
    183  1.13   atatat 
    184  1.13   atatat 	if (hash->hashname == NULL) {
    185  1.13   atatat 		hash = NULL;
    186  1.13   atatat 
    187  1.13   atatat 		if (!strcmp(progname, "sum")) {
    188  1.13   atatat 			dosum = 1;
    189  1.13   atatat 			cfncn = csum1;
    190  1.13   atatat 			pfncn = psum1;
    191  1.13   atatat 		} else {
    192  1.15   atatat 			cfncn = crc;
    193  1.13   atatat 			pfncn = pcrc;
    194  1.13   atatat 		}
    195   1.5  mycroft 	}
    196   1.5  mycroft 
    197  1.36     elad 	while ((ch = getopt(argc, argv, "a:cno:ps:twx")) != -1)
    198   1.1      cgd 		switch(ch) {
    199  1.25     elad 		case 'a':
    200  1.25     elad 			if (hash != NULL || dosum) {
    201  1.25     elad 				warnx("illegal use of -a option\n");
    202  1.25     elad 				usage();
    203  1.25     elad 			}
    204  1.25     elad 			i = 0;
    205  1.25     elad 			while (hashes[i].hashname != NULL) {
    206  1.25     elad 				if (!strcasecmp(hashes[i].hashname, optarg)) {
    207  1.25     elad 					hash = &hashes[i];
    208  1.25     elad 					break;
    209  1.25     elad 				}
    210  1.25     elad 				i++;
    211  1.25     elad 			}
    212  1.25     elad 			if (hash == NULL) {
    213  1.25     elad 				if (!strcasecmp(optarg, "old1")) {
    214  1.25     elad 					cfncn = csum1;
    215  1.25     elad 					pfncn = psum1;
    216  1.25     elad 				} else if (!strcasecmp(optarg, "old2")) {
    217  1.25     elad 					cfncn = csum2;
    218  1.25     elad 					pfncn = psum2;
    219  1.25     elad 				} else if (!strcasecmp(optarg, "crc")) {
    220  1.25     elad 					cfncn = crc;
    221  1.25     elad 					pfncn = pcrc;
    222  1.25     elad 				} else {
    223  1.25     elad 					warnx("illegal argument to -a option");
    224  1.25     elad 					usage();
    225  1.25     elad 				}
    226  1.25     elad 			}
    227  1.25     elad 			break;
    228  1.33  hubertf 		case 'c':
    229  1.35  hubertf 			do_check = 1;
    230  1.33  hubertf 			break;
    231  1.17   atatat 		case 'n':
    232  1.17   atatat 			normal = 1;
    233  1.17   atatat 			break;
    234   1.1      cgd 		case 'o':
    235  1.13   atatat 			if (hash) {
    236  1.13   atatat 				warnx("%s mutually exclusive with sum",
    237  1.13   atatat 				      hash->hashname);
    238   1.8  thorpej 				usage();
    239   1.8  thorpej 			}
    240   1.5  mycroft 			if (!strcmp(optarg, "1")) {
    241   1.1      cgd 				cfncn = csum1;
    242   1.1      cgd 				pfncn = psum1;
    243   1.5  mycroft 			} else if (!strcmp(optarg, "2")) {
    244   1.1      cgd 				cfncn = csum2;
    245   1.1      cgd 				pfncn = psum2;
    246   1.1      cgd 			} else {
    247   1.5  mycroft 				warnx("illegal argument to -o option");
    248   1.1      cgd 				usage();
    249   1.1      cgd 			}
    250   1.1      cgd 			break;
    251   1.8  thorpej 		case 'p':
    252  1.13   atatat 			if (hash == NULL)
    253  1.13   atatat 				requirehash("-p");
    254   1.8  thorpej 			pflag = 1;
    255   1.8  thorpej 			break;
    256   1.8  thorpej 		case 's':
    257  1.13   atatat 			if (hash == NULL)
    258  1.13   atatat 				requirehash("-s");
    259  1.13   atatat 			nohashstdin = 1;
    260  1.13   atatat 			hash->stringfunc(optarg);
    261   1.8  thorpej 			break;
    262   1.8  thorpej 		case 't':
    263  1.13   atatat 			if (hash == NULL)
    264  1.13   atatat 				requirehash("-t");
    265  1.13   atatat 			nohashstdin = 1;
    266  1.13   atatat 			hash->timetrialfunc();
    267   1.8  thorpej 			break;
    268  1.33  hubertf 		case 'w':
    269  1.33  hubertf 			check_warn = 1;
    270  1.33  hubertf 			break;
    271   1.8  thorpej 		case 'x':
    272  1.13   atatat 			if (hash == NULL)
    273  1.13   atatat 				requirehash("-x");
    274  1.13   atatat 			nohashstdin = 1;
    275  1.13   atatat 			hash->testsuitefunc();
    276   1.8  thorpej 			break;
    277   1.1      cgd 		case '?':
    278   1.1      cgd 		default:
    279   1.1      cgd 			usage();
    280   1.1      cgd 		}
    281   1.1      cgd 	argc -= optind;
    282   1.1      cgd 	argv += optind;
    283   1.1      cgd 
    284  1.35  hubertf 	if (do_check) {
    285  1.33  hubertf 		/*
    286  1.33  hubertf 		 * Verify checksums
    287  1.33  hubertf 		 */
    288  1.33  hubertf 		FILE *f;
    289  1.33  hubertf 		char buf[BUFSIZ];
    290  1.33  hubertf 		char *s, *p_filename, *p_cksum;
    291  1.33  hubertf 		int l_filename, l_cksum;
    292  1.33  hubertf 		char filename[BUFSIZ];
    293  1.33  hubertf 		char cksum[BUFSIZ];
    294  1.33  hubertf 		int ok,cnt,badcnt;
    295  1.33  hubertf 
    296  1.33  hubertf 		rval = 0;
    297  1.33  hubertf 		cnt = badcnt = 0;
    298  1.35  hubertf 
    299  1.35  hubertf 		if (argc == 0) {
    300  1.35  hubertf 			f = fdopen(STDIN_FILENO, "r");
    301  1.35  hubertf 		} else {
    302  1.35  hubertf 			f = fopen(argv[0], "r");
    303  1.35  hubertf 		}
    304  1.35  hubertf 		if (f == NULL)
    305  1.35  hubertf 			err(1, "Cannot read %s",
    306  1.35  hubertf 			    argc>0?argv[0]:"stdin");
    307  1.33  hubertf 
    308  1.33  hubertf 		while(fgets(buf, sizeof(buf), f) != NULL) {
    309  1.33  hubertf 			s=strrchr(buf, '\n');
    310  1.33  hubertf 			if (s)
    311  1.33  hubertf 				*s = '\0';
    312  1.33  hubertf 
    313  1.33  hubertf 			p_cksum = p_filename = NULL;
    314  1.33  hubertf 
    315  1.33  hubertf 			p_filename = strchr(buf, '(');
    316  1.33  hubertf 			if (p_filename) {
    317  1.33  hubertf 				/*
    318  1.33  hubertf 				 * Assume 'normal' output if there's a '('
    319  1.33  hubertf 				 */
    320  1.33  hubertf 				p_filename += 1;
    321  1.33  hubertf 				normal = 0;
    322  1.33  hubertf 
    323  1.33  hubertf 				p_cksum = strrchr(p_filename, ')');
    324  1.33  hubertf 				if (p_cksum == NULL) {
    325  1.33  hubertf 					if (check_warn)
    326  1.33  hubertf 						warnx("bogus format: %s. "
    327  1.33  hubertf 						      "Skipping...",
    328  1.33  hubertf 						      buf);
    329  1.33  hubertf 					rval = 1;
    330  1.33  hubertf 					continue;
    331  1.33  hubertf 				}
    332  1.33  hubertf  				p_cksum += 4;
    333  1.33  hubertf 
    334  1.33  hubertf 				l_cksum = strlen(p_cksum);
    335  1.33  hubertf 				l_filename = p_cksum - p_filename - 4;
    336  1.33  hubertf 
    337  1.34  hubertf 				/* Sanity check, and find proper hash if
    338  1.34  hubertf 				 * it's not the same as the current program
    339  1.34  hubertf 				 */
    340  1.34  hubertf 				if (hash == NULL ||
    341  1.34  hubertf 				    strncmp(buf, hash->hashname,
    342  1.33  hubertf 					    strlen(hash->hashname)) != 0) {
    343  1.34  hubertf 					/*
    344  1.34  hubertf 					 * Search proper hash
    345  1.34  hubertf 					 */
    346  1.34  hubertf 					struct hash *nhash;
    347  1.34  hubertf 
    348  1.34  hubertf 					for (nhash = hashes ;
    349  1.34  hubertf 					     nhash->hashname != NULL;
    350  1.34  hubertf 					     nhash++)
    351  1.34  hubertf 						if (strncmp(buf,
    352  1.34  hubertf 							    nhash->hashname,
    353  1.34  hubertf 							    strlen(nhash->hashname)) == 0)
    354  1.34  hubertf 							break;
    355  1.34  hubertf 
    356  1.34  hubertf 
    357  1.34  hubertf 					if (nhash->hashname == NULL) {
    358  1.34  hubertf 						if (check_warn)
    359  1.34  hubertf 							warnx("unknown hash: %s",
    360  1.34  hubertf 							      buf);
    361  1.34  hubertf 						rval = 1;
    362  1.34  hubertf 						continue;
    363  1.34  hubertf 					} else {
    364  1.34  hubertf 						hash = nhash;
    365  1.34  hubertf 					}
    366  1.33  hubertf 				}
    367  1.33  hubertf 
    368  1.33  hubertf 			} else {
    369  1.33  hubertf 				if (hash) {
    370  1.37  hubertf 					int nspaces;
    371  1.37  hubertf 
    372  1.33  hubertf 					/*
    373  1.33  hubertf 					 * 'normal' output, no (ck)sum
    374  1.33  hubertf 					 */
    375  1.33  hubertf 					normal = 1;
    376  1.37  hubertf 					nspaces = 1;
    377  1.33  hubertf 
    378  1.33  hubertf 					p_cksum = buf;
    379  1.33  hubertf 					p_filename = strchr(buf, ' ');
    380  1.33  hubertf 					if (p_filename == NULL) {
    381  1.33  hubertf 						if (check_warn)
    382  1.33  hubertf 							warnx("no filename in %s? "
    383  1.33  hubertf 							      "Skipping...", buf);
    384  1.33  hubertf 						rval = 1;
    385  1.33  hubertf 						continue;
    386  1.33  hubertf 					}
    387  1.37  hubertf 					while (isspace((int)*++p_filename))
    388  1.37  hubertf 						nspaces++;
    389  1.33  hubertf 					l_filename = strlen(p_filename);
    390  1.37  hubertf 					l_cksum = p_filename - buf - nspaces;
    391  1.33  hubertf 				} else {
    392  1.33  hubertf 					/*
    393  1.33  hubertf 					 * sum/cksum output format
    394  1.33  hubertf 					 */
    395  1.33  hubertf 					p_cksum = buf;
    396  1.33  hubertf 					s=strchr(p_cksum, ' ');
    397  1.33  hubertf 					if (s == NULL) {
    398  1.33  hubertf 						if (check_warn)
    399  1.33  hubertf 							warnx("bogus format: %s."
    400  1.33  hubertf 							      " Skipping...",
    401  1.33  hubertf 							      buf);
    402  1.33  hubertf 						rval = 1;
    403  1.33  hubertf 						continue;
    404  1.33  hubertf 					}
    405  1.33  hubertf 					l_cksum = s - p_cksum;
    406  1.33  hubertf 
    407  1.33  hubertf 					p_filename = strrchr(buf, ' ');
    408  1.33  hubertf 					if (p_filename == NULL) {
    409  1.33  hubertf 						if (check_warn)
    410  1.33  hubertf 							warnx("no filename in %s?"
    411  1.33  hubertf 							      " Skipping...",
    412  1.33  hubertf 							      buf);
    413  1.33  hubertf 						rval = 1;
    414  1.33  hubertf 						continue;
    415  1.33  hubertf 					}
    416  1.33  hubertf 					p_filename++;
    417  1.33  hubertf 					l_filename = strlen(p_filename);
    418  1.33  hubertf 				}
    419  1.33  hubertf 			}
    420  1.33  hubertf 
    421  1.33  hubertf 			strlcpy(filename, p_filename, l_filename+1);
    422  1.33  hubertf 			strlcpy(cksum, p_cksum, l_cksum+1);
    423  1.33  hubertf 
    424  1.33  hubertf 			if (hash) {
    425  1.33  hubertf 				if (access(filename, R_OK) == 0
    426  1.33  hubertf 				    && strcmp(cksum, hash->filefunc(filename, NULL)) == 0)
    427  1.33  hubertf 					ok = 1;
    428  1.33  hubertf 				else
    429  1.33  hubertf 					ok = 0;
    430  1.33  hubertf 			} else {
    431  1.33  hubertf 				if ((fd = open(filename, O_RDONLY, 0)) < 0) {
    432  1.33  hubertf 					if (check_warn)
    433  1.33  hubertf 						warn("%s", filename);
    434   1.8  thorpej 					rval = 1;
    435  1.33  hubertf 					ok = 0;
    436  1.33  hubertf 				} else {
    437  1.33  hubertf 					if (cfncn(fd, &val, &len))
    438  1.33  hubertf 						ok = 0;
    439  1.33  hubertf 					else {
    440  1.33  hubertf 						u_int32_t should_val;
    441  1.33  hubertf 
    442  1.33  hubertf 						should_val =
    443  1.33  hubertf 						  strtoul(cksum, NULL, 10);
    444  1.33  hubertf 						if (val == should_val)
    445  1.33  hubertf 							ok = 1;
    446  1.33  hubertf 						else
    447  1.33  hubertf 							ok = 0;
    448  1.33  hubertf 					}
    449   1.8  thorpej 				}
    450   1.8  thorpej 			}
    451  1.33  hubertf 
    452  1.34  hubertf 			if (! ok) {
    453  1.34  hubertf 				if (hash)
    454  1.34  hubertf 					printf("(%s) ", hash->hashname);
    455  1.33  hubertf 				printf("%s: FAILED\n", filename);
    456  1.33  hubertf 				badcnt++;
    457   1.1      cgd 			}
    458  1.33  hubertf 			cnt++;
    459  1.33  hubertf 
    460  1.13   atatat 		}
    461  1.33  hubertf 		fclose(f);
    462   1.8  thorpej 
    463  1.34  hubertf 		if (badcnt > 0)
    464  1.34  hubertf 			rval = 1;
    465  1.33  hubertf 
    466  1.33  hubertf 	} else {
    467  1.33  hubertf 		/*
    468  1.33  hubertf 		 * Calculate checksums
    469  1.33  hubertf 		 */
    470  1.33  hubertf 
    471  1.33  hubertf 		fd = STDIN_FILENO;
    472  1.33  hubertf 		fn = NULL;
    473  1.33  hubertf 		rval = 0;
    474  1.33  hubertf 		do {
    475  1.33  hubertf 			if (*argv) {
    476  1.33  hubertf 				fn = *argv++;
    477  1.33  hubertf 				if (hash != NULL) {
    478  1.33  hubertf 					if (hash_digest_file(fn, hash, normal)) {
    479  1.33  hubertf 						warn("%s", fn);
    480  1.33  hubertf 						rval = 1;
    481  1.33  hubertf 					}
    482  1.33  hubertf 					continue;
    483  1.33  hubertf 				}
    484  1.33  hubertf 				if ((fd = open(fn, O_RDONLY, 0)) < 0) {
    485  1.33  hubertf 					warn("%s", fn);
    486  1.33  hubertf 					rval = 1;
    487  1.33  hubertf 					continue;
    488  1.33  hubertf 				}
    489  1.33  hubertf 			} else if (hash && !nohashstdin) {
    490  1.33  hubertf 				hash->filterfunc(pflag);
    491  1.33  hubertf 			}
    492  1.33  hubertf 
    493  1.33  hubertf 			if (hash == NULL) {
    494  1.33  hubertf 				if (cfncn(fd, &val, &len)) {
    495  1.33  hubertf 					warn("%s", fn ? fn : "stdin");
    496  1.33  hubertf 					rval = 1;
    497  1.33  hubertf 				} else
    498  1.33  hubertf 					pfncn(fn, val, len);
    499  1.33  hubertf 				(void)close(fd);
    500  1.33  hubertf 			}
    501  1.33  hubertf 		} while (*argv);
    502  1.33  hubertf 	}
    503   1.1      cgd 	exit(rval);
    504   1.1      cgd }
    505   1.1      cgd 
    506   1.8  thorpej int
    507  1.22  xtraeme hash_digest_file(char *fn, struct hash *hash, int normal)
    508   1.8  thorpej {
    509  1.30     elad 	char *cp;
    510   1.8  thorpej 
    511  1.30     elad 	cp = hash->filefunc(fn, NULL);
    512   1.8  thorpej 	if (cp == NULL)
    513  1.22  xtraeme 		return 1;
    514   1.8  thorpej 
    515  1.17   atatat 	if (normal)
    516  1.17   atatat 		printf("%s %s\n", cp, fn);
    517  1.17   atatat 	else
    518  1.17   atatat 		printf("%s (%s) = %s\n", hash->hashname, fn, cp);
    519  1.30     elad 
    520  1.30     elad 	free(cp);
    521  1.30     elad 
    522  1.22  xtraeme 	return 0;
    523   1.8  thorpej }
    524   1.8  thorpej 
    525   1.8  thorpej void
    526  1.22  xtraeme requirehash(const char *flg)
    527   1.8  thorpej {
    528  1.13   atatat 	warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
    529  1.13   atatat 	    flg);
    530   1.8  thorpej 	usage();
    531   1.8  thorpej }
    532   1.8  thorpej 
    533   1.3      cgd void
    534  1.22  xtraeme usage(void)
    535   1.1      cgd {
    536   1.5  mycroft 
    537  1.36     elad 	(void)fprintf(stderr, "usage: cksum [-nw] [-a algorithm | -c file ]\n\t\t| [-o 1 | 2]] [file ...]\n");
    538  1.33  hubertf 	(void)fprintf(stderr, "       sum [-c] [file ...]\n");
    539   1.8  thorpej 	(void)fprintf(stderr,
    540  1.17   atatat 	    "       md2 [-n] [-p | -t | -x | -s string] [file ...]\n");
    541  1.13   atatat 	(void)fprintf(stderr,
    542  1.17   atatat 	    "       md4 [-n] [-p | -t | -x | -s string] [file ...]\n");
    543  1.13   atatat 	(void)fprintf(stderr,
    544  1.17   atatat 	    "       md5 [-n] [-p | -t | -x | -s string] [file ...]\n");
    545  1.13   atatat 	(void)fprintf(stderr,
    546  1.17   atatat 	    "       sha1 [-n] [-p | -t | -x | -s string] [file ...]\n");
    547  1.13   atatat 	(void)fprintf(stderr,
    548  1.17   atatat 	    "       rmd160 [-n] [-p | -t | -x | -s string] [file ...]\n");
    549   1.1      cgd 	exit(1);
    550   1.1      cgd }
    551