Last active
August 29, 2015 13:57
-
-
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のコードを調整したもの。よく忘れるので...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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