source: trunk/minix/lib/ip/oneC_sum.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 1.1 KB
Line 
1/* oneC_sum() - One complement's checksum Author: Kees J. Bot
2 * 8 May 1995
3 * See RFC 1071, "Computing the Internet checksum"
4 */
5
6#include <sys/types.h>
7#include <net/gen/oneCsum.h>
8
9u16_t oneC_sum(U16_t prev, void *data, size_t size)
10{
11 u8_t *dptr;
12 size_t n;
13 u16_t word;
14 u32_t sum;
15 int swap= 0;
16
17 sum= prev;
18 dptr= data;
19 n= size;
20
21 swap= ((size_t) dptr & 1);
22 if (swap) {
23 sum= ((sum & 0xFF) << 8) | ((sum & 0xFF00) >> 8);
24 if (n > 0) {
25 ((u8_t *) &word)[0]= 0;
26 ((u8_t *) &word)[1]= dptr[0];
27 sum+= (u32_t) word;
28 dptr+= 1;
29 n-= 1;
30 }
31 }
32
33 while (n >= 8) {
34 sum+= (u32_t) ((u16_t *) dptr)[0]
35 + (u32_t) ((u16_t *) dptr)[1]
36 + (u32_t) ((u16_t *) dptr)[2]
37 + (u32_t) ((u16_t *) dptr)[3];
38 dptr+= 8;
39 n-= 8;
40 }
41
42 while (n >= 2) {
43 sum+= (u32_t) ((u16_t *) dptr)[0];
44 dptr+= 2;
45 n-= 2;
46 }
47
48 if (n > 0) {
49 ((u8_t *) &word)[0]= dptr[0];
50 ((u8_t *) &word)[1]= 0;
51 sum+= (u32_t) word;
52 }
53
54 sum= (sum & 0xFFFF) + (sum >> 16);
55 if (sum > 0xFFFF) sum++;
56
57 if (swap) {
58 sum= ((sum & 0xFF) << 8) | ((sum & 0xFF00) >> 8);
59 }
60 return sum;
61}
Note: See TracBrowser for help on using the repository browser.