Skip to content

Instantly share code, notes, and snippets.

@tsuyoshicho
Last active August 29, 2015 13:57
Show Gist options
  • Save tsuyoshicho/9362129 to your computer and use it in GitHub Desktop.
Save tsuyoshicho/9362129 to your computer and use it in GitHub Desktop.
Geekなページさんから ( http://www.geekpage.jp/programming/linux-network/book/12/12-5.php ) checksumのコードを調整したもの。よく忘れるので...
/*
* 16bit Checksum
* Endianness?
*/
uint16_t
checksum(uint16_t *buf, size_t bufsz)
{
uint_least32_t sum = 0;
while (bufsz > 1) {
sum += *buf;
buf++;
bufsz -= 2;
}
if (bufsz == 1) {
sum += *(uint8_t *)buf;
}
sum = (sum & 0xffff) + (sum >> 16);
sum = (sum & 0xffff) + (sum >> 16);
return ~(uint16_t)(sum & 0xffff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment