unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--) // add words(16bits) together
{
sum += *buf++;
}
sum = (sum >> 16) + (sum & 0xffff); //add carry over
sum += (sum >> 16); //MY question: what exactly does this step do??? add possible left-over
//byte? But hasn't it already been added in the loop (if
//any)?
return ((unsigned short) ~sum);
}
谢谢!
答案 0 :(得分:0)
你是对的。步骤3将总和(32位长)压缩为16位无符号短路,这是校验和的长度。这是出于性能目的,允许人们在没有跟踪溢出直到结束时计算校验和。它在步骤2和步骤3都执行此操作,因为它可能已从第2步溢出。然后它只返回反转的低16位和。