diff options
author | Alex Xu (Hello71) <alex_y_xu@yahoo.ca> | 2016-07-02 19:47:51 -0400 |
---|---|---|
committer | Alex Xu (Hello71) <alex_y_xu@yahoo.ca> | 2016-07-02 19:48:24 -0400 |
commit | 86799647b1afbaa0df36718b216efd876ffd2627 (patch) | |
tree | f791bf32bba53754a2027664ea6c5a2db077a37a /src/checksum.h | |
parent | 61e60c05097d7def8643a5a66bb6bc26229494a7 (diff) | |
download | udpastcp-86799647b1afbaa0df36718b216efd876ffd2627.tar.xz udpastcp-86799647b1afbaa0df36718b216efd876ffd2627.zip |
Add start of checksumming.
Diffstat (limited to 'src/checksum.h')
-rw-r--r-- | src/checksum.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/checksum.h b/src/checksum.h new file mode 100644 index 0000000..d9e6bda --- /dev/null +++ b/src/checksum.h @@ -0,0 +1,27 @@ +#include <stdint.h> + +static inline uint16_t do_csum(const char *buf, unsigned size) { + unsigned int sum = 0; + unsigned int i; + + for (i = 0; i < size - 1; i += 2) + sum += *(uint16_t *)&buf[i]; + + if (size & 1) + sum += (uint8_t)buf[i]; + + while (sum >> 16) + sum = (sum & 0xFFFF) + (sum >> 16); + + return ~sum; +} + +static inline uint16_t csum_partial(const void *buff, int len, uint16_t wsum) { + unsigned int sum = (unsigned int)wsum; + unsigned int result = do_csum(buff, len); + + result += sum; + if (sum > result) + result += 1; + return result; +} |