Skip to content

Instantly share code, notes, and snippets.

@tjs-w
Created February 22, 2016 20:17
Show Gist options
  • Save tjs-w/98e0bdb28053c79ba812 to your computer and use it in GitHub Desktop.
Save tjs-w/98e0bdb28053c79ba812 to your computer and use it in GitHub Desktop.
TCP Checksum
uint32_t __core_csum_tcp(uint16_t *addr, uint32_t count)
{
register uint32_t sum = 0;
while (count > 1) {
sum += *addr++;
count -= 2;
}
/* Add left-over byte, if any, (zero padding) */
if (count)
sum += *(uint8_t *) addr;
/* Fold 32-bit sum to 16 bits */
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment