Commit | Line | Data |
---|---|---|
ef0d1ed0 KB |
1 | /*- |
2 | * Copyright (c) 1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * %sccs.include.redist.c% | |
6 | */ | |
7 | ||
8 | #ifndef lint | |
31785468 | 9 | static char sccsid[] = "@(#)sum2.c 5.2 (Berkeley) %G%"; |
ef0d1ed0 KB |
10 | #endif /* not lint */ |
11 | ||
12 | #include <sys/types.h> | |
13 | #include <unistd.h> | |
14 | ||
31785468 | 15 | int |
ef0d1ed0 KB |
16 | csum2(fd, cval, clen) |
17 | register int fd; | |
18 | u_long *cval, *clen; | |
19 | { | |
20 | register u_long crc, total; | |
21 | register int nr; | |
22 | register u_char *p; | |
23 | u_char buf[8192]; | |
24 | ||
25 | /* | |
26 | * Draft 8 POSIX 1003.2: | |
27 | * | |
28 | * s = sum of all bytes | |
29 | * r = s % 2^16 + (s % 2^32) / 2^16 | |
30 | * crc = (r % 2^16) + r / 2^16 | |
31 | */ | |
32 | crc = total = 0; | |
33 | while ((nr = read(fd, buf, sizeof(buf))) > 0) | |
34 | for (total += nr, p = buf; nr--; ++p) | |
35 | crc += *p; | |
36 | if (nr < 0) | |
37 | return(1); | |
38 | ||
39 | crc = (crc & 0xffff) + (crc >> 16); | |
40 | crc = (crc & 0xffff) + (crc >> 16); | |
41 | ||
42 | *cval = crc; | |
43 | *clen = total; | |
44 | return(0); | |
45 | } |