xref: /csrg-svn/usr.bin/cksum/sum2.c (revision 51818)
147798Sbostic /*-
247798Sbostic  * Copyright (c) 1991 The Regents of the University of California.
347798Sbostic  * All rights reserved.
447798Sbostic  *
547798Sbostic  * %sccs.include.redist.c%
647798Sbostic  */
747798Sbostic 
847798Sbostic #ifndef lint
9*51818Sbostic static char sccsid[] = "@(#)sum2.c	5.2 (Berkeley) 11/27/91";
1047798Sbostic #endif /* not lint */
1147798Sbostic 
1247798Sbostic #include <sys/types.h>
1347798Sbostic #include <unistd.h>
1447798Sbostic 
15*51818Sbostic int
1647798Sbostic csum2(fd, cval, clen)
1747798Sbostic 	register int fd;
1847798Sbostic 	u_long *cval, *clen;
1947798Sbostic {
2047798Sbostic 	register u_long crc, total;
2147798Sbostic 	register int nr;
2247798Sbostic 	register u_char *p;
2347798Sbostic 	u_char buf[8192];
2447798Sbostic 
2547798Sbostic 	/*
2647798Sbostic 	 * Draft 8 POSIX 1003.2:
2747798Sbostic 	 *
2847798Sbostic 	 *   s = sum of all bytes
2947798Sbostic 	 *   r = s % 2^16 + (s % 2^32) / 2^16
3047798Sbostic 	 * crc = (r % 2^16) + r / 2^16
3147798Sbostic 	 */
3247798Sbostic 	crc = total = 0;
3347798Sbostic 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
3447798Sbostic 		for (total += nr, p = buf; nr--; ++p)
3547798Sbostic 			crc += *p;
3647798Sbostic 	if (nr < 0)
3747798Sbostic 		return(1);
3847798Sbostic 
3947798Sbostic 	crc = (crc & 0xffff) + (crc >> 16);
4047798Sbostic 	crc = (crc & 0xffff) + (crc >> 16);
4147798Sbostic 
4247798Sbostic 	*cval = crc;
4347798Sbostic 	*clen = total;
4447798Sbostic 	return(0);
4547798Sbostic }
46