sum2.c revision 112212
157429Smarkm/*- 260573Skris * Copyright (c) 1991, 1993 357429Smarkm * The Regents of the University of California. All rights reserved. 460573Skris * 557429Smarkm * Redistribution and use in source and binary forms, with or without 657429Smarkm * modification, are permitted provided that the following conditions 760573Skris * are met: 857429Smarkm * 1. Redistributions of source code must retain the above copyright 957429Smarkm * notice, this list of conditions and the following disclaimer. 1060573Skris * 2. Redistributions in binary form must reproduce the above copyright 1157429Smarkm * notice, this list of conditions and the following disclaimer in the 1260573Skris * documentation and/or other materials provided with the distribution. 1357429Smarkm * 3. All advertising materials mentioning features or use of this software 1457429Smarkm * must display the following acknowledgement: 1560573Skris * This product includes software developed by the University of 1657429Smarkm * California, Berkeley and its contributors. 1757429Smarkm * 4. Neither the name of the University nor the names of its contributors 1857429Smarkm * may be used to endorse or promote products derived from this software 1957429Smarkm * without specific prior written permission. 2057429Smarkm * 2157429Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2257429Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2357429Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2457429Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2557429Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2657429Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2757429Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2857429Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2957429Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3057429Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3157429Smarkm * SUCH DAMAGE. 3257429Smarkm */ 3357429Smarkm 3457429Smarkm#ifndef lint 3557429Smarkm#if 0 3657429Smarkmstatic char sccsid[] = "@(#)sum2.c 8.1 (Berkeley) 6/6/93"; 3757429Smarkm#endif 3857429Smarkm#endif /* not lint */ 3957429Smarkm#include <sys/cdefs.h> 4057429Smarkm__FBSDID("$FreeBSD: head/usr.bin/cksum/sum2.c 112212 2003-03-13 23:32:28Z robert $"); 4157429Smarkm 4257429Smarkm#include <sys/types.h> 4357429Smarkm 4457429Smarkm#include <unistd.h> 4557429Smarkm#include <stdint.h> 4657429Smarkm 4757429Smarkm#include "extern.h" 4857429Smarkm 4957429Smarkmint 5057429Smarkmcsum2(int fd, uint32_t *cval, off_t *clen) 5157429Smarkm{ 5257429Smarkm uint32_t lcrc; 5357429Smarkm int nr; 5457429Smarkm off_t total; 5557429Smarkm u_char *p; 5657429Smarkm u_char buf[8192]; 5757429Smarkm 5857429Smarkm /* 5957429Smarkm * Draft 8 POSIX 1003.2: 6057429Smarkm * 6157429Smarkm * s = sum of all bytes 6257429Smarkm * r = s % 2^16 + (s % 2^32) / 2^16 6357429Smarkm * lcrc = (r % 2^16) + r / 2^16 6457429Smarkm */ 6557429Smarkm lcrc = total = 0; 6657429Smarkm while ((nr = read(fd, buf, sizeof(buf))) > 0) 6757429Smarkm for (total += nr, p = buf; nr--; ++p) 6857429Smarkm lcrc += *p; 6957429Smarkm if (nr < 0) 7057429Smarkm return (1); 7157429Smarkm 7257429Smarkm lcrc = (lcrc & 0xffff) + (lcrc >> 16); 7357429Smarkm lcrc = (lcrc & 0xffff) + (lcrc >> 16); 7457429Smarkm 7557429Smarkm *cval = lcrc; 7657429Smarkm *clen = total; 7757429Smarkm return (0); 7857429Smarkm} 7957429Smarkm