home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / CHECKSUM.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  828b  |  40 lines

  1. /*
  2. **  CHECKSUM.C - Compute the checksum of a file
  3. **
  4. **  public somain demo by Bob Stout
  5. */
  6.  
  7. #include <stdlib.h>
  8.  
  9. unsigned checksum(void *buffer, size_t len, unsigned int seed)
  10. {
  11.       unsigned char *buf = (unsigned char *)buffer;
  12.       size_t i;
  13.  
  14.       for (i = 0; i < len; ++i)
  15.             seed += (unsigned int)(*buf++);
  16.       return seed;
  17. }
  18.  
  19. #ifdef TEST
  20.  
  21. #include <stdio.h>
  22.  
  23. main()
  24. {
  25.       FILE *fp;
  26.       size_t len;
  27.       char buf[4096], *file = "CHECKSUM.C";
  28.  
  29.       if (NULL == (fp = fopen(file, "rb")))
  30.       {
  31.             printf("Unable to open %s for reading\n", file);
  32.             return -1;
  33.       }
  34.       len = fread(buf, sizeof(char), sizeof(buf), fp);
  35.       printf("%d bytes read\n", len);
  36.       printf("The checksum of %s is %#x\n", file, checksum(buf, len, 0));
  37. }
  38.  
  39. #endif
  40.