1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)cksum.m68000.c	2.2 (Berkeley) 12/23/87";
15 #endif /* not lint */
16 
17 #include "../globals.h"
18 #include <protocols/timed.h>
19 
20 /* computes the checksum for ip packets for a Motorola 68000 base computer */
21 
22 in_cksum(w, mlen)
23 	register u_short *w;
24 	register int mlen;
25 {
26 	register int sum = 0;
27 
28 	if (mlen > 0) {
29 		if (((int)w & 1) == 0) {
30 			sum = ocsum(w, mlen>>1);
31 			w += mlen>>1;
32 			if (mlen & 1) {
33 				sum += *(u_char *)w << 8;
34 				mlen = -1;
35 			}
36 		} else {
37 			u_short swsum;
38 
39 			sum = *(u_char *)w << 8;
40 			mlen--;
41 			w = (u_short *)(1 + (int)w);
42 			swsum = ocsum(w, mlen>>1);
43 			swab((char *)&swsum, (char *)&swsum, sizeof swsum);
44 			sum += swsum;
45 			w += mlen>>1;
46 			if (mlen & 1)
47 				sum += *(u_char *)w;
48 		}
49 	}
50 	sum = (sum & 0xFFFF) + (sum >> 16);
51 	sum = (sum & 0xFFFF) + (sum >> 16);
52 	sum = (~sum) & 0xFFFF;
53 	return (sum);
54 }
55