首页 > 代码库 > 校验和函数

校验和函数

static int icmp_cksum(unsigned char *data, int len)

{
int nleft = len;
uint16_t *data_tmp = (uint16_t *)data;
int sum = 0;
uint16_t tmp = 0;


while(nleft > 1)
{
sum += *data_tmp++;
nleft -= 2;
}


if(nleft == 1)
{
*(uint8_t *)(&tmp) = *(uint8_t *)data_tmp;
sum += tmp;
}


sum = (sum >> 16) + (sum &0xffff);
sum += (sum >> 16);


tmp = ~sum;


return tmp;
}

校验和函数