Main Page | Class List | File List | Class Members | File Members

crc.c

Go to the documentation of this file.
00001 #define UCHAR unsigned char
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 
00006 void fwdccitthware(data,accum)
00007 UCHAR data;
00008 UCHAR *accum;
00009 {
00010   UCHAR i,j,tmp1,tmp2,tmp3;
00011   
00012   for(i=0;i<8;i++) {
00013     tmp1=((data<<i)&(0x80))/0x80;
00014     for(j=16;j>0;j--)
00015       accum[j]=accum[j-1];
00016     if ((tmp2=accum[16]^tmp1)==1)
00017       {
00018         accum[0]=tmp2;
00019         accum[5]=accum[5]^1;
00020         accum[12]=accum[12]^1;}
00021     else accum[0]=tmp2;
00022   }
00023 }
00024 
00025 void MakeCRCBytes(in, info_dim, CRC_dim)
00026 unsigned char *in;
00027 int info_dim;  /* number of info bytes */
00028 int CRC_dim;  
00029 {
00030 
00031   int i;
00032   UCHAR *crcbit;  
00033 
00034   crcbit = (UCHAR *) calloc(17, sizeof(UCHAR));
00035   if (crcbit == NULL) 
00036     printf("warning: CheckCRC calloc failed\n");
00037 
00038   for(i = 0; i <info_dim; i++)
00039     fwdccitthware(in[i],crcbit);
00040   
00041 /*  for(i = 15; i >=0 ; i--)
00042     printf("%d", crcbit[i]);
00043   printf("\n");  */
00044 
00045   in[info_dim] = 0;
00046   in[info_dim+1] = 0;
00047   for(i = 7; i >= 0; i--){
00048     in[info_dim] = (in[info_dim] << 1) + crcbit[i+8];
00049     in[info_dim+1] = (in[info_dim+1] << 1) + crcbit[i];
00050   } 
00051   
00052   free(crcbit);
00053 
00054 /*  printf("%d %d\n", in[info_dim], in[info_dim+1]); */
00055 }
00056 
00057 
00058 int CheckCRC(out_byte, info_dim, CRC_dim)
00059 UCHAR *out_byte; 
00060 int info_dim;
00061 int CRC_dim;
00062 {
00063 
00064   int i, sum=0;
00065   UCHAR *crcbit;  
00066 
00067   crcbit = (UCHAR *) calloc(17, sizeof(UCHAR));
00068   if (crcbit == NULL) 
00069     printf("warning: CheckCRC calloc failed\n");
00070 
00071   for(i = 0; i <(info_dim+CRC_dim); i++)
00072     fwdccitthware(out_byte[i],crcbit);
00073   for(i = 0; i < 16; i++)
00074     sum += crcbit[i];
00075   
00076   
00077   /* printf("\n"); */
00078 
00079   free(crcbit);
00080   
00081   if(sum == 0) 
00082     return 1;
00083   else 
00084     return 0;
00085 }
00086 
00087 /*
00088  main()
00089 {
00090   int i;
00091   UCHAR in[4];
00092   
00093   in[0] = 15;
00094   in[1] = 17;
00095 
00096   MakeCRCBytes(in, 2, 2);
00097   for (i=0; i<4;i++)
00098     printf(" %d\n", in[i]);
00099   
00100   in[0] = 14;
00101 
00102   if(CheckCRC(in, 2, 2))
00103     printf("No error\n");
00104   else
00105     printf("Oops! there is error\n");
00106   
00107 } */
00108 

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1