
#include <stdio.h>	

/*
 *
 * static void init_crc16_tab( void );
 *
 * For optimal performance uses the CRC16 routine a lookup table with values
 * that can be used directly in the XOR arithmetic in the algorithm. This
 * lookup table is calculated by the init_crc16_tab() routine, the first time
 * the CRC function is called.
 */

#define		CRC_POLY_16		0xA001
static unsigned short    crc_tab16[256];

static void init_crc16_tab( void ) {
unsigned short i, j, crc, c;

for (i=0; i<256; i++) {
		crc = 0;
		c   = i;

		for (j=0; j<8; j++) {

			if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ CRC_POLY_16;
			else                      crc =   crc >> 1;

			c = c >> 1;
		}

	crc_tab16[i] = crc;
	}
}  /* init_crc16_tab */



int main(int argc,char *argv[])
{ 
  int i=1;
  unsigned short crc;
  int c;
  FILE *fptr;

  init_crc16_tab(); 

  while (i<argc)
    { fptr=fopen(argv[i],"rb");
	{ if (!fptr)
	       printf("Unable to open file %s\n",argv[i]);
          else
	  { crc = 0x0000;	
            while( (c=fgetc(fptr)) !=EOF )
	     {              
		crc = (crc >> 8) ^ crc_tab16[ (crc ^ (c & 0xFF)) & 0x00FF ];
    		}
    	   printf(" %4.4x    %s\n",crc,argv[i]);
           fclose(fptr);
	  };
	};
     i++;
  };
   
};



