Checksum of a file? 
Author Message
 Checksum of a file?

hi,
  I really need help ....
I have to develope a module for checking File Integrity.For this I
need to use Rhapsody(in C) and design and code each state in the OMD.
I already have the statechart but don't know how to proceed.I have to
write code for performing the following

Develope a function to calculate check-sum.(this is for checking file
integrity)

Open a file
calculate checksum
Compare with the check sum at the end of same file
if checksum fails go to backup file open
calculate checksum
Compare with the check sum at the end of same file
       As i am a newbie with Rhapsody I would be really greatfull to
all help as to how I need to proceed with my module.
Basically  i was given the following piece of code
  if(fp==FAIL) next=OPEN_BACKUP_FILE;
else {
      calculatechecksum();
      varifychecksum();

Quote:
}

I have started of like this...
FILE *fp;

fp=fopen("chechksum.c","rw");
if(fp==FAIL)
printf("original file not opened\n");
next=OPEN_BACKUP_FILE;
printf("opened backup file\n");
else {
      calculatechecksum();
      varifychecksum();

Quote:
}

Now how to write a c program to calculatechecksum???
 calculatechecksum(file *fp,...?)

Please help,
Regards
Kavitha



Tue, 22 Nov 2005 11:48:18 GMT  
 Checksum of a file?


Quote:
> hi,
>   I really need help ....
> I have to develope a module for checking File Integrity.For this I
> need to use Rhapsody(in C) and design and code each state in the OMD.
> I already have the statechart but don't know how to proceed.I have to
> write code for performing the following

> Develope a function to calculate check-sum.(this is for checking file
> integrity)

Look into the CRC32 algorithm.

Google for the following articles...
"A Painless Guide to CRC Error Detection Algorithms", by Ross N. Williams
19 August 1993.

"File verification using CRC", by Mark Nelson, Dr. Dobb's Journal, May
1992.

If you need help with the C implementation after you've tried it
yourself, feel free to post here again, but please keep in mind this
isn't a source request newsgroup.

- Daniel



Tue, 22 Nov 2005 12:27:21 GMT  
 Checksum of a file?

Quote:

> I have to develope a module for checking File Integrity.For this
> I need to use Rhapsody(in C) and design and code each state in
> the OMD. I already have the statechart but don't know how to
> proceed.I have to write code for performing the following

> Develope a function to calculate check-sum.(this is for checking
> file integrity)

> Open a file
> calculate checksum
> Compare with the check sum at the end of same file
> if checksum fails go to backup file open
> calculate checksum
> Compare with the check sum at the end of same file

If running on MSDOS or Windoze check out validate at:

   <http://cbfalconer.home.att.net/download/>

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Tue, 22 Nov 2005 18:20:47 GMT  
 Checksum of a file?

Quote:
> Now how to write a c program to calculatechecksum???
>  calculatechecksum(file *fp,...?)

It depends on your definition of what a checksum is, but the process is
always the same :

- specifications (what to do?)
- design (how to do it ?)
- implementation (do it)
- integration (make it work)
- validation (is the result complying with the specification?)

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
                          -- Jerry Coffin in a.l.c.c++



Wed, 23 Nov 2005 13:53:20 GMT  
 Checksum of a file?
Hi Daniel,
  Thank u for ur response.I already have a working c program which
calculates the checksum of a file-it prints the checksum of the
file,but i need to know to calculate checksum at the start and end of
the same file??coz in the Rhapsody model in the OMD i first need to
calculate chck sum at start and end of original file.so how to
initiate the events/operations for this??
Well I have pasted a copy of the program
/**CHECKSUM.C**/
#include <stdlib.h>
#define TEST

unsigned checksum(void *buffer, size_t len, unsigned int seed)
{
      unsigned char *buf = (unsigned char *)buffer;
      size_t i;

      for (i = 0; i < len; ++i)
            seed += (unsigned int)(*buf++);
      return seed;

Quote:
}

#ifdef TEST

#include <stdio.h>

main()
{
      FILE *fp;
      size_t len;
      char buf[4096], *file = "CHECKSUM.C";

      if (NULL == (fp = fopen(file, "rb")))
      {
            printf("Unable to open %s for reading\n", file);
            return -1;
      }
      len = fread(buf, sizeof(char), sizeof(buf), fp);
      printf("%d bytes read\n", len);
      printf("The checksum of %s is %#x\n", file, checksum(buf, len,
0));

Quote:
}

#endif

maybe i haven't understood the concept of all this...i am confused.
thanks
kavitha



Fri, 25 Nov 2005 18:16:51 GMT  
 Checksum of a file?

Quote:

> Thank u for ur response.I already have a working c program which
> calculates the checksum of a file-it prints the checksum of the
> file,but i need to know to calculate checksum at the start and
> end of the same file??coz in the Rhapsody model in the OMD i
> first need to calculate chck sum at start and end of original
> file.so how to initiate the events/operations for this??
> Well I have pasted a copy of the program
> /**CHECKSUM.C**/
> #include <stdlib.h>

  #include <stddef.h>

Quote:
> #define TEST

> unsigned checksum(void *buffer, size_t len, unsigned int seed)
> {
>       unsigned char *buf = (unsigned char *)buffer;
>       size_t i;

>       for (i = 0; i < len; ++i)
>             seed += (unsigned int)(*buf++);
>       return seed;
> }

> #ifdef TEST

> #include <stdio.h>

> main()

  #include <stdlib.h> /* not needed for checksum() */

  int main(void)

Quote:
> {

  #define BUFLGH 4096

Quote:
>       FILE *fp;
>       size_t len;
>       char buf[4096], *file = "CHECKSUM.C";

        char buf[BUFLGH],
             *file = "CHECKSUM.C";
        unsigned int cksum;
        size_t       flen;

Quote:

>       if (NULL == (fp = fopen(file, "rb")))
>       {
>             printf("Unable to open %s for reading\n", file);
>             return -1;

              return EXIT_FAILURE;

Quote:
>       }
>       len = fread(buf, sizeof(char), sizeof(buf), fp);
>       printf("%d bytes read\n", len);
>       printf("The checksum of %s is %#x\n", file, checksum(buf, len, 0));

        cksum = 0;
        flen = 0;
        do {
           len = fread(buf, 1, BUFLGH, fp);
           flen += len;
           cksum = checksum(buf, len, cksum);
        } while (len < BUFLGH);
        printf("%lu bytes read\n"
               "Checksum of %s is %x\n",
               (unsigned long)flen, file, cksum);

Quote:
> }

> #endif

Try the above (untested) changes.  Requires that a len of 0 for
checksum simply returns seed, else obscure bug.  It will be much
handier if you get the filename from the command line, and also
handle defining TEST there on compilation.

Please don't use silly abbreviations like u and ur.  Please do
insert spaces after periods and commas.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Fri, 25 Nov 2005 20:43:45 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Good checksum computation for files.

2. how do you compute the checksum of a file ???

3. How can I get a checksum of a file using MFC?

4. File checksum algorithm/code in C

5. Help: CRC32 checksum on files

6. get the checksum and block number of a file

7. hiding a checksum in an unused file

8. File checksum

9. file checksums

10. credit card checksums in C#?

11. calculating a (crc) checksum

12. Need simple checksum algorithm

 

 
Powered by phpBB® Forum Software