1*280d8c66SLionel Sambuc /* $NetBSD: sum2.c,v 1.13 2005/02/05 00:13:34 simonb Exp $ */
2*280d8c66SLionel Sambuc
3*280d8c66SLionel Sambuc /*-
4*280d8c66SLionel Sambuc * Copyright (c) 1991, 1993
5*280d8c66SLionel Sambuc * The Regents of the University of California. All rights reserved.
6*280d8c66SLionel Sambuc *
7*280d8c66SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*280d8c66SLionel Sambuc * modification, are permitted provided that the following conditions
9*280d8c66SLionel Sambuc * are met:
10*280d8c66SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*280d8c66SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*280d8c66SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*280d8c66SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*280d8c66SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*280d8c66SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
16*280d8c66SLionel Sambuc * may be used to endorse or promote products derived from this software
17*280d8c66SLionel Sambuc * without specific prior written permission.
18*280d8c66SLionel Sambuc *
19*280d8c66SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*280d8c66SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*280d8c66SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*280d8c66SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*280d8c66SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*280d8c66SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*280d8c66SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*280d8c66SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*280d8c66SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*280d8c66SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*280d8c66SLionel Sambuc * SUCH DAMAGE.
30*280d8c66SLionel Sambuc */
31*280d8c66SLionel Sambuc
32*280d8c66SLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
33*280d8c66SLionel Sambuc #include "nbtool_config.h"
34*280d8c66SLionel Sambuc #endif
35*280d8c66SLionel Sambuc
36*280d8c66SLionel Sambuc #include <sys/cdefs.h>
37*280d8c66SLionel Sambuc #if defined(__RCSID) && !defined(lint)
38*280d8c66SLionel Sambuc #if 0
39*280d8c66SLionel Sambuc static char sccsid[] = "@(#)sum2.c 8.1 (Berkeley) 6/6/93";
40*280d8c66SLionel Sambuc #else
41*280d8c66SLionel Sambuc __RCSID("$NetBSD: sum2.c,v 1.13 2005/02/05 00:13:34 simonb Exp $");
42*280d8c66SLionel Sambuc #endif
43*280d8c66SLionel Sambuc #endif /* not lint */
44*280d8c66SLionel Sambuc
45*280d8c66SLionel Sambuc #include <sys/types.h>
46*280d8c66SLionel Sambuc #include <unistd.h>
47*280d8c66SLionel Sambuc
48*280d8c66SLionel Sambuc #include "extern.h"
49*280d8c66SLionel Sambuc
50*280d8c66SLionel Sambuc int
csum2(int fd,u_int32_t * cval,off_t * clen)51*280d8c66SLionel Sambuc csum2(int fd, u_int32_t *cval, off_t *clen)
52*280d8c66SLionel Sambuc {
53*280d8c66SLionel Sambuc u_int32_t thecrc;
54*280d8c66SLionel Sambuc off_t total;
55*280d8c66SLionel Sambuc int nr;
56*280d8c66SLionel Sambuc u_char *p;
57*280d8c66SLionel Sambuc u_char buf[8192];
58*280d8c66SLionel Sambuc
59*280d8c66SLionel Sambuc /*
60*280d8c66SLionel Sambuc * Draft 8 POSIX 1003.2:
61*280d8c66SLionel Sambuc *
62*280d8c66SLionel Sambuc * s = sum of all bytes
63*280d8c66SLionel Sambuc * r = s % 2^16 + (s % 2^32) / 2^16
64*280d8c66SLionel Sambuc * thecrc = (r % 2^16) + r / 2^16
65*280d8c66SLionel Sambuc */
66*280d8c66SLionel Sambuc thecrc = 0;
67*280d8c66SLionel Sambuc total = 0;
68*280d8c66SLionel Sambuc while ((nr = read(fd, buf, sizeof(buf))) > 0)
69*280d8c66SLionel Sambuc for (total += nr, p = buf; nr--; ++p)
70*280d8c66SLionel Sambuc thecrc += *p;
71*280d8c66SLionel Sambuc if (nr < 0)
72*280d8c66SLionel Sambuc return 1;
73*280d8c66SLionel Sambuc
74*280d8c66SLionel Sambuc thecrc = (thecrc & 0xffff) + (thecrc >> 16);
75*280d8c66SLionel Sambuc thecrc = (thecrc & 0xffff) + (thecrc >> 16);
76*280d8c66SLionel Sambuc
77*280d8c66SLionel Sambuc *cval = thecrc;
78*280d8c66SLionel Sambuc *clen = total;
79*280d8c66SLionel Sambuc return 0;
80*280d8c66SLionel Sambuc }
81