-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCPPacket.cpp
52 lines (39 loc) · 1.14 KB
/
TCPPacket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// TCPPacket.cpp: implementation of the CTCPPacket class.
//
//////////////////////////////////////////////////////////////////////
#include "TCPPacket.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTCPPacket::CTCPPacket()
{
}
CTCPPacket::~CTCPPacket()
{
}
WORD CTCPPacket::Checksum(PWORD pData, int nBytes)
{
DWORD dwSum;
/*
* Our algorithm is simple, using a 32-bit accumulator (sum),
* we add sequential 16-bit words to it, and at the end, fold back
* all the carry bits from the top 16 bits into the lower 16 bits.
*/
dwSum = 0;
while (nBytes > 1)
{
dwSum += *pData++;
nBytes -= sizeof(WORD);
}
/* mop up an odd byte, if necessary */
if (nBytes == 1)
{
dwSum = *(PBYTE)pData; /* one byte only */
}
/*
* Add back carry outs from top 16 bits to low 16 bits.
*/
dwSum = (dwSum >> 16) + (dwSum & 0xffff); /* add high-16 to low-16 */
dwSum += (dwSum >> 16); /* add carry */
return ( (USHORT)(~dwSum) ); /* ones-complement, then truncate to 16 bits */
}