sum2.c revision 1590
1220137Strasz/*- 2220137Strasz * Copyright (c) 1991, 1993 3220137Strasz * The Regents of the University of California. All rights reserved. 4220137Strasz * 5220137Strasz * Redistribution and use in source and binary forms, with or without 6220137Strasz * modification, are permitted provided that the following conditions 7220137Strasz * are met: 8220137Strasz * 1. Redistributions of source code must retain the above copyright 9220137Strasz * notice, this list of conditions and the following disclaimer. 10220137Strasz * 2. Redistributions in binary form must reproduce the above copyright 11220137Strasz * notice, this list of conditions and the following disclaimer in the 12220137Strasz * documentation and/or other materials provided with the distribution. 13220137Strasz * 3. All advertising materials mentioning features or use of this software 14220137Strasz * must display the following acknowledgement: 15220137Strasz * This product includes software developed by the University of 16220137Strasz * California, Berkeley and its contributors. 17220137Strasz * 4. Neither the name of the University nor the names of its contributors 18220137Strasz * may be used to endorse or promote products derived from this software 19220137Strasz * without specific prior written permission. 20220137Strasz * 21220137Strasz * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22220137Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23220137Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24220137Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25220137Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26220137Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27220137Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28220137Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29220137Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30220137Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31220137Strasz * SUCH DAMAGE. 32220137Strasz */ 33220137Strasz 34220137Strasz#ifndef lint 35220137Straszstatic char sccsid[] = "@(#)sum2.c 8.1 (Berkeley) 6/6/93"; 36242139Strasz#endif /* not lint */ 37220137Strasz 38220137Strasz#include <sys/types.h> 39228430Savg#include <unistd.h> 40220137Strasz 41220137Straszint 42220137Straszcsum2(fd, cval, clen) 43220137Strasz register int fd; 44220137Strasz u_long *cval, *clen; 45220137Strasz{ 46220137Strasz register u_long crc, total; 47220137Strasz register int nr; 48220137Strasz register u_char *p; 49220137Strasz u_char buf[8192]; 50220137Strasz 51220137Strasz /* 52220137Strasz * Draft 8 POSIX 1003.2: 53220137Strasz * 54242139Strasz * s = sum of all bytes 55220137Strasz * r = s % 2^16 + (s % 2^32) / 2^16 56242139Strasz * crc = (r % 2^16) + r / 2^16 57220137Strasz */ 58220137Strasz crc = total = 0; 59220137Strasz while ((nr = read(fd, buf, sizeof(buf))) > 0) 60242139Strasz for (total += nr, p = buf; nr--; ++p) 61220137Strasz crc += *p; 62220137Strasz if (nr < 0) 63220137Strasz return(1); 64220137Strasz 65220137Strasz crc = (crc & 0xffff) + (crc >> 16); 66220137Strasz crc = (crc & 0xffff) + (crc >> 16); 67220137Strasz 68220137Strasz *cval = crc; 69220137Strasz *clen = total; 70242139Strasz return(0); 71242139Strasz} 72242139Strasz