sum2.c revision 200462
190380Smsmith/*- 290380Smsmith * Copyright (c) 1991, 1993 390380Smsmith * The Regents of the University of California. All rights reserved. 490380Smsmith * 590380Smsmith * Redistribution and use in source and binary forms, with or without 690380Smsmith * modification, are permitted provided that the following conditions 798146Siwasaki * are met: 898146Siwasaki * 1. Redistributions of source code must retain the above copyright 998146Siwasaki * notice, this list of conditions and the following disclaimer. 1098146Siwasaki * 2. Redistributions in binary form must reproduce the above copyright 1198146Siwasaki * notice, this list of conditions and the following disclaimer in the 1290380Smsmith * documentation and/or other materials provided with the distribution. 13235945Sjkim * 3. All advertising materials mentioning features or use of this software 14235945Sjkim * must display the following acknowledgement: 1590380Smsmith * This product includes software developed by the University of 16123333Snjl * California, Berkeley and its contributors. 17231844Sjkim * 4. Neither the name of the University nor the names of its contributors 18193529Sjkim * may be used to endorse or promote products derived from this software 1990380Smsmith * without specific prior written permission. 20231844Sjkim * 21239340Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22250838Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23252279Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24252279Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25193529Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26151946Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27239340Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28239340Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29239340Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30239340Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31239340Sjkim * SUCH DAMAGE. 32239340Sjkim */ 33239340Sjkim 34239340Sjkim#ifndef lint 35213806Sjkim#if 0 36234623Sjkimstatic char sccsid[] = "@(#)sum2.c 8.1 (Berkeley) 6/6/93"; 37193529Sjkim#endif 3890380Smsmith#endif /* not lint */ 3990380Smsmith#include <sys/cdefs.h> 4090380Smsmith__FBSDID("$FreeBSD: head/usr.bin/cksum/sum2.c 200462 2009-12-13 03:14:06Z delphij $"); 41193529Sjkim 4290380Smsmith#include <sys/types.h> 4390380Smsmith 4490380Smsmith#include <unistd.h> 4590380Smsmith#include <stdint.h> 4690380Smsmith 4790380Smsmith#include "extern.h" 4890380Smsmith 4990380Smsmithint 5090380Smsmithcsum2(int fd, uint32_t *cval, off_t *clen) 5190380Smsmith{ 52209746Sjkim uint32_t lcrc; 5390380Smsmith int nr; 5490380Smsmith off_t total; 55151946Sjkim u_char *p; 5690380Smsmith u_char buf[8192]; 5790380Smsmith 58193529Sjkim /* 59123333Snjl * Draft 8 POSIX 1003.2: 60123333Snjl * 61209746Sjkim * s = sum of all bytes 62123333Snjl * r = s % 2^16 + (s % 2^32) / 2^16 63193529Sjkim * lcrc = (r % 2^16) + r / 2^16 64209746Sjkim */ 6590380Smsmith lcrc = total = 0; 66151604Sobrien while ((nr = read(fd, buf, sizeof(buf))) > 0) 67151946Sjkim for (total += nr, p = buf; nr--; ++p) 68220663Sjkim lcrc += *p; 69193529Sjkim if (nr < 0) 70151946Sjkim return (1); 71151946Sjkim 72235945Sjkim lcrc = (lcrc & 0xffff) + (lcrc >> 16); 73235945Sjkim lcrc = (lcrc & 0xffff) + (lcrc >> 16); 74151946Sjkim 75151946Sjkim *cval = lcrc; 76193529Sjkim *clen = total; 77209746Sjkim return (0); 78193529Sjkim} 79193529Sjkim