473,395 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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?
Expand|Select|Wrap|Line Numbers
  1.     inline void byteUpdate(unsigned char data);
  2.     void addCrc(unsigned char* start);
  3.     unsigned char byteCrc[256];
  4.     unsigned char shftRgstr;
  5.     unsigned char k;
  6.     unsigned short i;
  7.     unsigned short j;
  8.  
  9.  
  10.  // CRC generator polynomial x^5+x^2+1 represented in a byte, starting
  11. // from the most significant bit, excluding the first bit
  12.  
  13. const unsigned char Aal2CpsPoly = 0x28; 
  14.  
  15. // Below table generated is used in calculating the CRC 
  16. {
  17.   for (i=0; i<256; i++)
  18.   {
  19.     byteCrc[255-i] = 0;
  20.  
  21.     for (j = 0xff80 - i*0x0100; j != 0x8000; j <<= 1)
  22.     {
  23.       if ((byteCrc[255-i] & 0x80) ^ ((j & 0x8000) >> 8))
  24.       {
  25.         byteCrc[255-i] <<= 1;
  26.         byteCrc[255-i] ^= Aal2CpsPoly;
  27.       } else
  28.       {
  29.         byteCrc[255-i] <<= 1;
  30.       }
  31.     }
  32.   }
  33. }
  34.  
  35.  
  36. void Aal2CpsCrc::byteUpdate(unsigned char data)
  37. {
  38.   shftRgstr = byteCrc[shftRgstr ^ data];
  39. }
  40.  
  41.  
  42. // Function used in calculating the CRC
  43. void Aal2CpsCrc::addCrc(unsigned char* start)
  44. {
  45.   shftRgstr = 0;
  46.   byteUpdate(*start++);
  47.   byteUpdate(*start++);
  48.  
  49.   for (k = (*start & 0xe0) | (0x10); k != 0x80; k <<= 1)
  50.   {
  51.     if ((shftRgstr & 0x80) ^ (k & 0x80))
  52.     {
  53.       shftRgstr <<= 1;
  54.       shftRgstr ^= Aal2CpsPoly;  
  55.     } else
  56.     {
  57.       shftRgstr <<= 1;
  58.     }
  59.   }
  60.  
  61.   *start = (*start & 0xe0) | (shftRgstr >> 3);
  62. }
  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
Aug 19 '08 #1
3 3794
weaknessforcats
9,208 Expert Mod 8TB
1) You are using non-ANS C++:
Expand|Select|Wrap|Line Numbers
  1. void Aal2CpsCrc::byteUpdate(unsigned char data)
  2. {
  3.   shftRgstr = byteCrc[shftRgstr ^ data];     //non-ANS C++
  4. }
  5.  
2) I assume you have stepped though this code exactly using your debugger. Yes?
Aug 19 '08 #2
1) You are using non-ANS C++:
Expand|Select|Wrap|Line Numbers
  1. void Aal2CpsCrc::byteUpdate(unsigned char data)
  2. {
  3.   shftRgstr = byteCrc[shftRgstr ^ data];     //non-ANS C++
  4. }
  5.  
2) I assume you have stepped though this code exactly using your debugger. Yes?

Could you please explain me, what is the impact of non-ANS C++ code in the implementation of the given algorithm?
Aug 21 '08 #3
Banfa
9,065 Expert Mod 8TB
shftRgstr = byteCrc[shftRgstr ^ data]; //non-ANS C++
[/code]
What is non-ANSI about this?

Taking the xor of 2 unsigned chars and using the result as an index into an array of unsigned chars and then assigning it to an unsigned char looks OK to me.

Original message : 0000 1101 0000 1011 0100 0000
Poly :100101
Message after appending W zeros : 0000 1101 0000 1011 0100 0000 0000 0
This is a mistake in your logic. In your original text you state that the message is 19 bits with a 5 bit CRC so it should be

Original message : 0000 1101 0000 1011 010

After appending W zeros you get

Original message : 0000 1101 0000 1011 0100 0000

You have put in too many zeros.
Aug 21 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on...
55
by: ben | last post by:
is it true that a function without an inline keyword never get inlined? If not true when is it inlined or not? ben
8
by: Jef Driesen | last post by:
I'm working on an image segmentation algorithm. An essential part of the algorithm is a graph to keep track of the connectivity between regions. At the moment I have a working implementation, but...
30
by: gaoxtwarrior | last post by:
a sort which is stable means it keeps the object's original order. A sort which is in place is stable. does anyone can explain me what is sort in place? thx.
13
by: Jon Davis | last post by:
Why is the IDisposeable implementation of WCF clients private? I'm glad that you can still Dispose() by casting or with using ( .. ) { .. } because IDisposable is indeed implemented but what was...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
2
by: Man4ish | last post by:
Hi , How we can find the cyclic path and path between two nodes using Depth first search algorithm in Graph Data Structure. Can i get Pseudo code or code doing the same.I will be very much thankful...
39
by: Juha Nieminen | last post by:
I was once taught that if some integral value can never have negative values, it's a good style to use an 'unsigned' type for that: It's informative, self-documenting, and you are not wasting half...
0
by: khatavkar | last post by:
please help me!! i have written a program to calculate 32 bit CRC(used for ETHERNET)..but its not working and giving correct output.. please let me know my mistakes.. output should be :E7 B0 51...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.