xref: /csrg-svn/usr.bin/cksum/sum1.c (revision 51818)
147797Sbostic /*-
247797Sbostic  * Copyright (c) 1991 The Regents of the University of California.
347797Sbostic  * All rights reserved.
447797Sbostic  *
547797Sbostic  * %sccs.include.redist.c%
647797Sbostic  */
747797Sbostic 
847797Sbostic #ifndef lint
9*51818Sbostic static char sccsid[] = "@(#)sum1.c	5.2 (Berkeley) 11/27/91";
1047797Sbostic #endif /* not lint */
1147797Sbostic 
1247797Sbostic #include <sys/types.h>
1347797Sbostic #include <unistd.h>
1447797Sbostic 
15*51818Sbostic int
1647797Sbostic csum1(fd, cval, clen)
1747797Sbostic 	register int fd;
1847797Sbostic 	u_long *cval, *clen;
1947797Sbostic {
2047797Sbostic 	register u_long total;
2147797Sbostic 	register int nr;
2247797Sbostic 	register u_int crc;
2347797Sbostic 	register u_char *p;
2447797Sbostic 	u_char buf[8192];
2547797Sbostic 
2647797Sbostic 	/*
2747797Sbostic 	 * 16-bit checksum, rotating right before each addition;
2847797Sbostic 	 * overflow is discarded.
2947797Sbostic 	 */
3047797Sbostic 	crc = total = 0;
3147797Sbostic 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
3247797Sbostic 		for (total += nr, p = buf; nr--; ++p) {
3347797Sbostic 			if (crc & 1)
3447797Sbostic 				crc |= 0x10000;
3547797Sbostic 			crc = ((crc >> 1) + *p) & 0xffff;
3647797Sbostic 		}
3747797Sbostic 	if (nr < 0)
3847797Sbostic 		return(1);
3947797Sbostic 
4047797Sbostic 	*cval = crc;
4147797Sbostic 	*clen = total;
4247797Sbostic 	return(0);
4347797Sbostic }
44