home *** CD-ROM | disk | FTP | other *** search
- /*
- @ C H E C K S U M . C
- @
- @ Checksum routine for Internet Protocol family headers (C Version)
- @
- */
- checksum(buf, len)
- register unsigned short* buf;
- register int len;
- {
- register int sum = 0;
-
- /*
- * Our algorithm is simple, using a 32 bit accumulator (sum),
- * we add sequential 16 bit words to it, and at the end, fold
- * back all the carry bits from the top 16 bits into the lower
- * 16 bits.
- */
- while (len > 1) {
- sum += *buf++;
- len -= 2;
- }
-
- /* mop up an odd byte, if necessary */
- if(len == 1)
- sum += *(unsigned char *)buf;
-
- /*
- * add back carry outs from top 16 bits to low 16 bits
- */
- sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
- sum += (sum >> 16); /* add carry */
- return ~sum; /* truncate to 16 bits */
- }
-
-