Server Help

Non-Subspace Related Coding - Crc32

Samapico - Fri Sep 24, 2010 5:29 pm
Post subject: Crc32
I implemented a CRC32 algorythm in both LabVIEW and a Fanuc industrial robot so files sent from the computer to the robot can be checked for errors.
The CRC value for a file is correct; it gives the same value as, say, WinRAR.

But....
I read everywhere about this, but somehow I can't get it to work:
If I append 32 0 bits ((u32)0) to a byte buffer, calculate the CRC of it, then replace the appended zeroes by the CRC value, the receiver should be able to read the whole thing, and get a CRC of 0.
It doesn't seem to work for me...

Is there something I understand wrong in this? Is there something in this particular implementation of the CRC that makes this property invalid? It uses 0xFFFFFFFF as initial value, and XOR's with 0xFFFFFFFF at the end... Though this is pretty standard.


Here are some sources (other than the obvious wikipedia) if you want to save up on the googling:
source 1: http://www.relisoft.com/science/crcmath.html
Quote:
There is one more trick used in packaging CRCs. First calculate the CRC for a message to which you have appended 32 zero bits. Suppose that the message had N bits, thus corresponding to degree N-1 polynomial. After appending 32 bits, it will correspond to a degree N + 31 polynomial. The top-level bit that was multiplying xN-1 will be now multiplying xN+31 and so on. In all, this operation is equivalent to multiplying the message polynomial by x32. If we denote the original message polynomial by M (x), the key polynomial by K (x) and the CRC by R (x) (remainder) we have:
M * x32 = Q (x) * K (x) + R (x)
Now add the CRC to the augmented message and send it away. When the recipient calculates the CRC for this sum, and there was no transmission error, he will get zero. That's because:
M * x32 + R (x) = Q (x) * K (x) (no remainder!)
You might think I made a sign mistake--it should be -R (x) on the left. Remember, however, that in arithmetic modulo 2 addition and subtraction are the same!


source 2: http://www.repairfaq.org/filipg/LINK/F_crc_v34.html#CRCV_003

source 3: http://marcel.wanda.ch/Tutorial/CRC/CyclicRedundancyCheck

My implementation is a translated version of this VB code:
http://www.vbaccelerator.com/home/vb/code/libraries/CRC32/article.asp
It was the closest to the language used in the Fanuc robot, so I used that. Shift registers are a pain in vb6... but these fanuc robots have no shift registers, no XOR operator, no way to define hexadecimal values, and no way to define unsigned values tongue.gif
Dr Brain - Fri Sep 24, 2010 10:50 pm
Post subject:
I think to get the CRC to come out to zero you have to append the actual CRC (not an XORed one). I'm not sure if initial value matters, since you're using the same initial on both computations.
Samapico - Mon Sep 27, 2010 9:07 am
Post subject:
The attached screenshot shows what I'm doing:

1) The file is closed after the data has been written to it
2) The file is reopened (I need to do that since I open it as a 'file', while it was opened as a 'config file' before)
3) I scroll to the end of the file
4) I get the current position (to go back there later; let's call this END_POS)
5) I write a u32 0
6) I close the file, and calculate the CRC32 of it (Let's call it CRC_A)

7) I open the file once again
icon_cool.gif I scroll to END_POS to replace the four zero-bytes by CRC_A
9) I write CRC_A in the file
10) I check the CRC once again, and this one should be 0... I'm trying it both with initial value of 0 or 0xFFFFFFFF (default), and I even tried xor'ing the output. None of these outputs are equal to CRC_A
Samapico - Mon Sep 27, 2010 9:33 am
Post subject:
Here's another simple test, with the values I got at runtime. Using "testtesttest" as data string.

Edit: outputs on the screenshot are bypassing the final XOR in the CRC32 function... Outputs with the XOR as it should:
0x44608E84
0xBC9826D4
0x5A5CAB57
Dr Brain - Mon Sep 27, 2010 9:59 am
Post subject:
Are you sure you're doing the CRC calculation correctly? For 'testtesttest' I get 0x44608E84, not the value you've displayed.
Samapico - Mon Sep 27, 2010 10:37 am
Post subject:
Read my edit ;D
Samapico - Mon Sep 27, 2010 11:13 am
Post subject:
This guy:
http://cs.nju.edu.cn/yangxc/dcc_teach/fcs-calc.pdf

says something about reversing the bits and bytes and crap... I'll try it out.

It's kind of confusing cause all the info I can find about this is related to serial communication... so there's a lot of inversions and crap.
Dr Brain - Mon Sep 27, 2010 11:17 am
Post subject:
http://zorc.breitbandkatze.de/crc.html

1.) The CRC has to be appended. This means testtesttest into testtesttest<CRC> not testtesttest\0\0\0\0 into testtesttest<CRC>
2.) The CRC to be appended can't be XORed or reversed.
3.) The data bytes can't be reversed if you want to append things. If you want to keep the reversal, prepend the bytes (in reverse order) instead.
4.) Initial value doesn't matter.
Samapico - Mon Sep 27, 2010 11:31 am
Post subject:
?

Were you actually able to compute a CRC of 0 with that thing?
I tried a couple of things, no luck
Samapico - Mon Sep 27, 2010 11:42 am
Post subject:
OHH!

it worked

infinite thanks Brain, I owe you an e-beer
Samapico - Mon Sep 27, 2010 11:56 am
Post subject:
Here's how I did it : <screenshot attached>

So I get to keep the IEEE standard CRC32 algorithm, only thing I need to do is invert the resulting CRC on the robot's side, or simply expect -1 (0xFFFFFFFF)
L.C. - Tue Oct 05, 2010 6:38 am
Post subject:
Samapico wrote:
Here's how I did it : <screenshot attached>

So I get to keep the IEEE standard CRC32 algorithm, only thing I need to do is invert the resulting CRC on the robot's side, or simply expect -1 (0xFFFFFFFF)
Fixed.
Samapico - Tue Oct 05, 2010 7:48 am
Post subject:
Fix ur face lulz
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group