1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)sum1.c 8.1 (Berkeley) 06/06/93"; 10 #endif /* not lint */ 11 12 #include <sys/types.h> 13 #include <unistd.h> 14 15 int 16 csum1(fd, cval, clen) 17 register int fd; 18 u_long *cval, *clen; 19 { 20 register u_long total; 21 register int nr; 22 register u_int crc; 23 register u_char *p; 24 u_char buf[8192]; 25 26 /* 27 * 16-bit checksum, rotating right before each addition; 28 * overflow is discarded. 29 */ 30 crc = total = 0; 31 while ((nr = read(fd, buf, sizeof(buf))) > 0) 32 for (total += nr, p = buf; nr--; ++p) { 33 if (crc & 1) 34 crc |= 0x10000; 35 crc = ((crc >> 1) + *p) & 0xffff; 36 } 37 if (nr < 0) 38 return(1); 39 40 *cval = crc; 41 *clen = total; 42 return(0); 43 } 44