From:	MERC::"uunet!WKUVX1.BITNET!MacroMan" 23-MAR-1993 16:35:59.11
To:	MACRO32 <MACRO32@WKUVX1.BITNET>
CC:	
Subj:	Re: How does the crc instruction work? (buggy C code)

> I've looked in the VAX Architecture book on the 3 measley pages concerning
> the crc instruction, and I *still* don't know what it is doing?  Here is the
> code that I am trying to get to work.  Can anyone tell me what the correct
> steps are in the crc routine?
>
> It has occured to me that the CRC table *is* passed as  table.ab  and I am
> passing the address of a table of longwords, but that doesn't tell me what
> I am supposed to do with each byte.
>
[code omitted]

The following code will generate the same results as LIB$CRC:

#include <stdio.h>
#include <descrip.h>
#include <ssdef.h>

#define step 4
#define limit 2
#define index 1

unsigned int softcrc(int crctbl[16],int init,char *tststr)
{
   register char *s;
   register unsigned int tmp,tmp2,i;
   s = tststr;
   tmp = init;
   while (*s)
      {
         tmp = tmp^((int)*s);
         for (i=0;i<limit;i++)
            {
               tmp2 = tmp>>step;
               tmp2 = tmp2^crctbl[(tmp-(tmp2<<step))*index];
               tmp = tmp2;
            };
         s++;
      };
   return tmp;
};

void lib$crc_table();
int lib$crc();

#define tststr "This is a stupid test-string choosen random"
#ifdef CRC16
int poly = 0x0000A001;
int init = 0x00000000;
#endif
#ifdef CCITT
int poly = 0x00008408;
int init = 0x0000FFFF;
#endif
#ifdef AUTODINII
int poly = 0xEDB88320;
int init = 0xFFFFFFFF;
#endif
int crctbl[16];

main()
{
   $DESCRIPTOR(tstdes,tststr);
   lib$crc_table(&poly,&crctbl);
   printf("CRC of >>%s<< is  %8X\n",tststr,lib$crc(&crctbl,&init,&tstdes));
   printf("soft-CRC of >>%s<< is  %8X\n",tststr,softcrc(crctbl,init,tststr));
   return SS$_NORMAL;
};

To test: try CC/DEFINE=polynomium !

                                                          Arne

Arne Vajhxj                             local DECNET:  KO::ARNE
Computer Department                     PSI:           PSI%238310013040::ARNE
Business School of Southern Denmark     Internet:      ARNE@KO.HHS.DK

