我需要读取PNG文件并解释存储在其中的所有信息,并以人类可读的格式打印.在处理PNG时,我理解它使用CRC-32为每个块生成校验和.但我无法理解PNG文件规范网站上提到的以下信息:PNG使用的多项式为:x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
以下链接供参考:http: //www.w3.org/TR/PNG/
有人可以帮我理解这个吗?
http://en.wikipedia.org/wiki/Computation_of_CRC?
根据 wiki 中的 CRC 列表,这个多项式(又名 AUTODIN II 多项式)是最常用的多项式之一。CRC-32-IEEE 802.3 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
用于(以太网、V.42、MPEG-2、PNG、POSIX cksum、Arj、Lha32、Rar、Zip 等..)
用标记为的权力重写^:
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.
Run Code Online (Sandbox Code Playgroud)
所以你可以阅读 cksum 的来源,例如这里
http://www.opensource.apple.com/source/file_cmds/file_cmds-188/cksum/crc32.c
32 位 AutoDIN-II CRC 基于以下移位寄存器参考模型构建。
多项式: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
先输入数据位0
Run Code Online (Sandbox Code Playgroud)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.
这是在zlib中实现的CRC-32算法.如果您可以使用该库,请不要实现自己的.
[编辑]:如何使用zlib中的CRC计算器(从zlib docs中提取的C中的示例).
#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();
Run Code Online (Sandbox Code Playgroud)
如果您有想要获取CRC的数据块,则不需要while循环; 您只需获取初始值(首先分配给crc上面的值),然后计算您拥有的数据的值(第二次赋值crc).