1 /*$Id: checksum.c,v 1.1 2005/12/12 18:14:22 nobunaga Exp $
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 in_cksum(unsigned short *addr, int len)
23 unsigned short *w = addr;
24 unsigned short answer = 0;
27 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
28 * sequential 16 bit words to it, and at the end, fold back all the
29 * carry bits from the top 16 bits into the lower 16 bits.
36 /* mop up an odd byte, if necessary */
38 *(unsigned char *)(&answer) = *(unsigned char *)w ;
42 /* add back carry outs from top 16 bits to low 16 bits */
43 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
44 sum += (sum >> 16); /* add carry */
45 answer = ~sum; /* truncate to 16 bits */