embkernel
 All Classes Functions Variables Typedefs Groups Pages
NetChecksum.cpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #include "NetChecksum.hpp"
7 
8 uint32_t NetChecksum::calcPartialChecksum(uint8_t* buffer, uint16_t len) {
9  uint32_t sum = 0;
10  uint16_t i = 0;
11 
12  if (len <= 0) {
13  return 0;
14  }
15 
16  for (; i < len - 1; i += 2) {
17  sum += *((uint16_t*) (&buffer[i]));
18  }
19 
20  if (i < len) {
21  sum += buffer[i];
22  }
23 
24  return sum;
25 }
26 
27 uint16_t NetChecksum::finalizeCheckSum(uint32_t tempCheckSum) {
28  while (tempCheckSum >> 16) {
29  tempCheckSum = (tempCheckSum & 0xFFFF) + (tempCheckSum >> 16);
30  }
31  return ~((uint16_t) tempCheckSum);
32 }