[lib] remove autogenerated file, update .cvsignore
[jleu-quagga.git] / lib / checksum.c
blob201da59fe26e746fd3552e5897f1de72c8b4a8b5
1 /*
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.
7 */
9 #include <zebra.h>
10 #include "checksum.h"
12 int /* return checksum in low-order 16 bits */
13 in_cksum(void *parg, int nbytes)
15 u_short *ptr = parg;
16 register long sum; /* assumes long == 32 bits */
17 u_short oddbyte;
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.
26 sum = 0;
27 while (nbytes > 1) {
28 sum += *ptr++;
29 nbytes -= 2;
32 /* mop up an odd byte, if necessary */
33 if (nbytes == 1) {
34 oddbyte = 0; /* make sure top half is zero */
35 *((u_char *) &oddbyte) = *(u_char *)ptr; /* one byte only */
36 sum += oddbyte;
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 */
46 return(answer);