Commit | Line | Data |
---|---|---|
d34e2139 SL |
1 | /* |
2 | * Copyright (c) 1986 Regents of the University of California. | |
26b5a830 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b8c620d6 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
d34e2139 SL |
16 | */ |
17 | ||
18 | #ifndef lint | |
ca67e7b4 | 19 | static char sccsid[] = "@(#)cksum.tahoe.c 2.3 (Berkeley) 6/18/88"; |
26b5a830 | 20 | #endif /* not lint */ |
d34e2139 SL |
21 | |
22 | #include <sys/types.h> | |
23 | ||
24 | /* | |
25 | * Checksum routine for Internet Protocol family headers. | |
26 | * | |
27 | * This routine is very heavily used in the network | |
28 | * code and should be modified for each CPU to be as fast as possible. | |
29 | * | |
30 | * This implementation is TAHOE version. | |
31 | */ | |
32 | ||
33 | #undef ADDCARRY | |
34 | #define ADDCARRY(sum) { \ | |
35 | if (sum & 0xffff0000) { \ | |
36 | sum &= 0xffff; \ | |
37 | sum++; \ | |
38 | } \ | |
39 | } | |
40 | ||
41 | in_cksum(addr, len) | |
42 | register u_short *addr; | |
43 | register int len; | |
44 | { | |
45 | union word { | |
46 | char c[2]; | |
47 | u_short s; | |
48 | } u; | |
49 | register int sum = 0; | |
50 | ||
51 | while (len > 0) { | |
52 | /* | |
53 | * add by words. | |
54 | */ | |
55 | while ((len -= 2) >= 0) { | |
56 | if ((int)addr & 0x1) { | |
57 | /* word is not aligned */ | |
58 | u.c[0] = *(char *)addr; | |
59 | u.c[1] = *((char *)addr+1); | |
60 | sum += u.s; | |
61 | addr++; | |
62 | } else | |
63 | sum += *addr++; | |
64 | ADDCARRY(sum); | |
65 | } | |
66 | if (len == -1) | |
67 | /* | |
68 | * Odd number of bytes. | |
69 | */ | |
70 | u.c[0] = *(u_char *)addr; | |
71 | } | |
72 | if (len == -1) { | |
73 | /* The last mbuf has odd # of bytes. Follow the | |
74 | standard (the odd byte is shifted left by 8 bits) */ | |
75 | u.c[1] = 0; | |
76 | sum += u.s; | |
77 | ADDCARRY(sum); | |
78 | } | |
79 | return (~sum & 0xffff); | |
80 | } |