7f11c1a74aea57a88ec9c4d62f1288b4972dc208
[reactos.git] / reactos / lib / drivers / ip / network / checksum.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: tcpip/checksum.c
5 * PURPOSE: Checksum routines
6 * NOTES: The checksum routine is from RFC 1071
7 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * REVISIONS:
9 * CSH 01/08-2000 Created
10 */
11
12 #include "precomp.h"
13
14
15 ULONG ChecksumFold(
16 ULONG Sum)
17 {
18 /* Fold 32-bit sum to 16 bits */
19 while (Sum >> 16)
20 {
21 Sum = (Sum & 0xFFFF) + (Sum >> 16);
22 }
23
24 return Sum;
25 }
26
27 ULONG ChecksumCompute(
28 PVOID Data,
29 UINT Count,
30 ULONG Seed)
31 /*
32 * FUNCTION: Calculate checksum of a buffer
33 * ARGUMENTS:
34 * Data = Pointer to buffer with data
35 * Count = Number of bytes in buffer
36 * Seed = Previously calculated checksum (if any)
37 * RETURNS:
38 * Checksum of buffer
39 */
40 {
41 register ULONG Sum = Seed;
42
43 while (Count > 1)
44 {
45 Sum += *(PUSHORT)Data;
46 Count -= 2;
47 Data = (PVOID)((ULONG_PTR) Data + 2);
48 }
49
50 /* Add left-over byte, if any */
51 if (Count > 0)
52 {
53 Sum += *(PUCHAR)Data;
54 }
55
56 return Sum;
57 }