|
发表于 2005-2-21 16:49:16
|
显示全部楼层
RFC
IPv4 CRC-sum:
unsigned short int check_sum(unsigned char* addr, size_t count)
{
unsigned short checksum = 0;
/* Compute Internet Checksum for "count" bytes
* beginning at location "addr".
*/
register long sum = 0;
while( count > 1 ) {
/* This is the inner loop */
sum += * (unsigned short*) addr++;
count -= 2;
}
/* Add left-over byte, if any */
if( count > 0 )
sum += * (unsigned char *) addr;
/* Fold 32-bit sum to 16 bits */
while (sum>>16)
sum = (sum & 0xffff) + (sum >> 16);
checksum = ~sum;
return checksum;
} |
|