Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 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 MTT 0x100011021 /* crc-ccitt mask */
  18. #define CLK_TCK CLOCKS_PER_SEC
  19.  
  20. /* function declarations */
  21. unsigned long long int updcrc(unsigned long long int,int,unsigned long long int);
  22. void perr(char *);
  23.  
  24. /* variables */
  25. char filename[100];
  26. unsigned long long int crctt;
  27. int ch;
  28. unsigned long num;
  29. FILE *fp;
  30.  
  31. /* driver */
  32. int main(argc,argv)
  33. int argc; char **argv;
  34. {
  35. clock_t start,end;
  36. if(argc>2) perr("Usage: ccitt [filename]");
  37. if(argc==2) strcpy(filename,argv[1]);
  38. else
  39. {
  40. printf("\nEnter filename: ");
  41. gets(filename);
  42. }
  43. if((fp=fopen(filename,"rb"))==NULL) perr("Can't open file");
  44. num=0L; crctt=0;
  45. start=clock();
  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. crctt=updcrc(crctt,ch,MTT);
  54. //}
  55. }
  56. end=clock();
  57. fclose(fp);
  58. printf("\nNumber of bytes = %lu\n CRCTT = %04llX\n", num,crctt);
  59. printf("Czas trwania: %f\n",(double)(end-start)/CLK_TCK);
  60. getchar();
  61. return 0;
  62. }
  63.  
  64. /* update crc */
  65. unsigned long long int updcrc(crc,c,mask)
  66. unsigned long long int crc,mask; int c;
  67. {
  68. int i;
  69. c<<=56;
  70. for(i=0;i<8;i++)
  71. {
  72. if((crc ^ c) & 0x8000000000000000) crc=(crc<<1)^mask;
  73. else crc<<=1;
  74. c<<=1;
  75. }
  76. return crc;
  77. }
  78.  
  79. /* error abort */
  80. void perr(s)
  81. char *s;
  82. {
  83. printf("\n%s",s); exit(1);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement