2 * Checksum routine for Internet Protocol family headers (C Version).
4 * Refer to "Computing the Internet Checksum" by R. Braden, D. Borman and
5 * C. Partridge, Computer Communication Review, Vol. 19, No. 2, April 1989,
6 * pp. 86-101, for additional details on computing this checksum.
12 int /* return checksum in low-order 16 bits */
13 in_cksum(void *parg
, int nbytes
)
16 register long sum
; /* assumes long == 32 bits */
18 register u_short answer
; /* assumes u_short == 16 bits */
21 * Our algorithm is simple, using a 32-bit accumulator (sum),
22 * we add sequential 16-bit words to it, and at the end, fold back
23 * all the carry bits from the top 16 bits into the lower 16 bits.
32 /* mop up an odd byte, if necessary */
34 oddbyte
= 0; /* make sure top half is zero */
35 *((u_char
*) &oddbyte
) = *(u_char
*)ptr
; /* one byte only */
40 * Add back carry outs from top 16 bits to low 16 bits.
43 sum
= (sum
>> 16) + (sum
& 0xffff); /* add high-16 to low-16 */
44 sum
+= (sum
>> 16); /* add carry */
45 answer
= ~sum
; /* ones-complement, then truncate to 16 bits */