home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / HPACK78S.ZIP / crc / crc16.c next >
C/C++ Source or Header  |  1992-06-22  |  1KB  |  52 lines

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            HPACK Multi-System Archiver                        *
  4. *                            ===========================                        *
  5. *                                                                            *
  6. *                                  CRC16 Routines                            *
  7. *                             CRC16.C  Updated 16/07/91                        *
  8. *                                                                            *
  9. * This program is protected by copyright and as such any use or copying of    *
  10. *  this code for your own purposes directly or indirectly is highly uncool    *
  11. *                      and if you do so there will be....trubble.                *
  12. *                 And remember: We know where your kids go to school.            *
  13. *                                                                            *
  14. *            Copyright 1991  Peter C.Gutmann.  All rights reserved            *
  15. *                                                                            *
  16. ****************************************************************************/
  17.  
  18. #include "defs.h"
  19.  
  20. /* The crc16 table and crc16 variable itself */
  21.  
  22. WORD crc16tbl[ 256 ];
  23. WORD crc16;
  24.  
  25. /* The block crc16 calculation routine.  Ideally this should be done in
  26.    assembly language for speed */
  27.  
  28. void crc16buffer( BYTE *bufPtr, int length )
  29.     {
  30.     while( length-- )
  31.         crc16 = crc16tbl[ ( BYTE ) crc16 ^ *bufPtr++ ] ^ ( crc16 >> 8 );
  32.     }
  33.  
  34. /* The initialisation routine for the crc16 table */
  35.  
  36. void initCRC16( void )
  37.     {
  38.     int bitCount, tblIndex;
  39.     WORD crcVal;
  40.  
  41.     for( tblIndex = 0; tblIndex < 256; tblIndex++ )
  42.         {
  43.         crcVal = tblIndex;
  44.         for( bitCount = 0; bitCount < 8; bitCount++ )
  45.             if( crcVal & 0x01 )
  46.                 crcVal = ( crcVal >> 1 ) ^ 0xA001;
  47.             else
  48.                 crcVal >>=  1;
  49.         crc16tbl[ tblIndex ] = crcVal;
  50.         }
  51.     }
  52.