Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* compute crc's */
- /* crc-ccitt is based on the polynomial x^16+x^12+x^5+1 */
- /* The prescription for determining the mask to use for a given polynomial
- is as follows:
- 1. Represent the polynomial by a 17-bit number
- 2. Assume that the most and least significant bits are 1
- 3. Place the right 16 bits into an integer
- */
- /* Usage : ccitt [filename] */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define MTT 0x8000000200021013L /* crc-ccitt mask */
- #define CLK_TCK CLOCKS_PER_SEC
- /* function declarations */
- void updcrc(unsigned long long*,unsigned long long,unsigned long long);
- void perr(char *);
- /* variables */
- char filename[100];
- unsigned long long crctt[2];
- unsigned long long ch;
- unsigned long num;
- FILE *fp;
- /* driver */
- int main(argc,argv)
- int argc; char **argv;
- {
- clock_t start,end;
- if(argc>2) perr("Usage: ccitt [filename]");
- if(argc==2) strcpy(filename,argv[1]);
- else
- {
- printf("\nEnter filename: ");
- gets(filename);
- }
- if((fp=fopen(filename,"rb"))==NULL) perr("Can't open file");
- num=0L; crctt[0]=0, crctt[1]=0;
- start=clock();
- //printf("%d\n", sizeof(unsigned long long));
- while((ch=fgetc(fp))!=EOF)
- {
- //if (ch == '\n')
- // continue;
- num++;
- //int i; for (i=0;i<10000;i++){
- updcrc(crctt,ch,MTT);
- //}
- }
- crctt[0]%=(1<<10);
- end=clock();
- fclose(fp);
- printf("\nNumber of bytes = %lu\n CRCTT = %llX%llX\n", num, crctt[0], crctt[1]);
- printf("Czas trwania: %f\n",(double)(end-start)/CLK_TCK);
- return 0;
- }
- /* update crc */
- void updcrc(crc,c,mask)
- unsigned long long *crc, c, mask;
- {
- int i;
- c<<=2;
- for(i=0;i<8;i++)
- {
- if((crc[0] ^ c) & (1<<9))
- {
- crc[0]<<=1;
- if(crc[1]&(1LLU<<63))
- crc[0]+=1;
- crc[1]=(crc[1]<<1)^mask;
- crc[0]=crc[0]^0LLU;
- }
- else
- {
- crc[0]<<=1;
- if(crc[1]&(1LLU<<63))
- crc[0]+=1;
- crc[1]<<=1;
- }
- c<<=1;
- }
- }
- /* error abort */
- void perr(s)
- char *s;
- {
- printf("\n%s",s); exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement