home *** CD-ROM | disk | FTP | other *** search
/ PCMania 10 / Pcmania_Ep2_10_CD-01.iso / ARTICULOS / tecnologia / DLOCK2.ZIP / CRC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-02  |  1.4 KB  |  61 lines

  1. /* crc.cpp -- contains table based CCITT 32 bit CRC function.
  2.    This file is in the Public Domain.
  3. */
  4.  
  5. #define CRC_MASK           0xFFFFFFFFL
  6. #define CRC32_POLYNOMIAL   0xEDB88320L
  7.  
  8. #include "def.h"
  9. #include "crc.h"
  10.  
  11. unsigned long  *Ccitt32Table = (unsigned long *)NULL;
  12.  
  13. /****************************************************************************/
  14.  
  15. /*
  16.  * This routine simply builds the coefficient table used to calculate
  17.  * 32 bit CRC values throughout this program.  The 256 long word table
  18.  * has to be set up once when the program starts.  Alternatively, the
  19.  * values could be hard coded in, which would offer a miniscule improvement
  20.  * in overall performance of the program.
  21.  */
  22.  
  23. int CALLTYPE BuildCRCTable(void)
  24.     {
  25.     int i;
  26.     int j;
  27.     unsigned long value;
  28.  
  29.     if (Ccitt32Table)
  30.         return 0;
  31.     Ccitt32Table = new unsigned long int[256];
  32.     if (Ccitt32Table == NULL)
  33.         {
  34.         return 1;
  35.         }
  36.     for ( i = 0; i <= 255 ; i++ )
  37.         {
  38.         value = i;
  39.         for ( j = 8 ; j > 0; j-- )
  40.             {
  41.             if ( value & 1 )
  42.                 value = ( value >> 1 ) ^ CRC32_POLYNOMIAL;
  43.             else
  44.                 value >>= 1;
  45.             }
  46.         Ccitt32Table[ i ] = value;
  47.         }
  48.     return 0;
  49.     }
  50.  
  51.  
  52. void CALLTYPE crc32done(void)
  53.     {
  54.     if (Ccitt32Table)
  55.         {
  56.         delete Ccitt32Table;
  57.         Ccitt32Table = (unsigned long*)NULL;
  58.         }
  59.     }
  60.  
  61.