checksum.c

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <fcntl.h>
00004 
00005 int main(int argc,char **argv)
00006 {
00007         int fd;
00008         struct stat statbuf;
00009         unsigned int checksum,i;
00010         unsigned int seek,len,size;
00011         char *buf;
00012         seek = strtoul(argv[2],0,0);
00013         len = strtoul(argv[3],0,0);
00014         if(stat(argv[1],&statbuf)!=0)
00015         {
00016                 printf("error read file %s\n",argv[1]);
00017                 exit(-1);
00018         }
00019         printf("open file %s,size %d\n",argv[1],statbuf.st_size);
00020         buf=malloc(statbuf.st_size+4);
00021         if(!buf)printf("error allocate memory\n");
00022         fd=open(argv[1],O_RDONLY);
00023         read(fd,buf,statbuf.st_size);
00024         close(fd);
00025         checksum = 0;
00026         size = (statbuf.st_size+3)&~3;
00027         if(len+seek>size) len=size;
00028 
00029         for(i=seek,checksum=0;i<seek+len;i+=4)
00030         {
00031                 checksum += *(unsigned int *)(buf+i);
00032         }
00033         printf("checksum 0x%08x\n",checksum);
00034 
00035         return 0;
00036 }

Generated on Fri Nov 5 07:24:46 2010 for ejtag debug tools for loongson 232 cpu by  doxygen 1.5.6