OpenCores
URL https://opencores.org/ocsvn/1g_ethernet_dpi/1g_ethernet_dpi/trunk

Subversion Repositories 1g_ethernet_dpi

[/] [1g_ethernet_dpi/] [trunk/] [sw/] [dev/] [test_main/] [src/] [net/] [cksum.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 kuzmi4
/*
2
 *
3
 * Portable C implementation of the Internet checksum, derived
4
 * from Braden, Borman, and Partridge's example implementation
5
 * in RFC 1071.
6
 *
7
 */
8
 
9
unsigned short cksum(void * ptr, int count)
10
{
11
    unsigned short checksum;
12
    unsigned short *addr = (unsigned short *)ptr;
13
 
14
    /********** code from rfc1071 **********/
15
    {
16
        /* Compute Internet Checksum for "count" bytes
17
        *         beginning at location "addr".
18
        */
19
 
20
        long sum = 0;
21
 
22
        while( count > 1 )
23
        {
24
            /*  This is the inner loop */
25
            sum += *addr++;
26
            count -= 2;
27
        }
28
 
29
        /*  Add left-over byte, if any */
30
        if( count > 0 )
31
            sum += * (unsigned char *) addr;
32
 
33
        /*  Fold 32-bit sum to 16 bits */
34
        while (sum>>16)
35
            sum = (sum & 0xffff) + (sum >> 16);
36
 
37
        checksum = ~sum;
38
   }
39
    /******** end of RFC 1071 code **********/
40
 
41
    return checksum;
42
}
43
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.