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();
}
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();
}
Now how to write a c program to calculatechecksum???
calculatechecksum(file *fp,...?)
Please help,
Regards
Kavitha
> 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
If running on MSDOS or Windoze check out validate at:
<http://cbfalconer.home.att.net/download/>
--
Chuck F (cbfal...@yahoo.com) (cbfal...@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
> 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++
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()
{
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));
}
#endif
maybe i haven't understood the concept of all this...i am confused.
thanks
kavitha
> #include <stdlib.h>
#include <stddef.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;
> }
>
> #ifdef TEST
>
> #include <stdio.h>
>
> main()
#include <stdlib.h> /* not needed for checksum() */
int main(void)
> {
#define BUFLGH 4096
> FILE *fp;
> size_t len;
> char buf[4096], *file = "CHECKSUM.C";
char buf[BUFLGH],
*file = "CHECKSUM.C";
unsigned int cksum;
size_t flen;
>
> if (NULL == (fp = fopen(file, "rb")))
> {
> printf("Unable to open %s for reading\n", file);
> return -1;
return EXIT_FAILURE;
> }
> 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);
> }
>
> #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.