home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / unix / unxbin.sha / crc.c < prev    next >
C/C++ Source or Header  |  1986-04-02  |  446b  |  34 lines

  1. /*
  2.  * compute the crc used in .hqx files
  3.  */
  4. #include <stdio.h>
  5.  
  6. int crc, temp;
  7.  
  8. main()
  9. {
  10.     int c;
  11.  
  12.     crc = 0;
  13.     while ( ( c = getchar() ) != EOF )
  14.         docrc( c );
  15.     docrc(0); docrc(0);
  16.     putchar( (crc>>8) & 0xff ); putchar( crc & 0xff );
  17. }
  18.  
  19. docrc( c )
  20.     int c;
  21. {
  22.     register int i;
  23.     temp = crc;
  24.     for ( i = 0; i < 8; i++ )
  25.     {
  26.         c <<= 1;
  27.         if ( (temp <<= 1) & 0x10000 )
  28.             temp = (temp & 0xffff) ^ 0x1021;
  29.         temp ^= (c >> 8);
  30.         c &= 0xff;
  31.     }
  32.     crc = temp;
  33. }
  34.