?
Solved

Checksum program

Posted on 2003-03-10
7
Medium Priority
?
490 Views
Last Modified: 2010-04-15
Could anyone please provide me with a simple C program (or sources of any)to calculate the checksum over a string(input being a pointer to the string and the length of the string). I have to use it in another program (to manipulate IP datagrams)
Thanks

                       
0
Comment
Question by:pm0615
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
gj62 earned 75 total points
ID: 8104632
This is about as simple a checksum as you can have - it uses a full unsigned int...  Do you want this, or a CRC?

unsigned int simpleChecksum(char *p, unsigned int strLength)
{
  unsigned int i,sum = 0;
  for(i=0;i<strLength;++i)
  {
    sum += *p++;
  }
  return(sum);
}
0
 
LVL 6

Expert Comment

by:gj62
ID: 8104746
This is about as simple a checksum as you can have - it uses a full unsigned int...  Do you want this, or a CRC?

unsigned int simpleChecksum(char *p, unsigned int strLength)
{
  unsigned int i,sum = 0;
  for(i=0;i<strLength;++i)
  {
    sum += *p++;
  }
  return(sum);
}
0
 

Author Comment

by:pm0615
ID: 8105486
basically I need a checksum program over 32 bit IP headers. So I have to perform the ones complement at the end. Any help?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:jcaldwel
ID: 8105507
If you mean THE TCP/IP Header Checksum Algo, here it is:

/*
 * in_cksum --
 *      Checksum routine for Internet Protocol family headers (C Version)
 */
static int
in_cksum(u_short *addr, int len)
{
        register int nleft = len;
        register u_short *w = addr;
        register int sum = 0;
        u_short answer = 0;

        /*
         * 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.
         */
        while (nleft > 1)  {
                sum += *w++;
                nleft -= 2;
        }

        /* mop up an odd byte, if necessary */
        if (nleft == 1) {
                *(u_char *)(&answer) = *(u_char *)w ;
                sum += answer;
        }

        /* add back carry outs from top 16 bits to low 16 bits */
        sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
        sum += (sum >> 16);                     /* add carry */
        answer = ~sum;                          /* truncate to 16 bits */
        return(answer);
}
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8105509
If you mean THE TCP/IP Header Checksum Algo, here it is:

/*
 * in_cksum --
 *      Checksum routine for Internet Protocol family headers (C Version)
 */
static int
in_cksum(u_short *addr, int len)
{
        register int nleft = len;
        register u_short *w = addr;
        register int sum = 0;
        u_short answer = 0;

        /*
         * 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.
         */
        while (nleft > 1)  {
                sum += *w++;
                nleft -= 2;
        }

        /* mop up an odd byte, if necessary */
        if (nleft == 1) {
                *(u_char *)(&answer) = *(u_char *)w ;
                sum += answer;
        }

        /* add back carry outs from top 16 bits to low 16 bits */
        sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
        sum += (sum >> 16);                     /* add carry */
        answer = ~sum;                          /* truncate to 16 bits */
        return(answer);
}
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8105524
Noticed you accepted the answer... I forgot to put Berkley's Copyright message on the previous post:

/*
 * Copyright (c) 1989 The Regents of the University of California.
 * All rights reserved.
 */
0
 

Author Comment

by:pm0615
ID: 8106129
ok..and then I just write  a main program accepting from the user the above arguments and call this routine?

Thanks!!

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question