tags:

views:

280

answers:

3

Hi All,

I need to read a PNG file and interpret all the information stored in it and print it in human readable format. While working on PNG, i understood that it uses CRC-32 for generating checksum for each chunk. But I could not understand the following information mentioned on the PNG file specification site: The polynomial used by PNG is: x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1

Here is the link for reference: http://www.w3.org/TR/PNG/

Can anyone please help me in understanding this?

Regards, darkie

+2  A: 

http://en.wikipedia.org/wiki/Computation_of_CRC ?

According to list of CRCs in wiki, this polynomial (aka AUTODIN II polynomial ) is one of the most used. CRC-32-IEEE 802.3 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1

Used in (Ethernet, V.42, MPEG-2, PNG, POSIX cksum, Arj, Lha32, Rar, Zip, and more..)

Rewritted with power marked by ^:

 x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1.

So you can read source of cksum, e.g. here

http://www.opensource.apple.com/source/file_cmds/file_cmds-188/cksum/crc32.c

The 32-bit AutoDIN-II CRC is built upon the following shift-register reference model.

Polynomial: g(x) = 1 + x + x^4 + x^5 + x^7 + x^8 + x^10 + x^11 + x^12 + x^1 + x^22 + x^23 + x^26 + x^32

Input data bit 0 first

Leading-zero checking is performed by the following procedure:

 1. The crc register is initialized to 0xffffffff, not zero.

 2. When a crc is appended, the 32 bits of the crc are inverted.

 3. When checking a good message with an appended crc, the register
    will return to the fixed value of 0xdebb20e3, rather than zero.
osgx
A: 

Have you read wikipedia articles about CRCs? There is no unified CRC format, every 'format' is built upon a polynomial like this.

I'm not very familiar with calculating CRCs by hand either, but what you're looking at is in fact the format which is used in POSIX. I'm sure there are tons of useful implementations of it.

svens
+1  A: 

That's the CRC-32 algorithm implemented in zlib. Please don't implement your own when you can use that library instead.


[EDIT]: How to use the CRC calculator from zlib (an example in C extracted from the zlib docs).

#include <zlib.h>

uLong crc = crc32(0L, Z_NULL, 0);

while (read_buffer(buffer, length) != EOF) {
   crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();

If you have the block of data that you want to get the CRC for, you don't need that while loop; you just get the initial value (first assignment to crc above) and then compute the value over the data that you have (second assignment to crc).

Donal Fellows
Hi Donald,I need to check if the checksum matches for each chunk with my own computed checksum value. What should be the procedure in this case?
darkie15