147797Sbostic /*-
2*61940Sbostic * Copyright (c) 1991, 1993
3*61940Sbostic * The Regents of the University of California. All rights reserved.
447797Sbostic *
547797Sbostic * %sccs.include.redist.c%
647797Sbostic */
747797Sbostic
847797Sbostic #ifndef lint
9*61940Sbostic static char sccsid[] = "@(#)sum1.c 8.1 (Berkeley) 06/06/93";
1047797Sbostic #endif /* not lint */
1147797Sbostic
1247797Sbostic #include <sys/types.h>
1347797Sbostic #include <unistd.h>
1447797Sbostic
1551818Sbostic int
csum1(fd,cval,clen)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