Added networking code from Casper Hornstrup
[reactos.git] / reactos / drivers / net / tcpip / tcpip / 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 #include <tcpip.h>
12 #include <checksum.h>
13
14
15 ULONG ChecksumCompute(
16 PVOID Data,
17 UINT Count,
18 ULONG Seed)
19 /*
20 * FUNCTION: Calculate checksum of a buffer
21 * ARGUMENTS:
22 * Data = Pointer to buffer with data
23 * Count = Number of bytes in buffer
24 * Seed = Previously calculated checksum (if any)
25 * RETURNS:
26 * Checksum of buffer
27 */
28 {
29 /* FIXME: This should be done in assembler */
30
31 register ULONG Sum = Seed;
32
33 while (Count > 1) {
34 Sum += *(PUSHORT)Data;
35 Count -= 2;
36 (ULONG_PTR)Data += 2;
37 }
38
39 /* Add left-over byte, if any */
40 if (Count > 0)
41 Sum += *(PUCHAR)Data;
42
43 /* Fold 32-bit sum to 16 bits */
44 while (Sum >> 16)
45 Sum = (Sum & 0xFFFF) + (Sum >> 16);
46
47 return ~Sum;
48 }
49
50 /* EOF */