-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChecksum.c
68 lines (59 loc) · 1.32 KB
/
Checksum.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
////////////////////////////////////////////////////////////////////////////////
//
// Global including files.
//
// Including the files for the system.
//
// These including files are generally used in different operation system.
//
////////////////////////////////////////////////////////////////////////////////
#include "Global.h"
////////////////////////////////////////////////////////////////////////////////
//
// Get checksum16.
//
////////////////////////////////////////////////////////////////////////////////
_UINT32 GetChecksum16(_BYTE* lpBytes,_INT32 nSize)
{
//Checksum.
_UINT32 nChecksum;
//Byte
_UINT8 bValue;
//Short.
_UINT16* lpShorts = (_UINT16 *)lpBytes;
#ifdef _DEBUG
assert(lpBytes != NULL && nSize > 0);
#endif
//Set checksum.
nChecksum = 0;
//Do while.
while(nSize > 1)
{
//Add byte.
nChecksum += *lpShorts;
//Add pointer.
lpShorts ++;
//Sub
nSize -= sizeof(_UINT16);
}
//Check size.
if(nSize != 0)
{
//Add it.
nChecksum += *((_UINT8 *)lpShorts);
}
//Adjust.
nChecksum = (nChecksum & 0xFFFF) + (nChecksum >> 16);
//Adjust.
nChecksum += nChecksum >> 16;
//Adjust.
nChecksum = ~nChecksum;
//Get low byte.
bValue = (_UINT8)nChecksum;
//Shift right.
nChecksum = (nChecksum >> 8) & 0xFF;
//Add higher part.
nChecksum |= bValue << 8;
//Return result.
return nChecksum;
}