Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 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 0x10000000100011020 /* crc-ccitt mask */
  17. #define CLK_TCK CLOCKS_PER_SEC
  18.  
  19. /* function declarations */
  20. unsigned long long int updcrc(unsigned long long int,int,unsigned long long int);
  21. void perr(char *);
  22.  
  23. /* variables */
  24. char filename[100];
  25. unsigned long long int crctt;
  26. int 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;
  44. start=clock();
  45.  
  46. while((ch=fgetc(fp))!=EOF)
  47. {
  48. //if (ch == '\n')
  49. // continue;
  50. num++;
  51. //int i; for (i=0;i<10000;i++){
  52. crctt=updcrc(crctt,ch,MTT);
  53. //}
  54. }
  55. end=clock();
  56. fclose(fp);
  57. printf("\nNumber of bytes = %lu\n CRCTT = %04X\n", num,crctt);
  58. printf("Czas trwania: %f\n",(double)(end-start)/CLK_TCK);
  59. return 0;
  60. }
  61.  
  62. /* update crc */
  63. unsigned long long int updcrc(crc,c,mask)
  64. unsigned long long int crc,mask; int c;
  65. {
  66. int i;
  67. c<<=56;
  68. for(i=0;i<8;i++)
  69. {
  70. if((crc ^ c) & 0x80000000000000003) crc=(crc<<1)^mask;
  71. else crc<<=1;
  72. c<<=1;
  73. }
  74. return crc;
  75. }
  76.  
  77. /* error abort */
  78. void perr(s)
  79. char *s;
  80. {
  81. printf("\n%s",s); exit(1);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement