/* crc.c */

/* Copyright (C) 2000 Grant P. Maizels. All rights reserved
 * This software is copyrighted work licensed under the terms of the
 * GNU General Public License. Please consult
 * http://www.gnu.org/licenses/licenses.html#GPL for details.
 */

/* Version 1.0 last update 19Nov2001 */
#include <stdio.h>
#include "crcmodel.h"

main(argc, argv) 
int argc;
char *argv[];
{
   int c, i, n; 
   FILE *fp, *fopen();
   cm_t cm;
   p_cm_t p_cm = &cm;

   p_cm->cm_width = 16;
   p_cm->cm_poly   = 0x8005L;
   p_cm->cm_init  = 0L;
   p_cm->cm_refin = TRUE;
   p_cm->cm_refot  = TRUE;
   p_cm->cm_xorot  = 0l;

   i = 1;
   fp = stdin;
   do {
      if (argc > 1 && (fp=fopen(argv[i], "r")) == NULL) {
	 fprintf(stderr, "%s: can't open %s\n", argv[0], argv[i]);
	 continue;
      }
      cm_ini(p_cm);

      n = 0;
      while ((c = getc(fp)) != EOF) { 
   /* 0x11 & 0x13 in the first byte represent r-code - but are not part data */
	 if (n > 0 || (c != 0x11 && c != 0x13))
	    cm_nxt(p_cm, c);
	 n++;
      }
     
      printf("%s CRC: %d\n", (argc > 1?argv[i]:"stdin"), cm_crc(p_cm) & 0xffff);
   } while (++i < argc);
   exit(0);
}
