home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / windows / winterm / crc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-30  |  785 b   |  41 lines

  1. /* crc.c */
  2.  
  3. #include "crc.h"
  4.  
  5. #define POLY 0x1021
  6.  
  7. static unsigned short CRCtable[256];
  8.  
  9. /* initialize CRC table */
  10.  
  11. void InitCRC(void)
  12. {int i;
  13.  unsigned short CalcTable();
  14.  for(i=0;i<256;i++) CRCtable[i] = CalcTable(i,POLY,0);
  15. }
  16.  
  17. /* calculate CRC table entry */
  18.  
  19. unsigned short CalcTable(data,genpoly,accum)
  20. unsigned short data;
  21. unsigned short genpoly;
  22. unsigned short accum;
  23. {static int i;
  24.  data <<= 8;
  25.  for(i=8;i>0;i--)
  26.          {
  27.           if((data^accum) & 0x8000) accum = (accum << 1) ^ genpoly;
  28.           else accum <<= 1;
  29.           data <<= 1;
  30.          }
  31.  return(accum);
  32. }
  33.  
  34. /* compute updated CRC */
  35.  
  36. unsigned short UpdateCRC(crc,byte)
  37. unsigned short crc;
  38. unsigned char  byte;
  39. {
  40.  return( (crc << 8) ^ CRCtable[ (crc >> 8) ^ byte ] );
  41. }