Commit | Line | Data |
---|---|---|
6fab43e6 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[] = "@(#)sum1.c 5.2 (Berkeley) %G%"; |
6fab43e6 KB |
10 | #endif /* not lint */ |
11 | ||
12 | #include <sys/types.h> | |
13 | #include <unistd.h> | |
14 | ||
31785468 | 15 | int |
6fab43e6 KB |
16 | csum1(fd, cval, clen) |
17 | register int fd; | |
18 | u_long *cval, *clen; | |
19 | { | |
20 | register u_long total; | |
21 | register int nr; | |
22 | register u_int crc; | |
23 | register u_char *p; | |
24 | u_char buf[8192]; | |
25 | ||
26 | /* | |
27 | * 16-bit checksum, rotating right before each addition; | |
28 | * overflow is discarded. | |
29 | */ | |
30 | crc = total = 0; | |
31 | while ((nr = read(fd, buf, sizeof(buf))) > 0) | |
32 | for (total += nr, p = buf; nr--; ++p) { | |
33 | if (crc & 1) | |
34 | crc |= 0x10000; | |
35 | crc = ((crc >> 1) + *p) & 0xffff; | |
36 | } | |
37 | if (nr < 0) | |
38 | return(1); | |
39 | ||
40 | *cval = crc; | |
41 | *clen = total; | |
42 | return(0); | |
43 | } |