[comp.os.os9] CRC calculation

alanm@bprcsitu.UUCP (Alan McIvor) (06/20/91)

Hi,
	Sorry if these are FAQs, but I am a new reader of this list.

1) Can someone please give me a working algorithm for doing the CRC
calculation. I have tried the one in Dibble but can't get it to work.

2) How do I go about getting code from places like smilodon, preferably from a
mail server, as we don't have ftp access yet.

Thanks,

Dr Alan M. McIvor		
BP International Ltd    	...{mcsun,uunet}!ukc!bprcsitu!alanm
Research Centre Sunbury	    	alanm%bprcsitu.uucp@uk.ac.ukc
Chertsey Road			bprcsitu!alanm@relay.eu.net
Sunbury-on-Thames       	Tel: +44 932 764252
Middlesex TW16 7LN		Fax: +44 932 762999
U.K.                            

ekuns@kilroy.chi.il.us (Eddie Kuns) (06/23/91)

Here's a fragment of a program I had lying around.  It's a sample CRC
calculation under OS-9/6809 and it updates the CRC when done.  It's
a very simplistic program just as an example.  It assumes the entire
file is a single module.  Bells and whistles can easily be added, but
I never needed them.  Here's the program:

========================= 8< ===================== 8< ========================
/*  calc_crc.c  */

/*
 *  I cut calc_crc.c out of a larger program.  I release this code into
 *  the public domain as a demonstration of how to calculate CRC's in OS-9.
 *  Anyone is free to copy and distribute this code as freely as desired,
 *  including using it in commercial applications without my explicit
 *  permission.  (That's what PD means!)  When distributing this file without
 *  any changes to the code, please leave this header unchanged as well.
 *
 *  This code was written and tested under OS-9/6809 and may or may not work
 *  under OS-9/680x0 or OS-9000; however, the basic principles should be the
 *  same. This program, as written, depends on Carl Krieder's clib.l for
 *  OS-9/6809.
 *
 *                   Eddie Kuns
 *
 *    School: EKuns@zodiac.rutgers.edu (domain)  EKuns@zodiac (bitnet)
 *    Home  : ekuns@kilroy.chi.il.us       kilroy!ekuns@linac.fnal.gov
 *    Home  : ...!{linac,liltyke}!kilroy!ekuns       Delphi: EddieKuns
 */

#include <stdio.h>
#include <os9.h>

#define ctoi(x) ((x) & 0xff)

main(argc, argv)
   int  argc;
   char *argv[];
{
   struct registers regs;
   char CRC[3], *iobuf, *file, *malloc();
   long length, left, _gs_size();
   int  path, count, bufsiz, err;

   if (argc != 2)
   {
      printf("This program calculates and updates the CRC of a module.\n");
      printf("In this simple version, it assumes the file it is given\n");
      printf("only contains a single module.\n\n");
      printf("Usage: calc_crc filename\n");
      exit(0);
   }

   file = argv[1];

   /* Initialize CRC */
   CRC[0] = CRC[1] = CRC[2] = 0xff;

   if ((path = open(file, _READ | _WRITE)) == -1)
      exit(errno);

   /* find out how long the file is, excepting CRC bytes */
   left = length = _gs_size(path) - 3L;

   bufsiz = 25600;    /* 25k buffer  */
   if ((iobuf = malloc(bufsiz)) == NULL)
      exit(207);  /* Out of memory */

   /* Calculate the CRC on the file incrementally */
   while (left > 0L)
   {
      count = (left < (long) bufsiz) ? (int) left : bufsiz;
      if ((count = read(path, iobuf, count)) <= 0)
      {
         err = errno;
         printf("Disk read failure, CRC calculation aborted\n");
         exit(err);
      }
      left -= (long) count;

      regs.rg_x = iobuf;
      regs.rg_y = count;
      regs.rg_u = CRC;
      _os9(F_CRC, &regs);
   }

   /* Calculate one's complement */
   CRC[0] = ~CRC[0];
   CRC[1] = ~CRC[1];
   CRC[2] = ~CRC[2];

   printf("The new CRC = $%02X%02X%02X\n\n", ctoi(CRC[0]), ctoi(CRC[1]),
                                             ctoi(CRC[2]));

   /*
    * Update the CRC
    */
   lseek(path, length, 0);
   write(path, CRC, 3);

   close(path);
}
========================= 8< ===================== 8< ========================