CRC for PNG file format
I need to read a PNG file and in开发者_开发问答terpret 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?
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.
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
).
Every x term refers to a 1 in the binary representation of 0xedb88320, which is 11101101 10111000 10000011 00100000. The number on the left (least-significant) end, 1, is the (coefficient of the) constant (x^0) term. The next number from the left, 1, is the coefficient of the x term. The next number, 1, is the coefficient of the x^2 term. The next number, 0, is the coefficient of the x^3 term (which is absent because 0*x^3 = 0). And so on. There is an implied 1 to the right of the right (most-significant) end that is the coefficient of the x^32 term.
精彩评论