release.sh: restore -jJAILDIR option
[minix.git] / lib / libminlib / oneC_sum.c
blob702b9d35baa903b6cf3d4a24cc22d5f081bef1ac
1 /* oneC_sum() - One complement's checksum Author: Kees J. Bot
2 * 8 May 1995
3 * See RFC 1071, "Computing the Internet checksum"
4 */
6 #include <sys/types.h>
7 #include <net/gen/oneCsum.h>
9 u16_t oneC_sum(u16_t prev, void *data, size_t size)
11 u8_t *dptr;
12 size_t n;
13 u16_t word;
14 u32_t sum;
15 int swap= 0;
17 sum= prev;
18 dptr= data;
19 n= size;
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;
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;
42 while (n >= 2) {
43 sum+= (u32_t) ((u16_t *) dptr)[0];
44 dptr+= 2;
45 n-= 2;
48 if (n > 0) {
49 ((u8_t *) &word)[0]= dptr[0];
50 ((u8_t *) &word)[1]= 0;
51 sum+= (u32_t) word;
54 sum= (sum & 0xFFFF) + (sum >> 16);
55 if (sum > 0xFFFF) sum++;
57 if (swap) {
58 sum= ((sum & 0xFF) << 8) | ((sum & 0xFF00) >> 8);
60 return sum;