home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume28 / crc16 / part01 / crc.c next >
C/C++ Source or Header  |  1992-03-15  |  1KB  |  43 lines

  1. /* The following routines can be used to interpret the crc_table.
  2.   First use init_crc; then add_crc for each character, from
  3.   first to last; and finally, as report_crc to yield the
  4.   result. It is probably much more convenient to code these
  5.   routines inline; this file serves primarily as documentation. */
  6. #include <stdio.h>
  7. #include "crc_tab.h"
  8.  
  9. static unsigned short crc;
  10.  
  11. void init_crc(){crc = 0;}
  12.  
  13. void add_crc(c) unsigned char c;
  14.   {crc = crc_table[(crc & 0x00ff) ^ c] ^ (crc >> 8);
  15.   }
  16.  
  17. unsigned short report_crc(){return crc;}
  18.  
  19. void crcfile(name)char *name;
  20.  {FILE *f; int c; unsigned short ck;
  21.   printf("crc %s ", name);
  22.   f = fopen(name, "rb");
  23.   if(! f){printf("cannot find %s\n", name); return;}
  24.   init_crc();
  25.   while((c = getc(f)) >= 0){add_crc(c);}
  26.   ck =  report_crc();
  27. #ifdef AMIGA
  28.   printf("0x%x %d\n", ck, ck);
  29. #else
  30.   printf("%#.4x %d\n", ck, ck);
  31. #endif
  32.   fclose(f);
  33.   return;
  34.  }
  35.  
  36. void main(argc, argv)int argc; char *argv[];
  37.  {int i; char **c;
  38.   if(argc < 2){printf("crc filenames\n"); exit(1);}
  39.   c = &(argv[1]); i=argc;
  40.   while(--i)crcfile(*c++);
  41.   exit(0);
  42.  }
  43.