Advertisement
Guest User

fixcrc.c

a guest
Jul 2nd, 2021
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. /*
  2.  * Demonstrates how the incorrect check value of 0x29B1 may be reported for the test string
  3.  * "123456789" when it should be 0xE5CC.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #define poly 0x1021 /* crc-ccitt mask */
  10.  
  11. /* Global variables */
  12.  
  13. int off = 0;
  14. char text[1000];
  15. unsigned short good_crc = 0;
  16. unsigned short bad_crc = 0;
  17. unsigned short text_length = 750;
  18.  
  19. void go(void);
  20. void repeat_character(unsigned char, unsigned short);
  21. void update_good_crc(unsigned short);
  22. void augment_message_for_good_crc(void);
  23. void update_bad_crc(unsigned short);
  24.  
  25. int
  26. main(int argc, char **argv)
  27. {
  28.         FILE *f = fopen(argv[1], "r");
  29.         printf("%d bytes read\n", fread(text, 1, 752, f));
  30.         fclose(f);
  31.  
  32.         go();
  33.         printf("Good_CRC = %04X,  Bad_CRC = %04X,  Length = %u\n", good_crc, bad_crc, text_length);
  34.         text[750] = (good_crc & 0xFF00) >> 8;
  35.         text[751] = (good_crc & 0x00FF);
  36.  
  37.         f = fopen(argv[1], "w");
  38.         printf("%d bytes written\n", fwrite(text, 1, 752, f));
  39.         fclose(f);
  40.  
  41.         return 0;
  42. }
  43.  
  44. void
  45. go(void)
  46. {
  47.         int i;
  48.         unsigned short ch;
  49.  
  50.         for (i = 0; i < (text_length - off); i++) {
  51.                 update_good_crc(text[i + off]);
  52.                 update_bad_crc(text[i + off]);
  53.         }
  54.         augment_message_for_good_crc();
  55. }
  56.  
  57. void
  58. repeat_character(unsigned char ch, unsigned short n)
  59. {
  60.         unsigned short i;
  61.         for (i = 0; i < n; i++)
  62.                 text[i] = ch;
  63.         text[n] = 0;
  64. }
  65.  
  66. void
  67. update_good_crc(unsigned short ch)
  68. {
  69.         unsigned short i, v, xor_flag;
  70.  
  71.         /* Align test bit with leftmost bit of the message byte. */
  72.         v = 0x80;
  73.  
  74.         for (i = 0; i < 8; i++) {
  75.                 if (good_crc & 0x8000)
  76.                         xor_flag = 1;
  77.                 else
  78.                         xor_flag = 0;
  79.                 good_crc <<= 1;
  80.  
  81.                 if (ch & v)
  82.                         /*
  83.                          * Append next bit of message to end of CRC if it is not zero. The zero bit
  84.                          * placed there by the shift above need not be changed if the next bit of
  85.                          * the message is zero.
  86.                          */
  87.                         good_crc++;
  88.  
  89.                 if (xor_flag)
  90.                         good_crc ^= poly;
  91.  
  92.                 /* Align test bit with next bit of the message byte. */
  93.                 v >>= 1;
  94.         }
  95. }
  96.  
  97. void
  98. augment_message_for_good_crc(void)
  99. {
  100.         unsigned short i, xor_flag;
  101.  
  102.         for (i = 0; i < 16; i++) {
  103.                 if (good_crc & 0x8000)
  104.                         xor_flag = 1;
  105.                 else
  106.                         xor_flag = 0;
  107.                 good_crc <<= 1;
  108.  
  109.                 if (xor_flag)
  110.                         good_crc ^= poly;
  111.         }
  112. }
  113.  
  114. void
  115. update_bad_crc(unsigned short ch)
  116. {
  117.         /*
  118.         * Based on code found at
  119.         * http://www.programmingparadise.com/utility/crc.html
  120.         */
  121.  
  122.         unsigned short i, xor_flag;
  123.  
  124.         /*
  125.          * Why are they shifting this byte left by 8 bits??
  126.          * How do the low bits of the poly ever see it?
  127.          */
  128.         ch <<= 8;
  129.  
  130.         for (i = 0; i < 8; i++) {
  131.                 if ((bad_crc ^ ch) & 0x8000)
  132.                         xor_flag = 1;
  133.                 else
  134.                         xor_flag = 0;
  135.                 bad_crc <<= 1;
  136.                 if (xor_flag)
  137.                         bad_crc ^= poly;
  138.                 ch <<= 1;
  139.         }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement