sum2.c revision 1590
1202719Sgabor/*- 2265533Sdelphij * Copyright (c) 1991, 1993 3202719Sgabor * The Regents of the University of California. All rights reserved. 4202719Sgabor * 5202719Sgabor * Redistribution and use in source and binary forms, with or without 6202719Sgabor * modification, are permitted provided that the following conditions 7202719Sgabor * are met: 8202719Sgabor * 1. Redistributions of source code must retain the above copyright 9202719Sgabor * notice, this list of conditions and the following disclaimer. 10202719Sgabor * 2. Redistributions in binary form must reproduce the above copyright 11202719Sgabor * notice, this list of conditions and the following disclaimer in the 12202719Sgabor * documentation and/or other materials provided with the distribution. 13202719Sgabor * 3. All advertising materials mentioning features or use of this software 14202719Sgabor * must display the following acknowledgement: 15202719Sgabor * This product includes software developed by the University of 16202719Sgabor * California, Berkeley and its contributors. 17202719Sgabor * 4. Neither the name of the University nor the names of its contributors 18202719Sgabor * may be used to endorse or promote products derived from this software 19202719Sgabor * without specific prior written permission. 20202719Sgabor * 21202719Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22202719Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23202719Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24202719Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25202719Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26202719Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27202719Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28202719Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29202719Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30202719Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31202719Sgabor * SUCH DAMAGE. 32202719Sgabor */ 33202719Sgabor 34202719Sgabor#ifndef lint 35202719Sgaborstatic char sccsid[] = "@(#)sum2.c 8.1 (Berkeley) 6/6/93"; 36202719Sgabor#endif /* not lint */ 37202719Sgabor 38265533Sdelphij#include <sys/types.h> 39202719Sgabor#include <unistd.h> 40202719Sgabor 41202719Sgaborint 42202719Sgaborcsum2(fd, cval, clen) 43202719Sgabor register int fd; 44202719Sgabor u_long *cval, *clen; 45202719Sgabor{ 46202719Sgabor register u_long crc, total; 47202719Sgabor register int nr; 48202719Sgabor register u_char *p; 49202719Sgabor u_char buf[8192]; 50202719Sgabor 51202719Sgabor /* 52202719Sgabor * Draft 8 POSIX 1003.2: 53202719Sgabor * 54202719Sgabor * s = sum of all bytes 55202719Sgabor * r = s % 2^16 + (s % 2^32) / 2^16 56202719Sgabor * crc = (r % 2^16) + r / 2^16 57202719Sgabor */ 58202719Sgabor crc = total = 0; 59202719Sgabor while ((nr = read(fd, buf, sizeof(buf))) > 0) 60202719Sgabor for (total += nr, p = buf; nr--; ++p) 61202719Sgabor crc += *p; 62202719Sgabor if (nr < 0) 63202719Sgabor return(1); 64202719Sgabor 65202719Sgabor crc = (crc & 0xffff) + (crc >> 16); 66202719Sgabor crc = (crc & 0xffff) + (crc >> 16); 67202719Sgabor 68202719Sgabor *cval = crc; 69202719Sgabor *clen = total; 70202847Sdelphij return(0); 71202719Sgabor} 72202719Sgabor