Advertisement
rowers

crc1

Dec 3rd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. /* compute crc's */
  2. /* crc-ccitt is based on the polynomial x^16+x^12+x^5+1 */
  3. /* The prescription for determining the mask to use for a given polynomial
  4.     is as follows:
  5.         1.  Represent the polynomial by a 17-bit number
  6.         2.  Assume that the most and least significant bits are 1
  7.         3.  Place the right 16 bits into an integer
  8. */
  9. /* Usage : ccitt [filename] */
  10.  
  11. #include    <stdio.h>
  12. #include    <stdlib.h>
  13. #include    <string.h>
  14. #include    <time.h>
  15.  
  16. #define     MTT 0x8000000200021013L     /* crc-ccitt mask */
  17. #define     CLK_TCK CLOCKS_PER_SEC
  18.  
  19. /* function declarations */
  20. void updcrc(unsigned long long*,unsigned long long,unsigned long long);
  21. void perr(char *);
  22.  
  23. /* variables */
  24. char filename[100];
  25. unsigned long long crctt[2];
  26. unsigned long long ch;
  27. unsigned long num;
  28. FILE *fp;
  29.  
  30. /* driver */
  31. int main(argc,argv)
  32. int argc; char **argv;
  33. {
  34.  clock_t start,end;
  35.     if(argc>2) perr("Usage:  ccitt [filename]");
  36.     if(argc==2) strcpy(filename,argv[1]);
  37.     else
  38.     {
  39.         printf("\nEnter filename:  ");
  40.         gets(filename);
  41.     }
  42.     if((fp=fopen(filename,"rb"))==NULL) perr("Can't open file");
  43.     num=0L; crctt[0]=0, crctt[1]=0;
  44.     start=clock();
  45.     //printf("%d\n", sizeof(unsigned long long));
  46.  
  47.     while((ch=fgetc(fp))!=EOF)
  48.     {
  49.         //if (ch == '\n')
  50.         //  continue;
  51.         num++;
  52.         //int i; for (i=0;i<10000;i++){
  53.         updcrc(crctt,ch,MTT);
  54.         //}
  55.     }
  56.     crctt[0]%=(1<<10);
  57.     end=clock();
  58.     fclose(fp);
  59.     printf("\nNumber of bytes = %lu\n CRCTT = %llX%llX\n", num, crctt[0], crctt[1]);
  60.     printf("Czas trwania: %f\n",(double)(end-start)/CLK_TCK);
  61.     return 0;
  62. }
  63.  
  64. /* update crc */
  65. void updcrc(crc,c,mask)
  66. unsigned long long *crc, c, mask;
  67. {
  68.     int i;
  69.     c<<=2;
  70.     for(i=0;i<8;i++)
  71.     {
  72.         if((crc[0] ^ c) & (1<<9))
  73.         {
  74.             crc[0]<<=1;
  75.             if(crc[1]&(1LLU<<63))
  76.                 crc[0]+=1;
  77.             crc[1]=(crc[1]<<1)^mask;
  78.             crc[0]=crc[0]^0LLU;
  79.         }
  80.         else
  81.         {
  82.             crc[0]<<=1;
  83.             if(crc[1]&(1LLU<<63))
  84.                 crc[0]+=1;
  85.             crc[1]<<=1;
  86.         }
  87.         c<<=1;
  88.     }
  89. }
  90.  
  91. /* error abort */
  92. void perr(s)
  93. char *s;
  94. {
  95.     printf("\n%s",s); exit(1);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement