Question on implementation on CRC (Cyclic Redundancy Code) algorithm.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 1
Reputation: mfareed is an unknown quantity at this point 
Solved Threads: 0
mfareed mfareed is offline Offline
Newbie Poster

Question on implementation on CRC (Cyclic Redundancy Code) algorithm.

 
0
  #1
Aug 21st, 2008
Can somebody help me with the CRC (Cyclic Redundancy Code) algorithm implementation given below:

There is a three byte packet header.

<--------1st byte-----><--------2nd byte-----><--------3rd byte------------->
---------------------------------------------------------------------------------------------
| 8bits | 8bits | 3 bits| 5 bits (CRC ) |
---------------------------------------------------------------------------------------------

for example: 0D 0B 40 ---> Input Data stream ( 19 bits)
04 ---> CRC ( 5bits)
Now combining 0D 0B 44 ----> Data stream after CRC (24 bits)

The function addCrc() generates a 5 bit CRC checksum, which is the part of the header. I am not getting how this checksum is implemented?
  1.  
  2. inline void byteUpdate(unsigned char data);
  3. void addCrc(unsigned char* start);
  4. unsigned char byteCrc[256];
  5. unsigned char shftRgstr;
  6. unsigned char k;
  7. unsigned short i;
  8. unsigned short j;
  9.  
  10.  
  11. // CRC generator polynomial x^5+x^2+1 represented in a byte, starting
  12. // from the most significant bit, excluding the first bit
  13.  
  14. const unsigned char Aal2CpsPoly = 0x28;
  15.  
  16. // Below table generated is used in calculating the CRC
  17. {
  18. for (i=0; i<256; i++)
  19. {
  20. byteCrc[255-i] = 0;
  21.  
  22. for (j = 0xff80 - i*0x0100; j != 0x8000; j <<= 1)
  23. {
  24. if ((byteCrc[255-i] & 0x80) ^ ((j & 0x8000) >> 8))
  25. {
  26. byteCrc[255-i] <<= 1;
  27. byteCrc[255-i] ^= Aal2CpsPoly;
  28. } else
  29. {
  30. byteCrc[255-i] <<= 1;
  31. }
  32. }
  33. }
  34. }
  35.  
  36.  
  37. void Aal2CpsCrc::byteUpdate(unsigned char data)
  38. {
  39. shftRgstr = byteCrc[shftRgstr ^ data];
  40. }
  41.  
  42.  
  43. // Function used in calculating the CRC
  44. void Aal2CpsCrc::addCrc(unsigned char* start)
  45. {
  46. shftRgstr = 0;
  47. byteUpdate(*start++);
  48. byteUpdate(*start++);
  49.  
  50. for (k = (*start & 0xe0) | (0x10); k != 0x80; k <<= 1)
  51. {
  52. if ((shftRgstr & 0x80) ^ (k & 0x80))
  53. {
  54. shftRgstr <<= 1;
  55. shftRgstr ^= Aal2CpsPoly;
  56. } else
  57. {
  58. shftRgstr <<= 1;
  59. }
  60. }
  61.  
  62. *start = (*start & 0xe0) | (shftRgstr >> 3);
  63. }
As per my understanding the CRC has been calculated by division method. The divisor ("generator polynomial") is x^5+x^2+1 (100101). Dividend (Input Data stream) is 0D 0B 40 ( 0000 1101 0000 1011 0100 0000)

The width(W) of a poly is the actual bit position of the
highest bit. For example, the width of 10011 is 4, not 5. For the
Having chosen a poly, we can proceed with the calculation. This is
simply a division (in CRC arithmetic) of the message by the poly. The
only trick is that W zero bits are appended to the message before the
CRC is calculated. Thus we have:

Original message : 0000 1101 0000 1011 0100 0000
Poly :100101
Message after appending W zeros : 0000 1101 0000 1011 0100 0000 0000 0

Now we simply divide the augmented message by the poly using CRC

____ 00101111111011110_______________________
100101) 1101 0000 1011 0100 0000 0000 0 Augmented message (0000 1101 0000 1011 0100 0000 + 00000)
1001 01
-----------
001010
000000
----------
.
.
.
.
and so on
.
.
001100
000000
-----------
01100 = Remainder = CRC
Note: The sum uses CRC addition(Binary Arithmetic with No Carries, that is XOR operation)

The manually calculated CRC(01100) is not matching with the calculated CRC (04 - 00100) from the above algorithm. So can somebody help me in resolving this discrepancies?


Regards,
Mohammed Fareed
----------------------------------------------------------------------------------------------------------------------------------------------------
Last edited by Ancient Dragon; Aug 22nd, 2008 at 12:44 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC